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

Missing key(s) in state_dict error when testing the pretrained models #24

Open
ni-wei opened this issue Sep 2, 2019 · 5 comments
Open

Comments

@ni-wei
Copy link

ni-wei commented Sep 2, 2019

Hi,

thank you for sharing your excellent work!

I wanna test your pre-trained lung segmentation model (this release) with my CT data, and I encountered the following error when running "python test.py --gpu_id 0 --resume_path pretrain/resnet_50_23dataset.pth --img_list data/val.txt":

...
loading pretrained model pretrain/resnet_50_23dataset.pth
Traceback (most recent call last):
  File "test.py", line 90, in <module>
    net.load_state_dict(checkpoint['state_dict'])
  File "_<my_conda_dir>_/envs/torch3.7.0/lib/python3.7/site-packages/torch/nn/modules/module.py", line 719, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for DataParallel:
        Missing key(s) in state_dict: "module.conv_seg.0.weight", "module.conv_seg.0.bias", "module.conv_seg.1.weight", "module.conv_seg.1.bias", "module.conv_seg.1.running_mean", "module.conv_seg.1.running_var", "module.conv_seg.3.weight", "module.conv_seg.4.weight", "module.conv_seg.4.bias", "module.conv_seg.4.running_mean", "module.conv_seg.4.running_var", "module.conv_seg.6.weight".

The same error persists when I replace "--resume_path pretrain/resnet_50_23dataset.pth" with "--resume_path pretrain/resnet_50.pth". It does not work by adding "--model resnet --model_depth 50 --resnet_shortcut B".

However, it works when I use another pre-trained model instead: "--resume_path trails/resnet_50_epoch_200_batch_0.pth.tar".

Would appreciate your advice on how to make the pre-trained models work. My environment:

  • Python 3.7.0
  • PyTorch-0.4.1 (installed by conda)
  • CUDA Version 9.0 (conda package)

Thanks!
Wei

@cshwhale
Copy link
Collaborator

cshwhale commented Sep 2, 2019

Hi, you can't get the segmentation result when using the segmentation test code in the situation you mentioned since "resnet_50_23dataset.pth" and "resnet_50.pth" are just backbones without segmentation head.

@ni-wei
Copy link
Author

ni-wei commented Sep 2, 2019

Alright. Could you kindly show me how to add necessary segmentation heads to the backbone?

@CCCalcifer
Copy link

Alright. Could you kindly show me how to add necessary segmentation heads to the backbone?

you need to change the 'shortcut' parameter in Resnet class settings when you use different depth of resnet.

At the same time, you need to comment out the self.conv_seg variable when init the class.

@t0tl
Copy link

t0tl commented Dec 22, 2023

Alright. Could you kindly show me how to add necessary segmentation heads to the backbone?

you need to change the 'shortcut' parameter in Resnet class settings when you use different depth of resnet.

At the same time, you need to comment out the self.conv_seg variable when init the class.

I have been struggling with this myself, if I comment out the self.conv_seg then I can't use the pretrained resnet_50_epoch model right? Also, I have an issue with the resnet_50_epoch model where it has 2 classes specified but I need 1 class only. Any ideas how to approach this?

@CCCalcifer
Copy link

Alright. Could you kindly show me how to add necessary segmentation heads to the backbone?

you need to change the 'shortcut' parameter in Resnet class settings when you use different depth of resnet.
At the same time, you need to comment out the self.conv_seg variable when init the class.

I have been struggling with this myself, if I comment out the self.conv_seg then I can't use the pretrained resnet_50_epoch model right? Also, I have an issue with the resnet_50_epoch model where it has 2 classes specified but I need 1 class only. Any ideas how to approach this?

I haven't test it on resnet50, but on resnet 10, resnet 18 and resnet34, comment out the self.conv_seg worked well. Attention you need to change to 'shortcut'. I don't have any idea of the following question if you only need you class maybe this pretrained model do not fit your needs.

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