Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix bug in create_meshgrid3d along depth (#1330)

shiyangc-intusurgd959f2995921
diff --git a/kornia/augmentation/augmentation3d.py b/kornia/augmentation/augmentation3d.py
index f07d83815..01738cd84 100644
--- a/kornia/augmentation/augmentation3d.py
+++ b/kornia/augmentation/augmentation3d.py
@@ -942,16 +942,16 @@ class RandomPerspective3D(AugmentationBase3D):
... ]]])
>>> aug = RandomPerspective3D(0.5, p=1., align_corners=True)
>>> aug(inputs)
- tensor([[[[[0.1348, 0.2359, 0.0363],
- [0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000]],
- <BLANKLINE>
- [[0.3976, 0.5507, 0.0000],
+ tensor([[[[[0.3976, 0.5507, 0.0000],
[0.0901, 0.3668, 0.0000],
[0.0000, 0.0000, 0.0000]],
<BLANKLINE>
[[0.2651, 0.4657, 0.0000],
[0.1390, 0.5174, 0.0000],
+ [0.0000, 0.0000, 0.0000]],
+ <BLANKLINE>
+ [[0.0000, 0.1153, 0.0000],
+ [0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000]]]]])
"""
diff --git a/kornia/utils/grid.py b/kornia/utils/grid.py
index 20a8d3fe9..9b625b518 100644
--- a/kornia/utils/grid.py
+++ b/kornia/utils/grid.py
@@ -57,8 +57,8 @@ 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])).transpose(1, 2) # 2xHxW
- return torch.unsqueeze(base_grid, dim=0).permute(0, 2, 3, 1) # 1xHxWx2
+ base_grid: torch.Tensor = torch.stack(torch.meshgrid([xs, ys]), dim=-1) # WxHx2
+ return base_grid.permute(1, 0, 2).unsqueeze(0) # 1xHxWx2
def create_meshgrid3d(
@@ -95,7 +95,7 @@ def create_meshgrid3d(
if normalized_coordinates:
xs = (xs / (width - 1) - 0.5) * 2
ys = (ys / (height - 1) - 0.5) * 2
- zs = (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])).transpose(1, 2) # 3xHxW
- return base_grid.unsqueeze(0).permute(0, 3, 4, 2, 1) # 1xHxWx3
+ base_grid: torch.Tensor = torch.stack(torch.meshgrid([zs, xs, ys]), dim=-1) # DxWxHx3
+ return base_grid.permute(0, 2, 1, 3).unsqueeze(0) # 1xDxHxWx3