Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix depth warper and add tests

edgarriba77563720b1ca
diff --git a/test/test_depth_warper.py b/test/test_depth_warper.py
index 7916a542a..d7a0fa160 100644
--- a/test/test_depth_warper.py
+++ b/test/test_depth_warper.py
@@ -17,14 +17,14 @@ class Tester(unittest.TestCase):
fx, fy = 1., 1.
rx, ry, rz = 0., 0., 0.
tx, ty, tz = 0., 0., 0.
- offset_x = 0. # we will apply a 10units offset to `i` camera
+ offset = 1. # we will apply a 1unit offset to `i` camera
pinhole_src = utils.create_pinhole(fx, fy, cx, cy, \
height, width, rx, ry, rx, tx, ty, tz)
pinhole_src = pinhole_src.expand(batch_size, -1)
pinhole_dst = utils.create_pinhole(fx, fy, cx, cy, \
- height, width, rx, ry, rx, tx + offset_x, ty, tz)
+ height, width, rx, ry, rx, tx + offset, ty + offset, tz)
pinhole_dst = pinhole_dst.expand(batch_size, -1)
# create checkerboard
@@ -32,15 +32,22 @@ class Tester(unittest.TestCase):
patch_src = torch.from_numpy(board).view(
1, 1, height, width).expand(batch_size, 1, height, width)
- # instantiate warper
+ # instantiate warper and compute relative homographies
warper = dgm.DepthWarper(pinhole_src, height, width)
warper.compute_homographies(pinhole_dst, scale=torch.ones(batch_size, 1))
# generate synthetic inverse depth
inv_depth_src = torch.ones(batch_size, 1, height, width)
- import pdb;pdb.set_trace()
+ # warpd source patch by depth
patch_dst = warper(inv_depth_src, patch_src)
+
+ # compute error
+ res = utils.check_equal_torch( \
+ patch_src[..., :-int(offset), :-int(offset)], \
+ patch_dst[..., int(offset):, int(offset):])
+ self.assertTrue(res)
+
pass
diff --git a/torchgeometry/__init__.py b/torchgeometry/__init__.py
index 56e860f17..0f4141ff9 100644
--- a/torchgeometry/__init__.py
+++ b/torchgeometry/__init__.py
@@ -1,6 +1,6 @@
from .functional import *
from .homography_warper import HomographyWarper
-#from .depth_warper import DepthWarper
+from .depth_warper import DepthWarper
from torchgeometry import utils
from torchgeometry import transforms
diff --git a/torchgeometry/depth_warper.py b/torchgeometry/depth_warper.py
index 15956c460..5d87bc354 100644
--- a/torchgeometry/depth_warper.py
+++ b/torchgeometry/depth_warper.py
@@ -6,8 +6,11 @@ from .functional import scale_pinhole, homography_i_H_ref
class DepthWarper(nn.Module):
+ """Warps a patch by inverse depth.
+ """
def __init__(self, pinholes, width=None, height=None):
super(DepthWarper, self).__init__()
+ # TODO: add type and value checkings
self.width = width
self.height = height
self._pinholes = pinholes
@@ -15,6 +18,7 @@ class DepthWarper(nn.Module):
self._pinhole_ref = None # to be filled later
def compute_homographies(self, pinhole, scale):
+ # TODO: add type and value checkings
pinhole_ref = scale_pinhole(pinhole, scale)
if self.width is None:
self.width = pinhole_ref[..., 4]
@@ -26,7 +30,7 @@ class DepthWarper(nn.Module):
self._i_Hs_ref = homography_i_H_ref(pinhole_i, pinhole_ref)
def _compute_projection(self, x, y, invd):
- point = torch.FloatTensor([[x], [y], [1.0], [invd]])).to(x.device)
+ point = torch.FloatTensor([[x], [y], [1.0], [invd]]).to(x.device)
flow = torch.matmul(self._i_Hs_ref, point)
z = 1. / flow[:, :, 2]
x = (flow[:, :, 0] * z)
@@ -52,6 +56,7 @@ accurate sampling of the depth cost volume, per camera.
# compute grids
def warp(self, inv_depth_ref, roi=None):
+ # TODO: add type and value checkings
assert self._i_Hs_ref is not None, 'call compute_homographies'
if roi == None:
roi = (0, self.height, 0, self.width)
@@ -62,9 +67,6 @@ accurate sampling of the depth cost volume, per camera.
area = width * height
# take sub region
- #inv_depth_ref = inv_depth_ref.squeeze(0)[start_row:end_row, start_col:
- # end_col].contiguous()
- import pdb;pdb.set_trace()
inv_depth_ref = inv_depth_ref[..., start_row:end_row, \
start_col:end_col].contiguous()
@@ -79,15 +81,20 @@ accurate sampling of the depth cost volume, per camera.
xv = torch.ger(ones_x, x).view(area)
yv = torch.ger(y, ones_y).view(area)
- flow = [xv, yv, ones, inv_depth_ref.view(area)]
- flow = torch.stack(flow, 0)
+ grid = [xv, yv, ones, inv_depth_ref.view(area)]
+ grid = torch.stack(grid, 0)
+ batch_size = inv_depth_ref.shape[0]
+ grid = grid.unsqueeze(0).expand(batch_size, -1, -1)
- flow = torch.matmul(self._i_Hs_ref, flow)
+ flow = torch.matmul(self._i_Hs_ref, grid)
assert len(flow.shape) == 3, flow.shape
+ factor_x = (self.width - 1) / 2
+ factor_y = (self.height - 1) / 2
+
z = 1. / flow[:, 2] # Nx(H*W)
- x = (flow[:, 0] * z - self.width / 2) / (self.width / 2)
- y = (flow[:, 1] * z - self.height / 2) / (self.height / 2)
+ x = (flow[:, 0] * z - factor_x) / factor_x
+ y = (flow[:, 1] * z - factor_y) / factor_y
flow = torch.stack([x, y], 1) # Nx2x(H*W)
@@ -96,5 +103,5 @@ accurate sampling of the depth cost volume, per camera.
return flows.permute(0, 2, 3, 1) # NxHxWx2
def forward(self, inv_depth_ref, data):
- # be aware that grid_sample only supports float or double type
+ # TODO: add type and value checkings
return torch.nn.functional.grid_sample(data, self.warp(inv_depth_ref))
diff --git a/torchgeometry/functional.py b/torchgeometry/functional.py
index bf2e88835..5a2abecaa 100644
--- a/torchgeometry/functional.py
+++ b/torchgeometry/functional.py
@@ -191,8 +191,8 @@ def inv_pinhole_matrix(pinhole, eps=1e-6):
def scale_pinhole(pinhole, scale):
"""Scales the pinhole matrix from a pinhole model.
"""
- assert len(pinhole) == 2 and pinhole.shape[1] == 12, pinhole.shape
- assert len(scale) == 2 and scale.shape[1] == 1, scale.shape
+ assert len(pinhole.shape) == 2 and pinhole.shape[1] == 12, pinhole.shape
+ assert len(scale.shape) == 2 and scale.shape[1] == 1, scale.shape
pinhole_scaled = pinhole.clone()
pinhole_scaled[..., :6] = pinhole[..., :6] * scale
return pinhole_scaled