Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix warning: `meshgrid` need `indexing argument` (#1629)

FavorMylikesd2a87c1f3215
diff --git a/kornia/utils/_compat.py b/kornia/utils/_compat.py
index 4bd3beca1..5d19b65b5 100644
--- a/kornia/utils/_compat.py
+++ b/kornia/utils/_compat.py
@@ -1,4 +1,7 @@
+from typing import TYPE_CHECKING, List, Optional, Tuple
+
import torch
+from torch import Tensor
from packaging import version
@@ -22,7 +25,7 @@ else:
from torch import solve as _solve
# NOTE: in previous versions `torch.solve` accepted arguments in another order.
- def solve(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
+ def solve(A: Tensor, B: Tensor) -> Tensor:
return _solve(B, A).solution
@@ -33,3 +36,24 @@ if version.parse(torch_version()) > version.parse("1.7.1"):
from torch.linalg import qr as linalg_qr # type: ignore
else:
from torch import qr as linalg_qr # type: ignore # noqa: F401
+
+
+# TODO: remove after deprecating < 1.9.1
+if version.parse(torch_version()) > version.parse("1.9.1"):
+
+ if not TYPE_CHECKING:
+
+ def torch_meshgrid(tensors: List[Tensor], indexing: str):
+ return torch.meshgrid(tensors, indexing=indexing)
+
+else:
+
+ if TYPE_CHECKING:
+
+ def torch_meshgrid(tensors: List[Tensor], indexing: Optional[str] = None) -> Tuple[Tensor, ...]:
+ return torch.meshgrid(tensors)
+
+ else:
+
+ def torch_meshgrid(tensors: List[Tensor], indexing: str):
+ return torch.meshgrid(tensors)
diff --git a/kornia/utils/grid.py b/kornia/utils/grid.py
index 9b625b518..2cfb2c101 100644
--- a/kornia/utils/grid.py
+++ b/kornia/utils/grid.py
@@ -1,6 +1,9 @@
from typing import Optional
import torch
+from torch import Tensor, stack
+
+from kornia.utils._compat import torch_meshgrid
def create_meshgrid(
@@ -9,7 +12,7 @@ def create_meshgrid(
normalized_coordinates: bool = True,
device: Optional[torch.device] = torch.device('cpu'),
dtype: torch.dtype = torch.float32,
-) -> torch.Tensor:
+) -> Tensor:
"""Generate a coordinate grid for an image.
When the flag ``normalized_coordinates`` is set to True, the grid is
@@ -43,8 +46,8 @@ def create_meshgrid(
[[0., 1.],
[1., 1.]]]])
"""
- xs: torch.Tensor = torch.linspace(0, width - 1, width, device=device, dtype=dtype)
- ys: torch.Tensor = torch.linspace(0, height - 1, height, device=device, dtype=dtype)
+ xs: Tensor = torch.linspace(0, width - 1, width, device=device, dtype=dtype)
+ ys: Tensor = torch.linspace(0, height - 1, height, device=device, dtype=dtype)
# Fix TracerWarning
# Note: normalize_pixel_coordinates still gots TracerWarning since new width and height
# tensors will be generated.
@@ -57,7 +60,7 @@ def create_meshgrid(
xs = (xs / (width - 1) - 0.5) * 2
ys = (ys / (height - 1) - 0.5) * 2
# generate grid by stacking coordinates
- base_grid: torch.Tensor = torch.stack(torch.meshgrid([xs, ys]), dim=-1) # WxHx2
+ base_grid: Tensor = stack(torch_meshgrid([xs, ys], indexing="ij"), dim=-1) # WxHx2
return base_grid.permute(1, 0, 2).unsqueeze(0) # 1xHxWx2
@@ -68,7 +71,7 @@ def create_meshgrid3d(
normalized_coordinates: bool = True,
device: Optional[torch.device] = torch.device('cpu'),
dtype: torch.dtype = torch.float32,
-) -> torch.Tensor:
+) -> Tensor:
"""Generate a coordinate grid for an image.
When the flag ``normalized_coordinates`` is set to True, the grid is
@@ -88,14 +91,14 @@ def create_meshgrid3d(
Return:
grid tensor with shape :math:`(1, D, H, W, 3)`.
"""
- xs: torch.Tensor = torch.linspace(0, width - 1, width, device=device, dtype=dtype)
- ys: torch.Tensor = torch.linspace(0, height - 1, height, device=device, dtype=dtype)
- zs: torch.Tensor = torch.linspace(0, depth - 1, depth, device=device, dtype=dtype)
+ xs: Tensor = torch.linspace(0, width - 1, width, device=device, dtype=dtype)
+ ys: Tensor = torch.linspace(0, height - 1, height, device=device, dtype=dtype)
+ zs: Tensor = torch.linspace(0, depth - 1, depth, device=device, dtype=dtype)
# Fix TracerWarning
if normalized_coordinates:
xs = (xs / (width - 1) - 0.5) * 2
ys = (ys / (height - 1) - 0.5) * 2
zs = (zs / (depth - 1) - 0.5) * 2
# generate grid by stacking coordinates
- base_grid: torch.Tensor = torch.stack(torch.meshgrid([zs, xs, ys]), dim=-1) # DxWxHx3
+ base_grid = stack(torch_meshgrid([zs, xs, ys], indexing="ij"), dim=-1) # DxWxHx3
return base_grid.permute(0, 2, 1, 3).unsqueeze(0) # 1xDxHxWx3