Predicting on single image using Fastai Model


(Zachary Thomas) #1

Hi! Quick q. I went back to Lesson 1 and wanted to get the dog/cat predictions on a new image using the model we built (learn, from the 12th notebook code block). Let’s say I want to predict the score for this one I found on the internet.

!wget http://d2trtkcohkrm90.cloudfront.net/images/emoji/apple/ios-10/256/dog.png dog.png 
img = plt.imread('dog.png')
plt.imshow(img);

12 PM

I wasn’t totally sure how to predict on a new image but I played around with the code and tried out:
learn.predict_array(img)

But then I got this strange error:
RuntimeError: running_mean should contain 256 elements not 1024

Thoughts on how to debug this? Am I using the wrong command to predict? Let me know if this isn’t reproducible.


(Phani Srikanth) #2

You have to pass the image through the transformations before you feed it into the classifier. This was discussed at length here: How do we use our model against a specific image?


(Zachary Thomas) #3

Just clarify since the linked thread is a little bit unclear about the syntax on how to predict on a single image:

trn_tfms, val_tfms = tfms_from_model(arch,sz) # get transformations
im = val_tfms(Image.open('image.png'))
learn.precompute=False # We'll pass in a raw image, not activations
preds = learn.predict_array(im[None])
np.argmax(preds) # preds are log probabilities of classes

(If you simply use Jeremy’s code linked in Phani’s reply, you’ll probably get a running mean error – that’s since we’re passing in an image and not a precomputed activation, as Jeremy later details here)


(Zachary Thomas) #4

Thanks!


(Phani Srikanth) #5

You’re right @thomzi12. I haven’t linked to the working code. I ran into the same problem and I went through the complete thread, fiddled with the code and figured it out in the end. Maybe I wanted to teach how to fish than merely giving away the answer :sweat_smile: Apologies!


(Pierre Guillou) #6

Hello @thomzi12,

I use your code and get an error. Do you know what I type wrong ?

My code :

trn_tfms, val_tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1) # get transformations

fn = PATH+data.val_ds.fnames[775]
im = PIL.Image.open(fn)
im_val = val_tfms(im)

Notes :

  • When I type im, the image is well displayed in my jupyter notebook.
  • When I use tfms_from_model(arch, sz) like you, I get the same error.
  • The problem comes from val_tfms() and I got the same error if I use trn_tfms() but why ?


(Jeremy Howard) #7

Apologies - there’s been a slight change. Instead of PIL.Image.open you should use open_image (you’ll need to git pull to get this function, since it’s new).


(Pierre Guillou) #8

Thanks @jeremy ! It works well now.


(Stephen Rimac) #9

Hi Jeremy, I have tried both of below in the dog ID code from lesson 2, in order to view one image, but neither work. Please advize :slight_smile:

fn = PATH + data.trn_ds.fnames[0]
img = PIL.Image.open(fn); img
img = open_image(fn); img