π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): validate positive pyramid level counts (#5778)
peng xiang6d9a4a99c202
diff --git a/changelog.d/5778.breaking.md b/changelog.d/5778.breaking.mdnew file mode 100644index 000000000..91e1f67cb--- /dev/null+++ b/changelog.d/5778.breaking.md@@ -0,0 +1,5 @@+`build_pyramid` and `build_laplacian_pyramid` now reject non-positive integer `max_level` values with a named+validation error instead of returning a single level. Non-integer values, including booleans previously treated as+integers, are rejected consistently. `ImageRegistrator.register` builds its pyramids with `build_pyramid`, so+`pyramid_levels <= 0` (or a boolean) now raises the same error instead of registering at the full resolution only.+Positive integer levels and the existing padding behavior are unchanged.diff --git a/kornia/geometry/transform/pyramid.py b/kornia/geometry/transform/pyramid.pyindex 547ed9b6c..0c6a60fdb 100644--- a/kornia/geometry/transform/pyramid.py+++ b/kornia/geometry/transform/pyramid.py@@ -515,15 +515,9 @@ def build_pyramid(- each subsequent level is produced by :func:`pyrdown` (``floor(side / 2)``per dimension)- .. warning::- The ``max_level`` bounds check does not currently reject non-positive- **integer** values: passing an integer ``max_level <= 0`` returns the- same single-element list as ``max_level=1`` instead of raising. Tracked in- `#3927 <https://github.com/kornia/kornia/issues/3927>`_.-Args:input : the torch.Tensor to be used to construct the pyramid.- max_level: the number of pyramid levels to return, including the+ max_level: a positive integer giving the number of pyramid levels to return, including theunchanged original image as level 0.border_type: the padding mode to be applied before convolving.The expected modes are: ``'constant'``, ``'reflect'``,@@ -537,7 +531,7 @@ def build_pyramid("""KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])KORNIA_CHECK(- isinstance(max_level, int) or max_level < 0,+ isinstance(max_level, int) and not isinstance(max_level, bool) and max_level > 0,f"Invalid max_level, it must be a positive integer. Got: {max_level}",)@@ -592,15 +586,9 @@ def build_laplacian_pyramid(- border_type: ``'reflect'`` by default- align_corners: ``False`` by default- .. warning::- The ``max_level`` bounds check does not currently reject non-positive- **integer** values: passing an integer ``max_level <= 0`` returns the- same single-element list as ``max_level=1`` instead of raising. Tracked- in `#3927 <https://github.com/kornia/kornia/issues/3927>`_.-Args:input : the torch.Tensor to be used to construct the pyramid with shape :math:`(B, C, H, W)`.- max_level: the number of pyramid levels to return (see Convention+ max_level: a positive integer giving the number of pyramid levels to return (see Conventionabove for what each level contains).border_type: the padding mode to be applied before convolving.The expected modes are: ``'constant'``, ``'reflect'``,@@ -615,7 +603,7 @@ def build_laplacian_pyramid("""KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])KORNIA_CHECK(- isinstance(max_level, int) or max_level < 0,+ isinstance(max_level, int) and not isinstance(max_level, bool) and max_level > 0,f"Invalid max_level, it must be a positive integer. Got: {max_level}",)diff --git a/tests/geometry/transform/test_pyramid.py b/tests/geometry/transform/test_pyramid.pyindex 000ef474f..85bb3d1ea 100644--- a/tests/geometry/transform/test_pyramid.py+++ b/tests/geometry/transform/test_pyramid.py@@ -19,6 +19,7 @@ import pytestimport torchimport kornia+from kornia.core.exceptions import BaseErrorfrom testing.base import BaseTester@@ -202,6 +203,15 @@ class TestScalePyramid(BaseTester):self.assert_close(pyr[octave], ref_pyr[octave])+class TestPyramidLevelValidation(BaseTester):+ @pytest.mark.parametrize("op", ["build_pyramid", "build_laplacian_pyramid"])+ @pytest.mark.parametrize("max_level", [0, -1, -2, 1.5, -1.5, None, "2", True, False])+ def test_invalid_max_level_3927(self, op, max_level, device, dtype):+ image = torch.rand(1, 1, 8, 8, device=device, dtype=dtype)+ with pytest.raises(BaseError, match="Invalid max_level, it must be a positive integer"):+ getattr(kornia.geometry.transform, op)(image, max_level)++class TestBuildPyramid(BaseTester):def test_smoke(self, device, dtype):sample = torch.ones(1, 2, 4, 5, device=device, dtype=dtype)