def test_homography_i_H_ref(batch_size, device_type):
    # generate input data
    device = torch.device(device_type)
    image_height, image_width = 32., 32.
    cx, cy = image_width / 2, image_height / 2
    fx, fy = 1., 1.
    rx, ry, rz = 0., 0., 0.
    tx, ty, tz = 0., 0., 0.
    offset_x = 10.  # we will apply a 10units offset to `i` camera
    eps = 1e-6

    pinhole_ref = utils.create_pinhole(fx, fy, cx, cy, image_height,
                                       image_width, rx, ry, rx, tx, ty, tz)
    pinhole_ref = pinhole_ref.repeat(batch_size, 1).to(device)

    pinhole_i = utils.create_pinhole(fx, fy, cx, cy, image_height, image_width,
                                     rx, ry, rx, tx + offset_x, ty, tz)
    pinhole_i = pinhole_i.repeat(batch_size, 1).to(device)

    # compute homography from ref to i
    i_H_ref = tgm.homography_i_H_ref(pinhole_i, pinhole_ref) + eps
    i_H_ref_inv = torch.inverse(i_H_ref)

    # compute homography from i to ref
    ref_H_i = tgm.homography_i_H_ref(pinhole_ref, pinhole_i) + eps
    assert utils.check_equal_torch(i_H_ref_inv, ref_H_i)

    # evaluate function gradient
    assert gradcheck(tgm.homography_i_H_ref, (
        utils.tensor_to_gradcheck_var(pinhole_ref) + eps,
        utils.tensor_to_gradcheck_var(pinhole_i) + eps,
    ),
                     raise_exception=True)
    def test_homography_i_H_ref(self):
        # generate input data
        image_height, image_width = 32., 32.
        cx, cy = image_width / 2, image_height / 2
        fx, fy = 1., 1.
        rx, ry, rz = 0., 0., 0.
        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)

        # compute homography from ref to i
        i_H_ref = tgm.homography_i_H_ref(pinhole_i, pinhole_ref)
        i_H_ref_inv = tgm.inverse(i_H_ref)

        # compute homography from i to ref
        ref_H_i = tgm.homography_i_H_ref(pinhole_ref, pinhole_i)

        res = utils.check_equal_torch(i_H_ref_inv, ref_H_i)
        self.assertTrue(res)
    def test_homography_i_H_ref_gradcheck(self):
        # generate input data
        image_height, image_width = 32., 32.
        cx, cy = image_width / 2, image_height / 2
        fx, fy = 1., 1.
        rx, ry, rz = 0., 0., 0.
        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.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.tensor_to_gradcheck_var(pinhole_ref)  # to var

        # evaluate function gradient
        res = gradcheck(tgm.homography_i_H_ref, (
            pinhole_i,
            pinhole_ref,
        ),
                        raise_exception=True)
        self.assertTrue(res)
def test_inverse_pinhole_matrix(batch_size, device_type):
    # generate input data
    image_height, image_width = 32., 32.
    cx, cy = image_width / 2, image_height / 2
    fx, fy = 1., 1.
    rx, ry, rz = 0., 0., 0.
    tx, ty, tz = 0., 0., 0.
    offset_x = 10.  # we will apply a 10units offset to `i` camera
    eps = 1e-6

    pinhole = utils.create_pinhole(
        fx, fy, cx, cy, image_height, image_width, rx, ry, rx, tx, ty, tz)
    pinhole = pinhole.repeat(batch_size, 1).to(torch.device(device_type))

    pinhole_matrix = tgm.inverse_pinhole_matrix(pinhole)

    ones = torch.ones(batch_size)
    assert utils.check_equal_torch(pinhole_matrix[:, 0, 0], (1. / fx) * ones)
    assert utils.check_equal_torch(pinhole_matrix[:, 1, 1], (1. / fy) * ones)
    assert utils.check_equal_torch(
        pinhole_matrix[:, 0, 2], (-1. * cx / fx) * ones)
    assert utils.check_equal_torch(
        pinhole_matrix[:, 1, 2], (-1. * cy / fx) * ones)

    # functional
    assert tgm.InversePinholeMatrix()(pinhole).shape == (batch_size, 4, 4)

    # evaluate function gradient
    pinhole = utils.tensor_to_gradcheck_var(pinhole)  # to var
    assert gradcheck(tgm.pinhole_matrix, (pinhole,),
                     raise_exception=True)
Exemple #5
0
    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_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)

        # instantiate warper and compute relative homographies
        warper = tgm.DepthWarper(pinhole_i)
        warper.compute_homographies(pinhole_ref,
                                    scale=torch.ones(batch_size, 1))

        # 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)

        # 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)
        res = utils.check_equal_torch(patch_ref, patch_ref_functional)
        self.assertTrue(res)