Skip to content

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

Closed

Description

@Elferodie

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

Activity

  1. advcu987 commented on Dec 27, 2024

    @advcu987
    Contributor

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

  2. Elferodie commented on Dec 27, 2024

    @Elferodie
    Author

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

  3. added 3 commits that reference this issue on Jan 8, 2025
    b55a02e
    28a0df1
    bdb3604
  4. added a commit that references this issue on Jan 20, 2025
    e39bad9
  5. added a commit that references this issue on Feb 7, 2025
    c17938b
  6. added a commit that references this issue on Mar 10, 2025
    e516098
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions