Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

generalize homography warper to handle local homographies case

edgarriba3de9f7e77f02
diff --git a/test/test_homography_warper.py b/test/test_homography_warper.py
index 024b2824c..cd7838098 100644
--- a/test/test_homography_warper.py
+++ b/test/test_homography_warper.py
@@ -53,6 +53,27 @@ class Tester(unittest.TestCase):
patch_dst_to_src_functional)
self.assertTrue(res)
+ def test_local_homography_warper(self):
+ # generate input data
+ batch_size = 1
+ height, width = 16, 32
+ eye_size = 3 # identity 3x3
+
+ # create checkerboard
+ board = utils.create_checkerboard(height, width, 4)
+ patch_src = torch.from_numpy(board).view(
+ 1, 1, height, width).expand(batch_size, 1, height, width)
+
+ # create local homography
+ dst_homo_src = utils.create_eye_batch(batch_size, eye_size)
+ dst_homo_src = dst_homo_src.view(batch_size, -1).unsqueeze(1)
+ dst_homo_src = dst_homo_src.repeat(1, height * width, 1).view(
+ 1, height, width, 3, 3) # NxHxWx3x3
+
+ # warp reference patch
+ patch_src_to_i = tgm.homography_warp(
+ patch_src, dst_homo_src, (height, width))
+
def test_homography_warper_gradcheck(self):
# generate input data
batch_size = 1
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.py
index a0f268db2..cfbbef8c6 100644
--- a/torchgeometry/conversions.py
+++ b/torchgeometry/conversions.py
@@ -95,11 +95,6 @@ def convert_points_from_homogeneous(points, eps=1e-6):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(points)))
- if not len(points.shape) == 3:
- raise ValueError(
- "Input size must be a three dimensional tensor. Got {}".format(
- points.shape))
-
return points[..., :-1] / (points[..., -1:] + eps)
@@ -124,12 +119,10 @@ def convert_points_to_homogeneous(points):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(points)))
- if not len(points.shape) == 3:
- raise ValueError(
- "Input size must be a three dimensional tensor. Got {}".format(
- points.shape))
-
- return torch.cat([points, torch.ones_like(points)[..., :1]], dim=-1)
+ # create shape for ones tensor: Nx(...)xD-1
+ new_shape = points.shape[:-1] + (points.shape[-1].bit_length() - 1,)
+ ones = torch.ones(new_shape, dtype=points.dtype)
+ return torch.cat([points, ones.to(points.device)], dim=-1)
def transform_points(dst_pose_src, points_src):
@@ -155,17 +148,16 @@ def transform_points(dst_pose_src, points_src):
raise TypeError("Input type is not a torch.Tensor")
if not dst_pose_src.device == points_src.device:
raise TypeError("Tensor must be in the same device")
- if not len(dst_pose_src.shape) == 3 or not len(points_src.shape) == 3:
- raise ValueError("Input size must be a three dimensional tensor")
if not dst_pose_src.shape[0] == points_src.shape[0]:
raise ValueError("Input batch size must be the same for both tensors")
- if not dst_pose_src.shape[2] == (points_src.shape[2] + 1):
- raise ValueError("Input dimensions must differe by one unit")
+ if not dst_pose_src.shape[-1] == (points_src.shape[-1] + 1):
+ raise ValueError("Last input dimensions must differe by one unit")
# to homogeneous
points_src_h = convert_points_to_homogeneous(points_src) # BxNxD+1
+ points_src_h = torch.unsqueeze(points_src_h, dim=-1)
# transform coordinates
- points_dst_h = torch.matmul(dst_pose_src, points_src_h.transpose(1, 2))
- points_dst_h = points_dst_h.permute(0, 2, 1) # BxNxD+1
+ points_dst_h = torch.matmul(dst_pose_src, points_src_h)
+ points_dst_h = torch.squeeze(points_dst_h, dim=-1)
# to euclidean
points_dst = convert_points_from_homogeneous(points_dst_h) # BxNxD
return points_dst
diff --git a/torchgeometry/homography_warper.py b/torchgeometry/homography_warper.py
index 7f3dfeb66..abe699bd4 100644
--- a/torchgeometry/homography_warper.py
+++ b/torchgeometry/homography_warper.py
@@ -64,11 +64,14 @@ class HomographyWarper(nn.Module):
:returns: Tensor[1, Height, Width, 2] containing transformed points in
normalized images space.
"""
+ grid = self.grid
+ if len(H.shape) > 3: # local homography case
+ grid = grid.view(*H.shape[:-2], 2) # 1xHxWx2
batch_size = H.shape[0] # expand grid to match the input batch size
- grid = self.grid.expand(batch_size, *self.grid.shape[-2:]) # Nx(H*W)x2
+ grid = grid.expand(batch_size, *grid.shape[1:]) # Nx(...)x2
# perform the actual grid transformation,
# the grid is copied to input device and casted to the same type
- flow = transform_points(H, grid.to(H.device).type_as(H)) # Nx(H*W)x2
+ flow = transform_points(H, grid.to(H.device).type_as(H)) # Nx(...)x2
return flow.view(batch_size, self.height, self.width, 2) # NxHxWx2
def random_warp(self, patch, dist):