Example #1
0
def restore_dtypes_(images, dtypes, clip=True, round=True):
    if ia.is_np_array(images):
        if ia.is_iterable(dtypes):
            assert len(dtypes) > 0

            if len(dtypes) > 1:
                assert all([dtype_i == dtypes[0] for dtype_i in dtypes])

            dtypes = dtypes[0]

        dtypes = np.dtype(dtypes)

        dtype_to = dtypes
        if images.dtype.type == dtype_to:
            result = images
        else:
            if round and dtype_to.kind in ["u", "i", "b"]:
                images = np.round(images)
            if clip:
                min_value, _, max_value = get_value_range_of_dtype(dtype_to)
                images = np.clip(images, min_value, max_value, out=images)
            result = images.astype(dtype_to, copy=False)
    elif ia.is_iterable(images):
        result = images
        dtypes = dtypes if not isinstance(dtypes,
                                          np.dtype) else [dtypes] * len(images)
        for i, (image, dtype) in enumerate(zip(images, dtypes)):
            assert ia.is_np_array(image)
            result[i] = restore_dtypes_(image, dtype, clip=clip)
    else:
        raise Exception(
            "Expected numpy array or iterable of numpy arrays, got type '%s'."
            % (type(images), ))
    return result
Example #2
0
def restore_dtypes_(images, dtypes, clip=True, round=True):
    if ia.is_np_array(images):
        if ia.is_iterable(dtypes):
            assert len(dtypes) > 0

            if len(dtypes) > 1:
                assert all([dtype_i == dtypes[0] for dtype_i in dtypes])

            dtypes = dtypes[0]

        dtypes = np.dtype(dtypes)

        dtype_to = dtypes
        if images.dtype.type == dtype_to:
            result = images
        else:
            if round and dtype_to.kind in ["u", "i", "b"]:
                images = np.round(images)
            if clip:
                min_value, _, max_value = get_value_range_of_dtype(dtype_to)
                images = clip_(images, min_value, max_value)
            result = images.astype(dtype_to, copy=False)
    elif ia.is_iterable(images):
        result = images
        dtypes = dtypes if not isinstance(dtypes, np.dtype) else [dtypes] * len(images)
        for i, (image, dtype) in enumerate(zip(images, dtypes)):
            assert ia.is_np_array(image)
            result[i] = restore_dtypes_(image, dtype, clip=clip)
    else:
        raise Exception("Expected numpy array or iterable of numpy arrays, got type '%s'." % (type(images),))
    return result
Example #3
0
def change_dtypes_(images, dtypes, clip=True, round=True):
    if ia.is_np_array(images):
        if ia.is_iterable(dtypes):
            dtypes = normalize_dtypes(dtypes)
            n_distinct_dtypes = len(set([dt.name for dt in dtypes]))
            assert len(dtypes) == len(images), (
                "If an iterable of dtypes is provided to "
                "change_dtypes_(), it must contain as many dtypes as "
                "there are images. Got %d dtypes and %d images." %
                (len(dtypes), len(images)))

            assert n_distinct_dtypes == 1, (
                "If an image array is provided to change_dtypes_(), the "
                "provided 'dtypes' argument must either be a single dtype "
                "or an iterable of N times the *same* dtype for N images. "
                "Got %d distinct dtypes." % (n_distinct_dtypes, ))

            dtype = dtypes[0]
        else:
            dtype = normalize_dtype(dtypes)

        result = change_dtype_(images, dtype, clip=clip, round=round)
    elif ia.is_iterable(images):
        dtypes = ([normalize_dtype(dtypes)] * len(images) if
                  not isinstance(dtypes, list) else normalize_dtypes(dtypes))
        assert len(dtypes) == len(images)

        result = images
        for i, (image, dtype) in enumerate(zip(images, dtypes)):
            assert ia.is_np_array(image)
            result[i] = change_dtype_(image, dtype, clip=clip, round=round)
    else:
        raise Exception("Expected numpy array or iterable of numpy arrays, "
                        "got type '%s'." % (type(images), ))
    return result
