Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix imgwarp test

edgarriba44d2b0127999
diff --git a/test/test_imgwarp.py b/test/test_imgwarp.py
index 4c7be0617..cfc41e7d9 100644
--- a/test/test_imgwarp.py
+++ b/test/test_imgwarp.py
@@ -12,7 +12,7 @@ class Tester(unittest.TestCase):
def test_warp_perspective(self):
# generate input data
batch_size = 1
- height, width = 2, 4
+ height, width = 16, 32
alpha = tgm.pi / 2 # 90 deg rotation
# create data patch
@@ -26,13 +26,19 @@ class Tester(unittest.TestCase):
]]) # Bx3x3
# apply transformation and inverse
- import ipdb;ipdb.set_trace()
_, _, 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),
dsize=(height, width))
- res = utils.check_equal_torch(patch, patch_warped_inv)
+ # 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))
+
+ res = utils.check_equal_torch(mask_warped_inv * patch,
+ mask_warped_inv * patch_warped_inv)
self.assertTrue(res)
def test_warp_perspective_gradcheck(self):
diff --git a/torchgeometry/imgwarp.py b/torchgeometry/imgwarp.py
index 0a02394ea..b57e416c5 100644
--- a/torchgeometry/imgwarp.py
+++ b/torchgeometry/imgwarp.py
@@ -12,11 +12,13 @@ __all__ = [
def center_transform(transform, height, width):
assert len(transform.shape) == 3, transform.shape
# move points to origin
- center_mat_origin = torch.unsqueeze(torch.eye(3), dim=0)
+ center_mat_origin = torch.unsqueeze(torch.eye(3,
+ device=transform.device, dtype=transform.dtype), dim=0)
center_mat_origin[..., 0, 2] = float(width) / 2
center_mat_origin[..., 1, 2] = float(height) / 2
# move points from origin
- origin_mat_center = torch.unsqueeze(torch.eye(3), dim=0)
+ origin_mat_center = torch.unsqueeze(torch.eye(3,
+ device=transform.device, dtype=transform.dtype), dim=0)
origin_mat_center[..., 0, 2] = -float(width) / 2
origin_mat_center[..., 1, 2] = -float(height) / 2
return torch.matmul(center_mat_origin,
@@ -57,7 +59,7 @@ def warp_perspective(src, M, dsize, flags='bilinear', border_mode=None,
- Output: :math:`(B, C, H, W)`
.. note::
- See a working example `here <../../../examples/warp_perspective.ipynb>`_.
+ See a working example `here <https://github.com/arraiy/torchgeometry/blob/master/examples/warp_perspective.ipynb>`_.
"""
if not torch.is_tensor(src):
raise TypeError("Input src type is not a torch.Tensor. Got {}"