π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
feat(camera): deprecate (B, N, 4, 4) intrinsics in PinholeCamera (#6331)
Omkar Kabde79465eda86bd
diff --git a/changelog.d/6331.added.md b/changelog.d/6331.added.mdnew file mode 100644index 000000000..b83d88803--- /dev/null+++ b/changelog.d/6331.added.md@@ -0,0 +1 @@+Constructing `PinholeCamera` with `(B, N, 4, 4)` intrinsics or extrinsics is deprecated and emits a `DeprecationWarning`; it will raise in a future release, because `project` and `unproject` never supported that layout. Flatten the cameras into the batch dimension and pass `(B, 4, 4)` tensors.diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.pyindex bd0da4fb6..37169696c 100644--- a/kornia/geometry/camera/pinhole.py+++ b/kornia/geometry/camera/pinhole.py@@ -15,12 +15,13 @@# limitations under the License.#+import warningsfrom typing import Iterable, List, Unionimport torchfrom kornia.core.check import KORNIA_CHECK_SAME_DEVICE-from kornia.core.utils import _torch_inverse_cast+from kornia.core.utils import _torch_inverse_cast, is_compilingfrom kornia.geometry.conversions import convert_points_from_homogeneous, convert_points_to_homogeneousfrom kornia.geometry.linalg import transform_points@@ -56,8 +57,8 @@ class PinholeCamera:.. warning:::meth:`scale` and :meth:`scale_` rescale the principal point as ``cx' = s * cx`` (the half-pixel rule),which disagrees with the integer pixel centres above:- `#4263 <https://github.com/kornia/kornia/issues/4263>`_. With the :math:`(B, N, 4, 4)` storage the- validator admits, :meth:`project` raises on :math:`(B, N, 3)` points:+ `#4263 <https://github.com/kornia/kornia/issues/4263>`_. :math:`(B, N, 4, 4)` storage is+ deprecated and will be rejected in a future release:`#4266 <https://github.com/kornia/kornia/issues/4266>`_. The ``intrinsics`` form is not validated, but:meth:`project`, :meth:`unproject`, :meth:`intrinsics_inverse` and:class:`~kornia.geometry.depth.DepthWarper` read only the top-left :math:`3 \times 3` block, so a@@ -65,9 +66,9 @@ class PinholeCamera:Args:intrinsics: torch.Tensor with shape :math:`(B, 4, 4)`- containing the full 4x4 camera calibration matrix. The shared shape validator also accepts- :math:`(B, N, 4, 4)` for ``PinholeCamerasList``, while :meth:`project` requires- the documented :math:`(B, 4, 4)` layout.+ containing the full 4x4 camera calibration matrix. Pass :math:`(B, 4, 4)`:+ :math:`(B, N, 4, 4)` is deprecated, and :meth:`project` and :meth:`unproject` never supported it.+ ``PinholeCamerasList`` still stores that layout internally.extrinsics: torch.Tensor with shape :math:`(B, 4, 4)`containing the full 4x4 rotation-translation matrix, checked by the same predicate.height: torch.Tensor with shape :math:`(B)` containing the image height.@@ -86,6 +87,17 @@ class PinholeCamera:self._check_valid([intrinsics, extrinsics, height, width])self._check_valid_params(intrinsics, "intrinsics")self._check_valid_params(extrinsics, "extrinsics")+ is_rank4 = intrinsics.dim() == 4 or extrinsics.dim() == 4+ if is_rank4 and not torch.jit.is_scripting() and not torch.jit.is_tracing() and not is_compiling():+ warnings.warn(+ "Passing (B, N, 4, 4) `intrinsics` or `extrinsics` to `PinholeCamera` is deprecated since kornia "+ "0.9.1 and will raise a ValueError in a future release: `project` and `unproject` support only "+ "(B, 4, 4). Flatten the cameras into the batch dimension, for example "+ "`intrinsics.reshape(-1, 4, 4)` and `extrinsics.reshape(-1, 4, 4)` (expanding a (B, 4, 4) "+ "matrix across N first), and repeat `height` and `width` to match.",+ DeprecationWarning,+ stacklevel=2,+ )self._check_valid_shape(height, "height")self._check_valid_shape(width, "width")self._check_consistent_device([intrinsics, extrinsics, height, width])diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.pyindex 4f17814d6..bf3f38389 100644--- a/tests/geometry/camera/test_pinhole.py+++ b/tests/geometry/camera/test_pinhole.py@@ -16,6 +16,7 @@#import math+import warningsimport pytestimport torch@@ -918,9 +919,10 @@ class TestPinholeCamera(BaseTester):def test_pinhole_camera_preserves_rank_four_constructor_support(self, device, dtype):intrinsics = _k44(device, dtype).unsqueeze(1).expand(1, 2, 4, 4).clone()extrinsics = _e44(device, dtype, tx=1.0).unsqueeze(1).expand(1, 2, 4, 4).clone()- camera = kornia.geometry.camera.PinholeCamera(- intrinsics, extrinsics, torch.tensor([6], device=device), torch.tensor([8], device=device)- )+ with pytest.warns(DeprecationWarning, match="deprecated since kornia 0.9.1"):+ camera = kornia.geometry.camera.PinholeCamera(+ intrinsics, extrinsics, torch.tensor([6], device=device), torch.tensor([8], device=device)+ )assert camera.intrinsics.shape == (1, 2, 4, 4)assert camera.extrinsics.shape == (1, 2, 4, 4)@@ -935,8 +937,10 @@ class TestPinholeCamera(BaseTester):assert camera.rotation_matrix.shape == (1, 2, 3, 3)assert camera.translation_vector.shape == (1, 2, 3, 1)assert camera.intrinsics_inverse().shape == (1, 2, 4, 4)- self.assert_close(camera.clone().intrinsics, intrinsics)- self.assert_close(camera.clone().extrinsics, extrinsics)+ with pytest.warns(DeprecationWarning, match="deprecated since kornia 0.9.1"):+ cloned = camera.clone()+ self.assert_close(cloned.intrinsics, intrinsics)+ self.assert_close(cloned.extrinsics, extrinsics)def test_camera_list_preserves_rank_four_parameters(self, device, dtype):first = kornia.geometry.camera.PinholeCamera(@@ -1039,6 +1043,63 @@ class TestPinholeCamera(BaseTester):self.assert_close(inplace.width, expected_widths)self.assert_close(inplace.extrinsics, cameras.extrinsics)+ @staticmethod+ def _rank4_cameras(device, dtype, intrinsics_rank=4, extrinsics_rank=4):+ k = _k44(device, dtype)+ e = _e44(device, dtype)+ k = k[:, None].expand(1, 2, 4, 4) if intrinsics_rank == 4 else k+ e = e[:, None].expand(1, 2, 4, 4) if extrinsics_rank == 4 else e+ return k, e, torch.tensor([6], device=device), torch.tensor([8], device=device)++ @pytest.mark.parametrize("intrinsics_rank,extrinsics_rank", [(4, 3), (3, 4), (4, 4)])+ def test_rank4_parameters_are_deprecated_4266(self, intrinsics_rank, extrinsics_rank, device, dtype):+ args = self._rank4_cameras(device, dtype, intrinsics_rank, extrinsics_rank)+ with pytest.warns(DeprecationWarning, match="deprecated since kornia 0.9.1") as record:+ kornia.geometry.camera.PinholeCamera(*args)+ assert len(record) == 1+ assert record[0].filename == __file__++ def test_rank3_parameters_do_not_warn_4266(self, device, dtype):+ cam_args = (+ _k44(device, dtype),+ _e44(device, dtype),+ torch.tensor([6], device=device),+ torch.tensor([8], device=device),+ )+ with warnings.catch_warnings():+ warnings.simplefilter("error")+ cam = kornia.geometry.camera.PinholeCamera(*cam_args)+ cam.clone()+ cam.scale(0.5)++ def test_cameras_list_does_not_warn_4266(self, device, dtype):+ cam = kornia.geometry.camera.PinholeCamera(+ _k44(device, dtype), _e44(device, dtype), torch.tensor([6], device=device), torch.tensor([8], device=device)+ )+ with warnings.catch_warnings():+ warnings.simplefilter("error")+ PinholeCamerasList([cam, cam])++ def test_rank4_values_are_unchanged_4266(self, device, dtype):+ k, e, h, w = self._rank4_cameras(device, dtype)+ with pytest.warns(DeprecationWarning, match="deprecated since kornia 0.9.1"):+ cam = kornia.geometry.camera.PinholeCamera(k, e, h, w)+ assert cam.fx.shape == (1, 2)+ self.assert_close(cam.fx, torch.full((1, 2), 100.0, device=device, dtype=dtype))+ with pytest.raises(RuntimeError):+ cam.project(torch.ones(1, 5, 3, device=device, dtype=dtype))++ def test_rank4_construction_does_not_warn_under_trace_4266(self, device, dtype):+ k, e, h, w = self._rank4_cameras(device, dtype)++ def build(k, e):+ return kornia.geometry.camera.PinholeCamera(k, e, h, w).intrinsics++ with warnings.catch_warnings(record=True) as record:+ warnings.simplefilter("always")+ torch.jit.trace(build, (k, e), check_trace=False)+ assert not [w for w in record if "deprecated since kornia" in str(w.message)]+def test_project_and_unproject_reject_rank_one_points_4266(self, device, dtype):cam = kornia.geometry.camera.PinholeCamera(_k44(device, dtype),