Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

move inverse_pose to transformations.py and add formula to docs

edgarriba2a50f68ce5e7
diff --git a/docs/source/pinhole.rst b/docs/source/pinhole.rst
index 544a01446..0dbc7f222 100644
--- a/docs/source/pinhole.rst
+++ b/docs/source/pinhole.rst
@@ -14,12 +14,10 @@ Pinhole
:math:`(t_x, t_y, t_z)` is the translation vector.
-.. autofunction:: inverse_pose
.. autofunction:: pinhole_matrix
.. autofunction:: inverse_pinhole_matrix
.. autofunction:: scale_pinhole
.. autofunction:: homography_i_H_ref
-.. autoclass:: InversePose
.. autoclass:: PinholeMatrix
.. autoclass:: InversePinholeMatrix
diff --git a/docs/source/transformations.rst b/docs/source/transformations.rst
index 1a4894945..ff21e365b 100644
--- a/docs/source/transformations.rst
+++ b/docs/source/transformations.rst
@@ -4,3 +4,6 @@ Linear Transformations
.. currentmodule:: torchgeometry
.. autofunction:: relative_pose
+.. autofunction:: inverse_pose
+
+.. autoclass:: InversePose
diff --git a/test/test_pinhole.py b/test/test_pinhole.py
index f993cf1f9..2d9211466 100644
--- a/test/test_pinhole.py
+++ b/test/test_pinhole.py
@@ -8,33 +8,6 @@ import utils # test utilities
from common import TEST_DEVICES
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_size", [1, 2, 5, 6])
-def test_inverse_pose(batch_size, device_type):
- # generate input data
- eye_size = 4 # identity 4x4
- dst_pose_src = utils.create_random_homography(batch_size, eye_size)
- dst_pose_src = dst_pose_src.to(torch.device(device_type))
- dst_pose_src[:, -1] = 0.0
- dst_pose_src[:, -1, -1] = 1.0
-
- # compute the inverse of the pose
- src_pose_dst = tgm.inverse_pose(dst_pose_src)
-
- # H_inv * H == I
- eye = 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 gradient
- dst_pose_src = utils.tensor_to_gradcheck_var(dst_pose_src) # to var
- assert gradcheck(tgm.inverse_pose, (dst_pose_src,),
- raise_exception=True)
-
-
@pytest.mark.parametrize("device_type", TEST_DEVICES)
@pytest.mark.parametrize("batch_size", [1, 2, 5, 6])
def test_scale_pinhole(batch_size, device_type):
diff --git a/test/test_transformations.py b/test/test_transformations.py
index 2941a00ea..c98bab805 100644
--- a/test/test_transformations.py
+++ b/test/test_transformations.py
@@ -88,3 +88,31 @@ class TestTransformPose:
self._test_rotation()
self._test_integration()
self._test_gradcheck()
+
+
+# TODO: embedd to a class
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_size", [1, 2, 5, 6])
+def test_inverse_pose(batch_size, device_type):
+ # generate input data
+ eye_size = 4 # identity 4x4
+ dst_pose_src = utils.create_random_homography(batch_size, eye_size)
+ dst_pose_src = dst_pose_src.to(torch.device(device_type))
+ dst_pose_src[:, -1] = 0.0
+ dst_pose_src[:, -1, -1] = 1.0
+
+ # compute the inverse of the pose
+ src_pose_dst = tgm.inverse_pose(dst_pose_src)
+
+ # H_inv * H == I
+ eye = 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 gradient
+ dst_pose_src = utils.tensor_to_gradcheck_var(dst_pose_src) # to var
+ assert gradcheck(tgm.inverse_pose, (dst_pose_src,),
+ raise_exception=True)
diff --git a/torchgeometry/pinhole.py b/torchgeometry/pinhole.py
index 4526bca98..3e8ff7d67 100644
--- a/torchgeometry/pinhole.py
+++ b/torchgeometry/pinhole.py
@@ -6,53 +6,16 @@ from .conversions import rtvec_to_pose
__all__ = [
# functional api
- "inverse_pose",
"pinhole_matrix",
"inverse_pinhole_matrix",
"scale_pinhole",
"homography_i_H_ref",
# layer api
- "InversePose",
"PinholeMatrix",
"InversePinholeMatrix",
]
-def inverse_pose(pose, eps=1e-6):
- r"""Function that inverts a 4x4 pose.
-
- Args:
- points (Tensor): tensor with poses.
-
- Returns:
- Tensor: tensor with inverted poses.
-
- Shape:
- - Input: :math:`(N, 4, 4)`
- - Output: :math:`(N, 4, 4)`
-
- Example:
- >>> pose = torch.rand(1, 4, 4) # Nx4x4
- >>> pose_inv = tgm.inverse_pose(pose) # Nx4x4
- """
- if not torch.is_tensor(pose):
- raise TypeError("Input type is not a torch.Tensor. Got {}".format(
- type(pose)))
- if not len(pose.shape) == 3 and pose.shape[-2:] == (4, 4):
- raise ValueError("Input size must be a Nx4x4 tensor. Got {}"
- .format(pose.shape))
-
- r_mat = pose[..., :3, 0:3] # Nx3x3
- t_vec = pose[..., :3, 3:4] # Nx3x1
- r_mat_trans = torch.transpose(r_mat, 1, 2)
-
- pose_inv = pose.new_zeros(pose.shape) + eps
- pose_inv[..., :3, 0:3] = r_mat_trans
- pose_inv[..., :3, 3:4] = torch.matmul(-1.0 * r_mat_trans, t_vec)
- pose_inv[..., 3, 3] = 1.0
- return pose_inv
-
-
def pinhole_matrix(pinholes, eps=1e-6):
r"""Function that returns the pinhole matrix from a pinhole model
@@ -195,32 +158,6 @@ def homography_i_H_ref(pinhole_i, pinhole_ref):
# layer api
-class InversePose(nn.Module):
- r"""Creates a transformation that inverts a 4x4 pose.
-
- Args:
- points (Tensor): tensor with poses.
-
- Returns:
- Tensor: tensor with inverted poses.
-
- Shape:
- - Input: :math:`(N, 4, 4)`
- - Output: :math:`(N, 4, 4)`
-
- Example:
- >>> pose = torch.rand(1, 4, 4) # Nx4x4
- >>> transform = tgm.InversePose()
- >>> pose_inv = transform(pose) # Nx4x4
- """
-
- def __init__(self):
- super(InversePose, self).__init__()
-
- def forward(self, input):
- return inverse_pose(input)
-
-
class PinholeMatrix(nn.Module):
r"""Creates an object that returns the pinhole matrix from a pinhole model
diff --git a/torchgeometry/transformations.py b/torchgeometry/transformations.py
index 6c46d9e45..0b0b05d25 100644
--- a/torchgeometry/transformations.py
+++ b/torchgeometry/transformations.py
@@ -1,13 +1,59 @@
from typing import Optional
import torch
+import torch.nn as nn
__all__ = [
+ "inverse_pose",
"relative_pose",
+ "InversePose",
]
+def inverse_pose(pose, eps=1e-6):
+ r"""Function that inverts a 4x4 pose :math:`P =
+ \begin{bmatrix} R & t \\ \mathbf{0} & 1 \end{bmatrix}`
+
+ The inverse pose is computed as follows:
+
+ .. math::
+
+ P^{-1} = \begin{bmatrix} R^T & -R^T t \\ \mathbf{0} &
+ 1\end{bmatrix}
+
+ Args:
+ points (Tensor): tensor with poses.
+
+ Returns:
+ Tensor: tensor with inverted poses.
+
+ Shape:
+ - Input: :math:`(N, 4, 4)`
+ - Output: :math:`(N, 4, 4)`
+
+ Example:
+ >>> pose = torch.rand(1, 4, 4) # Nx4x4
+ >>> pose_inv = tgm.inverse_pose(pose) # Nx4x4
+ """
+ if not torch.is_tensor(pose):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(pose)))
+ if not len(pose.shape) == 3 and pose.shape[-2:] == (4, 4):
+ raise ValueError("Input size must be a Nx4x4 tensor. Got {}"
+ .format(pose.shape))
+
+ r_mat = pose[..., :3, 0:3] # Nx3x3
+ t_vec = pose[..., :3, 3:4] # Nx3x1
+ r_mat_trans = torch.transpose(r_mat, 1, 2)
+
+ pose_inv = pose.new_zeros(pose.shape) + eps
+ pose_inv[..., :3, 0:3] = r_mat_trans
+ pose_inv[..., :3, 3:4] = torch.matmul(-1.0 * r_mat_trans, t_vec)
+ pose_inv[..., 3, 3] = 1.0
+ return pose_inv
+
+
def relative_pose(pose_1: torch.Tensor, pose_2: torch.Tensor,
eps: Optional[float] = 1e-6) -> torch.Tensor:
r"""Function that computes the relative transformation from a reference
@@ -69,3 +115,32 @@ def relative_pose(pose_1: torch.Tensor, pose_2: torch.Tensor,
pose_21[..., :3, -1:] = t_vec_21
pose_21[..., -1, -1] += 1.0
return pose_21 + eps
+
+
+# layer api
+
+
+class InversePose(nn.Module):
+ r"""Creates a transformation that inverts a 4x4 pose.
+
+ Args:
+ points (Tensor): tensor with poses.
+
+ Returns:
+ Tensor: tensor with inverted poses.
+
+ Shape:
+ - Input: :math:`(N, 4, 4)`
+ - Output: :math:`(N, 4, 4)`
+
+ Example:
+ >>> pose = torch.rand(1, 4, 4) # Nx4x4
+ >>> transform = tgm.InversePose()
+ >>> pose_inv = transform(pose) # Nx4x4
+ """
+
+ def __init__(self):
+ super(InversePose, self).__init__()
+
+ def forward(self, input):
+ return inverse_pose(input)