Learn.get_preds() output doesn't include input file names

Hi all

When using learn.get_preds() on several inputs (using ds_type), the output is a a tensor of the output categories etc. However, this tensor doesn’t the file name for each output.

Is it assumed that the first row of the output tensor corresponds to the first input file to the learn.get_preds() function (i.e. the first file in the ImageList folder below), and so on for the x^th output row and the x^th input file?

Example code:
learn = load_learner(path / ‘models’, file=model_name, test=ImageList.from_folder(test_set)))
preds = learn.get_preds(ds_type=DatasetType.Test)

Thanks very much

Yes, get_preds() works like that :slightly_smiling_face:
Actually, it doesn’t make a lot sense to also output the corresponding input file name as well. You see, ideally, you wish to pick an input, say an image file, from your desktop, and run it through the model to get the predicted output. So you’re just expecting the prediction and not that the model first prints out your input data, and then prints your output. I hope it makes sense to you

I understand. For me it would be useful for also output the corresponding input file name for cases where there are lots of inputs that I want predictions from.

Thanks very much for your help

Hi, I have a problem for matching the name of my predicted images to its name in test file.
Hi,
I will be thankful if you could help me to find a solution for this problem. I am trying to use segmentation for a data collection with 987 images, and I want to export my predicted images with the name of their real image(used as input in test file)?

This is the code that I used for predicting:
dl = learn.dls.test_dl(fnames[:])
preds = learn.get_preds(dl=dl,reorder=False)
for i, pred in enumerate(preds[0]):
pred_arg = pred.argmax(dim=0).numpy()
rescaled = pred_arg.astype(np.uint8)
im = Image.fromarray(rescaled)
im.save(f’/content/drive/MyDrive/classification/CNN_segmentation/big_Image/pred_all_tiff’)
Also, the order of predicted images does not match the order of images in the test file.