Example #4
0
    def __init__(self, k=1, name=None, deterministic=False, random_state=None):
        super(AverageBlur, self).__init__(name=name,
                                          deterministic=deterministic,
                                          random_state=random_state)

        # TODO replace this by iap.handle_discrete_kernel_size()
        self.mode = "single"
        if ia.is_single_number(k):
            self.k = iap.Deterministic(int(k))
        elif ia.is_iterable(k):
            assert len(k) == 2, (
                "Expected iterable 'k' to contain exactly 2 entries, "
                "got %d." % (len(k), ))
            if all([ia.is_single_number(ki) for ki in k]):
                self.k = iap.DiscreteUniform(int(k[0]), int(k[1]))
            elif all([isinstance(ki, iap.StochasticParameter) for ki in k]):
                self.mode = "two"
                self.k = (k[0], k[1])
            else:
                k_tuple = [None, None]
                if ia.is_single_number(k[0]):
                    k_tuple[0] = iap.Deterministic(int(k[0]))
                elif ia.is_iterable(k[0]) and all(
                    [ia.is_single_number(ki) for ki in k[0]]):
                    k_tuple[0] = iap.DiscreteUniform(int(k[0][0]),
                                                     int(k[0][1]))
                else:
                    raise Exception(
                        "k[0] expected to be int or tuple of two ints, got %s"
                        % (type(k[0]), ))

                if ia.is_single_number(k[1]):
                    k_tuple[1] = iap.Deterministic(int(k[1]))
                elif ia.is_iterable(k[1]) and all(
                    [ia.is_single_number(ki) for ki in k[1]]):
                    k_tuple[1] = iap.DiscreteUniform(int(k[1][0]),
                                                     int(k[1][1]))
                else:
                    raise Exception(
                        "k[1] expected to be int or tuple of two ints, got %s"
                        % (type(k[1]), ))

                self.mode = "two"
                self.k = k_tuple
        elif isinstance(k, iap.StochasticParameter):
            self.k = k
        else:
            raise Exception(
                "Expected int, tuple/list with 2 entries or StochasticParameter. Got %s."
                % (type(k), ))
Example #5
0
    def __init__(self, to_colorspace, from_colorspace="RGB", alpha=1.0, name=None, deterministic=False,
                 random_state=None):
        super(ChangeColorspace, self).__init__(name=name, deterministic=deterministic, random_state=random_state)

        # TODO somehow merge this with Alpha augmenter?
        self.alpha = iap.handle_continuous_param(alpha, "alpha", value_range=(0, 1.0), tuple_to_uniform=True,
                                                 list_to_choice=True)

        if ia.is_string(to_colorspace):
            ia.do_assert(to_colorspace in ChangeColorspace.COLORSPACES)
            self.to_colorspace = iap.Deterministic(to_colorspace)
        elif ia.is_iterable(to_colorspace):
            ia.do_assert(all([ia.is_string(colorspace) for colorspace in to_colorspace]))
            ia.do_assert(all([(colorspace in ChangeColorspace.COLORSPACES) for colorspace in to_colorspace]))
            self.to_colorspace = iap.Choice(to_colorspace)
        elif isinstance(to_colorspace, iap.StochasticParameter):
            self.to_colorspace = to_colorspace
        else:
            raise Exception("Expected to_colorspace to be string, list of strings or StochasticParameter, got %s." % (
                type(to_colorspace),))

        self.from_colorspace = from_colorspace
        ia.do_assert(self.from_colorspace in ChangeColorspace.COLORSPACES)
        ia.do_assert(from_colorspace != ChangeColorspace.GRAY)

        self.eps = 0.001  # epsilon value to check if alpha is close to 1.0 or 0.0
