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