π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): size pyrdown axes consistently for fractional factors (#5817)
Devulapalli Naga Sri Vaishnavi246e88683ede
diff --git a/changelog.d/5817.breaking.md b/changelog.d/5817.breaking.mdnew file mode 100644index 000000000..a20e5ec65--- /dev/null+++ b/changelog.d/5817.breaking.md@@ -0,0 +1 @@+`pyrdown` and `PyrDown` now compute the output width with the same rule as the height, `int(width / factor)`, so both axes follow the documented `floor(side / factor)`. The width used floor division of the floating-point values, which could drop a pixel when `width / factor` is a whole number and the factor's binary value lies slightly above its decimal value (1.1, 1.3, 1.6, 1.8, 2.2, ...): at `factor=1.6` a 480 x 640 image used to give 300 x 399 and now gives 300 x 400. Every width that is a multiple of 8 at `factor=1.6`, of 13 at 1.3 or of 9 at 1.8, and some multiples of 11 at 1.1, gains that pixel, and the output values change with the new sampling grid. Integer and dyadic factors (2.0, 3.0, 1.5), KeyNet's internal 1.2 and the default `scale_factor_levels` of `MultiResolutionDetector` (`sqrt(2)`) give the same sizes as before; a `MultiResolutionDetector` configured with an affected `scale_factor_levels` can return different detections.diff --git a/kornia/geometry/transform/pyramid.py b/kornia/geometry/transform/pyramid.pyindex 0c6a60fdb..4b1976f48 100644--- a/kornia/geometry/transform/pyramid.py+++ b/kornia/geometry/transform/pyramid.py@@ -446,7 +446,7 @@ def pyrdown(# downsample.out: torch.Tensor = F.interpolate(x_blur,- size=(int(float(height) / factor), int(float(width) // factor)),+ size=(int(float(height) / factor), int(float(width) / factor)),mode="bilinear",align_corners=align_corners,)diff --git a/tests/geometry/transform/test_pyramid.py b/tests/geometry/transform/test_pyramid.pyindex 85bb3d1ea..2dc2f76f9 100644--- a/tests/geometry/transform/test_pyramid.py+++ b/tests/geometry/transform/test_pyramid.py@@ -51,6 +51,34 @@ class TestPyrDown(BaseTester):pyr = kornia.geometry.PyrDown(factor=3.0)assert pyr(inp).shape == (1, 2, 3, 3)+ @pytest.mark.parametrize("as_module", [False, True])+ @pytest.mark.parametrize("align_corners", [False, True])+ @pytest.mark.parametrize(+ "height, width, factor, expected_size",+ [(11, 22, 1.1, (10, 20)), (9, 18, 1.8, (5, 10)), (9, 15, 1.2, (7, 12)), (11, 15, 2.0, (5, 7))],+ )+ def test_fractional_factor_axis_symmetry(+ self, device, dtype, as_module, align_corners, height, width, factor, expected_size+ ):+ inp = torch.arange(height * width, device=device, dtype=dtype).reshape(1, 1, height, width)+ inp = inp / (height * width)+ if as_module:+ pyr = kornia.geometry.PyrDown(factor=factor, align_corners=align_corners)+ out = pyr(inp)+ transposed = pyr(inp.transpose(-2, -1))+ else:+ out = kornia.geometry.pyrdown(inp, factor=factor, align_corners=align_corners)+ transposed = kornia.geometry.pyrdown(inp.transpose(-2, -1), factor=factor, align_corners=align_corners)+ assert out.shape == (1, 1, *expected_size)+ self.assert_close(out, transposed.transpose(-2, -1))++ def test_dynamo(self, device, dtype, torch_optimizer):+ inp = torch.rand(1, 1, 11, 22, device=device, dtype=dtype)+ pyr = kornia.geometry.PyrDown(factor=1.1)+ actual = torch_optimizer(pyr)(inp)+ assert actual.shape == (1, 1, 10, 20)+ self.assert_close(actual, pyr(inp))+def test_shape_batch(self, device, dtype):inp = torch.zeros(2, 2, 4, 4, device=device, dtype=dtype)pyr = kornia.geometry.PyrDown()@@ -63,9 +91,10 @@ class TestPyrDown(BaseTester):self.assert_close(pyr_out, pyr_out.flip(0))self.assert_close(pyr_out, pyr_out.flip(1))- def test_gradcheck(self, device):+ @pytest.mark.parametrize("factor", [2.0, 1.1, 1.8])+ def test_gradcheck(self, device, factor):img = torch.rand(1, 2, 5, 4, device=device, dtype=torch.float64)- self.gradcheck(kornia.geometry.pyrdown, (img,), nondet_tol=1e-8)+ self.gradcheck(lambda x: kornia.geometry.pyrdown(x, factor=factor), (img,), nondet_tol=1e-8)@pytest.mark.parametrize("op, side", [("pyrdown", 5), ("pyrup", 4)])def test_convention_align_corners_and_border_type_change_output(self, op, side, device, dtype):