π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): avoid overflow in integer min-max normalization (#6022)
NIKHILe354e746fb5c
diff --git a/changelog.d/6022.fixed.md b/changelog.d/6022.fixed.mdnew file mode 100644index 000000000..93a0ce3c2--- /dev/null+++ b/changelog.d/6022.fixed.md@@ -0,0 +1 @@+`normalize_min_max` promotes integer and boolean images to torch's default floating dtype before subtracting, as `normalize` and `denormalize` already do. A signed image such as an `int8` one holding `-128` and `127` used to wrap and return values outside the requested range, and a boolean mask raised.diff --git a/kornia/enhance/normalize.py b/kornia/enhance/normalize.pyindex 2597b0646..57ee5cd89 100644--- a/kornia/enhance/normalize.py+++ b/kornia/enhance/normalize.py@@ -404,6 +404,8 @@ def normalize_min_max(Input is ``(*, C, H, W)``: minima and maxima are taken over H and W separately for everyleading index and channel. Constant planes map to min_val. Empty leading dimensions arepreserved; channel and spatial dimensions must be nonzero.+ Integer and boolean inputs are promoted to torch's default floating dtype first, as in+ :func:`normalize`; floating inputs keep their dtype.The data is normalised using the following formulation:@@ -445,6 +447,9 @@ def normalize_min_max(if not isinstance(max_val, float):raise TypeError(f"'max_val' should be a float. Got: {type(max_val)}.")+ if not input.is_floating_point() and not input.is_complex():+ # Promote as `_promote_integer_data` does; `* 1.0` also follows the default dtype under TorchScript.+ input = input * 1.0shape = input.shapex_reshaped = input.flatten(start_dim=-2)x_min = x_reshaped.min(-1, keepdim=True)[0]diff --git a/tests/enhance/test_normalize.py b/tests/enhance/test_normalize.pyindex d3f92180c..d11d2e920 100644--- a/tests/enhance/test_normalize.py+++ b/tests/enhance/test_normalize.py@@ -615,6 +615,77 @@ class TestNormalizeIntegerInput(BaseTester):class TestNormalizeMinMax(BaseTester):+ @pytest.mark.parametrize("input_dtype", [torch.int8, torch.int16, torch.int32, torch.int64])+ @pytest.mark.parametrize("shape", [(2, 3), (2, 2, 3), (2, 2, 2, 3), (2, 2, 2, 2, 3)])+ def test_signed_integer_range(self, device, input_dtype, shape):+ """Promote integer pixels before subtracting, so a span wider than the signed range does not wrap."""+ limits = torch.iinfo(input_dtype)+ pixels = torch.tensor([limits.min, 0, limits.max], device=device, dtype=input_dtype)+ image = pixels.repeat(torch.Size(shape).numel() // 3).reshape(shape).transpose(-1, -2)+ expected_row = [-1.0, 1.0 / (limits.max - limits.min), 1.0]+ expected = torch.tensor(expected_row, device=device).repeat(torch.Size(shape).numel() // 3)+ expected = expected.reshape(shape).transpose(-1, -2)++ actual = kornia.enhance.normalize_min_max(image, min_val=-1.0, max_val=1.0)++ assert actual.dtype == torch.get_default_dtype()+ assert actual.shape == image.shape+ assert actual.device == image.device+ self.assert_close(actual, expected)++ @pytest.mark.parametrize(+ "input_dtype", [torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64, torch.bool]+ )+ def test_integer_and_bool_values(self, device, input_dtype):+ """Integer and boolean planes normalize to the same pixel values as their floating equivalents."""+ values = [0, 0, 1] if input_dtype == torch.bool else [1, 2, 3]+ image = torch.tensor(values, device=device, dtype=input_dtype).reshape(1, 1, 1, 3)+ expected = torch.tensor([0.0, 0.0, 1.0] if input_dtype == torch.bool else [0.0, 0.5, 1.0], device=device)+ actual = kornia.enhance.normalize_min_max(image)+ self.assert_close(actual.flatten(), expected)++ @pytest.mark.parametrize(+ "input_dtype", [torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64, torch.bool]+ )+ def test_constant_integer_plane(self, device, input_dtype):+ """Constant integer planes map to min_val, including a one-pixel boolean mask."""+ image = torch.ones(2, 3, 1, 1, device=device, dtype=input_dtype)+ actual = kornia.enhance.normalize_min_max(image, min_val=-2.0, max_val=3.0)+ self.assert_close(actual, torch.full(image.shape, -2.0, device=device))++ @pytest.mark.parametrize("scripted", [False, True])+ def test_integer_follows_default_dtype(self, device, scripted):+ """The integer promotion follows normalize/denormalize and torch's default floating dtype."""+ if device.type == "mps":+ pytest.skip("MPS does not support float64")+ image = torch.tensor([-128, 0, 127], device=device, dtype=torch.int8).reshape(1, 3)+ previous = torch.get_default_dtype()+ try:+ torch.set_default_dtype(torch.float64)+ op = kornia.enhance.normalize_min_max+ actual = (torch.jit.script(op) if scripted else op)(image)+ expected = torch.tensor([[0.0, 128.0 / (255.0 + 1e-6), 255.0 / (255.0 + 1e-6)]], device=device)+ assert actual.dtype == torch.float64+ self.assert_close(actual, expected, rtol=1e-12, atol=1e-12)+ finally:+ torch.set_default_dtype(previous)++ @pytest.mark.parametrize("input_dtype", [torch.int8, torch.bool])+ def test_integer_jit(self, device, input_dtype):+ """Scripted min-max normalization uses the same integer promotion as eager execution."""+ values = [-128, 0, 127] if input_dtype == torch.int8 else [0, 1, 0]+ image = torch.tensor(values, device=device, dtype=input_dtype).reshape(1, 3)+ op = kornia.enhance.normalize_min_max+ self.assert_close(torch.jit.script(op)(image), op(image.to(torch.get_default_dtype())))++ @pytest.mark.parametrize("input_dtype", [torch.int8, torch.bool])+ def test_integer_dynamo(self, device, input_dtype, torch_optimizer):+ """Compiled min-max normalization keeps integer promotion inside the captured operation."""+ values = [-128, 0, 127] if input_dtype == torch.int8 else [0, 1, 0]+ image = torch.tensor(values, device=device, dtype=input_dtype).reshape(1, 3)+ op = kornia.enhance.normalize_min_max+ self.assert_close(torch_optimizer(op)(image), op(image.to(torch.get_default_dtype())))+@pytest.mark.parametrize("shape", [(2, 3, 6, 8), (0, 3, 6, 8), (2, 0, 3, 6, 8)])def test_dynamo(self, shape, device, dtype, torch_optimizer):data = torch.rand(shape, device=device, dtype=dtype)