MONAI icon indicating copy to clipboard operation
MONAI copied to clipboard

Make `apply_pending_transforms` optional in `execute_compose`

Open function2-llx opened this issue 8 months ago • 10 comments

Is your feature request related to a problem? Please describe. Suppose I'm defining transform as follows:

from monai import transforms as mt
crop_t = mt.OneOf(
    [
        mt.RandSpatialCropD(['img', 'seg'], roi_size=(8, 8)),
        mt.RandCropByPosNegLabelD(['img', 'seg'], label_key='seg', spatial_size=(8, 8)),
    ],
    lazy=True,
)
rotate_t = mt.RotateD(['img', 'seg'], np.pi / 3)
t = mt.Compose([crop_t, rotate_t], lazy=True)

What I intend to do here is to crop a patch from the image with one of the two methods (similar to what nnU-Net does), and rotate the cropped patch afterwards. It will be very nice if I can lazy resample the patch after both crop and rotation (, and which should be possible). However, OneOf is a subclass of Compose, and it calls execute_compose, which always calls apply_pending_transforms at last, which makes the cropped patch resampled in advance and causes information loss during the subsequent rotation.

https://github.com/Project-MONAI/MONAI/blob/0a3cdd9f1403cbad057b96230840ee17cf430dbf/monai/transforms/compose.py#L476-L487

https://github.com/Project-MONAI/MONAI/blob/0a3cdd9f1403cbad057b96230840ee17cf430dbf/monai/transforms/compose.py#L114

Describe the solution you'd like We can add a parameter to execute_compose, and the subclasses of Compose if necessary, with a name like apply_pendding indicating if the pending operations should be applied.

Additional context This was originally proposed in a discussion (the second question). It seems that my feature request is not clearly conveyed. But I believe this is a very useful feature (as my example shows), therefore I'd like to propose again here.

function2-llx avatar Oct 18 '23 07:10 function2-llx