TypeError: batch must contain tensors, numbers, dicts or lists; found <class 'PIL.PngImagePlugin.PngImageFile'>

Despite of doing


transform = transforms.Compose([transforms.Resize(256),  transforms.ToTensor()])



voc_data = torchvision.datasets.VOCSegmentation('/home/as/Desktop/mi/datasets/', download = True, transform=transform)
data_loader = torch.utils.data.DataLoader(voc_data, batch_size)

Getting error"

Using downloaded and verified file: /home/as/Desktop/mi/datasets/VOCtrainval_11-May-2012.tar
Traceback (most recent call last):
  File "main.py", line 108, in <module>
    train(10)
  File "main.py", line 63, in train
    for batch_idx, image, segment in enumerate(data_loader):
  File "/home/as/vir_env/mine/local/lib/python2.7/site-packages/torch/utils/data/dataloader.py", line 560, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/as/vir_env/mine/local/lib/python2.7/site-packages/torch/utils/data/_utils/collate.py", line 68, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/as/vir_env/mine/local/lib/python2.7/site-packages/torch/utils/data/_utils/collate.py", line 70, in default_collate
    raise TypeError((error_msg_fmt.format(type(batch[0]))))
TypeError: batch must contain tensors, numbers, dicts or lists; found <class 'PIL.PngImagePlugin.PngImageFile'>

Hello. I was facing the same problem. Do you have any idea about this now?

Hello.I am facing the same problem too…Is there any solution about this issue?

I had the same problem, the solution is to also set target_transform in the dataset. I believe it is the masks that are causing the error, and simply converting them to tensors fixed the error for me; haven’t trained a model yet so can’t say for sure that’s the right thing to do.