test_vision.py 2.8 KB
Newer Older
1 2 3 4 5 6
import time

import numpy as np
import pytest

from megengine import Tensor
7
from megengine.functional import mean, zeros
8 9 10 11
from megengine.module import (
    AdditiveGaussianNoise,
    AdditiveLaplaceNoise,
    AdditivePoissonNoise,
12 13 14
    Emboss,
    LinearContrast,
    Sharpen,
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
)


@pytest.mark.parametrize(
    "cls", [AdditiveGaussianNoise, AdditiveLaplaceNoise, AdditivePoissonNoise]
)
@pytest.mark.parametrize("per_channel", [False, True])
@pytest.mark.parametrize(
    "shape, format",
    [
        ((128, 3, 160, 160), "default"),
        ((128, 160, 160, 3), "nhwc"),
        ((128, 3, 160, 160), "nchw"),
    ],
)
@pytest.mark.parametrize("seed", [1024, None])
def test_AdditiveNoise(cls, per_channel, shape, format, seed):
    if not per_channel and format == "default":
        return

    input_tensor = Tensor(
        np.random.random(shape), np.float32, device="xpux", format=format
    )

    aug = cls(per_channel=per_channel, seed=seed)
    aug_data = aug(input_tensor)
    if seed is not None:  # fix rng seed
        aug_ref = cls(per_channel=per_channel, seed=seed)
        aug_data_ref = aug_ref(input_tensor)
        np.testing.assert_allclose(aug_data, aug_data_ref)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90


@pytest.mark.parametrize("cls", [Emboss, Sharpen])
@pytest.mark.parametrize(
    "shape, format, dtype",
    [
        ((128, 2, 160, 160), "default", np.uint8),
        ((128, 2, 160, 160), "default", np.float32),
    ],
)
@pytest.mark.parametrize(
    "param1, param2", [(0.5, 0.7), (0.6, 0.8), ((0.6, 0.8), (0.6, 0.8)),],
)
@pytest.mark.parametrize("seed", [1024, None])
def test_blur(cls, shape, format, dtype, param1, param2, seed):
    input_array = np.random.randint(0, 255, size=shape).astype(dtype)
    input_tensor = Tensor(input_array, device="xpux", format=format)

    aug = cls(param1, param2, seed=seed)
    aug_data = aug(input_tensor)
    if seed is not None:  # fix rng seed
        aug_ref = cls(param1, param2, seed=seed)
        aug_data_ref = aug_ref(input_tensor)
        np.testing.assert_allclose(aug_data, aug_data_ref)


@pytest.mark.parametrize("per_channel", [False, True])
@pytest.mark.parametrize(
    "shape, format, dtype",
    [
        ((128, 2, 160, 160), "default", np.uint8),
        ((128, 2, 160, 160), "default", np.float32),
    ],
)
@pytest.mark.parametrize("param1", [0.6, 0.8, (0.6, 0.8)])
@pytest.mark.parametrize("seed", [1024, None])
def test_LinearContrast(per_channel, shape, format, dtype, param1, seed):
    input_array = np.random.randint(0, 255, size=shape).astype(dtype)
    input_tensor = Tensor(input_array, device="xpux", format=format)

    aug = LinearContrast(param1, per_channel=per_channel, seed=seed)
    aug_data = aug(input_tensor)
    if seed is not None:  # fix rng seed
        aug_ref = LinearContrast(param1, per_channel=per_channel, seed=seed)
        aug_data_ref = aug_ref(input_tensor)
        np.testing.assert_allclose(aug_data, aug_data_ref)