How to make predictions with vision learner? (solved)


I have trained a model, and want to put some new arbitrary images (that were not apart of the training/testing/validation) through it.

I can’t figure out how. The ‘predict’ method does not accept a path to an image.


learn.predict(f"{BASE}/test/0aa98293e.jpg ")

The entire notebook with the training can be found here:

Ultimately what I am trying to do take that test folder and get the top 5 predictions for each image.


As shown here, learn.predict takes an Image, not a filename.


I appreciate the link and have looked there. That is showing how to do a prediction on data that is already “in” the learner.

I am trying to figure out how to load the image from disk into an “image” as you say.


Is there a tutorial anywhere on how to use learn.predict on data that is not a part of the training?


@sgugger the link you provided has reference to load_learner which doesn’t seem to exist anymore?

(Dilip Thiagarajan) #6

At the very worst, I think you could call learn.model() on your input, but I think you’d have to make sure to transform the input properly if necessary.


As @sgugger has said, the function takes an Image, while you are passing a string.

Open the image and pass that instead.

You can find it here


Thanks @miko

Now it looks like predict eturns the top category and a list of probabilities for all categories.

I am not sure how to map the human readable categories to the list of probabilities returned.


cat, tensor, probs = learn.predict(open_image(f"{BASE}/test/d042ac24f.jpg"))

where probs is a tensor of length num_cats that, I presume, maps to a list of categories.

I am trying to get the top 5 (rather than top 1) most likely labels.


i have too many questions about the return of predict. i will open a new thread.