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

8267 fix normalize intensity #8286

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,8 @@ def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor:
if self.divisor is not None and len(self.divisor) != len(img):
raise ValueError(f"img has {len(img)} channels, but divisor has {len(self.divisor)} components.")

img, *_ = convert_data_type(img, dtype=torch.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix. Seems we can remove the convert out side of the self._normalize to avoid convert multiple times here.

Copy link
Member

Choose a reason for hiding this comment

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

Should we also check that the dtype of img isn't float16/float32/float64 and only do the conversion in that case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it would be a little more rigorous. Although in the non channel-wise version, we do the conversion directly. But yes if we could do the check, would be better.


for i, d in enumerate(img):
img[i] = self._normalize( # type: ignore
d,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_normalize_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ def test_channel_wise(self, im_type):
normalized = normalizer(input_data)
assert_allclose(normalized, im_type(expected), type_test="tensor")

@parameterized.expand([[p] for p in TEST_NDARRAYS])
def test_channel_wise_int(self, im_type):
normalizer = NormalizeIntensity(nonzero=True, channel_wise=True)
input_data = im_type(torch.arange(1, 25).reshape(2, 3, 4))
expected = np.array(
[
[
[-1.593255, -1.3035723, -1.0138896, -0.7242068],
[-0.4345241, -0.1448414, 0.1448414, 0.4345241],
[0.7242068, 1.0138896, 1.3035723, 1.593255],
],
[
[-1.593255, -1.3035723, -1.0138896, -0.7242068],
[-0.4345241, -0.1448414, 0.1448414, 0.4345241],
[0.7242068, 1.0138896, 1.3035723, 1.593255],
],
]
)
normalized = normalizer(input_data)
assert_allclose(normalized, im_type(expected), type_test="tensor", rtol=1e-7, atol=1e-7) # tolerance

@parameterized.expand([[p] for p in TEST_NDARRAYS])
def test_value_errors(self, im_type):
input_data = im_type(np.array([[0.0, 3.0, 0.0, 4.0], [0.0, 4.0, 0.0, 5.0]]))
Expand Down
Loading