π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
update rad2deg, deg2rad test and docs
edgarriba20018a245483
diff --git a/test/test_conversions.py b/test/test_conversions.pyindex bad5e63f7..334d0b5aa 100644--- a/test/test_conversions.py+++ b/test/test_conversions.py@@ -11,6 +11,54 @@ from utils import check_equal_torch, check_equal_numpyfrom common import TEST_DEVICES+def test_pi():+ assert pytest.approx(tgm.pi.item(), 3.141592)+[email protected]("device_type", TEST_DEVICES)[email protected]("batch_shape", [+ (2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3),])+def test_rad2deg(batch_shape, device_type):+ # generate input data+ x_rad = tgm.pi * torch.rand(batch_shape)+ x_rad = x_rad.to(torch.device(device_type))++ # convert radians/degrees+ x_deg = tgm.rad2deg(x_rad)+ x_deg_to_rad = tgm.deg2rad(x_deg)++ # compute error+ error = utils.compute_mse(x_rad, x_deg_to_rad)+ assert pytest.approx(error.item(), 0.0)++ # functional+ assert torch.allclose(x_deg, tgm.RadToDeg()(x_rad))++ # evaluate function gradient+ assert gradcheck(tgm.rad2deg, (utils.tensor_to_gradcheck_var(x_rad),),+ 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_deg2rad(batch_shape, device_type):+ # generate input data+ x_deg = 180. * torch.rand(batch_shape)+ x_deg = x_deg.to(torch.device(device_type))++ # convert radians/degrees+ x_rad = tgm.deg2rad(x_deg)+ x_rad_to_deg = tgm.rad2deg(x_rad)++ # compute error+ error = utils.compute_mse(x_deg, x_rad_to_deg)+ assert pytest.approx(error.item(), 0.0)++ # functional+ assert torch.allclose(x_rad, tgm.DegToRad()(x_deg))++ assert gradcheck(tgm.deg2rad, (utils.tensor_to_gradcheck_var(x_deg),),+ raise_exception=True)+@pytest.mark.parametrize("device_type", TEST_DEVICES)@pytest.mark.parametrize("batch_shape", [(2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3),])diff --git a/test/test_functional.py b/test/test_functional.pyindex 17d189b61..39bb300a3 100644--- a/test/test_functional.py+++ b/test/test_functional.py@@ -10,57 +10,6 @@ import utils # test utilitiesclass Tester(unittest.TestCase):- def test_pi(self):- self.assertAlmostEqual(tgm.pi.item(), 3.141592, places=4)-- def test_rad2deg(self):- # generate input data- x_rad = tgm.pi * torch.rand(2, 3, 4)-- # convert radians/degrees- x_deg = tgm.rad2deg(x_rad)- x_deg_to_rad = tgm.deg2rad(x_deg)-- # compute error- error = utils.compute_mse(x_rad, x_deg_to_rad)- self.assertAlmostEqual(error.item(), 0.0, places=4)-- # functional- self.assertTrue(torch.allclose(x_deg, tgm.RadToDeg()(x_rad)))-- def test_rad2deg_gradcheck(self):- # generate input data- x_rad = tgm.pi * torch.rand(2, 3, 4)-- # evaluate function gradient- res = gradcheck(tgm.rad2deg, (utils.tensor_to_gradcheck_var(x_rad),),- raise_exception=True)- self.assertTrue(res)-- def test_deg2rad(self):- # generate input data- x_deg = 180. * torch.rand(2, 3, 4)-- # convert radians/degrees- x_rad = tgm.deg2rad(x_deg)- x_rad_to_deg = tgm.rad2deg(x_rad)-- # compute error- error = utils.compute_mse(x_deg, x_rad_to_deg)- self.assertAlmostEqual(error.item(), 0.0, places=4)-- # functional- self.assertTrue(torch.allclose(x_rad, tgm.DegToRad()(x_deg)))-- def test_deg2rad_gradcheck(self):- # generate input data- x_deg = 180. * torch.rand(2, 3, 4)-- # evaluate function gradient- res = gradcheck(tgm.deg2rad, (utils.tensor_to_gradcheck_var(x_deg),),- raise_exception=True)- self.assertTrue(res)-def test_inverse_pose(self):# generate input databatch_size = 1diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.pyindex 0bbc359fa..abc393505 100644--- a/torchgeometry/conversions.py+++ b/torchgeometry/conversions.py@@ -35,13 +35,15 @@ pi = torch.Tensor([3.141592653589793])def rad2deg(tensor):- """Converts angles from radians to degrees.+ r"""Function that converts angles from radians to degrees.++ See :class:`~torchgeometry.RadToDeg` for details.Args:- tensor (Tensor): Tensor to be converted of unspecified shape.+ tensor (Tensor): Tensor of arbitrary shape.Returns:- Tensor: Converted tensor with same shape as input.+ Tensor: Tensor with same shape as input.Example:>>> input = tgm.pi * torch.rand(1, 3, 3)@@ -55,15 +57,18 @@ def rad2deg(tensor):def deg2rad(tensor):- """Converts angles from degrees to radians.+ r"""Function that converts angles from degrees to radians.++ See :class:`~torchgeometry.DegToRad` for details.Args:- tensor (Tensor): Tensor to be converted of unspecified shape.+ tensor (Tensor): Tensor of arbitrary shape.Returns:- Tensor: Converted tensor with same shape as input.+ Tensor: Tensor with same shape as input.++ Examples::- Example:>>> input = 360. * torch.rand(1, 3, 3)>>> output = tgm.deg2rad(input)"""@@ -438,6 +443,19 @@ def quaternion_to_angle_axis(quaternion, eps=1e-6):class RadToDeg(nn.Module):+ r"""Creates an object that converts angles from radians to degrees.++ Args:+ tensor (Tensor): Tensor of arbitrary shape.++ Returns:+ Tensor: Tensor with same shape as input.++ Examples::++ >>> input = tgm.pi * torch.rand(1, 3, 3)+ >>> output = tgm.RadToDeg()(input)+ """def __init__(self):super(RadToDeg, self).__init__()@@ -446,6 +464,19 @@ class RadToDeg(nn.Module):class DegToRad(nn.Module):+ r"""Function that converts angles from degrees to radians.++ Args:+ tensor (Tensor): Tensor of arbitrary shape.++ Returns:+ Tensor: Tensor with same shape as input.++ Examples::++ >>> input = 360. * torch.rand(1, 3, 3)+ >>> output = tgm.DegToRad()(input)+ """def __init__(self):super(DegToRad, self).__init__()