Пример #1
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
Пример #2
0
def gate_dtypes(dtypes, allowed, disallowed, augmenter=None):
    assert len(allowed) > 0
    assert ia.is_string(allowed[0])
    if len(disallowed) > 0:
        assert ia.is_string(disallowed[0])

    if ia.is_np_array(dtypes):
        dtypes = [dtypes.dtype]
    else:
        dtypes = [np.dtype(dtype) if not ia.is_np_array(dtype) else dtype.dtype for dtype in dtypes]
    for dtype in dtypes:
        if dtype.name in allowed:
            pass
        elif dtype.name in disallowed:
            if augmenter is None:
                raise ValueError("Got dtype '%s', which is a forbidden dtype (%s)." % (
                    dtype.name, ", ".join(disallowed)
                ))
            else:
                raise ValueError("Got dtype '%s' in augmenter '%s' (class '%s'), which is a forbidden dtype (%s)." % (
                    dtype.name, augmenter.name, augmenter.__class__.__name__, ", ".join(disallowed)
                ))
        else:
            if augmenter is None:
                warnings.warn(("Got dtype '%s', which was neither explicitly allowed "
                               + "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors.") % (
                        dtype.name, ", ".join(allowed), ", ".join(disallowed)
                    ))
            else:
                warnings.warn(("Got dtype '%s' in augmenter '%s' (class '%s'), which was neither explicitly allowed "
                               + "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors.") % (
                    dtype.name, augmenter.name, augmenter.__class__.__name__, ", ".join(allowed), ", ".join(disallowed)
                ))
Пример #3
0
def gate_dtypes(dtypes, allowed, disallowed, augmenter=None):
    # assume that at least one allowed dtype string is given
    assert len(allowed) > 0, (
        "Expected at least one dtype to be allowed, but got an empty list.")
    # check only first dtype for performance
    assert ia.is_string(
        allowed[0]), ("Expected only strings as dtypes, but got type %s." %
                      (type(allowed[0]), ))

    if len(disallowed) > 0:
        # check only first disallowed dtype for performance
        assert ia.is_string(disallowed[0]), (
            "Expected only strings as dtypes, but got type %s." %
            (type(disallowed[0]), ))

    # verify that "allowed" and "disallowed" do not contain overlapping
    # dtypes
    inters = set(allowed).intersection(set(disallowed))
    nb_overlapping = len(inters)
    assert nb_overlapping == 0, (
        "Expected 'allowed' and 'disallowed' to not contain the same dtypes, "
        "but %d appeared in both arguments. Got allowed: %s, "
        "disallowed: %s, intersection: %s" %
        (nb_overlapping, ", ".join(allowed), ", ".join(disallowed),
         ", ".join(inters)))

    dtypes = normalize_dtypes(dtypes)

    for dtype in dtypes:
        if dtype.name in allowed:
            pass
        elif dtype.name in disallowed:
            if augmenter is None:
                raise ValueError(
                    "Got dtype '%s', which is a forbidden dtype (%s)." %
                    (dtype.name, ", ".join(disallowed)))

            raise ValueError(
                "Got dtype '%s' in augmenter '%s' (class '%s'), which "
                "is a forbidden dtype (%s)." %
                (dtype.name, augmenter.name, augmenter.__class__.__name__,
                 ", ".join(disallowed)))
        else:
            if augmenter is None:
                ia.warn(
                    "Got dtype '%s', which was neither explicitly allowed "
                    "(%s), nor explicitly disallowed (%s). Generated "
                    "outputs may contain errors." %
                    (dtype.name, ", ".join(allowed), ", ".join(disallowed)))
            else:
                ia.warn(
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which was "
                    "neither explicitly allowed (%s), nor explicitly "
                    "disallowed (%s). Generated outputs may contain "
                    "errors." %
                    (dtype.name, augmenter.name, augmenter.__class__.__name__,
                     ", ".join(allowed), ", ".join(disallowed)))
Пример #4
0
def gate_dtypes(dtypes, allowed, disallowed, augmenter=None):
    assert len(allowed) > 0
    assert ia.is_string(allowed[0])
    if len(disallowed) > 0:
        assert ia.is_string(disallowed[0])

    # verify that "allowed" and "disallowed" do not contain overlapping
    # dtypes
    inters = set(allowed).intersection(set(disallowed))
    nb_overlapping = len(inters)
    assert nb_overlapping == 0, (
        "Expected 'allowed' and 'disallowed' to not contain the same dtypes, "
        "but %d appeared in both arguments. Got allowed: %s, "
        "disallowed: %s, intersection: %s" %
        (nb_overlapping, ", ".join(allowed), ", ".join(disallowed),
         ", ".join(inters)))

    # don't use is_np_array() here, because this is supposed to handle numpy
    # scalars too
    if hasattr(dtypes, "dtype"):
        dtypes = [dtypes.dtype]
    else:
        dtypes = [
            dtype if not hasattr(dtype, "dtype") else dtype.dtype
            for dtype in dtypes
        ]

    for dtype in dtypes:
        if dtype.name in allowed:
            pass
        elif dtype.name in disallowed:
            if augmenter is None:
                raise ValueError(
                    "Got dtype '%s', which is a forbidden dtype (%s)." %
                    (dtype.name, ", ".join(disallowed)))
            else:
                raise ValueError(
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which is a forbidden dtype (%s)."
                    % (dtype.name, augmenter.name,
                       augmenter.__class__.__name__, ", ".join(disallowed)))
        else:
            if augmenter is None:
                warnings.warn((
                    "Got dtype '%s', which was neither explicitly allowed " +
                    "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors."
                ) % (dtype.name, ", ".join(allowed), ", ".join(disallowed)))
            else:
                warnings.warn((
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which was neither explicitly allowed "
                    +
                    "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors."
                ) % (dtype.name, augmenter.name, augmenter.__class__.__name__,
                     ", ".join(allowed), ", ".join(disallowed)))
