π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
refactor compose_transformations
edgarriba397be1fa79f0
diff --git a/docs/source/transformations.rst b/docs/source/transformations.rstindex ff21e365b..15959c83f 100644--- a/docs/source/transformations.rst+++ b/docs/source/transformations.rst@@ -3,7 +3,6 @@ Linear Transformations.. currentmodule:: torchgeometry-.. autofunction:: relative_pose-.. autofunction:: inverse_pose--.. autoclass:: InversePose+.. autofunction:: relative_transformation+.. autofunction:: inverse_transformation+.. autofunction:: compose_transformationsdiff --git a/test/test_transformations.py b/test/test_transformations.pyindex c4b1d0c8e..42a9cb166 100644--- a/test/test_transformations.py+++ b/test/test_transformations.py@@ -13,15 +13,34 @@ class TestComposeTransforms:def _identity_matrix(self, batch_size):return torch.eye(4).repeat(batch_size, 1, 1) # Nx4x4- def test_compose_transforms(self):- batch_size = 1- trans_1 = self._identity_matrix(batch_size)- trans_2 = self._identity_matrix(batch_size)- trans_2[..., :3, -1] += 10 # add offset to translation vector+ def test_compose_transforms_4x4(self):+ offset = 10+ trans_01 = self._identity_matrix(batch_size=1)[0]+ trans_12 = self._identity_matrix(batch_size=1)[0]+ trans_12[..., :3, -1] += offset # add offset to translation vector- trans_21 = tgm.compose_transformations(trans_1, trans_2)- assert utils.check_equal_torch(trans_21, trans_2)+ trans_02 = tgm.compose_transformations(trans_01, trans_12)+ assert utils.check_equal_torch(trans_02, trans_12)+ @pytest.mark.parametrize("batch_size", [1, 2, 5])+ def test_compose_transforms_Bx4x4(self, batch_size):+ offset = 10+ trans_01 = self._identity_matrix(batch_size)+ trans_12 = self._identity_matrix(batch_size)+ trans_12[..., :3, -1] += offset # add offset to translation vector++ trans_02 = tgm.compose_transformations(trans_01, trans_12)+ assert utils.check_equal_torch(trans_02, trans_12)++ @pytest.mark.parametrize("batch_size", [1, 2, 5])+ def test_gradcheck(self, batch_size):+ trans_01 = self._identity_matrix(batch_size)+ trans_12 = self._identity_matrix(batch_size)++ trans_01 = utils.tensor_to_gradcheck_var(trans_01) # to var+ trans_12 = utils.tensor_to_gradcheck_var(trans_12) # to var+ assert gradcheck(tgm.compose_transformations, (trans_01, trans_12,),+ raise_exception=True)class TestRelativeTransform:@@ -118,17 +137,13 @@ def test_inverse_pose(batch_size, device_type):dst_pose_src[:, -1, -1] = 1.0# compute the inverse of the pose- src_pose_dst = tgm.inverse_pose(dst_pose_src)+ src_pose_dst = tgm.inverse_transformation(dst_pose_src)# H_inv * H == Ieye = torch.matmul(src_pose_dst, dst_pose_src)assert utils.check_equal_torch(eye, torch.eye(4), eps=1e-3)- # functional- eye = torch.matmul(tgm.InversePose()(dst_pose_src), dst_pose_src)- assert utils.check_equal_torch(eye, torch.eye(4), eps=1e-3)-# evaluate function gradientdst_pose_src = utils.tensor_to_gradcheck_var(dst_pose_src) # to var- assert gradcheck(tgm.inverse_pose, (dst_pose_src,),+ assert gradcheck(tgm.inverse_transformation, (dst_pose_src,),raise_exception=True)diff --git a/torchgeometry/core/transformations.py b/torchgeometry/core/transformations.pyindex 0a1abd038..b800a270a 100644--- a/torchgeometry/core/transformations.py+++ b/torchgeometry/core/transformations.py@@ -12,42 +12,57 @@ __all__ = [def compose_transformations(- trans_1: torch.Tensor, trans_2: torch.Tensor,- eps: Optional[float] = 1e-6) -> torch.Tensor:- r"""Functions that composes two homogeneous transformations."""- if not torch.is_tensor(trans_1):- raise TypeError("Input trans_1 type is not a torch.Tensor. Got {}"- .format(type(trans_1)))- if not torch.is_tensor(trans_2):- raise TypeError("Input trans_2 type is not a torch.Tensor. Got {}"- .format(type(trans_2)))- if not (len(trans_1.shape) == 3 and trans_1.shape[-2:] == (4, 4)):- raise ValueError("Input trans_1 must be a of the shape Nx4x4."- " Got {}".format(trans_1.shape))- if not (len(trans_2.shape) == 3 and trans_2.shape[-2:] == (4, 4)):- raise ValueError("Input trans_2 must be a of the shape Nx4x4."- " Got {}".format(trans_2.shape))+ trans_01: torch.Tensor, trans_12: torch.Tensor) -> torch.Tensor:+ r"""Functions that composes two homogeneous transformations.++ Args:+ trans_01 (torch.Tensor): tensor with the homogenous transformation from+ a reference frame 1 respect to a frame 0. The tensor has must have a+ shape of :math:`(B, 4, 4)` or :math:`(4, 4)`.+ trans_12 (torch.Tensor): tensor with the homogenous transformation from+ a reference frame 2 respect to a frame 1. The tensor has must have a+ shape of :math:`(B, 4, 4)` or :math:`(4, 4)`.++ Returns:+ torch.Tensor: the transformation between the reference frame 1 respect+ to frame 2. The output shape will be :math:`(B, 4, 4)` or+ :math:`(4 ,4)`.++ """+ if not torch.is_tensor(trans_01):+ raise TypeError("Input trans_01 type is not a torch.Tensor. Got {}"+ .format(type(trans_01)))+ if not torch.is_tensor(trans_12):+ raise TypeError("Input trans_12 type is not a torch.Tensor. Got {}"+ .format(type(trans_12)))+ 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):+ 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():+ raise ValueError("Input number of dims must match. Got {} and {}"+ .format(trans_01.dim(), trans_12.dim()))# unpack input data- rmat_1: torch.Tensor = trans_1[..., :3, :3] # Nx3x3- rmat_2: torch.Tensor = trans_2[..., :3, :3] # Nx3x3- tvec_1: torch.Tensor = trans_1[..., :3, -1:] # Nx3x1- tvec_2: torch.Tensor = trans_2[..., :3, -1:] # Nx3x1+ rmat_01: torch.Tensor = trans_01[..., :3, :3] # Nx3x3+ rmat_12: torch.Tensor = trans_12[..., :3, :3] # Nx3x3+ tvec_01: torch.Tensor = trans_01[..., :3, -1:] # Nx3x1+ tvec_12: torch.Tensor = trans_12[..., :3, -1:] # Nx3x1- # compute the actual transforms composition, return the transform- # from frame two to one. The variable should be trans_2_to_1, but for- # simplicity it's just trans_21.- rmat_21: torch.Tensor = torch.matmul(rmat_1, rmat_2)- tvec_21: torch.Tensor = torch.matmul(rmat_1, tvec_2) + tvec_1+ # compute the actual transforms composition+ rmat_02: torch.Tensor = torch.matmul(rmat_01, rmat_12)+ tvec_02: torch.Tensor = torch.matmul(rmat_01, tvec_12) + tvec_01# pack output tensor- trans_21: torch.Tensor = torch.zeros_like(trans_1)- trans_21[..., :3, 0:3] += rmat_21- trans_21[..., :3, -1:] += tvec_21- trans_21[..., -1, -1:] += 1.0- return trans_21 + eps+ trans_02: torch.Tensor = torch.zeros_like(trans_01)+ trans_02[..., :3, 0:3] += rmat_02+ trans_02[..., :3, -1:] += tvec_02+ trans_02[..., -1, -1:] += 1.0+ return trans_02-def inverse_transformation(trans_12, eps=1e-6):+def inverse_transformation(trans_12):r"""Function that inverts a 4x4 homogeneous transformation:math:`P = \begin{bmatrix} R & t \\ \mathbf{0} & 1 \end{bmatrix}`@@ -73,17 +88,17 @@ def inverse_transformation(trans_12, eps=1e-6):>>> trans_21 = tgm.inverse_transformation(trans_12) # Nx4x4"""if not torch.is_tensor(trans_12):- raise TypeError("Input type is not a torch.Tensor. Got {}".format(- type(trans_12)))- if not len(trans_12.shape) == 3 and trans_12.shape[-2:] == (4, 4):- raise ValueError("Input size must be a Nx4x4 tensor. Got {}"+ raise TypeError("Input type is not a torch.Tensor. Got {}"+ .format(type(trans_12)))+ if not trans_12.dim() in (2, 3) and trans_12.shape[-2:] == (4, 4):+ raise ValueError("Input size must be a Nx4x4 or 4x4. Got {}".format(trans_12.shape))# unpack input tensorrmat_12: torch.Tensor = trans_12[..., :3, 0:3] # Nx3x3tvec_12: torch.Tensor = trans_12[..., :3, 3:4] # Nx3x1# compute the actual inverse- rmat_21: torch.Tensor = torch.transpose(rmat_12, 1, 2)+ rmat_21: torch.Tensor = torch.transpose(rmat_12, -1, -2)tvec_21: torch.Tensor = torch.matmul(-rmat_21, tvec_12)# pack to output tensor@@ -91,12 +106,11 @@ def inverse_transformation(trans_12, eps=1e-6):trans_21[..., :3, 0:3] += rmat_21trans_21[..., :3, -1:] += tvec_21trans_21[..., -1, -1:] += 1.0- return trans_21 + eps+ return trans_21def relative_transformation(- trans_1: torch.Tensor, trans_2: torch.Tensor,- eps: Optional[float] = 1e-6) -> torch.Tensor:+ trans_01: torch.Tensor, trans_02: torch.Tensor) -> torch.Tensor:r"""Function that computes the relative transformation from a referencepose :math:`P_1^{\{W\}} = \begin{bmatrix} R_1 & t_1 \\ \mathbf{0} & 1\end{bmatrix}` to destination :math:`P_2^{\{W\}} = \begin{bmatrix} R_2 &@@ -127,35 +141,24 @@ def relative_transformation(>>> pose_2 = torch.eye(4).unsqueeze(0) # 1x4x4>>> pose_21 = tgm.relative_pose(pose_1, pose_2) # 1x4x4"""- if not torch.is_tensor(trans_1):- raise TypeError("Input trans_1 type is not a torch.Tensor. Got {}"- .format(type(pose_1)))- if not torch.is_tensor(trans_2):- raise TypeError("Input trans_2 type is not a torch.Tensor. Got {}"- .format(type(trans_2)))- if not (len(trans_1.shape) == 3 and trans_1.shape[-2:] == (4, 4)):- raise ValueError("Input must be a of the shape Nx4x4."- " Got {}".format(trans_1.shape))- if not (len(trans_2.shape) == 3 and trans_2.shape[-2:] == (4, 4)):- raise ValueError("Input must be a of the shape Nx4x4."- " Got {}".format(trans_2.shape))- # unpack input data- rmat_1: torch.Tensor = trans_1[..., :3, :3] # Nx3x3- rmat_2: torch.Tensor = trans_2[..., :3, :3] # Nx3x3- tvec_1: torch.Tensor = trans_1[..., :3, -1:] # Nx3x1- tvec_2: torch.Tensor = trans_2[..., :3, -1:] # Nx3x1-- # compute relative transformation- rmat_2_trans: torch.Tensor = rmat_2.transpose(1, 2)- rmat_12: torch.Tensor = torch.matmul(rmat_2_trans, rmat_1)- tvec_12: torch.Tensor = torch.matmul(rmat_2_trans, tvec_1 - tvec_2)-- # pack output data- trans_12: torch.Tensor = torch.zeros_like(trans_1)- trans_12[..., :3, 0:3] += rmat_12- trans_12[..., :3, -1:] += tvec_12- trans_12[..., -1, -1:] += 1.0- return trans_12 + eps+ if not torch.is_tensor(trans_01):+ raise TypeError("Input trans_01 type is not a torch.Tensor. Got {}"+ .format(type(pose_01)))+ if not torch.is_tensor(trans_02):+ raise TypeError("Input trans_02 type is not a torch.Tensor. Got {}"+ .format(type(trans_02)))+ if not (len(trans_01.shape) 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 (len(trans_02.shape) 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("Input number of dims must match. Got {} and {}"+ .format(trans_01.dim(), trans_02.dim()))+ trans_10: torch.Tensor = inverse_transformation(trans_01)+ trans_12: torch.Tensor = compose_transformations(trans_10, trans_02)+ return trans_12# layer api