π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[feat] implement adjoint for liegroups (#2007)
Christie Jacob1734bf1ef357
diff --git a/kornia/geometry/liegroup/se3.py b/kornia/geometry/liegroup/se3.pyindex 0f9a04a4d..eaccdcc3c 100644--- a/kornia/geometry/liegroup/se3.py+++ b/kornia/geometry/liegroup/se3.py@@ -323,3 +323,22 @@ class Se3(Module):"""zs = zeros_like(z)return cls.trans(zs, zs, z)++ def adjoint(self) -> Tensor:+ """Returns the adjoint matrix of shape :math:`(B, 6, 6)`.++ Example:+ >>> s = Se3.identity()+ >>> s.adjoint()+ tensor([[1., 0., 0., 0., 0., 0.],+ [0., 1., 0., 0., 0., 0.],+ [0., 0., 1., 0., 0., 0.],+ [0., 0., 0., 1., 0., 0.],+ [0., 0., 0., 0., 1., 0.],+ [0., 0., 0., 0., 0., 1.]], grad_fn=<CatBackward0>)+ """+ R = self.so3.matrix()+ z = zeros_like(R)+ row0 = concatenate((R, So3.hat(self.t) @ R), -1)+ row1 = concatenate((z, R), -1)+ return concatenate((row0, row1), -2)diff --git a/kornia/geometry/liegroup/so3.py b/kornia/geometry/liegroup/so3.pyindex 562208b47..f9cdea591 100644--- a/kornia/geometry/liegroup/so3.py+++ b/kornia/geometry/liegroup/so3.py@@ -280,3 +280,15 @@ class So3(Module):"""zs = zeros_like(z)return cls.exp(stack((zs, zs, z), -1))++ def adjoint(self) -> Tensor:+ """Returns the adjoint matrix of shape :math:`(B, 3, 3)`.++ Example:+ >>> s = So3.identity()+ >>> s.adjoint()+ tensor([[1., 0., 0.],+ [0., 1., 0.],+ [0., 0., 1.]], grad_fn=<StackBackward0>)+ """+ return self.matrix()diff --git a/test/geometry/liegroup/test_se3.py b/test/geometry/liegroup/test_se3.pyindex 31032e0eb..ab04a8f88 100644--- a/test/geometry/liegroup/test_se3.py+++ b/test/geometry/liegroup/test_se3.py@@ -212,3 +212,12 @@ class TestSe3(BaseTester):se3 = Se3.trans_z(z)self.assert_close(se3.t, torch.stack((zs, zs, z), -1))self.assert_close(se3.so3.matrix(), So3.identity(batch_size, device, dtype).matrix())++ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_adjoint(self, device, dtype, batch_size):+ x_data = self._make_rand_data(device, dtype, batch_size, dims=6)+ y_data = self._make_rand_data(device, dtype, batch_size, dims=6)+ x = Se3.exp(x_data)+ y = Se3.exp(y_data)+ 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_so3.py b/test/geometry/liegroup/test_so3.pyindex 520f207cc..df3e95295 100644--- a/test/geometry/liegroup/test_so3.py+++ b/test/geometry/liegroup/test_so3.py@@ -208,3 +208,12 @@ class TestSo3(BaseTester):so3 = So3.rot_z(z)_, _, yaw = euler_from_quaternion(*so3.q.coeffs)self.assert_close(z, yaw)++ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))+ def test_adjoint(self, device, dtype, batch_size):+ q1 = Quaternion.random(batch_size, device, dtype)+ q2 = Quaternion.random(batch_size, device, dtype)+ x = So3(q1)+ y = So3(q2)+ self.assert_close(x.inverse().adjoint(), x.adjoint().inverse())+ self.assert_close((x * y).adjoint(), x.adjoint() @ y.adjoint())