Running predictions against a trained model

Once we have a trained model, say the model for predicting dogs and cats, how can we programmatically predict for a given image? Is there a code example of this in one of the notebooks?

Yes. Even the first lesson talks about this.

I am looking for something more programmatic. For instance, something like this:

learner.predict(img) #img is a file path and this would return a score

Did you ever find a solution? I’m not through all the courses yet but I would like to test what I have trained so far.

Yes, we found a solution, kind of a hack, but it worked for my purposes at the time. Ypu can find the code here:

The last few lines here do what you are looking for:

The ‘hack’ is that we only use a single image in the test dir, and we swap it out with the image we want to check right before we call predict on the learner.

This was a few months ago, so the fastai lib might have something cleaner now. But to be fair, we were looking to make it do something it wasn’t really designed to do.


Thanks for the hack James. Unfortunately I cannot apply it because I am working with ColumnarModelData instead of ImageClassifierData.