Пример #5
0
    def __init__(self, paths, values):
        if ia.is_string(paths):
            paths = [paths]
            values = [values]

        self.paths = [".".join(path_i.split(".")[:-1]) for path_i in paths]
        self.cnames = [path_i.split(".")[-1] for path_i in paths]
        self.values = values
        self.old_values = None
Пример #6
0
def get_minimal_dtype_by_value_range(low, high, kind, default):
    assert low <= high, "Expected low to be less or equal than high, got %s and %s." % (low, high)
    for dt in KIND_TO_DTYPES[kind]:
        min_value, _center_value, max_value = get_value_range_of_dtype(dt)
        if min_value <= low and high <= max_value:
            return np.dtype(dt)
    if ia.is_string(default) and default == "raise":
        raise Exception("Could not find dtype of kind '%s' within value range [%s, %s]" % (kind, low, high))
    return default
Пример #7
0
    def __init__(self, value):
        StochasticParameter.__init__(self)

        if isinstance(value, StochasticParameter):
            self.value = value.draw_sample()
        elif ia.is_single_number(value) or ia.is_string(value):
            self.value = value
        else:
            raise Exception("Expected StochasticParameter object or number or string, got %s." % (type(value),))
Пример #8
0
def gate_dtypes(dtypes, allowed, disallowed, augmenter=None):
    assert len(allowed) > 0
    assert ia.is_string(allowed[0])
    if len(disallowed) > 0:
        assert ia.is_string(disallowed[0])

    # don't use is_np_array() here, because this is supposed to handle numpy
    # scalars too
    if hasattr(dtypes, "dtype"):
        dtypes = [dtypes.dtype]
    else:
        dtypes = [
            dtype if not hasattr(dtype, "dtype") else dtype.dtype
            for dtype in dtypes
        ]

    for dtype in dtypes:
        if dtype.name in allowed:
            pass
        elif dtype.name in disallowed:
            if augmenter is None:
                raise ValueError(
                    "Got dtype '%s', which is a forbidden dtype (%s)." %
                    (dtype.name, ", ".join(disallowed)))
            else:
                raise ValueError(
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which is a forbidden dtype (%s)."
                    % (dtype.name, augmenter.name,
                       augmenter.__class__.__name__, ", ".join(disallowed)))
        else:
            if augmenter is None:
                warnings.warn((
                    "Got dtype '%s', which was neither explicitly allowed " +
                    "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors."
                ) % (dtype.name, ", ".join(allowed), ", ".join(disallowed)))
            else:
                warnings.warn((
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which was neither explicitly allowed "
                    +
                    "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors."
                ) % (dtype.name, augmenter.name, augmenter.__class__.__name__,
                     ", ".join(allowed), ", ".join(disallowed)))
Пример #9
0
    def __init__(self, value):
        StochasticParameter.__init__(self)

        if isinstance(value, StochasticParameter):
            self.value = value.draw_sample()
        elif ia.is_single_number(value) or ia.is_string(value):
            self.value = value
        else:
            raise Exception(
                "Expected StochasticParameter object or number or string, got %s."
                % (type(value), ))
Пример #10
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
Пример #11
0
def get_minimal_dtype_by_value_range(low, high, kind, default):
    assert low <= high, "Expected low to be less or equal than high, got %s and %s." % (
        low, high)
    for dt in KIND_TO_DTYPES[kind]:
        min_value, _center_value, max_value = get_value_range_of_dtype(dt)
        if min_value <= low and high <= max_value:
            return np.dtype(dt)
    if ia.is_string(default) and default == "raise":
        raise Exception(
            "Could not find dtype of kind '%s' within value range [%s, %s]" %
            (kind, low, high))
    return default