Example #6
0
def assert_is_iterable_of(iterable_var, classes):
    """Assert that `iterable_var` only contains instances of given classes.

    Parameters
    ----------
    iterable_var : iterable
        See :func:`imgaug.validation.is_iterable_of`.

    classes : type or iterable of type
        See :func:`imgaug.validation.is_iterable_of`.

    """
    valid = is_iterable_of(iterable_var, classes)
    if not valid:
        expected_types_str = (", ".join([
            class_.__name__ for class_ in classes
        ]) if not isinstance(classes, type) else classes.__name__)
        if not ia.is_iterable(iterable_var):
            raise AssertionError(
                "Expected an iterable of the following types: %s. "
                "Got instead a single instance of: %s." %
                (expected_types_str, type(iterable_var).__name__))

        raise AssertionError(
            "Expected an iterable of the following types: %s. "
            "Got an iterable of types: %s." %
            (expected_types_str,
             convert_iterable_to_string_of_types(iterable_var)))
Example #7
0
def is_iterable_of(iterable_var, classes):
    """Check whether `iterable_var` contains only instances of given classes.

    Parameters
    ----------
    iterable_var : iterable
        An iterable of items that will be matched against `classes`.

    classes : type or iterable of type
        One or more classes that each item in `var` must be an instanceof.
        If this is an iterable, a single match per item is enough.

    Returns
    -------
    bool
        Whether `var` only contains instances of `classes`.
        If `var` was empty, ``True`` will be returned.

    """
    if not ia.is_iterable(iterable_var):
        return False

    for var_i in iterable_var:
        if not isinstance(var_i, classes):
            return False

    return True
    def __init__(self,
                 gamma_log2=0.0,
                 per_channel=False,
                 name=None,
                 deterministic=False,
                 random_state=None):
        super(Gamma, self).__init__(name=name,
                                    deterministic=deterministic,
                                    random_state=random_state)

        if ia.is_single_number(gamma_log2):
            self.gamma_log2 = iaa.Deterministic(gamma_log2)
        elif ia.is_iterable(gamma_log2):
            assert len(
                gamma_log2
            ) == 2, "Expected tuple/list with 2 entries, got %d entries." % (
                len(gamma_log2), )
            self.gamma_log2 = iaa.Uniform(gamma_log2[0], gamma_log2[1])
        elif isinstance(gamma_log2, iaa.StochasticParameter):
            self.gamma_log2 = gamma_log2
        else:
            raise Exception(
                "Expected float or int, tuple/list with 2 entries or StochasticParameter. Got %s."
                % (type(gamma_log2), ))

        if per_channel in [True, False, 0, 1, 0.0, 1.0]:
            self.per_channel = iaa.Deterministic(int(per_channel))
        elif ia.is_single_number(per_channel):
            assert 0 <= per_channel <= 1.0
            self.per_channel = iaa.Binomial(per_channel)
        else:
            raise Exception(
                "Expected per_channel to be boolean or number or StochasticParameter"
            )
Example #9
0
def AdditiveGaussianNoise(loc=0,
                          scale=0,
                          per_channel=False,
                          name='None',
                          deterministic=False,
                          random_state=None):

    if ia.is_single_number(loc):
        loc2 = ia.parameters.Deterministic(loc)
    elif ia.is_iterable(loc):
        assert len(
            loc
        ) == 2, "Expected tuple/list with 2 entries for argument 'loc', got %d entries." % (
            len(scale), )
        loc2 = ia.parameters.Uniform(loc[0], loc[1])
    elif isinstance(loc, ia.parameters.StochasticParameter):
        loc2 = loc
    else:
        raise Exception(
            "Expected float, int, tuple/list with 2 entries or StochasticParameter for argument 'loc'. Got %s."
            % (type(loc), ))

    if ia.is_single_number(scale):
        scale2 = ia.parameters.Deterministic(scale)
    elif ia.is_iterable(scale):
        assert len(
            scale
        ) == 2, "Expected tuple/list with 2 entries for argument 'scale', got %d entries." % (
            len(scale), )
        scale2 = ia.parameters.Uniform(scale[0], scale[1])
    elif isinstance(scale, ia.parameters.StochasticParameter):
        scale2 = scale
    else:
        raise Exception(
            "Expected float, int, tuple/list with 2 entries or StochasticParameter for argument 'scale'. Got %s."
            % (type(scale), ))

    return AddElementwise(scale,
                          per_channel=per_channel,
                          name=name,
                          deterministic=deterministic,
                          random_state=random_state)
