How to get name of files while loading images in Pytorch

I am loading images through DataLoader, to test them and get the accuracy, but the problem is Dataloader does not give me the file names so I understand which file was classified write and which one was wrong. how can I get the names? you can see the function I used

def get_all_images(data_location):
data = datasets.ImageFolder(data_location, transform=test_transforms)
classes = data.classes
loader = torch.utils.data.DataLoader(data,batch_size = len(data))
dataiter = iter(loader)
images, labels = dataiter.next()
return classes,images, labels

create custom Dataset object with help of Dataset class
this code snippet might help you

import os
import pandas as pd
from torchvision.io import read_image

class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
        self.img_labels = pd.read_csv(annotations_file)
        self.img_dir = img_dir
        self.transform = transform
        self.target_transform = target_transform

    def __len__(self):
        return len(self.img_labels)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label

source for learning more about dataloader class
https://pytorch.org/tutorials/beginner/basics/data_tutorial.html#creating-a-custom-dataset-for-your-files

Thank you for the reply and help, however can I do this instead?

to have “filenames = data.imgs”

def get_all_images(data_location):
data = datasets.ImageFolder(data_location, transform=test_transforms)
classes = data.classes
filenames = data.imgs
loader = torch.utils.data.DataLoader(data,batch_size = len(data))
dataiter = iter(loader)
images, labels = dataiter.next()
return filenames,classes,images, labels

yes you can but It won’t be eficient
making custom datasets for using them with dataloader is more efficient :slightly_smiling_face: