π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement relative_pose function
edgarriba0ef2105d7f14
diff --git a/test/test_transformations.py b/test/test_transformations.pynew file mode 100644index 000000000..80d146ef0--- /dev/null+++ b/test/test_transformations.py@@ -0,0 +1,71 @@+import pytest++import torch+import torchgeometry as tgm+from torch.autograd import gradcheck++import utils # test utilities+from common import TEST_DEVICES++class TestTransformPose:+ def test_identity(self):+ pose_1 = torch.eye(4).unsqueeze(0)+ pose_2 = torch.eye(4).unsqueeze(0)+ pose_21 = tgm.relative_pose(pose_1, pose_2)+ assert utils.check_equal_torch(pose_21, torch.eye(4).unsqueeze(0))++ def test_translation(self):+ offset = 10.+ pose_1 = torch.eye(4).unsqueeze(0)+ pose_2 = torch.eye(4).unsqueeze(0)+ pose_2[..., :3, -1:] += offset # add translation++ # compute relative pose+ pose_21 = tgm.relative_pose(pose_1, pose_2)+ assert utils.check_equal_torch(pose_21[...,:3, -1:], offset)++ def test_rotation(self):+ pose_1 = torch.eye(4).unsqueeze(0)+ pose_2 = torch.zeros(1, 4, 4) # Rz (90deg)+ pose_2[..., 0, 1] = -1.0+ pose_2[..., 1, 0] = 1.0+ pose_2[..., 2, 2] = 1.0+ pose_2[..., 3, 3] = 1.0++ # compute relative pose+ pose_21 = tgm.relative_pose(pose_1, pose_2)+ assert utils.check_equal_torch(pose_21, pose_2)++ @pytest.mark.parametrize("device_type", TEST_DEVICES)+ @pytest.mark.parametrize("batch_size", [1, 2, 5])+ def test_integration(self, batch_size, device_type):+ pose_1 = torch.eye(4).repeat(batch_size, 1, 1)+ pose_1 = pose_1.to(torch.device(device_type))++ pose_2 = torch.eye(4).repeat(batch_size, 1, 1)+ pose_2[..., :3, :3] = torch.rand(batch_size, 3, 3)+ pose_2[..., :3, -1:] = torch.rand(batch_size, 3, 1)+ pose_2 = pose_2.to(torch.device(device_type))++ pose_21 = tgm.relative_pose(pose_1, pose_2)+ assert utils.check_equal_torch(+ torch.matmul(pose_21, pose_1), pose_2)++ @pytest.mark.skip("Converting a tensor to a Python boolean ...")+ def test_jit(self):+ pose_1 = torch.eye(4).unsqueeze(0)+ pose_2 = torch.eye(4).unsqueeze(0)++ pose_21 = tgm.relative_pose(pose_1, pose_2)+ pose_21_jit = torch.jit.trace(+ tgm.relative_pose, (pose_1, pose_2,))(pose_1, pose_2)+ assert utils.check_equal_torch(pose_21, pose_21_jit)++ def test_gradcheck(self):+ pose_1 = torch.eye(4).unsqueeze(0)+ pose_2 = torch.eye(4).unsqueeze(0)++ pose_1 = utils.tensor_to_gradcheck_var(pose_1) # to var+ pose_2 = utils.tensor_to_gradcheck_var(pose_2) # to var+ assert gradcheck(tgm.relative_pose, (pose_1, pose_2,),+ raise_exception=True)diff --git a/torchgeometry/__init__.py b/torchgeometry/__init__.pyindex 9126467b8..645a9da73 100644--- a/torchgeometry/__init__.py+++ b/torchgeometry/__init__.py@@ -4,6 +4,7 @@ from .pinhole import *from .conversions import *from .utils import *from .imgwarp import *+from .transformations import *from torchgeometry import imagefrom torchgeometry import lossesdiff --git a/torchgeometry/transformations.py b/torchgeometry/transformations.pynew file mode 100644index 000000000..d55caa128--- /dev/null+++ b/torchgeometry/transformations.py@@ -0,0 +1,44 @@+from typing import Optional++import torch+++__all__ = [+ "relative_pose",+]+++def relative_pose(pose_1: torch.Tensor, pose_2: torch.Tensor, eps: Optional[float] = 1e-6) -> torch.Tensor:+ """Function that computes the relative transformation from a reference+ pose 1 to 2.+ """+ if not torch.is_tensor(pose_1):+ raise TypeError("Input pose_1 type is not a torch.Tensor. Got {}"+ .format(type(pose_1)))+ if not torch.is_tensor(pose_2):+ raise TypeError("Input pose_2 type is not a torch.Tensor. Got {}"+ .format(type(pose_2)))+ if not (len(pose_1.shape) == 3 and pose_1.shape[-2:] == (4, 4)):+ raise ValueError("Input must be a of the shape Nx4x4."+ " Got {}".format(pose_1.shape, pose_2.shape))+ if not pose_1.shape == pose_2.shape:+ raise ValueError("Input pose_1 and pose_2 must be a of the same shape."+ " Got {}".format(pose_1.shape, pose_2.shape))+ # unpack input data+ rot_mat_1 = pose_1[..., :3, :3] # Nx3x3+ rot_mat_2 = pose_2[..., :3, :3] # Nx3x3+ t_vec_1 = pose_1[..., :3, -1:] # Nx3x1+ t_vec_2 = pose_2[..., :3, -1:] # Nx3x1++ # compute relative pose+ rot_mat_21 = torch.matmul(rot_mat_2, rot_mat_1.transpose(1, 2))+ t_vec_21 = t_vec_2 - torch.matmul(rot_mat_21, t_vec_1)++ # pack output data+ pose_21 = torch.zeros_like(pose_1)+ pose_21[..., :3, :3] = rot_mat_21+ pose_21[..., :3, -1:] = t_vec_21+ pose_21[..., -1, -1] += 1.0+ return pose_21 + eps++