Example #10
0
    def __init__(self, k=1, name=None, deterministic=False, random_state=None):
        super(MedianBlur, self).__init__(name=name, deterministic=deterministic, random_state=random_state)

        # TODO replace this by iap.handle_discrete_kernel_size()
        self.k = iap.handle_discrete_param(k, "k", value_range=(1, None), tuple_to_uniform=True, list_to_choice=True,
                                           allow_floats=False)
        if ia.is_single_integer(k):
            ia.do_assert(k % 2 != 0, "Expected k to be odd, got %d. Add or subtract 1." % (int(k),))
        elif ia.is_iterable(k):
            ia.do_assert(all([ki % 2 != 0 for ki in k]),
                         "Expected all values in iterable k to be odd, but at least one was not. "
                         + "Add or subtract 1 to/from that value.")
Example #11
0
    def __init__(self, k=1, name=None, deterministic=False, random_state=None):
        super(MedianBlur, self).__init__(name=name, deterministic=deterministic, random_state=random_state)

        # TODO replace this by iap.handle_discrete_kernel_size()
        self.k = iap.handle_discrete_param(k, "k", value_range=(1, None), tuple_to_uniform=True, list_to_choice=True,
                                           allow_floats=False)
        if ia.is_single_integer(k):
            ia.do_assert(k % 2 != 0, "Expected k to be odd, got %d. Add or subtract 1." % (int(k),))
        elif ia.is_iterable(k):
            ia.do_assert(all([ki % 2 != 0 for ki in k]),
                         "Expected all values in iterable k to be odd, but at least one was not. "
                         + "Add or subtract 1 to/from that value.")
Example #12
0
    def __init__(self, k=1, name=None, deterministic=False, random_state=None):
        super(AverageBlur, self).__init__(name=name, deterministic=deterministic, random_state=random_state)

        # TODO replace this by iap.handle_discrete_kernel_size()
        self.mode = "single"
        if ia.is_single_number(k):
            self.k = iap.Deterministic(int(k))
        elif ia.is_iterable(k):
            ia.do_assert(len(k) == 2)
            if all([ia.is_single_number(ki) for ki in k]):
                self.k = iap.DiscreteUniform(int(k[0]), int(k[1]))
            elif all([isinstance(ki, iap.StochasticParameter) for ki in k]):
                self.mode = "two"
                self.k = (k[0], k[1])
            else:
                k_tuple = [None, None]
                if ia.is_single_number(k[0]):
                    k_tuple[0] = iap.Deterministic(int(k[0]))
                elif ia.is_iterable(k[0]) and all([ia.is_single_number(ki) for ki in k[0]]):
                    k_tuple[0] = iap.DiscreteUniform(int(k[0][0]), int(k[0][1]))
                else:
                    raise Exception("k[0] expected to be int or tuple of two ints, got %s" % (type(k[0]),))

                if ia.is_single_number(k[1]):
                    k_tuple[1] = iap.Deterministic(int(k[1]))
                elif ia.is_iterable(k[1]) and all([ia.is_single_number(ki) for ki in k[1]]):
                    k_tuple[1] = iap.DiscreteUniform(int(k[1][0]), int(k[1][1]))
                else:
                    raise Exception("k[1] expected to be int or tuple of two ints, got %s" % (type(k[1]),))

                self.mode = "two"
                self.k = k_tuple
        elif isinstance(k, iap.StochasticParameter):
            self.k = k
        else:
            raise Exception("Expected int, tuple/list with 2 entries or StochasticParameter. Got %s." % (type(k),))
Example #13
0
    def __init__(self, k=1, seed=None, name=None, **old_kwargs):
        super(MedianBlur, self).__init__(
            seed=seed, name=name, **old_kwargs)

        # TODO replace this by iap.handle_discrete_kernel_size()
        self.k = iap.handle_discrete_param(
            k, "k", value_range=(1, None), tuple_to_uniform=True,
            list_to_choice=True, allow_floats=False)
        if ia.is_single_integer(k):
            assert k % 2 != 0, (
                "Expected k to be odd, got %d. Add or subtract 1." % (
                    int(k),))
        elif ia.is_iterable(k):
            assert all([ki % 2 != 0 for ki in k]), (
                "Expected all values in iterable k to be odd, but at least "
                "one was not. Add or subtract 1 to/from that value.")
