Can't Load Trained Classifier Model

keremnymn: <p>Hello I’ve trained ResNet18 model yesterday but can’t load it for inference today. What am I missing?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/5/053fe17a35d48b7356536277b3825220471a8135.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/053fe17a35d48b7356536277b3825220471a8135" title="Ekran Alıntısı"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/053fe17a35d48b7356536277b3825220471a8135_2_690x383.jpeg" alt="Ekran Alıntısı" data-base62-sha1="KrfzTnOO1WNQ2dsslkkYGablbL" width="690" height="383" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/053fe17a35d48b7356536277b3825220471a8135_2_690x383.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/053fe17a35d48b7356536277b3825220471a8135_2_1035x574.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/053fe17a35d48b7356536277b3825220471a8135_2_1380x766.jpeg 2x" data-dominant-color="3D3D3D"><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 Alıntısı</span><span class="informations">1452×806 179 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>And when I change the last cell to: <code>model.load_state_dict(checkpoint['state_dict'])</code> it gives me this error: <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/1/f1e35d65c8a6f577fdff8e10ad6fe09266145681.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/f1e35d65c8a6f577fdff8e10ad6fe09266145681" title="Ekran Alıntısı2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/1/f1e35d65c8a6f577fdff8e10ad6fe09266145681_2_690x212.jpeg" alt="Ekran Alıntısı2" data-base62-sha1="yvQcenxB3LAT5yVb0q4w4KF5jiN" width="690" height="212" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/1/f1e35d65c8a6f577fdff8e10ad6fe09266145681_2_690x212.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/f/1/f1e35d65c8a6f577fdff8e10ad6fe09266145681.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/1/f1e35d65c8a6f577fdff8e10ad6fe09266145681.jpeg 2x" data-dominant-color="3A3A3A"><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 Alıntısı2</span><span class="informations">857×264 26 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>

bing: <p>did you trained it on gpu and now performing the inference on cpu?</p>

tom: <p>You probably saved a dict <code>{"model": models_sd, ...}</code> as checkpoint, so load <code>checkpoint["model"]</code>. (The key information here is not the keys it is missing but the ones that are unexpected.)</p>
<p>Best regards</p>
<p>Thomas</p>

keremnymn: <p>Yes indeed, is it a problem?</p>

keremnymn: <p>I trained it using <a href="https://colab.research.google.com/drive/1mVISoBYTDk3Q9D5VmviTkYD28WlFW-Jk#scrollTo=iU73-rhTaMVI" rel="nofollow noopener">this tutorial</a>. It uses fast.ai library. Should I use fast.ai library again to infer?</p>

bing: <p>I was using multiple gpu’s and was using <code>nn.DataParallel</code> at the wrong place. So my checkpoint saved the <code>module</code> with the key values, I solved it using the below way-<br>
<code>from collections import OrderedDict</code><br>
<code>new_state_dict = OrderedDict()</code><br>
<code>for k, v in state_dict.items():</code><br>
<code> name = k[7:] # remove</code>module.``<br>
<code> new_state_dict[name] = v</code></p>
<p>Now, you are using resnet, so I don’t know exactly how resnet behaves in mutli gpu settings.</p>

bing: <p>try to load in this way<br>
<code>torch.load('my_file.pt', map_location=lambda storage, loc: storage)</code></p>

keremnymn: <p>Okay solution was using Fast.Ai’s library since I trained using that. Thanks everyone who helped me.</p>