Exemplo n.º 1
0
    def raysampler(
        raysampler_type=GridRaysampler,
        camera_type=PerspectiveCameras,
        n_pts_per_ray=10,
        batch_size=1,
        image_width=10,
        image_height=20,
    ):

        device = torch.device("cuda")

        # init raysamplers
        raysampler = TestRaysampling.init_raysampler(
            raysampler_type=raysampler_type,
            min_x=-1.0,
            max_x=1.0,
            min_y=-1.0,
            max_y=1.0,
            image_width=image_width,
            image_height=image_height,
            min_depth=1.0,
            max_depth=10.0,
            n_pts_per_ray=n_pts_per_ray,
        ).to(device)

        # init a batch of random cameras
        cameras = init_random_cameras(camera_type, batch_size, random_z=True).to(device)

        def run_raysampler():
            raysampler(cameras=cameras)
            torch.cuda.synchronize()

        return run_raysampler
Exemplo n.º 2
0
    def test_ndc_convention(
        self,
        h=428,
        w=760,
    ):
        device = torch.device("cuda")

        camera = init_random_cameras(PerspectiveCameras, 1,
                                     random_z=True).to(device)

        depth_map = torch.ones((1, 1, h, w)).to(device)

        xyz = ray_bundle_to_ray_points(
            NDCGridRaysampler(
                image_width=w,
                image_height=h,
                n_pts_per_ray=1,
                min_depth=1.0,
                max_depth=1.0,
            )(camera)._replace(lengths=depth_map[:, 0, ...,
                                                 None])).view(1, -1, 3)

        # project pointcloud
        xy = camera.transform_points(xyz)[:, :, :2].squeeze()

        xy_grid = self._get_ndc_grid(h, w, device)

        self.assertClose(
            xy,
            xy_grid,
            atol=1e-4,
        )
Exemplo n.º 3
0
    def test_corresponding_cameras_alignment(self):
        """
        Checks the corresponding_cameras_alignment function.
        """
        self.skipTest("Temporarily disabled pending investigation")
        device = torch.device("cuda:0")

        # try few different random setups
        for _ in range(3):
            for estimate_scale in (True, False):
                # init true alignment transform
                R_align_gt = random_rotations(1, device=device)[0]
                T_align_gt = torch.randn(3, dtype=torch.float32, device=device)

                # init true scale
                if estimate_scale:
                    s_align_gt = torch.randn(1,
                                             dtype=torch.float32,
                                             device=device).exp()
                else:
                    s_align_gt = torch.tensor(1.0,
                                              dtype=torch.float32,
                                              device=device)

                for cam_type in (
                        SfMOrthographicCameras,
                        OpenGLPerspectiveCameras,
                        OpenGLOrthographicCameras,
                        SfMPerspectiveCameras,
                ):
                    # try well-determined and underdetermined cases
                    for batch_size in (10, 4, 3, 2, 1):
                        # get random cameras
                        cameras = init_random_cameras(cam_type,
                                                      batch_size,
                                                      random_z=True).to(device)
                        # try all alignment modes
                        for mode in ("extrinsics", "centers"):
                            # try different noise levels
                            for add_noise in (0.0, 0.01, 1e-4):
                                self._corresponding_cameras_alignment_test_case(
                                    cameras,
                                    R_align_gt,
                                    T_align_gt,
                                    s_align_gt,
                                    estimate_scale,
                                    mode,
                                    add_noise,
                                )
Exemplo n.º 4
0
    def corresponding_cameras_alignment(
        batch_size: int, estimate_scale: bool, mode: str, cam_type=SfMPerspectiveCameras
    ):
        device = torch.device("cuda:0")
        cameras_src, cameras_tgt = [
            init_random_cameras(cam_type, batch_size, random_z=True).to(device)
            for _ in range(2)
        ]

        torch.cuda.synchronize()

        def compute_corresponding_cameras_alignment():
            corresponding_cameras_alignment(
                cameras_src, cameras_tgt, estimate_scale=estimate_scale, mode=mode
            )
            torch.cuda.synchronize()

        return compute_corresponding_cameras_alignment
