Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test(camera): draw project/unproject focal lengths and depth away from zero (#4551)

Dipak Chaudhari38960b71dee6
diff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txt
index fda2fe84b..4d8004fda 100644
--- a/testing/half_precision_xfails/cpu_bfloat16.txt
+++ b/testing/half_precision_xfails/cpu_bfloat16.txt
@@ -484,4 +484,3 @@ call builtins.AssertionError tests/losses/test_total_variation.py::TestTotalVari
call builtins.AssertionError tests/losses/test_total_variation.py::TestTotalVariation::test_tv_on_4d[cpu-bfloat16-pred0-expected0]
call builtins.AssertionError tests/metrics/test_ssim3d.py::TestSSIM3d::test_unit[cpu-bfloat16]
call builtins.AssertionError tests/models/test_rt_detr.py::test_regvgg_optimize_for_deployment[cpu-bfloat16]
-call builtins.AssertionError tests/sensors/camera/test_camera_model.py::TestPinholeCamera::test_project_unproject[cpu-bfloat16-2]
diff --git a/tests/sensors/camera/test_camera_model.py b/tests/sensors/camera/test_camera_model.py
index 754b786ce..2cece5dce 100644
--- a/tests/sensors/camera/test_camera_model.py
+++ b/tests/sensors/camera/test_camera_model.py
@@ -30,7 +30,7 @@ from kornia.sensors.camera.distortion_model import (
)
from kornia.sensors.camera.projection_model import OrthographicProjection, Z1Projection
-from testing.base import BaseTester
+from testing.base import _DTYPE_PRECISIONS, BaseTester
# Asymmetric fixture shared by the convention/wart pins below: fx = 100 != fy = 50 and cx = 4 != cy = 3 on a
# non-square 6 x 8 image, so a transposed or swapped reading of the parameter vector changes the literals.
@@ -94,11 +94,21 @@ class TestPinholeCamera(BaseTester):
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_project_unproject(self, device, dtype, batch_size):
params, image_size = self._make_rand_data(batch_size, device, dtype)
+ # unproject divides by fx / fy and scales by z, so a focal length or a depth drawn near
+ # zero makes the half-precision round trip miss by far more than the dtype tolerance.
+ # Keep both in [1, 2) (#4399).
+ params[:, :2] += 1.0
cam = CameraModel(image_size, CameraModelType.PINHOLE, params)
points = torch.rand((batch_size, 3), device=device, dtype=dtype)
+ points[..., 2] += 1.0
projected = cam.project(Vector3(points))
unprojected = cam.unproject(projected, points[..., 2])
- self.assert_close(points, unprojected.data)
+ # Even there the round trip is not exact: u = fx * x / z + cx lands in [1, 4), where it is
+ # rounded to one ULP, and unproject scales that rounding by z / fx < 2. The miss is bounded
+ # by ULP(u) * z / fx, which reaches 2 * eps, while the half-precision default atol is about
+ # 1 eps. Floor atol at 4 * eps so the verdict does not depend on the draw.
+ rtol, atol = _DTYPE_PRECISIONS[dtype]
+ self.assert_close(points, unprojected.data, rtol=rtol, atol=max(atol, 4 * torch.finfo(dtype).eps))
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_matrix(self, device, dtype, batch_size):