π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add typehints for `kornia.geometry.linalg.inverse_transformation` (#2286)
Ngo Viet Hoai Bao682f4771bd51
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.pyindex ff3f09357..c897ad327 100644--- a/kornia/geometry/linalg.py+++ b/kornia/geometry/linalg.py@@ -1,8 +1,8 @@from __future__ import annotationsimport torch-from torch import Tensor+from kornia.core import Tensorfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPEfrom kornia.geometry.conversions import convert_points_from_homogeneous, convert_points_to_homogeneous@@ -19,7 +19,7 @@ __all__ = []-def compose_transformations(trans_01: torch.Tensor, trans_12: torch.Tensor) -> torch.Tensor:+def compose_transformations(trans_01: Tensor, trans_12: Tensor) -> Tensor:r"""Function that composes two homogeneous transformations... math::@@ -43,10 +43,10 @@ def compose_transformations(trans_01: torch.Tensor, trans_12: torch.Tensor) -> t>>> trans_02 = compose_transformations(trans_01, trans_12) # 4x4"""if not torch.is_tensor(trans_01):- raise TypeError(f"Input trans_01 type is not a torch.Tensor. Got {type(trans_01)}")+ raise TypeError(f"Input trans_01 type is not a Tensor. Got {type(trans_01)}")if not torch.is_tensor(trans_12):- raise TypeError(f"Input trans_12 type is not a torch.Tensor. Got {type(trans_12)}")+ raise TypeError(f"Input trans_12 type is not a Tensor. Got {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))@@ -58,24 +58,24 @@ def compose_transformations(trans_01: torch.Tensor, trans_12: torch.Tensor) -> traise ValueError(f"Input number of dims must match. Got {trans_01.dim()} and {trans_12.dim()}")# unpack input data- 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+ rmat_01: Tensor = trans_01[..., :3, :3] # Nx3x3+ rmat_12: Tensor = trans_12[..., :3, :3] # Nx3x3+ tvec_01: Tensor = trans_01[..., :3, -1:] # Nx3x1+ tvec_12: Tensor = trans_12[..., :3, -1:] # Nx3x1# 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+ rmat_02: Tensor = torch.matmul(rmat_01, rmat_12)+ tvec_02: Tensor = torch.matmul(rmat_01, tvec_12) + tvec_01# pack output tensor- trans_02: torch.Tensor = torch.zeros_like(trans_01)+ trans_02: Tensor = torch.zeros_like(trans_01)trans_02[..., :3, 0:3] += rmat_02trans_02[..., :3, -1:] += tvec_02trans_02[..., -1, -1:] += 1.0return trans_02-def inverse_transformation(trans_12):+def inverse_transformation(trans_12: Tensor) -> Tensor:r"""Function that inverts a 4x4 homogeneous transformation:math:`T_1^{2} = \begin{bmatrix} R_1 & t_1 \\ \mathbf{0} & 1 \end{bmatrix}`@@ -97,26 +97,26 @@ def inverse_transformation(trans_12):>>> trans_21 = inverse_transformation(trans_12) # Nx4x4"""if not torch.is_tensor(trans_12):- raise TypeError(f"Input type is not a torch.Tensor. Got {type(trans_12)}")+ raise TypeError(f"Input type is not a Tensor. Got {type(trans_12)}")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 tensor- rmat_12: torch.Tensor = trans_12[..., :3, 0:3] # Nx3x3- tvec_12: torch.Tensor = trans_12[..., :3, 3:4] # Nx3x1+ rmat_12: Tensor = trans_12[..., :3, 0:3] # Nx3x3+ tvec_12: Tensor = trans_12[..., :3, 3:4] # Nx3x1# compute the actual inverse- rmat_21: torch.Tensor = torch.transpose(rmat_12, -1, -2)- tvec_21: torch.Tensor = torch.matmul(-rmat_21, tvec_12)+ rmat_21: Tensor = torch.transpose(rmat_12, -1, -2)+ tvec_21: Tensor = torch.matmul(-rmat_21, tvec_12)# pack to output tensor- trans_21: torch.Tensor = torch.zeros_like(trans_12)+ trans_21: Tensor = torch.zeros_like(trans_12)trans_21[..., :3, 0:3] += rmat_21trans_21[..., :3, -1:] += tvec_21trans_21[..., -1, -1:] += 1.0return trans_21-def relative_transformation(trans_01: torch.Tensor, trans_02: torch.Tensor) -> torch.Tensor:+def relative_transformation(trans_01: Tensor, trans_02: Tensor) -> Tensor:r"""Function that computes the relative homogeneous transformation from areference transformation :math:`T_1^{0} = \begin{bmatrix} R_1 & t_1 \\\mathbf{0} & 1 \end{bmatrix}` to destination :math:`T_2^{0} =@@ -141,29 +141,29 @@ def relative_transformation(trans_01: torch.Tensor, trans_02: torch.Tensor) -> t>>> trans_12 = relative_transformation(trans_01, trans_02) # 4x4"""if not torch.is_tensor(trans_01):- raise TypeError(f"Input trans_01 type is not a torch.Tensor. Got {type(trans_01)}")+ raise TypeError(f"Input trans_01 type is not a 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)}")+ raise TypeError(f"Input trans_02 type is not a Tensor. Got {type(trans_02)}")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))):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()}")- trans_10: torch.Tensor = inverse_transformation(trans_01)- trans_12: torch.Tensor = compose_transformations(trans_10, trans_02)+ trans_10: Tensor = inverse_transformation(trans_01)+ trans_12: Tensor = compose_transformations(trans_10, trans_02)return trans_12-def transform_points(trans_01: torch.Tensor, points_1: torch.Tensor) -> torch.Tensor:+def transform_points(trans_01: Tensor, points_1: Tensor) -> Tensor:r"""Function that applies transformations to a set of points.Args:- trans_01 (torch.Tensor): tensor for transformations of shape+ trans_01 (Tensor): tensor for transformations of shape:math:`(B, D+1, D+1)`.- points_1 (torch.Tensor): tensor of points of shape :math:`(B, N, D)`.+ points_1 (Tensor): tensor of points of shape :math:`(B, N, D)`.Returns:- torch.Tensor: tensor of N-dimensional points.+ Tensor: tensor of N-dimensional points.Shape:- Output: :math:`(B, N, D)`