π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
more device exposure
Dmytro Mishkind5f7ae1e2fb9
diff --git a/kornia/geometry/spatial_soft_argmax.py b/kornia/geometry/spatial_soft_argmax.pyindex 1834c0eab..dad173198 100644--- a/kornia/geometry/spatial_soft_argmax.py+++ b/kornia/geometry/spatial_soft_argmax.py@@ -8,27 +8,31 @@ from kornia.geometry import normalize_pixel_coordinates, normalize_pixel_coordinfrom typing import Tuple, Union-def _get_window_grid_kernel2d(h: int, w: int) -> torch.Tensor:+def _get_window_grid_kernel2d(h: int, w: int,+ device: torch.device = torch.device('cpu')) -> torch.Tensor:'''Helper function, which generates a kernel towith window coordinates, residual to window centerArgs:h (int): kernel heightw (int): kernel width+ device (torch.device): device, on which generateReturns:conv_kernel (torch.Tensor) [2x1xhxw]'''- window_grid2d = create_meshgrid(h, w, False)+ window_grid2d = create_meshgrid(h, w, False, device=device)window_grid2d = normalize_pixel_coordinates(window_grid2d, h, w)conv_kernel = window_grid2d.permute(3, 0, 1, 2)return conv_kernel-def _get_center_kernel2d(h: int, w: int) -> torch.Tensor:+def _get_center_kernel2d(h: int, w: int,+ device: torch.device = torch.device('cpu')) -> torch.Tensor:'''Helper function, which generates a kernel toreturn center coordinates, when applied with F.conv2d to 2d coordinates gridArgs:h (int): kernel heightw (int): kernel width+ device (torch.device): device, on which generateReturns:conv_kernel (torch.Tensor) [2x2xhxw]'''@@ -48,16 +52,18 @@ def _get_center_kernel2d(h: int, w: int) -> torch.Tensor:w_i1 = (w // 2) - 1w_i2 = (w // 2) + 1center_kernel[(0, 1), (0, 1), h_i1: h_i2, w_i1: w_i2] = 1.0 / float(((h_i2 - h_i1) * (w_i2 - w_i1)))- return center_kernel+ return center_kernel.to(device)-def _get_center_kernel3d(d: int, h: int, w: int) -> torch.Tensor:+def _get_center_kernel3d(d: int, h: int, w: int,+ device: torch.device = torch.device('cpu')) -> torch.Tensor:'''Helper function, which generates a kernel toreturn center coordinates, when applied with F.conv2d to 3d coordinates gridArgs:d (int): kernel depthh (int): kernel heightw (int): kernel width+ device (torch.device): device, on which generateReturns:conv_kernel (torch.Tensor) [3x3xdxhxw]'''@@ -83,24 +89,26 @@ def _get_center_kernel3d(d: int, h: int, w: int) -> torch.Tensor:d_i2 = (d // 2) + 1center_num = float((h_i2 - h_i1) * (w_i2 - w_i1) * (d_i2 - d_i1))center_kernel[(0, 1, 2), (0, 1, 2), d_i1: d_i2, h_i1: h_i2, w_i1: w_i2] = 1.0 / center_num- return center_kernel+ return center_kernel.to(device)-def _get_window_grid_kernel3d(d: int, h: int, w: int) -> torch.Tensor:+def _get_window_grid_kernel3d(d: int, h: int, w: int,+ device: torch.device = torch.device('cpu')) -> torch.Tensor:'''Helper function, which generates a kernel to return coordinates,residual to window centerArgs:d (int): kernel depthh (int): kernel heightw (int): kernel width+ device (torch.device): device, on which generateReturns:conv_kernel (torch.Tensor) [3x1xdxhxw]'''- grid2d = create_meshgrid(h, w, True)+ grid2d = create_meshgrid(h, w, True, device=device)if d > 1:- z = torch.linspace(-1, 1, d).view(d, 1, 1, 1)+ z = torch.linspace(-1, 1, d, device=device).view(d, 1, 1, 1)else: # only onr channel with index == 0- z = torch.zeros(1, 1, 1, 1)+ z = torch.zeros(1, 1, 1, 1, device=device)grid3d = torch.cat([z.repeat(1, h, w, 1).contiguous(), grid2d.repeat(d, 1, 1, 1)], dim=3)conv_kernel = grid3d.permute(3, 0, 1, 2).unsqueeze(1)return conv_kernel@@ -252,11 +260,12 @@ def conv_soft_argmax2d(input: torch.Tensor,.format(temperature))b, c, h, w = input.shape+ dev: torch.device = input.deviceinput = input.view(b * c, 1, h, w)- center_kernel = _get_center_kernel2d(kernel_size[0], kernel_size[1])- window_kernel = _get_window_grid_kernel2d(kernel_size[0], kernel_size[1])- window_kernel = window_kernel.to(input.device).to(input.dtype)+ center_kernel = _get_center_kernel2d(kernel_size[0], kernel_size[1], dev)+ window_kernel = _get_window_grid_kernel2d(kernel_size[0], kernel_size[1], dev)+ window_kernel = window_kernel.to(input.dtype)x_exp = (input / temperature).exp()@@ -278,11 +287,10 @@ 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,- device=input.device).permute(0, 3, 1, 2)+ grid_global: torch.Tensor = create_meshgrid(h, w, False, dev).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),+ center_kernel.to(input.dtype),stride=stride,padding=padding)@@ -374,10 +382,11 @@ def conv_soft_argmax3d(input: torch.Tensor,b, c, d, h, w = input.shapeinput = input.view(b * c, 1, d, h, w)+ dev: torch.device = input.device- center_kernel = _get_center_kernel3d(kernel_size[0], kernel_size[1], kernel_size[2])- window_kernel = _get_window_grid_kernel3d(kernel_size[0], kernel_size[1], kernel_size[2])- window_kernel = window_kernel.to(input.device).to(input.dtype)+ center_kernel = _get_center_kernel3d(kernel_size[0], kernel_size[1], kernel_size[2], dev)+ window_kernel = _get_window_grid_kernel3d(kernel_size[0], kernel_size[1], kernel_size[2], dev)+ window_kernel = window_kernel.to(input.dtype)x_exp = (input / temperature).exp()@@ -405,7 +414,7 @@ def conv_soft_argmax3d(input: torch.Tensor,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),+ center_kernel.to(input.dtype),stride=stride,padding=padding)diff --git a/kornia/utils/grid.py b/kornia/utils/grid.pyindex 1a2901714..392a8d80b 100644--- a/kornia/utils/grid.py+++ b/kornia/utils/grid.py@@ -29,11 +29,11 @@ def create_meshgrid(xs: Optional[torch.Tensor] = Noneys: Optional[torch.Tensor] = Noneif normalized_coordinates:- xs = torch.linspace(-1, 1, width, device=device)- ys = torch.linspace(-1, 1, height, device=device)+ xs = torch.linspace(-1, 1, width, device=device, dtype=torch.float)+ ys = torch.linspace(-1, 1, height, device=device, dtype=torch.float)else:- xs = torch.linspace(0, width - 1, width, device=device)- ys = torch.linspace(0, height - 1, height, device=device)+ xs = torch.linspace(0, width - 1, width, device=device, dtype=torch.float)+ ys = torch.linspace(0, height - 1, height, device=device, dtype=torch.float)# generate grid by stacking coordinatesbase_grid: torch.Tensor = torch.stack(torch.meshgrid([xs, ys])).transpose(1, 2) # 2xHxW@@ -68,13 +68,13 @@ def create_meshgrid3d(ys: Optional[torch.Tensor] = Nonezs: Optional[torch.Tensor] = Noneif normalized_coordinates:- xs = torch.linspace(-1, 1, width, device=device)- ys = torch.linspace(-1, 1, height, device=device)- zs = torch.linspace(-1, 1, depth, device=device)+ xs = torch.linspace(-1, 1, width, device=device, dtype=torch.float)+ ys = torch.linspace(-1, 1, height, device=device, dtype=torch.float)+ zs = torch.linspace(-1, 1, depth, device=device, dtype=torch.float)else:- 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)+ xs = torch.linspace(0, width - 1, width, device=device, dtype=torch.float)+ ys = torch.linspace(0, height - 1, height, device=device, dtype=torch.float)+ zs = torch.linspace(0, depth - 1, depth, device=device, dtype=torch.float)# generate grid by stacking coordinatesbase_grid: torch.Tensor = torch.stack(torch.meshgrid([zs, xs, ys])).transpose(1, 2) # 3xHxW