def test_homography_warper_gradcheck(self):
        # generate input data
        batch_size = 1
        height, width = 16, 32  # small patch, otherwise the test takes forever
        eye_size = 3  # identity 3x3

        # create checkerboard
        board = utils.create_checkerboard(height, width, 4)
        patch_src = torch.from_numpy(board).view(
            1, 1, height, width).expand(batch_size, 1, height, width)
        patch_src = utils.tensor_to_gradcheck_var(patch_src)  # to var

        # create base homography
        dst_homo_src = utils.create_eye_batch(batch_size, eye_size)
        dst_homo_src = utils.tensor_to_gradcheck_var(
            dst_homo_src, requires_grad=False)  # to var

        # instantiate warper
        warper = tgm.HomographyWarper(height, width)

        # evaluate function gradient
        res = gradcheck(warper, (patch_src, dst_homo_src,),
                        raise_exception=True)
        self.assertTrue(res)

        # evaluate function gradient
        res = gradcheck(
            tgm.homography_warp,
            (patch_src,
             dst_homo_src,
             (height,
              width)),
            raise_exception=True)
        self.assertTrue(res)
Example #2
0
def test_get_perspective_transform(batch_size, device_type):
    # generate input data
    device = torch.device(device_type)

    h_max, w_max = 64, 32  # height, width
    h = torch.ceil(h_max * torch.rand(batch_size)).to(device)
    w = torch.ceil(w_max * torch.rand(batch_size)).to(device)

    norm = torch.rand(batch_size, 4, 2).to(device)
    points_src = torch.zeros_like(norm)
    points_src[:, 1, 0] = h
    points_src[:, 2, 1] = w
    points_src[:, 3, 0] = h
    points_src[:, 3, 1] = w
    points_dst = points_src + norm

    # compute transform from source to target
    dst_homo_src = tgm.get_perspective_transform(points_src, points_dst)

    assert utils.check_equal_torch(
        tgm.transform_points(dst_homo_src, points_src), points_dst)

    # compute gradient check
    points_src = utils.tensor_to_gradcheck_var(points_src)  # to var
    points_dst = utils.tensor_to_gradcheck_var(points_dst)  # to var
    assert gradcheck(tgm.get_perspective_transform, (
        points_src,
        points_dst,
    ),
                     raise_exception=True)
Example #3
0
    def test_warp_perspective_gradcheck(self):
        # generate input data
        batch_size = 1
        height, width = 16, 32
        alpha = tgm.pi / 2  # 90 deg rotation

        # create data patch
        patch = torch.rand(batch_size, 1, height, width)
        patch = utils.tensor_to_gradcheck_var(patch)  # to var

        # create transformation (rotation)
        M = torch.tensor([[
            [torch.cos(alpha), -torch.sin(alpha), 0.],
            [torch.sin(alpha), torch.cos(alpha), 0.],
            [0., 0., 1.],
        ]])  # Bx3x3
        M = utils.tensor_to_gradcheck_var(M, requires_grad=False)  # to var

        # evaluate function gradient
        res = gradcheck(tgm.warp_perspective, (patch, M, (
            height,
            width,
        )),
                        raise_exception=True)
        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_transform_points(batch_size, num_points, num_dims, device_type):
    # generate input data
    eye_size = num_dims + 1
    points_src = torch.rand(batch_size, num_points, num_dims)
    points_src = points_src.to(torch.device(device_type))

    dst_homo_src = utils.create_random_homography(batch_size, eye_size)
    dst_homo_src = dst_homo_src.to(torch.device(device_type))

    # transform the points from dst to ref
    points_dst = tgm.transform_points(dst_homo_src, points_src)

    # transform the points from ref to dst
    src_homo_dst = torch.inverse(dst_homo_src)
    points_dst_to_src = tgm.transform_points(src_homo_dst, points_dst)

    # projected should be equal as initial
    error = utils.compute_mse(points_src, points_dst_to_src)
    assert pytest.approx(error.item(), 0.0)

    # functional
    assert torch.allclose(points_dst,
                          tgm.TransformPoints(dst_homo_src)(points_src))

    # evaluate function gradient
    points_src = utils.tensor_to_gradcheck_var(points_src)  # to var
    dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src)  # to var
    assert gradcheck(tgm.transform_points, (
        dst_homo_src,
        points_src,
    ),
                     raise_exception=True)
