Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix geometry.depth typing (#2563)

Edgar Ribacb2eff6f8702
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.py
index 21356f761..01b5e9885 100644
--- a/kornia/geometry/depth.py
+++ b/kornia/geometry/depth.py
@@ -26,7 +26,12 @@ __all__ = [
def unproject_meshgrid(
- height: int, width: int, camera_matrix: Tensor, normalize_points: bool = False, device=None, dtype=None
+ height: int,
+ width: int,
+ camera_matrix: Tensor,
+ normalize_points: bool = False,
+ device: torch.device | None = None,
+ dtype: torch.dtype | None = None,
) -> Tensor:
"""Compute a 3d point per pixel given its depth value and the camera intrinsics.
@@ -90,10 +95,10 @@ def depth_to_3d_v2(
KORNIA_CHECK_SHAPE(camera_matrix, ["*", "3", "3"])
# create base grid if not provided
- points_xyz = xyz_grid
- if xyz_grid is None:
- height, width = depth.shape[-2:]
- points_xyz = unproject_meshgrid(height, width, camera_matrix, normalize_points, depth.device, depth.dtype)
+ height, width = depth.shape[-2:]
+ points_xyz: Tensor = xyz_grid or unproject_meshgrid(
+ height, width, camera_matrix, normalize_points, depth.device, depth.dtype
+ )
KORNIA_CHECK_SHAPE(points_xyz, ["*", "H", "W", "3"])
diff --git a/kornia/geometry/transform/thin_plate_spline.py b/kornia/geometry/transform/thin_plate_spline.py
index 2dc853481..b644ebefe 100644
--- a/kornia/geometry/transform/thin_plate_spline.py
+++ b/kornia/geometry/transform/thin_plate_spline.py
@@ -1,4 +1,4 @@
-from typing import Tuple
+from __future__ import annotations
import torch
from torch import nn
@@ -34,7 +34,7 @@ def _kernel_distance(squared_distances: torch.Tensor, eps: float = 1e-8) -> torc
return 0.5 * squared_distances * squared_distances.add(eps).log()
-def get_tps_transform(points_src: torch.Tensor, points_dst: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
+def get_tps_transform(points_src: torch.Tensor, points_dst: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
r"""Compute the TPS transform parameters that warp source points to target points.
The input to this function is a tensor of :math:`(x, y)` source points :math:`(B, N, 2)` and a corresponding
@@ -224,9 +224,8 @@ def warp_image_tps(
if not len(affine_weights.shape) == 3:
raise ValueError(f"Invalid shape for affine_weights, expected BxNx2. Got {affine_weights.shape}")
- device, dtype = image.device, image.dtype
batch_size, _, h, w = image.shape
- coords: torch.Tensor = create_meshgrid(h, w, device=device).to(dtype=dtype)
+ coords: torch.Tensor = create_meshgrid(h, w, device=image.device, dtype=image.dtype)
coords = coords.reshape(-1, 2).expand(batch_size, -1, -1)
warped: torch.Tensor = warp_points_tps(coords, kernel_centers, kernel_weights, affine_weights)
warped = warped.view(-1, h, w, 2)
diff --git a/kornia/utils/grid.py b/kornia/utils/grid.py
index b87d48aef..ca4235b4a 100644
--- a/kornia/utils/grid.py
+++ b/kornia/utils/grid.py
@@ -10,8 +10,8 @@ def create_meshgrid(
height: int,
width: int,
normalized_coordinates: bool = True,
- device: Optional[torch.device] = torch.device('cpu'),
- dtype: torch.dtype = torch.float32,
+ device: Optional[torch.device] = None,
+ dtype: Optional[torch.dtype] = None,
) -> Tensor:
"""Generate a coordinate grid for an image.
@@ -74,8 +74,8 @@ def create_meshgrid3d(
height: int,
width: int,
normalized_coordinates: bool = True,
- device: Optional[torch.device] = torch.device('cpu'),
- dtype: torch.dtype = torch.float32,
+ device: Optional[torch.device] = None,
+ dtype: Optional[torch.dtype] = None,
) -> Tensor:
"""Generate a coordinate grid for an image.