π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
so2 tests update and cleanup (#2029)
Christie Jacobfd88d24cc0ca
diff --git a/kornia/geometry/liegroup/_utils.py b/kornia/geometry/liegroup/_utils.pyindex 24aa50d37..9a9788790 100644--- a/kornia/geometry/liegroup/_utils.py+++ b/kornia/geometry/liegroup/_utils.py@@ -1,13 +1,39 @@-from kornia.core import Tensor-from kornia.testing import KORNIA_CHECK+# Temporary shape check functions until KORNIA_CHECK_SHAPE is ready+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}")-def squared_norm(x, y=None) -> Tensor:- return _batched_squared_norm(x, y)+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}")-def _batched_squared_norm(x, y=None):- if y is None:- y = x- KORNIA_CHECK(x.shape == y.shape)- return (x[..., None, :] @ y[..., :, None])[..., 0]+def check_so2_theta_shape(theta):+ theta_shape = theta.shape+ len_theta_shape = len(theta_shape)+ if (+ (len_theta_shape == 2 and theta_shape[1] != 1)+ 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}")+++def check_so2_matrix_shape(matrix):+ matrix_shape = matrix.shape+ len_matrix_shape = len(matrix_shape)+ if (+ (len_matrix_shape == 3 and (matrix_shape[1] != 2 or matrix_shape[2] != 2))+ or (len_matrix_shape == 2 and (matrix_shape[0] != 2 or matrix_shape[1] != 2))+ or (len_matrix_shape > 3 or len_matrix_shape < 2)+ ):+ raise ValueError(f"Invalid input size, we expect [B, 2, 2] or [2, 2]. Got: {matrix_shape}")diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.pyindex f42b9f1ce..796c8672e 100644--- a/kornia/geometry/liegroup/so2.py+++ b/kornia/geometry/liegroup/so2.py@@ -3,6 +3,12 @@from typing import Optional, Unionfrom kornia.core import Module, Parameter, Tensor, complex, stack, tensor, zeros_like+from kornia.geometry.liegroup._utils import (+ check_so2_matrix_shape,+ check_so2_t_shape,+ check_so2_theta_shape,+ check_so2_z_shape,+)from kornia.testing import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR@@ -41,10 +47,7 @@ class So2(Module):super().__init__()KORNIA_CHECK_IS_TENSOR(z)# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- 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}")+ check_so2_z_shape(z)self._z = Parameter(z)def __repr__(self) -> str:@@ -67,14 +70,7 @@ class So2(Module):out = So2(self.z * right.z)elif isinstance(right, Tensor):# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- right_shape = right.shape- len_right_shape = len(right_shape)- if (- (len_right_shape == 3 and (right_shape[1] != 2 or right_shape[2] != 1))- or (len_right_shape == 2 and (sum(right.shape) != 3))- or (len_right_shape > 3 or len_right_shape < 2)- ):- raise ValueError(f"Invalid input size, we expect [B, 2, 1], [2, 1] or [1, 2]. Got: {right_shape}")+ check_so2_t_shape(right)out = self.matrix() @ rightelse:raise TypeError(f"Not So2 or Tensor type. Got: {type(right)}")@@ -100,14 +96,7 @@ class So2(Module):tensor([4.6329e-05+1.j], requires_grad=True)"""# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- theta_shape = theta.shape- len_theta_shape = len(theta_shape)- if (- (len_theta_shape == 2 and theta_shape[1] != 1)- 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}")+ check_so2_theta_shape(theta)return So2(complex(theta.cos(), theta.sin()))def log(self) -> Tensor:@@ -135,14 +124,7 @@ class So2(Module):[1.5707, 0.0000]])"""# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- theta_shape = theta.shape- len_theta_shape = len(theta_shape)- if (- (len_theta_shape == 2 and theta_shape[1] != 1)- 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}")+ check_so2_theta_shape(theta)z = zeros_like(theta)row0 = stack((z, theta), -1)row1 = stack((theta, z), -1)@@ -177,14 +159,7 @@ class So2(Module):tensor(1.+0.j, requires_grad=True)"""# TODO change to KORNIA_CHECK_SHAPE once there is multiple shape support- matrix_shape = matrix.shape- len_matrix_shape = len(matrix_shape)- if (- (len_matrix_shape == 3 and (matrix_shape[1] != 2 or matrix_shape[2] != 2))- or (len_matrix_shape == 2 and (matrix_shape[0] != 2 or matrix_shape[1] != 2))- or (len_matrix_shape > 3 or len_matrix_shape < 2)- ):- raise ValueError(f"Invalid input size, we expect [B, 2, 2] or [2, 2]. Got: {matrix_shape}")+ check_so2_matrix_shape(matrix)z = complex(matrix[..., 0, 0], matrix[..., 1, 0])return cls(z)diff --git a/test/geometry/liegroup/test_so2.py b/test/geometry/liegroup/test_so2.pyindex babe88176..9eefba60d 100644--- a/test/geometry/liegroup/test_so2.py+++ b/test/geometry/liegroup/test_so2.py@@ -11,15 +11,17 @@ class TestSo2(BaseTester):shape = input_shape[1:] if batch_size is None else input_shapereturn torch.rand(shape, device=device, dtype=dtype)- def test_smoke(self, device, dtype):- z = torch.randn(2, 1, dtype=torch.cfloat, device=device)+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_smoke(self, device, cdtype):+ z = torch.randn(2, 1, dtype=cdtype, device=device)s = So2(z)assert isinstance(s, So2)self.assert_close(s.z.data, z.data)@pytest.mark.parametrize("input_shape", [(1,), (2,), (5,), ()])- def test_cardinality(self, device, dtype, input_shape):- z = torch.randn(input_shape, dtype=torch.cfloat, device=device)+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_cardinality(self, device, dtype, input_shape, cdtype):+ z = torch.randn(input_shape, dtype=cdtype, device=device)s = So2(z)theta = torch.rand(input_shape, dtype=dtype, device=device)assert s.z.shape == input_shape@@ -40,9 +42,10 @@ class TestSo2(BaseTester):assert s.matrix().shape == input_shape@pytest.mark.parametrize("batch_size", (1, 2, 5))- def test_exception(self, batch_size, device, dtype):+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_exception(self, batch_size, device, dtype, cdtype):with pytest.raises(ValueError):- z = torch.randn(batch_size, 2, dtype=torch.cfloat, device=device)+ z = torch.randn(batch_size, 2, dtype=cdtype, device=device)assert So2(z)with pytest.raises(TypeError):assert So2.identity(1, device, dtype) * [1.0, 2.0, 1.0]@@ -71,23 +74,35 @@ class TestSo2(BaseTester):pass@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))- def test_init(self, device, dtype, batch_size):- z = self._make_rand_data(device, dtype, (batch_size, 2))- s1 = So2(torch.complex(z[..., 0, None], z[..., 1, None]))+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_init(self, device, dtype, batch_size, cdtype):+ z1 = self._make_rand_data(device, cdtype, (batch_size,))+ z2 = self._make_rand_data(device, cdtype, (batch_size, 1))+ z3_real = self._make_rand_data(device, dtype, (batch_size,))+ z3_imag = self._make_rand_data(device, dtype, (batch_size,))+ z3 = torch.complex(z3_real, z3_imag)+ s1 = So2(z1)s2 = So2(s1.z)assert isinstance(s2, So2)- self.assert_close(s1.z.data, s2.z.data)+ self.assert_close(s1.z, s2.z)+ self.assert_close(So2(z1).z, z1)+ self.assert_close(So2(z2).z, z2)+ self.assert_close(So2(z3).z, z3)@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))- def test_getitem(self, device, dtype, batch_size):- data = self._make_rand_data(device, dtype, (batch_size, 2))- z = torch.complex(data[..., 0, None], data[..., 1, None])+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_getitem(self, device, batch_size, cdtype):+ z = self._make_rand_data(device, cdtype, (batch_size,))s = So2(z)n = 1 if batch_size is None else batch_sizefor i in range(n):- s1 = s[i]- self.assert_close(s1.z.data[0].real.squeeze(), z.data[i].real.squeeze())- self.assert_close(s1.z.data[0].imag.squeeze(), z.data[i].imag.squeeze())+ if batch_size is None:+ expected = s.z+ actual = z+ else:+ expected = s[i].z.data.squeeze()+ actual = z[i]+ self.assert_close(expected, actual)@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))def test_mul(self, device, dtype, batch_size):@@ -111,11 +126,11 @@ class TestSo2(BaseTester):self.assert_close(s.z.imag, theta.sin())@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))- def test_log(self, device, dtype, batch_size):- data = self._make_rand_data(device, dtype, (batch_size, 2))- z = torch.complex(data[..., 0], data[..., 1])+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_log(self, device, batch_size, cdtype):+ z = self._make_rand_data(device, cdtype, (batch_size,))t = So2(z).log()- self.assert_close(t, data[..., 1].atan2(data[..., 0]))+ self.assert_close(t, z.imag.atan2(z.real))@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))def test_exp_log(self, device, dtype, batch_size):@@ -133,15 +148,15 @@ class TestSo2(BaseTester):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))- ss = So2.exp(theta)- p1 = ss * t- p2 = ss.matrix() @ t+ s = So2.exp(theta)+ p1 = s * t+ p2 = s.matrix() @ tself.assert_close(p1, p2)@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))- def test_inverse(self, device, dtype, batch_size):- data = self._make_rand_data(device, dtype, (batch_size, 2))- z = torch.complex(data[..., 0], data[..., 1])+ @pytest.mark.parametrize("cdtype", (torch.cfloat, torch.cdouble))+ def test_inverse(self, device, batch_size, cdtype):+ z = self._make_rand_data(device, cdtype, (batch_size,))s = So2(z)self.assert_close(s.inverse().inverse().z.real, z.real)self.assert_close(s.inverse().inverse().z.imag, z.imag)