How to loading and using a trained model?

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:

> from keras.models import load_model
> import numpy as np
> from keras.preprocessing import image
> import time
> import sys
> import PIL
>    
> start_time =time.time()
> classifier = load_model("C:/Users/deonh/Pictures/Summer Work/model/Covid-19.h5")
> #classifier.summary()
> 
> test_image = image.load_img('C:/Users/deonh/Pictures/Summer Work/Prediction/pnemonia_Covid_or_Normal_1.jpeg', target_size = (64, 64))
> #test_image.show()
> test_image = image.img_to_array(test_image)
> 
> test_image = np.expand_dims(test_image, axis = 0)
> result = classifier.predict(test_image)
> print(result)
> #training_set.class_indices
> print("%s seconds" %(time.time() - start_time) + " run time")

My goal is to do something similar with my Pytorch model. This is what I have so far, but it is not working.

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets, transforms, models # add models to the list
import os
import time
from PIL import Image
from IPython.display import display

model = torch.load(‘C:/Users/deonh/Pictures/Summer Work/xrays/PythonApplication1/scans’)

photo = Image.open(‘C:/Users/deonh/Pictures/Summer Work/prediction/Covid_Normal_or_Pnemonia.jpg’)

photo.show()

transform = transforms.Compose([

    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],
                         [0.229, 0.224, 0.225])
])

im = transform(photo)

#with torch.no_grad():
prediction = model(im)

I am getting a ‘collections.OrderedDict’ object is not callable error

This error should be coming from your torch.load() function. Whenever you load a model, it should be of .pth extension.

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?

It seems

torch.load(‘C:/Users/deonh/Pictures/Summer Work/xrays/PythonApplication1/scans’)

returns the state_dict of the model, not the model instance itself.
You would have to create the model instance before and load the state_dict afterwards.
Here is a small code snippet:

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
1 Like

But even if we load the model but the saved model should be stored in .pth extension @ptrblck

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 .pth is commonly used).
All these state_dicts can be successfully loaded:

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'))
1 Like

Ohhh Okay! Got to learn something today!