Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix transform_points docs and move test

edgarribabae24cf80c0c
diff --git a/test/test_conversions.py b/test/test_conversions.py
index 921b6ddaa..d7098d9c7 100644
--- a/test/test_conversions.py
+++ b/test/test_conversions.py
@@ -107,41 +107,6 @@ 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)
-
-
@pytest.mark.parametrize("device_type", TEST_DEVICES)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_angle_axis_to_rotation_matrix(batch_size, device_type):
diff --git a/test/test_transformations.py b/test/test_transformations.py
index 1a9fec6ed..99d4e050f 100644
--- a/test/test_transformations.py
+++ b/test/test_transformations.py
@@ -42,6 +42,46 @@ def euler_angles_to_rotation_matrix(x, y, z):
return torch.matmul(rz, torch.matmul(ry, rx)) # Bx4x4
+class TestTransformPoints:
+
+ @pytest.mark.parametrize("device_type", TEST_DEVICES)
+ @pytest.mark.parametrize("batch_size", [1, 2, 5])
+ @pytest.mark.parametrize("num_points", [2, 3, 5])
+ @pytest.mark.parametrize("num_dims", [2, 3])
+ def test_transform_points(
+ self, 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)
+
+ def test_gradcheck(self):
+ # generate input data
+ batch_size, num_points, num_dims = 2, 3, 2
+ eye_size = num_dims + 1
+ points_src = torch.rand(batch_size, num_points, num_dims)
+ dst_homo_src = utils.create_random_homography(batch_size, eye_size)
+ # 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 TestComposeTransforms:
def test_translation_4x4(self):
diff --git a/torchgeometry/core/conversions.py b/torchgeometry/core/conversions.py
index aa67143a5..4fd464c2b 100644
--- a/torchgeometry/core/conversions.py
+++ b/torchgeometry/core/conversions.py
@@ -71,7 +71,7 @@ def deg2rad(tensor):
return tensor * pi.to(tensor.device).type(tensor.dtype) / 180.
-def convert_points_from_homogeneous(points, eps=1e-6):
+def convert_points_from_homogeneous(points):
r"""Function that converts points from homogeneous to Euclidean space.
See :class:`~torchgeometry.ConvertPointsFromHomogeneous` for details.
@@ -88,7 +88,7 @@ def convert_points_from_homogeneous(points, eps=1e-6):
raise ValueError("Input must be at least a 2D tensor. Got {}".format(
points.shape))
- return points[..., :-1] / (points[..., -1:] + eps)
+ return points[..., :-1] / points[..., -1:]
def convert_points_to_homogeneous(points):
diff --git a/torchgeometry/core/transformations.py b/torchgeometry/core/transformations.py
index 4c43e4d13..c306c3f35 100644
--- a/torchgeometry/core/transformations.py
+++ b/torchgeometry/core/transformations.py
@@ -175,14 +175,14 @@ def relative_transformation(
return trans_12
-def transform_points(dst_trans_src: torch.Tensor,
- points_src: torch.Tensor) -> torch.Tensor:
+def transform_points(trans_01: torch.Tensor,
+ points_1: torch.Tensor) -> torch.Tensor:
r"""Function that applies transformations to a set of points.
Args:
- dst_trans_src (torch.Tensor): tensor for transformations of shape
+ trans_01 (torch.Tensor): tensor for transformations of shape
:math:`(B, D+1, D+1)`.
- points_src (torch.Tensor): tensor of points of shape :math:`(B, N, D)`.
+ points_1 (torch.Tensor): tensor of points of shape :math:`(B, N, D)`.
Returns:
torch.Tensor: tensor of N-dimensional points.
@@ -191,29 +191,27 @@ def transform_points(dst_trans_src: torch.Tensor,
Examples:
- >>> x_src = torch.rand(2, 4, 3) # BxNx3
- >>> dst_trans_src = torch.eye(4).view(1, 4, 4) # Bx4x4
- >>> x_dst = tgm.transform_points(dst_trans_src, x_src) # BxNx3
+ >>> points_1 = torch.rand(2, 4, 3) # BxNx3
+ >>> trans_01 = torch.eye(4).view(1, 4, 4) # Bx4x4
+ >>> points_0 = tgm.transform_points(trans_01, points_1) # BxNx3
"""
- if not torch.is_tensor(dst_trans_src) or not torch.is_tensor(points_src):
+ if not torch.is_tensor(trans_01) or not torch.is_tensor(points_1):
raise TypeError("Input type is not a torch.Tensor")
- if not dst_trans_src.device == points_src.device:
+ if not trans_01.device == points_1.device:
raise TypeError("Tensor must be in the same device")
- if not dst_trans_src.shape[0] == points_src.shape[0]:
+ if not trans_01.shape[0] == points_1.shape[0]:
raise ValueError("Input batch size must be the same for both tensors")
- if not dst_trans_src.shape[-1] == (points_src.shape[-1] + 1):
+ if not trans_01.shape[-1] == (points_1.shape[-1] + 1):
raise ValueError("Last input dimensions must differe by one unit")
# to homogeneous
- points_src_h: torch.Tensor = convert_points_to_homogeneous(
- points_src) # BxNxD+1
+ points_1_h = convert_points_to_homogeneous(points_1) # BxNxD+1
# transform coordinates
- points_dst_h: torch.Tensor = torch.matmul(
- dst_trans_src.unsqueeze(1), points_src_h.unsqueeze(-1))
- points_dst_h = torch.squeeze(points_dst_h, dim=-1)
+ points_0_h = torch.matmul(
+ trans_01.unsqueeze(1), points_1_h.unsqueeze(-1))
+ points_0_h = torch.squeeze(points_0_h, dim=-1)
# to euclidean
- points_dst: torch.Tensor = convert_points_from_homogeneous(
- points_dst_h) # BxNxD
- return points_dst
+ points_0 = convert_points_from_homogeneous(points_0_h) # BxNxD
+ return points_0
# layer api