π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Added dtype for create_mesh (#919)
MrParoskd3000ba1d293
diff --git a/kornia/utils/grid.py b/kornia/utils/grid.pyindex 193880d66..eb8a537cf 100644--- a/kornia/utils/grid.py+++ b/kornia/utils/grid.py@@ -8,7 +8,8 @@ def create_meshgrid(height: int,width: int,normalized_coordinates: bool = True,- device: Optional[torch.device] = torch.device('cpu')) -> torch.Tensor:+ device: Optional[torch.device] = torch.device('cpu'),+ dtype: torch.dtype = torch.float32) -> torch.Tensor:"""Generates a coordinate grid for an image.When the flag `normalized_coordinates` is set to True, the grid is@@ -22,12 +23,14 @@ def create_meshgrid(normalized_coordinates (bool): whether to normalizecoordinates in the range [-1, 1] in order to be consistent with thePyTorch function grid_sample.+ device (torch.device): the device on which the grid will be generated. Default: cpu.+ dtype (torch.dtype): the data type of the generated gird. Default: float32.Return:torch.Tensor: returns a grid tensor with shape :math:`(1, H, W, 2)`."""- xs: torch.Tensor = torch.linspace(0, width - 1, width, device=device, dtype=torch.float)- ys: torch.Tensor = torch.linspace(0, height - 1, height, device=device, dtype=torch.float)+ 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)# Fix TracerWarning# Note: normalize_pixel_coordinates still gots TracerWarning since new width and height# tensors will be generated.@@ -50,7 +53,8 @@ def create_meshgrid3d(height: int,width: int,normalized_coordinates: bool = True,- device: Optional[torch.device] = torch.device('cpu')) -> torch.Tensor:+ device: Optional[torch.device] = torch.device('cpu'),+ dtype: torch.dtype = torch.float32) -> torch.Tensor:"""Generates a coordinate grid for an image.When the flag `normalized_coordinates` is set to True, the grid is@@ -65,13 +69,15 @@ def create_meshgrid3d(normalized_coordinates (bool): whether to normalizecoordinates in the range [-1, 1] in order to be consistent with thePyTorch function grid_sample.+ device (torch.device): the device on which the grid will be generated. Default: cpu.+ dtype (torch.dtype): the data type of the generated gird. Default: float32.Return:torch.Tensor: returns a grid tensor with shape :math:`(1, D, H, W, 3)`."""- xs: torch.Tensor = torch.linspace(0, width - 1, width, device=device, dtype=torch.float)- ys: torch.Tensor = torch.linspace(0, height - 1, height, device=device, dtype=torch.float)- zs: torch.Tensor = torch.linspace(0, depth - 1, depth, device=device, dtype=torch.float)+ 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)# Fix TracerWarningif normalized_coordinates:xs = (xs / (width - 1) - 0.5) * 2diff --git a/test/utils/test_grid.py b/test/utils/test_grid.pyindex e0db790ee..85e82acbf 100644--- a/test/utils/test_grid.py+++ b/test/utils/test_grid.py@@ -7,36 +7,54 @@ import kornia.testing as utils # test utilsfrom torch.testing import assert_allclose-def test_create_meshgrid():+def test_create_meshgrid(device, dtype):height, width = 4, 6normalized_coordinates = False# create the meshgrid and verify shapegrid = kornia.utils.create_meshgrid(- height, width, normalized_coordinates)+ height, width, normalized_coordinates,+ device=device, dtype=dtype)++ assert grid.device == device+ assert grid.dtype == dtypeassert grid.shape == (1, height, width, 2)# check grid corner values- assert tuple(grid[0, 0, 0].numpy()) == (0., 0.)+ assert tuple(grid[0, 0, 0].cpu().numpy()) == (0., 0.)assert tuple(- grid[0, height - 1, width - 1].numpy()) == (width - 1, height - 1)+ grid[0, height - 1, width - 1].cpu().numpy()) == (width - 1, height - 1)++def test_normalize_pixel_grid(device, dtype):+ if device.type == 'cuda' and dtype == torch.float16:+ pytest.skip(+ '"inverse_cuda" not implemented for "Half"'+ )-def test_normalize_pixel_grid():# generate input databatch_size = 1height, width = 2, 4# create points gridgrid_norm = kornia.utils.create_meshgrid(- height, width, normalized_coordinates=True)+ height, width, normalized_coordinates=True,+ device=device, dtype=dtype)++ assert grid_norm.device == device+ assert grid_norm.dtype == dtypegrid_norm = torch.unsqueeze(grid_norm, dim=0)+grid_pix = kornia.utils.create_meshgrid(- height, width, normalized_coordinates=False)+ height, width, normalized_coordinates=False,+ device=device, dtype=dtype)++ assert grid_pix.device == device+ assert grid_pix.dtype == dtypegrid_pix = torch.unsqueeze(grid_pix, dim=0)# grid from pixel space to normalized- norm_trans_pix = kornia.normal_transform_pixel(height, width) # 1x3x3+ norm_trans_pix = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) # 1x3x3pix_trans_norm = torch.inverse(norm_trans_pix) # 1x3x3# transform gridsgrid_pix_to_norm = kornia.transform_points(norm_trans_pix, grid_pix)@@ -45,16 +63,20 @@ def test_normalize_pixel_grid():assert_allclose(grid_norm, grid_pix_to_norm)-def test_create_meshgrid3d():+def test_create_meshgrid3d(device, dtype):depth, height, width = 5, 4, 6normalized_coordinates = False# create the meshgrid and verify shapegrid = kornia.utils.create_meshgrid3d(- depth, height, width, normalized_coordinates)+ depth, height, width, normalized_coordinates,+ device=device, dtype=dtype)++ assert grid.device == device+ assert grid.dtype == dtypeassert grid.shape == (1, depth, height, width, 3)# check grid corner values- assert tuple(grid[0, 0, 0, 0].numpy()) == (0., 0., 0.)+ assert tuple(grid[0, 0, 0, 0].cpu().numpy()) == (0., 0., 0.)assert tuple(- grid[0, depth - 1, height - 1, width - 1].numpy()) == (depth - 1, width - 1, height - 1)+ grid[0, depth - 1, height - 1, width - 1].cpu().numpy()) == (depth - 1, width - 1, height - 1)