Skip to content
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

Implement TorchIO transforms wrapper analogous to TorchVision transfo… #7579

Merged

Conversation

SomeUserName1
Copy link
Contributor

@SomeUserName1 SomeUserName1 commented Mar 25, 2024

…rms wrapper and test case

Fixes #7499 .

Description

As discussed in the issue, this PR implements a wrapper class for TorchIO transforms, analogous to the TorchVision transforms wrapper.

The test cases just check that transforms are callable and that after applying a transform, the result is different from the inputs.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

…rms wrapper and test case

Signed-off-by: Fabian Klopfer <[email protected]>
@SomeUserName1 SomeUserName1 force-pushed the 7499-torchio-transforms-wrapper branch from 0af6b6b to 2b37b94 Compare March 25, 2024 15:10
Signed-off-by: Fabian Klopfer <[email protected]>
@SomeUserName1
Copy link
Contributor Author

SomeUserName1 commented Mar 25, 2024

Not sure where I have to add the torchio dependency to make CI pass.
All min-dep checks will fail I guess as i put the dependency in requirements-dev.txt, environment-dev.yml and setup.cfg.
The premerge-quick should pass.

Signed-off-by: Fabian Klopfer <[email protected]>
@KumoLiu
Copy link
Contributor

KumoLiu commented Mar 26, 2024

Hi @SomeUserName1, you can add a skip wrapper like this:

@skipUnless(has_cim, "Requires cucim")

Thanks.

@SomeUserName1
Copy link
Contributor Author

One test failed, i have no idea why as the test and corresponding code is nothing that I've touched

Traceback (most recent call last):
  File "/home/runner/work/MONAI/MONAI/tests/test_regularization.py", line 73, in test_cutmixd
    self.assertTrue(not torch.allclose(output["a"], output["b"]))
AssertionError: False is not true

@ericspod ericspod requested a review from KumoLiu March 26, 2024 14:24
@KumoLiu
Copy link
Contributor

KumoLiu commented Mar 26, 2024

Looks like a random error, fixed in this commit.
40bb130

@SomeUserName1
Copy link
Contributor Author

@KumoLiu only the blossom-ci check hasn't passed. Can we merge?

@KumoLiu
Copy link
Contributor

KumoLiu commented Mar 27, 2024

Hi @SomeUserName1, thanks for the PR! We might consider incorporating this new feature after the 1.3.1 release due to the new requirements included in this PR.

Copy link
Contributor

@johnzielke johnzielke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this by accident, so first of all: Really nice, I'd love to be able to easily use some of the transforms in torchio, and I think it helps the available transforms there gain visibility.

While looking at this, I had a few thoughts:

  1. It would be nice to have this available for the dictionary versions as well (like torchvisiond)
  2. The transform does currently not implement the RandomizableTrait, so I'm not sure how this would interact with CacheDatasets etc. I think it might help to have a version with the RandomizableTrait for all Random transforms (it seems like these are easy to find since they start with "Random") and one without for the rest. I'm not sure how to best design this, but one could maybe use the __new__() method to select the correct one based on the name. Alternatively an error/warning could be thrown if the wrong version is used.

monai/transforms/utility/array.py Outdated Show resolved Hide resolved
monai/transforms/utility/array.py Outdated Show resolved Hide resolved
@SomeUserName1
Copy link
Contributor Author

