DataLoader for 2D images of different sizes #6096
-
Hi, I am trying to do segmentation on 2D microscopy images. Usually, these images are pretty large, and the images in my dataset have various sizes. I am following the 2D-unet tutorial but the dataloader cannot properly load the images: the So for training, an ideal scenario would be to crop the images into 256x256 non-overlapping contiguous patches. I don't mind zero padding a few patches to avoid resizing. However, I can't really find a way to do it. For example, the following is almost what I want, using train_transforms = Compose(
[
LoadImaged(keys=["im", "seg-ax", "seg-my"]),
EnsureChannelFirstd(keys=["im", "seg-ax", "seg-my"]),
NormalizeIntensityd(keys="im"),
# for affine and elastic transforms, adapted from default-SEM-model/model_seg_rat_axon-myelin_sem/model_seg_rat_axon-myelin_sem.json
# see https://github.com/axondeepseg/default-SEM-model
RandAffined(
keys=["im", "seg-ax", "seg-my"],
prob=1.0,
rotate_range=np.pi/64,
scale_range=0.05,
translate_range=(10, 10),
padding_mode="zeros",
device=device
),
Rand2DElasticd(
keys=["im", "seg-ax", "seg-my"],
prob=0.5,
spacing=(30, 30),
magnitude_range=(1, 2),
padding_mode="zeros",
device=device,
),
GridPatchd(keys=["im", "seg-ax", "seg-my"], patch_size=[256, 256])
]
)
check_ds = monai.data.Dataset(data=train_files, transform=train_transforms)
check_loader = DataLoader(check_ds, batch_size=2, num_workers=4, collate_fn=list_data_collate)
check_data = monai.utils.misc.first(check_loader)
print(check_data["im"].shape, check_data["seg-ax"].shape, check_data["seg-my"]) But
I understand Similar to #678 but I am really trying to avoid resizing the images to a common size because my annotations are large and detailed and I'm concerned it would degrade them. Would you have any recommendation on best practices for such a case? Would the best solution be to write my own collate function to pass to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @hermancollin, maybe you could try to use |
Beta Was this translation helpful? Give feedback.
Hi @hermancollin, maybe you could try to use
GridPatchDataset
withoutGridPatchd
.https://github.com/Project-MONAI/MONAI/blob/dev/monai/data/grid_dataset.py#L133
Hope it can help you, thanks!