Example #6
0
def test_ssim(batch_shape, device_type, window_size, reduction_type):
    # input data
    device = torch.device(device_type)
    img1 = torch.rand(batch_shape).to(device)
    img2 = torch.rand(batch_shape).to(device)

    ssim = tgm.losses.SSIM(window_size, reduction_type)
    ssim_loss_val = ssim(img1, img2)

    if reduction_type == 'none':
        assert ssim_loss_val.shape == batch_shape
    else:
        assert ssim_loss_val.dim() == 0

    assert pytest.approx(ssim(img1, img1).sum().item(), 0.0)
    assert pytest.approx(ssim(img2, img2).sum().item(), 0.0)

    # functional
    assert utils.check_equal_torch(
        ssim_loss_val, tgm.losses.ssim(
            img1, img2, window_size, reduction_type))

    # evaluate function gradient
    img1 = utils.tensor_to_gradcheck_var(img1)  # to var
    img2 = utils.tensor_to_gradcheck_var(img2, requires_grad=False)  # to var
    assert gradcheck(ssim, (img1, img2,), raise_exception=True)
Example #7
0
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)
Example #8
0
 def _test_gradcheck(self):
     image = self.image.clone()
     depth = self.depth.clone()
     depth = utils.tensor_to_gradcheck_var(depth)  # to var
     image = utils.tensor_to_gradcheck_var(image)  # to var
     assert gradcheck(tgm.losses.inverse_depth_smoothness_loss,
                      (depth, image,), raise_exception=True)
    def test_gradcheck(self, batch_size):
        trans_01 = identity_matrix(batch_size)
        trans_02 = identity_matrix(batch_size)

        trans_01 = utils.tensor_to_gradcheck_var(trans_01)  # to var
        trans_02 = utils.tensor_to_gradcheck_var(trans_02)  # to var
        assert gradcheck(tgm.boxminus_transformation, (trans_01, trans_02,),
                         raise_exception=True)
Example #10
0
    def test_gradcheck(self):
        # test parameters
        shear = torch.tensor([[0.5, 0.0]])
        shear = utils.tensor_to_gradcheck_var(
            shear, requires_grad=False)  # to var

        # evaluate function gradient
        input = torch.rand(1, 2, 3, 4)
        input = utils.tensor_to_gradcheck_var(input)  # to var
        assert gradcheck(kornia.shear, (input, shear,), raise_exception=True)
Example #11
0
    def test_gradcheck(self):
        # test parameters
        angle = torch.tensor([90.])
        angle = utils.tensor_to_gradcheck_var(
            angle, requires_grad=False)  # to var

        # evaluate function gradient
        input = torch.rand(1, 2, 3, 4)
        input = utils.tensor_to_gradcheck_var(input)  # to var
        assert gradcheck(kornia.rotate, (input, angle,), raise_exception=True)
 def test_gradcheck(self):
     # generate input data
     batch_size, num_points, num_dims = 2, 3, 2
     eye_size = num_dims + 1
     points_src = torch.rand(batch_size, num_points, num_dims)
     dst_homo_src = utils.create_random_homography(batch_size, eye_size)
     # evaluate function gradient
     points_src = utils.tensor_to_gradcheck_var(points_src)  # to var
     dst_homo_src = utils.tensor_to_gradcheck_var(dst_homo_src)  # to var
     assert gradcheck(tgm.TransformPoints(dst_homo_src), (points_src, ),
                      raise_exception=True)
Example #13
0
    def test_gradcheck(self, batch_size):
        trans_01 = identity_matrix(batch_size)
        trans_12 = identity_matrix(batch_size)

        trans_01 = utils.tensor_to_gradcheck_var(trans_01)  # to var
        trans_12 = utils.tensor_to_gradcheck_var(trans_12)  # to var
        assert gradcheck(kornia.compose_transformations, (
            trans_01,
            trans_12,
        ),
                         raise_exception=True)
Example #14
0
    def test_gradcheck(self):
        # test parameters
        translation = torch.tensor([[1., 0.]])
        translation = utils.tensor_to_gradcheck_var(
            translation, requires_grad=False)  # to var

        # evaluate function gradient
        input = torch.rand(1, 2, 3, 4)
        input = utils.tensor_to_gradcheck_var(input)  # to var
        assert gradcheck(kornia.translate, (input, translation,),
                         raise_exception=True)
