Ejemplo n.º 1
0
    def test_point_to_plane_gradICP_raises_value_error(self):
        dtype = torch.float32
        device = default_to_cpu_if_no_gpu("cuda")
        rad = 0.2
        transform = torch.tensor(
            [
                [np.cos(rad), -np.sin(rad), 0.0, 0.05],
                [np.sin(rad), np.cos(rad), 0.0, 0.03],
                [0.0, 0.0, 1.0, 0.01],
                [0.0, 0.0, 0.0, 1.0],
            ],
            device=device,
            dtype=dtype,
        )
        src_pc = torch.rand((1, 5, 3), dtype=dtype, device=device)
        tgt_pc = torch.rand((1, 8, 3), dtype=dtype, device=device)
        tgt_normals = torch.rand((1, 8, 3), dtype=dtype, device=device)
        initial_transform = torch.eye(4, device=device)

        with pytest.raises(ValueError):
            point_to_plane_gradICP(src_pc.unsqueeze(0), tgt_pc, tgt_normals,
                                   initial_transform)
        with pytest.raises(ValueError):
            point_to_plane_gradICP(src_pc, tgt_pc.unsqueeze(0), tgt_normals,
                                   initial_transform)
        with pytest.raises(ValueError):
            point_to_plane_gradICP(src_pc, tgt_pc, tgt_normals.unsqueeze(0),
                                   initial_transform)
        with pytest.raises(ValueError):
            point_to_plane_gradICP(src_pc, tgt_pc, tgt_normals,
                                   initial_transform.unsqueeze(0))
        with pytest.raises(ValueError):
            point_to_plane_gradICP(src_pc, tgt_pc, tgt_normals,
                                   initial_transform[:3, :3])
Ejemplo n.º 2
0
    def test_unhomogenize_points(self, device):
        device = default_to_cpu_if_no_gpu(device)
        # Copied Kornia testcase (and added a few).
        # Points to unhomogenize
        pts = torch.tensor(
            [
                [1.0, 2.0, 1.0],
                [0.0, 1.0, 2.0],
                [2.0, 1.0, 0.0],
                [-1.0, -2.0, -1.0],
                [0.0, 1.0, -2.0],
                [0.0, 0.0, 0.0],
                [1.0, 0.0, 0.0],
            ],
            device=device,
        )

        pts_gt = torch.tensor(
            [
                [1.0, 2.0],
                [0.0, 0.5],
                [2.0, 1.0],
                [1.0, 2.0],
                [0.0, -0.5],
                [0.0, 0.0],
                [1.0, 0.0],
            ],
            device=device,
        )

        pts_pred = gs.unhomogenize_points(pts)
        assert_allclose(pts_pred, pts_gt)
Ejemplo n.º 3
0
    def test_solve_linear_system_raises_type_error(self):
        device = default_to_cpu_if_no_gpu("cuda")
        A = torch.tensor(
            [
                [0.1, 0.7, 0.3, 0.6],
                [0.5, 0.2, 0.4, 0.8],
                [0.3, 0.9, 0.5, 0.2],
                [0.8, 0.2, 0.3, 0.4],
                [0.7, 0.9, 0.3, 0.8],
            ],
            dtype=torch.float32,
            device=device,
        )
        b = torch.tensor(
            [
                [0.7],
                [0.2],
                [0.9],
                [0.2],
                [0.9],
            ],
            dtype=torch.float32,
            device=device,
        )
        damp = torch.tensor(1e-8, device=device)

        with pytest.raises(TypeError):
            x = solve_linear_system(np.ones((5, 4)), b, damp)
        with pytest.raises(TypeError):
            x = solve_linear_system(A, np.ones((5, 1)), damp)
        with pytest.raises(TypeError):
            x = solve_linear_system(A, b, 5)
