RuntimeError: stack expects each tensor to be equal size, but got [1, 677, 347] at entry 0 and [1, 512, 512] at entry 1
Traceback (most recent call last):
File "train.py", line 104, in
@apsthe I think you should check if the size of the Image Train and Annotation image are the same, for example, if the Image Train is portrait but the Annotation is landscape.
And this relation with RandomResize and RandomCrop in the configs/_base_/datasets/xxx.py file
@apsthe You encounter this error because of how the padding function is designed. When you prepare the data pipeline in mmsegmentation/configs/base/datasets/your_dataset.py you use the RandomResize method. You need to specife the parameters scale and ratio_range. Let's consider the case where you provide a tuple for both and let's say you define the size of your input images to be 512 x 512. This input size will be the base size and the ratio range will have a upper and lower limit (let's agree on 0.5 - 2.0) of the original size of the input image. Then you need to set scale accordingly. I.e. scale=(1024, 256). Once these values agree the padding function for the image resizing will make sure that all images have the same size and you will no longer encouter this error.
Do you have any solutions now?
@NiklasDHahn What should I do if my dataset image sizes are inconsistent?Thanks! dict( keep_ratio=True, ratio_range=( 0.5, 2.0, ), scale=( 256, 1024, ), type='RandomResize'), dict( cat_max_ratio=0.75, crop_size=( 512, 512, ), type='RandomCrop'),
the reason is that the size of input image is different from annotation image size
'RandomResize' will got RuntimeError: stack expects each tensor to be equal size, but got [1, 512, 512] at entry 0 and [1, 512, 527] at entry 5. So I changed it to Resize. It works.
crop_size = (512, 512)
train_pipeline = [
dict(type="LoadImageFromFile"),
dict(type="LoadAnnotations", reduce_zero_label=False),
# dict(
# type='RandomResize',
# scale=(2048, 512),
# ratio_range=(0.5, 2.0),
# keep_ratio=True),
dict(
type="Resize",
scale=(2048, 512),
keep_ratio=True,
),
dict(type="RandomCrop", crop_size=crop_size, cat_max_ratio=0.75),
dict(type="RandomFlip", prob=0.5),
dict(type="PhotoMetricDistortion"),
dict(type="PackSegInputs"),
]