Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): keep the batch in NamedPose.from_rt for a rotation tensor (#4950)

Mohammad Hijjawic32b7a4a3de5
diff --git a/changelog.d/4950.breaking.md b/changelog.d/4950.breaking.md
new file mode 100644
index 000000000..cbe6b28e3
--- /dev/null
+++ b/changelog.d/4950.breaking.md
@@ -0,0 +1 @@
+`NamedPose.from_rt` keeps the batch of a rotation tensor. A `(B, 3, 3)` or `(B, 2, 2)` rotation tensor with a `(B, 3)` or `(B, 2)` translation now builds a `(B, 4, 4)` or `(B, 3, 3)` pose, as `NamedPose.from_matrix` and the `So3`/`So2` rotation path do. Previously `B > 1` raised a torch `RuntimeError`, and `B = 1` returned an unbatched `(4, 4)` or `(3, 3)` pose with a `(3,)` or `(2,)` translation; it now returns `(1, 4, 4)` or `(1, 3, 3)` with a `(1, 3)` or `(1, 2)` translation. The translation must now have exactly the rotation's batch shape, otherwise `from_rt` raises a `ValueError`: a `(1, D, D)` rotation with a `(D,)` translation, and a `(D, D)` rotation with a `(1, D)` translation, used to return an unbatched pose and now raise. An unbatched `(D, D)` rotation with a `(D,)` translation is unchanged. (#4950)
diff --git a/kornia/geometry/pose.py b/kornia/geometry/pose.py
index 60f64de7f..046c77fd5 100644
--- a/kornia/geometry/pose.py
+++ b/kornia/geometry/pose.py
@@ -164,7 +164,14 @@ class NamedPose:
if isinstance(rotation, torch.Tensor):
check_matrix_shape(rotation)
dim = rotation.shape[-1]
+ batch_shape = rotation.shape[:-2]
+ if translation.shape != rotation.shape[:-1]:
+ raise ValueError(
+ f"translation must have shape {tuple(rotation.shape[:-1])} for a rotation of shape "
+ f"{tuple(rotation.shape)}, got {tuple(translation.shape)}"
+ )
RT = torch.eye(dim + 1, device=rotation.device, dtype=rotation.dtype)
+ RT = RT.expand(*batch_shape, dim + 1, dim + 1).clone()
RT[..., :dim, :dim] = rotation
RT[..., :dim, dim] = translation
if dim == 2:
diff --git a/tests/geometry/test_pose.py b/tests/geometry/test_pose.py
index b185d8683..993fe00f1 100644
--- a/tests/geometry/test_pose.py
+++ b/tests/geometry/test_pose.py
@@ -91,6 +91,63 @@ class TestNamedPose(BaseTester):
assert isinstance(b_from_a, NamedPose)
assert isinstance(b_from_a.pose, Se2)
+ @pytest.mark.parametrize("batch_size", [None, 1, 2])
+ def test_from_rt_tensor_batch_3d(self, device, dtype, batch_size):
+ batch_shape = () if batch_size is None else (batch_size,)
+ rotation = So3.random(batch_size, device=device, dtype=dtype).matrix()
+ translation = torch.rand(*batch_shape, 3, device=device, dtype=dtype)
+ b_from_a = NamedPose.from_rt(rotation, translation, frame_src="frame_a", frame_dst="frame_b")
+ assert isinstance(b_from_a.pose, Se3)
+ assert b_from_a.pose.matrix().shape == (*batch_shape, 4, 4)
+ assert b_from_a.translation.shape == (*batch_shape, 3)
+
+ matrix = torch.eye(4, device=device, dtype=dtype).repeat(*batch_shape, 1, 1)
+ matrix[..., :3, :3] = rotation
+ matrix[..., :3, 3] = translation
+ self.assert_close(b_from_a.pose.matrix(), NamedPose.from_matrix(matrix).pose.matrix())
+ self.assert_close(
+ b_from_a.pose.matrix(), NamedPose.from_rt(So3.from_matrix(rotation), translation).pose.matrix()
+ )
+
+ @pytest.mark.parametrize("batch_size", [None, 1, 2])
+ def test_from_rt_tensor_batch_2d(self, device, dtype, batch_size):
+ if dtype == torch.bfloat16:
+ pytest.skip("torch.complex has no bfloat16 overload, so So2 cannot be built at all")
+ batch_shape = () if batch_size is None else (batch_size,)
+ rotation = So2.random(batch_size, device=device, dtype=dtype).matrix()
+ translation = torch.rand(*batch_shape, 2, device=device, dtype=dtype)
+ b_from_a = NamedPose.from_rt(rotation, translation, frame_src="frame_a", frame_dst="frame_b")
+ assert isinstance(b_from_a.pose, Se2)
+ assert b_from_a.pose.matrix().shape == (*batch_shape, 3, 3)
+ assert b_from_a.translation.shape == (*batch_shape, 2)
+
+ matrix = torch.eye(3, device=device, dtype=dtype).repeat(*batch_shape, 1, 1)
+ matrix[..., :2, :2] = rotation
+ matrix[..., :2, 2] = translation
+ self.assert_close(b_from_a.pose.matrix(), NamedPose.from_matrix(matrix).pose.matrix())
+ self.assert_close(
+ b_from_a.pose.matrix(), NamedPose.from_rt(So2.from_matrix(rotation), translation).pose.matrix()
+ )
+
+ @pytest.mark.parametrize(
+ ("rotation_shape", "translation_shape"),
+ [
+ ((2, 3, 3), (1, 3)),
+ ((2, 3, 3), (3,)),
+ ((1, 3, 3), (3,)),
+ ((3, 3), (1, 3)),
+ ((2, 2, 2), (2,)),
+ ((2, 2), (1, 2)),
+ ],
+ )
+ def test_from_rt_tensor_batch_translation_mismatch(self, device, dtype, rotation_shape, translation_shape):
+ # The translation must carry exactly the rotation's batch shape, as with So3/So2 rotations: neither a
+ # broadcast translation nor a batched translation for an unbatched rotation is accepted.
+ rotation = torch.eye(rotation_shape[-1], device=device, dtype=dtype).expand(*rotation_shape)
+ translation = torch.zeros(*translation_shape, device=device, dtype=dtype)
+ with pytest.raises(ValueError, match="translation must have shape"):
+ NamedPose.from_rt(rotation, translation)
+
def test_from_matrix(self, device, dtype):
b_from_a_matrix = Se3.identity(device=device, dtype=dtype).matrix()
b_from_a = NamedPose.from_matrix(b_from_a_matrix, frame_src="frame_a", frame_dst="frame_b")