Save the model and load it as model.pth including the model state

alber_mel: <p>Hi all,</p>
<p>I am trying to save the model in PyTorch by using the below code:</p>
<blockquote>
<p>model=utils.get_model(self.model)<br>
torch.save({#‘model_state_dict’: model,<br>
<span class="hashtag-raw">#added</span> new<br>
‘model_state_dict’: model.state_dict(),<br>
}, os.path.join(self.checkpoint, ‘model_{}.pth’.format(task_id)))</p>
</blockquote>
<p>I am able to load the model successfully with no issues in my app. The model is been saved in to a pth file.</p>
<p>My second step is to take the saved model model.pth and load it via the code below into another application:</p>
<blockquote>
<p>model.load_state_dict(torch.load(“./checkpoint/model.pth”))</p>
</blockquote>
<p>It is giving me the below error:</p>
<blockquote>
<p>RuntimeError Traceback (most recent call last)<br>
in<br>
----&gt; 1 model.load_state_dict(torch.load(“/home/jovyan/.cache/torch/checkpoints/resnext50_32x4d-7cdf4587.pth”))<br>
2 model = model.eval()</p>
<p>/srv/conda/envs/notebook/lib/python3.7/site-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)<br>
828 if len(error_msgs) &gt; 0:<br>
829 raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
 → 830 self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