Пример #12
0
def gate_dtypes(dtypes, allowed, disallowed, augmenter=None):
    assert len(allowed) > 0
    assert ia.is_string(allowed[0])
    if len(disallowed) > 0:
        assert ia.is_string(disallowed[0])

    if ia.is_np_array(dtypes):
        dtypes = [dtypes.dtype]
    else:
        dtypes = [
            np.dtype(dtype) if not ia.is_np_array(dtype) else dtype.dtype
            for dtype in dtypes
        ]
    for dtype in dtypes:
        if dtype.name in allowed:
            pass
        elif dtype.name in disallowed:
            if augmenter is None:
                raise ValueError(
                    "Got dtype '%s', which is a forbidden dtype (%s)." %
                    (dtype.name, ", ".join(disallowed)))
            else:
                raise ValueError(
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which is a forbidden dtype (%s)."
                    % (dtype.name, augmenter.name,
                       augmenter.__class__.__name__, ", ".join(disallowed)))
        else:
            if augmenter is None:
                warnings.warn((
                    "Got dtype '%s', which was neither explicitly allowed "
                    "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors.."
                ) % (dtype.name, augmenter.name, augmenter.__class__.__name__,
                     ", ".join(allowed), ", ".join(disallowed)))
            else:
                warnings.warn((
                    "Got dtype '%s' in augmenter '%s' (class '%s'), which was neither explicitly allowed "
                    "(%s), nor explicitly disallowed (%s). Generated outputs may contain errors.."
                ) % (dtype.name, augmenter.name, augmenter.__class__.__name__,
                     ", ".join(allowed), ", ".join(disallowed)))
Пример #13
0
def deepcopy_fast(obj):
    if obj is None:
        return None
    if ia.is_single_number(obj) or ia.is_string(obj):
        return obj
    if isinstance(obj, list):
        return [deepcopy_fast(el) for el in obj]
    if isinstance(obj, tuple):
        return tuple([deepcopy_fast(el) for el in obj])
    if ia.is_np_array(obj):
        return np.copy(obj)
    if hasattr(obj, "deepcopy"):
        return obj.deepcopy()
    return copylib.deepcopy(obj)
Пример #14
0
def get_minimal_dtype_for_values(values,
                                 allowed_kinds,
                                 default,
                                 allow_bool_as_intlike=True):
    values_normalized = []
    for value in values:
        if ia.is_np_array(value):
            values_normalized.extend([np.min(values), np.max(values)])
        else:
            values_normalized.append(value)
    vmin = np.min(values_normalized)
    vmax = np.max(values_normalized)
    possible_kinds = []
    if ia.is_single_float(vmin) or ia.is_single_float(vmax):
        # at least one is a float
        possible_kinds.append("f")
    elif ia.is_single_bool(vmin) and ia.is_single_bool(vmax):
        # both are bools
        possible_kinds.extend(["b", "u", "i"])
    else:
        # at least one of them is an integer and none is float
        if vmin >= 0:
            possible_kinds.append("u")
        possible_kinds.append("i")
        # vmin and vmax are already guarantueed to not be float due to if-statement above
        if allow_bool_as_intlike and 0 <= vmin <= 1 and 0 <= vmax <= 1:
            possible_kinds.append("b")

    for allowed_kind in allowed_kinds:
        if allowed_kind in possible_kinds:
            dt = get_minimal_dtype_by_value_range(vmin,
                                                  vmax,
                                                  allowed_kind,
                                                  default=None)
            if dt is not None:
                return dt

    if ia.is_string(default) and default == "raise":
        raise Exception((
            "Did not find matching dtypes for vmin=%s (type %s) and vmax=%s (type %s). "
            + "Got %s input values of types %s.") %
                        (vmin, type(vmin), vmax, type(vmax), ", ".join(
                            [str(type(value)) for value in values])))
    return default
Пример #15
0
def get_minimal_dtype_for_values(values, allowed_kinds, default, allow_bool_as_intlike=True):
    values_normalized = []
    for value in values:
        if ia.is_np_array(value):
            values_normalized.extend([np.min(values), np.max(values)])
        else:
            values_normalized.append(value)
    vmin = np.min(values_normalized)
    vmax = np.max(values_normalized)
    possible_kinds = []
    if ia.is_single_float(vmin) or ia.is_single_float(vmax):
        # at least one is a float
        possible_kinds.append("f")
    elif ia.is_single_bool(vmin) and ia.is_single_bool(vmax):
        # both are bools
        possible_kinds.extend(["b", "u", "i"])
    else:
        # at least one of them is an integer and none is float
        if vmin >= 0:
            possible_kinds.append("u")
        possible_kinds.append("i")
        # vmin and vmax are already guarantueed to not be float due to if-statement above
        if allow_bool_as_intlike and 0 <= vmin <= 1 and 0 <= vmax <= 1:
            possible_kinds.append("b")

    for allowed_kind in allowed_kinds:
        if allowed_kind in possible_kinds:
            dt = get_minimal_dtype_by_value_range(vmin, vmax, allowed_kind, default=None)
            if dt is not None:
                return dt

    if ia.is_string(default) and default == "raise":
        raise Exception(("Did not find matching dtypes for vmin=%s (type %s) and vmax=%s (type %s). "
                         + "Got %s input values of types %s.") % (
            vmin, type(vmin), vmax, type(vmax), ", ".join([str(type(value)) for value in values])))
    return default