Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

parametrize inverse_pose test

edgarriba2e8a0b120b9d
diff --git a/test/test_functional.py b/test/test_functional.py
index 39bb300a3..e3de841b3 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -6,36 +6,32 @@ import torchgeometry as tgm
from torch.autograd import gradcheck
import utils # test utilities
+from common import TEST_DEVICES
-class Tester(unittest.TestCase):
-
- def test_inverse_pose(self):
- # generate input data
- batch_size = 1
- eye_size = 4 # identity 4x4
- dst_pose_src = utils.create_random_homography(batch_size, eye_size)
- dst_pose_src[:, -1] = 0.0
- dst_pose_src[:, -1, -1] = 1.0
[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)
+ # 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)
- res = utils.check_equal_torch(eye, torch.eye(4), eps=1e-3)
+ # H_inv * H == I
+ eye = torch.matmul(src_pose_dst, dst_pose_src)
+ res = utils.check_equal_torch(eye, torch.eye(4), eps=1e-3)
- def test_inverse_pose_gradcheck(self):
- # generate input data
- batch_size = 2
- eye_size = 4 # identity 4x4
- dst_pose_src = utils.create_random_homography(batch_size, eye_size)
- dst_pose_src = utils.tensor_to_gradcheck_var(dst_pose_src) # to var
+ # 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)
- # evaluate function gradient
- res = gradcheck(tgm.inverse_pose, (dst_pose_src,),
- raise_exception=True)
- self.assertTrue(res)
+class Tester(unittest.TestCase):
def test_homography_i_H_ref(self):
# generate input data
diff --git a/torchgeometry/pinhole.py b/torchgeometry/pinhole.py
index e3d920294..5d8189026 100644
--- a/torchgeometry/pinhole.py
+++ b/torchgeometry/pinhole.py
@@ -40,25 +40,18 @@ def inverse_pose(pose, eps=1e-6):
if not torch.is_tensor(pose):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(pose)))
- if not pose.shape[-2:] == (4, 4):
- raise ValueError("Input size must be a 4x4 tensor. Got {}"
+ 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))
- pose_shape = pose.shape
- if len(pose_shape) == 2:
- pose = torch.unsqueeze(pose, dim=0)
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 = torch.zeros_like(pose) + eps
+ 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
-
- if len(pose_shape) == 2:
- pose_inv = torch.squeeze(pose_inv, dim=0)
-
return pose_inv