Exemplo n.º 1
0
    def test_dim_args(self, dim, device, dtype):
        if 'xla' in device.type:
            pytest.skip("buggy with XLA devices.")

        data = torch.tensor([[0, 1], [1, 0], [-1, 0], [0, -1]],
                            device=device,
                            dtype=dtype)

        if dim == 1:
            expected = torch.tensor(
                [
                    [-0.35360718, 0.35360718],
                    [0.35351562, -0.35351562],
                    [-0.35353088, 0.35353088],
                    [0.35353088, -0.35353088],
                ],
                device=device,
                dtype=dtype,
            )
        elif dim == 0:
            expected = torch.tensor([[0.0, 1.2247448], [1.2247448, 0.0],
                                     [-1.2247448, 0.0], [0.0, -1.2247448]],
                                    device=device,
                                    dtype=dtype)

        zca = kornia.enhance.ZCAWhitening(dim=dim)
        actual = zca(data, True)

        tol_val: float = utils._get_precision(device, dtype)
        assert_close(actual, expected, rtol=tol_val, atol=tol_val)
Exemplo n.º 2
0
    def test_value_batch(self, device, dtype):
        torch.manual_seed(0)

        inputs = torch.rand(2, 1, 3, 3).to(device=device, dtype=dtype)

        # Output generated is similar (1e-2 due to the uint8 conversions) to the below output:
        # img = PIL.Image.fromarray(arr)
        # en = ImageEnhance.Sharpness(img).enhance(0.8)
        # np.array(en) / 255.
        expected_08 = torch.tensor(
            [
                [[[0.4963, 0.7682, 0.0885], [0.1320, 0.3305, 0.6341], [0.4901, 0.8964, 0.4556]]],
                [[[0.6323, 0.3489, 0.4017], [0.0223, 0.2052, 0.2939], [0.5185, 0.6977, 0.8000]]],
            ],
            device=device,
            dtype=dtype,
        )

        expected_08_13 = torch.tensor(
            [
                [[[0.4963, 0.7682, 0.0885], [0.1320, 0.3305, 0.6341], [0.4901, 0.8964, 0.4556]]],
                [[[0.6323, 0.3489, 0.4017], [0.0223, 0.1143, 0.2939], [0.5185, 0.6977, 0.8000]]],
            ],
            device=device,
            dtype=dtype,
        )

        # If factor == 1, shall return original
        tol_val: float = utils._get_precision(device, dtype)
        assert_close(TestSharpness.f(inputs, 1), inputs, rtol=tol_val, atol=tol_val)
        assert_close(TestSharpness.f(inputs, torch.tensor([1.0, 1.0])), inputs, rtol=tol_val, atol=tol_val)
        assert_close(TestSharpness.f(inputs, 0.8), expected_08, rtol=tol_val, atol=tol_val)
        assert_close(TestSharpness.f(inputs, torch.tensor([0.8, 1.3])), expected_08_13, rtol=tol_val, atol=tol_val)
Exemplo n.º 3
0
    def test_module(self, device, dtype):
        B, C, H, W = 1, 1, 99, 100
        sample1 = torch.rand(B, C, H, W, device=device, dtype=dtype)
        distance_transformer = kornia.contrib.DistanceTransform().to(device, dtype)

        output1 = distance_transformer(sample1)
        output2 = kornia.contrib.distance_transform(sample1)
        tol_val: float = utils._get_precision(device, dtype)
        assert_close(output1, output2, rtol=tol_val, atol=tol_val)
