Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WARNING - The model and loaded state dict do not match exactly UserWarning:DeprecationWarning:pretrained is deprecated,please use “init_cfg” instead #5177

Closed
Ian-Wong-ion opened this issue May 17, 2021 · 8 comments

Comments

@Ian-Wong-ion
Copy link

Ian-Wong-ion commented May 17, 2021

UserWarning: init_cfg without layer key, if you do not define override key either, this init_cfg will do nothing
'init_cfg without layer key, if you do not define override'
WARNING - The model and loaded state dict do not match exactly UserWarning:DeprecationWarning:pretrained is deprecated,please use “init_cfg” instead

@Ian-Wong-ion Ian-Wong-ion changed the title During the training, the memory footprint increased WARNING - The model and loaded state dict do not match exactly May 17, 2021
@Ian-Wong-ion Ian-Wong-ion changed the title WARNING - The model and loaded state dict do not match exactly WARNING - The model and loaded state dict do not match exactly UserWarning:DeprecationWarning:pretrained is deprecated,please use “init_cfg” instead May 17, 2021
@K0mp0t
Copy link

K0mp0t commented May 20, 2021

17 days ago new mmdetection version was released (https://github.com/open-mmlab/mmdetection/releases/tag/v2.12.0 <- second list item in Backwards Incompatible Changes).
To unify parameters initialization you have to manually initialize pre-trained network weights (and other things). Add init_cfg to your config as shown below:

model = dict(
    type='FasterRCNN',
    backbone=dict(
        type='ResNet',
        depth=50,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        frozen_stages=1,
        norm_cfg=dict(type='BN', requires_grad=False),
        norm_eval=True,
        style='caffe',
        init_cfg=dict(type='Pretrained', checkpoint='open-mmlab://detectron2/resnet50_caffe'),), # <- you shuold add this
    ....))

Actually, you are supposed to add override to init_cfg, but it works okay this way. And then, in your train script add the following:

from mmdet.apis import train_detector
from mmdet.models import build_detector
import os.path as osp


model = build_detector(cfg.model, test_cfg=cfg.get('test_cfg'), train_cfg=cfg.get('train_cfg'))
model.init_weights() # <- this is what you should add

model.CLASSES = datasets[0].CLASSES

mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))
train_detector(model, datasets, cfg, distributed=False, validate=True)

@EdyQiu
Copy link

EdyQiu commented May 24, 2021

'open-mmlab://detectron2/resnet50_caffe'

I have the same issue and thanks for your reply!
I modified the code according to your suggestions, and the warning "pretrained is deprecated,please use “init_cfg” instead"
disappeared, but the warning" init_cfg without layer key, if you do not define override key either, this init_cfg will do nothing" remained.

I guess I need to specify" layer key" in my " init_cfg", but I have totally no clue about how to do that.
Is there any update document mentioning that?

@K0mp0t
Copy link

K0mp0t commented May 24, 2021

You have to add an override key as the warning says. But I have no clue how to do it for pretrained part of init_cfg. I've checked official docs and configs and have found nothing (even mmdet's github configs have depreciated pretrained key in model dict).

In other init_cfgs override is used for overriding init_cfg layers, but our case has no layers (if we use only pretrained).
Looks like, current situation is the result of unifying model initializing and will be fixed.

In either case,

the warning" init_cfg without layer key, if you do not define override key either, this init_cfg will do nothing"

doesn't harm training and validating models, so just ignore it.

@EdyQiu
Copy link

EdyQiu commented May 24, 2021

You have to add an override key as the warning says. But I have no clue how to do it for pretrained part of init_cfg. I've checked official docs and configs and have found nothing (even mmdet's github configs have depreciated pretrained key in model dict).

In other init_cfgs override is used for overriding init_cfg layers, but our case has no layers (if we use only pretrained).
Looks like, current situation is the result of unifying model initializing and will be fixed.

In either case,

the warning" init_cfg without layer key, if you do not define override key either, this init_cfg will do nothing"

doesn't harm training and validating models, so just ignore it.

I see I see.
I just used "init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50'),),"
I am having a test on VOC data set, if the performance is still close to the benchmark, the msg should have no effect indeed then.

It is hard for beginners like me to figure out how to modify the init_cfg without official documents and example, and all I need is just to use a pretrained model. The most related official doc maybe open-mmlab/mmcv#780, but still, it is not helpful for me.

Thanks a lot for your feedback!

@K0mp0t
Copy link

K0mp0t commented May 24, 2021

The warning says two things:

  1. Your init_cfg has no layer key (which you don't need because you just want to get pretrained model weights)
  2. Because of missing layer key you should add an override key (or it will be ignored, but it won't if it's a pretrained dict).
    Layer key specifies layers that are in PyTorch or MMCV and override key specifies sub-modules with different initialization (these aren't in BaseModule).

So this warning is just a result of little logic misconduct (the warning shouldn't be shown for pretrained init_cfg dicts)

@EdyQiu
Copy link

EdyQiu commented May 24, 2021

The warning says two things:

  1. Your init_cfg has no layer key (which you don't need because you just want to get pretrained model weights)
  2. Because of missing layer key you should add an override key (or it will be ignored, but it won't if it's a pretrained dict).
    Layer key specifies layers that are in PyTorch or MMCV and override key specifies sub-modules with different initialization (these aren't in BaseModule).

So this warning is just a result of little logic misconduct (the warning shouldn't be shown for pretrained init_cfg dicts)

Thanks for your further explanation, right now I more or less understand what happens.

So if I want to have no pretrained model (random initialization), I should use "init_cfg=dict(type='Pretrained', checkpoint= None),)," ?
And if I want to finetune the model (load from checkpoint), the init_cfg is not related then, since I can use "load from = xxx.pth" and use "freeze stage", "paramwise_cfg" to define which parts of the model should be fixed.

@K0mp0t
Copy link

K0mp0t commented May 24, 2021

If you don't want to have pretrained model weights (e.g. for ResNet) you can just remove dict with type='Pretrained' from init_cfg (or even init_cfg if it contains only pretrained dict).

And if I want to finetune the model (load from checkpoint), the init_cfg is not related then, since I can use "load from = xxx.pth" and use "freeze stage", "paramwise_cfg" to define which parts of the model should be fixed.

That's right, not sure about freeze stages and paramwise_cfg (I've never specified these), but looks right.

@EdyQiu
Copy link

EdyQiu commented May 24, 2021

If you don't want to have pretrained model (e.g. for ResNet) you can just remove dict with type='Pretrained' from init_cfg (or even init_cfg if it contains only pretrained dict).

And if I want to finetune the model (load from checkpoint), the init_cfg is not related then, since I can use "load from = xxx.pth" and use "freeze stage", "paramwise_cfg" to define which parts of the model should be fixed.

That's right, not sure about freeze stages and paramwise_cfg (I've never specified these), but looks right.

GREAT!!!! Thanks for your reply! Otherwise I hardly could continue my work!
Hope you have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants