Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): handle z1 unprojection depth by tensor rank (#4355)

Jeevan Mohan Pawarf4532f39c571
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1348416c3..1741406dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -356,6 +356,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* Fixed `unproject_points_z1` depth shape handling for singleton and multi-axis batches,
+ accepting both trailing-singleton and flat depth tensors. (#4355)
+
* `RenderingDeFMO` (used by `DeFMO`) no longer crashes on a half-precision forward pass. Its rendering
time-steps (`times`) were a plain Python attribute, not a registered buffer, so `nn.Module.to()` never
moved it; `forward` re-derived `times`'s device from the input on every call but never its dtype, so
diff --git a/kornia/geometry/camera/projection_z1.py b/kornia/geometry/camera/projection_z1.py
index 06c0ad377..3df297dfb 100644
--- a/kornia/geometry/camera/projection_z1.py
+++ b/kornia/geometry/camera/projection_z1.py
@@ -68,7 +68,8 @@ def unproject_points_z1(
Args:
points_in_cam_canonical: torch.Tensor representing the points to unproject with shape (..., 2).
- extension: torch.Tensor representing the extension (depth) of the points to unproject with shape (..., 1).
+ extension: torch.Tensor representing the extension (depth) of the points to unproject with shape
+ (..., 1) or (...), matching the points' leading dimensions. Defaults to unit depth.
Returns:
torch.Tensor representing the unprojected points with shape (..., 3).
@@ -88,7 +89,7 @@ def unproject_points_z1(
device=points_in_cam_canonical.device,
dtype=points_in_cam_canonical.dtype,
) # (..., 1)
- elif extension.shape[0] > 1:
+ elif extension.ndim == points_in_cam_canonical.ndim - 1:
extension = extension[..., None] # (..., 1)
return torch.cat([points_in_cam_canonical * extension, extension], dim=-1)
diff --git a/tests/geometry/camera/test_projections.py b/tests/geometry/camera/test_projections.py
index c47f25146..dbf9245a9 100644
--- a/tests/geometry/camera/test_projections.py
+++ b/tests/geometry/camera/test_projections.py
@@ -100,6 +100,26 @@ class TestProjectionZ1(BaseTester):
expected = torch.tensor([[2.0, 4.0, 2.0], [9.0, 12.0, 3.0]], device=device, dtype=dtype)
self.assert_close(unproject_points_z1(points, extension), expected)
+ @pytest.mark.parametrize("batch_shape", [(), (0,), (1,), (2,), (1, 2), (2, 3)])
+ @pytest.mark.parametrize("column_depth", [False, True])
+ def test_unproject_depth_shapes_4282(self, device, dtype, batch_shape, column_depth):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype).expand(batch_shape + (2,))
+ depth_shape = batch_shape + (1,) if column_depth else batch_shape
+ depth = torch.full(depth_shape, 3.0, device=device, dtype=dtype)
+ expected = torch.tensor([3.0, 6.0, 3.0], device=device, dtype=dtype).expand(batch_shape + (3,))
+ actual = unproject_points_z1(points, depth)
+ self.assert_close(actual, expected)
+ self.assert_close(project_points_z1(actual), points)
+ self.assert_close(torch.jit.script(unproject_points_z1)(points, depth), expected)
+
+ @pytest.mark.parametrize("column_depth", [False, True])
+ def test_unproject_batched_depth_gradcheck(self, device, column_depth):
+ points = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device, dtype=torch.float64)
+ depth = torch.tensor([2.0, 3.0], device=device, dtype=torch.float64)
+ if column_depth:
+ depth = depth.unsqueeze(-1)
+ self.gradcheck(unproject_points_z1, (points, depth))
+
def test_dx_proj_x(self, device, dtype):
points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
expected = torch.tensor(