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

monai.transforms.intensity.array.NormalizeIntensity bug when __call__ with channel_wise=True #8267

Closed
Elferodie opened this issue Dec 19, 2024 · 2 comments · Fixed by #8286
Closed

Comments

@Elferodie
Copy link

Elferodie commented Dec 19, 2024

Describe the bug
In the __call__ method of monai.transforms.intensity.array.NormalizeIntensity class, the conversion into torch.float32 performed in self._normalize line 911, is not taken into account when channel_wise = True. The following code does not define a new variable img with the right type but it keeps the type of the original img variable. Therefore, if the original image was filled with int, the normalization will be performed on int and the normalization will fail.

for i, d in enumerate(img):
    img[i] = self._normalize(  # type: ignore
        d,
        sub=self.subtrahend[i] if self.subtrahend is not None else None,
        div=self.divisor[i] if self.divisor is not None else None,
    )

To Reproduce
Steps to reproduce the behavior:
Run commands

import torch
from monai.transforms.intensity.array import NormalizeIntensity
tensor = torch.arange(24).reshape(2,3,4)
normalized_correctly = NormalizeIntensity()(tensor)
normalized_corrupted = NormalizeIntensity(channel_wise=True)(tensor)
print(normalized_correctly)
print(normalized_corrupted)

Expected behavior

>>> normalized_correctly
metatensor([[[-1.6613, -1.5169, -1.3724, -1.2279],
         [-1.0835, -0.9390, -0.7945, -0.6501],
         [-0.5056, -0.3612, -0.2167, -0.0722]],

        [[ 0.0722,  0.2167,  0.3612,  0.5056],
         [ 0.6501,  0.7945,  0.9390,  1.0835],
         [ 1.2279,  1.3724,  1.5169,  1.6613]]])
>>> normalized_corrupted
metatensor([[[-1., -1., -1.,  0.],
         [ 0.,  0.,  0.,  0.],
         [ 0.,  1.,  1.,  1.]],

        [[-1., -1., -1.,  0.],
         [ 0.,  0.,  0.,  0.],
         [ 0.,  1.,  1.,  1.]]])
@advcu987
Copy link
Contributor

@Elferodie did you fix it? otherwise, can I take a look at it?

@Elferodie
Copy link
Author

No I didn’t fix it, you can take a look, thank you !

advcu987 added a commit to advcu987/MONAI that referenced this issue Jan 8, 2025
    Fix channel-wise intensity normalization for integer type inputs.

    Signed-off-by: Adrian Voicu <[email protected]>
advcu987 added a commit to advcu987/MONAI that referenced this issue Jan 8, 2025
advcu987 added a commit to advcu987/MONAI that referenced this issue Jan 8, 2025
    Fix channel-wise intensity normalization for integer type inputs.

    Signed-off-by: Adrian Voicu <[email protected]>
KumoLiu pushed a commit that referenced this issue Jan 20, 2025
Fixes #8267 .

### Description

Fix channel-wise intensity normalization for integer type inputs. 

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [ ] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [x] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [x] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: advcu987 <[email protected]>
Signed-off-by: advcu <[email protected]>
Co-authored-by: Eric Kerfoot <[email protected]>
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 a pull request may close this issue.

2 participants