Ejemplo n.º 4
0
    def test_solve_linear_system(self, device):
        device = default_to_cpu_if_no_gpu(device)
        A = torch.tensor(
            [
                [0.1, 0.7, 0.3, 0.6],
                [0.5, 0.2, 0.4, 0.8],
                [0.3, 0.9, 0.5, 0.2],
                [0.8, 0.2, 0.3, 0.4],
                [0.7, 0.9, 0.3, 0.8],
            ],
            dtype=torch.float32,
            device=device,
        )
        b = torch.tensor(
            [
                [0.7],
                [0.2],
                [0.9],
                [0.2],
                [0.9],
            ],
            dtype=torch.float32,
            device=device,
        )
        damp = 1e-8

        x = solve_linear_system(A, b, damp)
        res = A @ x
        assert res.shape == b.shape
        assert_allclose(res, b)
Ejemplo n.º 5
0
 def test_groundtruth_raises_value_error(self, device):
     device = default_to_cpu_if_no_gpu(device)
     channels_first = False
     rgbdimages = TestGroundTruth.init_rgbdimages(
         device=device, channels_first=channels_first)
     nopose_rgbdimages = TestGroundTruth.init_rgbdimages(device=device)
     nopose_rgbdimages.poses = None
     batch_rgbdimages = RGBDImages(
         rgbdimages.rgb_image.repeat(2, 1, 1, 1, 1),
         rgbdimages.depth_image.repeat(2, 1, 1, 1, 1),
         rgbdimages.intrinsics.repeat(2, 1, 1, 1),
         rgbdimages.poses.repeat(2, 1, 1, 1),
         channels_first=channels_first,
     ).to(device)
     tensor = torch.rand(1, 1, 4, 4)
     odom = GroundTruthOdometryProvider()
     with pytest.raises(ValueError):
         odom = odom.provide(rgbdimages[:, 0], nopose_rgbdimages[:, 1])
     with pytest.raises(ValueError):
         odom = odom.provide(nopose_rgbdimages[:, 0], rgbdimages[:, 1])
     with pytest.raises(ValueError):
         odom = odom.provide(rgbdimages[:, 0], rgbdimages)
     with pytest.raises(ValueError):
         odom = odom.provide(rgbdimages, rgbdimages[:, 1])
     with pytest.raises(ValueError):
         odom = odom.provide(rgbdimages, batch_rgbdimages)
Ejemplo n.º 6
0
 def test_cases_3_and_6(self, device, lastdim):
     device = default_to_cpu_if_no_gpu(device)
     pixel_coords = torch.rand(2, 10, lastdim, device=device)
     intrinsics_inv = torch.rand(2, 3, 3, device=device)
     depths = torch.rand(2, 10, device=device)
     cam_coords = gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     assert cam_coords.shape == (2, 10, 3)
Ejemplo n.º 7
0
    def test_indexing(self):
        device = default_to_cpu_if_no_gpu("cuda:0")

        for channels_first in [False, True]:
            # rgb_image
            rgbdimages, *_ = TestRGBDImages.init_rgbdimages(
                channels_first=channels_first, use_poses=True, device=device)
            self.assertClose(rgbdimages.rgb_image, rgbdimages.rgb_image)
            self.assertClose(rgbdimages[0:2, 0:2].rgb_image,
                             rgbdimages.rgb_image[0:2, 0:2])

            self.assertClose(
                rgbdimages[1, 1].rgb_image.squeeze(0).squeeze(0),
                rgbdimages.rgb_image[1, 1],
            )

            # depth_image
            self.assertClose(rgbdimages.depth_image, rgbdimages.depth_image)
            self.assertClose(rgbdimages[0:2, 0:2].depth_image,
                             rgbdimages.depth_image[0:2, 0:2])
            self.assertClose(
                rgbdimages[1, 1].depth_image.squeeze(0).squeeze(0),
                rgbdimages.depth_image[1, 1],
            )

            # intrinsics
            self.assertClose(rgbdimages.intrinsics, rgbdimages.intrinsics)
            self.assertClose(rgbdimages[0:2, 0:1].intrinsics,
                             rgbdimages.intrinsics[0:2, 0:1])
            self.assertClose(
                rgbdimages[1, 0].intrinsics.squeeze(0).squeeze(0),
                rgbdimages.intrinsics[1, 0],
            )

            # poses
            self.assertClose(rgbdimages.poses, rgbdimages.poses)
            self.assertClose(rgbdimages[0:2, 0:2].poses, rgbdimages.poses[0:2,
                                                                          0:2])
            self.assertClose(rgbdimages[1, 1].poses.squeeze(0).squeeze(0),
                             rgbdimages.poses[1, 1])

            # vertex_map
            self.assertClose(rgbdimages.vertex_map, rgbdimages.vertex_map)
            self.assertClose(rgbdimages[0:2, 0:2].vertex_map,
                             rgbdimages.vertex_map[0:2, 0:2])
            self.assertClose(
                rgbdimages[1, 1].vertex_map.squeeze(0).squeeze(0),
                rgbdimages.vertex_map[1, 1],
            )

            # normal_map
            self.assertClose(rgbdimages.normal_map, rgbdimages.normal_map)
            self.assertClose(rgbdimages[0:2, 0:2].normal_map,
                             rgbdimages.normal_map[0:2, 0:2])
            self.assertClose(
                rgbdimages[1, 1].normal_map.squeeze(0).squeeze(0),
                rgbdimages.normal_map[1, 1],
            )
