-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Make NormalizeIntensity invertible (#5647) #8905
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from parameterized import parameterized | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from monai.data import MetaTensor, set_track_meta | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from monai.transforms import NormalizeIntensity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from tests.test_utils import TEST_NDARRAYS, NumpyImageTestCase2D, assert_allclose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -138,6 +139,30 @@ def test_value_errors(self, im_type): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with self.assertRaises(ValueError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalizer(input_data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @parameterized.expand( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ["global_computed", {}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ["channelwise_computed", {"channel_wise": True}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ["global_explicit", {"subtrahend": 2.0, "divisor": 3.0}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ["channelwise_explicit", {"subtrahend": [1.0, 2.0, 3.0], "divisor": [2.0, 3.0, 4.0], "channel_wise": True}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_inverse(self, _, args): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set_track_meta(True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| img = MetaTensor(torch.randn(3, 6, 6) * 5 + 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalizer = NormalizeIntensity(**args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out = normalizer(img.clone()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inv = normalizer.inverse(out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_allclose(inv, img, type_test=False, rtol=1e-4, atol=1e-4) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_inverse_nonzero_not_implemented(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set_track_meta(True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| img = MetaTensor(torch.randn(2, 5, 5)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalizer = NormalizeIntensity(nonzero=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out = normalizer(img.clone()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with self.assertRaises(NotImplementedError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| normalizer.inverse(out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+151
to
+164
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restore These tests set global metadata tracking to Suggested fix-from monai.data import MetaTensor, set_track_meta
+from monai.data import MetaTensor, get_track_meta, set_track_meta
...
def test_inverse(self, _, args):
- set_track_meta(True)
+ prev = get_track_meta()
+ self.addCleanup(set_track_meta, prev)
+ set_track_meta(True)
...
def test_inverse_nonzero_not_implemented(self):
- set_track_meta(True)
+ prev = get_track_meta()
+ self.addCleanup(set_track_meta, prev)
+ set_track_meta(True)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid storing
Nonein invertibleextra_info.For
nonzero=True+ all-zero mask,_normalize()returnsNonestats, then_push_transform_with_stats()serializes them intoextra_info. That can break transform-history collation in batched pipelines.Suggested fix
Also applies to: 977-980
🤖 Prompt for AI Agents