Example #14
0
def CoarseDropout(p=0,
                  size_px=None,
                  size_percent=None,
                  per_channel=False,
                  min_size=4,
                  name=None,
                  deterministic=False,
                  random_state=None):
    """
	The version exactly the same, it would be nice to just route the calling from IMGaug
	"""
    if ia.is_single_number(p):
        p2 = ia.parameters.Binomial(1 - p)
    elif ia.is_iterable(p):
        do_assert(len(p) == 2)
        do_assert(p[0] < p[1])
        do_assert(0 <= p[0] <= 1.0)
        do_assert(0 <= p[1] <= 1.0)
        p2 = ia.parameters.Binomial(ia.parameters.Uniform(1 - p[1], 1 - p[0]))
    elif isinstance(p, ia.parameters.StochasticParameter):
        p2 = p
    else:
        raise Exception(
            "Expected p to be float or int or StochasticParameter, got %s." %
            (type(p), ))

    if size_px is not None:
        p3 = ia.parameters.FromLowerResolution(other_param=p2,
                                               size_px=size_px,
                                               min_size=min_size)
    elif size_percent is not None:
        p3 = ia.parameters.FromLowerResolution(other_param=p2,
                                               size_percent=size_percent,
                                               min_size=min_size)
    else:
        raise Exception("Either size_px or size_percent must be set.")

    return MultiplyElementwise(p3,
                               per_channel=per_channel,
                               name=name,
                               deterministic=deterministic,
                               random_state=random_state)
Example #15
0
    def __init__(self,
                 to_colorspace,
                 from_colorspace="RGB",
                 alpha=1.0,
                 name=None,
                 deterministic=False,
                 random_state=None):
        super(ChangeColorspace, self).__init__(name=name,
                                               deterministic=deterministic,
                                               random_state=random_state)

        # TODO somehow merge this with Alpha augmenter?
        self.alpha = iap.handle_continuous_param(alpha,
                                                 "alpha",
                                                 value_range=(0, 1.0),
                                                 tuple_to_uniform=True,
                                                 list_to_choice=True)

        if ia.is_string(to_colorspace):
            ia.do_assert(to_colorspace in ChangeColorspace.COLORSPACES)
            self.to_colorspace = iap.Deterministic(to_colorspace)
        elif ia.is_iterable(to_colorspace):
            ia.do_assert(
                all([ia.is_string(colorspace)
                     for colorspace in to_colorspace]))
            ia.do_assert(
                all([(colorspace in ChangeColorspace.COLORSPACES)
                     for colorspace in to_colorspace]))
            self.to_colorspace = iap.Choice(to_colorspace)
        elif isinstance(to_colorspace, iap.StochasticParameter):
            self.to_colorspace = to_colorspace
        else:
            raise Exception(
                "Expected to_colorspace to be string, list of strings or StochasticParameter, got %s."
                % (type(to_colorspace), ))

        self.from_colorspace = from_colorspace
        ia.do_assert(self.from_colorspace in ChangeColorspace.COLORSPACES)
        ia.do_assert(from_colorspace != ChangeColorspace.GRAY)

        self.eps = 0.001  # epsilon value to check if alpha is close to 1.0 or 0.0
