π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix warper.compute_subpixel_step and _compute_projection to not crash
edgarriba65c302414ebb
diff --git a/test/test_depth_warper.py b/test/test_depth_warper.pyindex e4adde536..0d248a931 100644--- a/test/test_depth_warper.py+++ b/test/test_depth_warper.py@@ -1,4 +1,4 @@-import unittest+import pytestimport torchimport torchgeometry as tgm@@ -7,66 +7,68 @@ from torch.autograd import gradcheckimport utils # test utils-class Tester(unittest.TestCase):+def test_depth_warper():+ # generate input data+ batch_size = 1+ height, width = 8, 8+ cx, cy = width / 2, height / 2+ fx, fy = 1., 1.+ rx, ry, rz = 0., 0., 0.+ tx, ty, tz = 0., 0., 0.+ offset = 1. # we will apply a 1unit offset to `i` camera- def test_depth_warper(self):- # generate input data- batch_size = 1- height, width = 8, 8- cx, cy = width / 2, height / 2- fx, fy = 1., 1.- rx, ry, rz = 0., 0., 0.- tx, ty, tz = 0., 0., 0.- offset = 1. # we will apply a 1unit offset to `i` camera+ pinhole_ref = utils.create_pinhole(+ fx, fy, cx, cy, height, width, rx, ry, rx, tx, ty, tz)+ pinhole_ref = pinhole_ref.expand(batch_size, -1)- pinhole_ref = utils.create_pinhole(- fx, fy, cx, cy, height, width, rx, ry, rx, tx, ty, tz)- pinhole_ref = pinhole_ref.expand(batch_size, -1)+ pinhole_i = utils.create_pinhole(+ fx,+ fy,+ cx,+ cy,+ height,+ width,+ rx,+ ry,+ rx,+ tx + offset,+ ty + offset,+ tz)+ pinhole_i = pinhole_i.expand(batch_size, -1)- pinhole_i = utils.create_pinhole(- fx,- fy,- cx,- cy,- height,- width,- rx,- ry,- rx,- tx + offset,- ty + offset,- tz)- pinhole_i = pinhole_i.expand(batch_size, -1)+ # create checkerboard+ board = utils.create_checkerboard(height, width, 4)+ patch_i = torch.from_numpy(board).view(+ 1, 1, height, width).expand(batch_size, 1, height, width)- # create checkerboard- board = utils.create_checkerboard(height, width, 4)- patch_i = torch.from_numpy(board).view(- 1, 1, height, width).expand(batch_size, 1, height, width)+ # instantiate warper and compute relative homographies+ warper = tgm.DepthWarper(pinhole_i)+ warper.compute_homographies(+ pinhole_ref, scale=torch.ones(+ batch_size))- # instantiate warper and compute relative homographies- warper = tgm.DepthWarper(pinhole_i)- warper.compute_homographies(- pinhole_ref, scale=torch.ones(- batch_size))+ # generate synthetic inverse depth+ inv_depth_ref = torch.ones(batch_size, 1, height, width)- # generate synthetic inverse depth- inv_depth_ref = torch.ones(batch_size, 1, height, width)+ # warpd source patch by depth+ patch_ref = warper(inv_depth_ref, patch_i)- # warpd source patch by depth- patch_ref = warper(inv_depth_ref, patch_i)+ # compute error+ assert utils.check_equal_torch(+ patch_ref[..., :int(height - offset), :int(width - offset)],+ patch_i[..., int(offset):, int(offset):])- # compute error- res = utils.check_equal_torch(- patch_ref[..., :int(height - offset), :int(width - offset)],- patch_i[..., int(offset):, int(offset):])- self.assertTrue(res)+ # test functional+ patch_ref_functional = tgm.depth_warp(pinhole_i, pinhole_ref,+ inv_depth_ref, patch_i)+ assert utils.check_equal_torch(patch_ref, patch_ref_functional)- # test functional- patch_ref_functional = tgm.depth_warp(pinhole_i, pinhole_ref,- inv_depth_ref, patch_i)- res = utils.check_equal_torch(patch_ref, patch_ref_functional)- self.assertTrue(res)+ # test compute_projection+ # TODO: this might need a better test+ xy_projected = warper._compute_projection(0.0, 0.0, 1.0)+ assert xy_projected.shape == (1, 2)--if __name__ == '__main__':- unittest.main()+ # test compute_subpixel_step+ # TODO: this might need a better test+ subpixel_step = warper.compute_subpixel_step()+ assert pytest.approx(subpixel_step.item(), 0.3536)diff --git a/torchgeometry/depth_warper.py b/torchgeometry/depth_warper.pyindex d52b147fb..5d38407fb 100644--- a/torchgeometry/depth_warper.py+++ b/torchgeometry/depth_warper.py@@ -53,12 +53,12 @@ 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)- flow = torch.matmul(self._i_Hs_ref, point)- z = 1. / flow[:, :, 2]- x = (flow[:, :, 0] * z)- y = (flow[:, :, 1] * z)- return torch.stack([x, y], 1)+ point = torch.FloatTensor([[[x], [y], [1.0], [invd]]])+ flow = torch.matmul(self._i_Hs_ref, point.to(self._i_Hs_ref.device))+ z = 1. / flow[:, 2]+ x = (flow[:, 0] * z)+ y = (flow[:, 1] * z)+ return torch.cat([x, y], 1)def compute_subpixel_step(self):"""This computes the required inverse depth step to achieve sub pixel@@ -73,7 +73,7 @@ class DepthWarper(nn.Module):1.0 - delta_d)xy_p1 = self._compute_projection(self.width / 2, self.height / 2,1.0 + delta_d)- dx = torch.norm((xy_p1 - xy_m1), 2, dim=2) / 2.0+ dx = torch.norm((xy_p1 - xy_m1), 2, dim=-1) / 2.0dxdd = dx / (delta_d) # pixel*(1/meter)# half pixel sampling, we're interested in the min for all camerasreturn torch.min(0.5 / dxdd)