Project-MONAI / Project-MONAI/MONAI

Make `apply_pending_transforms` optional in `execute_compose`

Open
#7,139 10 comments 0 reactions 1 assignee View on GitHub

@atbenmurray is already working on this.

Since Jan 19, 2024.

Feature request
Dominant language
Python
Stars
8.7k
Forks
1.6k
Avg merge
5d 1h
Merged PRs (30d)
20

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.