π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix float rotation angles in camera factories (#6286)
A Aswanth Raj1de66a69e59e
diff --git a/changelog.d/6286.fixed.md b/changelog.d/6286.fixed.mdnew file mode 100644index 000000000..9bf85e415--- /dev/null+++ b/changelog.d/6286.fixed.md@@ -0,0 +1 @@+Fix camera factories raising `TypeError` when constructing extrinsics from float rotation angles.diff --git a/kornia/geometry/camera/utils.py b/kornia/geometry/camera/utils.pyindex 029fe2d0f..6a9ca5c2f 100644--- a/kornia/geometry/camera/utils.py+++ b/kornia/geometry/camera/utils.py@@ -123,6 +123,9 @@ def create_extrinsics_with_rotation("""extrinsics_batch: list[torch.Tensor] = []for alpha, beta, gamma, tx, ty, tz in zip(alphas, betas, gammas, txs, tys, tzs):+ alpha = torch.as_tensor(alpha, device=device, dtype=dtype)+ beta = torch.as_tensor(beta, device=device, dtype=dtype)+ gamma = torch.as_tensor(gamma, device=device, dtype=dtype)Rx = torch.eye(3, device=device, dtype=dtype)Rx[1, 1] = torch.cos(alpha)Rx[1, 2] = torch.sin(alpha)diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.pyindex ca18ed4a2..4f17814d6 100644--- a/tests/geometry/camera/test_pinhole.py+++ b/tests/geometry/camera/test_pinhole.py@@ -22,10 +22,52 @@ import torchimport korniafrom kornia.geometry.camera.pinhole import PinholeCamerasList+from kornia.geometry.camera.utils import create_extrinsics_with_rotation, create_four_cameras, create_pinhole_camerafrom testing.base import BaseTester+class TestCameraFactories(BaseTester):+ 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+ )+ expected = torch.tensor(+ [+ [[1, 0, 0, 1], [0, 1, 0, 3], [0, 0, 1, 5], [0, 0, 0, 1]],+ [[1, 0, 0, 2], [0, 0, 1, 4], [0, -1, 0, 6], [0, 0, 0, 1]],+ ],+ device=device,+ dtype=dtype,+ )+ self.assert_close(actual, expected)+ assert actual.device == device+ assert actual.dtype == dtype++ @pytest.mark.parametrize("batch_size", [1, 4])+ def test_factory(self, batch_size: int, device: torch.device, dtype: torch.dtype) -> None:+ camera = (+ create_pinhole_camera(480, 640, device, dtype) if batch_size == 1 else create_four_cameras(device, dtype)+ )+ assert camera.batch_size == batch_size+ assert camera.intrinsics.device == device+ assert camera.intrinsics.dtype == dtype+ self.assert_close(+ camera.rotation_matrix @ camera.rotation_matrix.transpose(-1, -2),+ torch.eye(3, device=device, dtype=dtype).expand(batch_size, -1, -1),+ )++ def test_gradcheck(self, device: torch.device) -> None:+ angles = torch.tensor([0.2, -0.3, 0.4], device=device, dtype=torch.float64, requires_grad=True)++ def extrinsics(angles: torch.Tensor) -> torch.Tensor:+ return create_extrinsics_with_rotation(+ [angles[0]], [angles[1]], [angles[2]], [1.0], [2.0], [3.0], device, angles.dtype+ )++ self.gradcheck(extrinsics, (angles,))++def _k44(device, dtype, fx=100.0, fy=100.0, cx=4.0, cy=3.0):"""Build a (1, 4, 4) intrinsics matrix in ``PinholeCamera``'s layout.