Пример #1
0
    def test_other_dtypes_sigma_0(self):
        dtypes_to_test_list = [[
            "bool", "uint8", "uint16", "uint32", "uint64", "int8", "int16",
            "int32", "int64", "float16", "float32", "float64", "float128"
        ],
                               [
                                   "bool", "uint8", "uint16", "uint32",
                                   "uint64", "int8", "int16", "int32", "int64",
                                   "float16", "float32", "float64", "float128"
                               ]]
        for backend, dtypes_to_test in zip(["scipy", "cv2"],
                                           dtypes_to_test_list):
            # bool
            if "bool" in dtypes_to_test:
                with self.subTest(backend=backend, dtype="bool"):
                    image = np.zeros((3, 3), dtype=bool)
                    image[1, 1] = True
                    image_aug = iaa.blur_gaussian_(np.copy(image),
                                                   sigma=0,
                                                   backend=backend)
                    assert image_aug.dtype.name == "bool"
                    assert np.all(image_aug == image)

            # uint, int
            for dtype in [
                    np.uint8, np.uint16, np.uint32, np.uint64, np.int8,
                    np.int16, np.int32, np.int64
            ]:
                dtype = np.dtype(dtype)
                if dtype.name in dtypes_to_test:
                    with self.subTest(backend=backend, dtype=dtype.name):
                        _min_value, center_value, _max_value = iadt.get_value_range_of_dtype(
                            dtype)
                        image = np.zeros((3, 3), dtype=dtype)
                        image[1, 1] = int(center_value)
                        image_aug = iaa.blur_gaussian_(np.copy(image),
                                                       sigma=0,
                                                       backend=backend)
                        assert image_aug.dtype.name == dtype.name
                        assert np.all(image_aug == image)

            # float
            for dtype in [np.float16, np.float32, np.float64, np.float128]:
                dtype = np.dtype(dtype)
                if dtype.name in dtypes_to_test:
                    with self.subTest(backend=backend, dtype=dtype.name):
                        _min_value, center_value, _max_value = iadt.get_value_range_of_dtype(
                            dtype)
                        image = np.zeros((3, 3), dtype=dtype)
                        image[1, 1] = center_value
                        image_aug = iaa.blur_gaussian_(np.copy(image),
                                                       sigma=0,
                                                       backend=backend)
                        assert image_aug.dtype.name == dtype.name
                        assert np.allclose(image_aug, image)
Пример #2
0
    def test_other_dtypes_uint_int_non_identity_matrix_with_large_values(self):
        matrix = np.float64([
            [0, 0.5, 0],
            [0, 0.5, 0],
            [0,   0, 0]
        ])
        aug = iaa.Convolve(matrix=matrix)

        for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
            _min_value, center_value, max_value = \
                iadt.get_value_range_of_dtype(dtype)

            value = int(center_value + 0.4 * max_value)

            image = np.zeros((3, 3), dtype=dtype)
            image[1, 1] = value
            image[2, 1] = value
            image_aug = aug.augment_image(image)

            expected = np.zeros((3, 3), dtype=dtype)
            expected[0, 1] = int(np.round(value * 0.5))
            expected[1, 1] = int(np.round(value * 0.5))
            expected[2, 1] = int(np.round(value * 0.5 + value * 0.5))

            diff = np.abs(
                image_aug.astype(np.int64)
                - expected.astype(np.int64))
            assert image_aug.dtype.type == dtype
            assert np.max(diff) <= 2
 def test_other_dtypes_uint_int(self):
     aug = self.create_aug(1.0)
     dtypes = ["uint8", "uint16", "uint32", "uint64",
               "int8", "int32", "int64"]
     for dtype in dtypes:
         with self.subTest(dtype=dtype):
             min_value, center_value, max_value = \
                 iadt.get_value_range_of_dtype(dtype)
             value = max_value
             image = self.create_arr(value, dtype)
             expected = self.create_arr_flipped(value, dtype)
             image_aug = aug.augment_image(image)
             assert image_aug.dtype.name == dtype
             assert np.array_equal(image_aug, expected)
    def test_uint_int_faithful(self):
        dts = ["uint8", "uint16", "uint32", "uint64",
               "int8", "int16", "int32", "int64"]
        for dt in dts:
            with self.subTest(dtype=dt):
                dt = np.dtype(dt)
                minv, center, maxv = iadt.get_value_range_of_dtype(dt)
                center = int(center)
                arr = np.array([[minv, center, maxv]], dtype=dt)

                arr_flipped = fliplib.fliplr(arr)

                expected = np.array([[maxv, center, minv]], dtype=dt)
                assert arr_flipped.dtype.name == dt.name
                assert arr_flipped.shape == (1, 3)
                assert np.array_equal(arr_flipped, expected)
Пример #5
0
    def test_float_faithful_to_min_max(self):
        dts = ["float16", "float32", "float64", "float128"]
        for dt in dts:
            with self.subTest(dtype=dt):
                dt = np.dtype(dt)
                minv, center, maxv = iadt.get_value_range_of_dtype(dt)
                center = int(center)
                atol = 1e-4 if dt.name == "float16" else 1e-8
                arr = np.array([[minv, center, maxv]], dtype=dt)

                arr_flipped = fliplib.fliplr(arr)

                expected = np.array([[maxv, center, minv]], dtype=dt)
                assert arr_flipped.dtype.name == dt.name
                assert arr_flipped.shape == (1, 3)
                assert np.allclose(arr_flipped, expected, rtol=0, atol=atol)
Пример #6
0
    def test_other_dtypes_uint_int(self):
        for dtype in [np.uint8, np.uint16, np.uint32,
                      np.int8, np.int16, np.int32]:
            min_value, center_value, max_value = \
                iadt.get_value_range_of_dtype(dtype)

            if np.dtype(dtype).kind == "i":
                values = [int(center_value), int(0.1 * max_value),
                          int(0.2 * max_value), int(0.5 * max_value),
                          max_value-100]
                values = [((-1)*value, value) for value in values]
            else:
                values = [(0, int(center_value)),
                          (10, int(0.1 * max_value)),
                          (10, int(0.2 * max_value)),
                          (10, int(0.5 * max_value)),
                          (0, max_value),
                          (int(center_value),
                           max_value)]

            for v1, v2 in values:
                aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
                img = np.array([
                    [v1, v1, v2, v2],
                    [v1, v1, v2, v2]
                ], dtype=dtype)
                img_aug = aug.augment_image(img)
                assert img_aug.dtype == np.dtype(dtype)
                assert np.array_equal(img_aug, img)

                aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
                img = np.array([
                    [v2, v2, v2, v2],
                    [v1, v2, v2, v2]
                ], dtype=dtype)
                img_aug = aug.augment_image(img)
                assert img_aug.dtype == np.dtype(dtype)
                assert np.all(img_aug == int(np.round((7/8)*v2 + (1/8)*v1)))
Пример #7
0
def test_Convolve():
    reseed()

    img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    img = np.uint8(img)

    # matrix is None
    aug = iaa.Convolve(matrix=None)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: [None])
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    # matrix is [[1]]
    aug = iaa.Convolve(matrix=np.float32([[1]]))
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    aug = iaa.Convolve(
        matrix=lambda _img, nb_channels, random_state: np.float32([[1]]))
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    # matrix is [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    m = np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]])
    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]]
    m = np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]])
    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, 2 * img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, 2 * img)

    # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]]
    # with 3 channels
    m = np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]])
    img3 = np.tile(img[..., np.newaxis], (1, 1, 3))
    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img3)
    assert np.array_equal(observed, 2 * img3)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img3)
    assert np.array_equal(observed, 2 * img3)

    # matrix is [[0, -1, 0], [0, 10, 0], [0, 0, 0]]
    m = np.float32([[0, -1, 0], [0, 10, 0], [0, 0, 0]])
    expected = np.uint8(
        [[10 * 1 + (-1) * 4, 10 * 2 + (-1) * 5, 10 * 3 + (-1) * 6],
         [10 * 4 + (-1) * 1, 10 * 5 + (-1) * 2, 10 * 6 + (-1) * 3],
         [10 * 7 + (-1) * 4, 10 * 8 + (-1) * 5, 10 * 9 + (-1) * 6]])

    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, expected)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, expected)

    # changing matrices when using callable
    expected = []
    for i in sm.xrange(5):
        expected.append(img * i)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: np.
                       float32([[random_state.randint(0, 5)]]))
    seen = [False] * 5
    for _ in sm.xrange(200):
        observed = aug.augment_image(img)
        found = False
        for i, expected_i in enumerate(expected):
            if np.array_equal(observed, expected_i):
                seen[i] = True
                found = True
                break
        assert found
        if all(seen):
            break
    assert all(seen)

    # bad datatype for matrix
    got_exception = False
    try:
        aug = iaa.Convolve(matrix=False)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # get_parameters()
    matrix = np.int32([[1]])
    aug = iaa.Convolve(matrix=matrix)
    params = aug.get_parameters()
    assert np.array_equal(params[0], matrix)
    assert params[1] == "constant"

    # TODO add test for keypoints once their handling was improved in Convolve

    ###################
    # test other dtypes
    ###################
    identity_matrix = np.int64([[1]])
    aug = iaa.Convolve(matrix=identity_matrix)

    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image_aug = aug.augment_image(image)
    assert image.dtype.type == np.bool_
    assert np.all(image_aug == image)

    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100
        image_aug = aug.augment_image(image)
        assert image.dtype.type == dtype
        assert np.all(image_aug == image)

    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100.0
        image_aug = aug.augment_image(image)
        assert image.dtype.type == dtype
        assert np.allclose(image_aug, image)

    # ----
    # non-identity matrix
    # ----
    matrix = np.float64([[0, 0.6, 0], [0, 0.4, 0], [0, 0, 0]])
    aug = iaa.Convolve(matrix=matrix)

    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image[2, 1] = True
    expected = np.zeros((3, 3), dtype=bool)
    expected[0, 1] = True
    expected[2, 1] = True
    image_aug = aug.augment_image(image)
    assert image.dtype.type == np.bool_
    assert np.all(image_aug == expected)

    matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]])
    aug = iaa.Convolve(matrix=matrix)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100
        image[2, 1] = 100
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = int(np.round(100 * 0.5))
        expected[1, 1] = int(np.round(100 * 0.5))
        expected[2, 1] = int(np.round(100 * 0.5 + 100 * 0.5))

        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 2

    # float
    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100.0
        image[2, 1] = 100.0
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = 100 * 0.5
        expected[1, 1] = 100 * 0.5
        expected[2, 1] = 100 * 0.5 + 100 * 0.5

        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 1.0

    # ----
    # non-identity matrix, higher values
    # ----
    matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]])
    aug = iaa.Convolve(matrix=matrix)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        _min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        value = int(center_value + 0.4 * max_value)

        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 1] = value
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = int(np.round(value * 0.5))
        expected[1, 1] = int(np.round(value * 0.5))
        expected[2, 1] = int(np.round(value * 0.5 + value * 0.5))

        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 2

    # float
    for dtype, value in zip([np.float16, np.float32, np.float64],
                            [5000, 1000 * 1000, 1000 * 1000 * 1000]):
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 1] = value
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = value * 0.5
        expected[1, 1] = value * 0.5
        expected[2, 1] = value * 0.5 + value * 0.5

        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 1.0

    # assert failure on invalid dtypes
    aug = iaa.Convolve(matrix=identity_matrix)
    for dt in [np.uint32, np.uint64, np.int32, np.int64]:
        got_exception = False
        try:
            _ = aug.augment_image(np.zeros((1, 1), dtype=dt))
        except Exception as exc:
            assert "forbidden dtype" in str(exc)
            got_exception = True
        assert got_exception
