π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix linter errors
edgarriba7100c0c53a51
diff --git a/setup.py b/setup.pyindex 4db57f8d3..a49274ccd 100644--- a/setup.py+++ b/setup.py@@ -48,7 +48,7 @@ setup(tests_require=['pytest'],# Package info- packages=find_packages(exclude=('test',)),+ packages=find_packages(exclude=('test', 'examples',)),zip_safe=True,install_requires=requirements,diff --git a/test/test_depth_warper.py b/test/test_depth_warper.pyindex 8dd436382..3f3af9db9 100644--- a/test/test_depth_warper.py+++ b/test/test_depth_warper.py@@ -19,12 +19,23 @@ class Tester(unittest.TestCase):tx, ty, tz = 0., 0., 0.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 = 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, ty + offset, tz)+ pinhole_dst = utils.create_pinhole(+ fx,+ fy,+ cx,+ cy,+ height,+ width,+ rx,+ ry,+ rx,+ tx + offset,+ ty + offset,+ tz)pinhole_dst = pinhole_dst.expand(batch_size, -1)# create checkerboard@@ -34,7 +45,9 @@ class Tester(unittest.TestCase):# instantiate warper and compute relative homographieswarper = dgm.DepthWarper(pinhole_src, height, width)- warper.compute_homographies(pinhole_dst, scale=torch.ones(batch_size, 1))+ warper.compute_homographies(+ pinhole_dst, scale=torch.ones(+ batch_size, 1))# generate synthetic inverse depthinv_depth_src = torch.ones(batch_size, 1, height, width)@@ -43,8 +56,8 @@ class Tester(unittest.TestCase):patch_dst = warper(inv_depth_src, patch_src)# compute error- res = utils.check_equal_torch( \- patch_src[..., :-int(offset), :-int(offset)], \+ res = utils.check_equal_torch(+ patch_src[..., :-int(offset), :-int(offset)],patch_dst[..., int(offset):, int(offset):])self.assertTrue(res)diff --git a/test/test_functional.py b/test/test_functional.pyindex 733d44f31..79b920145 100644--- a/test/test_functional.py+++ b/test/test_functional.py@@ -195,11 +195,22 @@ class Tester(unittest.TestCase):tx, ty, tz = 0., 0., 0.offset_x = 10. # we will apply a 10units offset to `i` camera- pinhole_ref = utils.create_pinhole(fx, fy, cx, cy, \- image_height, image_width, rx, ry, rx, tx, ty, tz)-- pinhole_i = utils.create_pinhole(fx, fy, cx, cy, \- image_height, image_width, rx, ry, rx, tx + offset_x, ty, tz)+ pinhole_ref = utils.create_pinhole(+ fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)++ pinhole_i = utils.create_pinhole(+ fx,+ fy,+ cx,+ cy,+ image_height,+ image_width,+ rx,+ ry,+ rx,+ tx + offset_x,+ ty,+ tz)# compute homography from ref to ii_H_ref = dgm.homography_i_H_ref(pinhole_i, pinhole_ref)@@ -221,12 +232,23 @@ class Tester(unittest.TestCase):tx, ty, tz = 0., 0., 0.offset_x = 10. # we will apply a 10units offset to `i` camera- pinhole_ref = utils.create_pinhole(fx, fy, cx, cy, \- image_height, image_width, rx, ry, rx, tx, ty, tz)+ pinhole_ref = utils.create_pinhole(+ fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)pinhole_ref = utils.tensor_to_gradcheck_var(pinhole_ref) # to var- pinhole_i = utils.create_pinhole(fx, fy, cx, cy, \- image_height, image_width, rx, ry, rx, tx + offset_x, ty, tz)+ pinhole_i = utils.create_pinhole(+ fx,+ fy,+ cx,+ cy,+ image_height,+ image_width,+ rx,+ ry,+ rx,+ tx + offset_x,+ ty,+ tz)pinhole_i = utils.tensor_to_gradcheck_var(pinhole_ref) # to var# evaluate function gradientdiff --git a/test/test_transforms.py b/test/test_transforms.pyindex 814d60c7d..69f4ee27f 100644--- a/test/test_transforms.py+++ b/test/test_transforms.py@@ -59,15 +59,18 @@ class Tester(unittest.TestCase):self.assertTrue(res)def test_rotation_matrix_to_angle_axis_torch(self):- rmat = torch.tensor([[-0.30382753, -0.95095137, -0.05814062, 0.],- [-0.71581715, 0.26812278, -0.64476041, 0.],- [ 0.62872461, -0.15427791, -0.76217038, 0.],- [ 0. , 0. , 0. , 1.]])- rvec = torch.tensor([1.50485376, -2.10737739, 0.7214174 ])- self.assertTrue(check_equal_torch(tf.rotation_matrix_to_angle_axis(rmat), rvec))+ rmat = torch.tensor([[-0.30382753, -0.95095137, -0.05814062, 0.],+ [-0.71581715, 0.26812278, -0.64476041, 0.],+ [0.62872461, -0.15427791, -0.76217038, 0.],+ [0., 0., 0., 1.]])+ rvec = torch.tensor([1.50485376, -2.10737739, 0.7214174])+ self.assertTrue(+ check_equal_torch(+ tf.rotation_matrix_to_angle_axis(rmat),+ rvec))def test_rotation_matrix_to_angle_axis_gradcheck(self):- print('test_rotation_matrix_to_angle_axis_gradcheck to be implemented :)')+ print('test_rotation_matrix_to_angle_axis_gradcheck to be implemented :)') # noqaself.assertTrue(True)if __name__ == '__main__':diff --git a/torchgeometry/depth_warper.py b/torchgeometry/depth_warper.pyindex 635a41937..ccc120066 100644--- a/torchgeometry/depth_warper.py+++ b/torchgeometry/depth_warper.py@@ -8,6 +8,7 @@ from .functional import scale_pinhole, homography_i_H_refclass 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@@ -20,7 +21,9 @@ class DepthWarper(nn.Module):def compute_homographies(self, pinhole, scale=None):if scale is None:batch_size = pinhole.shape[0]- scale = torch.ones(batch_size, 1).to(pinhole.device).type_as(pinhole)+ scale = torch.ones(+ batch_size, 1).to(+ pinhole.device).type_as(pinhole)# TODO: add type and value checkingspinhole_ref = scale_pinhole(pinhole, scale)if self.width is None:@@ -30,7 +33,7 @@ class DepthWarper(nn.Module):self._pinhole_ref = pinhole_ref# scale pinholes_i and compute homographiespinhole_i = scale_pinhole(self._pinholes, scale)- self._i_Hs_ref = homography_i_H_ref(pinhole_i, pinhole_ref)+ 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)@@ -43,7 +46,9 @@ class DepthWarper(nn.Module):def compute_subpixel_step(self):"""This computes the required inverse depth step to achieve sub pixelaccurate sampling of the depth cost volume, per camera.- Szeliski, Richard, and Daniel Scharstein. "Symmetric sub-pixel stereo matching." European Conference on Computer Vision. Springer Berlin Heidelberg, 2002.+ Szeliski, Richard, and Daniel Scharstein.+ "Symmetric sub-pixel stereo matching." European Conference on Computer+ Vision. Springer Berlin Heidelberg, 2002."""delta_d = 0.01xy_m1 = self._compute_projection(self.width / 2, self.height / 2,@@ -52,16 +57,15 @@ accurate sampling of the depth cost volume, per camera.1.0 + delta_d)dx = torch.norm((xy_p1 - xy_m1), 2, dim=2) / 2.0dxdd = dx / (delta_d) # pixel*(1/meter)- return torch.min(- 0.5 /- dxdd) # half pixel sampling, we're interested in the min for all cameras+ # half pixel sampling, we're interested in the min for all cameras+ return torch.min(0.5 / dxdd)# compute gridsdef warp(self, inv_depth_ref, roi=None):# TODO: add type and value checkingsassert self._i_Hs_ref is not None, 'call compute_homographies'- if roi == None:+ if roi is None:roi = (0, int(self.height), 0, int(self.width))start_row, end_row, start_col, end_col = roiassert start_row < end_row@@ -70,9 +74,8 @@ accurate sampling of the depth cost volume, per camera.area = width * height# take sub region- #import ipdb;ipdb.set_trace()- inv_depth_ref = inv_depth_ref[..., start_row:end_row, \- start_col:end_col].contiguous()+ inv_depth_ref = inv_depth_ref[..., start_row:end_row,+ start_col:end_col].contiguous()device = inv_depth_ref.deviceones_x = torch.ones(height).to(device)diff --git a/torchgeometry/functional.py b/torchgeometry/functional.pyindex 5a2abecaa..7a68f14e5 100644--- a/torchgeometry/functional.py+++ b/torchgeometry/functional.py@@ -170,8 +170,10 @@ def pinhole_matrix(pinhole):k = torch.eye(4, device=pinhole.device, dtype=pinhole.dtype)k = k.view(1, 4, 4).repeat(pinhole.shape[0], 1, 1) # Nx4x4# fill output with pinhole values- k[..., 0, 0] = fx; k[..., 0, 2] = cx- k[..., 1, 1] = fy; k[..., 1, 2] = cy+ k[..., 0, 0] = fx+ k[..., 0, 2] = cx+ k[..., 1, 1] = fy+ k[..., 1, 2] = cyreturn k@@ -183,10 +185,12 @@ def inv_pinhole_matrix(pinhole, eps=1e-6):k = torch.eye(4, device=pinhole.device, dtype=pinhole.dtype)k = k.view(1, 4, 4).repeat(pinhole.shape[0], 1, 1) # Nx4x4# fill output with inverse values- k[..., 0, 0] = 1. / (fx + eps); k[..., 0, 2] = -1. * cx / (fx + eps)- k[..., 1, 1] = 1. / (fy + eps); k[..., 1, 2] = -1. * cy / (fy + eps)+ k[..., 0, 0] = 1. / (fx + eps)+ k[..., 1, 1] = 1. / (fy + eps)+ k[..., 0, 2] = -1. * cx / (fx + eps)+ k[..., 1, 2] = -1. * cy / (fy + eps)return k-+def scale_pinhole(pinhole, scale):"""Scales the pinhole matrix from a pinhole model.@@ -215,5 +219,8 @@ def homography_i_H_ref(pinhole_i, pinhole_ref):i_pose_base = get_optical_pose_base(pinhole_i)ref_pose_base = get_optical_pose_base(pinhole_ref)i_pose_ref = torch.matmul(i_pose_base, inverse_pose(ref_pose_base))- return torch.matmul(pinhole_matrix(pinhole_i), \- torch.matmul(i_pose_ref, inv_pinhole_matrix(pinhole_ref)))+ return torch.matmul(+ pinhole_matrix(pinhole_i),+ torch.matmul(+ i_pose_ref,+ inv_pinhole_matrix(pinhole_ref)))diff --git a/torchgeometry/transforms/__init__.py b/torchgeometry/transforms/__init__.pyindex 597808231..85557dad4 100644--- a/torchgeometry/transforms/__init__.py+++ b/torchgeometry/transforms/__init__.py@@ -1,4 +1,5 @@-from .functional import angle_axis_to_rotation_matrix, rtvec_to_pose, rotation_matrix_to_angle_axis+from .functional import angle_axis_to_rotation_matrix, rtvec_to_pose+from .functional import rotation_matrix_to_angle_axis__all__ = ['angle_axis_to_rotation_matrix',diff --git a/torchgeometry/transforms/functional.py b/torchgeometry/transforms/functional.pyindex 395d8daa2..92d6318b9 100644--- a/torchgeometry/transforms/functional.py+++ b/torchgeometry/transforms/functional.py@@ -91,8 +91,8 @@ def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):# create mask to handle both casesmask = (theta2 > eps).view(-1, 1, 1).to(theta2.device)- mask_pos = (mask == True).type_as(theta2)- mask_neg = (mask == False).type_as(theta2)+ mask_pos = (mask).type_as(theta2)+ mask_neg = (mask == False).type_as(theta2) # noqa# create output pose matrixbatch_size = angle_axis.shape[0]@@ -106,13 +106,15 @@ def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):def angle_axis_to_rotation_matrix(angle_axis):if isinstance(angle_axis, np.ndarray):- return angle_axis_to_rotation_matrix_numpy(angle_axis)+ return angle_axis_to_rotation_matrix_numpy(angle_axis)elif isinstance(angle_axis, torch.Tensor):- if not (len(angle_axis.shape) == 2 and angle_axis.shape[1] == 3):- raise ValueError("Input must be a two dimensional torch.Tensor.")- return angle_axis_to_rotation_matrix_torch(angle_axis)+ if not (len(angle_axis.shape) == 2 and angle_axis.shape[1] == 3):+ raise ValueError("Input must be a two dimensional torch.Tensor.")+ return angle_axis_to_rotation_matrix_torch(angle_axis)else:- raise NotImplementedError('Not suported type {}'.format(type(angle_axis)))+ raise NotImplementedError(+ 'Not suported type {}'.format(+ type(angle_axis)))def rtvec_to_pose(rtvec):@@ -138,7 +140,8 @@ def rotation_matrix_to_quaternion(rotation_matrix):Convert 4x4 rotation matrix to 4d quaternion vector'''quaternion = torch.zeros(4)- trace = rotation_matrix[0,0] + rotation_matrix[1,1] + rotation_matrix[2,2]+ trace = rotation_matrix[0, 0] + \+ rotation_matrix[1, 1] + rotation_matrix[2, 2]if trace >= 0.0:t = torch.sqrt(trace + 1.0)quaternion[0] = 0.5 * t@@ -157,7 +160,7 @@ def rotation_matrix_to_quaternion(rotation_matrix):j = (i + 1) % 3k = (j + 1) % 3t = torch.sqrt(rotation_matrix[i, i] - rotation_matrix[j, j] -- rotation_matrix[k, k] + 1.0)+ rotation_matrix[k, k] + 1.0)quaternion[i + 1] = 0.5 * tt = 0.5 / tquaternion[0] = (rotation_matrix[k, j] - rotation_matrix[j, k]) * t