π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix test error checks in geometry linalg (#1382)
Bharath Raj7700ce08cb33
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.pyindex 176a4c057..9a1d9bee4 100644--- a/kornia/geometry/linalg.py+++ b/kornia/geometry/linalg.py@@ -23,10 +23,10 @@ def compose_transformations(trans_01: torch.Tensor, trans_12: torch.Tensor) -> tArgs:trans_01: tensor with the homogeneous transformation froma reference frame 1 respect to a frame 0. The tensor has must have a- shape of :math:`(B, 4, 4)` or :math:`(4, 4)`.+ shape of :math:`(N, 4, 4)` or :math:`(4, 4)`.trans_12: tensor with the homogeneous transformation froma reference frame 2 respect to a frame 1. The tensor has must have a- shape of :math:`(B, 4, 4)` or :math:`(4, 4)`.+ shape of :math:`(N, 4, 4)` or :math:`(4, 4)`.Returns:the transformation between the two frames with shape :math:`(N, 4, 4)` or :math:`(4, 4)`.@@ -43,10 +43,10 @@ def compose_transformations(trans_01: torch.Tensor, trans_12: torch.Tensor) -> tif not torch.is_tensor(trans_12):raise TypeError(f"Input trans_12 type is not a torch.Tensor. Got {type(trans_12)}")- if not trans_01.dim() in (2, 3) and trans_01.shape[-2:] == (4, 4):+ if not ((trans_01.dim() in (2, 3)) and (trans_01.shape[-2:] == (4, 4))):raise ValueError("Input trans_01 must be a of the shape Nx4x4 or 4x4." " Got {}".format(trans_01.shape))- if not trans_12.dim() in (2, 3) and trans_12.shape[-2:] == (4, 4):+ if not ((trans_12.dim() in (2, 3)) and (trans_12.shape[-2:] == (4, 4))):raise ValueError("Input trans_12 must be a of the shape Nx4x4 or 4x4." " Got {}".format(trans_12.shape))if not trans_01.dim() == trans_12.dim():@@ -93,7 +93,7 @@ def inverse_transformation(trans_12):"""if not torch.is_tensor(trans_12):raise TypeError(f"Input type is not a torch.Tensor. Got {type(trans_12)}")- if not trans_12.dim() in (2, 3) and trans_12.shape[-2:] == (4, 4):+ if not ((trans_12.dim() in (2, 3)) and (trans_12.shape[-2:] == (4, 4))):raise ValueError(f"Input size must be a Nx4x4 or 4x4. Got {trans_12.shape}")# unpack input tensorrmat_12: torch.Tensor = trans_12[..., :3, 0:3] # Nx3x3@@ -139,9 +139,9 @@ def relative_transformation(trans_01: torch.Tensor, trans_02: torch.Tensor) -> traise TypeError(f"Input trans_01 type is not a torch.Tensor. Got {type(trans_01)}")if not torch.is_tensor(trans_02):raise TypeError(f"Input trans_02 type is not a torch.Tensor. Got {type(trans_02)}")- if not trans_01.dim() in (2, 3) and trans_01.shape[-2:] == (4, 4):+ if not ((trans_01.dim() in (2, 3)) and (trans_01.shape[-2:] == (4, 4))):raise ValueError("Input must be a of the shape Nx4x4 or 4x4." " Got {}".format(trans_01.shape))- if not trans_02.dim() in (2, 3) and trans_02.shape[-2:] == (4, 4):+ if not ((trans_02.dim() in (2, 3)) and (trans_02.shape[-2:] == (4, 4))):raise ValueError("Input must be a of the shape Nx4x4 or 4x4." " Got {}".format(trans_02.shape))if not trans_01.dim() == trans_02.dim():raise ValueError(f"Input number of dims must match. Got {trans_01.dim()} and {trans_02.dim()}")diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.pyindex dac1ac243..89512070d 100644--- a/test/geometry/test_linalg.py+++ b/test/geometry/test_linalg.py@@ -124,6 +124,42 @@ class TestTransformPoints:class TestComposeTransforms:+ def test_smoke(self, device, dtype):+ batch_size = 2+ trans_01 = identity_matrix(batch_size=batch_size, device=device, dtype=dtype)+ trans_12 = identity_matrix(batch_size=batch_size, device=device, dtype=dtype)++ to_check_1 = kornia.geometry.compose_transformations(trans_01, trans_12)+ to_check_2 = kornia.geometry.compose_transformations(trans_01[0], trans_12[0])++ assert to_check_1.shape == (batch_size, 4, 4)+ assert to_check_2.shape == (4, 4)++ def test_exception(self, device, dtype):+ to_check_1 = torch.rand((7, 4, 4, 3), device=device, dtype=dtype)+ to_check_2 = torch.rand((5, 10, 10), device=device, dtype=dtype)+ to_check_3 = torch.rand((6, 4, 4), device=device, dtype=dtype)+ to_check_4 = torch.rand((4, 4), device=device, dtype=dtype)+ to_check_5 = torch.rand((3, 3), device=device, dtype=dtype)++ # Testing if exception is thrown when both inputs have shape (3, 3)+ with pytest.raises(ValueError):+ _ = kornia.geometry.compose_transformations(to_check_5, to_check_5)++ # Testing if exception is thrown when both inputs have shape (5, 10, 10)+ with pytest.raises(ValueError):+ _ = kornia.geometry.compose_transformations(to_check_2, to_check_2)++ # Testing if exception is thrown when one input has shape (6, 4, 4)+ # whereas the other input has shape (4, 4)+ with pytest.raises(ValueError):+ _ = kornia.geometry.compose_transformations(to_check_3, to_check_4)++ # Testing if exception is thrown when one input has shape (7, 4, 4, 3)+ # whereas the other input has shape (4, 4)+ with pytest.raises(ValueError):+ _ = kornia.geometry.compose_transformations(to_check_1, to_check_4)+def test_translation_4x4(self, device, dtype):offset = 10trans_01 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]@@ -154,6 +190,33 @@ class TestComposeTransforms:class TestInverseTransformation:+ def test_smoke(self, device, dtype):+ batch_size = 2+ trans_01 = identity_matrix(batch_size=batch_size, device=device, dtype=dtype)++ to_check_1 = kornia.geometry.inverse_transformation(trans_01)+ to_check_2 = kornia.geometry.inverse_transformation(trans_01[0])++ assert to_check_1.shape == (batch_size, 4, 4)+ assert to_check_2.shape == (4, 4)++ def test_exception(self, device, dtype):+ to_check_1 = torch.rand((7, 4, 4, 3), device=device, dtype=dtype)+ to_check_2 = torch.rand((5, 10, 10), device=device, dtype=dtype)+ to_check_3 = torch.rand((3, 3), device=device, dtype=dtype)++ # Testing if exception is thrown when the input has shape (7, 4, 4, 3)+ with pytest.raises(ValueError):+ _ = kornia.geometry.inverse_transformation(to_check_1)++ # Testing if exception is thrown when the input has shape (5, 10, 10)+ with pytest.raises(ValueError):+ _ = kornia.geometry.inverse_transformation(to_check_2)++ # Testing if exception is thrown when the input has shape (3, 3)+ with pytest.raises(ValueError):+ _ = kornia.geometry.inverse_transformation(to_check_3)+def test_translation_4x4(self, device, dtype):offset = 10trans_01 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]@@ -196,6 +259,42 @@ class TestInverseTransformation:class TestRelativeTransformation:+ def test_smoke(self, device, dtype):+ batch_size = 2+ trans_01 = identity_matrix(batch_size=batch_size, device=device, dtype=dtype)+ trans_02 = identity_matrix(batch_size=batch_size, device=device, dtype=dtype)++ to_check_1 = kornia.geometry.relative_transformation(trans_01, trans_02)+ to_check_2 = kornia.geometry.relative_transformation(trans_01[0], trans_02[0])++ assert to_check_1.shape == (batch_size, 4, 4)+ assert to_check_2.shape == (4, 4)++ def test_exception(self, device, dtype):+ to_check_1 = torch.rand((7, 4, 4, 3), device=device, dtype=dtype)+ to_check_2 = torch.rand((5, 10, 10), device=device, dtype=dtype)+ to_check_3 = torch.rand((6, 4, 4), device=device, dtype=dtype)+ to_check_4 = torch.rand((4, 4), device=device, dtype=dtype)+ to_check_5 = torch.rand((3, 3), device=device, dtype=dtype)++ # Testing if exception is thrown when both inputs have shape (3, 3)+ with pytest.raises(ValueError):+ _ = kornia.geometry.relative_transformation(to_check_5, to_check_5)++ # Testing if exception is thrown when both inputs have shape (5, 10, 10)+ with pytest.raises(ValueError):+ _ = kornia.geometry.relative_transformation(to_check_2, to_check_2)++ # Testing if exception is thrown when one input has shape (6, 4, 4)+ # whereas the other input has shape (4, 4)+ with pytest.raises(ValueError):+ _ = kornia.geometry.relative_transformation(to_check_3, to_check_4)++ # Testing if exception is thrown when one input has shape (7, 4, 4, 3)+ # whereas the other input has shape (4, 4)+ with pytest.raises(ValueError):+ _ = kornia.geometry.relative_transformation(to_check_1, to_check_4)+def test_translation_4x4(self, device, dtype):offset = 10.0trans_01 = identity_matrix(batch_size=1, device=device, dtype=dtype)[0]