π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add adjoint for se2, so2 (#2101)
Christie Jacob3fb32a407b8b
diff --git a/kornia/geometry/liegroup/se2.py b/kornia/geometry/liegroup/se2.pyindex e59729f2e..08e94c966 100644--- a/kornia/geometry/liegroup/se2.py+++ b/kornia/geometry/liegroup/se2.py@@ -280,3 +280,17 @@ class Se2(Module):KORNIA_CHECK(batch_size >= 1, msg="batch_size must be positive")shape = (batch_size, 2)return cls(r, rand(shape, device=device, dtype=dtype))++ def adjoint(self) -> Tensor:+ """Returns the adjoint matrix of shape :math:`(B, 3, 3)`.++ Example:+ >>> s = Se2.identity()+ >>> s.adjoint()+ tensor([[1., -0., 0.],+ [0., 1., -0.],+ [0., 0., 1.]], grad_fn=<CopySlices>)+ """+ rt = self.matrix()+ rt[..., 0:2, 2] = stack((self.t[..., 1], -self.t[..., 0]), -1)+ return rtdiff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.pyindex 4e3067950..d31cb5add 100644--- a/kornia/geometry/liegroup/so2.py+++ b/kornia/geometry/liegroup/so2.py@@ -242,3 +242,15 @@ class So2(Module):real_data = rand((), device=device, dtype=dtype)imag_data = rand((), device=device, dtype=dtype)return cls(complex(real_data, imag_data))++ def adjoint(self) -> Tensor:+ """Returns the adjoint matrix of shape :math:`(B, 2, 2)`.++ Example:+ >>> s = So2.identity()+ >>> s.adjoint()+ tensor([[1., -0.],+ [0., 1.]], grad_fn=<StackBackward0>)+ """+ batch_size = len(self.z) if len(self.z.shape) > 0 else None+ return self.identity(batch_size, self.z.device, self.z.real.dtype).matrix()diff --git a/test/geometry/liegroup/test_se2.py b/test/geometry/liegroup/test_se2.pyindex 5907b385d..879e19168 100644--- a/test/geometry/liegroup/test_se2.py+++ b/test/geometry/liegroup/test_se2.py@@ -175,3 +175,10 @@ class TestSe2(BaseTester):self.assert_close(se2_in_se2.so2.z.real, i.so2.z.real)self.assert_close(se2_in_se2.so2.z.imag, i.so2.z.imag)self.assert_close(se2_in_se2.t, i.t)++ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_adjoint(self, device, dtype, batch_size):+ x = Se2.random(batch_size)+ y = Se2.random(batch_size)+ self.assert_close(x.inverse().adjoint(), x.adjoint().inverse())+ self.assert_close((x * y).adjoint(), x.adjoint() @ y.adjoint())diff --git a/test/geometry/liegroup/test_so2.py b/test/geometry/liegroup/test_so2.pyindex b623e470a..9f1c996cf 100644--- a/test/geometry/liegroup/test_so2.py+++ b/test/geometry/liegroup/test_so2.py@@ -184,3 +184,8 @@ class TestSo2(BaseTester):i = So2.identity(batch_size=batch_size, device=device, dtype=dtype)self.assert_close(s_in_s.z.real, i.z.real)self.assert_close(s_in_s.z.imag, i.z.imag)++ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_adjoint(self, device, dtype, batch_size):+ s = So2.identity(batch_size, device=device, dtype=dtype)+ self.assert_close(s.matrix(), s.adjoint())