How to load a model properly?

Asya: <p>I’ve trained my model and saved it as <code>model.pt</code> file and now I would like to use it to predict new records.</p>
<p>So I’m trying to do the same as my tensorflow code:</p>
<pre><code class="lang-auto">model = keras.models.load_model('path/to/location')
prediction = model.predict(my_new_record)
</code></pre>
<p>but when I do with pytorch:</p>
<pre><code class="lang-auto">model = torch.load('model.pt')
prediction = model(my_new_record)
</code></pre>
<p>I get</p>
<blockquote>
<p>TypeError: ‘collections.OrderedDict’ object is not callable</p>
</blockquote>

AlphaBetaGamma96: <p>How exactly are you saving the model? When to save the model it usually saved as an OrderedDict and hence it’ll be a dictionary that contains multiple objects which can be obtained via a given key. For example, I save my model like,</p>
<pre><code class="lang-auto">torch.save({'epoch':epoch,
            'loss': loss,
            'model_state_dict':model.state_dict(), 
            'optim_state_dict':optim.state_dict()}, "path/to/location.pt')
</code></pre>
<p>then to load I do,</p>
<pre><code class="lang-auto">state_dict = torch.load(f="path/to/location.pt", map_location=device)
epoch = state_dict['epoch']
loss = state_dict['loss']
model.load_state_dict(state_dict['model_state_dict'])
optim.load_state_dict(state_dict['optim_state_dict'])
</code></pre>
<p>Could you print out <code>model</code> from the line <code>model = torch.load("model.pt")</code> ?</p>

Asya: <p>I saved model like that:<br>
<code> torch.save(model.state_dict(), 'model.pt')</code></p>
<p>when I print it, it’s just OrderDict with many tensors:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/a/ba58364b7800ecc5a3bf504b58d080ac97272580.png" data-download-href="https://discuss.pytorch.org/uploads/default/ba58364b7800ecc5a3bf504b58d080ac97272580" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/a/ba58364b7800ecc5a3bf504b58d080ac97272580.png" alt="image" data-base62-sha1="qAtNMRb92zgsUGfoy6efEzAC5dm" width="690" height="452" data-dominant-color="F3F3F3"><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">image</span><span class="informations">1236×810 37.3 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>

Sayed_Nadim: <p>Instead of doing,</p>
<pre><code class="lang-auto">model = torch.load('model.pt')
</code></pre>
<p>Try,</p>
<pre><code class="lang-auto">model = modelClass() # initialize your model class
model.load_state_dict(torch.load('model.pt'))
</code></pre>
<p>For loading and saving, refer to <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" rel="noopener nofollow ugc">this link.</a></p>

AlphaBetaGamma96: <p>Your OrderedDict seems to be all the parameters of your model. Try what <a class="mention" href="/u/sayed_nadim">@Sayed_Nadim</a> stated above pass the saved object to <code>model.load_state_dict</code>. That should work, if not just post the error below!</p>

Asya: <p>I’m not sure I know what to put into <code>modelClass</code><br>
because I load pretrained vgg16 model and then tuned on other set of images<br>
so I didn’t use <code>modelClass</code></p>

AlphaBetaGamma96: <p>How exactly is ‘model.pt’ saved? I assume you have some <code>model</code> and you just do <code>torch.save(model.state_dict(), 'model.pt')</code>? Is that correct?</p>

Asya: <aside class="quote no-group quote-modified" data-username="Asya" data-post="3" data-topic="131947">
<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/a/22d042/48.png" class="avatar"> Asya:</div>
<blockquote>
<p>torch.save(model.state_dict(), ‘model.pt’)</p>
</blockquote>
</aside>
<p>yes as I wrote in reply before it’s <code>torch.save(model.state_dict(), 'model.pt')</code></p>

AlphaBetaGamma96: <p>Could you try following what I said above?</p>
<p>So, save the model as,</p>
<pre><code class="lang-auto">torch.save({'model_state_dict': model.state_dict()}, "model.pt")
</code></pre>
<p>and try loading it back with,</p>
<pre><code class="lang-auto">state_dict = torch.load(f="model.pt", map_location=device) #device is torch.device('cpu') or torch.device("cuda")
model.load_state_dict(state_dict['model_state_dict'])
</code></pre>
<p>This is slightly more convoluted but it might work! If this fails, could you print out the keys of the Dictionary?</p>
<pre><code class="lang-auto">for key, value in state_dict:
  print(key)
</code></pre>

Asya: <p>what should be a model variable here in my case? <code>model = models.vgg16(pretrained=use_pretrained)</code> ? if so it breaks on:</p>
<pre><code class="lang-auto">RuntimeError: Error(s) in loading state_dict for VGG:
	size mismatch for classifier.6.weight: copying a param with shape torch.Size([27, 4096]) from checkpoint, the shape in current model is torch.Size([1000, 4096]).
	size mismatch for classifier.6.bias: copying a param with shape torch.Size([27]) from checkpoint, the shape in current model is torch.Size([1000]).
</code></pre>
<p>when I do <code>for key, value in state_dict:   print(key)</code><br>
it’s:</p>
<pre><code class="lang-auto">ValueError: too many values to unpack (expected 2)

</code></pre>
<p>so it just prints key <code>for key in state_dict:   print(key)</code><br>
it’s:</p>
<pre><code class="lang-auto">model_state_dict
</code></pre>

AlphaBetaGamma96: <p>Ah my apologises, I should’ve phrased the last statement more clearly. I meant to try the <code>for key, value in state_dict</code> expression for your original <code>torch.save</code> object. It makes sense it requires <code>model_state_dict</code> as that’s the key we use to save the model’s <code>state_dict</code>!</p>
<p>When loading the <code>model</code> you’re using the right object. It seems the issue is that your <code>state_dict</code> has the wrong shape for the layer called <code>classifier.6</code> The issue seems that your output shape should be of shape <code>[1000, 4096]</code> whereas yours is <code>[27, 4096]</code></p>
<p>I assume you’re trying to use VGG16 for classification but you only have 27 classes rather than the default 1000 classes? If so, it seems you just need to change the output shape to be for 27 classes and the model loading should be fine!</p>