1. It would be nice to have this available for the dictionary versions as well (like [torchvisiond](https://docs.monai.io/en/stable/transforms.html#torchvisiond))

Implemented.

2. The transform does currently not implement the RandomizableTrait, so I'm not sure how this would interact with CacheDatasets etc. I think it might help to have a version with the RandomizableTrait for all Random transforms (it seems like these are easy to find since they start with "Random") and one without for the rest. I'm not sure how to best design this, but one could maybe use the `__new__()` method to select the correct one based on the name. Alternatively an error/warning could be thrown if the wrong version is used.

They also have a common RandomTransform base class in tio, which differs from MONAIs base RandomTransform.
All tio transforms are probabilistic and support the p= parameter specifying the probability that the transform is applied. This is analogous to RandomizableTransform in MONAI.
The RandomTransform in tio states that there is randomness in the applied transform itself (like RandomNoise), additionally that its application (or not application) is probabilistic.

Thus, I add the RandomizableTrait as ABC to the TorchIO wrapper for all transforms.

I'm not sure if I should add Randomizable or RandomTranform as well as this is implemented in torchio already. So adding these two would be clearer in terms of the capabilities of these transforms but require manipulating TorchIO objects/class instances in MONAI code introducing complexity with little practical value (probabilistic application can be done via the tio parameters; is there a benefit in adding the same functionality via MONAI with the different concepts in mind?)

…sses to TorchIO and Transform to TorchVision. document conversion for torchvision and remove conversion for torchio. Add dtypes to docstring for tio

Signed-off-by: Fabian Klopfer <[email protected]>
…orms-wrapper' into 7499-torchio-transforms-wrapper

Signed-off-by: Fabian Klopfer <[email protected]>
Copy link
Contributor

@johnzielke johnzielke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!
Regarding the p argument. One could potentially use the Monai RandomizableTransform and it's prob parameter, and then force p=1 to only use the monai code. The only benefit of this in my opinion is a slightly easier understandable interface on the MONAI side, and that the randomstate from Monai is used. But I'm not sure it's worth it / necessary. As a intermediate way, you could consider rerouting the "prob" parameter to "p" as well (ensuring only one of them is set at any time)

monai/transforms/utility/array.py Show resolved Hide resolved
monai/transforms/utility/dictionary.py Outdated Show resolved Hide resolved
Signed-off-by: Fabian Klopfer <[email protected]>
…ct. add tags file generated by ctags to gitignore, make `prob` and `p` kwargs mutually exclusive for TorchIO transforms and initialize `p` with `prob` if the latter was provided. Add test cases for applying the same transform

Signed-off-by: Fabian Klopfer <[email protected]>
Copy link
Contributor

@KumoLiu KumoLiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed review; the point release is finally done. I have some concerns about the support for randomness, leave the comments inline.

@ericspod could you please also help review the PR? Thanks!

docs/source/transforms.rst Outdated Show resolved Hide resolved
docs/source/transforms.rst Outdated Show resolved Hide resolved
monai/transforms/utility/array.py Outdated Show resolved Hide resolved
monai/transforms/utility/array.py Show resolved Hide resolved
monai/transforms/utility/dictionary.py Outdated Show resolved Hide resolved
@KumoLiu KumoLiu requested review from ericspod and Nic-Ma May 31, 2024 08:51
Co-authored-by: YunLiu <[email protected]>
Signed-off-by: Fabian Klopfer <[email protected]>
@KumoLiu
Copy link
Contributor

KumoLiu commented Nov 11, 2024

Hi @SomeUserName1, apologies for overlooking this PR. I’ll review it and aim to have it merged by the end of the week. Thanks.

@ericspod
Copy link
Member

Is this something that you'd like to proceed with any time soon? otherwise I'd close the PR

Hi @SomeUserName1 I think it still stands that there's an issue of randomness. We can't change whether a transform is considered random or not at runtime since it's type-based, so either we have a random and non-random version of the transforms or state in the docstring to use only randomised TorchIO transforms. If you're good with the latter we can merge things sooner after a little review.

@SomeUserName1
Copy link
Contributor Author

No worries!

I agree that the randomness issue still stands and will implement the latter version.

[...] or state in the docstring to use only randomised TorchIO transforms.

@SomeUserName1
Copy link
Contributor Author

SomeUserName1 commented Nov 18, 2024

So, I added the changes as discussed.
Additionally, I added RandTorchVision as well to actually unify the wrappers as discussed in PR #5569 , mentioned by @KumoLiu.

tests/test_torchiod.py Outdated Show resolved Hide resolved
Signed-off-by: Fabian Klopfer <[email protected]>
@SomeUserName1
Copy link
Contributor Author

Locally the 4 unit test classes i wrote ran through, mypy and the formatters passed, so I hope this will be the last time, I'll kick off the pipeline.
If the pipeline runs green, please review.

Thanks for your time @ericspod & @KumoLiu !

Copy link
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good now, thanks!

@ericspod ericspod enabled auto-merge (squash) November 27, 2024 17:26
@KumoLiu
Copy link
Contributor

KumoLiu commented Nov 28, 2024

/build

@ericspod ericspod merged commit 44e249d into Project-MONAI:dev Nov 28, 2024
28 checks passed
@SomeUserName1 SomeUserName1 deleted the 7499-torchio-transforms-wrapper branch November 28, 2024 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random Motion artifacts for MRI
4 participants