Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

tgm.inverse -> torch.inverse

edgarriba406757dcc768
diff --git a/test/test_functional.py b/test/test_functional.py
index 57de71aeb..d830efcac 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -60,34 +60,6 @@ class Tester(unittest.TestCase):
raise_exception=True)
self.assertTrue(res)
- def test_inverse(self):
- # generate input data
- batch_size = 2
- eye_size = 3 # identity 3x3
- homographies = utils.create_random_homography(batch_size, eye_size)
- homographies_inv = tgm.inverse(homographies)
-
- # H_inv * H == I
- res = torch.matmul(homographies_inv, homographies)
- eye = utils.create_eye_batch(batch_size, eye_size)
- error = utils.compute_mse(res, eye)
- self.assertAlmostEqual(error.item(), 0.0, places=4)
-
- # functional
- self.assertTrue(
- torch.allclose(homographies_inv, tgm.Inverse()(homographies)))
-
- def test_inverse_gradcheck(self):
- # generate input data
- batch_size = 2
- eye_size = 3 # identity 3x3
- homographies = utils.create_random_homography(batch_size, eye_size)
- homographies = utils.tensor_to_gradcheck_var(homographies) # to var
-
- # evaluate function gradient
- res = gradcheck(tgm.inverse, (homographies,), raise_exception=True)
- self.assertTrue(res)
-
def test_transform_points(self):
# generate input data
batch_size = 2
@@ -101,7 +73,7 @@ class Tester(unittest.TestCase):
points_dst = tgm.transform_points(dst_homo_src, points_src)
# transform the points from ref to dst
- src_homo_dst = tgm.inverse(dst_homo_src)
+ src_homo_dst = torch.inverse(dst_homo_src)
points_dst_to_src = tgm.transform_points(src_homo_dst, points_dst)
# projected should be equal as initial
@@ -239,7 +211,7 @@ class Tester(unittest.TestCase):
# compute homography from ref to i
i_H_ref = tgm.homography_i_H_ref(pinhole_i, pinhole_ref) + eps
- i_H_ref_inv = tgm.inverse(i_H_ref)
+ i_H_ref_inv = torch.inverse(i_H_ref)
# compute homography from i to ref
ref_H_i = tgm.homography_i_H_ref(pinhole_ref, pinhole_i) + eps
diff --git a/test/test_homography_warper.py b/test/test_homography_warper.py
index cd7838098..498b46d89 100644
--- a/test/test_homography_warper.py
+++ b/test/test_homography_warper.py
@@ -37,7 +37,7 @@ class Tester(unittest.TestCase):
# transform the points from dst to ref
patch_dst = warper(patch_src, dst_homo_src_i)
- patch_dst_to_src = warper(patch_dst, tgm.inverse(dst_homo_src_i))
+ patch_dst_to_src = warper(patch_dst, torch.inverse(dst_homo_src_i))
# projected should be equal as initial
error = utils.compute_patch_error(
@@ -48,7 +48,7 @@ class Tester(unittest.TestCase):
# check functional api
patch_dst_to_src_functional = tgm.homography_warp(
- patch_dst, tgm.inverse(dst_homo_src_i), (height, width))
+ patch_dst, torch.inverse(dst_homo_src_i), (height, width))
res = utils.check_equal_torch(patch_dst_to_src,
patch_dst_to_src_functional)
self.assertTrue(res)
diff --git a/test/test_imgwarp.py b/test/test_imgwarp.py
index 4f2ca6710..09471152d 100644
--- a/test/test_imgwarp.py
+++ b/test/test_imgwarp.py
@@ -28,14 +28,14 @@ class Tester(unittest.TestCase):
# apply transformation and inverse
_, _, h, w = patch.shape
patch_warped = tgm.warp_perspective(patch, M, dsize=(height, width))
- patch_warped_inv = tgm.warp_perspective(patch_warped, tgm.inverse(M),
+ patch_warped_inv = tgm.warp_perspective(patch_warped, torch.inverse(M),
dsize=(height, width))
# generate mask to compute error
mask = torch.ones_like(patch)
mask_warped_inv = tgm.warp_perspective(
tgm.warp_perspective(patch, M, dsize=(height, width)),
- tgm.inverse(M), dsize=(height, width))
+ torch.inverse(M), dsize=(height, width))
res = utils.check_equal_torch(mask_warped_inv * patch,
mask_warped_inv * patch_warped_inv)
@@ -74,7 +74,7 @@ class Tester(unittest.TestCase):
grid_src_pix = torch.unsqueeze(grid_src_pix, dim=0)
src_norm_trans_src_pix = tgm.normal_transform_pixel(src_h, src_w)
- src_pix_trans_src_norm = tgm.inverse(src_norm_trans_src_pix)
+ src_pix_trans_src_norm = torch.inverse(src_norm_trans_src_pix)
dst_norm_trans_dst_pix = tgm.normal_transform_pixel(dst_h, dst_w)
diff --git a/test/test_utils.py b/test/test_utils.py
index 1ba5d75d9..0fc3e7201 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -51,7 +51,7 @@ class Tester(unittest.TestCase):
# grid from pixel space to normalized
norm_trans_pix = tgm.normal_transform_pixel(height, width) # 1x3x3
- pix_trans_norm = tgm.inverse(norm_trans_pix) # 1x3x3
+ pix_trans_norm = torch.inverse(norm_trans_pix) # 1x3x3
# transform grids
grid_pix_to_norm = tgm.transform_points(norm_trans_pix, grid_pix)
grid_norm_to_pix = tgm.transform_points(pix_trans_norm, grid_norm)
diff --git a/torchgeometry/imgwarp.py b/torchgeometry/imgwarp.py
index 7a92bae65..27f782e30 100644
--- a/torchgeometry/imgwarp.py
+++ b/torchgeometry/imgwarp.py
@@ -1,6 +1,5 @@
import torch
-from .utils import inverse
from .homography_warper import homography_warp
from .conversions import deg2rad
@@ -32,7 +31,7 @@ def dst_norm_to_dst_norm(dst_pix_trans_src_pix, dsize_src, dsize_dst):
# compute the transformation pixel/norm for src/dst
src_norm_trans_src_pix = normal_transform_pixel(
src_h, src_w).to(device).to(dtype)
- src_pix_trans_src_norm = inverse(src_norm_trans_src_pix)
+ src_pix_trans_src_norm = torch.inverse(src_norm_trans_src_pix)
dst_norm_trans_dst_pix = normal_transform_pixel(
dst_h, dst_w).to(device).to(dtype)
# compute chain transformations
@@ -47,7 +46,7 @@ def transform_warp_impl(src, dst_pix_trans_src_pix, dsize_src, dsize_dst):
"""
dst_norm_trans_dst_norm = dst_norm_to_dst_norm(
dst_pix_trans_src_pix, dsize_src, dsize_dst)
- return homography_warp(src, inverse(dst_norm_trans_dst_norm), dsize_dst)
+ return homography_warp(src, torch.inverse(dst_norm_trans_dst_norm), dsize_dst)
def warp_perspective(src, M, dsize, flags='bilinear', border_mode=None,
diff --git a/torchgeometry/utils.py b/torchgeometry/utils.py
index a33bc28ca..8acc68253 100644
--- a/torchgeometry/utils.py
+++ b/torchgeometry/utils.py
@@ -11,8 +11,6 @@ __all__ = [
"draw_rectangle",
"create_pinhole",
"create_meshgrid",
- "inverse",
- "Inverse",
]
@@ -45,32 +43,6 @@ def create_meshgrid(height, width, normalized_coordinates=True):
return torch.unsqueeze(base_grid, dim=0).permute(0, 2, 3, 1) # 1xHxwx2
-def inverse(transforms):
- """Batched version of `torch.inverse`
-
- Args:
- transforms (Tensor): tensor of transformations of size (B, D, D).
-
- Returns:
- Tensor: tensor of inverted transformations of size (B, D, D).
-
- """
- if not len(transforms.shape) == 3:
- raise ValueError(
- "Input size must be a three dimensional tensor. Got {}".format(
- transforms.shape))
- # iterate, compute inverse and stack tensors
- return torch.stack([torch.inverse(transform) for transform in transforms])
-
-
-class Inverse(nn.Module):
- def __init__(self):
- super(Inverse, self).__init__()
-
- def forward(self, input):
- return inverse(input)
-
-
def image_to_tensor(image):
"""Converts a numpy image to a torch.Tensor image.