Пример #8
0
def test_blend_alpha():
    img_fg = np.full((3, 3, 1), 0, dtype=bool)
    img_bg = np.full((3, 3, 1), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, 1.0, eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 1)
    assert np.all(img_blend == 0)

    img_fg = np.full((3, 3, 1), 0, dtype=bool)
    img_bg = np.full((3, 3, 1), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, 0.0, eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 1)
    assert np.all(img_blend == 1)

    img_fg = np.full((3, 3, 1), 0, dtype=bool)
    img_bg = np.full((3, 3, 1), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, 0.3, eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 1)
    assert np.all(img_blend == 1)

    img_fg = np.full((3, 3, 2), 0, dtype=bool)
    img_bg = np.full((3, 3, 2), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, [1.0, 0.0], eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 2)
    assert np.all(img_blend[:, :, 0] == 0)
    assert np.all(img_blend[:, :, 1] == 1)

    for dtype in [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(dtype)
        values = [
            (0, 0),
            (0, 10),
            (10, 20),
            (min_value, min_value),
            (max_value, max_value),
            (min_value, max_value),
            (min_value, int(center_value)),
            (int(center_value), max_value),
            (int(center_value + 0.20 * max_value), max_value),
            (int(center_value + 0.27 * max_value), max_value),
            (int(center_value + 0.40 * max_value), max_value),
            (min_value, 0),
            (0, max_value)
        ]
        values = values + [(v2, v1) for v1, v2 in values]

        for v1, v2 in values:
            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 1.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert np.all(img_blend == dtype(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.99, eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert np.all(img_blend == dtype(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert np.all(img_blend == dtype(v2))

            # TODO this test breaks for numpy <1.15 -- why?
            for c in sm.xrange(3):
                img_fg = np.full((3, 3, c), v1, dtype=dtype)
                img_bg = np.full((3, 3, c), v2, dtype=dtype)
                img_blend = blend.blend_alpha(img_fg, img_bg, 0.75, eps=0)
                assert img_blend.dtype.name == np.dtype(dtype)
                assert img_blend.shape == (3, 3, c)
                for ci in sm.xrange(c):
                    v_blend = min(max(int(0.75*np.float128(v1) + 0.25*np.float128(v2)), min_value), max_value)
                    diff = v_blend - img_blend if v_blend > img_blend[0, 0, ci] else img_blend - v_blend
                    assert np.all(diff < 1.01)

            img_fg = np.full((3, 3, 2), v1, dtype=dtype)
            img_bg = np.full((3, 3, 2), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.75, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 2)
            v_blend = min(max(int(0.75 * np.float128(v1) + 0.25 * np.float128(v2)), min_value), max_value)
            diff = v_blend - img_blend if v_blend > img_blend[0, 0, 0] else img_blend - v_blend
            assert np.all(diff < 1.01)

            img_fg = np.full((3, 3, 2), v1, dtype=dtype)
            img_bg = np.full((3, 3, 2), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, [1.0, 0.0], eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 2)
            assert np.all(img_blend[:, :, 0] == dtype(v1))
            assert np.all(img_blend[:, :, 1] == dtype(v2))

            # elementwise, alphas.shape = (1, 2)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2), dtype=np.float64)
            alphas[:, :] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert np.all(img_blend[0, 0, :] == dtype(v1))
            assert np.all(img_blend[0, 1, :] == dtype(v2))

            # elementwise, alphas.shape = (1, 2, 1)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 1), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert np.all(img_blend[0, 0, :] == dtype(v1))
            assert np.all(img_blend[0, 1, :] == dtype(v2))

            # elementwise, alphas.shape = (1, 2, 3)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 3), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            alphas[:, :, 1] = [0.0, 1.0]
            alphas[:, :, 2] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert np.all(img_blend[0, 0, [0, 2]] == dtype(v1))
            assert np.all(img_blend[0, 1, [0, 2]] == dtype(v2))
            assert np.all(img_blend[0, 0, 1] == dtype(v2))
            assert np.all(img_blend[0, 1, 1] == dtype(v1))

    for dtype in [np.float16, np.float32, np.float64]:
        def _allclose(a, b):
            atol = 1e-4 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        isize = np.dtype(dtype).itemsize
        max_value = 1000 ** (isize - 1)
        min_value = -max_value
        center_value = 0
        values = [
            (0, 0),
            (0, 10),
            (10, 20),
            (min_value, min_value),
            (max_value, max_value),
            (min_value, max_value),
            (min_value, center_value),
            (center_value, max_value),
            (center_value + 0.20 * max_value, max_value),
            (center_value + 0.27 * max_value, max_value),
            (center_value + 0.40 * max_value, max_value),
            (min_value, 0),
            (0, max_value)
        ]
        values = values + [(v2, v1) for v1, v2 in values]

        max_float_dt = np.float128

        for v1, v2 in values:
            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 1.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert _allclose(img_blend, max_float_dt(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.99, eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert _allclose(img_blend, max_float_dt(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert _allclose(img_blend, max_float_dt(v2))

            for c in sm.xrange(3):
                img_fg = np.full((3, 3, c), v1, dtype=dtype)
                img_bg = np.full((3, 3, c), v2, dtype=dtype)
                img_blend = blend.blend_alpha(img_fg, img_bg, 0.75, eps=0)
                assert img_blend.dtype.name == np.dtype(dtype)
                assert img_blend.shape == (3, 3, c)
                assert _allclose(img_blend, 0.75*max_float_dt(v1) + 0.25*max_float_dt(v2))

            img_fg = np.full((3, 3, 2), v1, dtype=dtype)
            img_bg = np.full((3, 3, 2), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, [1.0, 0.0], eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 2)
            assert _allclose(img_blend[:, :, 0], max_float_dt(v1))
            assert _allclose(img_blend[:, :, 1], max_float_dt(v2))

            # elementwise, alphas.shape = (1, 2)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2), dtype=np.float64)
            alphas[:, :] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert _allclose(img_blend[0, 0, :], dtype(v1))
            assert _allclose(img_blend[0, 1, :], dtype(v2))

            # elementwise, alphas.shape = (1, 2, 1)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 1), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert _allclose(img_blend[0, 0, :], dtype(v1))
            assert _allclose(img_blend[0, 1, :], dtype(v2))

            # elementwise, alphas.shape = (1, 2, 3)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 3), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            alphas[:, :, 1] = [0.0, 1.0]
            alphas[:, :, 2] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert _allclose(img_blend[0, 0, [0, 2]], dtype(v1))
            assert _allclose(img_blend[0, 1, [0, 2]], dtype(v2))
            assert _allclose(img_blend[0, 0, 1], dtype(v2))
            assert _allclose(img_blend[0, 1, 1], dtype(v1))
Пример #9
0
 def test_int16(self):
     minv, center, maxv = iadt.get_value_range_of_dtype(np.dtype("int16"))
     assert minv == -32768
     assert np.isclose(center, -0.5)
     assert maxv == 32767
Пример #10
0
 def test_float16(self):
     minv, center, maxv = iadt.get_value_range_of_dtype(np.dtype("float16"))
     assert minv < 100.0
     assert np.isclose(center, 0.0)
     assert maxv > 100.0
Пример #11
0
 def test_uint16(self):
     minv, center, maxv = iadt.get_value_range_of_dtype(np.dtype("uint16"))
     assert minv == 0
     assert np.isclose(center, 0.5 * 65535)
     assert maxv == 65535
Пример #12
0
 def test_int8(self):
     minv, center, maxv = iadt.get_value_range_of_dtype(np.dtype("int8"))
     assert minv == -128
     assert np.isclose(center, -0.5)
     assert maxv == 127
Пример #13
0
 def test_bool(self):
     minv, center, maxv = iadt.get_value_range_of_dtype(np.dtype(bool))
     assert minv == 0
     assert center is None
     assert maxv == 1
Пример #14
0
 def test_uint8_string_name(self):
     assert (iadt.get_value_range_of_dtype("uint8") ==
             iadt.get_value_range_of_dtype(np.dtype("uint8")))
Пример #15
0
def test_GaussianBlur():
    reseed()

    base_img = np.array([[0, 0, 0], [0, 255, 0], [0, 0, 0]], dtype=np.uint8)
    base_img = base_img[:, :, np.newaxis]

    images = np.array([base_img])
    images_list = [base_img]
    outer_pixels = ([], [])
    for i in sm.xrange(base_img.shape[0]):
        for j in sm.xrange(base_img.shape[1]):
            if i != j:
                outer_pixels[0].append(i)
                outer_pixels[1].append(j)

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=0, y=0),
            ia.Keypoint(x=1, y=1),
            ia.Keypoint(x=2, y=2)
        ],
                            shape=base_img.shape)
    ]

    # no blur, shouldnt change anything
    aug = iaa.GaussianBlur(sigma=0)

    observed = aug.augment_images(images)
    expected = images
    assert np.array_equal(observed, expected)

    # weak blur of center pixel
    aug = iaa.GaussianBlur(sigma=0.5)
    aug_det = aug.to_deterministic()

    # images as numpy array
    observed = aug.augment_images(images)
    assert 100 < observed[0][1, 1] < 255
    assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all()
    assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all()

    observed = aug_det.augment_images(images)
    assert 100 < observed[0][1, 1] < 255
    assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all()
    assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all()

    # images as list
    observed = aug.augment_images(images_list)
    assert 100 < observed[0][1, 1] < 255
    assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all()
    assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all()

    observed = aug_det.augment_images(images_list)
    assert 100 < observed[0][1, 1] < 255
    assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all()
    assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all()

    # keypoints shouldnt be changed
    observed = aug.augment_keypoints(keypoints)
    expected = keypoints
    assert keypoints_equal(observed, expected)

    observed = aug_det.augment_keypoints(keypoints)
    expected = keypoints
    assert keypoints_equal(observed, expected)

    # varying blur sigmas
    aug = iaa.GaussianBlur(sigma=(0, 1))
    aug_det = aug.to_deterministic()

    last_aug = None
    last_aug_det = None
    nb_changed_aug = 0
    nb_changed_aug_det = 0
    nb_iterations = 1000
    for i in sm.xrange(nb_iterations):
        observed_aug = aug.augment_images(images)
        observed_aug_det = aug_det.augment_images(images)
        if i == 0:
            last_aug = observed_aug
            last_aug_det = observed_aug_det
        else:
            if not np.array_equal(observed_aug, last_aug):
                nb_changed_aug += 1
            if not np.array_equal(observed_aug_det, last_aug_det):
                nb_changed_aug_det += 1
            last_aug = observed_aug
            last_aug_det = observed_aug_det
    assert nb_changed_aug >= int(nb_iterations * 0.8)
    assert nb_changed_aug_det == 0

    #############################
    # test other dtypes below
    #############################

    # --
    # blur of various dtypes at sigma=0
    # --
    aug = iaa.GaussianBlur(sigma=0)

    # bool
    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image_aug = aug.augment_image(image)
    assert image_aug.dtype.type == np.bool_
    assert np.all(image_aug == image)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]:
        _min_value, center_value, _max_value = iadt.get_value_range_of_dtype(
            dtype)
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = int(center_value)
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.all(image_aug == image)

    # float
    for dtype in [np.float16, np.float32, np.float64]:
        _min_value, center_value, _max_value = iadt.get_value_range_of_dtype(
            dtype)
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = center_value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.allclose(image_aug, image)

    # --
    # blur of bool input at sigma=0.6
    # --
    # here we use a special mask and sigma as otherwise the only values ending up with >0.5 would be the ones that
    # were before the blur already at >0.5
    # prototype kernel, generated via:
    #  mask = np.zeros((5, 5), dtype=np.float64)
    #  mask[1, 0] = 255
    #  mask[2, 0] = 255
    #  mask[2, 2] = 255
    #  mask[2, 4] = 255
    #  mask[3, 0] = 255
    #  mask = ndimage.gaussian_filter(mask, 1.0, mode="mirror")
    aug = iaa.GaussianBlur(sigma=0.6)

    mask_bool = np.float64([[57, 14, 2, 1, 1], [142, 42, 29, 14, 28],
                            [169, 69, 114, 56, 114], [142, 42, 29, 14, 28],
                            [57, 14, 2, 1, 1]]) / 255.0

    image = np.zeros((5, 5), dtype=bool)
    image[1, 0] = True
    image[2, 0] = True
    image[2, 2] = True
    image[2, 4] = True
    image[3, 0] = True
    image_aug = aug.augment_image(image)
    expected = mask_bool > 0.5
    assert image_aug.shape == mask_bool.shape
    assert image_aug.dtype.type == np.bool_
    assert np.all(image_aug == expected)

    # --
    # blur of various dtypes at sigma=1.0
    # and using an example value of 100 for int/uint/float and True for bool
    # --
    # prototype kernel, generated via:
    #  mask = np.zeros((5, 5), dtype=np.float64)
    #  mask[2, 2] = 100
    #  mask = ndimage.gaussian_filter(mask, 1.0, mode="mirror")
    aug = iaa.GaussianBlur(sigma=1.0)

    mask = np.float64([[1, 2, 3, 2, 1], [2, 5, 9, 5, 2], [4, 9, 15, 9, 4],
                       [2, 5, 9, 5, 2], [1, 2, 3, 2, 1]])

    # uint, int
    for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]:
        image = np.zeros((5, 5), dtype=dtype)
        image[2, 2] = 100
        image_aug = aug.augment_image(image)
        expected = mask.astype(dtype)
        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.shape == mask.shape
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 4
        assert np.average(diff) <= 2

    # float
    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((5, 5), dtype=dtype)
        image[2, 2] = 100.0
        image_aug = aug.augment_image(image)
        expected = mask.astype(dtype)
        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.shape == mask.shape
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 4
        assert np.average(diff) < 2.0

    # --
    # blur of various dtypes at sigma=0.4
    # and using an example value of 100 for int/uint/float and True for bool
    # --
    aug = iaa.GaussianBlur(sigma=0.4)

    # prototype kernel, generated via:
    #  mask = np.zeros((5, 5), dtype=np.uint8)
    #  mask[2, 2] = 100
    #  kernel = ndimage.gaussian_filter(mask, 0.4, mode="mirror")
    mask = np.float64([[0, 0, 0, 0, 0], [0, 0, 3, 0, 0], [0, 3, 83, 3, 0],
                       [0, 0, 3, 0, 0], [0, 0, 0, 0, 0]])

    # uint, int
    for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]:
        image = np.zeros((5, 5), dtype=dtype)
        image[2, 2] = 100
        image_aug = aug.augment_image(image)
        expected = mask.astype(dtype)
        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.shape == mask.shape
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 4

    # float
    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((5, 5), dtype=dtype)
        image[2, 2] = 100.0
        image_aug = aug.augment_image(image)
        expected = mask.astype(dtype)
        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.shape == mask.shape
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 4.0

    # --
    # blur of various dtypes at sigma=0.75
    # and values being half-way between center and maximum for each dtype
    # The goal of this test is to verify that no major loss of resolution happens for large dtypes.
    # Such inaccuracies appear for float64 if used.
    # --
    aug = iaa.GaussianBlur(sigma=0.75)

    # prototype kernel, generated via:
    # mask = np.zeros((5, 5), dtype=np.int32)
    # mask[2, 2] = 1000 * 1000
    # kernel = ndimage.gaussian_filter(mask, 0.75)
    mask = np.float64([[923, 6650, 16163, 6650, 923],
                       [6650, 47896, 116408, 47896, 6650],
                       [16163, 116408, 282925, 116408, 16163],
                       [6650, 47896, 116408, 47896, 6650],
                       [923, 6650, 16163, 6650, 923]]) / (1000.0 * 1000.0)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        dynamic_range = max_value - min_value

        value = int(center_value + 0.4 * max_value)
        image = np.zeros((5, 5), dtype=dtype)
        image[2, 2] = value
        image_aug = aug.augment_image(image)
        expected = (mask * value).astype(dtype)
        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.shape == mask.shape
        assert image_aug.dtype.type == dtype
        if np.dtype(dtype).itemsize <= 1:
            assert np.max(diff) <= 4
        else:
            assert np.max(diff) <= 0.01 * dynamic_range

    # float
    for dtype, value in zip([np.float16, np.float32, np.float64],
                            [5000, 1000 * 1000, 1000 * 1000 * 1000]):
        image = np.zeros((5, 5), dtype=dtype)
        image[2, 2] = value
        image_aug = aug.augment_image(image)
        expected = (mask * value).astype(dtype)
        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.shape == mask.shape
        assert image_aug.dtype.type == dtype
        # accepts difference of 2.0, 4.0, 8.0, 16.0 (at 1, 2, 4, 8 bytes, i.e. 8, 16, 32, 64 bit)
        assert np.max(
            diff) < np.dtype(dtype).itemsize * 0.01 * np.float128(value)

    # assert failure on invalid dtypes
    aug = iaa.GaussianBlur(sigma=1.0)
    for dt in [np.float128]:
        got_exception = False
        try:
            _ = aug.augment_image(np.zeros((1, 1), dtype=dt))
        except Exception as exc:
            assert "forbidden dtype" in str(exc)
            got_exception = True
        assert got_exception
Пример #16
0
    def test_other_dtypes_sigma_075(self):
        # prototype kernel, generated via:
        # mask = np.zeros((5, 5), dtype=np.int32)
        # mask[2, 2] = 1000 * 1000
        # kernel = ndimage.gaussian_filter(mask, 0.75)
        mask = np.float64([[923, 6650, 16163, 6650, 923],
                           [6650, 47896, 116408, 47896, 6650],
                           [16163, 116408, 282925, 116408, 16163],
                           [6650, 47896, 116408, 47896, 6650],
                           [923, 6650, 16163, 6650, 923]]) / (1000.0 * 1000.0)

        dtypes_to_test_list = [
            # scipy
            [
                "bool", "uint8", "uint16", "uint32", "uint64", "int8", "int16",
                "int32", "int64", "float16", "float32", "float64"
            ],
            # cv2
            [
                "bool", "uint8", "uint16", "int8", "int16", "int32", "float16",
                "float32", "float64"
            ]
        ]
        for backend, dtypes_to_test in zip(["scipy", "cv2"],
                                           dtypes_to_test_list):
            # bool
            if "bool" in dtypes_to_test:
                with self.subTest(backend=backend, dtype="bool"):
                    image = np.zeros((5, 5), dtype=bool)
                    image[2, 2] = True
                    image_aug = iaa.blur_gaussian_(np.copy(image),
                                                   sigma=0.75,
                                                   backend=backend)
                    assert image_aug.dtype.name == "bool"
                    assert np.all(image_aug == (mask > 0.5))

            # uint, int
            for dtype in [
                    np.uint8, np.uint16, np.uint32, np.uint64, np.int8,
                    np.int16, np.int32, np.int64
            ]:
                dtype = np.dtype(dtype)
                if dtype.name in dtypes_to_test:
                    with self.subTest(backend=backend, dtype=dtype.name):
                        min_value, center_value, max_value = iadt.get_value_range_of_dtype(
                            dtype)
                        dynamic_range = max_value - min_value

                        value = int(center_value + 0.4 * max_value)
                        image = np.zeros((5, 5), dtype=dtype)
                        image[2, 2] = value
                        image_aug = iaa.blur_gaussian_(image,
                                                       sigma=0.75,
                                                       backend=backend)
                        expected = (mask * value).astype(dtype)
                        diff = np.abs(
                            image_aug.astype(np.int64) -
                            expected.astype(np.int64))
                        assert image_aug.shape == mask.shape
                        assert image_aug.dtype.type == dtype
                        if dtype.itemsize <= 1:
                            assert np.max(diff) <= 4
                        else:
                            assert np.max(diff) <= 0.01 * dynamic_range

            # float
            values = [5000, 1000**1, 1000**2, 1000**3]
            for dtype, value in zip(
                [np.float16, np.float32, np.float64, np.float128], values):
                dtype = np.dtype(dtype)
                if dtype.name in dtypes_to_test:
                    with self.subTest(backend=backend, dtype=dtype.name):
                        image = np.zeros((5, 5), dtype=dtype)
                        image[2, 2] = value
                        image_aug = iaa.blur_gaussian_(image,
                                                       sigma=0.75,
                                                       backend=backend)
                        expected = (mask * value).astype(dtype)
                        diff = np.abs(
                            image_aug.astype(np.float128) -
                            expected.astype(np.float128))
                        assert image_aug.shape == mask.shape
                        assert image_aug.dtype.type == dtype
                        # accepts difference of 2.0, 4.0, 8.0, 16.0 (at 1, 2, 4, 8 bytes, i.e. 8, 16, 32, 64 bit)
                        assert np.max(diff) < np.dtype(
                            dtype).itemsize * 0.01 * np.float128(value)
Пример #17
0
def test_blend_alpha():
    img_fg = np.full((3, 3, 1), 0, dtype=bool)
    img_bg = np.full((3, 3, 1), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, 1.0, eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 1)
    assert np.all(img_blend == 0)

    img_fg = np.full((3, 3, 1), 0, dtype=bool)
    img_bg = np.full((3, 3, 1), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, 0.0, eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 1)
    assert np.all(img_blend == 1)

    img_fg = np.full((3, 3, 1), 0, dtype=bool)
    img_bg = np.full((3, 3, 1), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, 0.3, eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 1)
    assert np.all(img_blend == 1)

    img_fg = np.full((3, 3, 2), 0, dtype=bool)
    img_bg = np.full((3, 3, 2), 1, dtype=bool)
    img_blend = blend.blend_alpha(img_fg, img_bg, [1.0, 0.0], eps=0)
    assert img_blend.dtype.name == np.dtype(np.bool_)
    assert img_blend.shape == (3, 3, 2)
    assert np.all(img_blend[:, :, 0] == 0)
    assert np.all(img_blend[:, :, 1] == 1)

    for dtype in [
            np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16,
            np.int32, np.int64
    ]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        values = [(0, 0), (0, 10), (10, 20), (min_value, min_value),
                  (max_value, max_value), (min_value, max_value),
                  (min_value, int(center_value)),
                  (int(center_value), max_value),
                  (int(center_value + 0.20 * max_value), max_value),
                  (int(center_value + 0.27 * max_value), max_value),
                  (int(center_value + 0.40 * max_value), max_value),
                  (min_value, 0), (0, max_value)]
        values = values + [(v2, v1) for v1, v2 in values]

        for v1, v2 in values:
            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 1.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert np.all(img_blend == dtype(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.99, eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert np.all(img_blend == dtype(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert np.all(img_blend == dtype(v2))

            # TODO this test breaks for numpy <1.15 -- why?
            for c in sm.xrange(3):
                img_fg = np.full((3, 3, c), v1, dtype=dtype)
                img_bg = np.full((3, 3, c), v2, dtype=dtype)
                img_blend = blend.blend_alpha(img_fg, img_bg, 0.75, eps=0)
                assert img_blend.dtype.name == np.dtype(dtype)
                assert img_blend.shape == (3, 3, c)
                for ci in sm.xrange(c):
                    v_blend = min(
                        max(
                            int(0.75 * np.float128(v1) +
                                0.25 * np.float128(v2)), min_value), max_value)
                    diff = v_blend - img_blend if v_blend > img_blend[
                        0, 0, ci] else img_blend - v_blend
                    assert np.all(diff < 1.01)

            img_fg = np.full((3, 3, 2), v1, dtype=dtype)
            img_bg = np.full((3, 3, 2), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.75, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 2)
            v_blend = min(
                max(int(0.75 * np.float128(v1) + 0.25 * np.float128(v2)),
                    min_value), max_value)
            diff = v_blend - img_blend if v_blend > img_blend[
                0, 0, 0] else img_blend - v_blend
            assert np.all(diff < 1.01)

            img_fg = np.full((3, 3, 2), v1, dtype=dtype)
            img_bg = np.full((3, 3, 2), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, [1.0, 0.0], eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 2)
            assert np.all(img_blend[:, :, 0] == dtype(v1))
            assert np.all(img_blend[:, :, 1] == dtype(v2))

            # elementwise, alphas.shape = (1, 2)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2), dtype=np.float64)
            alphas[:, :] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert np.all(img_blend[0, 0, :] == dtype(v1))
            assert np.all(img_blend[0, 1, :] == dtype(v2))

            # elementwise, alphas.shape = (1, 2, 1)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 1), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert np.all(img_blend[0, 0, :] == dtype(v1))
            assert np.all(img_blend[0, 1, :] == dtype(v2))

            # elementwise, alphas.shape = (1, 2, 3)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 3), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            alphas[:, :, 1] = [0.0, 1.0]
            alphas[:, :, 2] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert np.all(img_blend[0, 0, [0, 2]] == dtype(v1))
            assert np.all(img_blend[0, 1, [0, 2]] == dtype(v2))
            assert np.all(img_blend[0, 0, 1] == dtype(v2))
            assert np.all(img_blend[0, 1, 1] == dtype(v1))

    for dtype in [np.float16, np.float32, np.float64]:

        def _allclose(a, b):
            atol = 1e-4 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        isize = np.dtype(dtype).itemsize
        max_value = 1000**(isize - 1)
        min_value = -max_value
        center_value = 0
        values = [(0, 0), (0, 10), (10, 20), (min_value, min_value),
                  (max_value, max_value), (min_value, max_value),
                  (min_value, center_value), (center_value, max_value),
                  (center_value + 0.20 * max_value, max_value),
                  (center_value + 0.27 * max_value, max_value),
                  (center_value + 0.40 * max_value, max_value), (min_value, 0),
                  (0, max_value)]
        values = values + [(v2, v1) for v1, v2 in values]

        max_float_dt = np.float128

        for v1, v2 in values:
            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 1.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert _allclose(img_blend, max_float_dt(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.99, eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert _allclose(img_blend, max_float_dt(v1))

            img_fg = np.full((3, 3, 1), v1, dtype=dtype)
            img_bg = np.full((3, 3, 1), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, 0.0, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 1)
            assert _allclose(img_blend, max_float_dt(v2))

            for c in sm.xrange(3):
                img_fg = np.full((3, 3, c), v1, dtype=dtype)
                img_bg = np.full((3, 3, c), v2, dtype=dtype)
                img_blend = blend.blend_alpha(img_fg, img_bg, 0.75, eps=0)
                assert img_blend.dtype.name == np.dtype(dtype)
                assert img_blend.shape == (3, 3, c)
                assert _allclose(
                    img_blend,
                    0.75 * max_float_dt(v1) + 0.25 * max_float_dt(v2))

            img_fg = np.full((3, 3, 2), v1, dtype=dtype)
            img_bg = np.full((3, 3, 2), v2, dtype=dtype)
            img_blend = blend.blend_alpha(img_fg, img_bg, [1.0, 0.0], eps=0.1)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (3, 3, 2)
            assert _allclose(img_blend[:, :, 0], max_float_dt(v1))
            assert _allclose(img_blend[:, :, 1], max_float_dt(v2))

            # elementwise, alphas.shape = (1, 2)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2), dtype=np.float64)
            alphas[:, :] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert _allclose(img_blend[0, 0, :], dtype(v1))
            assert _allclose(img_blend[0, 1, :], dtype(v2))

            # elementwise, alphas.shape = (1, 2, 1)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 1), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert _allclose(img_blend[0, 0, :], dtype(v1))
            assert _allclose(img_blend[0, 1, :], dtype(v2))

            # elementwise, alphas.shape = (1, 2, 3)
            img_fg = np.full((1, 2, 3), v1, dtype=dtype)
            img_bg = np.full((1, 2, 3), v2, dtype=dtype)
            alphas = np.zeros((1, 2, 3), dtype=np.float64)
            alphas[:, :, 0] = [1.0, 0.0]
            alphas[:, :, 1] = [0.0, 1.0]
            alphas[:, :, 2] = [1.0, 0.0]
            img_blend = blend.blend_alpha(img_fg, img_bg, alphas, eps=0)
            assert img_blend.dtype.name == np.dtype(dtype)
            assert img_blend.shape == (1, 2, 3)
            assert _allclose(img_blend[0, 0, [0, 2]], dtype(v1))
            assert _allclose(img_blend[0, 1, [0, 2]], dtype(v2))
            assert _allclose(img_blend[0, 0, 1], dtype(v2))
            assert _allclose(img_blend[0, 1, 1], dtype(v1))
Пример #18
0
def test_AverageBlur():
    reseed()

    base_img = np.zeros((11, 11, 1), dtype=np.uint8)
    base_img[5, 5, 0] = 200
    base_img[4, 5, 0] = 100
    base_img[6, 5, 0] = 100
    base_img[5, 4, 0] = 100
    base_img[5, 6, 0] = 100

    blur3x3 = [[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, 0, 0, 0, 0, 0, 0, 0, 0],
               [0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0],
               [0, 0, 0, 11, 44, 56, 44, 11, 0, 0, 0],
               [0, 0, 0, 11, 56, 67, 56, 11, 0, 0, 0],
               [0, 0, 0, 11, 44, 56, 44, 11, 0, 0, 0],
               [0, 0, 0, 0, 11, 11, 11, 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, 0],
               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    blur3x3 = np.array(blur3x3, dtype=np.uint8)[..., np.newaxis]

    blur4x4 = [[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, 0, 0, 0, 0, 0, 0, 0, 0],
               [0, 0, 0, 0, 6, 6, 6, 6, 0, 0, 0],
               [0, 0, 0, 6, 25, 31, 31, 25, 6, 0, 0],
               [0, 0, 0, 6, 31, 38, 38, 31, 6, 0, 0],
               [0, 0, 0, 6, 31, 38, 38, 31, 6, 0, 0],
               [0, 0, 0, 6, 25, 31, 31, 25, 6, 0, 0],
               [0, 0, 0, 0, 6, 6, 6, 6, 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]]
    blur4x4 = np.array(blur4x4, dtype=np.uint8)[..., np.newaxis]

    blur5x5 = [[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, 4, 4, 4, 4, 4, 0, 0, 0],
               [0, 0, 4, 16, 20, 20, 20, 16, 4, 0, 0],
               [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0],
               [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0],
               [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0],
               [0, 0, 4, 16, 20, 20, 20, 16, 4, 0, 0],
               [0, 0, 0, 4, 4, 4, 4, 4, 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]]
    blur5x5 = np.array(blur5x5, dtype=np.uint8)[..., np.newaxis]

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=0, y=0),
            ia.Keypoint(x=1, y=1),
            ia.Keypoint(x=2, y=2)
        ],
                            shape=base_img.shape)
    ]

    # no blur, shouldnt change anything
    aug = iaa.AverageBlur(k=0)
    observed = aug.augment_image(base_img)
    assert np.array_equal(observed, base_img)

    # k=3
    aug = iaa.AverageBlur(k=3)
    observed = aug.augment_image(base_img)
    assert np.array_equal(observed, blur3x3)

    # k=5
    aug = iaa.AverageBlur(k=5)
    observed = aug.augment_image(base_img)
    assert np.array_equal(observed, blur5x5)

    # k as (3, 4)
    aug = iaa.AverageBlur(k=(3, 4))
    nb_iterations = 100
    nb_seen = [0, 0]
    for i in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, blur3x3):
            nb_seen[0] += 1
        elif np.array_equal(observed, blur4x4):
            nb_seen[1] += 1
        else:
            raise Exception("Unexpected result in AverageBlur@1")
    p_seen = [v / nb_iterations for v in nb_seen]
    assert 0.4 <= p_seen[0] <= 0.6
    assert 0.4 <= p_seen[1] <= 0.6

    # k as (3, 5)
    aug = iaa.AverageBlur(k=(3, 5))
    nb_iterations = 100
    nb_seen = [0, 0, 0]
    for i in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, blur3x3):
            nb_seen[0] += 1
        elif np.array_equal(observed, blur4x4):
            nb_seen[1] += 1
        elif np.array_equal(observed, blur5x5):
            nb_seen[2] += 1
        else:
            raise Exception("Unexpected result in AverageBlur@2")
    p_seen = [v / nb_iterations for v in nb_seen]
    assert 0.23 <= p_seen[0] <= 0.43
    assert 0.23 <= p_seen[1] <= 0.43
    assert 0.23 <= p_seen[2] <= 0.43

    # k as stochastic parameter
    aug = iaa.AverageBlur(k=iap.Choice([3, 5]))
    nb_iterations = 100
    nb_seen = [0, 0]
    for i in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, blur3x3):
            nb_seen[0] += 1
        elif np.array_equal(observed, blur5x5):
            nb_seen[1] += 1
        else:
            raise Exception("Unexpected result in AverageBlur@3")
    p_seen = [v / nb_iterations for v in nb_seen]
    assert 0.4 <= p_seen[0] <= 0.6
    assert 0.4 <= p_seen[1] <= 0.6

    # k as ((3, 5), (3, 5))
    aug = iaa.AverageBlur(k=((3, 5), (3, 5)))

    possible = dict()
    for kh in [3, 4, 5]:
        for kw in [3, 4, 5]:
            key = (kh, kw)
            if kh == 0 or kw == 0:
                possible[key] = np.copy(base_img)
            else:
                possible[key] = cv2.blur(base_img, (kh, kw))[..., np.newaxis]

    nb_iterations = 250
    nb_seen = dict([(key, 0) for key, val in possible.items()])
    for i in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        for key, img_aug in possible.items():
            if np.array_equal(observed, img_aug):
                nb_seen[key] += 1
    # dont check sum here, because 0xX and Xx0 are all the same, i.e. much
    # higher sum than nb_iterations
    assert all([v > 0 for v in nb_seen.values()])

    # keypoints shouldnt be changed
    aug = iaa.AverageBlur(k=3)
    aug_det = aug.to_deterministic()
    observed = aug.augment_keypoints(keypoints)
    expected = keypoints
    assert keypoints_equal(observed, expected)

    observed = aug_det.augment_keypoints(keypoints)
    expected = keypoints
    assert keypoints_equal(observed, expected)

    #############################
    # test other dtypes below
    #############################

    # --
    # blur of various dtypes at k=0
    # --
    aug = iaa.AverageBlur(k=0)

    # bool
    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image[2, 2] = True
    image_aug = aug.augment_image(image)
    assert image_aug.dtype.type == np.bool_
    assert np.all(image_aug == image)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        _min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = int(center_value + 0.4 * max_value)
        image[2, 2] = int(center_value + 0.4 * max_value)
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.all(image_aug == image)

    # float
    for dtype, value in zip([np.float16, np.float32, np.float64],
                            [5000, 1000 * 1000, 1000 * 1000 * 1000]):
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.allclose(image_aug, image)

    # --
    # blur of various dtypes at k=3
    # and using an example value of 100 for int/uint/float and True for bool
    # --
    aug = iaa.AverageBlur(k=3)

    # prototype mask
    # we place values in a 3x3 grid at positions (row=1, col=1) and (row=2, col=2) (beginning with 0)
    # AverageBlur uses cv2.blur(), which uses BORDER_REFLECT_101 as its default padding mode,
    # see https://docs.opencv.org/3.1.0/d2/de8/group__core__array.html
    # the matrix below shows the 3x3 grid and the padded row/col values around it
    # [1, 0, 1, 0, 1]
    # [0, 0, 0, 0, 0]
    # [1, 0, 1, 0, 1]
    # [0, 0, 0, 1, 0]
    # [1, 0, 1, 0, 1]
    mask = np.float64([[4 / 9, 2 / 9, 4 / 9], [2 / 9, 2 / 9, 3 / 9],
                       [4 / 9, 3 / 9, 5 / 9]])

    # bool
    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image[2, 2] = True
    image_aug = aug.augment_image(image)
    expected = mask > 0.5
    assert image_aug.dtype.type == np.bool_
    assert np.all(image_aug == expected)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100
        image[2, 2] = 100
        image_aug = aug.augment_image(image)
        expected = np.round(mask * 100).astype(
            dtype)  # cv2.blur() applies rounding for int/uint dtypes
        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 2

    # float
    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100.0
        image[2, 2] = 100.0
        image_aug = aug.augment_image(image)
        expected = (mask * 100.0).astype(dtype)
        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 1.0

    # --
    # blur of various dtypes at k=3
    # and values being half-way between center and maximum for each dtype (bool is skipped as it doesnt make any
    # sense here)
    # The goal of this test is to verify that no major loss of resolution happens for large dtypes.
    # --
    aug = iaa.AverageBlur(k=3)

    # prototype mask (see above)
    mask = np.float64([[4 / 9, 2 / 9, 4 / 9], [2 / 9, 2 / 9, 3 / 9],
                       [4 / 9, 3 / 9, 5 / 9]])

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        _min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        value = int(center_value + 0.4 * max_value)
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 2] = value
        image_aug = aug.augment_image(image)
        expected = (mask * value).astype(dtype)
        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.dtype.type == dtype
        # accepts difference of 4, 8, 16 (at 1, 2, 4 bytes, i.e. 8, 16, 32 bit)
        assert np.max(diff) <= 2**(1 + np.dtype(dtype).itemsize)

    # float
    for dtype, value in zip([np.float16, np.float32, np.float64],
                            [5000, 1000 * 1000, 1000 * 1000 * 1000]):
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 2] = value
        image_aug = aug.augment_image(image)
        expected = (mask * value).astype(dtype)
        diff = np.abs(
            image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.dtype.type == dtype
        # accepts difference of 2.0, 4.0, 8.0, 16.0 (at 1, 2, 4, 8 bytes, i.e. 8, 16, 32, 64 bit)
        assert np.max(diff) < 2**(1 + np.dtype(dtype).itemsize)

    # assert failure on invalid dtypes
    aug = iaa.AverageBlur(k=3)
    for dt in [np.uint32, np.uint64, np.int32, np.int64]:
        got_exception = False
        try:
            _ = aug.augment_image(np.zeros((1, 1), dtype=dt))
        except Exception as exc:
            assert "forbidden dtype" in str(exc)
            got_exception = True
        assert got_exception
Пример #19
0
def test_Superpixels():
    reseed()

    def _array_equals_tolerant(a, b, tolerance):
        diff = np.abs(a.astype(np.int32) - b.astype(np.int32))
        return np.all(diff <= tolerance)

    base_img = [
        [255, 255, 255, 0, 0, 0],
        [255, 235, 255, 0, 20, 0],
        [250, 250, 250, 5, 5, 5]
    ]
    base_img = np.tile(np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3))

    base_img_superpixels = [
        [251, 251, 251, 4, 4, 4],
        [251, 251, 251, 4, 4, 4],
        [251, 251, 251, 4, 4, 4]
    ]
    base_img_superpixels = np.tile(np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis], (1, 1, 3))

    base_img_superpixels_left = np.copy(base_img_superpixels)
    base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :]

    base_img_superpixels_right = np.copy(base_img_superpixels)
    base_img_superpixels_right[:, :3, :] = base_img[:, :3, :]

    aug = iaa.Superpixels(p_replace=0, n_segments=2)
    observed = aug.augment_image(base_img)
    expected = base_img
    assert np.allclose(observed, expected)

    aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
    observed = aug.augment_image(base_img)
    expected = base_img_superpixels
    assert _array_equals_tolerant(observed, expected, 2)

    aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2))
    observed = aug.augment_image(base_img)
    expected = base_img_superpixels
    assert _array_equals_tolerant(observed, expected, 2)

    aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2)
    observed = aug.augment_image(base_img)
    assert np.allclose(observed, base_img) or _array_equals_tolerant(observed, base_img_superpixels, 2)

    aug = iaa.Superpixels(p_replace=0.5, n_segments=2)
    seen = {"none": False, "left": False, "right": False, "both": False}
    for _ in sm.xrange(100):
        observed = aug.augment_image(base_img)
        if _array_equals_tolerant(observed, base_img, 2):
            seen["none"] = True
        elif _array_equals_tolerant(observed, base_img_superpixels_left, 2):
            seen["left"] = True
        elif _array_equals_tolerant(observed, base_img_superpixels_right, 2):
            seen["right"] = True
        elif _array_equals_tolerant(observed, base_img_superpixels, 2):
            seen["both"] = True
        else:
            raise Exception("Generated superpixels image does not match any expected image.")
        if all(seen.values()):
            break
    assert all(seen.values())

    # test exceptions for wrong parameter types
    got_exception = False
    try:
        _ = iaa.Superpixels(p_replace="test", n_segments=100)
    except Exception:
        got_exception = True
    assert got_exception

    got_exception = False
    try:
        _ = iaa.Superpixels(p_replace=1, n_segments="test")
    except Exception:
        got_exception = True
    assert got_exception

    # test get_parameters()
    aug = iaa.Superpixels(p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest")
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Binomial)
    assert isinstance(params[0].p, iap.Deterministic)
    assert isinstance(params[1], iap.Deterministic)
    assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4
    assert params[1].value == 2
    assert params[2] == 100
    assert params[3] == "nearest"

    ###################
    # test other dtypes
    ###################
    # bool
    aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
    img = np.array([
        [False, False, True, True],
        [False, False, True, True]
    ], dtype=bool)
    img_aug = aug.augment_image(img)
    assert img_aug.dtype == img.dtype
    assert np.all(img_aug == img)

    aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
    img = np.array([
        [True, True, True, True],
        [False, True, True, True]
    ], dtype=bool)
    img_aug = aug.augment_image(img)
    assert img_aug.dtype == img.dtype
    assert np.all(img_aug)

    for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(dtype)

        if np.dtype(dtype).kind == "i":
            values = [int(center_value), int(0.1 * max_value), int(0.2 * max_value),
                      int(0.5 * max_value), max_value-100]
            values = [((-1)*value, value) for value in values]
        else:
            values = [(0, int(center_value)),
                      (10, int(0.1 * max_value)), (10, int(0.2 * max_value)), (10, int(0.5 * max_value)),
                      (0, max_value), (int(center_value), max_value)]

        for v1, v2 in values:
            aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
            img = np.array([
                [v1, v1, v2, v2],
                [v1, v1, v2, v2]
            ], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert np.array_equal(img_aug, img)

            aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
            img = np.array([
                [v2, v2, v2, v2],
                [v1, v2, v2, v2]
            ], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert np.all(img_aug == int(np.round((7/8)*v2 + (1/8)*v1)))

    for dtype in []:
        def _allclose(a, b):
            atol = 1e-4 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        isize = np.dtype(dtype).itemsize
        for value in [0, 1.0, 10.0, 1000 ** (isize - 1)]:
            v1 = (-1) * value
            v2 = value

            aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
            img = np.array([
                [v1, v1, v2, v2],
                [v1, v1, v2, v2]
            ], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert _allclose(img_aug, img)

            aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
            img = np.array([
                [v2, v2, v2, v2],
                [v1, v2, v2, v2]
            ], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert _allclose(img_aug, (7/8)*v2 + (1/8)*v1)
Пример #20
0
def test_Superpixels():
    reseed()

    def _array_equals_tolerant(a, b, tolerance):
        diff = np.abs(a.astype(np.int32) - b.astype(np.int32))
        return np.all(diff <= tolerance)

    base_img = [[255, 255, 255, 0, 0, 0], [255, 235, 255, 0, 20, 0],
                [250, 250, 250, 5, 5, 5]]
    base_img = np.tile(
        np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3))

    base_img_superpixels = [[251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4],
                            [251, 251, 251, 4, 4, 4]]
    base_img_superpixels = np.tile(
        np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis],
        (1, 1, 3))

    base_img_superpixels_left = np.copy(base_img_superpixels)
    base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :]

    base_img_superpixels_right = np.copy(base_img_superpixels)
    base_img_superpixels_right[:, :3, :] = base_img[:, :3, :]

    aug = iaa.Superpixels(p_replace=0, n_segments=2)
    observed = aug.augment_image(base_img)
    expected = base_img
    assert np.allclose(observed, expected)

    aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
    observed = aug.augment_image(base_img)
    expected = base_img_superpixels
    assert _array_equals_tolerant(observed, expected, 2)

    aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2))
    observed = aug.augment_image(base_img)
    expected = base_img_superpixels
    assert _array_equals_tolerant(observed, expected, 2)

    aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])),
                          n_segments=2)
    observed = aug.augment_image(base_img)
    assert np.allclose(observed, base_img) or _array_equals_tolerant(
        observed, base_img_superpixels, 2)

    aug = iaa.Superpixels(p_replace=0.5, n_segments=2)
    seen = {"none": False, "left": False, "right": False, "both": False}
    for _ in sm.xrange(100):
        observed = aug.augment_image(base_img)
        if _array_equals_tolerant(observed, base_img, 2):
            seen["none"] = True
        elif _array_equals_tolerant(observed, base_img_superpixels_left, 2):
            seen["left"] = True
        elif _array_equals_tolerant(observed, base_img_superpixels_right, 2):
            seen["right"] = True
        elif _array_equals_tolerant(observed, base_img_superpixels, 2):
            seen["both"] = True
        else:
            raise Exception(
                "Generated superpixels image does not match any expected image."
            )
        if all(seen.values()):
            break
    assert all(seen.values())

    # test exceptions for wrong parameter types
    got_exception = False
    try:
        _ = iaa.Superpixels(p_replace="test", n_segments=100)
    except Exception:
        got_exception = True
    assert got_exception

    got_exception = False
    try:
        _ = iaa.Superpixels(p_replace=1, n_segments="test")
    except Exception:
        got_exception = True
    assert got_exception

    # test get_parameters()
    aug = iaa.Superpixels(p_replace=0.5,
                          n_segments=2,
                          max_size=100,
                          interpolation="nearest")
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Binomial)
    assert isinstance(params[0].p, iap.Deterministic)
    assert isinstance(params[1], iap.Deterministic)
    assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4
    assert params[1].value == 2
    assert params[2] == 100
    assert params[3] == "nearest"

    ###################
    # test other dtypes
    ###################
    # bool
    aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
    img = np.array([[False, False, True, True], [False, False, True, True]],
                   dtype=bool)
    img_aug = aug.augment_image(img)
    assert img_aug.dtype == img.dtype
    assert np.all(img_aug == img)

    aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
    img = np.array([[True, True, True, True], [False, True, True, True]],
                   dtype=bool)
    img_aug = aug.augment_image(img)
    assert img_aug.dtype == img.dtype
    assert np.all(img_aug)

    for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)

        if np.dtype(dtype).kind == "i":
            values = [
                int(center_value),
                int(0.1 * max_value),
                int(0.2 * max_value),
                int(0.5 * max_value), max_value - 100
            ]
            values = [((-1) * value, value) for value in values]
        else:
            values = [(0, int(center_value)), (10, int(0.1 * max_value)),
                      (10, int(0.2 * max_value)), (10, int(0.5 * max_value)),
                      (0, max_value), (int(center_value), max_value)]

        for v1, v2 in values:
            aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
            img = np.array([[v1, v1, v2, v2], [v1, v1, v2, v2]], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert np.array_equal(img_aug, img)

            aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
            img = np.array([[v2, v2, v2, v2], [v1, v2, v2, v2]], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert np.all(img_aug == int(np.round((7 / 8) * v2 +
                                                  (1 / 8) * v1)))

    for dtype in []:

        def _allclose(a, b):
            atol = 1e-4 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        isize = np.dtype(dtype).itemsize
        for value in [0, 1.0, 10.0, 1000**(isize - 1)]:
            v1 = (-1) * value
            v2 = value

            aug = iaa.Superpixels(p_replace=1.0, n_segments=2)
            img = np.array([[v1, v1, v2, v2], [v1, v1, v2, v2]], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert _allclose(img_aug, img)

            aug = iaa.Superpixels(p_replace=1.0, n_segments=1)
            img = np.array([[v2, v2, v2, v2], [v1, v2, v2, v2]], dtype=dtype)
            img_aug = aug.augment_image(img)
            assert img_aug.dtype == np.dtype(dtype)
            assert _allclose(img_aug, (7 / 8) * v2 + (1 / 8) * v1)
Пример #21
0
def test_Fliplr():
    reseed()

    base_img = np.array([[0, 0, 1],
                         [0, 0, 1],
                         [0, 1, 1]], dtype=np.uint8)
    base_img = base_img[:, :, np.newaxis]

    base_img_flipped = np.array([[1, 0, 0],
                                 [1, 0, 0],
                                 [1, 1, 0]], dtype=np.uint8)
    base_img_flipped = base_img_flipped[:, :, np.newaxis]

    images = np.array([base_img])
    images_flipped = np.array([base_img_flipped])

    keypoints = [ia.KeypointsOnImage([ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1),
                                      ia.Keypoint(x=2, y=2)], shape=base_img.shape)]
    keypoints_flipped = [ia.KeypointsOnImage([ia.Keypoint(x=2, y=0), ia.Keypoint(x=1, y=1),
                                              ia.Keypoint(x=0, y=2)], shape=base_img.shape)]

    polygons = [ia.PolygonsOnImage(
        [ia.Polygon([(0, 0), (2, 0), (2, 2)])],
        shape=base_img.shape)]
    polygons_flipped = [ia.PolygonsOnImage(
        [ia.Polygon([(2, 0), (0, 0), (0, 2)])],
        shape=base_img.shape)]

    # 0% chance of flip
    aug = iaa.Fliplr(0)
    aug_det = aug.to_deterministic()

    for _ in sm.xrange(10):
        observed = aug.augment_images(images)
        expected = images
        assert np.array_equal(observed, expected)

        observed = aug_det.augment_images(images)
        expected = images
        assert np.array_equal(observed, expected)

        observed = aug.augment_keypoints(keypoints)
        expected = keypoints
        assert keypoints_equal(observed, expected)

        observed = aug_det.augment_keypoints(keypoints)
        expected = keypoints
        assert keypoints_equal(observed, expected)

        for aug_ in [aug, aug_det]:
            observed = aug_.augment_polygons(polygons)
            assert len(observed) == 1
            assert len(observed[0].polygons) == 1
            assert observed[0].shape == polygons[0].shape
            assert observed[0].polygons[0].exterior_almost_equals(
                polygons[0].polygons[0])
            assert observed[0].polygons[0].is_valid

    # 0% chance of flip, heatmaps
    aug = iaa.Fliplr(0)
    heatmaps = ia.HeatmapsOnImage(
        np.float32([
            [0, 0.5, 0.75],
            [0, 0.5, 0.75],
            [0.75, 0.75, 0.75],
        ]),
        shape=(3, 3, 3)
    )
    observed = aug.augment_heatmaps([heatmaps])[0]
    expected = heatmaps.get_arr()
    assert observed.shape == heatmaps.shape
    assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6
    assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6
    assert np.array_equal(observed.get_arr(), expected)

    # 100% chance of flip
    aug = iaa.Fliplr(1.0)
    aug_det = aug.to_deterministic()

    for _ in sm.xrange(10):
        observed = aug.augment_images(images)
        expected = images_flipped
        assert np.array_equal(observed, expected)

        observed = aug_det.augment_images(images)
        expected = images_flipped
        assert np.array_equal(observed, expected)

        observed = aug.augment_keypoints(keypoints)
        expected = keypoints_flipped
        assert keypoints_equal(observed, expected)

        observed = aug_det.augment_keypoints(keypoints)
        expected = keypoints_flipped
        assert keypoints_equal(observed, expected)

        for aug_ in [aug, aug_det]:
            observed = aug_.augment_polygons(polygons)
            assert len(observed) == 1
            assert len(observed[0].polygons) == 1
            assert observed[0].shape == polygons[0].shape
            assert observed[0].polygons[0].exterior_almost_equals(
                polygons_flipped[0].polygons[0])
            assert observed[0].polygons[0].is_valid

    # 100% chance of flip, heatmaps
    aug = iaa.Fliplr(1.0)
    heatmaps = ia.HeatmapsOnImage(
        np.float32([
            [0, 0.5, 0.75],
            [0, 0.5, 0.75],
            [0.75, 0.75, 0.75],
        ]),
        shape=(3, 3, 3)
    )
    observed = aug.augment_heatmaps([heatmaps])[0]
    expected = np.fliplr(heatmaps.get_arr())
    assert observed.shape == heatmaps.shape
    assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6
    assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6
    assert np.array_equal(observed.get_arr(), expected)

    # 50% chance of flip
    aug = iaa.Fliplr(0.5)
    aug_det = aug.to_deterministic()

    nb_iterations = 1000
    nb_images_flipped = 0
    nb_images_flipped_det = 0
    nb_keypoints_flipped = 0
    nb_keypoints_flipped_det = 0
    nb_polygons_flipped = 0
    nb_polygons_flipped_det = 0
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_images(images)
        if np.array_equal(observed, images_flipped):
            nb_images_flipped += 1

        observed = aug_det.augment_images(images)
        if np.array_equal(observed, images_flipped):
            nb_images_flipped_det += 1

        observed = aug.augment_keypoints(keypoints)
        if keypoints_equal(observed, keypoints_flipped):
            nb_keypoints_flipped += 1

        observed = aug_det.augment_keypoints(keypoints)
        if keypoints_equal(observed, keypoints_flipped):
            nb_keypoints_flipped_det += 1

        observed = aug.augment_polygons(polygons)
        if observed[0].polygons[0].exterior_almost_equals(
                polygons_flipped[0].polygons[0]):
            nb_polygons_flipped += 1

        observed = aug_det.augment_polygons(polygons)
        if observed[0].polygons[0].exterior_almost_equals(
                polygons_flipped[0].polygons[0]):
            nb_polygons_flipped_det += 1

    assert int(nb_iterations * 0.3) <= nb_images_flipped <= int(nb_iterations * 0.7)
    assert int(nb_iterations * 0.3) <= nb_keypoints_flipped <= int(nb_iterations * 0.7)
    assert int(nb_iterations * 0.3) <= nb_polygons_flipped <= int(nb_iterations * 0.7)
    assert nb_images_flipped_det in [0, nb_iterations]
    assert nb_keypoints_flipped_det in [0, nb_iterations]
    assert nb_polygons_flipped_det in [0, nb_iterations]

    # 50% chance of flipped, multiple images, list as input
    images_multi = [base_img, base_img]
    aug = iaa.Fliplr(0.5)
    aug_det = aug.to_deterministic()
    nb_iterations = 1000
    nb_flipped_by_pos = [0] * len(images_multi)
    nb_flipped_by_pos_det = [0] * len(images_multi)
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_images(images_multi)
        for i in sm.xrange(len(images_multi)):
            if np.array_equal(observed[i], base_img_flipped):
                nb_flipped_by_pos[i] += 1

        observed = aug_det.augment_images(images_multi)
        for i in sm.xrange(len(images_multi)):
            if np.array_equal(observed[i], base_img_flipped):
                nb_flipped_by_pos_det[i] += 1

    for val in nb_flipped_by_pos:
        assert int(nb_iterations * 0.3) <= val <= int(nb_iterations * 0.7)

    for val in nb_flipped_by_pos_det:
        assert val in [0, nb_iterations]

    # test StochasticParameter as p
    aug = iaa.Fliplr(p=iap.Choice([0, 1], p=[0.7, 0.3]))
    seen = [0, 0]
    for _ in sm.xrange(1000):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, base_img):
            seen[0] += 1
        elif np.array_equal(observed, base_img_flipped):
            seen[1] += 1
        else:
            assert False
    assert 700 - 75 < seen[0] < 700 + 75
    assert 300 - 75 < seen[1] < 300 + 75

    # test exceptions for wrong parameter types
    got_exception = False
    try:
        _ = iaa.Fliplr(p="test")
    except Exception:
        got_exception = True
    assert got_exception

    # test get_parameters()
    aug = iaa.Fliplr(p=0.5)
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Binomial)
    assert isinstance(params[0].p, iap.Deterministic)
    assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4

    ###################
    # test other dtypes
    ###################
    aug = iaa.Fliplr(1.0)

    image = np.zeros((3, 3), dtype=bool)
    image[0, 0] = True
    expected = np.zeros((3, 3), dtype=bool)
    expected[0, 2] = True
    image_aug = aug.augment_image(image)
    assert image_aug.dtype.type == image.dtype.type
    assert np.all(image_aug == expected)

    for dtype in [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int32, np.int64]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(dtype)
        value = max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.array_equal(image_aug, expected)

    for dtype, value in zip([np.float16, np.float32, np.float64, np.float128], [5000, 1000**2, 1000**3, 1000**4]):
        atol = 1e-9 * max_value if dtype != np.float16 else 1e-3 * max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.allclose(image_aug, expected, atol=atol)
