π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fixed divide by zero bug (#812)
Jim Bohnslavf82d283b5b5b
diff --git a/kornia/geometry/warp/homography_warper.py b/kornia/geometry/warp/homography_warper.pyindex ca3d8dd03..45e58a75c 100644--- a/kornia/geometry/warp/homography_warper.py+++ b/kornia/geometry/warp/homography_warper.py@@ -8,7 +8,6 @@ from kornia.utils import create_meshgrid, create_meshgrid3dfrom kornia.geometry.linalg import transform_pointsfrom kornia.testing import check_is_tensor-__all__ = ["HomographyWarper","homography_warp",@@ -109,7 +108,7 @@ def homography_warp(patch_src: torch.Tensor,if not src_homo_dst.device == patch_src.device:raise TypeError("Patch and homography must be on the same device. \Got patch.device: {} src_H_dst.device: {}.".format(- patch_src.device, src_homo_dst.device))+ patch_src.device, src_homo_dst.device))height, width = dsizegrid = create_meshgrid(height, width, normalized_coordinates=normalized_coordinates)@@ -155,7 +154,7 @@ def homography_warp3d(patch_src: torch.Tensor,if not src_homo_dst.device == patch_src.device:raise TypeError("Patch and homography must be on the same device. \Got patch.device: {} src_H_dst.device: {}.".format(- patch_src.device, src_homo_dst.device))+ patch_src.device, src_homo_dst.device))depth, height, width = dsizegrid = create_meshgrid3d(depth, height, width, normalized_coordinates=normalized_coordinates,@@ -266,7 +265,7 @@ class HomographyWarper(nn.Module):Got patch.device: {} warped_grid.device: {}. Wheter \recall precompute_warp_grid() with the correct device \for the homograhy or change the patch device.".format(- patch_src.device, _warped_grid.device))+ patch_src.device, _warped_grid.device))warped_patch = F.grid_sample(patch_src, _warped_grid, mode=self.mode, padding_mode=self.padding_mode,align_corners=self.align_corners)@@ -278,12 +277,13 @@ class HomographyWarper(nn.Module):return warped_patch-def normal_transform_pixel(height: int, width: int) -> torch.Tensor:+def normal_transform_pixel(height: int, width: int, eps: float = 1e-14) -> torch.Tensor:r"""Compute the normalization matrix from image size in pixels to [-1, 1].Args:height (int): image height.width (int): image width.+ eps (float): epsilon to prevent divide-by-zero errorsReturns:torch.Tensor: normalized transform with shape :math:`(1, 3, 3)`.@@ -292,19 +292,24 @@ def normal_transform_pixel(height: int, width: int) -> torch.Tensor:[0.0, 1.0, -1.0],[0.0, 0.0, 1.0]]) # 3x3- tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / (width - 1.0)- tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / (height - 1.0)+ # prevent divide by zero bugs+ width_denom: float = eps if width == 1 else width - 1.0+ height_denom: float = eps if height == 1 else height - 1.0++ tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / width_denom+ tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / height_denomreturn tr_mat.unsqueeze(0) # 1x3x3-def normal_transform_pixel3d(depth: int, height: int, width: int) -> torch.Tensor:+def normal_transform_pixel3d(depth: int, height: int, width: int, eps: float = 1e-14) -> torch.Tensor:r"""Compute the normalization matrix from image size in pixels to [-1, 1].Args:depth (int): image depth.height (int): image height.width (int): image width.+ eps (float): epsilon to prevent divide-by-zero errorsReturns:Tensor: normalized transform with shape :math:`(1, 4, 4)`.@@ -314,9 +319,14 @@ def normal_transform_pixel3d(depth: int, height: int, width: int) -> torch.Tenso[0.0, 0.0, 1.0, -1.0],[0.0, 0.0, 0.0, 1.0]]) # 4x4- tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / (width - 1.0)- tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / (height - 1.0)- tr_mat[2, 2] = tr_mat[2, 2] * 2.0 / (depth - 1.0)+ # prevent divide by zero bugs+ width_denom: float = eps if width == 1 else width - 1.0+ height_denom: float = eps if height == 1 else height - 1.0+ depth_denom: float = eps if depth == 1 else depth - 1.0++ tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / width_denom+ tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / height_denom+ tr_mat[2, 2] = tr_mat[2, 2] * 2.0 / depth_denomreturn tr_mat.unsqueeze(0) # 1x4x4diff --git a/test/geometry/warp/test_homography_warper.py b/test/geometry/warp/test_homography_warper.pyindex 8851d44f7..46ca239e3 100644--- a/test/geometry/warp/test_homography_warper.py+++ b/test/geometry/warp/test_homography_warper.py@@ -9,7 +9,6 @@ from torch.testing import assert_allcloseclass TestHomographyWarper:-num_tests = 10threshold = 0.1@@ -256,16 +255,45 @@ class TestHomographyWarper:class TestHomographyNormalTransform:-- def test_transform2d(self):- height, width = 2, 5+ expected_2d_0 = torch.tensor([[+ [0.5, 0.0, -1.],+ [0.0, 2.0, -1.],+ [0.0, 0.0, 1.]]])++ expected_2d_1 = torch.tensor([[+ [0.5, 0.0, -1.],+ [0.0, 2e14, -1.],+ [0.0, 0.0, 1.]]])++ expected_3d_0 = expected = torch.tensor([[+ [0.4, 0.0, 0.0, -1.],+ [0.0, 2.0, 0.0, -1.],+ [0.0, 0.0, 0.6667, -1.],+ [0.0, 0.0, 0.0, 1.],+ ]])++ expected_3d_1 = torch.tensor([[+ [0.4, 0.0, 0.0, -1.],+ [0.0, 2e14, 0.0, -1.],+ [0.0, 0.0, 0.6667, -1.],+ [0.0, 0.0, 0.0, 1.],+ ]])++ @pytest.mark.parametrize("height,width,expected", [+ (2, 5, expected_2d_0),+ (1, 5, expected_2d_1)+ ])+ def test_transform2d(self, height, width, expected):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)+ @pytest.mark.parametrize("height", [1, 2, 5])+ @pytest.mark.parametrize("width", [1, 2, 5])+ def test_divide_by_zero2d(self, height, width):+ output = kornia.normal_transform_pixel(height, width)+ assert torch.isinf(output).sum().item() == 0+def test_transform2d_apply(self):height, width = 2, 5input = torch.tensor([[0., 0.], [width - 1, height - 1]])@@ -274,17 +302,21 @@ class TestHomographyNormalTransform:output = kornia.transform_points(transform, input)assert_allclose(output, expected)- def test_transform3d(self):- height, width, depth = 2, 6, 4+ @pytest.mark.parametrize("height,width,depth,expected", [+ (2, 6, 4, expected_3d_0),+ (1, 6, 4, expected_3d_1)+ ])+ def test_transform3d(self, height, width, depth, expected):output = kornia.normal_transform_pixel3d(depth, height, width)- expected = torch.tensor([[- [0.4, 0.0, 0.0, -1.],- [0.0, 2.0, 0.0, -1.],- [0.0, 0.0, 0.6667, -1.],- [0.0, 0.0, 0.0, 1.],- ]])assert_allclose(output, expected)+ @pytest.mark.parametrize("height", [1, 2, 5])+ @pytest.mark.parametrize("width", [1, 2, 5])+ @pytest.mark.parametrize("depth", [1, 2, 5])+ def test_divide_by_zero3d(self, height, width, depth):+ output = kornia.normal_transform_pixel3d(depth, height, width)+ assert torch.isinf(output).sum().item() == 0+def test_transform3d_apply(self):depth, height, width = 3, 2, 5input = torch.tensor([[0., 0., 0.], [width - 1, height - 1, depth - 1]])@@ -295,7 +327,6 @@ class TestHomographyNormalTransform:class TestHomographyWarper3D:-num_tests = 10threshold = 0.1