Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix TorchScript compilation of depth projections (#4389)

SHILIN KONGee49a52fbd83
diff --git a/changelog.d/4389.fixed.md b/changelog.d/4389.fixed.md
new file mode 100644
index 000000000..cd9fc6adb
--- /dev/null
+++ b/changelog.d/4389.fixed.md
@@ -0,0 +1,2 @@
+Fixed TorchScript compilation of `unproject_meshgrid`, `depth_to_3d_v2` and
+`warp_frame_depth` by passing a floating-point norm order.
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.py
index f41ad4400..cedc0159a 100644
--- a/kornia/geometry/depth.py
+++ b/kornia/geometry/depth.py
@@ -112,7 +112,7 @@ def unproject_meshgrid(
points_xyz = convert_points_to_homogeneous(points_xy) # HxWx3
if normalize_points:
- points_xyz = F.normalize(points_xyz, dim=-1, p=2)
+ points_xyz = F.normalize(points_xyz, dim=-1, p=2.0)
return points_xyz
diff --git a/tests/geometry/test_depth.py b/tests/geometry/test_depth.py
index c757953af..25e38c23f 100644
--- a/tests/geometry/test_depth.py
+++ b/tests/geometry/test_depth.py
@@ -123,6 +123,20 @@ class TestDepthTo3d(BaseTester):
out_uncached = kornia.geometry.depth.depth_to_3d_v2(depth, camera_matrix)
self.assert_close(out_cached, out_uncached)
+ @pytest.mark.parametrize("normalize_points", [False, True])
+ def test_jit(self, normalize_points, device, dtype):
+ depth = torch.rand(2, 1, 3, 4, device=device, dtype=dtype).add_(1)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).repeat(2, 1, 1)
+ expected = kornia.geometry.depth.depth_to_3d(depth, camera_matrix, normalize_points).permute(0, 2, 3, 1)
+
+ grid_jit = torch.jit.script(kornia.geometry.unproject_meshgrid)
+ grid = grid_jit(3, 4, camera_matrix, normalize_points, device, dtype)
+ self.assert_close(grid * depth[:, 0, ..., None], expected)
+
+ depth_to_3d_jit = torch.jit.script(kornia.geometry.depth.depth_to_3d_v2)
+ self.assert_close(depth_to_3d_jit(depth[:, 0], camera_matrix, normalize_points), expected)
+ self.assert_close(depth_to_3d_jit(depth[:, 0], camera_matrix, normalize_points, grid), expected)
+
def test_unproject_meshgrid(self, device, dtype):
# TODO: implement me with batch
camera_matrix = torch.eye(3, device=device, dtype=dtype).repeat(2, 1, 1)
@@ -531,6 +545,17 @@ class TestDepthToNormals(BaseTester):
class TestWarpFrameDepth(BaseTester):
+ @pytest.mark.parametrize("normalize_points", [False, True])
+ def test_jit(self, normalize_points, device, dtype):
+ image = torch.rand(2, 3, 3, 4, device=device, dtype=dtype)
+ depth = torch.rand(2, 1, 3, 4, device=device, dtype=dtype).add_(1)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).repeat(2, 1, 1)
+ transform = torch.eye(4, device=device, dtype=dtype).repeat(2, 1, 1)
+ op = kornia.geometry.depth.warp_frame_depth
+ op_jit = torch.jit.script(op)
+ expected = op(image, depth, transform, camera_matrix, normalize_points)
+ self.assert_close(op_jit(image, depth, transform, camera_matrix, normalize_points), expected)
+
def test_smoke(self, device, dtype):
image_src = torch.rand(1, 3, 3, 4, device=device, dtype=dtype)
depth_dst = torch.rand(1, 1, 3, 4, device=device, dtype=dtype)