π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(sensors): preserve device and dtype for scalar depth in Z1Projection.unproject (#4340)
li-lizhe1f735028ea5b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8f289acd9..1348416c3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -395,6 +395,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0or `0.30078125` (bfloat16) rather than `0.3` -- the same buffer semantics as `Normalize`.(#4337)+* `Z1Projection.unproject` now materialises a python `int`/`float` `depth` on the device and+ in the dtype of `points`; it used to build a CPU float32 tensor, which raised `RuntimeError`+ on every accelerator and widened float16/bfloat16 results to float32. (#4340)+* `kornia.io.load_image` and `write_image` work on the kornia_rs that a plain `pip install kornia`resolves. kornia_rs 0.1.11 moved its image readers and writers from the package root into`kornia_rs.io`, and kornia kept calling the root, so on 0.1.11 and newer `load_image` raiseddiff --git a/kornia/sensors/camera/projection_model.py b/kornia/sensors/camera/projection_model.pyindex c8f1efaf0..fdf543633 100644--- a/kornia/sensors/camera/projection_model.py+++ b/kornia/sensors/camera/projection_model.py@@ -59,7 +59,7 @@ class Z1Projection:Args:points: Vector2 representing the points to unproject.- depth: torch.Tensor representing the depth of the points to unproject.+ depth: a :class:`torch.Tensor` of shape ``(B,)``, or a python scalar for a single point.Returns:Vector3 representing the unprojected points.@@ -73,7 +73,7 @@ class Z1Projection:"""if isinstance(depth, (float, int)):- depth = torch.Tensor([depth])+ depth = torch.as_tensor([depth], device=points.data.device, dtype=points.data.dtype)return Vector3.from_coords(points.x * depth, points.y * depth, depth)diff --git a/tests/sensors/camera/test_projection_model.py b/tests/sensors/camera/test_projection_model.pyindex 29d6fa417..5843ac862 100644--- a/tests/sensors/camera/test_projection_model.py+++ b/tests/sensors/camera/test_projection_model.py@@ -74,3 +74,25 @@ class TestProjection(BaseTester):).data,expected,)++ def test_unproject_scalar_depth(self, device, dtype):+ """Regression test: scalar depth must preserve device and dtype.++ PR #4340: Z1Projection.unproject used to build a CPU float32 tensor+ for python int/float depth, which raised RuntimeError on every+ accelerator and widened float16/bfloat16 results to float32.+ """+ projection = Z1Projection()+ points = Vector2(torch.tensor([[0.25, 0.5]], device=device, dtype=dtype))+ expected = projection.unproject(+ points,+ torch.tensor([4.0], device=device, dtype=dtype),+ ).data+ for depth in (4, 4.0):+ out = projection.unproject(points, depth)+ assert out.data.device.type == device.type, f"expected device {device.type}, got {out.data.device.type}"+ assert out.data.dtype == dtype, f"expected dtype {dtype}, got {out.data.dtype}"+ self.assert_close(out.data, expected, atol=0.0, rtol=0.0)+ # Also verify a vector depth still works unchanged+ out_vec = projection.unproject(points, torch.tensor([4.0], device=device, dtype=dtype))+ self.assert_close(out_vec.data, expected, atol=0.0, rtol=0.0)