MedicalNet icon indicating copy to clipboard operation
MedicalNet copied to clipboard

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

Open ni-wei opened this issue 6 years ago • 5 comments

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

ni-wei avatar Sep 02 '19 11:09 ni-wei

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.

cshwhale avatar Sep 02 '19 11:09 cshwhale

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

ni-wei avatar Sep 02 '19 11:09 ni-wei

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.

CCCalcifer avatar Dec 19 '23 14:12 CCCalcifer

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?

t0tl avatar Dec 22 '23 15:12 t0tl

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.

CCCalcifer avatar Dec 23 '23 15:12 CCCalcifer