Load my own test data (images)


I just want to know, how can I load my own test data (image.jpg) in pytorch in order to test my CNN.

I’ve tried test_data = datasets.ImageFolder(data_dir + '/test_cnn', transform=test_transforms)

but during the test, I have too many indices because my test data is automatically labeled by ImageFolder. A solution is to drop the classes but I don’t know How

Thanks for helping

An easy solution would be to just drop the labels and use your test data in the DataLoader loop:

for data, _ in loader:
    data = data.to(device)

This approach is a bit hacky, so the cleaner solution would be to write your own test dataset:

class TestDataset(Dataset):
    def __init__(self, path, transform=None):
        self.image_paths = glob.glob(path + '*.jpg')
        self.transform = transform

    def __getitem__(self, index):
        x = Image.open(self.image_paths[index])
        if self.transform is not None:
            x = self.transform(x)

        return x

    def __len__(self):
        return len(self.image_paths)
1 Like

Thanks for your answer. But How can you drop labels? (this is my problem)

Sorry I’m a bit beginner in DL area

No need to excuse for that. :wink:
I’ve just used an underscore in the data loading loop, to drop the targets:

for data, _ in loader:
    data = data.to(device)

In this loop only the data is used and no targets are available.
You could also just get the targets and not use them in any way.


Thank you so much, it solve my problem