Running predictions against a trained model

(James Birchfield) #1

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.

(James Birchfield) #3

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

(Anders) #4

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.

(James Birchfield) #5

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.


(Anders) #6

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