Пример #22
0
def test_Fliplr():
    reseed()

    base_img = np.array([[0, 0, 1], [0, 0, 1], [0, 1, 1]], dtype=np.uint8)
    base_img = base_img[:, :, np.newaxis]

    base_img_flipped = np.array([[1, 0, 0], [1, 0, 0], [1, 1, 0]],
                                dtype=np.uint8)
    base_img_flipped = base_img_flipped[:, :, np.newaxis]

    images = np.array([base_img])
    images_flipped = np.array([base_img_flipped])

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=0, y=0),
            ia.Keypoint(x=1, y=1),
            ia.Keypoint(x=2, y=2)
        ],
                            shape=base_img.shape)
    ]
    keypoints_flipped = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=2, y=0),
            ia.Keypoint(x=1, y=1),
            ia.Keypoint(x=0, y=2)
        ],
                            shape=base_img.shape)
    ]

    # 0% chance of flip
    aug = iaa.Fliplr(0)
    aug_det = aug.to_deterministic()

    for _ in sm.xrange(10):
        observed = aug.augment_images(images)
        expected = images
        assert np.array_equal(observed, expected)

        observed = aug_det.augment_images(images)
        expected = images
        assert np.array_equal(observed, expected)

        observed = aug.augment_keypoints(keypoints)
        expected = keypoints
        assert keypoints_equal(observed, expected)

        observed = aug_det.augment_keypoints(keypoints)
        expected = keypoints
        assert keypoints_equal(observed, expected)

    # 0% chance of flip, heatmaps
    aug = iaa.Fliplr(0)
    heatmaps = ia.HeatmapsOnImage(np.float32([
        [0, 0.5, 0.75],
        [0, 0.5, 0.75],
        [0.75, 0.75, 0.75],
    ]),
                                  shape=(3, 3, 3))
    observed = aug.augment_heatmaps([heatmaps])[0]
    expected = heatmaps.get_arr()
    assert observed.shape == heatmaps.shape
    assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6
    assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6
    assert np.array_equal(observed.get_arr(), expected)

    # 100% chance of flip
    aug = iaa.Fliplr(1.0)
    aug_det = aug.to_deterministic()

    for _ in sm.xrange(10):
        observed = aug.augment_images(images)
        expected = images_flipped
        assert np.array_equal(observed, expected)

        observed = aug_det.augment_images(images)
        expected = images_flipped
        assert np.array_equal(observed, expected)

        observed = aug.augment_keypoints(keypoints)
        expected = keypoints_flipped
        assert keypoints_equal(observed, expected)

        observed = aug_det.augment_keypoints(keypoints)
        expected = keypoints_flipped
        assert keypoints_equal(observed, expected)

    # 100% chance of flip, heatmaps
    aug = iaa.Fliplr(1.0)
    heatmaps = ia.HeatmapsOnImage(np.float32([
        [0, 0.5, 0.75],
        [0, 0.5, 0.75],
        [0.75, 0.75, 0.75],
    ]),
                                  shape=(3, 3, 3))
    observed = aug.augment_heatmaps([heatmaps])[0]
    expected = np.fliplr(heatmaps.get_arr())
    assert observed.shape == heatmaps.shape
    assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6
    assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6
    assert np.array_equal(observed.get_arr(), expected)

    # 50% chance of flip
    aug = iaa.Fliplr(0.5)
    aug_det = aug.to_deterministic()

    nb_iterations = 1000
    nb_images_flipped = 0
    nb_images_flipped_det = 0
    nb_keypoints_flipped = 0
    nb_keypoints_flipped_det = 0
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_images(images)
        if np.array_equal(observed, images_flipped):
            nb_images_flipped += 1

        observed = aug_det.augment_images(images)
        if np.array_equal(observed, images_flipped):
            nb_images_flipped_det += 1

        observed = aug.augment_keypoints(keypoints)
        if keypoints_equal(observed, keypoints_flipped):
            nb_keypoints_flipped += 1

        observed = aug_det.augment_keypoints(keypoints)
        if keypoints_equal(observed, keypoints_flipped):
            nb_keypoints_flipped_det += 1

    assert int(nb_iterations * 0.3) <= nb_images_flipped <= int(
        nb_iterations * 0.7)
    assert int(nb_iterations * 0.3) <= nb_keypoints_flipped <= int(
        nb_iterations * 0.7)
    assert nb_images_flipped_det in [0, nb_iterations]
    assert nb_keypoints_flipped_det in [0, nb_iterations]

    # 50% chance of flipped, multiple images, list as input
    images_multi = [base_img, base_img]
    aug = iaa.Fliplr(0.5)
    aug_det = aug.to_deterministic()
    nb_iterations = 1000
    nb_flipped_by_pos = [0] * len(images_multi)
    nb_flipped_by_pos_det = [0] * len(images_multi)
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_images(images_multi)
        for i in sm.xrange(len(images_multi)):
            if np.array_equal(observed[i], base_img_flipped):
                nb_flipped_by_pos[i] += 1

        observed = aug_det.augment_images(images_multi)
        for i in sm.xrange(len(images_multi)):
            if np.array_equal(observed[i], base_img_flipped):
                nb_flipped_by_pos_det[i] += 1

    for val in nb_flipped_by_pos:
        assert int(nb_iterations * 0.3) <= val <= int(nb_iterations * 0.7)

    for val in nb_flipped_by_pos_det:
        assert val in [0, nb_iterations]

    # test StochasticParameter as p
    aug = iaa.Fliplr(p=iap.Choice([0, 1], p=[0.7, 0.3]))
    seen = [0, 0]
    for _ in sm.xrange(1000):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, base_img):
            seen[0] += 1
        elif np.array_equal(observed, base_img_flipped):
            seen[1] += 1
        else:
            assert False
    assert 700 - 75 < seen[0] < 700 + 75
    assert 300 - 75 < seen[1] < 300 + 75

    # test exceptions for wrong parameter types
    got_exception = False
    try:
        _ = iaa.Fliplr(p="test")
    except Exception:
        got_exception = True
    assert got_exception

    # test get_parameters()
    aug = iaa.Fliplr(p=0.5)
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Binomial)
    assert isinstance(params[0].p, iap.Deterministic)
    assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4

    ###################
    # test other dtypes
    ###################
    aug = iaa.Fliplr(1.0)

    image = np.zeros((3, 3), dtype=bool)
    image[0, 0] = True
    expected = np.zeros((3, 3), dtype=bool)
    expected[0, 2] = True
    image_aug = aug.augment_image(image)
    assert image_aug.dtype.type == image.dtype.type
    assert np.all(image_aug == expected)

    for dtype in [
            np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int32,
            np.int64
    ]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        value = max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.array_equal(image_aug, expected)

    for dtype, value in zip([np.float16, np.float32, np.float64, np.float128],
                            [5000, 1000**2, 1000**3, 1000**4]):
        atol = 1e-9 * max_value if dtype != np.float16 else 1e-3 * max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.allclose(image_aug, expected, atol=atol)
