Can I change a custom resnet 18 architecture subtly and still use it in pre-trained mode?

Mona_Jalal: <p>Can I change a custom resnet 18 architecture and still use it in pre-trained = true mode? I am doing a subtle change in the architecture of a custom resnet18 and when i run it, i get the following error:<br>
This is how the custom resnet18 is called:<br>
<code> model = Resnet_18.resnet18(pretrained=True, embedding_size=args.dim_embed)</code></p>
<p>The new change in the custom resnet18:</p>
<pre><code class="lang-auto">self.layer_attend1 =  nn.Sequential(nn.Conv2d(layers[0], layers[0], stride=2, padding=1, kernel_size=3),
                                  nn.AdaptiveAvgPool2d(1),
                                  nn.Softmax(1))
</code></pre>
<p>The output of running the model is:</p>
<pre><code class="lang-auto">/scratch3/venv/fashcomp/lib/python3.8/site-packages/torchvision/transforms/transforms.py:310: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
  warnings.warn("The use of the transforms.Scale transform is deprecated, " +
=&gt; loading checkpoint 'runs/nondisjoint_l2norm/model_best.pth.tar'
Traceback (most recent call last):
  File "main.py", line 352, in &lt;module&gt;
    main()    
  File "main.py", line 145, in main
    tnet.load_state_dict(checkpoint['state_dict'])
  File "/scratch3/venv/fashcomp/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1406, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for Tripletnet:
        Missing key(s) in state_dict: "embeddingnet.embeddingnet.layer_attend1.0.weight", "embeddingnet.embeddingnet.layer_attend1.0.bias". /scratch3/venv/fashcomp/lib/python3.8/site-packages/torchvision/transforms/transforms.py:310: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
  warnings.warn("The use of the transforms.Scale transform is deprecated, " +
=&gt; loading checkpoint 'runs/nondisjoint_l2norm/model_best.pth.tar'
Traceback (most recent call last):
  File "main.py", line 352, in &lt;module&gt;
    main()    
  File "main.py", line 145, in main
    tnet.load_state_dict(checkpoint['state_dict'])
  File "/scratch3/venv/fashcomp/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1406, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for Tripletnet:
        Missing key(s) in state_dict: "embeddingnet.embeddingnet.layer_attend1.0.weight", "embeddingnet.embeddingnet.layer_attend1.0.bias".
</code></pre>
<p>So, how can you implement small architectural changes without retraining from the scratch every time?</p>

Mona_Jalal: <p>I do not know how to apply the answer by <a class="mention" href="/u/ptrblck">@ptrblck</a> to this case</p>
<aside class="quote" data-post="6" data-topic="22379">
  <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/ptrblck/48/1823_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/missing-keys-unexpected-keys-in-state-dict-when-loading-self-trained-model/22379/6">Missing keys &amp; unexpected keys in state_dict when loading self trained model</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    Could you try to load the state_dict using model.load_state_dict(torch.load(PATH), strict=False)?
  </blockquote>
</aside>


Mona_Jalal: <p>or for example should I use this answer?</p><aside class="quote quote-modified" data-post="14" data-topic="22379">
  <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/alexander_soare/48/24720_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/missing-keys-unexpected-keys-in-state-dict-when-loading-self-trained-model/22379/14">Missing keys &amp; unexpected keys in state_dict when loading self trained model</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I also am facing the same issue. I don’t use nn.DataParallel. 
I am using a slightly modified version of [this repo] in a Kaggle notebook <a href="https://github.com/aitorzip/PyTorch-CycleGAN" rel="noopener nofollow ugc">https://github.com/aitorzip/PyTorch-CycleGAN</a>. 
Here’s how I save: 
torch.save({
                'epoch': epoch,
                'model_state_dict': netG_A2B.state_dict(),
                'optimizer_state_dict': optimizer_G.state_dict(),
                'loss_histories': loss_histories,
                }, f'netG_A2B_{epoch:03d}.pth')

Here’s how I load: 
checkp…
  </blockquote>
</aside>

<p>Can you at a high-level (not code-level) please provide some hint why this answer should work?</p>
<p>I am also loading a torch model like below:<br>
<code>checkpoint = torch.load(args.resume, encoding='latin1')</code></p>

Alexander_Soare: <p><strong>EDIT</strong> - <em>If you come across this answer just see <a class="mention" href="/u/mona_jalal">@Mona_Jalal</a> 's answer below (use model.load_state_dict(state_dict, strict=False)).</em></p>
<p><a class="mention" href="/u/mona_jalal">@Mona_Jalal</a> it’s not quite the same. It might help if you include a snippet showing exactly how you try to load your checkpoint but my guess is you’re doing something like:</p>
<pre><code class="lang-python">state_dict = torch.load('checkpoint.pth')
model.load_state_dict(state_dict)
</code></pre>
<p>In which case the fix is to put the missing keys into <code>state_dict</code>. So do this:</p>
<pre><code class="lang-python">state_dict = torch.load('checkpoint.pth')
state_dict['embeddingnet.embeddingnet.layer_attend1.0.weight'] = model.embeddingnet.embeddingnet.layer_attend1.0.weight
state_dict['embeddingnet.embeddingnet.layer_attend1.0.bias'] = model.embeddingnet.embeddingnet.layer_attend1.0.bias
model.load_state_dict(state_dict)
</code></pre>
<p>… or something like that, just making sure you’re pointing to the correct submodule. More generally you could do:</p>
<pre><code class="lang-auto">loaded_state_dict = torch.load('checkpoint.pth')
model_state_dict = model.state_dict()
for k, v in model_state_dict.items():
    if k not in loaded_state_dict:
        loaded_state_dict[k] = v
model.load_state_dict(state_dict)
</code></pre>
<p>I haven’t tested any of this just now so you may need to fiddle.</p>
<p>PS: Just a side note. You don’t need <code>pretrained=True</code> when creating the model, as you are just going to load up another set of weights anyway.</p>

Mona_Jalal: <p>I set <code>strict</code> to <code>False</code> here and it is working. I didn’t change anything else.</p>
<p><code>tnet.load_state_dict(checkpoint['state_dict'], strict=False)</code></p>