Pytorch DataLoader RuntimeError

I met a strange Pytorch error and I can not fix it for a long time. I think this error is associated with the Pytorch DataLoader.
Here is the error info:

RuntimeError: Traceback (most recent call last):
  File "/home/leexy/softwares/anaconda3/lib/python3.6/site- 
packages/torch/utils/data/dataloader.py", line 55, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/leexy/softwares/anaconda3/lib/python3.6/site- 
packages/torch/utils/data/dataloader.py", line 135, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/leexy/softwares/anaconda3/lib/python3.6/site- 
packages/torch/utils/data/dataloader.py", line 135, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/home/leexy/softwares/anaconda3/lib/python3.6/site- 
packages/torch/utils/data/dataloader.py", line 112, in default_collate
    return torch.stack(batch, 0, out=out)
  File "/home/leexy/softwares/anaconda3/lib/python3.6/site- 
packages/torch/functional.py", line 66, in stack
    return torch.cat(inputs, dim, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in 
dimension 0. Got 3 and 1 in dimension 1 at /pytorch/torch/lib/TH/generic/THTensorMath.c:2897

And here is my traceback:

RuntimeError                              Traceback (most recent call last)
<ipython-input-16-28ab8a5aa636> in <module>()
----> 1 train_fc(resnet50, loss_fn, optimizer_fc, 
val_dataloader=validation_set_loader, num_epochs=40)#fig_loss_x_fc, 
fig_loss_y_fc = train_fc(resnet50_places365, loss_fn, optimizer_fc, 
num_epochs=40)

<ipython-input-13-949e2aa4f672> in train_fc(model, loss_fn, optimizer, 
val_dataloader, num_epochs)
     94         adjust_learning_rate(optimizer, 0.95)
---> 95         for t, (x, y) in enumerate(train_set_loader):
     96             y = y.numpy()
     97             y = y.reshape(-1)

~/softwares/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    265         if self.rcvd_idx in self.reorder_dict:
    266             batch = self.reorder_dict.pop(self.rcvd_idx)
--> 267             return self._process_next_batch(batch)
    268 
    269         if self.batches_outstanding == 0:

~/softwares/anaconda3/lib/python3.6/site- 
packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
    299         self._put_indices()
    300         if isinstance(batch, ExceptionWrapper):
--> 301             raise batch.exc_type(batch.exc_msg)
    302         return batch
    303 

I use the Pytorch DataSet and rewrite getitem() and len() method. I check the getitem() method and it can work. I make some DataAugmentation, such as Scale, RandomFlip. But I am sure every image is shaped to 224 * 224 and I’ve check it.
The most important is, this dataset is written some months ago and it can work in other imageset, so I really don’t know how to fix it.

Hi all.

I’ve solved this problem. The error is caused by my images, some of them are PNG format, thus these images have 4 channels. So I change them to the RGB and the problem is solved.

4 Likes

Please post a (complete) example of code that throws this error so I can try to replicate it.

Thank you for your reply.

I’ve solved this problem. The error is caused by my images, some of them are PNG format, thus these images have 4 channels. So I change them to the RGB and the problem is solved.

Thank you again.

1 Like

Thanks for the solution. I wish I had found this post like an hour earlier lol.
It’s exactly as you said. Worked like a charm.

Could you share how you converted to RGB?