π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): validate unique integral tensor dimensions (#5336)
NiteeshGowda727308ee0a5173
diff --git a/changelog.d/5336.breaking.md b/changelog.d/5336.breaking.mdnew file mode 100644index 000000000..2e477b628--- /dev/null+++ b/changelog.d/5336.breaking.md@@ -0,0 +1 @@+`integral_tensor` and `IntegralTensor` now raise a kornia error for a repeated axis, including a negative alias of a positive one such as `(1, -1)` on a rank-2 input, and for an axis outside the input's rank. A repeated axis used to be integrated twice (`integral_tensor(torch.ones(2, 2), (1, 1))` returned `[[1, 3], [1, 3]]`), and an axis outside the rank raised `IndexError` from `cumsum`. Distinct axes are accepted in any order.diff --git a/kornia/enhance/integral.py b/kornia/enhance/integral.pyindex abfce9ccf..a66e6ebe0 100644--- a/kornia/enhance/integral.py+++ b/kornia/enhance/integral.py@@ -28,8 +28,8 @@ def integral_tensor(input: torch.Tensor, dim: Optional[Tuple[int, ...]] = None)The algorithm computes the integral image by summing over the specified dimensions.- In case dim is specified, the contained dimensions must be unique and sorted in ascending order- and not exceed the number of dimensions of the input torch.Tensor.+ In case dim is specified, the contained dimensions must be unique+ and within the dimensions of the input torch.Tensor.Args:input: the input torch.Tensor with shape :math:`(*, D)`. Where D is the number of dimensions.@@ -55,8 +55,18 @@ def integral_tensor(input: torch.Tensor, dim: Optional[Tuple[int, ...]] = None)KORNIA_CHECK(len(dim) > 0, "dim must be a non-empty tuple.")KORNIA_CHECK(len(dim) <= len(input.shape), "dim must be a tuple of length <= input.shape.")+ normalized_dim = tuple(d if d >= 0 else d + input.ndim for d in dim)+ KORNIA_CHECK(+ all(0 <= d < input.ndim for d in normalized_dim),+ "dim must contain valid dimensions for the input.",+ )+ KORNIA_CHECK(+ len(set(normalized_dim)) == len(normalized_dim),+ "dim must contain unique dimensions.",+ )+output = input- for i in dim:+ for i in normalized_dim:output = output.cumsum(i)return outputdiff --git a/tests/enhance/test_integral.py b/tests/enhance/test_integral.pyindex cfac51cbf..2737c73a1 100644--- a/tests/enhance/test_integral.py+++ b/tests/enhance/test_integral.py@@ -45,6 +45,26 @@ class TestIntegralTensor(BaseTester):integral_tensor(tensor, ())assert "dim must be a non-empty tuple." in str(errinfo)+ @pytest.mark.parametrize("dim", [(1, 1), (-1, 1), (1, -1)])+ def test_duplicate_dimensions(self, device, dtype, dim):+ tensor = torch.ones(2, 2, device=device, dtype=dtype)+ with pytest.raises(Exception, match="unique dimensions"):+ integral_tensor(tensor, dim)++ @pytest.mark.parametrize("dim", [(2,), (-3,), (0, -3)])+ def test_out_of_range_dimensions(self, device, dtype, dim):+ # -3 + 2 = -1 is a valid cumsum axis, so a missing bounds check would integrate the last axis silently.+ tensor = torch.ones(2, 2, device=device, dtype=dtype)+ with pytest.raises(Exception, match="valid dimensions"):+ integral_tensor(tensor, dim)++ @pytest.mark.parametrize("dim", [(0, 1), (1, 0), (-2, -1)])+ def test_distinct_dimensions(self, device, dtype, dim):+ tensor = torch.ones(2, 2, device=device, dtype=dtype)+ output = integral_tensor(tensor, dim)+ expected = torch.tensor([[1, 2], [2, 4]], device=device, dtype=dtype)+ self.assert_close(output, expected)+def test_module(self, device, dtype):mod = IntegralTensor()op = integral_tensor