KeyError: 'unexpected key "cnn.classifier.1.weight" in state_dict'
Cadene opened this issue · 2 comments
Hello Fartash,
Thanks for your great code. However, I encounter a little problem when I want to load a pretrained model. The keys of the pretrained model (model_best.pth.tar) do not match with the architecture.
The keys from the pretrained model:
state = torch.load('coco_vse++_vggfull_restval_finetune/model_best.pth.tar')
print(state['model'][0].keys()) # image encoder with vgg16
> odict_keys(['cnn.features.module.0.weight', 'cnn.features.module.0.bias',
'cnn.features.module.2.weight', ...
'cnn.features.module.34.weight', 'cnn.features.module.34.bias',
'cnn.classifier.1.weight', 'cnn.classifier.1.bias',
'cnn.classifier.4.weight', 'cnn.classifier.4.bias',
'fc.weight', 'fc.bias']
)
The keys from the image encoder architecture printed from here:
print(self.state_dict().keys())
> odict_keys(['cnn.features.module.0.weight', 'cnn.features.module.0.bias',
'cnn.features.module.2.weight', ...
'cnn.features.module.34.weight', 'cnn.features.module.34.bias',
'cnn.classifier.0.weight', 'cnn.classifier.0.bias',
'cnn.classifier.3.weight', 'cnn.classifier.3.bias',
'fc.weight', 'fc.bias']
)
This error suggests that you had a layer (which is not a nn.Linear
) in the first position of your nn.Sequential
, but in the torchvision implementation of vgg19 that you are using, it is not the case, and even here you are just removing the last fully connected layer.
Any help would be appreciated, thanks :)
Thanks Remi, for using the code and reporting this issue. This is due to a non-backward-compatible change introduced by pytorch/vision@989d52a. I'm pushing a fix now.
Awesome thanks! I was not aware of this update.