Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add device query to Pinhole class (#1760)

Yaniv Hollander2ada27ca9346
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.py
index 0d148ccf4..bf3e684a4 100644
--- a/kornia/geometry/camera/pinhole.py
+++ b/kornia/geometry/camera/pinhole.py
@@ -1,9 +1,10 @@
-from typing import Iterable, Optional
+from typing import Iterable, List, Optional
import torch
from kornia.geometry.conversions import convert_points_from_homogeneous, convert_points_to_homogeneous
from kornia.geometry.linalg import inverse_transformation, transform_points
+from kornia.testing import KORNIA_CHECK_SAME_DEVICE
from kornia.utils.helpers import _torch_inverse_cast
@@ -32,6 +33,7 @@ class PinholeCamera:
self._check_valid_params(extrinsics, "extrinsics")
self._check_valid_shape(height, "height")
self._check_valid_shape(width, "width")
+ self._check_consistent_device([intrinsics, extrinsics, height, width])
# set class attributes
self.height: torch.Tensor = height
self.width: torch.Tensor = width
@@ -61,6 +63,20 @@ class PinholeCamera:
)
return True
+ @staticmethod
+ def _check_consistent_device(data_iter: List[torch.Tensor]) -> None:
+ first = data_iter[0]
+ for data in data_iter:
+ KORNIA_CHECK_SAME_DEVICE(data, first)
+
+ def device(self) -> torch.device:
+ r"""Returns the device for camera buffers.
+
+ Returns:
+ Device type
+ """
+ return self._intrinsics.device
+
@property
def intrinsics(self) -> torch.Tensor:
r"""The full 4x4 intrinsics matrix.
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index 6b291a8ef..8a41cbc37 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -333,7 +333,12 @@ def KORNIA_CHECK_IS_GRAY(x: Tensor, msg: Optional[str] = None):
def KORNIA_CHECK_IS_COLOR_OR_GRAY(x: Tensor, msg: Optional[str] = None):
if len(x.shape) < 3 or x.shape[-3] not in [1, 3]:
- raise TypeError(f"Not an color or gray tensor. Got: {type(x)}.\n{msg}")
+ raise TypeError(f"Not a color or gray tensor. Got: {type(x)}.\n{msg}")
+
+
+def KORNIA_CHECK_SAME_DEVICE(x: Tensor, y: Tensor):
+ if x.device != y.device:
+ raise TypeError(f"Not same device for tensors. Got: {x.device} and {y.device}")
def KORNIA_CHECK_DM_DESC(desc1: Tensor, desc2: Tensor, dm: Tensor):
diff --git a/test/geometry/camera/test_pinhole.py b/test/geometry/camera/test_pinhole.py
index 7c7838eea..2e4d67e0c 100644
--- a/test/geometry/camera/test_pinhole.py
+++ b/test/geometry/camera/test_pinhole.py
@@ -459,3 +459,13 @@ class TestPinholeCamera:
point_2d = pinhole.project(point_3d)
point_3d_hat = pinhole.unproject(point_2d, depth)
assert_close(point_3d, point_3d_hat, atol=1e-4, rtol=1e-4)
+
+ def test_pinhole_camera_device(self, device, dtype):
+ batch_size = 5
+ intrinsics = torch.rand((batch_size, 4, 4), device=device, dtype=dtype)
+ extrinsics = torch.rand((batch_size, 4, 4), device=device, dtype=dtype)
+ height = torch.randint(low=5, high=9, size=(batch_size,), device=device)
+ width = torch.randint(low=5, high=9, size=(batch_size,), device=device)
+
+ pinhole = kornia.geometry.camera.PinholeCamera(intrinsics, extrinsics, height, width)
+ assert pinhole.device() == intrinsics.device