RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. pls help

Hi All,
I am getting an error -
File “c:\Users\Paritosh\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 232, in
return [default_collate(samples) for samples in transposed]
File “c:\Users\Paritosh\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py”, line 218, in default_collate
return torch.stack([torch.from_numpy(b) for b in batch], 0)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 5 and 8 in dimension 1 at c:\a\w\1\s\tmp_conda_3.6_122405\conda\conda-bld\pytorch_1549283197917\work\aten\src\th\generic/THTensorMoreMath.cpp:1307

While I am using batchsize=1 then I am not getting any error.

Could anybody help me with the issue?

2 Likes

You should check the size of all data returned in your Dataset, and you will find the one with different shapes (5 or 8).

It was tensor shape issues. Thanks Yun Chen:grinning:

Could I ask the stupid question, what’s the meaning of “different shapes (5 or 8).” ? For e.g., My input shape is (3,512,512)(chw) and the outpust shape is (80, 128,128), if some data get shape 5 or 8, which dimension of the data is 5 or 8 :smiley:

I am having the same error which also doesn’t occur when I use a batch size of 1.

Looped through the dataset and can confirm all images are 3x224x224 and all annotations are 1x224x224.

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 224 and 244 in dimension 2 at /tmp/pip-req-build-9oilk29k/aten/src/TH/generic/THTensor.cpp:689

How is that happening? It even says 224 and 224 in dimension 2 which doesn’t make sense. Shouldn’t this exception occur when they aren’t matching?

The error states that the shapes are 224 vs. 244 (note the changed 2 and 4), so I guess you might have a similar typo somewhere in your code? :slight_smile:

1 Like

Thanks a lot! I already solved this.

I asked this as a separate question here and on SO. Then I noticed that it is not 224x224 but 224x244 and got mad. I am so blind. Anyways changing that one image manually in my case (or using a resize transformation) did the job.

PS: I have noticed how much you help on this forum even before signing up. You are the real MVP.