Example #15
0
    def test_gradcheck(self):
        batch_size, channels, height, width = 1, 2, 3, 4
        img = torch.rand(batch_size, channels, height, width)
        img = utils.tensor_to_gradcheck_var(img)  # to var

        grid = tgm.utils.create_meshgrid(
            height, width, normalized_coordinates=False)
        grid = utils.tensor_to_gradcheck_var(
            grid, requires_grad=False)  # to var

        assert gradcheck(tgm.remap, (img, grid[..., 0], grid[..., 1],),
                         raise_exception=True)
Example #16
0
 def test_gradcheck(self):
     batch_size, channels, height, width = 1, 2, 3, 4
     aff_ab = torch.eye(2, 3)[None]  # 1x2x3
     img_b = torch.rand(batch_size, channels, height, width)
     aff_ab = utils.tensor_to_gradcheck_var(aff_ab,
                                            requires_grad=False)  # to var
     img_b = utils.tensor_to_gradcheck_var(img_b)  # to var
     assert gradcheck(kornia.warp_affine, (
         img_b,
         aff_ab,
         (height, width),
     ),
                      raise_exception=True)
Example #17
0
def test_rotation_matrix2d(batch_size, device_type):
    # generate input data
    device = torch.device(device_type)
    center_base = torch.zeros(batch_size, 2).to(device)
    angle_base = torch.ones(batch_size).to(device)
    scale_base = torch.ones(batch_size).to(device)

    # 90 deg rotation
    center = center_base
    angle = 90. * angle_base
    scale = scale_base
    M = tgm.get_rotation_matrix2d(center, angle, scale)

    for i in range(batch_size):
        pytest.approx(M[i, 0, 0].item(), 0.0)
        pytest.approx(M[i, 0, 1].item(), 1.0)
        pytest.approx(M[i, 1, 0].item(), -1.0)
        pytest.approx(M[i, 1, 1].item(), 0.0)

    # 90 deg rotation + 2x scale
    center = center_base
    angle = 90. * angle_base
    scale = 2. * scale_base
    M = tgm.get_rotation_matrix2d(center, angle, scale)

    for i in range(batch_size):
        pytest.approx(M[i, 0, 0].item(), 0.0)
        pytest.approx(M[i, 0, 1].item(), 2.0)
        pytest.approx(M[i, 1, 0].item(), -2.0)
        pytest.approx(M[i, 1, 1].item(), 0.0)

    # 45 deg rotation
    center = center_base
    angle = 45. * angle_base
    scale = scale_base
    M = tgm.get_rotation_matrix2d(center, angle, scale)

    for i in range(batch_size):
        pytest.approx(M[i, 0, 0].item(), 0.7071)
        pytest.approx(M[i, 0, 1].item(), 0.7071)
        pytest.approx(M[i, 1, 0].item(), -0.7071)
        pytest.approx(M[i, 1, 1].item(), 0.7071)

    # evaluate function gradient
    center = utils.tensor_to_gradcheck_var(center)  # to var
    angle = utils.tensor_to_gradcheck_var(angle)  # to var
    scale = utils.tensor_to_gradcheck_var(scale)  # to var
    assert gradcheck(
        tgm.get_rotation_matrix2d, (center, angle, scale),
        raise_exception=True)
Example #18
0
    def test_gradcheck(self):
        # test parameters
        scale_factor = torch.tensor([0.5])
        scale_factor = utils.tensor_to_gradcheck_var(
            scale_factor, requires_grad=False)  # to var

        # evaluate function gradient
        input = torch.rand(1, 2, 3, 4)
        input = utils.tensor_to_gradcheck_var(input)  # to var
        assert gradcheck(tgm.scale, (
            input,
            scale_factor,
        ),
                         raise_exception=True)
Example #19
0
def test_scale_pinhole(batch_size, device_type):
    # generate input data
    device = torch.device(device_type)
    pinholes = torch.rand(batch_size, 12).to(device)
    scales = torch.rand(batch_size).to(device)

    pinholes_scale = tgm.scale_pinhole(pinholes, scales)
    assert utils.check_equal_torch(
        pinholes_scale[..., :6] / scales.unsqueeze(-1), pinholes[..., :6])

    # evaluate function gradient
    pinholes = utils.tensor_to_gradcheck_var(pinholes)  # to var
    scales = utils.tensor_to_gradcheck_var(scales)  # to var
    assert gradcheck(tgm.scale_pinhole, (pinholes, scales,),
                     raise_exception=True)
