How to get predicted labels with file names?

I want to print the name of the file along with the predicted label for that specific file for test dataset. How can I do this?

For example,
Name: img1, Label: A
Name: img2, Label: B

I have done through following method. But I am not sure whether it is right or not.

preds, y = learn.get_preds(DatasetType.Test)
test_predictions = []
for i, (pred, gt_class) in enumerate(zip(preds, y)):
pred_probability, predicted_class = torch.topk(pred, 1)
test_predictions.append((str(data.test_ds.items[i]).split(’/’)[7], ‘A’))
test_predictions.append((str(data.test_ds.items[i]).split(’/’)[7], ‘B’))
test_predictions.append((str(data.test_ds.items[i]).split(’/’)[7], ‘C’))


Here’s how you grab the file name in v1, this comes from my ClassConfusion widget:

img, lbl =[idx]
fn =[idx]
fn ='([^/*]+)_\d+.*$', str(fn)).group(0)

Hi @muellerzr,
Is there any way to match the predicted images to their file name in the test set? I am using V2 for image segmentation, and I used this for prediction:

data = DataBlock(blocks=(ImageBlock, MaskBlock(codes)),item_tfms=Resize(64), get_items=get_image_files, splitter=FileSplitter(’/content/drive/MyDrive/classification/CNN_segmentation/big_Image/valid_PNG_big.txt’),get_y=get_msk,batch_tfms=[*aug_transforms(size=half), Normalize.from_stats(*imagenet_stats)])
dls = data.dataloaders(path_im, bs=64)
dl = learn.dls.test_dl(fnames[:], shuffle=False)
preds= learn.get_preds(dl=dl)

I am trying to name the predicted images by their file name in test set.