I trained a CNN model using MNIST dataset and now want to predict a classification of the image, which contains a number 3.

But when I tried to use this CNN to predict, pytorch gives me this error:

TypeError: 'collections.OrderedDict' object is not callable 

And here's what I write:

cnn = torch.load("/usr/prakt/w153/Desktop/score_detector.pkl") img = scipy.ndimage.imread("/usr/prakt/w153/Desktop/resize_num_three.png") test_x = Variable(torch.unsqueeze(torch.FloatTensor(img), dim=1), volatile=True).type(torch.FloatTensor).cuda() test_output, last_layer = cnn(test_x) pred = torch.max(test_output, 1)[1].cuda().data.squeeze() print(pred) 

here's some explaination: img is the to be predicted image with size 28*28 score_detector.pkl is the trained CNN model

any help will be appreciated!

1

2 Answers

Indeed, you are loading a state_dict rather than the model itself.

Saving the model is as follows:

torch.save(model.state_dict(), 'model_state.pth') 

Whereas to load the model state you first need to init the model and then load the state

model = Model() model.load_state_dict(torch.load('model_state.pth')) 

If you trained your model on GPU but would like to load the model on a laptop which doesn't have CUDA, then you would need to add one more argument

model.load_state_dict(torch.load('model_state.pth', map_location='cpu')) 
2

I'm pretty sure score_detector.pkl is actually a state_dict and not the model itself. You will need to instantiate the model first and then load the state_dict, so your first line should be replaced by something like this:

cnn = MyModel() cnn.load_state_dict("/usr/prakt/w153/Desktop/score_detector.pkl") 

and then the rest should work. See this link for more information.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.