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.
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)
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 Apologies!
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).
Hi @jeremy and @thomzi12. I am trying to create a prototype application in my local environment just to play around with the model. I would like to try implementing it to detect images from my webcam. As my environment locally doesn’t have gpu and I don’t have cuda enabled, I setup torch without cuda.
learn.predict() works! and it was able to predict images in the valid/test folder even without gpu. But when I tried the method suggested in here:
I got an error that said:
“AssertionError: Torch not compiled with CUDA enabled”
I am wondering if there is a function I can call to predict single images without GPU? I am currently exploring the fastai library but I am not that good in python so I can’t find how learn.predict() was able to work vs the learn.predict_array() function.
I am not sure if this is still a scope of the lesson but I believe it would help me understand better how fastai library works. Thank you!
Hi,
we use im[None] because everything passed to or returned from models is assumed to be mini-batch or “tensors” so it should be a 4-dimensional tensor.
If you run something like the following code:
trn_tfms, val_tfms = tfms_from_model(resnet34,sz)
im = val_tfms(open_image(f'{PATH}valid/cats/cat.10016.jpg'))
print(im[None].shape)
print(im.shape)
So None is what numpy uses as a convention to specify that you want to create a new axis. You can alternatively use np.newaxis (which is just a variable that equals None for now) for readability.
Thank you @jeongyoonlee.
Is it possible if you can tell me the reason behind it? Like I get it that nn.functional is being used but how is it helping adaptive layer in being readable by the onnx layer as adaptive layers are absent in onnx.
Instead of AdaptiveAvgPool2d and AdaptiveMaxpool2d I used other layers like AvgPool2D and MaxPool2D but it was giving an error of running_mean should contain 50176 elements not 1024. If you know anything about this error.
Hello everyone,
“list index out of range” error appears when I use learn.predict(image).
Do you mind helping me with this error?
The screenshot of this error is below: