Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): accept unbatched transforms in transform_points (#4958)

A Aswanth Rajbce594622f45
diff --git a/changelog.d/4958.fixed.md b/changelog.d/4958.fixed.md
new file mode 100644
index 000000000..ece135c63
--- /dev/null
+++ b/changelog.d/4958.fixed.md
@@ -0,0 +1 @@
+`transform_points` now applies an unbatched transform to point sets of any size.
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index 0ccc14ab4..2328fa786 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -225,14 +225,14 @@ def transform_points(trans_01: torch.Tensor, points_1: torch.Tensor) -> torch.Te
Args:
trans_01: tensor for transformations of shape
- :math:`(B, D+1, D+1)`.
- points_1: tensor of points of shape :math:`(B, N, D)`.
+ :math:`(D+1, D+1)` or :math:`(B, D+1, D+1)`.
+ points_1: tensor of points of shape :math:`(N, D)` or :math:`(B, N, D)`.
Returns:
- a tensor of N-dimensional points.
+ transformed points with the same shape as ``points_1``.
Shape:
- - Output: :math:`(B, N, D)`
+ - Output: same shape as ``points_1``.
Examples:
>>> points_1 = torch.rand(2, 4, 3) # BxNx3
@@ -242,12 +242,14 @@ def transform_points(trans_01: torch.Tensor, points_1: torch.Tensor) -> torch.Te
"""
KORNIA_CHECK_IS_TENSOR(trans_01)
KORNIA_CHECK_IS_TENSOR(points_1)
+ if trans_01.ndim == 2:
+ trans_01 = trans_01.unsqueeze(0)
if not trans_01.shape[0] == points_1.shape[0] and trans_01.shape[0] != 1:
raise ValueError(
f"Input batch size must be the same for both tensors or 1. Got {trans_01.shape} and {points_1.shape}"
)
if not trans_01.shape[-1] == (points_1.shape[-1] + 1):
- raise ValueError(f"Last input dimensions must differ by one unit Got{trans_01} and {points_1}")
+ raise ValueError(f"Last input dimensions must differ by one unit. Got {trans_01.shape} and {points_1.shape}")
# No points to transform (e.g. an image chip with no annotations): transforming an empty set
# yields the same empty set. Return early β€” the reshape below cannot infer ``-1`` from a
diff --git a/tests/geometry/test_linalg.py b/tests/geometry/test_linalg.py
index 92d232239..76cec3b04 100644
--- a/tests/geometry/test_linalg.py
+++ b/tests/geometry/test_linalg.py
@@ -15,6 +15,8 @@
# limitations under the License.
#
+import math
+
import pytest
import torch
@@ -37,6 +39,26 @@ def _rigid_transforms(batch_size, device, dtype):
class TestTransformPoints(BaseTester):
+ @pytest.mark.parametrize("num_dims", [2, 3])
+ @pytest.mark.parametrize("batch_size", [None, 5])
+ def test_unbatched_transform(self, num_dims, batch_size, device, dtype):
+ transform = torch.eye(num_dims + 1, device=device, dtype=dtype)
+ translation = torch.arange(1, num_dims + 1, device=device, dtype=dtype)
+ transform[:-1, -1] = translation
+ shape = (5, num_dims) if batch_size is None else (batch_size, 5, num_dims)
+ points = torch.arange(math.prod(shape), device=device, dtype=dtype).reshape(shape)
+
+ result = kgl.transform_points(transform, points)
+
+ assert result.shape == points.shape
+ self.assert_close(result, points + translation)
+
+ def test_dimension_mismatch_message_names_the_shapes(self, device, dtype):
+ transform = torch.eye(4, device=device, dtype=dtype)
+ points = torch.zeros(5, 2, device=device, dtype=dtype)
+ with pytest.raises(ValueError, match=r"differ by one unit\. Got torch\.Size\(.*\) and torch\.Size\(\[5, 2\]\)"):
+ kgl.transform_points(transform, points)
+
@pytest.mark.parametrize("batch_size", [1, 2, 5])
@pytest.mark.parametrize("num_points", [2, 3, 5])
@pytest.mark.parametrize("num_dims", [2, 3])