Пример #23
0
def test_Convolve():
    reseed()

    img = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    img = np.uint8(img)

    # matrix is None
    aug = iaa.Convolve(matrix=None)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: [None])
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    # matrix is [[1]]
    aug = iaa.Convolve(matrix=np.float32([[1]]))
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: np.float32([[1]]))
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    # matrix is [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
    m = np.float32([
        [0, 0, 0],
        [0, 1, 0],
        [0, 0, 0]
    ])
    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, img)

    # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]]
    m = np.float32([
        [0, 0, 0],
        [0, 2, 0],
        [0, 0, 0]
    ])
    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, 2*img)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, 2*img)

    # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]]
    # with 3 channels
    m = np.float32([
        [0, 0, 0],
        [0, 2, 0],
        [0, 0, 0]
    ])
    img3 = np.tile(img[..., np.newaxis], (1, 1, 3))
    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img3)
    assert np.array_equal(observed, 2*img3)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img3)
    assert np.array_equal(observed, 2*img3)

    # matrix is [[0, -1, 0], [0, 10, 0], [0, 0, 0]]
    m = np.float32([
        [0, -1, 0],
        [0, 10, 0],
        [0, 0, 0]
    ])
    expected = np.uint8([
        [10*1+(-1)*4, 10*2+(-1)*5, 10*3+(-1)*6],
        [10*4+(-1)*1, 10*5+(-1)*2, 10*6+(-1)*3],
        [10*7+(-1)*4, 10*8+(-1)*5, 10*9+(-1)*6]
    ])

    aug = iaa.Convolve(matrix=m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, expected)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m)
    observed = aug.augment_image(img)
    assert np.array_equal(observed, expected)

    # changing matrices when using callable
    expected = []
    for i in sm.xrange(5):
        expected.append(img * i)

    aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: np.float32([[random_state.randint(0, 5)]]))
    seen = [False] * 5
    for _ in sm.xrange(200):
        observed = aug.augment_image(img)
        found = False
        for i, expected_i in enumerate(expected):
            if np.array_equal(observed, expected_i):
                seen[i] = True
                found = True
                break
        assert found
        if all(seen):
            break
    assert all(seen)

    # bad datatype for matrix
    got_exception = False
    try:
        aug = iaa.Convolve(matrix=False)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # get_parameters()
    matrix = np.int32([[1]])
    aug = iaa.Convolve(matrix=matrix)
    params = aug.get_parameters()
    assert np.array_equal(params[0], matrix)
    assert params[1] == "constant"

    # TODO add test for keypoints once their handling was improved in Convolve

    ###################
    # test other dtypes
    ###################
    identity_matrix = np.int64([[1]])
    aug = iaa.Convolve(matrix=identity_matrix)

    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image_aug = aug.augment_image(image)
    assert image.dtype.type == np.bool_
    assert np.all(image_aug == image)

    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100
        image_aug = aug.augment_image(image)
        assert image.dtype.type == dtype
        assert np.all(image_aug == image)

    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100.0
        image_aug = aug.augment_image(image)
        assert image.dtype.type == dtype
        assert np.allclose(image_aug, image)

    # ----
    # non-identity matrix
    # ----
    matrix = np.float64([
        [0, 0.6, 0],
        [0, 0.4, 0],
        [0,   0, 0]
    ])
    aug = iaa.Convolve(matrix=matrix)

    image = np.zeros((3, 3), dtype=bool)
    image[1, 1] = True
    image[2, 1] = True
    expected = np.zeros((3, 3), dtype=bool)
    expected[0, 1] = True
    expected[2, 1] = True
    image_aug = aug.augment_image(image)
    assert image.dtype.type == np.bool_
    assert np.all(image_aug == expected)

    matrix = np.float64([
        [0, 0.5, 0],
        [0, 0.5, 0],
        [0,   0, 0]
    ])
    aug = iaa.Convolve(matrix=matrix)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100
        image[2, 1] = 100
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = int(np.round(100 * 0.5))
        expected[1, 1] = int(np.round(100 * 0.5))
        expected[2, 1] = int(np.round(100 * 0.5 + 100 * 0.5))

        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 2

    # float
    for dtype in [np.float16, np.float32, np.float64]:
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = 100.0
        image[2, 1] = 100.0
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = 100 * 0.5
        expected[1, 1] = 100 * 0.5
        expected[2, 1] = 100 * 0.5 + 100 * 0.5

        diff = np.abs(image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 1.0

    # ----
    # non-identity matrix, higher values
    # ----
    matrix = np.float64([
        [0, 0.5, 0],
        [0, 0.5, 0],
        [0,   0, 0]
    ])
    aug = iaa.Convolve(matrix=matrix)

    # uint, int
    for dtype in [np.uint8, np.uint16, np.int8, np.int16]:
        _min_value, center_value, max_value = iadt.get_value_range_of_dtype(dtype)
        value = int(center_value + 0.4 * max_value)

        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 1] = value
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = int(np.round(value * 0.5))
        expected[1, 1] = int(np.round(value * 0.5))
        expected[2, 1] = int(np.round(value * 0.5 + value * 0.5))

        diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) <= 2

    # float
    for dtype, value in zip([np.float16, np.float32, np.float64], [5000, 1000*1000, 1000*1000*1000]):
        image = np.zeros((3, 3), dtype=dtype)
        image[1, 1] = value
        image[2, 1] = value
        image_aug = aug.augment_image(image)

        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 1] = value * 0.5
        expected[1, 1] = value * 0.5
        expected[2, 1] = value * 0.5 + value * 0.5

        diff = np.abs(image_aug.astype(np.float128) - expected.astype(np.float128))
        assert image_aug.dtype.type == dtype
        assert np.max(diff) < 1.0

    # assert failure on invalid dtypes
    aug = iaa.Convolve(matrix=identity_matrix)
    for dt in [np.uint32, np.uint64, np.int32, np.int64]:
        got_exception = False
        try:
            _ = aug.augment_image(np.zeros((1, 1), dtype=dt))
        except Exception as exc:
            assert "forbidden dtype" in str(exc)
            got_exception = True
        assert got_exception