-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Squeezing functionality in Monai transforms #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @martaranzini , Thanks for your feedback and detailed sample code. |
Hi @Nic-Ma, Thank you for getting back to me. Basically I want to train on 2D slices from 3D medical images, and all the images have with different size. I created the following chain of transforms to create the dataset for PyTorch DataLoader: train_transforms = Compose([
LoadNiftid(keys=['img', 'seg']),
AddChanneld(keys=['img', 'seg']),
NormalizeIntensityd(keys=['img']),
Resized(keys=['img'], spatial_size=[96, 96], order=1),
Resized(keys=['seg'], spatial_size=[96, 96], order=0, anti_aliasing=False),
RandSpatialCropd(keys=['img', 'seg'], roi_size=[96, 96, 1], random_size=False),
ToTensord(keys=['img', 'seg'])
])
check_ds = monai.data.Dataset(data=check_train_files, transform=train_transforms) The Resized transform resizes my inputs to (batch, channel, 96, 96, N) with N being the original number of slices along z, which is different for each image. I used RandSpatialCrop to extract the single slices (i.e. my 2D patches) along the z direction. However, if I set roi_size=[96, 96] instead of roi_size=[96, 96, 1], it preserves the size of z for each image, and thus it crashes as the images are not of the same size. I get this error message: With the transform I posted previously the train_transform for my case becomes train_transforms = Compose([
LoadNiftid(keys=['img', 'seg']),
AddChanneld(keys=['img', 'seg']),
NormalizeIntensityd(keys=['img']),
Resized(keys=['img'], spatial_size=[96, 96], order=1),
Resized(keys=['seg'], spatial_size=[96, 96], order=0, anti_aliasing=False),
RandSpatialCropd(keys=['img', 'seg'], roi_size=[96, 96, 1], random_size=False),
SqueezeDimd(keys=['img', 'seg'], dim=-1),
ToTensord(keys=['img', 'seg'])
]) and in my case it was effective in changing the patches obtained from RandSpatialCropd from [96, 96, 1] to [96, 96]. |
Hi @martaranzini , Thanks for your overall explanation, now it makes a lot of sense to me.
Thanks. |
Is your feature request related to a problem? Please describe.
I would like to feed 2D patches extracted from 3D images into a 2D U-Net, and I could not find a straightforward way to remove the redundant dimension. E.g. from size (B, C, X, Y, 1) to (B, C, X, Y), with B being batch size and C number of channels.
Describe the solution you'd like
A Monai transform that could perform similar to numpy.squeeze and composable for data preprocessing.
Additional context
My attempt to such transform:
The text was updated successfully, but these errors were encountered: