π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): accept an int mean or std in normalize and denormalize (#6061)
Saksham Sharmaecacb4743cd4
diff --git a/changelog.d/6061.fixed.md b/changelog.d/6061.fixed.mdnew file mode 100644index 000000000..51d2447de--- /dev/null+++ b/changelog.d/6061.fixed.md@@ -0,0 +1 @@+`kornia.enhance.normalize` and `kornia.enhance.denormalize` accept a Python `int` `mean` or `std` and treat it as the equal `float`, as `Normalize` and `Denormalize` already did. `denormalize(x, 0, 255)` and `normalize(x, 0.5, 2)` raised `AttributeError: 'int' object has no attribute 'shape'`.diff --git a/kornia/enhance/normalize.py b/kornia/enhance/normalize.pyindex 8370800c3..a9467f887 100644--- a/kornia/enhance/normalize.py+++ b/kornia/enhance/normalize.py@@ -189,10 +189,10 @@ def normalize(data: torch.Tensor, mean: torch.Tensor, std: torch.Tensor) -> torcf"Try changing mean shape and std shape from ({mean.shape}, {std.shape}) to (1, C) or (1, C, 1, 1).")else:- if isinstance(mean, float):+ if isinstance(mean, (int, float)):mean = torch.tensor([mean] * shape[1], device=data.device, dtype=data.dtype)- if isinstance(std, float):+ if isinstance(std, (int, float)):std = torch.tensor([std] * shape[1], device=data.device, dtype=data.dtype)# Allow broadcast on channel dimension@@ -360,10 +360,10 @@ def denormalize(data: torch.Tensor, mean: Union[torch.Tensor, float], std: Unionif mean.shape[0] != 1 or std.shape[0] != 1:raise ValueError("Batch dimension must be one for broadcasting when converting to ONNX.")else:- if isinstance(mean, float):+ if isinstance(mean, (int, float)):mean = torch.tensor([mean] * shape[1], device=data.device, dtype=data.dtype)- if isinstance(std, float):+ if isinstance(std, (int, float)):std = torch.tensor([std] * shape[1], device=data.device, dtype=data.dtype)# Allow broadcast on channel dimensiondiff --git a/tests/enhance/test_normalize.py b/tests/enhance/test_normalize.pyindex 2911d3008..d3f92180c 100644--- a/tests/enhance/test_normalize.py+++ b/tests/enhance/test_normalize.py@@ -459,6 +459,46 @@ class TestDenormalize(BaseTester):pass+class TestNormalizeIntegerStatistics(BaseTester):+ """A Python ``int`` ``mean`` or ``std`` is handled like the equal ``float`` (#6060).++ The functions wrapped only a ``float`` statistic into a tensor, so an ``int`` reached ``mean.shape`` and raised+ ``AttributeError``, although ``Normalize(0, 255)`` and ``Denormalize(0, 255)`` accepted the same values.+ """++ @pytest.mark.parametrize("fn", ["normalize", "denormalize"])+ @pytest.mark.parametrize("mean, std", [(0, 255), (0.0, 255), (0, 255.0), (1, 2)])+ def test_int_statistics_match_the_float_ones_6060(self, device, dtype, fn, mean, std):+ op = getattr(kornia.enhance, fn)+ g = torch.Generator().manual_seed(0)+ img = torch.rand(2, 3, 4, 5, generator=g).to(device, dtype)++ out = op(img, mean, std)++ assert out.dtype == dtype+ assert out.shape == img.shape+ assert torch.equal(out, op(img, float(mean), float(std)))++ @pytest.mark.parametrize("fn", ["normalize", "denormalize"])+ def test_int_statistics_on_integer_input_6060(self, device, fn):+ op = getattr(kornia.enhance, fn)+ img = torch.tensor([[[[0, 128, 255]]]], device=device, dtype=torch.uint8)++ out = op(img, 0, 255) if fn == "normalize" else op(img, 1, 2)++ expected = [0.0, 128.0 / 255.0, 1.0] if fn == "normalize" else [1.0, 257.0, 511.0]+ self.assert_close(out, torch.tensor([[[expected]]], device=device))++ @pytest.mark.parametrize("module, fn", [("Normalize", "normalize"), ("Denormalize", "denormalize")])+ def test_modules_and_functions_agree_on_int_statistics_6060(self, device, dtype, module, fn):+ g = torch.Generator().manual_seed(0)+ img = torch.rand(2, 3, 4, 5, generator=g).to(device, dtype)++ out = getattr(kornia.enhance, fn)(img, 0, 255)++ assert torch.equal(out, getattr(kornia.enhance, module)(0, 255).to(device)(img))++class TestNormalizeIntegerInput(BaseTester):"""`normalize` and `denormalize` used to cast mean and std to an integer input's dtype (#5403).