Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add test for pinhole_matrix and inverse_pinhole_matrix, fix homography_i_ref test

edgarribaca0ecb37dbe6
diff --git a/test/test_functional.py b/test/test_functional.py
index e3de841b3..298072c98 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -1,4 +1,3 @@
-import unittest
import pytest
import torch
@@ -24,90 +23,121 @@ def test_inverse_pose(batch_size, device_type):
# 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)
+ 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)
-class Tester(unittest.TestCase):
-
- def test_homography_i_H_ref(self):
- # generate input data
- image_height, image_width = 32., 32.
- cx, cy = image_width / 2, image_height / 2
- fx, fy = 1., 1.
- rx, ry, rz = 0., 0., 0.
- tx, ty, tz = 0., 0., 0.
- offset_x = 10. # we will apply a 10units offset to `i` camera
- eps = 1e-6
-
- pinhole_ref = utils.create_pinhole(
- fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
-
- pinhole_i = utils.create_pinhole(
- fx,
- fy,
- cx,
- cy,
- image_height,
- image_width,
- rx,
- ry,
- rx,
- tx + offset_x,
- ty,
- tz)
-
- # compute homography from ref to i
- i_H_ref = tgm.homography_i_H_ref(pinhole_i, pinhole_ref) + eps
- i_H_ref_inv = torch.inverse(i_H_ref)
-
- # compute homography from i to ref
- ref_H_i = tgm.homography_i_H_ref(pinhole_ref, pinhole_i) + eps
-
- res = utils.check_equal_torch(i_H_ref_inv, ref_H_i)
- self.assertTrue(res)
-
- def test_homography_i_H_ref_gradcheck(self):
- # generate input data
- image_height, image_width = 32., 32.
- cx, cy = image_width / 2, image_height / 2
- fx, fy = 1., 1.
- rx, ry, rz = 0., 0., 0.
- tx, ty, tz = 0., 0., 0.
- offset_x = 10. # we will apply a 10units offset to `i` camera
- eps = 1e-6
-
- pinhole_ref = utils.create_pinhole(
- fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
- pinhole_ref = utils.tensor_to_gradcheck_var(pinhole_ref) # to var
-
- pinhole_i = utils.create_pinhole(
- fx,
- fy,
- cx,
- cy,
- image_height,
- image_width,
- rx,
- ry,
- rx,
- tx + offset_x,
- ty,
- tz)
- pinhole_i = utils.tensor_to_gradcheck_var(pinhole_ref) # to var
-
- # evaluate function gradient
- res = gradcheck(
- tgm.homography_i_H_ref,
- (pinhole_i + eps,
- pinhole_ref + eps,
- ),
- raise_exception=True)
- self.assertTrue(res)
-
-
-if __name__ == '__main__':
- unittest.main()
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_size", [1, 2, 5, 6])
+def test_pinhole_matrix(batch_size, device_type):
+ # generate input data
+ image_height, image_width = 32., 32.
+ cx, cy = image_width / 2, image_height / 2
+ fx, fy = 1., 1.
+ rx, ry, rz = 0., 0., 0.
+ tx, ty, tz = 0., 0., 0.
+ offset_x = 10. # we will apply a 10units offset to `i` camera
+ eps = 1e-6
+
+ pinhole = utils.create_pinhole(
+ fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
+ pinhole = pinhole.repeat(batch_size, 1).to(torch.device(device_type))
+
+ pinhole_matrix = tgm.pinhole_matrix(pinhole)
+
+ ones = torch.ones(batch_size)
+ assert bool((pinhole_matrix[:, 0, 0] == fx * ones).all())
+ assert bool((pinhole_matrix[:, 1, 1] == fy * ones).all())
+ assert bool((pinhole_matrix[:, 0, 2] == cx * ones).all())
+ assert bool((pinhole_matrix[:, 1, 2] == cy * ones).all())
+
+ # functional
+ assert tgm.PinholeMatrix()(pinhole).shape == (batch_size, 4, 4)
+
+ # evaluate function gradient
+ pinhole = utils.tensor_to_gradcheck_var(pinhole) # to var
+ assert gradcheck(tgm.pinhole_matrix, (pinhole,),
+ raise_exception=True)
+
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_size", [1, 2, 5, 6])
+def test_inverse_pinhole_matrix(batch_size, device_type):
+ # generate input data
+ image_height, image_width = 32., 32.
+ cx, cy = image_width / 2, image_height / 2
+ fx, fy = 1., 1.
+ rx, ry, rz = 0., 0., 0.
+ tx, ty, tz = 0., 0., 0.
+ offset_x = 10. # we will apply a 10units offset to `i` camera
+ eps = 1e-6
+
+ pinhole = utils.create_pinhole(
+ fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
+ pinhole = pinhole.repeat(batch_size, 1).to(torch.device(device_type))
+
+ pinhole_matrix = tgm.inverse_pinhole_matrix(pinhole)
+
+ ones = torch.ones(batch_size)
+ assert utils.check_equal_torch(pinhole_matrix[:, 0, 0], (1. / fx) * ones)
+ assert utils.check_equal_torch(pinhole_matrix[:, 1, 1], (1. / fy) * ones)
+ assert utils.check_equal_torch(
+ pinhole_matrix[:, 0, 2], (-1. * cx / fx) * ones)
+ assert utils.check_equal_torch(
+ pinhole_matrix[:, 1, 2], (-1. * cy / fx) * ones)
+
+ # functional
+ assert tgm.InversePinholeMatrix()(pinhole).shape == (batch_size, 4, 4)
+
+ # evaluate function gradient
+ pinhole = utils.tensor_to_gradcheck_var(pinhole) # to var
+ assert gradcheck(tgm.pinhole_matrix, (pinhole,),
+ raise_exception=True)
+
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_size", [1, 2, 5, 6])
+def test_homography_i_H_ref(batch_size, device_type):
+ # generate input data
+ device = torch.device(device_type)
+ image_height, image_width = 32., 32.
+ cx, cy = image_width / 2, image_height / 2
+ fx, fy = 1., 1.
+ rx, ry, rz = 0., 0., 0.
+ tx, ty, tz = 0., 0., 0.
+ offset_x = 10. # we will apply a 10units offset to `i` camera
+ eps = 1e-6
+
+ pinhole_ref = utils.create_pinhole(
+ fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
+ pinhole_ref = pinhole_ref.repeat(batch_size, 1).to(device)
+
+ pinhole_i = utils.create_pinhole(
+ fx,
+ fy,
+ cx,
+ cy,
+ image_height,
+ image_width,
+ rx,
+ ry,
+ rx,
+ tx + offset_x,
+ ty,
+ tz)
+ pinhole_i = pinhole_i.repeat(batch_size, 1).to(device)
+
+ # compute homography from ref to i
+ i_H_ref = tgm.homography_i_H_ref(pinhole_i, pinhole_ref) + eps
+ i_H_ref_inv = torch.inverse(i_H_ref)
+
+ # compute homography from i to ref
+ ref_H_i = tgm.homography_i_H_ref(pinhole_ref, pinhole_i) + eps
+ assert utils.check_equal_torch(i_H_ref_inv, ref_H_i)
+
+ # evaluate function gradient
+ assert gradcheck(tgm.homography_i_H_ref,
+ (utils.tensor_to_gradcheck_var(pinhole_ref) + eps,
+ utils.tensor_to_gradcheck_var(pinhole_i) + eps,),
+ raise_exception=True)
diff --git a/torchgeometry/pinhole.py b/torchgeometry/pinhole.py
index 5d8189026..42c560479 100644
--- a/torchgeometry/pinhole.py
+++ b/torchgeometry/pinhole.py
@@ -21,7 +21,7 @@ __all__ = [
def inverse_pose(pose, eps=1e-6):
- """Inverts a 4x4 pose.
+ r"""Function that inverts a 4x4 pose.
Args:
points (Tensor): tensor with poses.
@@ -55,8 +55,8 @@ def inverse_pose(pose, eps=1e-6):
return pose_inv
-def pinhole_matrix(pinholes):
- """Returns the pinhole matrix from a pinhole model
+def pinhole_matrix(pinholes, eps=1e-6):
+ r"""Function that returns the pinhole matrix from a pinhole model
Args:
pinholes (Tensor): tensor of pinhole models.
@@ -76,18 +76,18 @@ def pinhole_matrix(pinholes):
# unpack pinhole values
fx, fy, cx, cy = torch.chunk(pinholes[..., :4], 4, dim=1) # Nx1
# create output container
- k = torch.eye(4, device=pinholes.device, dtype=pinholes.dtype)
+ k = torch.eye(4, device=pinholes.device, dtype=pinholes.dtype) + eps
k = k.view(1, 4, 4).repeat(pinholes.shape[0], 1, 1) # Nx4x4
# fill output with pinhole values
- k[..., 0, 0] = fx
- k[..., 0, 2] = cx
- k[..., 1, 1] = fy
- k[..., 1, 2] = cy
+ k[..., 0, 0:1] = fx
+ k[..., 0, 2:3] = cx
+ k[..., 1, 1:2] = fy
+ k[..., 1, 2:3] = cy
return k
def inverse_pinhole_matrix(pinhole, eps=1e-6):
- """Returns the inverted pinhole matrix from a pinhole model
+ r"""Returns the inverted pinhole matrix from a pinhole model
Args:
pinholes (Tensor): tensor with pinhole models.
@@ -110,15 +110,15 @@ def inverse_pinhole_matrix(pinhole, eps=1e-6):
k = torch.eye(4, device=pinhole.device, dtype=pinhole.dtype)
k = k.view(1, 4, 4).repeat(pinhole.shape[0], 1, 1) # Nx4x4
# fill output with inverse values
- k[..., 0, 0] = 1. / (fx + eps)
- k[..., 1, 1] = 1. / (fy + eps)
- k[..., 0, 2] = -1. * cx / (fx + eps)
- k[..., 1, 2] = -1. * cy / (fy + eps)
+ k[..., 0, 0:1] = 1. / (fx + eps)
+ k[..., 1, 1:2] = 1. / (fy + eps)
+ k[..., 0, 2:3] = -1. * cx / (fx + eps)
+ k[..., 1, 2:3] = -1. * cy / (fy + eps)
return k
def scale_pinhole(pinholes, scale):
- """Scales the pinhole matrix for each pinhole model.
+ r"""Scales the pinhole matrix for each pinhole model.
Args:
pinholes (Tensor): tensor with the pinhole model.
@@ -160,7 +160,7 @@ def get_optical_pose_base(pinholes):
def homography_i_H_ref(pinhole_i, pinhole_ref):
- """Homography from reference to ith pinhole
+ r"""Homography from reference to ith pinhole
.. math::
@@ -198,6 +198,23 @@ def homography_i_H_ref(pinhole_i, pinhole_ref):
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__()
@@ -206,6 +223,23 @@ class InversePose(nn.Module):
class PinholeMatrix(nn.Module):
+ r"""Creates an object that returns the pinhole matrix from a pinhole model
+
+ Args:
+ pinholes (Tensor): tensor of pinhole models.
+
+ Returns:
+ Tensor: tensor of pinhole matrices.
+
+ Shape:
+ - Input: :math:`(N, 12)`
+ - Output: :math:`(N, 4, 4)`
+
+ Example:
+ >>> pinhole = torch.rand(1, 12) # Nx12
+ >>> transform = tgm.PinholeMatrix()
+ >>> pinhole_matrix = transform(pinhole) # Nx4x4
+ """
def __init__(self):
super(PinholeMatrix, self).__init__()
@@ -214,6 +248,23 @@ class PinholeMatrix(nn.Module):
class InversePinholeMatrix(nn.Module):
+ r"""Returns and object that inverts a pinhole matrix from a pinhole model
+
+ Args:
+ pinholes (Tensor): tensor with pinhole models.
+
+ Returns:
+ Tensor: tensor of inverted pinhole matrices.
+
+ Shape:
+ - Input: :math:`(N, 12)`
+ - Output: :math:`(N, 4, 4)`
+
+ Example:
+ >>> pinhole = torch.rand(1, 12) # Nx12
+ >>> transform = tgm.InversePinholeMatrix()
+ >>> pinhole_matrix_inv = transform(pinhole) # Nx4x4
+ """
def __init__(self):
super(InversePinholeMatrix, self).__init__()