Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

refactor transform_points, update docs and tests

edgarriba383120cf6a16
diff --git a/test/test_conversions.py b/test/test_conversions.py
index 02ed229f2..bad5e63f7 100644
--- a/test/test_conversions.py
+++ b/test/test_conversions.py
@@ -47,7 +47,7 @@ def test_convert_points_from_homogeneous(batch_shape, device_type):
points = tgm.convert_points_from_homogeneous(points_h)
error = utils.compute_mse(points_h[..., :2], points)
- pytest.approx(error.item(), 0.0)
+ assert pytest.approx(error.item(), 0.0)
# functional
assert torch.allclose(points, tgm.ConvertPointsFromHomogeneous()(points_h))
@@ -58,6 +58,40 @@ def test_convert_points_from_homogeneous(batch_shape, device_type):
raise_exception=True)
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_size", [1, 2, 5])
[email protected]("num_points", [2, 3, 5])
[email protected]("num_dims", [2, 3])
+def test_transform_points(batch_size, num_points, num_dims, device_type):
+ # generate input data
+ eye_size = num_dims + 1
+ points_src = torch.rand(batch_size, num_points, num_dims)
+ points_src = points_src.to(torch.device(device_type))
+
+ dst_homo_src = utils.create_random_homography(batch_size, eye_size)
+ dst_homo_src = dst_homo_src.to(torch.device(device_type))
+
+ # transform the points from dst to ref
+ points_dst = tgm.transform_points(dst_homo_src, points_src)
+
+ # transform the points from ref to dst
+ src_homo_dst = torch.inverse(dst_homo_src)
+ points_dst_to_src = tgm.transform_points(src_homo_dst, points_dst)
+
+ # projected should be equal as initial
+ error = utils.compute_mse(points_src, points_dst_to_src)
+ assert pytest.approx(error.item(), 0.0)
+
+ # functional
+ assert torch.allclose(points_dst,
+ tgm.TransformPoints(dst_homo_src)(points_src))
+
+ # evaluate function gradient
+ points_src = utils.tensor_to_gradcheck_var(points_src) # to var
+ dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src) # to var
+ assert gradcheck(tgm.transform_points, (dst_homo_src, points_src,),
+ raise_exception=True)
+
class Tester(unittest.TestCase):
def test_angle_axis_to_rotation_matrix(self):
diff --git a/test/test_functional.py b/test/test_functional.py
index a5fce3b10..17d189b61 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -10,52 +10,6 @@ import utils # test utilities
class Tester(unittest.TestCase):
-
-
- def test_transform_points(self):
- # generate input data
- batch_size = 2
- num_points = 2
- num_dims = 2
- eye_size = 3 # identity 3x3
- points_src = torch.rand(batch_size, 2, num_dims)
- dst_homo_src = utils.create_random_homography(batch_size, eye_size)
-
- # transform the points from dst to ref
- points_dst = tgm.transform_points(dst_homo_src, points_src)
-
- # transform the points from ref to dst
- src_homo_dst = torch.inverse(dst_homo_src)
- points_dst_to_src = tgm.transform_points(src_homo_dst, points_dst)
-
- # projected should be equal as initial
- error = utils.compute_mse(points_src, points_dst_to_src)
- self.assertAlmostEqual(error.item(), 0.0, places=4)
-
- # functional
- self.assertTrue(
- torch.allclose(
- points_dst,
- tgm.TransformPoints()(
- dst_homo_src,
- points_src)))
-
- def test_transform_points_gradcheck(self):
- # generate input data
- batch_size = 2
- num_points = 2
- num_dims = 2
- eye_size = 3 # identity 3x3
- points_src = torch.rand(batch_size, 2, num_dims)
- points_src = utils.tensor_to_gradcheck_var(points_src) # to var
- dst_homo_src = utils.create_random_homography(batch_size, eye_size)
- dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src) # to var
-
- # evaluate function gradient
- res = gradcheck(tgm.transform_points, (dst_homo_src, points_src,),
- raise_exception=True)
- self.assertTrue(res)
-
def test_pi(self):
self.assertAlmostEqual(tgm.pi.item(), 3.141592, places=4)
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.py
index 65740e120..0bbc359fa 100644
--- a/torchgeometry/conversions.py
+++ b/torchgeometry/conversions.py
@@ -118,7 +118,9 @@ def convert_points_to_homogeneous(points):
def transform_points(dst_pose_src, points_src):
- """Applies batch of transformations to batch of sets of points.
+ r"""Function that applies transformations to a set of points.
+
+ See :class:`~torchgeometry.TransformPoints` for details.
Args:
dst_pose_src (Tensor): tensor for transformations.
@@ -131,7 +133,8 @@ def transform_points(dst_pose_src, points_src):
- Input: :math:`(B, D+1, D+1)` and :math:`(B, D, N)`
- Output: :math:`(B, N, D)`
- Example:
+ Examples::
+
>>> input = torch.rand(2, 4, 3) # BxNx3
>>> pose = torch.eye(4).view(1, 4, 4) # Bx4x4
>>> output = tgm.transform_points(pose, input) # BxNx3
@@ -146,9 +149,9 @@ def transform_points(dst_pose_src, points_src):
raise ValueError("Last input dimensions must differe by one unit")
# to homogeneous
points_src_h = convert_points_to_homogeneous(points_src) # BxNxD+1
- points_src_h = torch.unsqueeze(points_src_h, dim=-1)
# transform coordinates
- points_dst_h = torch.matmul(dst_pose_src, points_src_h)
+ points_dst_h = torch.matmul(
+ dst_pose_src.unsqueeze(1), points_src_h.unsqueeze(-1))
points_dst_h = torch.squeeze(points_dst_h, dim=-1)
# to euclidean
points_dst = convert_points_from_homogeneous(points_dst_h) # BxNxD
@@ -505,11 +508,31 @@ class ConvertPointsToHomogeneous(nn.Module):
class TransformPoints(nn.Module):
- def __init__(self):
+ r"""Creates an object to transform a set of points.
+
+ Args:
+ dst_pose_src (Tensor): tensor for transformations of shape :math:`(B, D+1, D+1)`.
+
+ Returns:
+ Tensor: tensor of N-dimensional points.
+
+ Shape:
+ - Input: :math:`(B, D, N)`
+ - Output: :math:`(B, N, D)`
+
+ Examples::
+
+ >>> input = torch.rand(2, 4, 3) # BxNx3
+ >>> transform = torch.eye(4).view(1, 4, 4) # Bx4x4
+ >>> transform_op = tgm.TransformPoints(transform)
+ >>> output = transform_op(input) # BxNx3
+ """
+ def __init__(self, dst_homo_src):
super(TransformPoints, self).__init__()
+ self.dst_homo_src = dst_homo_src
- def forward(self, dst_homo_src, points_src):
- return transform_points(dst_homo_src, points_src)
+ def forward(self, points_src):
+ return transform_points(self.dst_homo_src, points_src)
class AngleAxisToRotationMatrix(nn.Module):