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:

  
    
    
    [solved] KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict' 
  
  
    I am getting the following error while trying to load a saved model. 
KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict' 
This is the function I am using to load a saved model. (as suggested in http://pytorch.org/docs/notes/serialization.html#recommend-saving-models) 
def load_model_states(model, tag):
    """Load a previously saved model states."""
    filename = os.path.join(args.save_path, tag)
    with open(filename, 'rb') as f:
        model.load_state_dict(torch.loa…
  


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’