Bug when loading model

Sylvain_Ard: <p>Hi,<br>
I have the following piece of code :<br>
import torch<br>
import torchvision<br>
import cv2<br>
import argparse<br>
from PIL import Image<br>
from utils import draw_segmentation_map, get_outputs<br>
from torchvision.transforms import transforms</p>
<h1>
<a name="initialize-the-model-1" class="anchor" href="#initialize-the-model-1"></a>initialize the model</h1>
<p>model = torch.load(“model_folioles.pth”,map_location=torch.device(‘cpu’))</p>
<h1>
<a name="set-the-computation-device-2" class="anchor" href="#set-the-computation-device-2"></a>set the computation device</h1>
<p>device = torch.device(‘cpu’)</p>
<h1>
<a name="load-the-modle-on-to-the-computation-device-and-set-to-eval-mode-3" class="anchor" href="#load-the-modle-on-to-the-computation-device-and-set-to-eval-mode-3"></a>load the modle on to the computation device and set to eval mode</h1>
<p>model.to(device).eval()</p>
<p>and I have the following error :<br>
Traceback (most recent call last):<br>
File “demo.py”, line 14, in <br>
model.to(device).eval()<br>
AttributeError: ‘dict’ object has no attribute ‘to’</p>
<p>could you help me please ?<br>
thank you<br>
best regards</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="Sylvain_Ard" data-post="1" data-topic="153651">
<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/sylvain_ard/48/30714_2.png" class="avatar"> Sylvain_Ard:</div>
<blockquote>
<p>model = torch.load(“model_folioles.pth”,map_location=torch.device(‘cpu’))</p>
</blockquote>
</aside>
<p>This doesn’t return the <code>model</code> but returns a <code>dict</code> which will contain a key like <code>model_state_dict</code> that contains the <code>state_dict</code> of your model. You’ll need to re-init your model and then load the <code>state_model</code> via something like,</p>
<pre><code class="lang-auto">model = ModelClass() 
state_dict = torch.load("file.pth")
model.load_state_dict(state_dict['model_state_dict'])
</code></pre>
<p>This is why you get <code>dict</code> has no <code>to</code> attribute because you’re expecting <code>model</code> to be a <code>nn.Module</code> object which it’s not, it’s currently a <code>dict</code>.</p>

Sylvain_Ard: <p>ok thank you I had find that earlier, but my model is maskrcnn 101 but I don’t find a torchvision.models.detection.maskrcnn_resnet101_fpn, it is not exists ?</p>

AlphaBetaGamma96: <p>From the <a href="https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_models_for_inference.html" rel="noopener nofollow ugc">documentation</a>, you need access to the base <code>nn.Module</code> object in order to restore a given model from a pathfile. To quote the documentation,</p>
<p>“Notice that the <code>load_state_dict()</code> function takes a dictionary object, NOT a path to a saved object. This means that you must deserialize the saved state_dict before you pass it to the <code>load_state_dict()</code> function. For example, you CANNOT load using <code>model.load_state_dict(PATH)</code> .”</p>
<p>Wherever you got the <code>.pt</code> file from, you’ll need to find the corresponding model <code>nn.Module</code> object in order to restore the <code>model</code> using that <code>.pt</code> file. I don’t believe you can restore a <code>model</code> directly from a <code>.pt</code> file as stated in the documentation.</p>