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

pgadosey: <p>Hi I am trying to perform transfer learning  on a resnet18 model but i keep getting an error when loading the model.<br>
I tried to use the solution here to remove the multiple modules but that doesn’t seem to work for me:</p><aside class="quote quote-modified" data-post="1" data-topic="1686">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/48/248_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686">[solved] KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict'</a> 
  </div>
  <blockquote>
    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 <a href="http://pytorch.org/docs/notes/serialization.html#recommend-saving-models" rel="noopener nofollow ugc">http://pytorch.org/docs/notes/serialization.html#recommend-saving-models</a>) 
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…
  </blockquote>
</aside>
<p>
This is my code for editing the last fc layer:</p>
<p>class ModifiedResNet18Model(torch.nn.Module):<br>
def <strong>init</strong>(self):<br>
super(ModifiedResNet18Model, self).<strong>init</strong>()</p>
<pre><code>	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
</code></pre>
<p>and for saving after finetuning and loading it :<br>
model.cpu()<br>
torch.save(model.state_dict(), ‘resnet18_model.tar’)</p>
<p>model = ModifiedResNet18Model().cuda()<br>
model.load_state_dict(torch.load(‘resnet18_model.tar’))</p>
<p>This is the error I get:</p>
<p>KeyError: ‘unexpected key “module.module.module.conv1.weight” in state_dict’</p>
<p>after renaming the state_dict and removing all the modules I still get :</p>
<p>KeyError: ‘unexpected key “conv1.weight” in state_dict’</p>

pgadosey: <p>Any suggestions on what might be the problem or what I could do ?</p>

ptrblck: <p>Which PyTorch version are you using?<br>
I tried your code on my machine and it worked without an error.</p>

pgadosey: <p>I am using 0.2.0 which version did you use?</p>

ptrblck: <p>Could you update it to the latest release?<br>
There were a lot of bug fixes.<br>
You can find the instructions <a href="http://pytorch.org/">on the website</a>.</p>
<p>I’m using a version which I compiled a while ago from master (<code>0.4.0a0+5eefe87</code>).<br>
Let me know, if the error disappeared!</p>

pgadosey: <p>Thanks. Updating to the latest version  seems to have solved my problem</p>

raoanonymous: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>, I am trying to resume my training on ImageNet using <a href="https://github.com/pytorch/examples/tree/master/imagenet" rel="noopener nofollow ugc">example code</a></p>
<p>My pytorch version is:</p>
<blockquote>
<p>pytorch-0.4.1              |py35_cuda8.0.61_cudnn7.1.2_1</p>
</blockquote>
<p>Error I am encountering:</p>
<pre><code class="lang-auto">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" ..


</code></pre>
<p>Could you help me out?</p>

ptrblck: <p>It looks as if you’ve trained your model using <code>nn.DataParallel</code> and try to load it in a plain model.<br>
Have a look at <a href="https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686/2">this thread</a> for some solutions.</p>

ltong1130ztr: <p>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:</p>
<pre><code class="lang-auto">model = torch.nn.DataParallel(model)
</code></pre>
<p>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.</p>