Ejemplo n.º 8
0
    def test_vertex_map(self):
        device = default_to_cpu_if_no_gpu("cuda:0")

        scriptdir = os.path.dirname(os.path.realpath(__file__))
        gt_vmap = np.load(
            os.path.join(scriptdir, "../data/msrd_b2s3/vertex_map.npy"))
        gt_global_vmap = np.load(
            os.path.join(scriptdir, "../data/msrd_b2s3/global_vertex_map.npy"))

        for use_poses in [False, True]:
            for channels_first in [False, True]:
                rgbdimages, *_ = TestRGBDImages.init_rgbdimages(
                    channels_first=channels_first,
                    use_poses=use_poses,
                    device=device)
                vertex_map = rgbdimages.vertex_map
                global_vertex_map = rgbdimages.global_vertex_map
                depth_image = rgbdimages.depth_image
                intrinsics = rgbdimages.intrinsics
                self.assertEqual(vertex_map.ndim, 5)
                if channels_first:
                    vertex_map = vertex_map.permute(0, 1, 3, 4, 2).contiguous()
                    global_vertex_map = global_vertex_map.permute(
                        0, 1, 3, 4, 2).contiguous()
                    depth_image = depth_image.permute(0, 1, 3, 4,
                                                      2).contiguous()
                self.assertEqual(vertex_map.shape, (2, 3, 120, 160, 3))
                self.assertEqual(global_vertex_map.shape, (2, 3, 120, 160, 3))
                self.assertEqual(depth_image.shape, (2, 3, 120, 160, 1))

                for b in range(2):
                    for s in range(3):
                        vmap = vertex_map[b, s]
                        dmap = depth_image[b, s]
                        K = intrinsics[b, 0]
                        test_unproj_res = project_points(vmap, K)
                        meshgrid = (create_meshgrid(
                            120, 160, False).squeeze(0).to(device))
                        meshgrid = torch.cat(
                            [
                                meshgrid[..., 1:],
                                meshgrid[..., 0:1],
                            ],
                            -1,
                        )
                        correct_unproj_res = meshgrid * (dmap != 0).float()
                        # self.assertClose() fails here, probably because not close enough?
                        assert (test_unproj_res -
                                correct_unproj_res).abs().max() < 1e-4

                assert ((gt_vmap - vertex_map.cpu().numpy())**2).sum() < 1e-2
                if use_poses:
                    assert ((gt_global_vmap - global_vertex_map.cpu().numpy())
                            **2).sum() < 1e-2
                else:
                    assert ((gt_vmap - global_vertex_map.cpu().numpy())**
                            2).sum() < 1e-2
Ejemplo n.º 9
0
 def test_groundtruth_raises_type_error(self, device):
     device = default_to_cpu_if_no_gpu(device)
     rgbdimages = TestGroundTruth.init_rgbdimages(device=device)
     tensor = torch.rand(1, 1, 4, 4)
     odom = GroundTruthOdometryProvider()
     with pytest.raises(TypeError):
         odom = odom.provide(rgbdimages[:, 0], tensor)
     with pytest.raises(TypeError):
         odom = odom.provide(tensor, rgbdimages[:, 0])
