# Can't Load Trained Classifier Model

**URL:** <https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920>\
**Category:** vision\
**Created:** [September 11, 2020, 12:32pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920 "2020-09-11T12:32:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![keremnymn](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/keremnymn/32/28044_2.png) [@keremnymn](https://discuss.pytorch.org/u/keremnymn)\
**Post date:** [September 11, 2020, 12:32pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/1 "2020-09-11T12:32:11Z")

</div>

Hello I’ve trained ResNet18 model yesterday but can’t load it for inference today. What am I missing?

 ![Ekran Alıntısı](https://discuss.pytorch.org/uploads/default/original/3X/0/5/053fe17a35d48b7356536277b3825220471a8135.jpeg)

And when I change the last cell to: `model.load_state_dict(checkpoint['state_dict'])` it gives me this error:

 ![Ekran Alıntısı2](https://discuss.pytorch.org/uploads/default/original/3X/f/1/f1e35d65c8a6f577fdff8e10ad6fe09266145681.jpeg)

---

<div class="post-metadata">

**Author:** ![bing](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@bing](https://discuss.pytorch.org/u/bing)\
**Post date:** [September 11, 2020, 12:36pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/2 "2020-09-11T12:36:49Z")

</div>

did you trained it on gpu and now performing the inference on cpu?

---

<div class="post-metadata">

**Author:** ![tom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/32/3162_2.png) [@tom](https://discuss.pytorch.org/u/tom)\
**Post date:** [September 11, 2020, 12:36pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/3 "2020-09-11T12:36:51Z")

</div>

You probably saved a dict `{"model": models_sd, ...}` as checkpoint, so load `checkpoint["model"]`. (The key information here is not the keys it is missing but the ones that are unexpected.)

Best regards

Thomas

---

<div class="post-metadata">

**Author:** ![keremnymn](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/keremnymn/32/28044_2.png) [@keremnymn](https://discuss.pytorch.org/u/keremnymn)\
**Post date:** [September 11, 2020, 12:37pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/4 "2020-09-11T12:37:58Z")

</div>

Yes indeed, is it a problem?

---

<div class="post-metadata">

**Author:** ![keremnymn](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/keremnymn/32/28044_2.png) [@keremnymn](https://discuss.pytorch.org/u/keremnymn)\
**Post date:** [September 11, 2020, 12:39pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/5 "2020-09-11T12:39:45Z")

</div>

I trained it using [this tutorial](https://colab.research.google.com/drive/1mVISoBYTDk3Q9D5VmviTkYD28WlFW-Jk#scrollTo=iU73-rhTaMVI). It uses fast.ai library. Should I use fast.ai library again to infer?

---

<div class="post-metadata">

**Author:** ![bing](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@bing](https://discuss.pytorch.org/u/bing)\
**Post date:** [September 11, 2020, 12:42pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/6 "2020-09-11T12:42:49Z")

</div>

I was using multiple gpu’s and was using `nn.DataParallel` at the wrong place. So my checkpoint saved the `module` with the key values, I solved it using the below way-  
`from collections import OrderedDict`  
`new_state_dict = OrderedDict()`  
`for k, v in state_dict.items():`  
` name = k[7:] # remove`module.``  
` new_state_dict[name] = v`

Now, you are using resnet, so I don’t know exactly how resnet behaves in mutli gpu settings.

---

<div class="post-metadata">

**Author:** ![bing](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@bing](https://discuss.pytorch.org/u/bing)\
**Post date:** [September 11, 2020, 12:47pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/7 "2020-09-11T12:47:52Z")

</div>

try to load in this way  
`torch.load('my_file.pt', map_location=lambda storage, loc: storage)`

---

<div class="post-metadata">

**Author:** ![keremnymn](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/keremnymn/32/28044_2.png) [@keremnymn](https://discuss.pytorch.org/u/keremnymn)\
**Post date:** [September 11, 2020, 4:18pm UTC](https://discuss.pytorch.org/t/cant-load-trained-classifier-model/95920/8 "2020-09-11T16:18:43Z")

</div>

Okay solution was using Fast.Ai’s library since I trained using that. Thanks everyone who helped me.
