Error in importing pretrained resnet-18

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).

It’s possible that your model download got corrupted for some reason.
Try this:

rm -rf ~/.torch

And then try creating the pre-trained model again.

I just checked and it work on my side.

I found the problem. In some other code, someone was passing a real-valued vector for the argument of bias into Conv2D. In conv.py on line 34, there is a line: if bias, that was giving me error saying that passing a non-bool value is ambiguous. To correct that, I (for a moment) had changed it to if bias not None (and then forgot about it). I corrected it now.