How can I apply transformations to image at inference?

Hello,

I’m new to coding. The FastAI library is very impressive.

What is the most efficient way to do inference on the same image several times, with transformations applied to the same test image (in order to take the average predicted result)?

Or more simply, how can I do inference on an image after transformations have been applied?

This has been shown to be successful for improving prediction accuracy (see paragraph on inference here), and it seems to work better in my own tests using Keras, but my code is written in a clumsy way and I’d like to do this in FastAI:

when I do:
learn.predict(img.apply_tfms)
I get an error of:
‘function’ object has no attribute ‘apply_tfms’

Thanks,
Noob

What is img? Seems to be a function instead of a proper image and that’s the error shown.

You could create a databunch with your test set and give it the transformations you want so that it would apply them when you get items from it. You would need to set the sampler to not be random (to make sure prediction order is always the same) and then you can call learn.get_preds() x times and average the results.

Calling predict to the single image many times is simpler though, will just be more inefficient.

Thanks Julian,

img = open_image(’/home/user/Desktop/image_name.png’)

If I want to do inference for just one image, how do I create a DataBunch for that? DataBunch requires training dataset, validation dataset, and labels.

How do I get a single image object with transformations on which learn.predict() can be called?

Thanks

Learn.predict() will automatically use the transformations your model was trained to be tested on (the transforms passed into the initial DataBunch object). If you want to modify the image beforehand I’d look into some sort of external PIL functionality that can do it, or dig into some source code to what the images are doing.

Let me know if that helps.

4 Likes

As Mueller has said, the learner will use the transformations you already defined on the validation set. You can also do learn.predict(img.apply_tfms(get_transforms()[0]) to apply other transforms you want but that won’t stop the learner to apply the others as well.

4 Likes

I am also new to fastai and have some questions about this.

You wrote "the learner will use the transformations you already defined on the validation set. "

What do you mean by transformations defined on the validation set? Is it the not the same set of transformations that are used during training and validation? Or do we have to define the validation transformations separately?

Also, while defining a datablock, I can define item_tfms and batch_tfms. Are both of these applied automatically during inference or is it just the item_tfms.

I tried looking into the documentation for this but could not find these details or maybe just missed them.

Thanks

That’s all from fastai v1 and you using v2 ^^. There was no item_tfms or batch_tfms at that time. I am not familiar with v2 but you could either ask your question on a new post or go to the discord group