How to do inference with list of several images object as input

Hello everybody,
fastai version 1.0.40 is used
For some reason in my code, I’ve 4 images that are already present as object of type: fastai.vision.image.Image
I would like to make predictions not on those images 1 by 1 but on a small batch of these 4 images to speed up the inference.
I would like to know if it is possible to use the “get_preds” or “pred_batch” with a list of images, and not from a folder contening different images in .jpg format for example.
The idea was to do something like:

img4Inference = ImageItemList.? to load the 4 images, then:
model.data = img4Inference to change the data “associated” to the model, then:
model.get_preds(ds_type=DatasetType.Test)

Until now I didn’t find a way to create this ImageItemList from the 4 images, neither with the add_test function.

Any help would be appreciated :slight_smile:

Best regards

You can directly pass an array of filenames in ImageItemList (without using any factory method).

Thanks for your help Sylvain

The problem is that only the first image has a filename.
img1 = open_image(imgFileNamePath) --> type is fastai.visdion.image.Image

This is how I create img2, img3 and img4:
img2 = Image(tensor.flip(2)) --> type is fastai.visdion.image.Image
img3 = Image(tensor.flip(1)) --> type is fastai.visdion.image.Image
img4 = Image(tensor.transpose(1,2)) --> type is fastai.visdion.image.Image

What I’m doing is a king of “controled” TTA (as I’ve understood it)

I can do prediction with a loop, image by image:
imgList = [img1, img2, img3, img4]
for img in imgList:
prediction = model.predict(img)

But what I want to do is to make prediction on these 4 images as a mini batch from their current state.
It won’t be a good idea I think to save the 4 images in a folder, then “reload” them as test set (mini batch)

You will have to create your batch yourself then. Use data_collate, this is the function that is used (almost) every time to collate the images in a batch.

Thanks Sylvain.
I will try it tomorrow :-):slightly_smiling_face:

Hi @sgugger @laurent looking on best/faster way to precisely do this, have several Images (Image class) on a list.

Is there any sample on how to use data_collate for this? Struggling to find :slight_smile:

(so to avoid the for loop and use predict/pred_batch in batches)
thanks!