How to loading and using a trained model?

gamebo5000: <p>I am completely new to Pytorch and I created my first model. I made a similar model in keras and use this code to test it on data it never seen before:</p>
<pre><code class="lang-auto">&gt; from keras.models import load_model
&gt; import numpy as np
&gt; from keras.preprocessing import image
&gt; import time
&gt; import sys
&gt; import PIL
&gt;    
&gt; start_time =time.time()
&gt; classifier = load_model("C:/Users/deonh/Pictures/Summer Work/model/Covid-19.h5")
&gt; #classifier.summary()
&gt; 
&gt; test_image = image.load_img('C:/Users/deonh/Pictures/Summer Work/Prediction/pnemonia_Covid_or_Normal_1.jpeg', target_size = (64, 64))
&gt; #test_image.show()
&gt; test_image = image.img_to_array(test_image)
&gt; 
&gt; test_image = np.expand_dims(test_image, axis = 0)
&gt; result = classifier.predict(test_image)
&gt; print(result)
&gt; #training_set.class_indices
&gt; print("%s seconds" %(time.time() - start_time) + " run time")
</code></pre>
<p>My goal is to do something similar with my Pytorch model. This is what I have so far, but it is not working.</p>
<blockquote>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torch.utils.data import DataLoader<br>
from torchvision import datasets, transforms, models # add models to the list<br>
import os<br>
import time<br>
from PIL import Image<br>
from IPython.display import display</p>
<p>model = torch.load(‘C:/Users/deonh/Pictures/Summer Work/xrays/PythonApplication1/scans’)</p>
<p>photo = Image.open(‘C:/Users/deonh/Pictures/Summer Work/prediction/Covid_Normal_or_Pnemonia.jpg’)</p>
<p>photo.show()</p>
<p>transform = transforms.Compose([</p>
<pre><code>    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                         [0.229, 0.224, 0.225])
])
</code></pre>
<p>im = transform(photo)</p>
<p><span class="hashtag-raw">#with</span> torch.no_grad():<br>
prediction = model(im)</p>
</blockquote>
<p>I am getting a    <strong>‘collections.OrderedDict’ object is not callable</strong>  error</p>

cskarthik7: <p>This error should be coming from your torch.load() function. Whenever you load a model, it should be of .pth extension.</p>

gamebo5000: <p>Thank you!! Was I supposed to manually add the .pth extension to the save file name when I wrote the save code, or was it supposed to automatically get added and I made an error elsewhere?</p>

ptrblck: <p>It seems</p>
<pre><code class="lang-python">torch.load(‘C:/Users/deonh/Pictures/Summer Work/xrays/PythonApplication1/scans’)
</code></pre>
<p>returns the <code>state_dict</code> of the model, not the model instance itself.<br>
You would have to create the model instance before and load the <code>state_dict</code> afterwards.<br>
Here is a small code snippet:</p>
<pre><code class="lang-python">model = MyModel() # create model instance
state_dict = torch.load(‘C:/Users/deonh/Pictures/Summer Work/xrays/PythonApplication1/scans’) # load state_dict
model.load_state_dict(state_dict) # load state_dict into model
</code></pre>

cskarthik7: <p>But even if we load the model but the saved model should be stored in .pth extension <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

ptrblck: <p>The file extension would just associate this particular file to an application, but if I’m not mistaken PyTorch doesn’t check it and doesn’t have an official extension (even though <code>.pth</code> is commonly used).<br>
All these <code>state_dicts</code> can be successfully loaded:</p>
<pre><code class="lang-python">model = models.resnet18()
sd = model.state_dict()

torch.save(sd, 'tmp.pth')
model.load_state_dict(torch.load('tmp.pth'))

torch.save(sd, 'tmp')
model.load_state_dict(torch.load('tmp'))

torch.save(sd, 'tmp.jpeg')
model.load_state_dict(torch.load('tmp.jpeg'))

torch.save(sd, 'tmp.pkl')
model.load_state_dict(torch.load('tmp.pkl'))
</code></pre>

cskarthik7: <p>Ohhh Okay! Got to learn something today!</p>