Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

update mul method so2 (#2051)

Christie Jacobff943340d446
diff --git a/kornia/geometry/liegroup/_utils.py b/kornia/geometry/liegroup/_utils.py
index 9a9788790..40fdc72ca 100644
--- a/kornia/geometry/liegroup/_utils.py
+++ b/kornia/geometry/liegroup/_utils.py
@@ -3,18 +3,14 @@ def check_so2_z_shape(z):
z_shape = z.shape
len_z_shape = len(z_shape)
if (len_z_shape == 2 and z_shape[1] != 1) or (len_z_shape == 0 and not z.numel()) or (len_z_shape > 2):
- raise ValueError(f"Invalid input size, we expect [B, 1], [B] or []. Got: {z.shape}")
+ raise ValueError(f"Invalid input size, we expect [B]. Got: {z.shape}")
def check_so2_t_shape(t):
t_shape = t.shape
len_t_shape = len(t_shape)
- if (
- ((len_t_shape == 3) and ((t_shape[1] != 2) or (t_shape[2] != 1)))
- or ((len_t_shape == 2) and (t_shape[1] > 2))
- or ((len_t_shape == 1) and (t_shape[0] != 2))
- ):
- raise ValueError(f"Invalid translation shape, we expect [B, 2], [B, 2, 1] or [2] Got: {t_shape}")
+ if ((len_t_shape == 2) and (t_shape[1] > 2)) or ((len_t_shape == 1) and (t_shape[0] != 2)) or (len_t_shape > 2):
+ raise ValueError(f"Invalid translation shape, we expect [B, 2], or [2] Got: {t_shape}")
def check_so2_theta_shape(theta):
@@ -25,7 +21,7 @@ def check_so2_theta_shape(theta):
or (len_theta_shape == 0 and not theta.numel())
or (len_theta_shape > 2)
):
- raise ValueError(f"Invalid input size, we expect [B, 1] or [B]. Got: {theta_shape}")
+ raise ValueError(f"Invalid input size, we expect [B]. Got: {theta_shape}")
def check_so2_matrix_shape(matrix):
diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.py
index 8b4a7a33b..20b7aff34 100644
--- a/kornia/geometry/liegroup/so2.py
+++ b/kornia/geometry/liegroup/so2.py
@@ -71,7 +71,11 @@ class So2(Module):
elif isinstance(right, Tensor):
# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support
check_so2_t_shape(right)
- out = self.matrix() @ right
+ x = right[..., 0]
+ y = right[..., 1]
+ real = self.z.real
+ imag = self.z.imag
+ out = stack((real * x - imag * y, imag * x + real * y), -1)
else:
raise TypeError(f"Not So2 or Tensor type. Got: {type(right)}")
return out
diff --git a/test/geometry/liegroup/test_so2.py b/test/geometry/liegroup/test_so2.py
index e54a83d0c..832bfd765 100644
--- a/test/geometry/liegroup/test_so2.py
+++ b/test/geometry/liegroup/test_so2.py
@@ -109,14 +109,16 @@ class TestSo2(BaseTester):
s1 = So2.identity(batch_size, device, dtype)
z = self._make_rand_data(device, dtype, (batch_size, 2))
s2 = So2(torch.complex(z[..., 0], z[..., 1]))
- t = self._make_rand_data(device, dtype, (batch_size, 2, 1))
+ t1 = self._make_rand_data(device, dtype, (batch_size, 2))
+ t2 = self._make_rand_data(device, dtype, (2,))
s1_pose_s2 = s1 * s2
s2_pose_s2 = s2 * s2.inverse()
self.assert_close(s1_pose_s2.z.real, s2.z.real)
self.assert_close(s1_pose_s2.z.imag, s2.z.imag)
self.assert_close(s2_pose_s2.z.real, s1.z.real)
self.assert_close(s2_pose_s2.z.imag, s1.z.imag)
- self.assert_close((s1 * t), t)
+ self.assert_close((s1 * t1), t1)
+ self.assert_close((So2.identity(device=device, dtype=dtype) * t2), t2)
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_exp(self, device, dtype, batch_size):
@@ -146,12 +148,12 @@ class TestSo2(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, 1))
- t = self._make_rand_data(device, dtype, (batch_size, 2, 1))
+ 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
- self.assert_close(p1, p2)
+ p2 = s.matrix() @ t[..., None]
+ self.assert_close(p1, p2.squeeze(-1))
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
@pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))