Ejemplo n.º 10
0
    def test_groundtruth_provide(self, device):
        device = default_to_cpu_if_no_gpu(device)
        rgbdimages = TestGroundTruth.init_rgbdimages(device=device)
        odom = GroundTruthOdometryProvider()
        t = 0
        odom_transform = odom.provide(rgbdimages[:, t], rgbdimages[:, t + 1])
        new_pose = rgbdimages[:, t].poses.squeeze() @ odom_transform.squeeze()

        assert odom_transform.shape == rgbdimages[:, t + 1].poses.shape
        assert_allclose(new_pose.squeeze(), rgbdimages[:,
                                                       t + 1].poses.squeeze())
Ejemplo n.º 11
0
    def test_output_shape(self, device, lastdim):
        device = default_to_cpu_if_no_gpu(device)
        rand_vals = torch.rand(10, 4, device=device)
        intrinsics = torch.zeros(10, lastdim, lastdim, device=device)
        intrinsics[..., 0, 0] = rand_vals[:, 0]  # fx
        intrinsics[..., 1, 1] = rand_vals[:, 1]  # fy
        intrinsics[..., 0, 2] = rand_vals[:, 2]  # cx
        intrinsics[..., 1, 2] = rand_vals[:, 3]  # cy
        intrinsics[..., 2, 2] = 1
        intrinsics[..., -1, -1] = 1
        test_res = gs.inverse_intrinsics(intrinsics)

        assert test_res.shape == intrinsics.shape