Exemplo n.º 4
0
    def test_unit(self, device, dtype):
        data = torch.tensor(
            [[[0., 1., 0.69396782, 0.63581685, 0.09902618],
              [0.59459005, 0.74215373, 0.89662376, 0.25920381, 0.89937686],
              [0.29857584, 0.28139791, 0.16441015, 0.55507519, 0.06124221],
              [0.40908658, 0.10261389, 0.01691456, 0.76006799, 0.32971736]],
             [[0., 1., 0.79009938, 0.91742945, 0.60044175],
              [0.42812678, 0.18552390, 0.04186043, 0.38030245, 0.15420346],
              [0.13552373, 0.53955473, 0.79102736, 0.49050815, 0.75271446],
              [0.39861023, 0.80680277, 0.82823833, 0.54438462, 0.22063386]],
             [[0., 1., 0.84317145, 0.59529881, 0.15297393],
              [0.59235313, 0.36617295, 0.34600773, 0.40304737, 0.61720451],
              [0.46040250, 0.42006640, 0.54765106, 0.48982632, 0.13914755],
              [0.58402964, 0.89597990, 0.98276161, 0.25019163, 0.69285921]]],
            device=device,
            dtype=dtype)

        # Reference output generated using skimage: rgb2luv(data)
        expected = torch.tensor([
            [[0., 100., 79.75208282, 86.38912964, 55.25164032],
             [51.66668701, 43.81214523, 48.93865585, 39.03804398, 52.55152512],
             [23.71140671, 52.38661957, 72.54607391, 53.89587402, 67.94892883],
             [45.02897263, 75.98315430, 78.25762177, 61.85069656, 33.77972794]
             ],
            [[-0., -0.00054950, -13.54032803, -35.42317200, -49.27433014],
             [
                 21.34596062, 94.13956451, 137.11340332, -14.69241238,
                 102.94833374
             ],
             [
                 9.55611229, -30.01761436, -58.94236755, 9.83261871,
                 -62.96137619
             ],
             [
                 -1.55336237, -55.22497559, -56.21067810, 43.76751328,
                 1.46367633
             ]],
            [[-0., 0.00766720, -13.74480152, 52.17128372, 60.92724228],
             [
                 -27.01125526, -1.72837746, 6.57535267, -7.83582020,
                 -38.45543289
             ],
             [-50.89970779, 17.65329361, 36.54148102, 2.25501800, 78.93702698],
             [
                 -38.39783859, -31.71204376, -46.63606644, 50.16629410,
                 -84.74416351
             ]]
        ],
                                device=device,
                                dtype=dtype)

        tol_val: float = _get_precision(device, dtype)
        assert_allclose(kornia.color.rgb_to_luv(data),
                        expected,
                        rtol=tol_val,
                        atol=tol_val)
Exemplo n.º 5
0
    def test_magnitude_hyst(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],
                        [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
                        [1.0000, 0.0000, 0.0000, 1.0000, 0.0000],
                        [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
                        [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
                    ]
                ]
            ],
            device=device,
            dtype=dtype,
        )

        magnitude, edges = kornia.filters.canny(inp, hysteresis=True)

        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.º 6
0
    def test_magnitude(self, device, dtype):
        inp = torch.tensor(
            [
                [
                    [
                        [0.0, 0.0, 0.0, 0.0, 0.0],
                        [0.0, 0.0, 1.0, 0.0, 0.0],
                        [0.0, 1.0, 1.0, 1.0, 0.0],
                        [0.0, 0.0, 1.0, 0.0, 0.0],
                        [0.0, 0.0, 0.0, 0.0, 0.0],
                    ]
                ]
            ],
            device=device,
            dtype=dtype,
        )

        expected_magnitude = torch.tensor(
            [
                [
                    [
                        [0.0, 0.0, 0.0, 0.0, 0.0],
                        [0.0, 1.2458, 0.9672, 1.2458, 0.0],
                        [0.0, 0.9672, 0.0, 0.9672, 0.0],
                        [0.0, 1.2458, 0.9672, 1.2458, 0.0],
                        [0.0, 0.0, 0.0, 0.0, 0.0],
                    ]
                ]
            ],
            device=device,
            dtype=dtype,
        )

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

        magnitude, edges = kornia.filters.canny(inp)

        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.º 7
0
    def test_zca_whiten_func_unbiased(self, unbiased, device, dtype):

        data = torch.tensor([[0, 1], [1, 0], [-1, 0], [0, -1]], device=device, dtype=dtype)

        if unbiased:
            unbiased_val = 1.5
        else:
            unbiased_val = 2.0

        expected = torch.sqrt(unbiased_val * torch.abs(data)) * torch.sign(data)

        actual = kornia.zca_whiten(data, unbiased=unbiased)

        tol_val: float = utils._get_precision(device, dtype)
        assert_close(actual, expected, atol=tol_val, rtol=tol_val)
Exemplo n.º 8
0
    def test_zca_unbiased(self, unbiased, device, dtype):

        data = torch.tensor([[0, 1], [1, 0], [-1, 0], [0, -1]],
                            device=device,
                            dtype=dtype)

        if unbiased:
            unbiased_val = 1.5
        else:
            unbiased_val = 2.0

        expected = torch.sqrt(
            unbiased_val * torch.abs(data)) * torch.sign(data)

        zca = kornia.enhance.ZCAWhitening(unbiased=unbiased).fit(data)

        actual = zca(data)

        tol_val: float = utils._get_precision(device, dtype)
        assert_allclose(actual, expected, rtol=tol_val, atol=tol_val)