π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): read an unbatched (2,) translation or shear as one shared parameter (#6522)
Saksham Sharma532a4bf75a13
diff --git a/changelog.d/6522.fixed.md b/changelog.d/6522.fixed.mdnew file mode 100644index 000000000..fe183ab71--- /dev/null+++ b/changelog.d/6522.fixed.md@@ -0,0 +1 @@+`kornia.geometry.transform.translate` and `shear`, and the `Translate` and `Shear` modules, read an unbatched `(2,)` parameter as one shift or pair of factors, `(1, 2)`, that serves every image. With one image, `(C, H, W)` or `(1, C, H, W)`, they used to read its 2 components as 2 parameter sets and return the image twice, as a batch of 2, with no error; with 3 or more images they raised a `RuntimeError`. With 2 images the result is unchanged.diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.pyindex 5d789427a..3a8070a57 100644--- a/kornia/geometry/transform/affwarp.py+++ b/kornia/geometry/transform/affwarp.py@@ -536,11 +536,11 @@ def translate(``out = translate(tensor, translation)`` shifts ``(C, H, W)`` or ``(B, C, H, W)`` images by ``translation````(B, 2)``, ``(dx, dy)`` in pixels, a positive ``dx`` to the right and a positive ``dy`` down, and returns them at- their size; a fractional shift interpolates, and the uncovered border is padded by ``padding_mode``, except- along a one-pixel axis, which the default ``align_corners=True`` repeats- (`#6433 <https://github.com/kornia/kornia/issues/6433>`_). One- translation serves every image of a batch, and one image, unbatched or of batch 1, is shifted by every- translation. For a stored translation, use :class:`Translate`; for the matrix, :func:`get_translation_matrix2d`.+ their size; a fractional shift interpolates, and the uncovered border is padded by ``padding_mode``, except along a+ one-pixel axis, which the default ``align_corners=True`` repeats+ (`#6433 <https://github.com/kornia/kornia/issues/6433>`_). One translation, ``(1, 2)`` or ``(2,)``, serves every+ image of a batch, and one image, unbatched or of batch 1, is shifted by every translation. For a stored translation,+ use :class:`Translate`; for the matrix, :func:`get_translation_matrix2d`... image:: _static/img/translate.png@@ -551,7 +551,7 @@ def translate(tensor: The image tensor to be warped in shapes of ``(C, H, W)`` or ``(B, C, H, W)``.translation: tensor containing the amount of pixels totranslate in the x and y direction. The tensor must have a shape of- (B, 2), where B is batch size, last dimension contains dx dy.+ (B, 2), where B is batch size, last dimension contains dx dy, or (2,) for one translation.mode: interpolation mode to calculate output values``'bilinear'`` | ``'nearest'``.padding_mode: padding mode for outside grid values@@ -584,6 +584,11 @@ def translate(if len(tensor.shape) not in (3, 4):raise ValueError(f"Invalid tensor shape, we expect CxHxW or BxCxHxW. Got: {tensor.shape}")+ # One (2,) translation serves every image, as a (1, 2) one does; its 2 components used to be read as a batch of 2+ # and return one image twice (#6521).+ if translation.dim() == 1:+ translation = translation.unsqueeze(0)+# compute the translation matrixtranslation_matrix: torch.Tensor = _compute_translation_matrix(translation)@@ -679,11 +684,11 @@ def shear("""Shear an image by raw shear factors about pixel (0, 0).``out = shear(tensor, shear)`` shears ``(C, H, W)`` or ``(B, C, H, W)`` images by ``shear`` ``(B, 2)``, the raw- factors ``(shx, shy)`` described below, and returns them at their size. One pair of factors serves every image of- a batch, and one image, unbatched or of batch 1, is sheared by every pair. For shear angles in radians, use- :class:`Affine`, about the image centre by default, or :func:`get_shear_matrix2d`, about a centre you pass; both- shear by the negative tangent, so values below ``pi / 2`` in magnitude shear the other way there. For stored- factors, use :class:`Shear`.+ factors ``(shx, shy)`` described below, and returns them at their size. One pair of factors, ``(1, 2)`` or ``(2,)``,+ serves every image of a batch, and one image, unbatched or of batch 1, is sheared by every pair. For shear angles in+ radians, use :class:`Affine`, about the image centre by default, or :func:`get_shear_matrix2d`, about a centre you+ pass; both shear by the negative tangent, so values below ``pi / 2`` in magnitude shear the other way there. For+ stored factors, use :class:`Shear`... image:: _static/img/shear.png@@ -700,7 +705,7 @@ def shear(tensor: The image tensor to be skewed with shape of ``(C, H, W)`` or ``(B, C, H, W)``.shear: tensor containing raw shear factorsin the x and y direction about pixel (0, 0). The tensor must have a shape of- (B, 2), where B is batch size, last dimension contains shx shy.+ (B, 2), where B is batch size, last dimension contains shx shy, or (2,) for one pair.mode: interpolation mode to calculate output values``'bilinear'`` | ``'nearest'``.padding_mode: padding mode for outside grid values@@ -734,7 +739,12 @@ def shear(if len(tensor.shape) not in (3, 4):raise ValueError(f"Invalid tensor shape, we expect CxHxW or BxCxHxW. Got: {tensor.shape}")- # compute the translation matrix+ # One (2,) pair serves every image, as a (1, 2) one does; its 2 factors used to be read as a batch of 2 and+ # return one image twice (#6521).+ if shear.dim() == 1:+ shear = shear.unsqueeze(0)++ # compute the shear matrixshear_matrix: torch.Tensor = _compute_shear_matrix(shear)# warp using the affine transformdiff --git a/tests/geometry/transform/test_affine.py b/tests/geometry/transform/test_affine.pyindex 6cec2e5d4..b828b009b 100644--- a/tests/geometry/transform/test_affine.py+++ b/tests/geometry/transform/test_affine.py@@ -855,6 +855,17 @@ class TestTranslate(BaseTester):transform = kornia.geometry.transform.Translate(translation, align_corners=True)self.assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)+ @pytest.mark.parametrize("image_shape", [(0, 2, 4, 5), (1, 2, 4, 5), (2, 4, 5), (2, 2, 4, 5), (3, 2, 4, 5)])+ def test_unbatched_translation_is_shared_6521(self, device, dtype, image_shape):+ # A (2,) translation serves every image, as (1, 2) does. Its 2 components used to be read as a batch of 2,+ # which returned one image twice and raised for 3 images.+ image = torch.rand(image_shape, device=device, dtype=dtype)+ translation = torch.tensor([1.0, -0.5], device=device, dtype=dtype)+ out = kornia.geometry.transform.translate(image, translation)+ assert out.shape == image.shape+ self.assert_close(out, kornia.geometry.transform.translate(image, translation[None]), atol=0, rtol=0)+ self.assert_close(kornia.geometry.transform.Translate(translation)(image), out, atol=0, rtol=0)+def test_gradcheck(self, device):# test parameterstranslation = torch.tensor([[1.0, 0.0]], device=device, dtype=torch.float64)@@ -1070,6 +1081,17 @@ class TestShear(BaseTester):transform = kornia.geometry.transform.Shear(shear, align_corners=False)self.assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)+ @pytest.mark.parametrize("image_shape", [(0, 2, 4, 5), (1, 2, 4, 5), (2, 4, 5), (2, 2, 4, 5), (3, 2, 4, 5)])+ def test_unbatched_shear_is_shared_6521(self, device, dtype, image_shape):+ # A (2,) pair of factors serves every image, as (1, 2) does. Its 2 factors used to be read as a batch of 2,+ # which returned one image twice and raised for 3 images.+ image = torch.rand(image_shape, device=device, dtype=dtype)+ shear = torch.tensor([0.5, -0.25], device=device, dtype=dtype)+ out = kornia.geometry.transform.shear(image, shear)+ assert out.shape == image.shape+ self.assert_close(out, kornia.geometry.transform.shear(image, shear[None]), atol=0, rtol=0)+ self.assert_close(kornia.geometry.transform.Shear(shear, align_corners=False)(image), out, atol=0, rtol=0)+def test_gradcheck(self, device):# test parametersshear = torch.tensor([[0.5, 0.0]], device=device, dtype=torch.float64)