831 return _IncompatibleKeys(missing_keys, unexpected_keys)<br>
832</p>
<p>RuntimeError: Error(s) in loading state_dict for Target:<br>
Missing key(s) in state_dict: “conv_layer.0.weight”, “conv_layer.0.bias”, “conv_layer.1.weight”, “conv_layer.1.bias”, “conv_layer.4.weight”, “conv_layer.4.bias”, “conv_layer.5.weight”, “conv_layer.5.bias”, “conv_layer.7.weight”, “conv_layer.7.bias”, “conv_layer.8.weight”, “conv_layer.8.bias”, “conv_layer.11.weight”, “conv_layer.11.bias”, “conv_layer.12.weight”, “conv_layer.12.bias”, “conv_layer.14.weight”, “conv_layer.14.bias”, “conv_layer.15.weight”, “conv_layer.15.bias”, “conv_layer.18.weight”, “conv_layer.18.bias”, “conv_layer.19.weight”, “conv_layer.19.bias”, “conv_layer.21.weight”, “conv_layer.21.bias”, “conv_layer.22.weight”, “conv_layer.22.bias”, “conv_layer.24.weight”, “conv_layer.24.bias”</p>
</blockquote>
<p>Therefore in my code I start to explore additional options to add the model_state too,<br>
My question is, isn’t supposed once I use the below code save all my model including the model state?</p>
<blockquote>
<p>model=utils.get_model(self.model)<br>
torch.save({</p>
</blockquote>
<p>Apparently not, that’s why I added the below to my code:</p>
<blockquote>
<p>torch.save(model.state_dict(), os.path.join(self.checkpoint, ‘model_state_{}.pth’.format(task_id)))</p>
</blockquote>
<p>However, I am getting this error <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<blockquote>
<p>in save_all_models<br>
‘model_state_dict’: model.state_dict(),<br>
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘state_dict’</p>
</blockquote>
<p>Thank you for your help in advance.</p>

huahuanZ: <p>For you first trial</p>
<pre><code class="lang-auto">torch.save({
‘model_state_dict’: model.state_dict(),
}, os.path.join(self.checkpoint, ‘model_{}.pth’.format(task_id)))
</code></pre>
<p>If you load the checkpoint file and print its keys, you would see</p>
<pre><code class="lang-auto">checkpoint = torch.load('...&lt;the path&gt;...')
print(checkpoint.keys())
# output
&gt;&gt;&gt; ['model_state_dict']
</code></pre>
<p>So the solution is:</p>
<ol>
<li>directly saving the model and directly load</li>
</ol>
<pre><code class="lang-auto"># save
torch.save(model.state_dict(), '...&lt;path&gt;...')
# load
model.load_state_dict(torch.load('...&lt;path&gt;...'))
</code></pre>
<ol start="2">
<li>save the model states with a key and load it with THAT key</li>
</ol>
<pre><code class="lang-auto"># save
torch.save({
    'mymodel': model.state_dict()
    }, '...&lt;path&gt;...')
# load
model.load_state_dict(torch.load('...&lt;path&gt;...')['mymodel'])
</code></pre>
<p>And for the error <code>AttributeError: ‘collections.OrderedDict’ object has no attribute ‘state_dict’</code>, just use <code>type(model)</code> to check the type of <code>model</code>.</p>

alber_mel: <p>Hi David,</p>
<p>Thank you heaps for your reply,<br>
I updated my code as suggested instead of:</p>
<blockquote>
<p>‘model_state_dict’: model,</p>
</blockquote>
<p>to</p>
<blockquote>
<p>torch.save({‘model_state_dict’: model.state_dict(),<br>
}, os.path.join(self.checkpoint, ‘model_{}.pth’.format(task_id)))</p>
</blockquote>
<p>however, I am still getting the same error:</p>
<pre><code class="lang-auto"> torch.save({'model_state_dict': model.state_dict(),
AttributeError: 'collections.OrderedDict' object has no attribute 'state_dict'
</code></pre>
<p>I was reading the issues also discussed here:</p><aside class="quote quote-modified" data-post="9" data-topic="38888">
  <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/h/ba9def/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/attributeerror-collections-ordereddict-object-has-no-attribute-state-dict/38888/9">"AttributeError: 'collections.OrderedDict' object has no attribute 'state_dict"</a> 
  </div>
  <blockquote>
    Hi, 
thank you very much. 
I tried as you told me. 
It seems both 
net.load_state_dict(torch.load(model_path)) 
and 
net.load(model_path) 
works well(inference correctly) 
but still , I don’t know how to save a whole model in the case. 
using “net.load_state_dict(torch.load(model_path))” and torch.save(net,‘mb_v1_ssd_all.pth’) 
still give en error: 
File “/home/hong/anaconda3/envs/PyTorch1.0_GPU_ONNX/lib/python3.7/site-packages/torch/serialization.py”, line 291, in _save 
pickler.dump(obj) 
Type…
  </blockquote>
</aside>
<p>
and I tested what was suggested after the model saved and checkpoint loaded to net , I also saved another version of the modelFull by simply adding:<br>
torch.save(net, os.path.join(self.checkpoint, ‘modelFULL_{}.pth’.format(task_id)))<br>
After loading the model:</p>
<blockquote>
<p>model.load_state_dict(torch.load(“checkpoint/modelFULL_0.pth”)</p>
</blockquote>
<p>I am getting an error:<br>
AttributeError: Net object has no attribute Copy</p>
<p>thank you.</p>

huahuanZ: <p>Sorry for the late reply.</p>
<p>For the first error, can you check the <code>type(model)</code> is <code>torch.nn.Module</code> instead of <code>collections.OrderedDict</code> ?</p>
<p>As for the second error, there are commonly two ways of saving/loading models in PyTorch.<br>
One is save/load state_dict and the other is save/load the entire model. In your codes, you’re saving the entire model with <code>torch.save(net, ...)</code>, however load it in state_dict way:</p>
<pre><code class="lang-python"># WRONG. load state_dict, not match with entirely saving model. 
model.load_state_dict(torch.load(“checkpoint/modelFULL_0.pth”))
# CORRECT. Instead, you should load entire model
model = torch.load(“checkpoint/modelFULL_0.pth”)
</code></pre>
<p>Here for more details <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-for-inference" class="inline-onebox" rel="noopener nofollow ugc">Saving and Loading Models — PyTorch Tutorials 1.9.0+cu102 documentation</a></p>