π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Implement missing jit ops in kornia.geometry.transform (#981)
Edgar Ribabf3e7e2ee10c
diff --git a/kornia/geometry/camera/perspective.py b/kornia/geometry/camera/perspective.pyindex 3ad7339b7..1ace28383 100644--- a/kornia/geometry/camera/perspective.py+++ b/kornia/geometry/camera/perspective.py@@ -23,11 +23,11 @@ def project_points(Returns:torch.Tensor: array of (u, v) cam coordinates with shape :math:`(*, 2)`."""- if not torch.is_tensor(point_3d):+ if not isinstance(point_3d, torch.Tensor):raise TypeError("Input point_3d type is not a torch.Tensor. Got {}".format(type(point_3d)))- if not torch.is_tensor(camera_matrix):+ if not isinstance(camera_matrix, torch.Tensor):raise TypeError("Input camera_matrix type is not a torch.Tensor. Got {}".format(type(camera_matrix)))@@ -88,15 +88,15 @@ def unproject_points(torch.Tensor: tensor of (x, y, z) world coordinates with shape:math:`(*, 3)`."""- if not torch.is_tensor(point_2d):+ if not isinstance(point_2d, torch.Tensor):raise TypeError("Input point_2d type is not a torch.Tensor. Got {}".format(type(point_2d)))- if not torch.is_tensor(depth):+ if not isinstance(depth, torch.Tensor):raise TypeError("Input depth type is not a torch.Tensor. Got {}".format(type(depth)))- if not torch.is_tensor(camera_matrix):+ if not isinstance(camera_matrix, torch.Tensor):raise TypeError("Input camera_matrix type is not a torch.Tensor. Got {}".format(type(camera_matrix)))@@ -137,6 +137,6 @@ def unproject_points(xyz = convert_points_to_homogeneous(xyz)if normalize:- xyz = F.normalize(xyz, dim=-1, p=2)+ xyz = F.normalize(xyz, dim=-1, p=2.)return xyz * depthdiff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex de55a24a2..f987bfbff 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -432,7 +432,7 @@ def normalize_quaternion(quaternion: torch.Tensor,raise ValueError("Input must be a tensor of shape (*, 4). Got {}".format(quaternion.shape))- return F.normalize(quaternion, p=2, dim=-1, eps=eps)+ return F.normalize(quaternion, p=2.0, dim=-1, eps=eps)# based on:diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.pyindex c7e731061..d8c18e044 100644--- a/test/geometry/test_conversions.py+++ b/test/geometry/test_conversions.py@@ -116,15 +116,13 @@ class TestRotationMatrixToQuaternion:assert gradcheck(kornia.rotation_matrix_to_quaternion, (matrix,),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):op = kornia.quaternion_log_to_expop_script = torch.jit.script(op)-quaternion = torch.tensor([0., 0., 1.], device=device, dtype=dtype)actual = op_script(quaternion)expected = op(quaternion)- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(actual, expected)class TestQuaternionToRotationMatrix:@@ -182,16 +180,11 @@ class TestQuaternionToRotationMatrix:assert gradcheck(kornia.quaternion_to_rotation_matrix, (quaternion,),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- @torch.jit.script- def op_script(input):- return kornia.quaternion_to_rotation_matrix(input)-+ op = kornia.geometry.conversions.quaternion_to_rotation_matrix+ op_jit = torch.jit.script(op)quaternion = torch.tensor([0., 0., 1., 0.], device=device, dtype=dtype)- actual = op_script(quaternion)- expected = kornia.quaternion_to_rotation_matrix(quaternion)- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(op(quaternion), op_jit(quaternion))class TestQuaternionLogToExp:@@ -226,15 +219,11 @@ class TestQuaternionLogToExp:assert gradcheck(kornia.quaternion_log_to_exp, (quaternion,),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- op = kornia.quaternion_log_to_exp- op_script = torch.jit.script(op)-+ op = kornia.geometry.conversions.quaternion_log_to_exp+ op_jit = torch.jit.script(op)quaternion = torch.tensor([0., 0., 1.], device=device, dtype=dtype)- actual = op_script(quaternion)- expected = op(quaternion)- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(op(quaternion), op_jit(quaternion))class TestQuaternionExpToLog:@@ -268,15 +257,11 @@ class TestQuaternionExpToLog:assert gradcheck(kornia.quaternion_exp_to_log, (quaternion,),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- op = kornia.quaternion_exp_to_log- op_script = torch.jit.script(op)-+ op = kornia.geometry.conversions.quaternion_exp_to_log+ op_jit = torch.jit.script(op)quaternion = torch.tensor([0., 0., 1., 0.], device=device, dtype=dtype)- actual = op_script(quaternion)- expected = op(quaternion)- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(op(quaternion), op_jit(quaternion))class TestQuaternionToAngleAxis:@@ -464,16 +449,11 @@ class TestConvertPointsToHomogeneous:assert gradcheck(kornia.convert_points_to_homogeneous, (points_h,),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- op = kornia.convert_points_to_homogeneous- op_script = torch.jit.script(op)-+ op = kornia.geometry.conversions.convert_points_to_homogeneous+ op_jit = torch.jit.script(op)points_h = torch.zeros(1, 2, 3, device=device, dtype=dtype)- actual = op_script(points_h)- expected = op(points_h)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(op(points_h), op_jit(points_h))class TestConvertAtoH:@@ -504,16 +484,11 @@ class TestConvertAtoH:assert gradcheck(kornia.convert_affinematrix_to_homography, (points_h,),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- op = kornia.convert_affinematrix_to_homography- op_script = torch.jit.script(op)-+ op = kornia.geometry.conversions.convert_affinematrix_to_homography+ op_jit = torch.jit.script(op)points_h = torch.zeros(1, 2, 3, device=device, dtype=dtype)- actual = op_script(points_h)- expected = op(points_h)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(op(points_h), op_jit(points_h))class TestConvertPointsFromHomogeneous:@@ -591,14 +566,10 @@ class TestConvertPointsFromHomogeneous:raise_exception=True)def test_jit(self, device, dtype):- op = kornia.convert_points_from_homogeneous- op_script = torch.jit.script(op)-+ op = kornia.geometry.conversions.convert_points_from_homogeneous+ op_jit = torch.jit.script(op)points_h = torch.zeros(1, 2, 3, device=device, dtype=dtype)- actual = op_script(points_h)- expected = op(points_h)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(op(points_h), op_jit(points_h))@pytest.mark.parametrize("batch_size", [1, 2, 5])@@ -677,9 +648,8 @@ class TestNormalizePixelCoordinates:assert_allclose(grid_norm, expected, atol=1e-4, rtol=1e-4)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- op = kornia.normalize_pixel_coordinates+ op = kornia.geometry.conversions.normalize_pixel_coordinatesop_script = torch.jit.script(op)height, width = 3, 4@@ -688,8 +658,7 @@ class TestNormalizePixelCoordinates:actual = op_script(grid, height, width)expected = op(grid, height, width)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(actual, expected)class TestDenormalizePixelCoordinates:@@ -721,9 +690,8 @@ class TestDenormalizePixelCoordinates:assert_allclose(grid_norm, expected, atol=1e-4, rtol=1e-4)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- op = kornia.denormalize_pixel_coordinates+ op = kornia.geometry.conversions.denormalize_pixel_coordinatesop_script = torch.jit.script(op)height, width = 3, 4@@ -733,4 +701,4 @@ class TestDenormalizePixelCoordinates:actual = op_script(grid, height, width)expected = op(grid, height, width)- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ assert_allclose(actual, expected)diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.pyindex 6bd0015ce..79ac8f497 100644--- a/test/geometry/test_linalg.py+++ b/test/geometry/test_linalg.py@@ -75,36 +75,18 @@ class TestTransformPoints:# evaluate function gradientpoints_src = utils.tensor_to_gradcheck_var(points_src) # to vardst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src) # to var- assert gradcheck(kornia.transform_points, (dst_homo_src, points_src,),+ assert gradcheck(kornia.geometry.transform_points, (dst_homo_src, points_src,),raise_exception=True)def test_jit(self, device, dtype):- @torch.jit.script- def op_script(transform, points):- return kornia.transform_points(transform, points)-points = torch.ones(1, 2, 2, device=device, dtype=dtype)transform = kornia.eye_like(3, points)- op = kornia.transform_points+ op = kornia.geometry.transform_pointsop_script = torch.jit.script(op)actual = op_script(transform, points)expected = op(transform, points)assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)- @pytest.mark.skip(reason="turn off all jit for a while")- def test_jit_trace(self, device, dtype):- @torch.jit.script- def op_script(transform, points):- return kornia.transform_points(transform, points)-- points = torch.ones(1, 2, 2, device=device, dtype=dtype)- transform = torch.eye(3)[None].to(device)- op_script_trace = torch.jit.trace(op_script, (transform, points,))- actual = op_script_trace(transform, points)- expected = kornia.transform_points(transform, points)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)-class TestTransformBoxes:@@ -186,22 +168,15 @@ class TestTransformBoxes:assert gradcheck(kornia.transform_boxes, (trans_mat, boxes), raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- @torch.jit.script- def op_script(transform, boxes):- return kornia.transform_boxes(transform, boxes)-- boxes = torch.tensor([139.2640, 103.0150, 258.0480, 307.5075], device=device, dtype=dtype)-+ boxes = torch.tensor([[139.2640, 103.0150, 258.0480, 307.5075]], device=device, dtype=dtype)trans_mat = torch.tensor([[[-1., 0., 512.],[0., 1., 0.],[0., 0., 1.]]], device=device, dtype=dtype)-- actual = op_script(trans_mat, boxes)- expected = kornia.transform_points(trans_mat, boxes)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ args = (boxes, trans_mat)+ op = kornia.geometry.transform_points+ op_jit = torch.jit.script(op)+ assert_allclose(op(*args), op_jit(*args))class TestComposeTransforms:diff --git a/test/geometry/test_perspective.py b/test/geometry/test_perspective.pyindex 42b885255..312dcbe55 100644--- a/test/geometry/test_perspective.py+++ b/test/geometry/test_perspective.py@@ -12,19 +12,19 @@ class TestProjectPoints:def test_smoke(self, device, dtype):point_3d = torch.zeros(1, 3, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)- point_2d = kornia.project_points(point_3d, camera_matrix)+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)assert point_2d.shape == (1, 2)def test_smoke_batch(self, device, dtype):point_3d = torch.zeros(2, 3, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)- point_2d = kornia.project_points(point_3d, camera_matrix)+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)assert point_2d.shape == (2, 2)def test_smoke_batch_multi(self, device, dtype):point_3d = torch.zeros(2, 4, 3, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, 4, -1, -1)- point_2d = kornia.project_points(point_3d, camera_matrix)+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)assert point_2d.shape == (2, 4, 2)def test_project_and_unproject(self, device, dtype):@@ -35,8 +35,8 @@ class TestProjectPoints:[0., 2748., 619.],[0., 0., 1.],]], device=device, dtype=dtype)- point_2d = kornia.project_points(point_3d, camera_matrix)- point_3d_hat = kornia.unproject_points(point_2d, depth, camera_matrix)+ point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)+ point_3d_hat = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)assert_allclose(point_3d, point_3d_hat, atol=1e-4, rtol=1e-4)def test_gradcheck(self, device, dtype):@@ -47,22 +47,16 @@ class TestProjectPoints:# evaluate function gradientpoints_3d = tensor_to_gradcheck_var(points_3d)camera_matrix = tensor_to_gradcheck_var(camera_matrix)- assert gradcheck(kornia.project_points,+ assert gradcheck(kornia.geometry.camera.project_points,(points_3d, camera_matrix),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- @torch.jit.script- def op_script(points_3d, camera_matrix):- return kornia.project_points(points_3d, camera_matrix)-points_3d = torch.zeros(1, 3, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)- actual = op_script(points_3d, camera_matrix)- expected = kornia.project_points(points_3d, camera_matrix)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ op = kornia.geometry.camera.project_points+ op_jit = torch.jit.script(op)+ assert_allclose(op(points_3d, camera_matrix), op_jit(points_3d, camera_matrix))class TestUnprojectPoints:@@ -70,21 +64,21 @@ class TestUnprojectPoints:points_2d = torch.zeros(1, 2, device=device, dtype=dtype)depth = torch.ones(1, 1, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)- point_3d = kornia.unproject_points(points_2d, depth, camera_matrix)+ point_3d = kornia.geometry.camera.unproject_points(points_2d, depth, camera_matrix)assert point_3d.shape == (1, 3)def test_smoke_batch(self, device, dtype):points_2d = torch.zeros(2, 2, device=device, dtype=dtype)depth = torch.ones(2, 1, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)- point_3d = kornia.unproject_points(points_2d, depth, camera_matrix)+ point_3d = kornia.geometry.camera.unproject_points(points_2d, depth, camera_matrix)assert point_3d.shape == (2, 3)def test_smoke_multi_batch(self, device, dtype):points_2d = torch.zeros(2, 3, 2, device=device, dtype=dtype)depth = torch.ones(2, 3, 1, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, 3, -1, -1)- point_3d = kornia.unproject_points(points_2d, depth, camera_matrix)+ point_3d = kornia.geometry.camera.unproject_points(points_2d, depth, camera_matrix)assert point_3d.shape == (2, 3, 3)def test_unproject_center(self, device, dtype):@@ -96,7 +90,7 @@ class TestUnprojectPoints:[0., 0., 1.],], device=device, dtype=dtype)expected = torch.tensor([[0., 0., 2.]], device=device, dtype=dtype)- actual = kornia.unproject_points(point_2d, depth, camera_matrix)+ actual = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)def test_unproject_center_normalize(self, device, dtype):@@ -108,7 +102,7 @@ class TestUnprojectPoints:[0., 0., 1.],], device=device, dtype=dtype)expected = torch.tensor([[0., 0., 2.]], device=device, dtype=dtype)- actual = kornia.unproject_points(point_2d, depth, camera_matrix, True)+ actual = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix, True)assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)def test_unproject_and_project(self, device, dtype):@@ -119,8 +113,8 @@ class TestUnprojectPoints:[0., 1., 0.],[0., 0., 1.],], device=device, dtype=dtype)- point_3d = kornia.unproject_points(point_2d, depth, camera_matrix)- point_2d_hat = kornia.project_points(point_3d, camera_matrix)+ point_3d = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)+ point_2d_hat = kornia.geometry.camera.project_points(point_3d, camera_matrix)assert_allclose(point_2d, point_2d_hat, atol=1e-4, rtol=1e-4)def test_gradcheck(self, device, dtype):@@ -132,21 +126,15 @@ class TestUnprojectPoints:points_2d = tensor_to_gradcheck_var(points_2d)depth = tensor_to_gradcheck_var(depth)camera_matrix = tensor_to_gradcheck_var(camera_matrix)- assert gradcheck(kornia.unproject_points,+ assert gradcheck(kornia.geometry.camera.unproject_points,(points_2d, depth, camera_matrix),raise_exception=True)- @pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self, device, dtype):- @torch.jit.script- def op_script(points_2d, depth, camera_matrix):- return kornia.unproject_points(- points_2d, depth, camera_matrix, False)-points_2d = torch.zeros(1, 2, device=device, dtype=dtype)depth = torch.ones(1, 1, device=device, dtype=dtype)camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)- actual = op_script(points_2d, depth, camera_matrix)- expected = kornia.unproject_points(points_2d, depth, camera_matrix)-- assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)+ args = (points_2d, depth, camera_matrix)+ op = kornia.geometry.camera.unproject_points+ op_jit = torch.jit.script(op)+ assert_allclose(op(*args), op_jit(*args))