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

Open
Elferodie opened this issue Dec 19, 2024 · 0 comments

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

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.]]])
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

No branches or pull requests

1 participant