Model loading error when using torch.load

fkoca: <p>Hi, I want to use a trained model, for example, <a href="https://github.com/open-mmlab/mmfashion/blob/master/docs/MODEL_ZOO.md" rel="noopener nofollow ugc">this</a> model but when ı try to loading models via torch.load()</p>
<pre><code class="lang-auto">import torch
from torchvision import transforms
import torchvision.models as models
import cv2

device = torch.device("mps")
model = torch.load("epoch_50.pth", map_location = device)
model.eval()


transform = transforms.ToTensor()
image = "exmp.jpg"
image = cv2.imread(image)
input = transform(image)
input = input.unsqueeze(0)


input.to(device)
result = model(input)
print(result)
</code></pre>
<p>They say me that error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/b/ab9a86c58838223a3dc417e01506d96421f2da2c.png" data-download-href="https://discuss.pytorch.org/uploads/default/ab9a86c58838223a3dc417e01506d96421f2da2c" title="Ekran Resmi 2023-01-31 09.12.43"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab9a86c58838223a3dc417e01506d96421f2da2c_2_690x33.png" alt="Ekran Resmi 2023-01-31 09.12.43" data-base62-sha1="ou4IzehGzSeSR5NyAa0mYhrIvHm" width="690" height="33" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab9a86c58838223a3dc417e01506d96421f2da2c_2_690x33.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab9a86c58838223a3dc417e01506d96421f2da2c_2_1035x49.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/b/ab9a86c58838223a3dc417e01506d96421f2da2c_2_1380x66.png 2x" data-dominant-color="282828"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Ekran Resmi 2023-01-31 09.12.43</span><span class="informations">1868×90 5.36 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I finally figure out that this is because this model is saved according to state_dict.<br>
After that ı try to apply <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" rel="noopener nofollow ugc">this</a> tutorial but ı faced another problem that there is no a nn model that must be saved or load with state_dict, in short, ı have no class like <code>class TheModelClass(nn.Module)</code>, just want to use a trained model.</p>
<p>Maybe using backbone stated in model <a href="https://github.com/open-mmlab/mmfashion/blob/master/docs/MODEL_ZOO.md" rel="noopener nofollow ugc">page</a> to load model with state_dict would be useful, like so:</p>
<pre><code class="lang-auto">import torch
from torchvision import transforms
import torchvision.models as models
import cv2

device = torch.device("mps")
model = models.resnet50() ## adding this backbone according to model shared page
model.load_state_dict(torch.load("epoch_50.pth", map_location = device))
model.eval()


transform = transforms.ToTensor()
image = "exmp.jpg"
image = cv2.imread(image)
input = transform(image)
input = input.unsqueeze(0)


input.to(device)
result = model(input)
print(result)
</code></pre>
<p>but unfortunately this approach also ends up with a fancy another problem like:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/6/663d5e117732210f2a4f9b546a61dfb67b542010.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/663d5e117732210f2a4f9b546a61dfb67b542010" title="Ekran Resmi 2023-01-31 09.27.14"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/663d5e117732210f2a4f9b546a61dfb67b542010_2_690x325.jpeg" alt="Ekran Resmi 2023-01-31 09.27.14" data-base62-sha1="eAsbel1P3yoxIxabQCwyGZEDhzG" width="690" height="325" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/663d5e117732210f2a4f9b546a61dfb67b542010_2_690x325.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/663d5e117732210f2a4f9b546a61dfb67b542010_2_1035x487.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/663d5e117732210f2a4f9b546a61dfb67b542010_2_1380x650.jpeg 2x" data-dominant-color="3B3B3B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Ekran Resmi 2023-01-31 09.27.14</span><span class="informations">1920×905 400 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is a problem that ı cannot find any useful solution. Your reply will be appreciated, thank you so much.</p>

thecho7: <p><code>model.load_state_dict(torch.load("epoch_50.pth", map_location = device)) </code> is incorrect.</p>
<p>Try <code>model.load_state_dict(torch.load("epoch_50.pth", map_location = device)['state_dict']) </code> instead.</p>
<p>According to the error, <code>torch.load("epoch_50.pth", map_location = device)</code> is a dictionary that contains <code>meta</code>, <code>state_dict</code>, <code>optimizer</code>. The model parameter you need is in <code>state_dict</code>.</p>

fkoca: <p>Thank you for your response, this is a good point, but unfortunately, ı think the major problem is what that model must be. Because, ı just want to use a pre-trained model, not a nn model has written from scratch.<br>
and as depicted below, backbone approach is also not working.</p>
<pre><code class="lang-auto">from torchvision import transforms
import torchvision.models as models
import cv2


device = torch.device("mps")
model = models.resnet50()
checkpoint = torch.load("epoch_50.pth", map_location = device)

model.load_state_dict(checkpoint["state_dict"])

transform = transforms.ToTensor()
image = "exmp.jpg"
image = cv2.imread(image)
input = transform(image)
input = input.unsqueeze(0)


input.to(device)
result = model(input)
print(result)
</code></pre>
<p>output:</p>
<pre><code class="lang-auto">RuntimeError: Error(s) in loading state_dict for ResNet:
        Missing key(s) in state_dict: "conv1.weight", "bn1.weight", "bn1.bias", "bn1.running_mean", 
...
        Unexpected key(s) in state_dict: "backbone.conv1.weight", "backbone.bn1.weight", 
...
"visibility_classifier.linear.bias", "landmark_regression.linear.weight", "landmark_regression.linear.bias".
</code></pre>

thecho7: <p>That is because the names of each named parameters are different.<br>
You should manually change the key values like below,<br>
<code>backbone.conv1.weight</code> to <code>conv1.weight</code>.</p>
<p>Good luck:)</p>

fkoca: <p>Thanks thecho7. But this approach smells of brute force. There must be a proper way to handle this awkward problem.</p>

knoriy: <aside class="quote no-group" data-username="fkoca" data-post="3" data-topic="171445">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/ee59a6/48.png" class="avatar"> fkoca:</div>
<blockquote>
<p><code>epoch_50.pth</code></p>
</blockquote>
</aside>
<p>It looks like you are loading a different model’s checkpoint. The checkpoint (<code>epoch_50.pth</code> ) you have was not trained on the Torchvision’s implementation of resnet50.</p>
<pre><code class="lang-auto">models.resnet50(weights="IMAGENET1K_V2")
</code></pre>
<p>This will load a pre-trained model trained on <code>IMAGENET1K</code></p>