🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): reject (N, D) points with a transform batch when N == B (#5469)
Rızgar Ozana8b4490fbb14
diff --git a/changelog.d/5469.fixed.md b/changelog.d/5469.fixed.mdnew file mode 100644index 000000000..4f7bdad75--- /dev/null+++ b/changelog.d/5469.fixed.md@@ -0,0 +1 @@+`transform_points` with unbatched `(N, D)` points and a batch of `B > 1` transforms now raises its batch-size `ValueError` also when `N == B`; that case used to fail inside `torch.bmm` with an unrelated `RuntimeError`. `PinholeCamera.project` with `(B, 3)` points on a batch-`B` camera raises the same `ValueError`.diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.pyindex fea78e339..84b348e12 100644--- a/kornia/geometry/linalg.py+++ b/kornia/geometry/linalg.py@@ -244,7 +244,10 @@ def transform_points(trans_01: torch.Tensor, points_1: torch.Tensor) -> torch.TeKORNIA_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:+ # A 2-D ``points_1`` is (N, D) with no batch axis, so N equal to a transform batch B > 1 is a+ # coincidence, not a match: it used to reach ``bmm`` with an empty batch (kornia#4969).+ unbatched_points = points_1.ndim == 2 and trans_01.shape[0] > 1+ if (not trans_01.shape[0] == points_1.shape[0] or unbatched_points) 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}")diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.pyindex 346aa20f3..2c0121225 100644--- a/tests/geometry/camera/test_pinhole.py+++ b/tests/geometry/camera/test_pinhole.py@@ -901,6 +901,19 @@ class TestPinholeCamera(BaseTester):assert camera.project(torch.zeros(0, 3, device=device, dtype=dtype)).shape == (0, 2)assert camera.project(torch.zeros(0, 1, 3, device=device, dtype=dtype)).shape == (0, 1, 2)+ def test_project_rejects_unbatched_points_on_a_camera_batch_4969(self, device, dtype):+ # kornia#4969: (B, 3) points on a batch-B camera have no batch axis, so they get transform_points'+ # batch-size ValueError, as (N, 3) with N != B does; before the fix N == B failed inside bmm.+ trans = torch.eye(4, device=device, dtype=dtype).expand(2, 4, 4)+ ones = torch.ones(2, device=device, dtype=dtype)+ camera = kornia.geometry.camera.PinholeCamera(trans, trans, ones, ones)+ points = torch.zeros(2, 1, 3, device=device, dtype=dtype)+ points[..., 2] = 1.0++ assert camera.project(points).shape == (2, 1, 2)+ with pytest.raises(ValueError, match="Input batch size must be the same"):+ camera.project(points[:, 0])+@pytest.mark.parametrize("batch_sizes", [(1, 2, 1, 1), (0, 1, 0, 0)])def test_constructor_rejects_mismatched_batch_sizes_4281(self, batch_sizes, device, dtype):with pytest.raises(ValueError, match="Arguments shapes must match"):diff --git a/tests/geometry/test_linalg.py b/tests/geometry/test_linalg.pyindex bf68b2af8..e2dc8fc61 100644--- a/tests/geometry/test_linalg.py+++ b/tests/geometry/test_linalg.py@@ -59,6 +59,15 @@ class TestTransformPoints(BaseTester):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("num_points", [2, 3])+ def test_unbatched_points_with_transform_batch_raise(self, num_points, device, dtype):+ # (N, D) points have no batch axis, so N == B must hit the batch check like N != B does+ # instead of reaching bmm with an empty transform batch (kornia#4969).+ transform = torch.eye(4, device=device, dtype=dtype).expand(2, -1, -1)+ points = torch.zeros(num_points, 3, device=device, dtype=dtype)+ with pytest.raises(ValueError, match="Input batch size must be the same"):+ 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])