π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
refactor test_convertions.py
edgarriba929a8027a2ab
diff --git a/test/test_conversions.py b/test/test_conversions.pyindex 334d0b5aa..d14a53f73 100644--- a/test/test_conversions.py+++ b/test/test_conversions.py@@ -1,6 +1,4 @@-import unittestimport pytest-import numpy as npimport torchimport torchgeometry as tgm@@ -81,7 +79,6 @@ def test_convert_points_to_homogeneous(batch_shape, device_type):assert gradcheck(tgm.convert_points_to_homogeneous, (points,),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),])@@ -105,7 +102,6 @@ def test_convert_points_from_homogeneous(batch_shape, device_type):assert gradcheck(tgm.convert_points_from_homogeneous, (points,),raise_exception=True)-@pytest.mark.parametrize("device_type", TEST_DEVICES)@pytest.mark.parametrize("batch_size", [1, 2, 5])@pytest.mark.parametrize("num_points", [2, 3, 5])@@ -140,73 +136,60 @@ def test_transform_points(batch_size, num_points, num_dims, device_type):assert gradcheck(tgm.transform_points, (dst_homo_src, points_src,),raise_exception=True)-class Tester(unittest.TestCase):-- def test_angle_axis_to_rotation_matrix(self):- # generate input data- batch_size = 2- angle_axis = torch.rand(batch_size, 3)- eye_batch = utils.create_eye_batch(batch_size, 4)-- # apply transform- rotation_matrix = tgm.angle_axis_to_rotation_matrix(angle_axis)-- rotation_matrix_eye = torch.matmul(- rotation_matrix, rotation_matrix.transpose(1, 2))- self.assertTrue(check_equal_torch(rotation_matrix_eye, eye_batch))-- def test_angle_axis_to_rotation_matrix_gradcheck(self):- # generate input data- batch_size = 2- angle_axis = torch.rand(batch_size, 3)- angle_axis = utils.tensor_to_gradcheck_var(angle_axis) # to var-- # apply transform- rotation_matrix = tgm.angle_axis_to_rotation_matrix(angle_axis)-- # evaluate function gradient- res = gradcheck(tgm.angle_axis_to_rotation_matrix, (angle_axis,),- raise_exception=True)- self.assertTrue(res)-- def test_rtvec_to_pose_gradcheck(self):- # generate input data- batch_size = 2- rtvec = torch.rand(batch_size, 6)- rtvec = utils.tensor_to_gradcheck_var(rtvec) # to var-- # evaluate function gradient- res = gradcheck(tgm.rtvec_to_pose, (rtvec,), raise_exception=True)- self.assertTrue(res)-- def test_rotation_matrix_to_angle_axis(self):- rmat_1 = torch.tensor([[-0.30382753, -0.95095137, -0.05814062, 0.],- [-0.71581715, 0.26812278, -0.64476041, 0.],- [0.62872461, -0.15427791, -0.76217038, 0.]])- rvec_1 = torch.tensor([1.50485376, -2.10737739, 0.7214174])-- rmat_2 = torch.tensor([[0.6027768, -0.79275544, -0.09054801, 0.],- [-0.67915707, -0.56931658, 0.46327563, 0.],- [-0.41881476, -0.21775548, -0.88157628, 0.]])- rvec_2 = torch.tensor([-2.44916812, 1.18053411, 0.4085298])- rmat = torch.stack([rmat_2, rmat_1], dim=0)- rvec = torch.stack([rvec_2, rvec_1], dim=0)- self.assertTrue(- check_equal_torch(- tgm.rotation_matrix_to_angle_axis(rmat),- rvec))-- def test_rotation_matrix_to_angle_axis_gradcheck(self):- # generate input data- batch_size = 2- rmat = torch.rand(batch_size, 3, 4)- rmat = utils.tensor_to_gradcheck_var(rmat) # to var-- # evaluate function gradient- res = gradcheck(tgm.rotation_matrix_to_angle_axis,- (rmat,), raise_exception=True)- self.assertTrue(res)---if __name__ == '__main__':- unittest.main()[email protected]("device_type", TEST_DEVICES)[email protected]("batch_size", [1, 2, 5])+def test_angle_axis_to_rotation_matrix(batch_size, device_type):+ # generate input data+ device = torch.device(device_type)+ angle_axis = torch.rand(batch_size, 3).to(device)+ eye_batch = utils.create_eye_batch(batch_size, 4).to(device)++ # apply transform+ rotation_matrix = tgm.angle_axis_to_rotation_matrix(angle_axis)++ rotation_matrix_eye = torch.matmul(+ rotation_matrix, rotation_matrix.transpose(1, 2))+ assert check_equal_torch(rotation_matrix_eye, eye_batch)++ # evaluate function gradient+ angle_axis = utils.tensor_to_gradcheck_var(angle_axis) # to var+ assert gradcheck(tgm.angle_axis_to_rotation_matrix, (angle_axis,),+ raise_exception=True)+[email protected]("device_type", TEST_DEVICES)[email protected]("batch_size", [1, 2, 5])+def test_rtvec_to_pose_gradcheck(batch_size, device_type):+ # generate input data+ rtvec = torch.rand(batch_size, 6).to(torch.device(device_type))++ # evaluate function gradient+ rtvec = utils.tensor_to_gradcheck_var(rtvec) # to var+ assert gradcheck(tgm.rtvec_to_pose, (rtvec,), raise_exception=True)+[email protected]("device_type", TEST_DEVICES)[email protected]("batch_size", [1, 2, 5])+def test_rotation_matrix_to_angle_axis_gradcheck(batch_size, device_type):+ # generate input data+ rmat = torch.rand(batch_size, 3, 4).to(torch.device(device_type))++ # evaluate function gradient+ rmat = utils.tensor_to_gradcheck_var(rmat) # to var+ assert gradcheck(tgm.rotation_matrix_to_angle_axis,+ (rmat,), raise_exception=True)+[email protected]("device_type", TEST_DEVICES)+def test_rotation_matrix_to_angle_axis(device_type):+ device = torch.device(device_type)+ rmat_1 = torch.tensor([[-0.30382753, -0.95095137, -0.05814062, 0.],+ [-0.71581715, 0.26812278, -0.64476041, 0.],+ [0.62872461, -0.15427791, -0.76217038, 0.]])+ rvec_1 = torch.tensor([1.50485376, -2.10737739, 0.7214174])++ rmat_2 = torch.tensor([[0.6027768, -0.79275544, -0.09054801, 0.],+ [-0.67915707, -0.56931658, 0.46327563, 0.],+ [-0.41881476, -0.21775548, -0.88157628, 0.]])+ rvec_2 = torch.tensor([-2.44916812, 1.18053411, 0.4085298])+ rmat = torch.stack([rmat_2, rmat_1], dim=0).to(device)+ rvec = torch.stack([rvec_2, rvec_1], dim=0).to(device)++ assert check_equal_torch(tgm.rotation_matrix_to_angle_axis(rmat), rvec)