Ejemplo n.º 12
0
 def test_type_errors(self, device, lastdim):
     device = default_to_cpu_if_no_gpu(device)
     pixel_coords = [1, 2, 3]
     intrinsics_inv = [1, 2, 3]
     depths = [1, 2, 3]
     with pytest.raises(TypeError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     pixel_coords = torch.rand(2, 10, lastdim, device=device)
     with pytest.raises(TypeError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     intrinsics_inv = torch.rand(2, 3, 3, device=device)
     with pytest.raises(TypeError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
Ejemplo n.º 13
0
    def test_normal_map(self):
        device = default_to_cpu_if_no_gpu("cuda:0")

        def diff(x, y):
            # normals on gpu give slightly different values at some pixels
            return (((x - y)**2) < 1e-5).mean() > 0.99

        scriptdir = os.path.dirname(os.path.realpath(__file__))
        gt_nmap = np.load(
            os.path.join(scriptdir, "../data/msrd_b2s3/normal_map.npy"))
        gt_global_nmap = np.load(
            os.path.join(scriptdir, "../data/msrd_b2s3/global_normal_map.npy"))

        for use_poses in [False, True]:
            for channels_first in [False, True]:
                rgbdimages, *_ = TestRGBDImages.init_rgbdimages(
                    channels_first=channels_first,
                    use_poses=use_poses,
                    device=device)
                normal_map = rgbdimages.normal_map
                global_normal_map = rgbdimages.global_normal_map
                self.assertEqual(normal_map.ndim, 5)
                self.assertEqual(global_normal_map.ndim, 5)
                remove_missing = global_normal_map * rgbdimages.valid_depth_mask.to(
                    global_normal_map.dtype)
                assert ((global_normal_map - remove_missing)**
                        2).sum().item() < 1e-5
                if channels_first:
                    normal_map = normal_map.permute(0, 1, 3, 4, 2).contiguous()
                    global_normal_map = global_normal_map.permute(
                        0, 1, 3, 4, 2).contiguous()
                self.assertEqual(normal_map.shape, (2, 3, 120, 160, 3))
                self.assertEqual(global_normal_map.shape, (2, 3, 120, 160, 3))

                nmap = normal_map.detach().cpu().numpy()
                global_nmap = global_normal_map.detach().cpu().numpy()

                assert diff(gt_nmap, nmap)
                if use_poses:
                    # # visualize normals
                    # import matplotlib.pyplot as plt
                    # fig, ax = plt.subplots(2, 2)
                    # ax[0, 0].imshow((nmap[-1, -1] * 255).astype(np.uint8))
                    # ax[0, 1].imshow((gt_nmap[-1, -1] * 255).astype(np.uint8))
                    # ax[1, 0].imshow((global_nmap[-1, -1] * 255).astype(np.uint8))
                    # ax[1, 1].imshow((gt_global_nmap[-1, -1] * 255).astype(np.uint8))
                    # plt.show()

                    assert diff(gt_global_nmap, global_nmap)
                else:
                    assert diff(gt_nmap, global_nmap)
Ejemplo n.º 14
0
 def test_value_errors(self, device):
     device = default_to_cpu_if_no_gpu(device)
     intrinsics = torch.rand(3, device=device)
     with pytest.raises(ValueError):
         gs.inverse_intrinsics(intrinsics)
     intrinsics = torch.rand(2, 3, device=device)
     with pytest.raises(ValueError):
         gs.inverse_intrinsics(intrinsics)
     intrinsics = torch.rand(3, 4, device=device)
     with pytest.raises(ValueError):
         gs.inverse_intrinsics(intrinsics)
     intrinsics = torch.rand(5, 3, 4, device=device)
     with pytest.raises(ValueError):
         gs.inverse_intrinsics(intrinsics)
Ejemplo n.º 15
0
 def test_simple(self):
     device = default_to_cpu_if_no_gpu("cuda:0")
     args = [(True, True), (True, False), (False, True), (False, False)]
     for arg in args:
         res_tuple = TestRGBDImages.init_rgbdimages(use_poses=arg[0],
                                                    channels_first=arg[1],
                                                    device=device)
         rgbdimages, colors, depths, intrinsics, poses = res_tuple
         self.assertEqual(rgbdimages.shape, (2, 3, 120, 160))
         self.assertEqual(colors.shape, rgbdimages.rgb_image.shape)
         self.assertEqual(depths.shape, rgbdimages.depth_image.shape)
         self.assertEqual(intrinsics.shape, rgbdimages.intrinsics.shape)
         self.assertEqual(colors.shape, rgbdimages.vertex_map.shape)
         self.assertEqual(colors.shape, rgbdimages.normal_map.shape)
Ejemplo n.º 16
0
    def test_output_values(self, device, lastdim):
        device = default_to_cpu_if_no_gpu(device)
        rand_vals = torch.rand(4, device=device)
        intrinsics = torch.zeros(lastdim, lastdim, device=device)
        intrinsics[0, 0] = rand_vals[0]  # fx
        intrinsics[1, 1] = rand_vals[1]  # fy
        intrinsics[0, 2] = rand_vals[2]  # cx
        intrinsics[1, 2] = rand_vals[3]  # cy
        intrinsics[2, 2] = 1
        intrinsics[-1, -1] = 1
        test_res = gs.inverse_intrinsics(intrinsics)
        correct_res = torch.inverse(intrinsics)

        assert test_res.shape == intrinsics.shape
        assert ((test_res - correct_res).abs().sum() /
                correct_res.abs().sum()).item() < 1e-2
Ejemplo n.º 17
0
    def test_gauss_newton_raises_type_error(self):
        device = default_to_cpu_if_no_gpu("cuda")
        src_pc = torch.tensor(
            [
                [0.1, 0.7, 0.3],
                [0.5, 0.2, 0.4],
                [0.3, 0.9, 0.5],
                [0.8, 0.2, 0.3],
                [0.7, 0.9, 0.3],
            ],
            dtype=torch.float32,
            device=device,
        )
        rad = 0.2
        transform = torch.tensor(
            [
                [np.cos(rad), -np.sin(rad), 0.0, 0.05],
                [np.sin(rad), np.cos(rad), 0.0, 0.03],
                [0.0, 0.0, 1.0, 0.01],
                [0.0, 0.0, 0.0, 1.0],
            ],
            device=device,
            dtype=src_pc.dtype,
        )
        tgt_pc = ((transform[:3, :3] @ src_pc.T) + transform[:-1, -1:]).T
        tgt_normals = src_pc.clone()
        src_pc = src_pc.unsqueeze(0)
        tgt_pc = tgt_pc.unsqueeze(0)
        tgt_normals = tgt_normals.unsqueeze(0)
        dist_thresh = 0.2

        with pytest.raises(TypeError):
            x = gauss_newton_solve(src_pc.cpu().detach().numpy(), tgt_pc,
                                   tgt_normals, dist_thresh)
        with pytest.raises(TypeError):
            x = gauss_newton_solve(src_pc,
                                   tgt_pc.cpu().detach().numpy(), tgt_normals,
                                   dist_thresh)
        with pytest.raises(TypeError):
            x = gauss_newton_solve(src_pc, tgt_pc,
                                   tgt_normals.cpu().detach().numpy(),
                                   dist_thresh)
        with pytest.raises(TypeError):
            x = gauss_newton_solve(src_pc, tgt_pc, tgt_normals,
                                   torch.tensor(0.2, device=device))
Ejemplo n.º 18
0
    def test_pointclouds_from_rgbdimages(self, channels_first, device):
        device = default_to_cpu_if_no_gpu(device)
        colors, depths, intrinsics, poses = load_test_data(channels_first)
        rgbdimages = RGBDImages(
            colors.to(device),
            depths.to(device),
            intrinsics.to(device),
            poses.to(device),
            channels_first=channels_first,
        )

        pointclouds = pointclouds_from_rgbdimages(rgbdimages[:, 0]).to(device)
        projected_pointclouds = pointclouds.pinhole_projection(
            intrinsics.to(device).squeeze(1))
        proj0 = projected_pointclouds.points_list[0][..., :-1]
        meshgrid = (create_meshgrid(rgbdimages.shape[2], rgbdimages.shape[3],
                                    False).to(device).squeeze(0))
        meshgrid = torch.cat(
            [
                meshgrid[..., 1:],
                meshgrid[..., 0:1],
            ],
            -1,
        )
        groundtruth = meshgrid[rgbdimages[0, 0].valid_depth_mask.squeeze()]

        assert_allclose(proj0.round().float(), groundtruth.float())

        # without filtering missing depths
        pointclouds2 = pointclouds_from_rgbdimages(
            rgbdimages[:, 0], filter_missing_depths=False).to(device)

        for b in range(len(pointclouds)):
            filtered_points = pointclouds.points_list[b]
            unfiltered_points = pointclouds2.points_list[b]
            m = 0
            for n in range(len(filtered_points)):
                while (not ((filtered_points[n] - unfiltered_points[m])**
                            2).sum() < 1e-12):
                    m += 1
                assert ((filtered_points[n] - unfiltered_points[m])**
                        2).sum() < 1e-12
                m += 1
Ejemplo n.º 19
0
    def test_output_values_more_dims(self, device, lastdim):
        device = default_to_cpu_if_no_gpu(device)
        rand_vals = torch.rand(5, 10, 4, device=device)
        intrinsics = torch.zeros(5, 10, lastdim, lastdim, device=device)
        intrinsics[..., 0, 0] = rand_vals[..., 0]  # fx
        intrinsics[..., 1, 1] = rand_vals[..., 1]  # fy
        intrinsics[..., 0, 2] = rand_vals[..., 2]  # cx
        intrinsics[..., 1, 2] = rand_vals[..., 3]  # cy
        intrinsics[..., 2, 2] = 1
        intrinsics[..., -1, -1] = 1
        test_res = gs.inverse_intrinsics(intrinsics)
        correct_res = []
        for b in range(5):
            res = [torch.inverse(intrinsics[b, s]) for s in range(10)]
            correct_res.append(torch.stack(res, 0))
        correct_res = torch.stack(correct_res, 0)

        assert test_res.shape == intrinsics.shape
        assert ((test_res - correct_res).abs().sum() /
                correct_res.abs().sum()).item() < 1e-2
Ejemplo n.º 20
0
    def test_downsample_rgbdimages_raises_type_error(self):
        device = default_to_cpu_if_no_gpu("cuda")
        image = (torch.tensor(
            [
                [
                    [0.0, 0.0, 0.0],
                    [1.0, 1.0, 1.0],
                    [2.0, 2.0, 2.0],
                    [3.0, 3.0, 3.0],
                ],
                [
                    [4.0, 4.0, 4.0],
                    [5.0, 5.0, 5.0],
                    [6.0, 6.0, 6.0],
                    [7.0, 7.0, 7.0],
                ],
                [
                    [8.0, 8.0, 8.0],
                    [9.0, 9.0, 9.0],
                    [10.0, 10.0, 10.0],
                    [11.0, 11.0, 11.0],
                ],
            ],
            device=device,
            dtype=torch.float,
        ).unsqueeze(0).unsqueeze(0))

        depth = torch.ones_like(image[..., :1])
        intrinsics = torch.eye(4).unsqueeze(0).unsqueeze(0).to(device)
        poses = torch.eye(4).unsqueeze(0).unsqueeze(0).to(device)
        rgbdimages = RGBDImages(image,
                                depth,
                                intrinsics,
                                poses,
                                channels_first=False).to(device)
        ds_ratio = 2
        ds_pointclouds = downsample_rgbdimages(rgbdimages, ds_ratio)
        with pytest.raises(TypeError):
            downsample_rgbdimages("a", ds_ratio)
        with pytest.raises(TypeError):
            downsample_rgbdimages(rgbdimages, "a")
Ejemplo n.º 21
0
    def test_raises_errors(self, device):
        device = default_to_cpu_if_no_gpu(device)
        channels_first = False
        colors, depths, intrinsics, poses = load_test_data(channels_first)
        rgbdimages = RGBDImages(
            colors.to(device),
            depths.to(device),
            intrinsics.to(device),
            poses.to(device),
            channels_first=channels_first,
        )  # .to(device)

        sigma = 0.6
        with pytest.raises(
                TypeError,
                match="Expected rgbdimages to be of type gradslam.RGBDImages"):
            pointclouds = pointclouds_from_rgbdimages(depths).to(device)

        with pytest.raises(
                ValueError,
                match="Expected rgbdimages to have sequence length of 1"):
            pointclouds = pointclouds_from_rgbdimages(rgbdimages).to(device)
Ejemplo n.º 22
0
    def test_homogenize_points(self, device):

        device = default_to_cpu_if_no_gpu(device)

        # Points to homogenize
        pts = torch.tensor(
            [[1.0, 2.0, 3.0], [3.0, 2.0, 1.0], [-1.0, 0.0, 1.0],
             [0.0, 0.0, 0.0]],
            device=device,
        )

        pts_gt = torch.tensor(
            [
                [1.0, 2.0, 3.0, 1.0],
                [3.0, 2.0, 1.0, 1.0],
                [-1.0, 0.0, 1.0, 1.0],
                [0.0, 0.0, 0.0, 1.0],
            ],
            device=device,
        )

        pts_pred = gs.homogenize_points(pts)
        assert_allclose(pts_pred, pts_gt)
Ejemplo n.º 23
0
 def test_downsample_pointclouds_raises_value_error(self):
     device = default_to_cpu_if_no_gpu("cuda")
     points = torch.tensor(
         [
             [5.0, 5.0, 5.0],
             [3.0, 3.0, 3.0],
             [1.0, 2.0, 3.0],
             [3.0, 2.0, 1.0],
             [1.0, 0.0, 1.0],
             [0.0, 0.0, 0.0],
         ],
         device=device,
         dtype=torch.float,
     ).unsqueeze(0)
     normals = points * -1
     pc2im_bnhw = torch.tensor(
         [
             [0, 0, 0, 0],
             [0, 1, 4, 2],
             [0, 2, 3, 1],
             [0, 3, 0, 3],
             [0, 4, 3, 3],
             [0, 5, 3, 6],
         ],
         device=device,
         dtype=torch.int64,
     )
     pointclouds = Pointclouds(points, normals)
     ds_ratio = 3
     ds_pointclouds = downsample_pointclouds(pointclouds, pc2im_bnhw,
                                             ds_ratio)
     with pytest.raises(ValueError):
         downsample_pointclouds(pointclouds, pc2im_bnhw.unsqueeze(1),
                                ds_ratio)
     with pytest.raises(ValueError):
         downsample_pointclouds(pointclouds, pc2im_bnhw.repeat(1, 2),
                                ds_ratio)
Ejemplo n.º 24
0
    def test_solve_linear_system_raises_value_error(self):
        device = default_to_cpu_if_no_gpu("cuda")
        A = torch.tensor(
            [
                [0.1, 0.7, 0.3, 0.6],
                [0.5, 0.2, 0.4, 0.8],
                [0.3, 0.9, 0.5, 0.2],
                [0.8, 0.2, 0.3, 0.4],
                [0.7, 0.9, 0.3, 0.8],
            ],
            dtype=torch.float32,
            device=device,
        )
        b = torch.tensor(
            [
                [0.7],
                [0.2],
                [0.9],
                [0.2],
                [0.9],
            ],
            dtype=torch.float32,
            device=device,
        )
        damp = torch.tensor(1e-8, device=device)

        with pytest.raises(ValueError):
            x = solve_linear_system(A.unsqueeze(0), b, damp)
        with pytest.raises(ValueError):
            x = solve_linear_system(A, b.unsqueeze(0), damp)
        with pytest.raises(ValueError):
            x = solve_linear_system(A, b, damp.unsqueeze(0))
        with pytest.raises(ValueError):
            x = solve_linear_system(A, b.repeat(1, 2), damp)
        with pytest.raises(ValueError):
            x = solve_linear_system(A, b.repeat(2, 1), damp)
Ejemplo n.º 25
0
 def test_value_errors(self, device):
     device = default_to_cpu_if_no_gpu(device)
     pixel_coords = torch.rand(2, device=device)
     intrinsics_inv = torch.rand(3, 3, device=device)
     depths = torch.rand(2, 10, device=device)
     with pytest.raises(ValueError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     pixel_coords = torch.rand(2, 3, device=device)
     intrinsics_inv = torch.rand(3, device=device)
     with pytest.raises(ValueError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     intrinsics_inv = torch.rand(3, 3, device=device)
     pixel_coords = torch.rand(2, 3, device=device)
     depths = torch.rand(1, device=device)
     with pytest.raises(ValueError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     pixel_coords = torch.rand(2, 1, 2, 3, device=device)
     intrinsics_inv = torch.rand(1, 3, 3, device=device)
     with pytest.raises(ValueError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
     pixel_coords = torch.rand(2, 1, 2, 3, device=device)
     intrinsics_inv = torch.rand(1, 3, 3, device=device)
     with pytest.raises(ValueError):
         gs.unproject_points(pixel_coords, intrinsics_inv, depths)
Ejemplo n.º 26
0
 def test_value_error_proj_mat_2(self, device, dims):
     device = default_to_cpu_if_no_gpu(device)
     cam_coords = torch.rand(2, 2, device=device)
     proj_mat = torch.rand(dims[0], dims[1], device=device)
     with pytest.raises(ValueError):
         gs.project_points(cam_coords, proj_mat)
Ejemplo n.º 27
0
 def test_cases_1_and_4(self, device, lastdim):
     device = default_to_cpu_if_no_gpu(device)
     cam_coords = torch.rand(10, lastdim, device=device)
     proj_mat = torch.rand(4, 4, device=device)
     pixel_coords = gs.project_points(cam_coords, proj_mat)
     assert pixel_coords.shape == (10, 2)
Ejemplo n.º 28
0
 def test_raises_dim_error(self, device):
     device = default_to_cpu_if_no_gpu(device)
     pts = torch.rand(3, device=device)
     with pytest.raises(ValueError):
         gs.unhomogenize_points(pts)
Ejemplo n.º 29
0
 def test_value_error_batchsize_2(self, device):
     device = default_to_cpu_if_no_gpu(device)
     cam_coords = torch.rand(2, 10, 3, device=device)
     proj_mat = torch.rand(1, 4, 4, device=device)
     with pytest.raises(ValueError):
         gs.project_points(cam_coords, proj_mat)
Ejemplo n.º 30
0
 def test_type_errors(self, device, lastdim):
     device = default_to_cpu_if_no_gpu(device)
     intrinsics = [1, 2, 3]
     with pytest.raises(TypeError):
         gs.inverse_intrinsics(intrinsics)