Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[feat] add vee to so2, se2 (#2091)

Christie Jacobf398adffd911
diff --git a/kornia/geometry/liegroup/_utils.py b/kornia/geometry/liegroup/_utils.py
index 7c21308de..23c4420b0 100644
--- a/kornia/geometry/liegroup/_utils.py
+++ b/kornia/geometry/liegroup/_utils.py
@@ -53,3 +53,14 @@ def check_v_shape(v):
len_v_shape = len(v_shape)
if ((len_v_shape == 2) and (v_shape[1] != 3)) or ((len_v_shape == 1) and (v_shape[0] != 3)) or (len_v_shape > 3):
raise ValueError(f"Invalid input shape, we expect [B, 3], [3] Got: {v_shape}")
+
+
+def check_se2_omega_shape(matrix):
+ matrix_shape = matrix.shape
+ len_matrix_shape = len(matrix_shape)
+ if (
+ (len_matrix_shape == 3 and (matrix_shape[1] != 3 or matrix_shape[2] != 3))
+ or (len_matrix_shape == 2 and (matrix_shape[0] != 3 or matrix_shape[1] != 3))
+ or (len_matrix_shape > 3 or len_matrix_shape < 2)
+ ):
+ raise ValueError(f"Invalid input size, we expect [B, 3, 3] or [3, 3]. Got: {matrix_shape}")
diff --git a/kornia/geometry/liegroup/se2.py b/kornia/geometry/liegroup/se2.py
index 11a2fd95f..e59729f2e 100644
--- a/kornia/geometry/liegroup/se2.py
+++ b/kornia/geometry/liegroup/se2.py
@@ -3,7 +3,7 @@
from typing import Optional, Tuple, overload
from kornia.core import Module, Parameter, Tensor, concatenate, pad, rand, stack, tensor, where
-from kornia.geometry.liegroup._utils import check_se2_r_t_shape, check_v_shape
+from kornia.geometry.liegroup._utils import check_se2_omega_shape, check_se2_r_t_shape, check_v_shape
from kornia.geometry.liegroup.so2 import So2
from kornia.testing import KORNIA_CHECK, KORNIA_CHECK_TYPE
@@ -167,10 +167,10 @@ class Se2(Module):
@staticmethod
def hat(v):
- """Converts elements from vector space to lie algebra. Returns matrix of shape :math:`(B, 2, 2)`.
+ """Converts elements from vector space to lie algebra. Returns matrix of shape :math:`(B, 3, 3)`.
Args:
- theta: angle in radians of shape :math:`(B)`.
+ v: vector of shape:math:`(B, 3)`.
Example:
>>> theta = torch.tensor(3.1415/2)
@@ -185,6 +185,28 @@ class Se2(Module):
col0 = concatenate((So2.hat(theta), upsilon.unsqueeze(-2)), -2)
return pad(col0, (0, 1))
+ @staticmethod
+ def vee(omega) -> Tensor:
+ """Converts elements from lie algebra to vector space.
+
+ Args:
+ omega: 3x3-matrix representing lie algebra of shape :math:`(B, 3, 3)`.
+
+ Returns:
+ vector of shape :math:`(B, 3)`.
+
+ Example:
+ >>> v = torch.ones(3)
+ >>> omega_hat = Se2.hat(v)
+ >>> Se2.vee(omega_hat)
+ tensor([1., 1., 1.])
+ """
+ # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support
+ check_se2_omega_shape(omega)
+ upsilon = omega[..., 2, :2]
+ theta = So2.vee(omega[..., :2, :2])
+ return concatenate((upsilon, theta[..., None]), -1)
+
@classmethod
def identity(cls, batch_size: Optional[int] = None, device=None, dtype=None) -> 'Se2':
"""Create a Se2 group representing an identity rotation and zero translation.
diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.py
index d31d3b40d..4e3067950 100644
--- a/kornia/geometry/liegroup/so2.py
+++ b/kornia/geometry/liegroup/so2.py
@@ -141,6 +141,23 @@ class So2(Module):
row1 = stack((theta, z), -1)
return stack((row0, row1), -1)
+ @staticmethod
+ def vee(omega: Tensor) -> Tensor:
+ """Converts elements from lie algebra to vector space. Returns vector of shape :math:`(B,)`.
+
+ Args:
+ omega: 2x2-matrix representing lie algebra.
+
+ Example:
+ >>> v = torch.ones(3)
+ >>> omega = So2.hat(v)
+ >>> So2.vee(omega)
+ tensor([1., 1., 1.])
+ """
+ # TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support
+ check_so2_matrix_shape(omega)
+ return omega[..., 0, 1]
+
def matrix(self) -> Tensor:
"""Convert the complex number to a rotation matrix of shape :math:`(B, 2, 2)`.
diff --git a/test/geometry/liegroup/test_se2.py b/test/geometry/liegroup/test_se2.py
index d1f1f337c..5907b385d 100644
--- a/test/geometry/liegroup/test_se2.py
+++ b/test/geometry/liegroup/test_se2.py
@@ -6,9 +6,10 @@ from kornia.testing import BaseTester
class TestSe2(BaseTester):
- def _make_rand_data(self, device, dtype, batch_size, dims):
- shape = [] if batch_size is None else [batch_size]
- return torch.rand(shape + [dims], device=device, dtype=dtype)
+ def _make_rand_data(self, device, dtype, input_shape):
+ batch_size = input_shape[0]
+ shape = input_shape[1:] if batch_size is None else input_shape
+ return torch.rand(shape, device=device, dtype=dtype)
def test_smoke(self, device, dtype):
z = torch.rand((2,), dtype=torch.cfloat, device=device)
@@ -94,7 +95,7 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_exp(self, device, dtype, batch_size):
- t = self._make_rand_data(device, dtype, batch_size, dims=2)
+ t = self._make_rand_data(device, dtype, (batch_size, 2))
theta = torch.zeros(batch_size if batch_size is not None else (), device=device, dtype=dtype)
z = torch.zeros((batch_size, 2) if batch_size is not None else (2,), device=device, dtype=dtype)
s = Se2.exp(torch.cat((t, theta[..., None]), -1))
@@ -103,7 +104,7 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_log(self, device, dtype, batch_size):
- t = self._make_rand_data(device, dtype, batch_size, dims=2)
+ t = self._make_rand_data(device, dtype, (batch_size, 2))
s = Se2(So2.identity(batch_size, device, dtype), t)
s.log()
zero_vec = torch.zeros(3, device=device, dtype=dtype)
@@ -113,18 +114,32 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_exp_log(self, device, dtype, batch_size):
- a = self._make_rand_data(device, dtype, batch_size, dims=3)
+ a = self._make_rand_data(device, dtype, (batch_size, 3))
b = Se2.exp(a).log()
self.assert_close(b, a, low_tolerance=True)
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_hat(self, device, dtype, batch_size):
- v = self._make_rand_data(device, dtype, batch_size, dims=2)
- theta = self._make_rand_data(device, dtype, batch_size, dims=1)
+ v = self._make_rand_data(device, dtype, (batch_size, 2))
+ theta = self._make_rand_data(device, dtype, (batch_size, 1))
s_hat = Se2.hat(torch.cat((v, theta), -1))
self.assert_close(v, s_hat[..., 2, 0:2])
self.assert_close(s_hat[..., 0:2, 0:2].squeeze(), So2.hat(theta).squeeze())
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_vee(self, device, dtype, batch_size):
+ omega = self._make_rand_data(device, dtype, input_shape=(batch_size, 3, 3))
+ v = Se2.vee(omega)
+ self.assert_close(torch.stack((v[..., 0], v[..., 1]), -1), omega[..., 2, :2])
+ self.assert_close(v[..., -1], omega[..., 0, 1])
+
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_hat_vee(self, device, dtype, batch_size):
+ a = self._make_rand_data(device, dtype, (batch_size, 3))
+ omega_hat = Se2.hat(a)
+ b = Se2.vee(omega_hat)
+ self.assert_close(b, a)
+
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_identity(self, device, dtype, batch_size):
s = Se2.random(batch_size)
@@ -135,8 +150,8 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_matrix(self, device, dtype, batch_size):
- theta = self._make_rand_data(device, dtype, batch_size, dims=1)
- t = self._make_rand_data(device, dtype, batch_size, dims=2)
+ 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
p2 = s.matrix() @ t[..., None]
@@ -153,7 +168,7 @@ class TestSe2(BaseTester):
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_random(self, device, dtype, batch_size):
s = So2.random(batch_size=batch_size, device=device, dtype=dtype)
- t = self._make_rand_data(device, dtype, batch_size, dims=2)
+ t = self._make_rand_data(device, dtype, (batch_size, 2))
se2 = Se2(s, t)
se2_in_se2 = se2.inverse() * se2
i = Se2.identity(batch_size=batch_size, device=device, dtype=dtype)
diff --git a/test/geometry/liegroup/test_so2.py b/test/geometry/liegroup/test_so2.py
index 832bfd765..b623e470a 100644
--- a/test/geometry/liegroup/test_so2.py
+++ b/test/geometry/liegroup/test_so2.py
@@ -146,6 +146,19 @@ class TestSo2(BaseTester):
o = torch.ones((2, 1), device=device, dtype=dtype)
self.assert_close((m @ o).reshape(-1, 2, 1), theta.reshape(-1, 1, 1).repeat(1, 2, 1))
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_vee(self, device, dtype, batch_size):
+ omega = self._make_rand_data(device, dtype, (batch_size, 2, 2))
+ theta = So2.vee(omega)
+ self.assert_close(omega[..., 0, 1], theta)
+
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_hat_vee(self, device, dtype, batch_size):
+ a = self._make_rand_data(device, dtype, (batch_size,))
+ omega = So2.hat(a)
+ b = So2.vee(omega)
+ self.assert_close(b, a)
+
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_matrix(self, device, dtype, batch_size):
theta = self._make_rand_data(device, dtype, (batch_size,))