Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

update convert_from_homogeneous test and docs

edgarribae9e7736656ee
diff --git a/test/test_conversions.py b/test/test_conversions.py
index c9cb51739..02ed229f2 100644
--- a/test/test_conversions.py
+++ b/test/test_conversions.py
@@ -34,6 +34,29 @@ def test_convert_points_to_homogeneous(batch_shape, device_type):
raise_exception=True)
[email protected]("device_type", TEST_DEVICES)
[email protected]("batch_shape", [
+ (2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3),])
+def test_convert_points_from_homogeneous(batch_shape, device_type):
+ # generate input data
+ points_h = torch.rand(batch_shape)
+ points_h = points_h.to(torch.device(device_type))
+ points_h[..., -1] = 1.0
+
+ # to euclidean
+ points = tgm.convert_points_from_homogeneous(points_h)
+
+ error = utils.compute_mse(points_h[..., :2], points)
+ pytest.approx(error.item(), 0.0)
+
+ # functional
+ assert torch.allclose(points, tgm.ConvertPointsFromHomogeneous()(points_h))
+
+ # evaluate function gradient
+ points = utils.tensor_to_gradcheck_var(points) # to var
+ assert gradcheck(tgm.convert_points_from_homogeneous, (points,),
+ raise_exception=True)
+
class Tester(unittest.TestCase):
diff --git a/test/test_functional.py b/test/test_functional.py
index 2bd77a0ff..a5fce3b10 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -7,37 +7,6 @@ from torch.autograd import gradcheck
import utils # test utilities
[email protected]("expand", [True, False])
[email protected]("batch_size", [1, 2, 5])
-def test_convert_points_from_homogeneous(batch_size, expand):
- # generate input data
- points_h = torch.rand(2, 3)
- if expand:
- points_h = torch.rand(batch_size, 2, 3)
- points_h[..., -1] = 1.0
-
- # to euclidean
- points = tgm.convert_points_from_homogeneous(points_h)
-
- error = utils.compute_mse(points_h[..., :2], points)
- pytest.approx(error.item(), 0.0)
-
- # functional
- assert torch.allclose(points, tgm.ConvertPointsFromHomogeneous()(points_h))
-
-
[email protected]("expand", [True, False])
[email protected]("batch_size", [1, 2, 5])
-def test_convert_points_from_homogeneous_gradcheck(batch_size, expand):
- # generate input data
- points = torch.rand(2, 3)
- if expand:
- points = torch.rand(batch_size, 2, 3)
- points = utils.tensor_to_gradcheck_var(points) # to var
-
- # evaluate function gradient
- assert gradcheck(tgm.convert_points_from_homogeneous, (points,),
- raise_exception=True)
class Tester(unittest.TestCase):
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.py
index 074ecca04..65740e120 100644
--- a/torchgeometry/conversions.py
+++ b/torchgeometry/conversions.py
@@ -75,25 +75,21 @@ def deg2rad(tensor):
def convert_points_from_homogeneous(points, eps=1e-6):
- """Converts points from homogeneous to Euclidean space.
+ r"""Function that converts points from homogeneous to Euclidean space.
- Args:
- points (Tensor): tensor of N-dimensional points of size (B, D, N).
-
- Returns:
- Tensor: tensor of N-1-dimensional points of size (B, D, N-1).
+ See :class:`~torchgeometry.ConvertPointsFromHomogeneous` for details.
- Shape:
- - Input: :math:`(B, D, N)`
- - Output: :math:`(B, D, N - 1)`
+ Examples::
- Example:
>>> input = torch.rand(2, 4, 3) # BxNx3
>>> output = tgm.convert_points_from_homogeneous(input) # BxNx2
"""
if not torch.is_tensor(points):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(points)))
+ if len(points.shape) < 2:
+ raise ValueError("Input must be at least a 2D tensor. Got {}".format(
+ points.shape))
return points[..., :-1] / (points[..., -1:] + eps)
@@ -455,6 +451,25 @@ class DegToRad(nn.Module):
class ConvertPointsFromHomogeneous(nn.Module):
+ r"""Creates a transformation that converts points from homogeneous to
+ Euclidean space.
+
+ Args:
+ points (Tensor): tensor of N-dimensional points.
+
+ Returns:
+ Tensor: tensor of N-1-dimensional points.
+
+ Shape:
+ - Input: :math:`(B, D, N)` or :math:`(D, N)`
+ - Output: :math:`(B, D, N + 1)` or :math:`(D, N + 1)`
+
+ Examples::
+
+ >>> input = torch.rand(2, 4, 3) # BxNx3
+ >>> transform = tgm.ConvertPointsFromHomogeneous()
+ >>> output = transform(input) # BxNx2
+ """
def __init__(self):
super(ConvertPointsFromHomogeneous, self).__init__()