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

Hi I am trying to perform transfer learning on a resnet18 model but i keep getting an error when loading the model.
I tried to use the solution here to remove the multiple modules but that doesn’t seem to work for me:

This is my code for editing the last fc layer:

class ModifiedResNet18Model(torch.nn.Module):
def init(self):
super(ModifiedResNet18Model, self).init()

	model = models.resnet18(pretrained=True)#squeezenet1_1
	modules = list(model.children())[:-1]      # delete the last fc layer.
            model = nn.Sequential(*modules)
	self.features = model
	print("start pruning:")
	for param in self.features.parameters():
		param.requires_grad = False

	self.fc = nn.Sequential(
		nn.Dropout(),
		nn.Linear(512,400),
		nn.ReLU(inplace=True),
                    nn.Dropout(),
		nn.Linear(400,256),
		nn.ReLU(inplace=True),
		nn.Linear(256, 256))
	

def forward(self, x):
	x = self.features(x)
	x = x.view(x.size(0), -1)
	x = self.fc(x)#self.classifier(x)
	return x

and for saving after finetuning and loading it :
model.cpu()
torch.save(model.state_dict(), ‘resnet18_model.tar’)

model = ModifiedResNet18Model().cuda()
model.load_state_dict(torch.load(‘resnet18_model.tar’))

This is the error I get:

KeyError: ‘unexpected key “module.module.module.conv1.weight” in state_dict’

after renaming the state_dict and removing all the modules I still get :

KeyError: ‘unexpected key “conv1.weight” in state_dict’

Any suggestions on what might be the problem or what I could do ?

Which PyTorch version are you using?
I tried your code on my machine and it worked without an error.

I am using 0.2.0 which version did you use?

Could you update it to the latest release?
There were a lot of bug fixes.
You can find the instructions on the website.

I’m using a version which I compiled a while ago from master (0.4.0a0+5eefe87).
Let me know, if the error disappeared!

Thanks. Updating to the latest version seems to have solved my problem

@ptrblck, I am trying to resume my training on ImageNet using example code

My pytorch version is:

pytorch-0.4.1 |py35_cuda8.0.61_cudnn7.1.2_1

Error I am encountering:

RuntimeError: Error(s) in loading state_dict for ResNet: Missing key(s) in state_dict: "conv1.weight", "bn1.running_mean", "bn1.bias",  ..

Unexpected key(s) in state_dict: "module.conv1.weight", "module.bn1.weight", "module.bn1.bias", "module.bn1.running_mean", "module.bn1.running_var", "module.layer1.0.conv1.weight" ..


Could you help me out?

It looks as if you’ve trained your model using nn.DataParallel and try to load it in a plain model.
Have a look at this thread for some solutions.

2 Likes

I came across this error with repeating “module.module.module.conv1.weight” when loading a checkpoint in Jupyter notebook. Later I found out that I accidentally ran the following cell more than once:

model = torch.nn.DataParallel(model)

This line appears to insert the “module” tag to the weights’ names every time you run it. Once I fix this, the error is gone.