How can I get the image file names to map to the tensor outputs when using get_preds?

I have a very simple use case where I’m batch predicting images and predicting each one. I have already trained the model and am looking to speed up the predicting process by batching instead of calling .predict on each image file.

path = Path('./models')
learn_inf = load_learner(path/'my_model001.pkl')

list = []

image_files = get_image_files('images_folder_to_predict')

dl = learn_inf.dls.test_dl(image_files)
vocab = learn_inf.dls.vocab

preds = learn_inf.get_preds(dl=dl, with_decoded=True)

This works great and it’s much faster than looping over each image and calling .predict individually. The problem is is that the output order of the tensor array is not in the same order as the images i’m predicting in the folder. So I have no way of easily knowing which tensor item belongs to which image, which I need for my project.

Is there a way to achieve this? Thanks!

1 Like

The order is the same as in dl.items (it contains the file names), so you may iterate on it and extract the corresponding prediction from preds.

1 Like

@VDM very simple and straightforward. I knew there had to be a way. Thanks for the response!