π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement pi, rad2deg, deg2rad
edgarribaf915d69d03c0
diff --git a/.gitignore b/.gitignoreindex 12d3b0e59..55ffa1a3a 100644--- a/.gitignore+++ b/.gitignore@@ -6,6 +6,7 @@ torchgeometry.egg-info/*/**/*~*~*.swp+*.swo.eggs/*.cache/*.pytest_cache/*diff --git a/test/test_functional.py b/test/test_functional.pyindex 7820e4dc8..1c3eddf81 100644--- a/test/test_functional.py+++ b/test/test_functional.py@@ -24,7 +24,7 @@ def tensor_to_gradcheck_var(tensor):`gradcheck` needs 64-bit floating point and requires gradient."""tensor = tensor.type(torch.DoubleTensor)- return tensor.requires_grad_()+ return tensor.requires_grad_(True)class Tester(unittest.TestCase):@@ -124,6 +124,49 @@ class Tester(unittest.TestCase):res = gradcheck(dgm.transform_points, (dst_homo_src, points_src,),raise_exception=True)+ def test_pi(self):+ self.assertAlmostEqual(dgm.pi.item(), 3.141592, places=4)++ def test_rad2deg(self):+ # generate input data+ x_rad = dgm.pi * torch.rand(2, 3, 4)++ # convert radians/degrees+ x_deg = dgm.rad2deg(x_rad)+ x_deg_to_rad = dgm.deg2rad(x_deg)++ # compute error+ error = torch.sum((x_rad - x_deg_to_rad) ** 2)+ self.assertAlmostEqual(error.item(), 0.0)++ def test_rad2deg_gradcheck(self):+ # generate input data+ x_rad = dgm.pi * torch.rand(2, 3, 4)++ # evaluate function gradient+ res = gradcheck(dgm.rad2deg, (tensor_to_gradcheck_var(x_rad),),+ raise_exception=True)++ def test_deg2rad(self):+ # generate input data+ x_deg = 180. * torch.rand(2, 3, 4)++ # convert radians/degrees+ x_rad = dgm.deg2rad(x_deg)+ x_rad_to_deg = dgm.rad2deg(x_rad)++ # compute error+ error = torch.sum((x_deg - x_rad_to_deg) ** 2)+ self.assertAlmostEqual(error.item(), 0.0)++ def test_deg2rad_gradcheck(self):+ # generate input data+ x_deg = 180. * torch.rand(2, 3, 4)++ # evaluate function gradient+ res = gradcheck(dgm.deg2rad, (tensor_to_gradcheck_var(x_deg),),+ raise_exception=True)+if __name__ == '__main__':unittest.main()diff --git a/torchgeometry/functional.py b/torchgeometry/functional.pyindex d72f2e172..47940b90b 100644--- a/torchgeometry/functional.py+++ b/torchgeometry/functional.py@@ -1,6 +1,41 @@import torch+"""Constant with number pi+"""+pi = torch.Tensor([3.141592653589793])+++def rad2deg(x):+ """Converts angles from radians to degrees.++ Args:+ x (Tensor): tensor of unspecified size.++ Returns: tensor with same size as input.+ """+ if not torch.is_tensor(x):+ raise TypeError("Input type is not a torch.Tensor. Got {}"+ .format(type(x)))++ return 180. * x / pi.to(x.device).type(x.dtype)+++def deg2rad(x):+ """Converts angles from degrees to radians.++ Args:+ x (Tensor): tensor of unspecified size.++ Returns: tensor with same size as input.+ """+ if not torch.is_tensor(x):+ raise TypeError("Input type is not a torch.Tensor. Got {}"+ .format(type(x)))++ return x * pi.to(x.device).type(x.dtype) / 180.++def convert_points_from_homogeneous(points, eps=1e-6):"""Converts points from homogeneous to Euclidean space.