π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[feat] `from_matrix` for Se3 and Se2 (#2473)
Christie Jacob902eab0dc343
diff --git a/kornia/geometry/liegroup/_utils.py b/kornia/geometry/liegroup/_utils.pyindex 953870c4c..4c4245fc6 100644--- a/kornia/geometry/liegroup/_utils.py+++ b/kornia/geometry/liegroup/_utils.py@@ -38,6 +38,12 @@ def check_so2_matrix_shape(matrix: Tensor) -> None:raise ValueError(f"Invalid input size, we expect [B, 2, 2] or [2, 2]. Got: {matrix_shape}")+def check_so2_matrix(matrix: Tensor) -> None:+ for m in matrix.reshape(-1, 2, 2):+ if m[0, 0] != m[1, 1] or m[0, 1] != -m[1, 0]:+ raise ValueError("Invalid rotation matrix")++def check_se2_t_shape(t: Tensor) -> None:check_so2_t_shape(t)diff --git a/kornia/geometry/liegroup/se2.py b/kornia/geometry/liegroup/se2.pyindex 56be78c83..27636052f 100644--- a/kornia/geometry/liegroup/se2.py+++ b/kornia/geometry/liegroup/se2.py@@ -276,6 +276,28 @@ class Se2(Module):rt_3x3[..., -1, -1] = 1.0return rt_3x3+ @classmethod+ def from_matrix(cls, matrix: Tensor) -> Se2:+ """Create an Se2 group from a matrix.++ Args:+ matrix: tensor of shape :math:`(B, 3, 3)`.++ Example:+ >>> s = Se2.from_matrix(torch.eye(3).repeat(2, 1, 1))+ >>> s.r+ Parameter containing:+ tensor([1.+0.j, 1.+0.j], requires_grad=True)+ >>> s.t+ Parameter containing:+ tensor([[0., 0.],+ [0., 0.]], requires_grad=True)+ """+ # KORNIA_CHECK_SHAPE(matrix, ["B", "3", "3"]) # FIXME: resolve shape bugs. @edgarriba+ r = So2.from_matrix(matrix[..., :2, :2])+ t = matrix[..., :2, -1]+ return cls(r, t)+def inverse(self) -> Se2:"""Returns the inverse transformation.diff --git a/kornia/geometry/liegroup/se3.py b/kornia/geometry/liegroup/se3.pyindex 0b46513df..f52ff6c77 100644--- a/kornia/geometry/liegroup/se3.py+++ b/kornia/geometry/liegroup/se3.py@@ -276,6 +276,27 @@ class Se3(Module):rt_4x4[..., -1, -1] = 1.0return rt_4x4+ @classmethod+ def from_matrix(cls, matrix: Tensor) -> Se3:+ """Create a Se3 group from a matrix.++ Args:+ matrix: tensor of shape :math:`(B, 4, 4)`.++ Example:+ >>> s = Se3.from_matrix(torch.eye(4))+ >>> s.r+ Parameter containing:+ tensor([1., 0., 0., 0.], requires_grad=True)+ >>> s.t+ Parameter containing:+ tensor([0., 0., 0.], requires_grad=True)+ """+ # KORNIA_CHECK_SHAPE(matrix, ["B", "4", "4"]) # FIXME: resolve shape bugs. @edgarriba+ r = So3.from_matrix(matrix[..., :3, :3])+ t = matrix[..., :3, -1]+ return cls(r, t)+@classmethoddef from_qxyz(cls, qxyz: Tensor) -> Se3:"""Create a Se3 group a quaternion and translation vector.diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.pyindex 92b13cdae..7fa9da2e7 100644--- a/kornia/geometry/liegroup/so2.py+++ b/kornia/geometry/liegroup/so2.py@@ -7,6 +7,7 @@ from typing import overloadfrom kornia.core import Device, Dtype, Module, Parameter, Tensor, complex, rand, stack, tensor, zeros_likefrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSORfrom kornia.geometry.liegroup._utils import (+ check_so2_matrix,check_so2_matrix_shape,check_so2_t_shape,check_so2_theta_shape,@@ -196,6 +197,7 @@ class So2(Module):"""# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape supportcheck_so2_matrix_shape(matrix)+ check_so2_matrix(matrix)z = complex(matrix[..., 0, 0], matrix[..., 1, 0])return cls(z)diff --git a/test/geometry/liegroup/test_se2.py b/test/geometry/liegroup/test_se2.pyindex a299a7ad3..5d55ee870 100644--- a/test/geometry/liegroup/test_se2.py+++ b/test/geometry/liegroup/test_se2.py@@ -205,6 +205,19 @@ class TestSe2(BaseTester):p2 = s.matrix() @ t[..., None]self.assert_close(p1, p2.squeeze(-1))+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_from_matrix(self, device, dtype, batch_size):+ theta = self._make_rand_data(device, dtype, (batch_size,))+ t = self._make_rand_data(device, dtype, (batch_size, 2))+ s = So2.exp(theta)+ p1 = s * t+ RT = torch.eye(3, device=device, dtype=dtype)+ if batch_size is not None:+ RT = RT.repeat(batch_size, 1, 1)+ RT[..., :2, :2] = s.matrix()+ p2 = Se2.from_matrix(RT) * t+ self.assert_close(p1, p2)+@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))def test_inverse(self, device, batch_size, dtype):s = Se2.random(batch_size, device, dtype)diff --git a/test/geometry/liegroup/test_se3.py b/test/geometry/liegroup/test_se3.pyindex 6c336ce6f..a599024cb 100644--- a/test/geometry/liegroup/test_se3.py+++ b/test/geometry/liegroup/test_se3.py@@ -181,6 +181,19 @@ class TestSe3(BaseTester):self.assert_close(rot_mat[..., 0:3, 0:3], rot.matrix())self.assert_close(rot_mat[..., 0:3, 3], t)+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_from_matrix(self, device, dtype, batch_size):+ matrix = torch.tensor(+ ((1.0, 0.0, 0.0, 0.0), (0.0, 0.0, -1.0, 0.0), (0.0, 1.0, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0)),+ device=device,+ dtype=dtype,+ )+ if batch_size is not None:+ matrix = matrix.repeat(batch_size, 1, 1)+ s = Se3.from_matrix(matrix)+ self.assert_close(s.r.matrix(), matrix[..., 0:3, 0:3])+ self.assert_close(s.t, matrix[..., 0:3, 3])+@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))def test_from_qxyz(self, device, dtype, batch_size):qxyz = self._make_rand_data(device, dtype, batch_size, dims=7)diff --git a/test/geometry/liegroup/test_so2.py b/test/geometry/liegroup/test_so2.pyindex 767ef86f0..cc87515be 100644--- a/test/geometry/liegroup/test_so2.py+++ b/test/geometry/liegroup/test_so2.py@@ -39,6 +39,8 @@ class TestSo2(BaseTester):@pytest.mark.parametrize("input_shape", [(1, 2, 2), (2, 2, 2), (5, 2, 2), (2, 2)])def test_matrix_cardinality(self, device, dtype, input_shape):matrix = torch.rand(input_shape, dtype=dtype, device=device)+ matrix[..., 0, 1] = -matrix[..., 1, 0]+ matrix[..., 1, 1] = matrix[..., 0, 0]s = So2.from_matrix(matrix)assert s.matrix().shape == input_shape@@ -59,6 +61,9 @@ class TestSo2(BaseTester):with pytest.raises(ValueError):m = torch.rand((2, 2, 1), dtype=dtype, device=device)assert So2.from_matrix(m)+ with pytest.raises(ValueError):+ m = torch.rand((2, 2, 1), dtype=dtype, device=device)+ assert So2.from_matrix(m)with pytest.raises(Exception):assert So2.identity(batch_size=0)@@ -181,6 +186,20 @@ class TestSo2(BaseTester):p2 = s.matrix() @ t[..., None]self.assert_close(p1, p2.squeeze(-1))+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_from_matrix(self, device, dtype, batch_size):+ matrix = torch.eye(2, device=device, dtype=dtype)+ if batch_size is not None:+ matrix = matrix.repeat(batch_size, 1, 1)+ one = torch.ones((batch_size,), device=device, dtype=dtype)+ zero = torch.zeros((batch_size,), device=device, dtype=dtype)+ else:+ one = torch.tensor(1, device=device, dtype=dtype)+ zero = torch.tensor(0, device=device, dtype=dtype)+ s = So2.from_matrix(matrix)+ self.assert_close(s.z.real, one)+ self.assert_close(s.z.imag, zero)+@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))@pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))def test_inverse(self, device, batch_size, cdtype):