Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change **old_kwargs magic to explicit parameters #595

Merged
merged 3 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions changelogs/master/changed/20200112_simplified_augmenter_args.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simplified Standard Parameters of Augmenters #567
# Simplified Standard Parameters of Augmenters #567 #595

Changed the standard parameters shared by all augmenters to a
reduced and more self-explanatory set. Previously, all augmenters
Expand Down Expand Up @@ -26,6 +26,8 @@ The old parameter `random_state` is still accepted, but will
likely be deprecated in the future.

**[breaking]** This patch breaks if one relied on the order of
`name` and `random_state` instead of their names. These parameters
are now in inverted order, i.e. `(..., seed=None, name=None, ...)`
as seeds are much more commonly used than names.
`name`, `random_state` and `deterministic`. The new order is now
`seed=..., name=..., random_state=..., deterministic=...` (with the
latter two parameters being outdated or deprecated)
as opposed to previously
`name=..., deterministic=..., random_state=...`.
428 changes: 330 additions & 98 deletions imgaug/augmenters/arithmetic.py

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions imgaug/augmenters/artistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,16 @@ class Cartoon(meta.Augmenter):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -337,9 +345,11 @@ class Cartoon(meta.Augmenter):
def __init__(self, blur_ksize=(1, 5), segmentation_size=(0.8, 1.2),
saturation=(1.5, 2.5), edge_prevalence=(0.9, 1.1),
from_colorspace=colorlib.CSPACE_RGB,
seed=None, name=None, **old_kwargs):
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
super(Cartoon, self).__init__(
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)

self.blur_ksize = iap.handle_continuous_param(
blur_ksize, "blur_ksize", value_range=(0, None),
Expand Down
235 changes: 176 additions & 59 deletions imgaug/augmenters/blend.py

Large diffs are not rendered by default.

110 changes: 86 additions & 24 deletions imgaug/augmenters/blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,16 @@ class GaussianBlur(meta.Augmenter):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -431,9 +439,11 @@ class GaussianBlur(meta.Augmenter):
"""

def __init__(self, sigma=(0.0, 3.0),
seed=None, name=None, **old_kwargs):
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
super(GaussianBlur, self).__init__(
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)

self.sigma = iap.handle_continuous_param(
sigma, "sigma", value_range=(0, None), tuple_to_uniform=True,
Expand Down Expand Up @@ -520,8 +530,16 @@ class AverageBlur(meta.Augmenter):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -543,9 +561,12 @@ class AverageBlur(meta.Augmenter):

"""

def __init__(self, k=(1, 7), seed=None, name=None, **old_kwargs):
def __init__(self, k=(1, 7),
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
super(AverageBlur, self).__init__(
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)

# TODO replace this by iap.handle_discrete_kernel_size()
self.mode = "single"
Expand Down Expand Up @@ -711,8 +732,16 @@ class MedianBlur(meta.Augmenter):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -729,9 +758,12 @@ class MedianBlur(meta.Augmenter):

"""

def __init__(self, k=(1, 7), seed=None, name=None, **old_kwargs):
def __init__(self, k=(1, 7),
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
super(MedianBlur, self).__init__(
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)

# TODO replace this by iap.handle_discrete_kernel_size()
self.k = iap.handle_discrete_param(
Expand Down Expand Up @@ -865,8 +897,16 @@ class BilateralBlur(meta.Augmenter):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -881,10 +921,12 @@ class BilateralBlur(meta.Augmenter):
"""

def __init__(self, d=(1, 9), sigma_color=(10, 250), sigma_space=(10, 250),
seed=None, name=None, **old_kwargs):
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
# pylint: disable=invalid-name
super(BilateralBlur, self).__init__(
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)

self.d = iap.handle_discrete_param(
d, "d", value_range=(1, None), tuple_to_uniform=True,
Expand Down Expand Up @@ -995,8 +1037,16 @@ class MotionBlur(iaa_convolutional.Convolve):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -1013,7 +1063,8 @@ class MotionBlur(iaa_convolutional.Convolve):
"""

def __init__(self, k=(3, 7), angle=(0, 360), direction=(-1.0, 1.0), order=1,
seed=None, name=None, **old_kwargs):
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
# TODO allow (1, None) and set to identity matrix if k == 1
k_param = iap.handle_discrete_param(
k, "k", value_range=(3, None), tuple_to_uniform=True,
Expand All @@ -1030,7 +1081,8 @@ def __init__(self, k=(3, 7), angle=(0, 360), direction=(-1.0, 1.0), order=1,

super(MotionBlur, self).__init__(
matrix_gen,
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)


class _MotionBlurMatrixGenerator(object):
Expand Down Expand Up @@ -1123,8 +1175,16 @@ class MeanShiftBlur(meta.Augmenter):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand All @@ -1135,9 +1195,11 @@ class MeanShiftBlur(meta.Augmenter):

"""
def __init__(self, spatial_radius=(5.0, 40.0), color_radius=(5.0, 40.0),
seed=None, name=None, **old_kwargs):
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
super(MeanShiftBlur, self).__init__(
seed=seed, name=name, **old_kwargs)
seed=seed, name=name,
random_state=random_state, deterministic=deterministic)
self.spatial_window_radius = iap.handle_continuous_param(
spatial_radius, "spatial_radius",
value_range=(0.01, None), tuple_to_uniform=True,
Expand Down
27 changes: 19 additions & 8 deletions imgaug/augmenters/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,16 @@ class RandAugment(meta.Sequential):
name : None or str, optional
See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

**old_kwargs
Outdated parameters. Avoid using these.
random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
Old name for parameter `seed`.
Its usage will not yet cause a deprecation warning,
but it is still recommended to use `seed` now.
Outdated since 0.4.0.

deterministic : bool, optional
Deprecated since 0.4.0.
See method ``to_deterministic()`` for an alternative and for
details about what the "deterministic mode" actually does.

Examples
--------
Expand Down Expand Up @@ -173,9 +181,11 @@ class RandAugment(meta.Sequential):
# N=2, M=28 is optimal for ImageNet with EfficientNet-B7
# for cval they use [125, 122, 113]
def __init__(self, n=2, m=(6, 12), cval=128,
seed=None, name=None, **old_kwargs):
seed=None, name=None,
random_state="deprecated", deterministic="deprecated"):
# pylint: disable=invalid-name
random_state = iarandom.RNG(seed)
seed = seed if random_state == "deprecated" else random_state
rng = iarandom.RNG(seed)

# we don't limit the value range to 10 here, because the paper
# gives several examples of using more than 10 for M
Expand All @@ -198,16 +208,17 @@ def __init__(self, n=2, m=(6, 12), cval=128,
# assign random state to all child augmenters
for lst in [initial_augs, main_augs]:
for augmenter in lst:
augmenter.random_state = random_state
augmenter.random_state = rng

super(RandAugment, self).__init__(
[
meta.Sequential(initial_augs,
seed=random_state.derive_rng_()),
seed=rng.derive_rng_()),
meta.SomeOf(n, main_augs, random_order=True,
seed=random_state.derive_rng_())
seed=rng.derive_rng_())
],
seed=random_state, name=name, **old_kwargs
seed=rng, name=name,
random_state=random_state, deterministic=deterministic
)

@classmethod
Expand Down
Loading