Error when use ImageFolder to load my dataset

Hi. I want to use ImageFolder to load my dataset. However, I got a error message

Traceback (most recent call last):
  File "lenet.py", line 106, in <module>
    train(10, net, device, training_data, optimizer, epoch=ep_id)
  File "lenet.py", line 46, in train
    for batch_idx, (data, target) in enumerate(train_loader):
  File "/Users/komoriii/anaconda3/envs/Image_processing/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 314, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/Users/komoriii/anaconda3/envs/Image_processing/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 314, in <listcomp>
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/Users/komoriii/anaconda3/envs/Image_processing/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/datasets/folder.py", line 105, in __getitem__
  File "/Users/komoriii/anaconda3/envs/Image_processing/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py", line 49, in __call__
  File "/Users/komoriii/anaconda3/envs/Image_processing/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py", line 175, in __call__
  File "/Users/komoriii/anaconda3/envs/Image_processing/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/functional.py", line 189, in resize
TypeError: img should be PIL Image. Got <class 'int'>

I have no idea on why the img is a integer not PIL image.
Here is my folder structure.
image
Here is my code

# data augmentation
composed = transforms.Compose([transforms.Resize((32,32)), 
                               transforms.ToTensor()
                               ])

# CIFAR10 dataset
#training_data = DataLoader(CIFAR10('dataset/', train=True, download=True, transform=transforms.ToTensor()), shuffle=True, batch_size=100)
#testing_data = DataLoader(CIFAR10('dataset/', train=False, download=True, transform=transforms.ToTensor()), shuffle=True)

# Jockey Dataset
dataset = ImageFolder('./classfication_data', target_transform=composed)

Thanks a lot.

Currently you are passing the composed transformation as the target_transform.
Since it consists of a Resize, I think you wanted to pass it as the transform instead.
Could you try to change that and see if it’s working?

1 Like

Thank you so much ptrblck! You are correct. I should read the document more carefully next time.