video-classification-3d-cnn-pytorch icon indicating copy to clipboard operation
video-classification-3d-cnn-pytorch copied to clipboard

KeyError: 'unexpected key "module.layer1.0.downsample.0.weight" in state_dict'

Open xiadingZ opened this issue 6 years ago • 14 comments

when I use resnext-101 model and resnet-152 model, it throws this error. I can seccessfully use resnet-18 model. How to solve this problem?

xiadingZ avatar Dec 20 '17 12:12 xiadingZ

Could you tell me your settings of options in the execution?

kenshohara avatar Jan 04 '18 06:01 kenshohara

@kenshohara I also meet same problem when i use resnext-101 model The options in my execution is same as default, and i only change the input and output dir

CCV-Edward avatar Jan 05 '18 01:01 CCV-Edward

@kenshohara I only change the "model_name", "model_depth" and "model" options

xiadingZ avatar Jan 05 '18 02:01 xiadingZ

I could run the code by changing the 'shortcut' to 'B' in resnext-101 @xiadingZ sorry to bother u @kenshohara

CCV-Edward avatar Jan 05 '18 02:01 CCV-Edward

@CCV-Edward I'm glad the problem solved!

@xiadingZ As mentioned by @CCV-Edward , you should set --resnet_shortcut B to use the models except for resnet-18 and -34.

kenshohara avatar Jan 05 '18 05:01 kenshohara

@kenshohara

I am getting similar error for resnet-34 cpu model. Please let me know needs to be done to rectify this.

python3 main.py --input ./input --video_root ./videos --output ./output.json --model ./resnet-34-kinetics-cpu.pth --mode feature --no_cuda

KeyError: 'unexpected key "module.conv1.weight" in state_dict'

yash-bhat avatar Mar 01 '18 09:03 yash-bhat

Related to this issue, resnet-34-kinetics-cpu.pth includes some problems. Please download the latest file and try it again.

kenshohara avatar Mar 02 '18 02:03 kenshohara

@kenshohara

well i tried with the latest resnet-34-kinetics-cpu.pth! Can you please provide me the latest cpu model file? Currenty i do not have gpu support.

yash-bhat avatar Mar 02 '18 08:03 yash-bhat

When did you download the file? I update it 7 days ago. If you tried the latest file, my code or model still include some bugs.

kenshohara avatar Mar 02 '18 08:03 kenshohara

@kenshohara Yes. I used the same(updated on Feb23)!

Do you think solution in below helps. https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686

Suggestion in the above link is like this, if you saved the model using nn.DataParallel, which stores the model in module, and now trying to load it without DataParallel:

original saved file with DataParallel

state_dict = torch.load('myfile.pth.tar')

create new OrderedDict that does not contain module.

from collections import OrderedDict new_state_dict = OrderedDict() for k, v in state_dict.items(): name = k[7:] # remove module. new_state_dict[name] = v

load params

model.load_state_dict(new_state_dict)

yash-bhat avatar Mar 02 '18 10:03 yash-bhat

I removed nn.DataParallel from the latest file, and if no_cuda == True, nn.DataParallel does not be used. So I think there are other problems. But I currently confirmed the problem in my environment. Please wait a more few days.

kenshohara avatar Mar 05 '18 06:03 kenshohara

I'm sorry for the long delay. I executed the code using the same options as yours. But I can execute correctly in my environment. I'm not sure what is the problem...

kenshohara avatar Apr 03 '18 01:04 kenshohara

@yash-bhat Did you solve the problem? I encountered the exact same issue as you, and I solved it with the help of this thread, by adding the argument strict=False to model.load_state_dict in line 28, main.py.

Hope this helps.

llafcode avatar Oct 16 '19 11:10 llafcode

I can declare that you didn't search and examine the code carefully. Actually I used resnet101 these days, and the same problems still exist: image

poincarelee avatar Jun 27 '22 09:06 poincarelee