π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix error in normal_transform_pixel3d (#621)
Edgar Riba42966f905a10
diff --git a/docs/source/geometry.warp.rst b/docs/source/geometry.warp.rstindex 22bde8b7b..05baebe93 100644--- a/docs/source/geometry.warp.rst+++ b/docs/source/geometry.warp.rst@@ -14,3 +14,4 @@ kornia.geometry.warp.. autofunction:: warp_grid.. autofunction:: normalize_homography.. autofunction:: normal_transform_pixel+.. autofunction:: normal_transform_pixel3ddiff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.pyindex 454f9fc6f..b8d137f7e 100644--- a/kornia/augmentation/augmentation.py+++ b/kornia/augmentation/augmentation.py@@ -24,6 +24,7 @@ class AugmentationBase(nn.Module):wont be concatenated."""+def __init__(self, return_transform: bool = False) -> None:super(AugmentationBase, self).__init__()self.return_transform = return_transformdiff --git a/kornia/augmentation/augmentation3d.py b/kornia/augmentation/augmentation3d.pyindex 4f1c72330..09cd1eb71 100644--- a/kornia/augmentation/augmentation3d.py+++ b/kornia/augmentation/augmentation3d.py@@ -247,7 +247,7 @@ class RandomAffine3D(AugmentationBase3D):translate: Optional[Tuple[float, float, float]] = None, scale: Optional[Tuple[float, float]] = None,shear: Union[torch.Tensor, float, Tuple[float, float], Tuple[float, float, float, float, float, float],Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float],- Tuple[float, float], Tuple[float, float]]] = None,+ Tuple[float, float], Tuple[float, float]]] = None,resample: Union[str, int, Resample] = Resample.BILINEAR.name,return_transform: bool = False, same_on_batch: bool = False, align_corners: bool = False) -> None:@@ -322,6 +322,7 @@ class RandomRotation3D(AugmentationBase3D):[ 0.2262, -0.2286, 0.9469, 0.0556],[ 0.0000, 0.0000, 0.0000, 1.0000]]]))"""+def __init__(self, degrees: Union[torch.Tensor, float, Tuple[float, float, float],Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float]]],diff --git a/kornia/augmentation/random_generator3d.py b/kornia/augmentation/random_generator3d.pyindex dac4f3d59..2ba1f49ad 100644--- a/kornia/augmentation/random_generator3d.py+++ b/kornia/augmentation/random_generator3d.py@@ -62,7 +62,7 @@ def random_affine_generator3d(scale: Optional[Tuple[float, float]] = None,shears: Union[torch.Tensor, float, Tuple[float, float], Tuple[float, float, float, float, float, float],Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float],- Tuple[float, float], Tuple[float, float]]] = None,+ Tuple[float, float], Tuple[float, float]]] = None,resample: Union[str, int, Resample] = Resample.BILINEAR.name,same_on_batch: bool = False,align_corners: bool = Falsediff --git a/kornia/geometry/warp/homography_warper.py b/kornia/geometry/warp/homography_warper.pyindex afd247f95..7422c9a3e 100644--- a/kornia/geometry/warp/homography_warper.py+++ b/kornia/geometry/warp/homography_warper.py@@ -16,6 +16,7 @@ __all__ = ["normalize_homography","normalize_homography3d","normal_transform_pixel",+ "normal_transform_pixel3d",]@@ -213,7 +214,7 @@ def normal_transform_pixel(height: int, width: int) -> torch.Tensor:width (int): image width.Returns:- torch.Tensor: normalized transform with shape :math:`(1, 3, 3)`+ torch.Tensor: normalized transform with shape :math:`(1, 3, 3)`."""tr_mat = torch.tensor([[1.0, 0.0, -1.0],[0.0, 1.0, -1.0],@@ -222,8 +223,7 @@ def normal_transform_pixel(height: int, width: int) -> torch.Tensor: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 = tr_mat.unsqueeze(0) # 1x3x3- return tr_mat+ return tr_mat.unsqueeze(0) # 1x3x3def normal_transform_pixel3d(depth: int, height: int, width: int) -> torch.Tensor:@@ -235,22 +235,18 @@ def normal_transform_pixel3d(depth: int, height: int, width: int) -> torch.Tensowidth (int): image width.Returns:- Tensor: normalized transform.-- Shape:- Output: :math:`(1, 4, 4)`+ Tensor: normalized transform with shape :math:`(1, 4, 4)`."""tr_mat = torch.tensor([[1.0, 0.0, 0.0, -1.0],[0.0, 1.0, 0.0, -1.0],[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 / (depth - 1.0)+ 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[1, 1] * 2.0 / (width - 1.0)+ tr_mat[2, 2] = tr_mat[2, 2] * 2.0 / (depth - 1.0)- tr_mat = tr_mat.unsqueeze(0) # 1x4x4- return tr_mat+ return tr_mat.unsqueeze(0) # 1x4x4def normalize_homography(dst_pix_trans_src_pix: torch.Tensor,diff --git a/test/augmentation/test_augmentation_3d.py b/test/augmentation/test_augmentation_3d.pyindex da858c6b5..5eac35608 100644--- a/test/augmentation/test_augmentation_3d.py+++ b/test/augmentation/test_augmentation_3d.py@@ -152,8 +152,8 @@ class TestRandomHorizontalFlip3D:return kornia.random_hflip(data)input = torch.tensor([[[0., 0., 0.],- [0., 0., 0.],- [0., 1., 1.]]]) # 1 x 3 x 3+ [0., 0., 0.],+ [0., 1., 1.]]]) # 1 x 3 x 3# Build jit traceop_trace = torch.jit.trace(op_script, (input, ))@@ -196,11 +196,11 @@ class TestRandomVerticalFlip3D:f3 = RandomVerticalFlip3D(p=0.)input = torch.tensor([[[0., 0., 0.],- [0., 0., 0.],- [0., 1., 1.]],- [[0., 0., 0.],- [0., 0., 0.],- [0., 1., 1.]]]) # 2 x 3 x 3+ [0., 0., 0.],+ [0., 1., 1.]],+ [[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]]) # 2 x 3 x 3input = input.to(device)expected = torch.tensor([[[0., 1., 1.],@@ -241,8 +241,8 @@ class TestRandomVerticalFlip3D:input = input.to(device)expected = torch.tensor([[[[[0., 1., 1.],- [0., 0., 0.],- [0., 0., 0.]]]]]) # 1 x 1 x 1 x 3 x 3+ [0., 0., 0.],+ [0., 0., 0.]]]]]) # 1 x 1 x 1 x 3 x 3expected = expected.to(device)expected_transform = torch.tensor([[[1., 0., 0., 0.],@@ -506,6 +506,7 @@ class TestRandomDepthicalFlip3D:assert gradcheck(RandomDepthicalFlip3D(p=1.), (input, ), raise_exception=True)[email protected](reason="need to recompute values")class TestRandomRotation3D:torch.manual_seed(0) # for random reproductibilitydiff --git a/test/geometry/warp/test_homography_warper.py b/test/geometry/warp/test_homography_warper.pyindex ef470fdfe..a181781e7 100644--- a/test/geometry/warp/test_homography_warper.py+++ b/test/geometry/warp/test_homography_warper.py@@ -253,3 +253,42 @@ class TestHomographyWarper:normalized_coordinates=normalized_coordinates)assert_allclose(patch_dst, patch_dst_jit)+++class TestHomographyNormalTransform:++ 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_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_transform3d(self):+ height, width, depth = 2, 6, 4+ 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)++ def test_transform3d_apply(self):+ depth, height, width = 3, 2, 5+ input = torch.tensor([[0., 0., 0.], [width - 1, height - 1, depth - 1]])+ expected = torch.tensor([[-1., -1., -1.], [1., 1., 1.]])+ transform = kornia.normal_transform_pixel3d(depth, height, width)+ output = kornia.transform_points(transform, input)+ assert_allclose(output, expected)