Error in /torch/utils/data/_utils/collate.py: AttributeError: 'str' object has no attribute 'numel'

Hi everyone,

I’m iterating through a dataset in the following way:

from data_loader.hci_benchmark import HCIDataset
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize(
                                mean=[0.485, 0.456, 0.406],
                                std=[0.229, 0.224, 0.225])
                                ])

evalset = HCIDataset('test', False)

eval_loader = DataLoader(evalset,
                         batch_size=2,
                         pin_memory=False,
                         num_workers=2)

for i, sample in enumerate(eval_loader):
    print(i)
    for k in sample.keys():
        print(k)
    break

When I run the code above, on the start of the for loop, I get the following error:

Traceback (most recent call last):
  File "testDataLoaders.py", line 18, in <module>
    for i, sample in enumerate(eval_loader):
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 819, in __next__
    return self._process_data(data)
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 846, in _process_data
    data.reraise()
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/_utils.py", line 369, in reraise
    raise self.exc_type(msg)
AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 75, in default_collate
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 75, in <dictcomp>
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 53, in default_collate
    numel = sum([x.numel() for x in batch])
  File "/home/***/anaconda3/envs/epinet/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 53, in <listcomp>
    numel = sum([x.numel() for x in batch])
AttributeError: 'str' object has no attribute 'numel'

I am doing a fairly simple thing yet I can’t seem to get my head around the error. The dataset (evalset) follows the Dataset class documentation.

Im using Python 3.7.4, pytorch 1.2.0 and CUDA V10.0.130.

Googling the error doesnt provide any insights. Has anyone experienced this? Any suggestions on how to proceed?

1 Like

The error seems to have to do with the fact that I am sorting the keys of a dictionary when returning in def getitem(self, idx): part of the HCIDataset