Example #20
0
    def test_get_rotation_matrix2d_gradcheck(self):
        # generate input data
        batch_size = 1
        center = torch.zeros(batch_size, 2)
        angle = torch.ones(batch_size)
        scale = torch.ones(batch_size)

        center = utils.tensor_to_gradcheck_var(center)  # to var
        angle = utils.tensor_to_gradcheck_var(angle)  # to var
        scale = utils.tensor_to_gradcheck_var(scale)  # to var

        # evaluate function gradient
        res = gradcheck(tgm.get_rotation_matrix2d, (center, angle, scale),
                        raise_exception=True)
        self.assertTrue(res)
Example #21
0
    def test_get_perspective_transform_gradcheck(self):
        # generate input data
        h, w = 64, 32  # height, width
        norm = torch.randn(1, 4, 2)
        points_src = torch.FloatTensor([[
            [0, 0], [h, 0], [0, w], [h, w],
        ]])
        points_dst = points_src + norm
        points_src = utils.tensor_to_gradcheck_var(points_src)  # to var
        points_dst = utils.tensor_to_gradcheck_var(points_dst)  # to var

        # compute transform from source to target
        res = gradcheck(tgm.get_perspective_transform,
                        (points_src, points_dst,), raise_exception=True)
        self.assertTrue(res)
Example #22
0
    def test_gradcheck(self):
        batch_size, channels, height, width = 1, 2, 5, 4
        img = torch.rand(batch_size, channels, height, width)
        img = utils.tensor_to_gradcheck_var(img)  # to var

        assert gradcheck(kornia.geometry.pyrdown, (img, ),
                         raise_exception=True)
Example #23
0
 def test_gradcheck(self):
     eps = 1e-12
     quaternion = torch.Tensor([1, 0, 0, 0]) + eps
     quaternion = utils.tensor_to_gradcheck_var(quaternion)
     # evaluate function gradient
     assert gradcheck(kornia.quaternion_to_angle_axis, (quaternion,),
                      raise_exception=True)
Example #24
0
    def test_gradcheck(self, batch_shape):
        points_h = torch.rand(batch_shape)

        # evaluate function gradient
        points_h = utils.tensor_to_gradcheck_var(points_h)  # to var
        assert gradcheck(kornia.convert_points_from_homogeneous, (points_h,),
                         raise_exception=True)
Example #25
0
 def test_gradcheck(self):
     k = 0.04
     batch_size, channels, height, width = 1, 2, 5, 4
     img = torch.rand(batch_size, channels, height, width)
     img = utils.tensor_to_gradcheck_var(img)  # to var
     assert gradcheck(kornia.feature.corner_harris, (img, k),
                      raise_exception=True)
Example #26
0
def test_rtvec_to_pose_gradcheck(batch_size, device_type):
    # generate input data
    rtvec = torch.rand(batch_size, 6).to(torch.device(device_type))

    # evaluate function gradient
    rtvec = utils.tensor_to_gradcheck_var(rtvec)  # to var
    assert gradcheck(kornia.rtvec_to_pose, (rtvec,), raise_exception=True)
Example #27
0
 def test_gradcheck(self):
     eps = 1e-12
     angle_axis = torch.Tensor([0, 0, 0]) + eps
     angle_axis = utils.tensor_to_gradcheck_var(angle_axis)
     # evaluate function gradient
     assert gradcheck(kornia.angle_axis_to_quaternion, (angle_axis,),
                      raise_exception=True)
Example #28
0
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)
Example #29
0
 def test_gradcheck(self):
     input = torch.rand(2, 3, 4, 4)
     input = utils.tensor_to_gradcheck_var(input)  # to var
     assert gradcheck(kornia.contrib.extract_tensor_patches, (
         input,
         3,
     ),
                      raise_exception=True)
Example #30
0
 def test_gradcheck(self):
     input = torch.rand(2, 3, 4, 4)
     input = utils.tensor_to_gradcheck_var(input)  # to var
     assert gradcheck(kornia.contrib.max_blur_pool2d, (
         input,
         3,
     ),
                      raise_exception=True)