Example #16
0
def test_Alpha():
    reseed()

    base_img = np.zeros((3, 3, 1), dtype=np.uint8)
    heatmaps_arr = np.float32([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0],
                               [0.0, 1.0, 1.0]])
    heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],
                                  [0.0, 0.0, 1.0]])
    heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0], [0.0, 1.0, 0.0],
                                  [1.0, 1.0, 0.0]])
    heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3))

    aug = iaa.Alpha(1, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 10).astype(np.uint8)
    assert np.allclose(observed, expected)

    for per_channel in [False, True]:
        aug = iaa.Alpha(1,
                        iaa.Affine(translate_px={"x": 1}),
                        iaa.Affine(translate_px={"x": -1}),
                        per_channel=per_channel)
        observed = aug.augment_heatmaps([heatmaps])[0]
        assert observed.shape == heatmaps.shape
        assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6
        assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6
        assert np.allclose(observed.get_arr(), heatmaps_arr_r1)

    aug = iaa.Alpha(0, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 20).astype(np.uint8)
    assert np.allclose(observed, expected)

    for per_channel in [False, True]:
        aug = iaa.Alpha(0,
                        iaa.Affine(translate_px={"x": 1}),
                        iaa.Affine(translate_px={"x": -1}),
                        per_channel=per_channel)
        observed = aug.augment_heatmaps([heatmaps])[0]
        assert observed.shape == heatmaps.shape
        assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6
        assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6
        assert np.allclose(observed.get_arr(), heatmaps_arr_l1)

    aug = iaa.Alpha(0.75, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8)
    assert np.allclose(observed, expected)

    aug = iaa.Alpha(0.75, None, iaa.Add(20))
    observed = aug.augment_image(base_img + 10)
    expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype(
        np.uint8)
    assert np.allclose(observed, expected)

    aug = iaa.Alpha(0.75, iaa.Add(10), None)
    observed = aug.augment_image(base_img + 10)
    expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype(
        np.uint8)
    assert np.allclose(observed, expected)

    base_img = np.zeros((1, 2, 1), dtype=np.uint8)
    nb_iterations = 1000
    aug = iaa.Alpha((0.0, 1.0), iaa.Add(10), iaa.Add(110))
    values = []
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        observed_val = np.round(np.average(observed)) - 10
        values.append(observed_val / 100)

    nb_bins = 5
    hist, _ = np.histogram(values,
                           bins=nb_bins,
                           range=(0.0, 1.0),
                           density=False)
    density_expected = 1.0 / nb_bins
    density_tolerance = 0.05
    for nb_samples in hist:
        density = nb_samples / nb_iterations
        assert density_expected - density_tolerance < density < density_expected + density_tolerance

    # bad datatype for factor
    got_exception = False
    try:
        _ = iaa.Alpha(False, iaa.Add(10), None)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # per_channel
    aug = iaa.Alpha(1.0,
                    iaa.Add((0, 100), per_channel=True),
                    None,
                    per_channel=True)
    observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8))
    uq = np.unique(observed)
    assert len(uq) > 1
    assert np.max(observed) > 80
    assert np.min(observed) < 20

    aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), None, per_channel=True)
    observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8))
    uq = np.unique(observed)
    assert len(uq) > 1
    assert np.max(observed) > 80
    assert np.min(observed) < 20

    aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), iaa.Add(0), per_channel=0.5)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_image(np.zeros((1, 1, 100), dtype=np.uint8))
        uq = np.unique(observed)
        if len(uq) == 1:
            seen[0] += 1
        elif len(uq) > 1:
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # bad datatype for per_channel
    got_exception = False
    try:
        _ = iaa.Alpha(0.5, iaa.Add(10), None, per_channel="test")
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # propagating
    aug = iaa.Alpha(0.5, iaa.Add(100), iaa.Add(50), name="AlphaTest")

    def propagator(images, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksImages(propagator=propagator)
    image = np.zeros((10, 10, 3), dtype=np.uint8) + 1
    observed = aug.augment_image(image, hooks=hooks)
    assert np.array_equal(observed, image)

    # -----
    # keypoints
    # -----
    kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)]
    kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3))

    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    # per_channel
    aug = iaa.Alpha(1.0,
                    iaa.Noop(),
                    iaa.Affine(translate_px={"x": 1}),
                    per_channel=True)
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.0,
                    iaa.Noop(),
                    iaa.Affine(translate_px={"x": 1}),
                    per_channel=True)
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(iap.Choice([0.49, 0.51]),
                    iaa.Noop(),
                    iaa.Affine(translate_px={"x": 1}),
                    per_channel=True)
    expected_same = kpsoi.deepcopy()
    expected_shifted = kpsoi.shift(x=1)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_keypoints([kpsoi])[0]
        if keypoints_equal([observed], [expected_same]):
            seen[0] += 1
        elif keypoints_equal([observed], [expected_shifted]):
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # propagating
    aug = iaa.Alpha(0.0,
                    iaa.Affine(translate_px={"x": 1}),
                    iaa.Affine(translate_px={"y": 1}),
                    name="AlphaTest")

    def propagator(kpsoi_to_aug, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksKeypoints(propagator=propagator)
    observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0]
    assert keypoints_equal([observed], [kpsoi])

    # -----
    # get_parameters()
    # -----
    first = iaa.Noop()
    second = iaa.Sequential([iaa.Add(1)])
    aug = iaa.Alpha(0.65, first, second, per_channel=1)
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Deterministic)
    assert isinstance(params[1], iap.Deterministic)
    assert 0.65 - 1e-6 < params[0].value < 0.65 + 1e-6
    assert params[1].value == 1

    # -----
    # get_children_lists()
    # -----
    first = iaa.Noop()
    second = iaa.Sequential([iaa.Add(1)])
    aug = iaa.Alpha(0.65, first, second, per_channel=1)
    children_lsts = aug.get_children_lists()
    assert len(children_lsts) == 2
    assert ia.is_iterable([lst for lst in children_lsts])
    assert first in children_lsts[0]
    assert second == children_lsts[1]
