Hi,
I have a custom dataset class. In the getitem() function it returns a tensor with shape [3,300,300] and a target dictionary. When I initialize the dataloder with batch size of 1 and iterate over this dataloader, I’m able to print the shapes of all tensors and have confirmed this.
When I increase the batch size > 1, I run into the following error when trying to iterate over the dataloader:
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 79, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 79, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 74, in default_collate
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 74, in <dictcomp>
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 64, in default_collate
    return default_collate([torch.as_tensor(b) for b in batch])
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 2 and 1 in dimension 1 at /pytorch/aten/src/TH/generic/THTensor.cpp:612

I’m stumped. My tensors are the same shape, so why can’t they be stacked?
Edit - I believe it might be an issue with the target. When __getitem__ returns image, {}, this issue does not appear. Here is my get item code:
    def __getitem__(self, index):
      image_info = self.data[index]
      image = self._read_image(image_info['image_id'])
      # duplicate boxes to prevent corruption of dataset
      boxes = copy.copy(image_info['boxes'])
      # duplicate labels to prevent corruption of dataset
      labels = copy.copy(image_info['labels'])
      # Create target
      target = {}
      target['boxes'] = boxes
      target['labels'] = labels 
      target['image_id'] = image_info['image_id']
      # Perform transformations that model expects.
      if self.transforms:
        image = self.transforms(image)
      return image, target

Edit2 - It looks like the issue is that some images have two annotations where others have one and this is why stacking does not work. Below I printed the two targets that might be incompatible for stacking:
{'boxes': array([[0.288125, 0.561205, 0.600625, 0.952919],
       [0.285   , 0.464218, 0.655625, 0.702448]], dtype=float32), 'labels': array([1, 1]), 'image_id': '6c3ba7b8844e1ab5'}
{'boxes': array([[0.065   , 0.405833, 0.90375 , 1.      ]], dtype=float32), 'labels': array([1]), 'image_id': '4559641996704238'}

Any ideas how to resolve this?