Exemplo n.º 1
0
 def test_dxdy_batch(self, device, dtype):
     # prepare input data
     inp = torch.tensor([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]]],
                        device=device,
                        dtype=dtype).repeat(2, 1, 1, 1)
     expected = torch.tensor(
         [[[[0.0, 1.0], [0.0, 3.0], [0.0, 5.0], [0.0, 7.0]]],
          [[[0.0, 0.0], [0.0, 1.0], [0.0, 3.0], [0.0, 5.0]]]],
         device=device,
         dtype=dtype,
     )
     # prepare transformation
     translation = torch.tensor([[1.0, 0.0], [1.0, 1.0]],
                                device=device,
                                dtype=dtype)
     transform = kornia.geometry.transform.Translate(translation,
                                                     align_corners=True)
     assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
Exemplo n.º 2
0
 def test_scale_factor_05_batch2_broadcast(self, device, dtype):
     # prepare input data
     inp = torch.tensor(
         [[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0],
           [1.0, 1.0, 1.0, 1.0]]],
         device=device,
         dtype=dtype,
     ).repeat(2, 1, 1, 1)
     expected = torch.tensor(
         [[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0],
           [0.0, 0.0, 0.0, 0.0]]],
         device=device,
         dtype=dtype,
     ).repeat(2, 1, 1, 1)
     # prepare transformation
     scale_factor = torch.tensor([[0.5, 0.5]], device=device, dtype=dtype)
     transform = kornia.geometry.transform.Scale(scale_factor)
     assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
Exemplo n.º 3
0
    def test_shear_batch2_broadcast(self, device, dtype):
        # prepare input data
        inp = torch.tensor(
            [[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]],
            device=device,
            dtype=dtype,
        ).repeat(2, 1, 1, 1)

        expected = torch.tensor(
            [[[[0.75, 1.0, 1.0, 1.0], [0.25, 1.0, 1.0, 1.0], [0.0, 0.75, 1.0, 1.0], [0.0, 0.25, 1.0, 1.0]]]],
            device=device,
            dtype=dtype,
        ).repeat(2, 1, 1, 1)

        # prepare transformation
        shear = torch.tensor([[0.5, 0.0]], device=device, dtype=dtype)
        transform = kornia.Shear(shear, align_corners=False)
        assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
Exemplo n.º 4
0
    def test_warp_grid_translation(self, shape, offset, device, dtype):
        # create input data
        height, width = shape
        dst_homo_src = utils.create_eye_batch(batch_size=1,
                                              eye_size=3,
                                              device=device,
                                              dtype=dtype)
        dst_homo_src[..., 0, 2] = offset  # apply offset in x
        grid = kornia.utils.create_meshgrid(height,
                                            width,
                                            normalized_coordinates=False)
        flow = kornia.geometry.transform.warp_grid(grid, dst_homo_src)

        # the grid the src plus the offset should be equal to the flow
        # on the x-axis, y-axis remains the same.
        assert_close(grid[..., 0].to(device=device, dtype=dtype) + offset,
                     flow[..., 0])
        assert_close(grid[..., 1].to(device=device, dtype=dtype), flow[..., 1])
Exemplo n.º 5
0
    def test_clean_points(self, batch_size, device, dtype):
        # generate input data
        points_src = torch.rand(batch_size, 10, 2, device=device, dtype=dtype)
        H = kornia.eye_like(3, points_src)
        H = H * 0.3 * torch.rand_like(H)
        H = H / H[:, 2:3, 2:3]

        points_dst = kornia.transform_points(H, points_src)
        weights = torch.ones(batch_size, 10, device=device, dtype=dtype)

        # compute transform from source to target
        dst_homo_src = find_homography_dlt_iterated(points_src, points_dst,
                                                    weights, 10)

        assert_close(kornia.transform_points(dst_homo_src, points_src),
                     points_dst,
                     rtol=1e-3,
                     atol=1e-4)
