Getting image name from tensor data after batch data loading

To debug my code I need to get name of images in my batch with its labels and prediction. As images in batch are in form of tensor, So I can not acess image name.

In my dataloader below part of code is used-

def getitem(self, i):
data, label = self.data[i], self.label[i] #here data is actual file name of image
image = self.transform(Image.open(data).convert(‘RGB’)) # here its converted into tensor
return image, label #return image tensor and labels
Below part of code I am using to load data in batch-

for i, batch in tqdm(enumerate(self.test_loader, 1)):
if torch.cuda.is_available():
data, _ = [_.cuda() for _ in batch] #data loaded in form of tensor
logits = self.model(data)
loss = F.cross_entropy(logits, label)

So what needs to be changed so that I can see the data not only in form of tensor but also as name of data(file name)?

You could return the name (data in your example) directly in the Dataset.__getitem__ method:

def __getitem___(self, i):
    data, label = self.data[i], self.label[i] #here data is actual file name of image
    image = self.transform(Image.open(data).convert(‘RGB’)) # here its converted into tensor
    return image, label, data

I already tried to do the same suggested by you but then I got below error-

data, _ = [_.cuda() for _ in batch]
AttributeError: 'list' object has no attribute 'cuda'

Above error disapperas when I don’t return 3 values from dataloader
test_loader is used as below-

testset = Dataset('test', args)
test_sampler = CategoriesSampler(testset.label, 1, # args.num_eval_episodes, 
args.eval_way, args.eval_shot + args.eval_query)
test_loader = DataLoader(dataset=testset, batch_sampler=test_sampler, num_workers=args.num_workers)

Do I have to adapt test_loader so that batch takes image name?

You would have to split the returned batch to the three objects (tensor, tensor, list), as you are currently treating them all as tensors:

data, _ = [_.cuda() for _ in batch]

you mean like -

for i, batch in tqdm(enumerate(self.test_loader, 1)):
if torch.cuda.is_available():
    file_name=[]
    data, _ , file_name= [_.cuda() for _ in batch]

No, this should work:

data, _, file_name = batch
data = data.cuda()

assuming batch contains 3 objects.