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

username: mneira10
cooked: <p>Hi everyone,</p>
<p>I’m iterating through a dataset in the following way:</p>
<pre><code class="lang-python">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
</code></pre>
<p>When I run the code above, on the start of the for loop, I get the following error:</p>
<pre><code class="lang-bash">Traceback (most recent call last):
  File "testDataLoaders.py", line 18, in &lt;module&gt;
    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 &lt;dictcomp&gt;
    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 &lt;listcomp&gt;
    numel = sum([x.numel() for x in batch])
AttributeError: 'str' object has no attribute 'numel'
</code></pre>
<p>I am doing a fairly simple thing yet I can’t seem to get my head around the error. The dataset (evalset) follows <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener">the Dataset class documentation</a>.</p>
<p>Im using Python 3.7.4, pytorch 1.2.0 and CUDA V10.0.130.</p>
<p>Googling the error doesnt provide any insights. Has anyone experienced this? Any suggestions on how to proceed?</p>

username: mneira10
cooked: <p>The error seems to have to do with the fact that I am sorting the keys of a dictionary when returning in def <strong>getitem</strong>(self, idx): part of the HCIDataset</p>