Skip to content

Transforms with random size incompatible with default collater of data loader #1576

@rijobro

Description

@rijobro

Describe the bug
Transforms that produce images of variable size are incompatible with default collator, since this expects all images to be the same size.

Examples that I've found so far are (I guess same would apply to array versions)):

  • RandSpatialCropd (with random_size==True)
  • RandRotated (with keep_size==False)
  • RandZoomd (with keep_size==False)
  • RandRotate90d (if input wasn't square/cubic)

You wouldn't see this problem if you a) specify some custom collator, or b) followed up these transforms with other transforms that unified image shape (e.g., SpatialPadd). I can't imagine many people do the former, and for the latter I don't see the utility of allowing the image size to change if you are then going to crop/pad back to a fixed size.

Unless I've mistaken something, I suggest removing functionality that could result in images inside the same batch having different sizes.

To Reproduce

import numpy as np
from monai.data import CacheDataset, DataLoader
from monai.transforms import RandSpatialCropd, RandRotated

im = np.arange(0, 10 * 9).reshape(1, 10, 9)
data = [{"image": im} for _ in range(2)]

# alternate commenting these two lines
transform = RandSpatialCropd("image", roi_size=[8, 7], random_size=True)
transform = RandRotated("image", prob=1, range_x=np.pi, keep_size=False)

dataset = CacheDataset(data, transform)
loader = DataLoader(dataset, batch_size=2)
for _ in loader:
    pass

Example error:

stack expects each tensor to be equal size, but got [1, 13, 13] at entry 0 and [1, 10, 11] at entry 1
  File "/Users/rich/Documents/Code/MONAI/monai/data/utils.py", line 245, in list_data_collate
    return default_collate(data)
  File "/Users/rich/Desktop/text.py", line 14, in <module>
    for _ in loader:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions