π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
expose device to meshgrid
Dmytro Mishkin22f9e370dbfc
diff --git a/kornia/geometry/spatial_soft_argmax.py b/kornia/geometry/spatial_soft_argmax.pyindex b022e95ac..1834c0eab 100644--- a/kornia/geometry/spatial_soft_argmax.py+++ b/kornia/geometry/spatial_soft_argmax.py@@ -278,8 +278,9 @@ def conv_soft_argmax2d(input: torch.Tensor,# We need to output also coordinates# Pooled window center coordinates- grid_global: torch.Tensor = create_meshgrid(h, w, False).permute(0, 3, 1, 2)- grid_global = grid_global.to(input.device).to(input.dtype)+ grid_global: torch.Tensor = create_meshgrid(h, w, False,+ device=input.device).permute(0, 3, 1, 2)+ grid_global = grid_global.to(input.dtype)grid_global_pooled = F.conv2d(grid_global,center_kernel.to(input.device).to(input.dtype),stride=stride,@@ -400,8 +401,9 @@ def conv_soft_argmax3d(input: torch.Tensor,# We need to output also coordinates# Pooled window center coordinates- grid_global: torch.Tensor = create_meshgrid3d(d, h, w, False).permute(0, 4, 1, 2, 3)- grid_global = grid_global.to(input.device).to(input.dtype)+ grid_global: torch.Tensor = create_meshgrid3d(d, h, w, False,+ device=input.device).permute(0, 4, 1, 2, 3)+ grid_global = grid_global.to(input.dtype)grid_global_pooled = F.conv3d(grid_global,center_kernel.to(input.device).to(input.dtype),stride=stride,diff --git a/kornia/utils/grid.py b/kornia/utils/grid.pyindex f16fbffb5..1a2901714 100644--- a/kornia/utils/grid.py+++ b/kornia/utils/grid.py@@ -6,7 +6,8 @@ import torchdef create_meshgrid(height: int,width: int,- normalized_coordinates: Optional[bool] = True) -> torch.Tensor:+ normalized_coordinates: Optional[bool] = True,+ device: Optional[torch.device] = torch.device('cpu')) -> torch.Tensor:"""Generates a coordinate grid for an image.When the flag `normalized_coordinates` is set to True, the grid is@@ -28,11 +29,11 @@ def create_meshgrid(xs: Optional[torch.Tensor] = Noneys: Optional[torch.Tensor] = Noneif normalized_coordinates:- xs = torch.linspace(-1, 1, width)- ys = torch.linspace(-1, 1, height)+ xs = torch.linspace(-1, 1, width, device=device)+ ys = torch.linspace(-1, 1, height, device=device)else:- xs = torch.linspace(0, width - 1, width)- ys = torch.linspace(0, height - 1, height)+ xs = torch.linspace(0, width - 1, width, device=device)+ ys = torch.linspace(0, height - 1, height, device=device)# generate grid by stacking coordinatesbase_grid: torch.Tensor = torch.stack(torch.meshgrid([xs, ys])).transpose(1, 2) # 2xHxW@@ -43,7 +44,8 @@ def create_meshgrid3d(depth: int,height: int,width: int,- normalized_coordinates: Optional[bool] = True) -> torch.Tensor:+ normalized_coordinates: Optional[bool] = True,+ device: Optional[torch.device] = torch.device('cpu')) -> torch.Tensor:"""Generates a coordinate grid for an image.When the flag `normalized_coordinates` is set to True, the grid is@@ -66,13 +68,13 @@ def create_meshgrid3d(ys: Optional[torch.Tensor] = Nonezs: Optional[torch.Tensor] = Noneif normalized_coordinates:- xs = torch.linspace(-1, 1, width)- ys = torch.linspace(-1, 1, height)- zs = torch.linspace(-1, 1, depth)+ xs = torch.linspace(-1, 1, width, device=device)+ ys = torch.linspace(-1, 1, height, device=device)+ zs = torch.linspace(-1, 1, depth, device=device)else:- xs = torch.linspace(0, width - 1, width)- ys = torch.linspace(0, height - 1, height)- zs = torch.linspace(0, depth - 1, depth)+ xs = torch.linspace(0, width - 1, width, device=device)+ ys = torch.linspace(0, height - 1, height, device=device)+ zs = torch.linspace(0, depth - 1, depth, device=device)# generate grid by stacking coordinatesbase_grid: torch.Tensor = torch.stack(torch.meshgrid([zs, xs, ys])).transpose(1, 2) # 3xHxW