π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): pad mixed-size laplacian pyramids (#3971)
ZZZZZ47ce8b974e08
diff --git a/changelog.d/3971.fixed.md b/changelog.d/3971.fixed.mdnew file mode 100644index 000000000..7e5970da9--- /dev/null+++ b/changelog.d/3971.fixed.md@@ -0,0 +1,2 @@+`build_laplacian_pyramid()` now pads mixed image sizes when needed to keep adjacent pyramid levels aligned, while+preserving the output shapes of sizes that already worked.diff --git a/kornia/geometry/transform/pyramid.py b/kornia/geometry/transform/pyramid.pyindex 2fed6babb..069016079 100644--- a/kornia/geometry/transform/pyramid.py+++ b/kornia/geometry/transform/pyramid.py@@ -578,24 +578,19 @@ def build_laplacian_pyramid(element (index ``max_level - 1``) is the unsubtracted final Gaussianlevel β level 0 is **not** the unchanged original image whenever``max_level > 1``- - the input is reflect-padded up to the next power of two (per- dimension) only when **neither** the height **nor** the width is- already a power of two; when it is applied, every returned level- β including level 0 β is shaped from the padded size+ - the input is reflect-padded up to the next power of two (per dimension)+ when neither side is already a power of two, or when ``max_level > 1``+ and a side is not divisible by ``2 ** (max_level - 1)``. When+ padding is applied, every returned level β including level 0 β is+ shaped from the padded size- border_type: ``'reflect'`` by default- align_corners: ``False`` by default.. warning::- For a mixed odd/power-of-two input size (one dimension a power of two,- the other not), no padding is applied and the level-to-level- subtraction can raise ``RuntimeError`` from a shape mismatch when- ``max_level > 1`` (e.g. a :math:`(1, 1, 5, 8)` input with- ``max_level=2``). This is likely unintended and tracked in- `#3927 <https://github.com/kornia/kornia/issues/3927>`_. Separately, the- ``max_level`` bounds check does not currently reject non-positive+ 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 β also- tracked in `#3927 <https://github.com/kornia/kornia/issues/3927>`_.+ 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)`.@@ -621,6 +616,11 @@ def build_laplacian_pyramid(h = input.size()[2]w = input.size()[3]require_padding = not (is_powerof_two(w) or is_powerof_two(h))+ if max_level > 1:+ # pyrdown halves each side with floor division, while pyrup doubles it.+ # Every side subtracted from an upsampled level must therefore be even.+ stride = 1 << (max_level - 1)+ require_padding = require_padding or h % stride != 0 or w % stride != 0if require_padding:# in case of arbitrary shape torch.Tensor image need to be padded.diff --git a/tests/geometry/transform/test_pyramid.py b/tests/geometry/transform/test_pyramid.pyindex dd3cee806..754980878 100644--- a/tests/geometry/transform/test_pyramid.py+++ b/tests/geometry/transform/test_pyramid.py@@ -199,6 +199,27 @@ class TestBuildLaplacianPyramid(BaseTester):assert len(pyramid) == 1assert pyramid[0].shape == (1, 2, 4, 5)+ @pytest.mark.parametrize("height,width", ((5, 8), (8, 5)))+ def test_mixed_power_of_two_size_padding(self, height, width, device, dtype):+ sample = torch.rand(1, 2, height, width, device=device, dtype=dtype)+ pyramid = kornia.geometry.transform.build_laplacian_pyramid(sample, max_level=2)+ assert [img.shape for img in pyramid] == [torch.Size((1, 2, 8, 8)), torch.Size((1, 2, 4, 4))]++ @pytest.mark.parametrize(+ "height,width,max_level,level_sizes",+ (+ (8, 6, 2, ((8, 6), (4, 3))),+ (12, 8, 2, ((12, 8), (6, 4))),+ (8, 12, 3, ((8, 12), (4, 6), (2, 3))),+ (20, 64, 3, ((20, 64), (10, 32), (5, 16))),+ (20, 64, 4, ((32, 64), (16, 32), (8, 16), (4, 8))),+ ),+ )+ def test_padding_only_when_required_by_level_alignment(self, height, width, max_level, level_sizes, device, dtype):+ sample = torch.rand(1, 2, height, width, device=device, dtype=dtype)+ pyramid = kornia.geometry.transform.build_laplacian_pyramid(sample, max_level)+ assert [img.shape[-2:] for img in pyramid] == list(level_sizes)+@pytest.mark.parametrize("batch_size", (1, 2, 3))@pytest.mark.parametrize("channels", (1, 3))@pytest.mark.parametrize("max_level", (2, 3, 4))