Segmentation - 375x500px images/masks keeps blowing up - size of tensors must match

Trying to run my own dataset that has 375x500 images.
However, I keep blowing up during batch creation with

    File "/home/ubuntu/anaconda3/lib/python3.7/site-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 500 and 375 in dimension 2 at /pytorch/aten/src/TH/generic/THTensor.cpp:612

Is the issue that my images are not square? I see that camvid images are 960x720 so was assuming that should be ok as it can just pad it out.
Or is issue that images are odd (375px) rather than even (ala 376?).

If I use the aug_transforms with size = 187,500
batch_tfms=[*aug_transforms(size=(187,250)),Normalize.from_stats(*imagenet_stats)])

it will make a batch but then when I make a learner, it blows up with similar error:

/fastai2/fastai2/fastai2/data/load.py in fa_collate(t)
     43 def fa_collate(t):
     44     b = t[0]
---> 45     return (default_collate(t) if isinstance(b, _collate_types)
     46             else type(t[0])([fa_collate(s) for s in zip(*t)]) if isinstance(b, Sequence)
     47             else default_collate(t))

~/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     53             storage = elem.storage()._new_shared(numel)
     54             out = elem.new(storage)
---> 55         return torch.stack(batch, 0, out=out)
     56     elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
     57             and elem_type.__name__ != 'string_':

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 500 and 375 in dimension 2 at /pytorch/aten/src/TH/generic/THTensor.cpp:612

I’ll keep trying to debug but if anyone has input would appreciate it!

First lead is this from pytorch forums:

The error message notifies you that you should have equal width and height dimensions, which in your case did not happen. ’ Sizes of tensors must match except in dimension 0. Got 416 and 480 '.

Try putting a crop in your input transforms, e.g. transforms.RandomCrop(224) (or whichever size is required for YOLO V2). This fixed the dimensions mismatch in my case. Hopefully for yours too.

As a hack, I have set bs=1 which lets things run though that’s just hiding the problem.

Yes, this means exactly what it sounds like: your x’s and your y’s in a batch are not all the same size, so you cannot generate one. To test this, try manually grabbing two items and checking their sizes to be sure they are in fact the same size

And yes, IIRC they need to be even sized images so that resizing etc can go okay.

1 Like

alright thanks - I’m going to redo my dataset to be even sizes to avoid this issue though now I have to remake my segment masks.

but glad to learn this at pilot time and not later. :slight_smile:

btw - I find that almost all commercial segmentation tools (supervise.ly, hasty.ai) etc. do not have a ‘background’ concept. So you end up with masks that are random numbers except for what you have segmented.
I’ll have to make a MaskFix function to load, reset everything that is not in a class to background and save back out.

Look at my post on the useful segmentation tools, you can very easily refactor that to where it simply resets them all when you load it in (and do it lazily) rather than redo the entire dataset

1 Like