Exemplo n.º 6
0
    def test_random_mixup_p0(self, device, dtype):
        torch.manual_seed(0)
        f = RandomMixUp(p=0.0)

        input = torch.stack([
            torch.ones(1, 3, 4, device=device, dtype=dtype),
            torch.zeros(1, 3, 4, device=device, dtype=dtype)
        ])
        label = torch.tensor([1, 0], device=device)
        # TODO(jian): where is it used ?
        # lam = torch.tensor([0.0, 0.0], device=device, dtype=dtype)

        expected = input.clone()

        out_image, out_label = f(input, label)

        assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
        assert (out_label == label).all()
Exemplo n.º 7
0
 def test_kernel(self, device, dtype):
     tensor = torch.tensor(
         [[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],
         device=device,
         dtype=dtype)[None, None, :, :]
     kernel = torch.tensor(
         [[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]],
         device=device,
         dtype=dtype)
     expected = torch.tensor(
         [[0.5, 0.3, 0.3], [0.3, 0.3, 0.2], [0.4, 0.2, 0.2]],
         device=device,
         dtype=dtype)[None, None, :, :]
     assert_close(erosion(tensor, kernel), expected, atol=1e-4, rtol=1e-4)
     assert_close(erosion(tensor, kernel, engine='convolution'),
                  expected,
                  atol=1e-3,
                  rtol=1e-3)
Exemplo n.º 8
0
    def test_four_classes_one_missing(self, device, dtype):
        num_classes = 4
        actual = torch.tensor(
            [[3, 3, 1, 1, 2, 1, 1, 3, 2, 2, 1, 1, 2, 3, 2, 1]],
            device=device,
            dtype=torch.long)
        predicted = torch.tensor(
            [[3, 2, 1, 1, 1, 1, 1, 2, 1, 3, 3, 2, 1, 1, 3, 3]],
            device=device,
            dtype=torch.long)

        conf_mat = kornia.metrics.confusion_matrix(predicted, actual,
                                                   num_classes)
        conf_mat_real = torch.tensor(
            [[[0, 0, 0, 0], [0, 4, 1, 2], [0, 3, 0, 2], [0, 1, 2, 1]]],
            device=device,
            dtype=torch.float32)
        assert_close(conf_mat, conf_mat_real)
Exemplo n.º 9
0
    def test_project(self, num_points, device, dtype):

        intrinsics, _, world_points, img_points = self._get_test_data(
            num_points, device, dtype)

        pred_world_to_cam = kornia.geometry.solve_pnp_dlt(
            world_points, img_points, intrinsics)

        pred_world_to_cam_4x4 = kornia.eye_like(4, pred_world_to_cam)
        pred_world_to_cam_4x4[:, :3, :] = pred_world_to_cam

        repeated_intrinsics = intrinsics.unsqueeze(1).repeat(
            1, num_points, 1, 1)
        pred_img_points = self._project_to_image(world_points,
                                                 pred_world_to_cam_4x4,
                                                 repeated_intrinsics)

        assert_close(pred_img_points, img_points, atol=1e-3, rtol=1e-3)
Exemplo n.º 10
0
    def test_opencv(self, device, dtype):
        data = torch.tensor(
            [[
                [0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
                [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
                [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
                [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
                [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805],
            ]],
            device=device,
            dtype=dtype,
        )

        # Output data generated with OpenCV 4.5.2: cv2.cvtColor(img_np, cv2.COLOR_GRAY2RGB)
        expected = torch.tensor(
            [
                [
                    [0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
                    [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
                    [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
                    [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
                    [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805],
                ],
                [
                    [0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
                    [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
                    [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
                    [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
                    [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805],
                ],
                [
                    [0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
                    [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
                    [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
                    [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
                    [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805],
                ],
            ],
            device=device,
            dtype=dtype,
        )

        img_rgb = kornia.grayscale_to_rgb(data)
        assert_close(img_rgb, expected)
Exemplo n.º 11
0
    def test_opencv(self, device, dtype):
        data = torch.tensor(
            [
                [
                    [0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
                    [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
                    [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
                    [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
                    [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593],
                ],
                [
                    [0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
                    [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
                    [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
                    [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
                    [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426],
                ],
                [
                    [0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
                    [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
                    [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
                    [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
                    [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532],
                ],
            ],
            device=device,
            dtype=dtype,
        )

        # Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
        expected = torch.tensor(
            [[
                [0.4485849, 0.8233618, 0.6262833, 0.6218331, 0.6341921],
                [0.3200093, 0.4340172, 0.7107211, 0.5454938, 0.2801398],
                [0.6149265, 0.7018101, 0.3503231, 0.4891168, 0.5292346],
                [0.5096100, 0.4336508, 0.6704276, 0.4525143, 0.2134447],
                [0.7878902, 0.6494595, 0.5211386, 0.6623823, 0.6660464],
            ]],
            device=device,
            dtype=dtype,
        )

        img_gray = kornia.bgr_to_grayscale(data)
        assert_close(img_gray, expected)
Exemplo n.º 12
0
    def test_opencv(self, device, dtype):
        data = torch.tensor(
            [
                [
                    [0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
                    [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
                    [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
                    [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
                    [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593],
                ],
                [
                    [0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
                    [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
                    [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
                    [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
                    [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426],
                ],
                [
                    [0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
                    [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
                    [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
                    [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
                    [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532],
                ],
            ],
            device=device,
            dtype=dtype,
        )

        # Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
        expected = torch.tensor(
            [[
                [0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
                [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
                [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
                [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
                [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805],
            ]],
            device=device,
            dtype=dtype,
        )

        img_gray = kornia.rgb_to_grayscale(data)
        assert_close(img_gray, expected)
Exemplo n.º 13
0
    def test_magnitude_threshold(self, device, dtype):
        inp = torch.tensor(
            [[[
                [0.5, 0.4, 0.5, 0.45, 0.1],
                [0.3, 0.2, 0.3, 0.0, 0.3],
                [0.5, 1.0, 1.0, 0.6, 0.75],
                [0.2, 0.4, 0.6, 0.0, 0.5],
                [0.1, 0.35, 0.35, 0.26, 0.1],
            ]]],
            device=device,
            dtype=dtype,
        )

        expected_magnitude = torch.tensor(
            [[[
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
                [0.4858, 0.5594, 0.6878, 0.6977, 0.5602],
                [0.1129, 0.0000, 0.0000, 0.4531, 0.0000],
                [0.6115, 0.5859, 0.6110, 0.6766, 0.5160],
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
            ]]],
            device=device,
            dtype=dtype,
        )

        expected_edges = torch.tensor(
            [[[
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
                [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
            ]]],
            device=device,
            dtype=dtype,
        )

        magnitude, edges = kornia.filters.canny(inp,
                                                low_threshold=0.3,
                                                high_threshold=0.9)

        tol_val: float = utils._get_precision(device, dtype)
        assert_close(magnitude, expected_magnitude, rtol=tol_val, atol=tol_val)
        assert_close(edges, expected_edges, rtol=tol_val, atol=tol_val)
Exemplo n.º 14
0
    def test_normalized_mean_filter(self, padding, device, dtype):
        kernel = torch.ones(1, 3, 3).to(device)
        input = torch.tensor(
            [[[
                [0.0, 0.0, 0.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0],
                [0.0, 0.0, 5.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0],
            ]]],
            device=device,
            dtype=dtype,
        ).expand(2, 2, -1, -1)

        nv: float = 5.0 / 9  # normalization value
        expected_same = torch.tensor(
            [[[
                [0.0, 0.0, 0.0, 0.0, 0.0],
                [0.0, nv, nv, nv, 0.0],
                [0.0, nv, nv, nv, 0.0],
                [0.0, nv, nv, nv, 0.0],
                [0.0, 0.0, 0.0, 0.0, 0.0],
            ]]],
            device=device,
            dtype=dtype,
        ).expand(2, 2, -1, -1)

        expected_valid = torch.tensor(
            [[[[nv, nv, nv], [nv, nv, nv], [nv, nv, nv]]]],
            device=device,
            dtype=dtype).expand(2, 2, -1, -1)

        actual = kornia.filters.filter2d(input,
                                         kernel,
                                         normalized=True,
                                         padding=padding)

        tol_val: float = utils._get_precision_by_name(device, 'xla', 1e-1,
                                                      1e-4)
        if padding == 'same':
            assert_close(actual, expected_same, rtol=tol_val, atol=tol_val)
        else:
            assert_close(actual, expected_valid, rtol=tol_val, atol=tol_val)
Exemplo n.º 15
0
 def test_batch_variable_size(self, device, dtype):
     im = torch.rand(2, 3, 12, 16, dtype=dtype, device=device)
     pts = [
         torch.tensor([[4, 4], [12, 4], [12, 8], [4, 8]],
                      dtype=dtype,
                      device=device),
         torch.tensor([[0, 0], [2, 0], [4, 0], [4, 4], [0, 4]],
                      dtype=dtype,
                      device=device),
     ]
     color = torch.tensor([[0.5, 0.5, 0.5], [0.5, 0.5, 0.75]],
                          dtype=dtype,
                          device=device)
     poly_im = draw_convex_polygon(im.clone(), pts, color)
     rect = torch.tensor([[[4, 4, 12, 8]], [[0, 0, 4, 4]]],
                         dtype=dtype,
                         device=device)
     rect_im = draw_rectangle(im.clone(), rect, color[:, None], fill=True)
     assert_close(rect_im, poly_im)
Exemplo n.º 16
0
    def test_factor_tensor(self, device, dtype):
        # prepare input data
        data = torch.tensor(
            [
                [[1.0, 1.0], [1.0, 1.0]],
                [[0.5, 0.5], [0.5, 0.5]],
                [[0.25, 0.25], [0.25, 0.25]],
                [[0.5, 0.5], [0.5, 0.5]],
            ],
            device=device,
            dtype=dtype,
        )  # 4x2x2

        factor = torch.tensor([0, 0.5, 0.75, 2], device=device, dtype=dtype)

        expected = torch.ones_like(data)

        f = kornia.enhance.AdjustBrightness(factor)
        assert_close(f(data), expected)
Exemplo n.º 17
0
    def test_random_mixup_lam0(self, device, dtype):
        torch.manual_seed(0)
        f = RandomMixUp(lambda_val=(0.0, 0.0), p=1.0)

        input = torch.stack([
            torch.ones(1, 3, 4, device=device, dtype=dtype),
            torch.zeros(1, 3, 4, device=device, dtype=dtype)
        ])
        label = torch.tensor([1, 0], device=device)
        lam = torch.tensor([0.0, 0.0], device=device, dtype=dtype)

        expected = input.clone()

        out_image, out_label = f(input, label)

        assert_close(out_image, expected, rtol=1e-4, atol=1e-4)
        assert (out_label[:, 0] == label).all()
        assert (out_label[:, 1] == torch.tensor([0, 1], device=device)).all()
        assert_close(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)
Exemplo n.º 18
0
    def test_shear_y(self, device, dtype):
        # prepare input data
        inp = torch.tensor(
            [[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0],
              [1.0, 1.0, 1.0, 1.0]]],
            device=device,
            dtype=dtype,
        )
        expected = torch.tensor(
            [[[0.75, 0.25, 0.0, 0.0], [1.0, 1.0, 0.75, 0.25],
              [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]],
            device=device,
            dtype=dtype,
        )

        # prepare transformation
        shear = torch.tensor([[0.0, 0.5]], device=device, dtype=dtype)
        transform = kornia.geometry.transform.Shear(shear, align_corners=False)
        assert_close(transform(inp), expected, atol=1e-4, rtol=1e-4)
Exemplo n.º 19
0
 def test_real_keynet(self, device, dtype, data):
     torch.random.manual_seed(0)
     # This is not unit test, but that is quite good integration test
     matcher = LocalFeatureMatcher(KeyNetHardNet(500),
                                   DescriptorMatcher('snn', 0.9)).to(
                                       device, dtype)
     ransac = RANSAC('homography', 1.0, 2048, 10).to(device, dtype)
     data_dev = utils.dict_to(data, device, dtype)
     pts_src = data_dev['pts0']
     pts_dst = data_dev['pts1']
     with torch.no_grad():
         out = matcher(data_dev)
     homography, inliers = ransac(out['keypoints0'], out['keypoints1'])
     assert inliers.sum().item() > 50  # we have enough inliers
     # Reprojection error of 5px is OK
     assert_close(transform_points(homography[None], pts_src[None]),
                  pts_dst[None],
                  rtol=5e-2,
                  atol=5)
Exemplo n.º 20
0
    def test_crop_by_boxes_resizing(self, device, dtype):
        inp = torch.arange(0.0, 343.0, device=device, dtype=dtype).view(1, 1, 7, 7, 7)
        src_box = torch.tensor(
            [
                [
                    [1.0, 1.0, 1.0],
                    [3.0, 1.0, 1.0],
                    [3.0, 3.0, 1.0],
                    [1.0, 3.0, 1.0],
                    [1.0, 1.0, 2.0],
                    [3.0, 1.0, 2.0],
                    [3.0, 3.0, 2.0],
                    [1.0, 3.0, 2.0],
                ]
            ],
            device=device,
            dtype=dtype,
        )  # 1x8x3
        dst_box = torch.tensor(
            [
                [
                    [0.0, 0.0, 0.0],
                    [1.0, 0.0, 0.0],
                    [1.0, 1.0, 0.0],
                    [0.0, 1.0, 0.0],
                    [0.0, 0.0, 1.0],
                    [1.0, 0.0, 1.0],
                    [1.0, 1.0, 1.0],
                    [0.0, 1.0, 1.0],
                ]
            ],
            device=device,
            dtype=dtype,
        )  # 1x8x3

        expected = torch.tensor(
            [[[[[57.0000, 59.0000], [71.0000, 73.0000]], [[106.0000, 108.0000], [120.0000, 122.0000]]]]],
            device=device,
            dtype=dtype,
        )

        patches = kornia.geometry.transform.crop_by_boxes3d(inp, src_box, dst_box, align_corners=True)
        assert_close(patches, expected, rtol=1e-4, atol=1e-4)
Exemplo n.º 21
0
    def test_against_functional(self, input_shape):

        input = torch.randn(*input_shape)

        f = RandomMotionBlur(kernel_size=(3, 5),
                             angle=(10, 30),
                             direction=0.5,
                             p=1.0)
        output = f(input)

        expected = motion_blur(
            input,
            f._params['ksize_factor'].unique().item(),
            f._params['angle_factor'],
            f._params['direction_factor'],
            f.flags['border_type'].name.lower(),
        )

        assert_close(output, expected, rtol=1e-4, atol=1e-4)
Exemplo n.º 22
0
 def test_downscale_values(self, device, dtype):
     inp_x = torch.arange(20, device=device, dtype=dtype) / 20.0
     inp = inp_x[None].T @ inp_x[None]
     inp = inp[None, None]
     out = kornia.geometry.transform.rescale(inp, (0.25, 0.25),
                                             antialias=False,
                                             align_corners=False)
     expected = torch.tensor(
         [[[
             [0.0056, 0.0206, 0.0356, 0.0506, 0.0656],
             [0.0206, 0.0756, 0.1306, 0.1856, 0.2406],
             [0.0356, 0.1306, 0.2256, 0.3206, 0.4156],
             [0.0506, 0.1856, 0.3206, 0.4556, 0.5906],
             [0.0656, 0.2406, 0.4156, 0.5906, 0.7656],
         ]]],
         device=device,
         dtype=dtype,
     )
     assert_close(out, expected, atol=1e-3, rtol=1e-3)
Exemplo n.º 23
0
    def test_unit(self, device, dtype, aval):
        data = torch.tensor(
            [[[[1.0, 1.0], [1.0, 1.0]], [[2.0, 2.0], [2.0, 2.0]],
              [[3.0, 3.0], [3.0, 3.0]]]],
            device=device,
            dtype=dtype)  # Bx3x2x2

        expected = torch.tensor(
            [[
                [[1.0, 1.0], [1.0, 1.0]],
                [[2.0, 2.0], [2.0, 2.0]],
                [[3.0, 3.0], [3.0, 3.0]],
                [[aval, aval], [aval, aval]],
            ]],
            device=device,
            dtype=dtype,
        )  # Bx4x2x2

        assert_close(kornia.rgb_to_rgba(data, aval), expected)
Exemplo n.º 24
0
    def test_triplet_qma(self, axis, device, dtype, atol, rtol):
        array = [[0.0, 0.0, 0.0, 0.0]]
        array[0][1 + axis] = 1.0  # `1 + axis` this should fail when XYZW
        quaternion = torch.tensor(array, device=device, dtype=dtype)
        assert quaternion.shape[-1] == 4

        mm = kornia.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
        assert mm.shape[-1] == 3
        assert mm.shape[-2] == 3

        angle_axis = kornia.rotation_matrix_to_angle_axis(mm)
        assert angle_axis.shape[-1] == 3
        angle_axis_expected = [[0.0, 0.0, 0.0]]
        angle_axis_expected[0][axis] = kornia.pi
        angle_axis_expected = torch.tensor(angle_axis_expected, device=device, dtype=dtype)
        assert_close(angle_axis, angle_axis_expected, atol=atol, rtol=rtol)

        quaternion_hat = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
        assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
Exemplo n.º 25
0
    def test_values(self, device, dtype):
        image = torch.tensor(
            [[[[0.0018, 0.7521, 0.7550], [0.2053, 0.4249, 0.1369],
               [0.1027, 0.3992, 0.8773]]]],
            device=device,
            dtype=dtype,
        )

        noise = torch.ones(1, 2, 3, 3, device=device, dtype=dtype)

        expected = torch.tensor(
            [[[[0.0005, 0.3795, 0.1905], [0.1034, 0.4235, 0.0702],
               [0.0259, 0.2007, 0.2193]]]],
            device=device,
            dtype=dtype,
        )

        actual = elastic_transform2d(image, noise)
        assert_close(actual, expected, atol=1e-3, rtol=1e-3)
Exemplo n.º 26
0
 def test_b2_ch1_h3w3_ws2_stride1_padding1(self, device):
     batch_size = 2
     img = torch.arange(9.0).view(1, 1, 3, 3).to(device)
     img = img.expand(batch_size, -1, -1, -1)
     m = kornia.contrib.ExtractTensorPatches(2, stride=1, padding=1)
     patches = m(img)
     assert patches.shape == (batch_size, 16, 1, 2, 2)
     for i in range(batch_size):
         assert_close(img[i, :, 0:2, 0:2], patches[i, 5])
         assert_close(img[i, :, 0:2, 1:3], patches[i, 6])
         assert_close(img[i, :, 1:3, 0:2], patches[i, 9])
         assert_close(img[i, :, 1:3, 1:3], patches[i, 10])
Exemplo n.º 27
0
    def test_translation_normalized(self, device, dtype):
        # this is for normalize_points=True
        image_src = torch.tensor([[[[1.0, 2.0, 3.0], [1.0, 2.0, 3.0],
                                    [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]]],
                                 device=device,
                                 dtype=dtype)

        depth_dst = torch.tensor([[[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0],
                                    [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]],
                                 device=device,
                                 dtype=dtype)

        src_trans_dst = torch.tensor(
            [[[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0],
              [0.0, 0.0, 0.0, 1.0]]],
            device=device,
            dtype=dtype,
        )

        h, w = image_src.shape[-2:]
        camera_matrix = torch.tensor(
            [[[1.0, 0.0, w / 2], [0.0, 1.0, h / 2], [0.0, 0.0, 1.0]]],
            device=device,
            dtype=dtype)

        image_dst_expected = torch.tensor(
            [[[
                [0.9223, 0.0000, 0.0000],
                [2.8153, 1.5000, 0.0000],
                [2.8028, 2.6459, 0.0000],
                [2.8153, 1.5000, 0.0000],
            ]]],
            device=device,
            dtype=dtype,
        )

        image_dst = kornia.geometry.depth.warp_frame_depth(
            image_src,
            depth_dst,
            src_trans_dst,
            camera_matrix,
            normalize_points=True)
        assert_close(image_dst, image_dst_expected, rtol=1e-3, atol=1e-3)
Exemplo n.º 28
0
    def test_smoke(self, device, dtype):
        H, W = 5, 5
        translation = torch.tensor([[0.0, 0.0]], device=device, dtype=dtype)
        # NOTE: ideally the center should be [W * 0.5, H * 0.5]
        center = torch.tensor([[W // 2, H // 2]], device=device, dtype=dtype)
        zoom1 = torch.ones([1, 1], device=device, dtype=dtype) * 0.5
        zoom2 = torch.ones([1, 1], device=device, dtype=dtype) * 1.0
        zoom = torch.cat([zoom1, zoom2], -1)
        angle = torch.zeros([1], device=device, dtype=dtype)
        affine_mat = kornia.geometry.get_affine_matrix2d(
            translation, center, zoom, angle)

        img = torch.ones(1, 1, H, W, device=device, dtype=dtype)
        expected = torch.zeros_like(img)
        expected[..., 1:4] = 1.0

        out = kornia.geometry.transform.warp_affine(img, affine_mat[:, :2],
                                                    (H, W))
        assert_close(out, expected)
Exemplo n.º 29
0
    def test_unit_linear(self, device, dtype):
        data = torch.tensor(
            [
                [[1.00000000, 0.00000000], [0.21404116, 0.01002283]],
                [[1.00000000, 0.00000000], [0.21404116, 0.03310477]],
                [[1.00000000, 0.00000000], [0.21404116, 0.07323898]],
            ],
            device=device,
            dtype=dtype,
        )  # 3x2x2

        expected = torch.tensor(
            [[[1.0, 0.0], [0.5, 0.1]], [[1.0, 0.0], [0.5, 0.2]],
             [[1.0, 0.0], [0.5, 0.3]]],
            device=device,
            dtype=dtype)  # 3x2x2

        f = kornia.color.linear_rgb_to_rgb
        assert_close(f(data), expected)
Exemplo n.º 30
0
    def test_factor_zero_with_mean_subtraction(self, device, dtype):
        # prepare input data
        data = torch.tensor(
            [[[1.0, 1.0], [1.0, 1.0]], [[0.5, 0.5], [0.5, 0.5]],
             [[0.25, 0.25], [0.25, 0.25]]],
            device=device,
            dtype=dtype,
        )  # 3x2x2

        expected = torch.tensor(
            [[[0.6210, 0.6210], [0.6210, 0.6210]],
             [[0.6210, 0.6210], [0.6210, 0.6210]],
             [[0.6210, 0.6210], [0.6210, 0.6210]]],
            device=device,
            dtype=dtype,
        )  # 3x2x2

        f = kornia.enhance.AdjustContrastWithMeanSubtraction(0.0)
        assert_close(f(data), expected)