Example #17
0
def test_Alpha():
    reseed()

    base_img = np.zeros((3, 3, 1), dtype=np.uint8)
    heatmaps_arr = np.float32([[0.0, 0.0, 1.0],
                               [0.0, 0.0, 1.0],
                               [0.0, 1.0, 1.0]])
    heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0],
                                  [0.0, 0.0, 0.0],
                                  [0.0, 0.0, 1.0]])
    heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0],
                                  [0.0, 1.0, 0.0],
                                  [1.0, 1.0, 0.0]])
    heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3))

    aug = iaa.Alpha(1, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 10).astype(np.uint8)
    assert np.allclose(observed, expected)

    for per_channel in [False, True]:
        aug = iaa.Alpha(1, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}),
                        per_channel=per_channel)
        observed = aug.augment_heatmaps([heatmaps])[0]
        assert observed.shape == heatmaps.shape
        assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6
        assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6
        assert np.allclose(observed.get_arr(), heatmaps_arr_r1)

    aug = iaa.Alpha(0, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 20).astype(np.uint8)
    assert np.allclose(observed, expected)

    for per_channel in [False, True]:
        aug = iaa.Alpha(0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}),
                        per_channel=per_channel)
        observed = aug.augment_heatmaps([heatmaps])[0]
        assert observed.shape == heatmaps.shape
        assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6
        assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6
        assert np.allclose(observed.get_arr(), heatmaps_arr_l1)

    aug = iaa.Alpha(0.75, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8)
    assert np.allclose(observed, expected)

    aug = iaa.Alpha(0.75, None, iaa.Add(20))
    observed = aug.augment_image(base_img + 10)
    expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype(np.uint8)
    assert np.allclose(observed, expected)

    aug = iaa.Alpha(0.75, iaa.Add(10), None)
    observed = aug.augment_image(base_img + 10)
    expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype(np.uint8)
    assert np.allclose(observed, expected)

    base_img = np.zeros((1, 2, 1), dtype=np.uint8)
    nb_iterations = 1000
    aug = iaa.Alpha((0.0, 1.0), iaa.Add(10), iaa.Add(110))
    values = []
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        observed_val = np.round(np.average(observed)) - 10
        values.append(observed_val / 100)

    nb_bins = 5
    hist, _ = np.histogram(values, bins=nb_bins, range=(0.0, 1.0), density=False)
    density_expected = 1.0/nb_bins
    density_tolerance = 0.05
    for nb_samples in hist:
        density = nb_samples / nb_iterations
        assert density_expected - density_tolerance < density < density_expected + density_tolerance

    # bad datatype for factor
    got_exception = False
    try:
        _ = iaa.Alpha(False, iaa.Add(10), None)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # per_channel
    aug = iaa.Alpha(1.0, iaa.Add((0, 100), per_channel=True), None, per_channel=True)
    observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8))
    uq = np.unique(observed)
    assert len(uq) > 1
    assert np.max(observed) > 80
    assert np.min(observed) < 20

    aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), None, per_channel=True)
    observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8))
    uq = np.unique(observed)
    assert len(uq) > 1
    assert np.max(observed) > 80
    assert np.min(observed) < 20

    aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), iaa.Add(0), per_channel=0.5)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_image(np.zeros((1, 1, 100), dtype=np.uint8))
        uq = np.unique(observed)
        if len(uq) == 1:
            seen[0] += 1
        elif len(uq) > 1:
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # bad datatype for per_channel
    got_exception = False
    try:
        _ = iaa.Alpha(0.5, iaa.Add(10), None, per_channel="test")
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # propagating
    aug = iaa.Alpha(0.5, iaa.Add(100), iaa.Add(50), name="AlphaTest")

    def propagator(images, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksImages(propagator=propagator)
    image = np.zeros((10, 10, 3), dtype=np.uint8) + 1
    observed = aug.augment_image(image, hooks=hooks)
    assert np.array_equal(observed, image)

    # -----
    # keypoints
    # -----
    kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)]
    kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3))

    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    # per_channel
    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    expected_same = kpsoi.deepcopy()
    expected_shifted = kpsoi.shift(x=1)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_keypoints([kpsoi])[0]
        if keypoints_equal([observed], [expected_same]):
            seen[0] += 1
        elif keypoints_equal([observed], [expected_shifted]):
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # empty keypoints
    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints(ia.KeypointsOnImage([], shape=(1, 2, 3)))
    assert len(observed.keypoints) == 0
    assert observed.shape == (1, 2, 3)

    # propagating
    aug = iaa.Alpha(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaTest")

    def propagator(kpsoi_to_aug, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksKeypoints(propagator=propagator)
    observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0]
    assert keypoints_equal([observed], [kpsoi])

    # -----
    # polygons
    # -----
    ps = [ia.Polygon([(5, 5), (10, 5), (10, 10)])]
    psoi = ia.PolygonsOnImage(ps, shape=(20, 20, 3))

    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    # per_channel
    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    expected_same = psoi.deepcopy()
    expected_shifted = psoi.shift(left=1)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_polygons([psoi])[0]
        if observed.polygons[0].exterior_almost_equals(expected_same.polygons[0]):
            seen[0] += 1
        elif observed.polygons[0].exterior_almost_equals(expected_shifted.polygons[0]):
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # empty polygons
    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons(ia.PolygonsOnImage([], shape=(1, 2, 3)))
    assert len(observed.polygons) == 0
    assert observed.shape == (1, 2, 3)

    # propagating
    aug = iaa.Alpha(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaTest")

    def propagator(psoi_to_aug, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksKeypoints(propagator=propagator)  # no hooks for polygons yet, so we use HooksKeypoints
    observed = aug.augment_polygons([psoi], hooks=hooks)[0]
    assert observed.polygons[0].exterior_almost_equals(psoi.polygons[0])

    # -----
    # get_parameters()
    # -----
    first = iaa.Noop()
    second = iaa.Sequential([iaa.Add(1)])
    aug = iaa.Alpha(0.65, first, second, per_channel=1)
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Deterministic)
    assert isinstance(params[1], iap.Deterministic)
    assert 0.65 - 1e-6 < params[0].value < 0.65 + 1e-6
    assert params[1].value == 1

    # -----
    # get_children_lists()
    # -----
    first = iaa.Noop()
    second = iaa.Sequential([iaa.Add(1)])
    aug = iaa.Alpha(0.65, first, second, per_channel=1)
    children_lsts = aug.get_children_lists()
    assert len(children_lsts) == 2
    assert ia.is_iterable([lst for lst in children_lsts])
    assert first in children_lsts[0]
    assert second == children_lsts[1]