I am trying to load the cityscape dataset leftimg8bit and gtFine, downloaded from the website.
I am also using the DataLoader for city scape provided by PyTorch. However, I am getting the error " TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found
<class ‘PIL.PngImagePlugin.PngImageFile’>  " when enumerating the loaded data.
I have also already made sure to use transforms.ToTensor()
Any idea what I did wrong here?
An excerpt of my code:
data_transforms_train = transforms.Compose([
        transforms.Resize(256),
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])

    data_transforms_val = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
    ])

    train_dataset = torchvision.datasets.Cityscapes(os.path.join(root_dir, 'data/cityscapes'),
        split='train', mode='fine', target_type='semantic', transform=data_transforms_train)
    val_dataset = torchvision.datasets.Cityscapes(os.path.join(root_dir, 'data/cityscapes'),
        split='val', mode='fine', target_type='semantic', transform=data_transforms_val)

    train_loader = data.DataLoader(
        train_dataset,
        batch_size=batch_size,
        num_workers=num_workers,
        pin_memory=False,
        shuffle=True
    )
    val_loader = data.DataLoader(
        val_dataset,
        batch_size=batch_size,
        num_workers=num_workers,
        pin_memory=True,
        shuffle=False
    )


Blockquote
Traceback (most recent call last):
File “/Users/xavier/Programming/FishNet/main_semantic_segmentation.py”, line 271, in 
main()
File “/Users/xavier/Programming/FishNet/main_semantic_segmentation.py”, line 259, in main
train(train_loader, model, criterion, optimizer, epoch)
File “/Users/xavier/Programming/FishNet/main_semantic_segmentation.py”, line 99, in train
for i, data in enumerate(train_loader):
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/dataloader.py”, line 521, in next
data = self._next_data()
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/dataloader.py”, line 1203, in _next_data
return self._process_data(data)
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/dataloader.py”, line 1229, in _process_data
data.reraise()
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/_utils.py”, line 425, in reraise
raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/_utils/worker.py”, line 287, in _worker_loop
data = fetcher.fetch(index)
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py”, line 47, in fetch
return self.collate_fn(data)
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py”, line 84, in default_collate
return [default_collate(samples) for samples in transposed]
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py”, line 84, in 
return [default_collate(samples) for samples in transposed]
File “/Users/xavier/miniforge3/lib/python3.9/site-packages/torch/utils/data/_utils/collate.py”, line 86, in default_collate
raise TypeError(default_collate_err_msg_format.format(elem_type))
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class ‘PIL.PngImagePlugin.PngImageFile’>

Thanks!