π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement invert_affine_matrix
edgarriba42099fb8e9f7
diff --git a/docs/source/geometric.rst b/docs/source/geometric.rstindex 69d1c566f..ddd666cbc 100644--- a/docs/source/geometric.rst+++ b/docs/source/geometric.rst@@ -10,6 +10,7 @@ The functions in this section perform various geometrical transformations of 2D.. autofunction:: get_perspective_transform.. autofunction:: get_rotation_matrix2d.. autofunction:: remap+.. autofunction:: invert_affine_transform.. autofunction:: affine.. autofunction:: rotatediff --git a/setup_dev_env.sh b/setup_dev_env.shindex 25677165b..ee136bce3 100755--- a/setup_dev_env.sh+++ b/setup_dev_env.sh@@ -32,6 +32,7 @@ $sdk_dir/.dev_env/bin/conda install -y \pytest \pytest-cov \flake8 \+ autopep8 \mypy \mypy_extensions \--file docs/requirements.txt \diff --git a/test/test_imgwarp.py b/test/test_imgwarp.pyindex 5e8df9d80..30e691e1f 100644--- a/test/test_imgwarp.py+++ b/test/test_imgwarp.py@@ -375,3 +375,49 @@ class TestRemap:remap_traced = torch.jit.trace(tgm.remap, input)assert_allclose(tgm.remap(*input), remap_traced(*input))+++class TestInvertAffineTransform:+ def test_smoke(self):+ matrix = torch.eye(2, 3)+ matrix_inv = tgm.invert_affine_transform(matrix)+ assert_allclose(matrix, matrix_inv)++ def test_rot90(self):+ angle = torch.tensor([90.])+ scale = torch.tensor([1.])+ center = torch.tensor([[0., 0.]])+ expected = torch.tensor([[+ [0., -1., 0.],+ [1., 0., 0.],+ ]])+ matrix = tgm.get_rotation_matrix2d(center, angle, scale)+ matrix_inv = tgm.invert_affine_transform(matrix)+ assert_allclose(matrix_inv, expected)++ def test_rot90_batch(self):+ angle = torch.tensor([90.])+ scale = torch.tensor([1.])+ center = torch.tensor([[0., 0.]])+ expected = torch.tensor([[+ [0., -1., 0.],+ [1., 0., 0.],+ ]])+ matrix = tgm.get_rotation_matrix2d(+ center, angle, scale).repeat(2, 1, 1)+ matrix_inv = tgm.invert_affine_transform(matrix)+ assert_allclose(matrix_inv, expected)++ def test_gradcheck(self):+ matrix = torch.eye(2, 3)+ matrix = utils.tensor_to_gradcheck_var(matrix) # to var+ assert gradcheck(tgm.invert_affine_transform, (matrix,),+ raise_exception=True)++ def test_jit(self):+ matrix = torch.eye(2, 3)+ op_traced = torch.jit.trace(+ tgm.invert_affine_transform, matrix)+ actual = tgm.invert_affine_transform(matrix)+ actual_traced = op_traced(matrix)+ assert_allclose(actual, actual_traced)diff --git a/torchgeometry/core/imgwarp.py b/torchgeometry/core/imgwarp.pyindex eec0451b4..6f62ae0d9 100644--- a/torchgeometry/core/imgwarp.py+++ b/torchgeometry/core/imgwarp.py@@ -16,6 +16,7 @@ __all__ = ["get_rotation_matrix2d","normal_transform_pixel","remap",+ "invert_affine_transform",]@@ -411,3 +412,37 @@ def remap(tensor: torch.Tensor, map_x: torch.Tensor,# warp ans returntensor_warped: torch.Tensor = F.grid_sample(tensor, map_xy_norm)return tensor_warped+++def invert_affine_transform(matrix: torch.Tensor) -> torch.Tensor:+ r"""Inverts an affine transformation.++ The function computes an inverse affine transformation represented by+ 2Γ3 matrix:++ .. math::+ \begin{bmatrix}+ a_{11} & a_{12} & b_{1} \\+ a_{21} & a_{22} & b_{2} \\+ \end{bmatrix}++ The result is also a 2Γ3 matrix of the same type as M.++ Args:+ matrix (torch.Tensor): original affine transform. The tensor musth be+ in the shape of (B, 2, 3).++ Return:+ torch.Tensor: the reverse affine transform.+ """+ if not torch.is_tensor(matrix):+ raise TypeError("Input matrix type is not a torch.Tensor. Got {}"+ .format(type(matrix)))+ if not (len(matrix.shape) == 3 or matrix.shape[-2:] == (2, 3)):+ raise ValueError("Input matrix must be a Bx2x3 tensor. Got {}"+ .format(matrix.shape))+ matrix_tmp: torch.Tensor = F.pad(matrix, (0, 0, 0, 1), "constant", 0.0)+ matrix_tmp[..., 2, 2] += 1.0++ matrix_inv: torch.Tensor = torch.inverse(matrix_tmp)+ return matrix_inv[..., :2, :3]