Exemplo n.º 5
0
    def raysampler(
        raysampler_type,
        camera_type,
        n_pts_per_ray: int,
        batch_size: int,
        image_width: int,
        image_height: int,
    ) -> Callable[[], None]:
        """
        Used for benchmarks.
        """
        device = torch.device("cuda")

        # init raysamplers
        raysampler = TestRaysampling.init_raysampler(
            raysampler_type=raysampler_type,
            min_x=-1.0,
            max_x=1.0,
            min_y=-1.0,
            max_y=1.0,
            image_width=image_width,
            image_height=image_height,
            min_depth=1.0,
            max_depth=10.0,
            n_pts_per_ray=n_pts_per_ray,
        ).to(device)

        # init a batch of random cameras
        cameras = init_random_cameras(camera_type, batch_size,
                                      random_z=True).to(device)

        def run_raysampler() -> None:
            raysampler(cameras=cameras)
            torch.cuda.synchronize()

        return run_raysampler
Exemplo n.º 6
0
    def test_raysamplers(
        self,
        batch_size=25,
        min_x=-1.0,
        max_x=1.0,
        min_y=-1.0,
        max_y=1.0,
        image_width=10,
        image_height=20,
        min_depth=1.0,
        max_depth=10.0,
    ):
        """
        Tests the shapes and outputs of MC and GridRaysamplers for randomly
        generated cameras and different numbers of points per ray.
        """

        device = torch.device("cuda")

        for n_pts_per_ray in (100, 1):

            for raysampler_type in (
                MonteCarloRaysampler,
                GridRaysampler,
                NDCGridRaysampler,
            ):

                raysampler = TestRaysampling.init_raysampler(
                    raysampler_type=raysampler_type,
                    min_x=min_x,
                    max_x=max_x,
                    min_y=min_y,
                    max_y=max_y,
                    image_width=image_width,
                    image_height=image_height,
                    min_depth=min_depth,
                    max_depth=max_depth,
                    n_pts_per_ray=n_pts_per_ray,
                )

                if issubclass(raysampler_type, NDCGridRaysampler):
                    # adjust the gt bounds for NDCGridRaysampler
                    half_pix_width = 1.0 / image_width
                    half_pix_height = 1.0 / image_height
                    min_x_ = 1.0 - half_pix_width
                    max_x_ = -1.0 + half_pix_width
                    min_y_ = 1.0 - half_pix_height
                    max_y_ = -1.0 + half_pix_height
                else:
                    min_x_ = min_x
                    max_x_ = max_x
                    min_y_ = min_y
                    max_y_ = max_y

                # carry out the test over several camera types
                for cam_type in (
                    FoVPerspectiveCameras,
                    FoVOrthographicCameras,
                    OrthographicCameras,
                    PerspectiveCameras,
                ):

                    # init a batch of random cameras
                    cameras = init_random_cameras(
                        cam_type, batch_size, random_z=True
                    ).to(device)

                    # call the raysampler
                    ray_bundle = raysampler(cameras=cameras)

                    # check the shapes of the raysampler outputs
                    self._check_raysampler_output_shapes(
                        raysampler,
                        ray_bundle,
                        batch_size,
                        image_width,
                        image_height,
                        n_pts_per_ray,
                    )

                    # check the points sampled along each ray
                    self._check_raysampler_ray_points(
                        raysampler,
                        cameras,
                        ray_bundle,
                        min_x_,
                        max_x_,
                        min_y_,
                        max_y_,
                        image_width,
                        image_height,
                        min_depth,
                        max_depth,
                    )

                    # check the output direction vectors
                    self._check_raysampler_ray_directions(
                        cameras, raysampler, ray_bundle
                    )