π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
update test and docs for convert_points_to_homogeneous
edgarribae00b2aaf3ea2
diff --git a/test/test_functional.py b/test/test_functional.pyindex c48b6e5a5..198883567 100644--- a/test/test_functional.py+++ b/test/test_functional.py@@ -6,19 +6,22 @@ import torchgeometry as tgmfrom torch.autograd import gradcheckimport utils # test utilities+from common import TEST_DEVICES[email protected]("expand", [True, False])[email protected]("batch_size", [1, 2, 5])-def test_convert_points_to_homogeneous(batch_size, expand):[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_to_homogeneous(batch_shape, device_type):# generate input data- points = torch.rand(2, 3)- if expand:- points = torch.rand(batch_size, 2, 3)+ points = torch.rand(batch_shape)+ points = points.to(torch.device(device_type))# to homogeneouspoints_h = tgm.convert_points_to_homogeneous(points)- assert (points_h[..., -1:] == torch.ones(batch_size, 2, 1)).all()++ assert points_h.shape[-2] == batch_shape[-2]+ assert (points_h[..., -1] == torch.ones(points_h[..., -1].shape)).all()# functionalassert torch.allclose(points_h, tgm.ConvertPointsToHomogeneous()(points))diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.pyindex cfbbef8c6..074ecca04 100644--- a/torchgeometry/conversions.py+++ b/torchgeometry/conversions.py@@ -99,25 +99,21 @@ def convert_points_from_homogeneous(points, eps=1e-6):def convert_points_to_homogeneous(points):- """Converts points from Euclidean to homogeneous space.+ r"""Function that converts points from Euclidean to homogeneous space.- Args:- points (Tensor): tensor of N-dimensional points of size.+ See :class:`~torchgeometry.ConvertPointsToHomogeneous` for details.- Returns:- Tensor: tensor of N+1-dimensional points.+ Examples::- Shape:- - Input: :math:`(B, D, N)`- - Output: :math:`(B, D, N + 1)`-- Example:>>> input = torch.rand(2, 4, 3) # BxNx3>>> output = tgm.convert_points_to_homogeneous(input) # BxNx4"""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))# create shape for ones tensor: Nx(...)xD-1new_shape = points.shape[:-1] + (points.shape[-1].bit_length() - 1,)@@ -467,6 +463,25 @@ class ConvertPointsFromHomogeneous(nn.Module):class ConvertPointsToHomogeneous(nn.Module):+ r"""Creates a transformation to convert points from Euclidean to+ homogeneous 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.ConvertPointsToHomogeneous()+ >>> output = transform(input) # BxNx4+ """def __init__(self):super(ConvertPointsToHomogeneous, self).__init__()