I am using torch 0.2.0_3. The following simple code when run in terminal gives an error.
import torch
from torchvision import models
model = models.resnet18(pretrained=True)
The error is:
KeyError: 'missing keys in state_dict: "set([\'layer3.0.conv2.bias\', \'layer3.1.conv1.bias\', \'layer2.0.downsample.0.bias\', \'layer4.1.conv2.bias\', \'layer1.0.conv2.bias\', \'layer4.0.conv2.bias\', \'layer1.1.conv1.bias\', \'layer2.1.conv1.bias\', \'layer3.1.conv2.bias\', \'layer2.1.conv2.bias\', \'layer1.1.conv2.bias\', \'layer4.0.downsample.0.bias\', \'layer4.0.conv1.bias\', \'layer2.0.conv2.bias\', \'layer3.0.downsample.0.bias\', \'conv1.bias\', \'layer1.0.conv1.bias\', \'layer3.0.conv1.bias\', \'layer2.0.conv1.bias\', \'layer4.1.conv1.bias\'])"'
Basically keys for all the bias terms are missing in the state_dict
(and are present in own_state
). I investigated the issue a bit and found that in the resnet model definition, all the bias terms are set to false. So unless I change the bias_term=True
, this error will keep occurring, but this is obviously not the correct thing to do.
The error is on this line:
missing = set(own_state.keys()) - set(state_dict.keys())
If I change it to,
missing = set(state_dict.keys()) - set(own_state)
Any suggestions? This code should not give an error since I am not doing anything but importing a pre-trained resnet18 (the error occurs with other resnet models too).