def test_normalize_homography_identity(self, batch_size, device, dtype): # create input data height, width = 2, 5 dst_homo_src = utils.create_eye_batch(batch_size=batch_size, eye_size=3, device=device, dtype=dtype) res = torch.tensor( [[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype) assert (kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) == res).all() norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width)) assert (norm_homo == dst_homo_src).all() norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height, width)) assert (norm_homo == dst_homo_src).all() # change output scale norm_homo = kornia.normalize_homography(dst_homo_src, (height, width), (height * 2, width // 2)) res = torch.tensor( [[[4.0, 0.0, 3.0], [0.0, 1 / 3, -2 / 3], [0.0, 0.0, 1.0]]], device=device, dtype=dtype).repeat(batch_size, 1, 1) assert_allclose(norm_homo, res, atol=1e-4, rtol=1e-4)
def test_transform2d_apply(self, device, dtype): height, width = 2, 5 input = torch.tensor([[0.0, 0.0], [width - 1, height - 1]], device=device, dtype=dtype) expected = torch.tensor([[-1.0, -1.0], [1.0, 1.0]], device=device, dtype=dtype) transform = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) output = kornia.transform_points(transform, input) assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
def test_transform2d_apply(self): height, width = 2, 5 input = torch.tensor([[0., 0.], [width - 1, height - 1]]) expected = torch.tensor([[-1., -1.], [1., 1.]]) transform = kornia.normal_transform_pixel(height, width) output = kornia.transform_points(transform, input) assert_allclose(output, expected)
def test_normalize_pixel_grid(device, dtype): if device.type == 'cuda' and dtype == torch.float16: pytest.skip('"inverse_cuda" not implemented for "Half"') # generate input data batch_size = 1 height, width = 2, 4 # create points grid grid_norm = kornia.utils.create_meshgrid(height, width, normalized_coordinates=True, device=device, dtype=dtype) assert grid_norm.device == device assert grid_norm.dtype == dtype grid_norm = torch.unsqueeze(grid_norm, dim=0) grid_pix = kornia.utils.create_meshgrid(height, width, normalized_coordinates=False, device=device, dtype=dtype) assert grid_pix.device == device assert grid_pix.dtype == dtype grid_pix = torch.unsqueeze(grid_pix, dim=0) # grid from pixel space to normalized norm_trans_pix = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) # 1x3x3 pix_trans_norm = torch.inverse(norm_trans_pix) # 1x3x3 # transform grids grid_pix_to_norm = kornia.transform_points(norm_trans_pix, grid_pix) grid_norm_to_pix = kornia.transform_points(pix_trans_norm, grid_norm) assert_close(grid_pix, grid_norm_to_pix) assert_close(grid_norm, grid_pix_to_norm)
def test_transform2d(self): height, width = 2, 5 output = kornia.normal_transform_pixel(height, width) expected = torch.tensor([[ [0.5, 0.0, -1.], [0.0, 2.0, -1.], [0.0, 0.0, 1.]]]) assert_allclose(output, expected)
def test_transform2d(self, height, width, expected, device, dtype): output = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) assert_allclose(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
def test_normalize_pixel_grid(): # generate input data batch_size = 1 height, width = 2, 4 # create points grid grid_norm = kornia.utils.create_meshgrid( height, width, normalized_coordinates=True) grid_norm = torch.unsqueeze(grid_norm, dim=0) grid_pix = kornia.utils.create_meshgrid( height, width, normalized_coordinates=False) grid_pix = torch.unsqueeze(grid_pix, dim=0) # grid from pixel space to normalized norm_trans_pix = kornia.normal_transform_pixel(height, width) # 1x3x3 pix_trans_norm = torch.inverse(norm_trans_pix) # 1x3x3 # transform grids grid_pix_to_norm = kornia.transform_points(norm_trans_pix, grid_pix) grid_norm_to_pix = kornia.transform_points(pix_trans_norm, grid_norm) assert_allclose(grid_pix, grid_norm_to_pix) assert_allclose(grid_norm, grid_pix_to_norm)
def test_divide_by_zero2d(self, height, width, device, dtype): output = kornia.normal_transform_pixel(height, width, device=device, dtype=dtype) assert torch.isinf(output).sum().item() == 0