Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Preserve tensor principal-point gradients in camera construction (#6304)

A Aswanth Rajde3347bddc41
diff --git a/changelog.d/6304.fixed.md b/changelog.d/6304.fixed.md
new file mode 100644
index 000000000..1ed8e7f74
--- /dev/null
+++ b/changelog.d/6304.fixed.md
@@ -0,0 +1 @@
+`create_intrinsics` preserves gradients through tensor principal points instead of detaching them during camera construction.
diff --git a/kornia/geometry/camera/utils.py b/kornia/geometry/camera/utils.py
index 6a9ca5c2f..4af7716a8 100644
--- a/kornia/geometry/camera/utils.py
+++ b/kornia/geometry/camera/utils.py
@@ -71,6 +71,8 @@ def create_intrinsics(
) -> torch.Tensor:
"""Create intrinsic camera matrices from focal lengths and principal points.
+ Tensor principal points retain their gradients in the returned matrices.
+
Args:
fxs: Focal length in x direction
fys: Focal length in y direction
@@ -83,10 +85,7 @@ def create_intrinsics(
Stacked intrinsic matrices of shape (N, 4, 4)
"""
intrinsics_batch: list[torch.Tensor] = []
- # Convert cxs and cys to lists if they are tensors
- cxs_list = cxs.tolist() if isinstance(cxs, torch.Tensor) else cxs
- cys_list = cys.tolist() if isinstance(cys, torch.Tensor) else cys
- for fx, fy, cx, cy in zip(fxs, fys, cxs_list, cys_list):
+ for fx, fy, cx, cy in zip(fxs, fys, cxs, cys):
intrinsics = torch.eye(4, device=device, dtype=dtype)
intrinsics[0, 0] = fx
intrinsics[1, 1] = fy
diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.py
index bf3f38389..21385e113 100644
--- a/tests/geometry/camera/test_pinhole.py
+++ b/tests/geometry/camera/test_pinhole.py
@@ -23,12 +23,43 @@ import torch
import kornia
from kornia.geometry.camera.pinhole import PinholeCamerasList
-from kornia.geometry.camera.utils import create_extrinsics_with_rotation, create_four_cameras, create_pinhole_camera
+from kornia.geometry.camera.utils import (
+ create_extrinsics_with_rotation,
+ create_four_cameras,
+ create_intrinsics,
+ create_pinhole_camera,
+)
from testing.base import BaseTester
class TestCameraFactories(BaseTester):
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ def test_intrinsics_principal_point_gradients(self, batch_size, device, dtype):
+ cx = torch.full((batch_size,), 319.5, device=device, dtype=dtype, requires_grad=True)
+ cy = torch.full((batch_size,), 239.5, device=device, dtype=dtype, requires_grad=True)
+ intrinsics = create_intrinsics([500.0] * batch_size, [600.0] * batch_size, cx, cy, device, dtype)
+ assert intrinsics.requires_grad
+ self.assert_close(intrinsics[:, 0, 2], cx)
+ self.assert_close(intrinsics[:, 1, 2], cy)
+ camera = kornia.geometry.camera.PinholeCamera(
+ intrinsics,
+ torch.eye(4, device=device, dtype=dtype).expand(batch_size, -1, -1),
+ torch.full((batch_size,), 480.0, device=device, dtype=dtype),
+ torch.full((batch_size,), 640.0, device=device, dtype=dtype),
+ )
+ points = torch.tensor([[[1.0, 2.0, 4.0], [2.0, 1.0, 2.0]]], device=device, dtype=dtype)
+ gradients = torch.autograd.grad(camera.project(points.expand(batch_size, -1, -1)).sum(), (cx, cy))
+ for gradient in gradients:
+ self.assert_close(gradient, torch.full_like(cx, 2.0))
+
+ def test_intrinsics_gradcheck(self, device):
+ cx = torch.tensor([319.5, 315.25], device=device, dtype=torch.float64, requires_grad=True)
+ cy = torch.tensor([239.5, 241.5], device=device, dtype=torch.float64, requires_grad=True)
+ self.gradcheck(
+ lambda cx, cy: create_intrinsics([500.0, 600.0], [550.0, 650.0], cx, cy, device, cx.dtype), (cx, cy)
+ )
+
def test_float_angles(self, device: torch.device, dtype: torch.dtype) -> None:
actual = create_extrinsics_with_rotation(
[0.0, math.pi / 2], [0.0, 0.0], [0.0, 0.0], [1.0, 2.0], [3.0, 4.0], [5.0, 6.0], device, dtype