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

Introduce module imgaug.random #375

Merged
merged 34 commits into from
Aug 14, 2019
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ad144cf
Introduce module imgaug.random
aleju Aug 4, 2019
e8e61cc
Refactor library to directly call new random module everywhere
aleju Aug 7, 2019
5e44e58
Fix bad test for Poisson
aleju Aug 10, 2019
7a588d0
Fix sometimes failing NP 1.17 check
aleju Aug 10, 2019
9aa7db6
Simplify RNG functions
aleju Aug 10, 2019
9961341
Introduce class imgaug.random.RNG
aleju Aug 10, 2019
a399513
Improve function and argument names in imgaug.random
aleju Aug 11, 2019
dbb28a1
Fix random.seed() crashing in numpy <=1.16
aleju Aug 11, 2019
fccb1d5
Fix comparison for RandomStates
aleju Aug 11, 2019
3326e5f
Remove RNG copies wherever possible
aleju Aug 11, 2019
7741614
Add and improve docstrings in imgaug.random module
aleju Aug 11, 2019
b0f5220
Fix various issues in .random and clean up code
aleju Aug 13, 2019
fa4ec08
Add tests for functions in .random
aleju Aug 13, 2019
6e3bd7a
Fix ignored out arg in .random & fix docstring errors
aleju Aug 13, 2019
32d7651
Add tests for random.RNG
aleju Aug 13, 2019
9ffc68d
Update docstrings for random_state args wrt supported datatypes
aleju Aug 13, 2019
5c86abf
Improve sampling order of parameters
aleju Aug 14, 2019
0c84bc1
Fix .random tests for numpy <1.17 and older mock versions
aleju Aug 14, 2019
d896828
Remove CURRENT_RANDOM_STATE, remove default for n in derive_generator…
aleju Aug 14, 2019
d78f2e5
Remove todo
aleju Aug 14, 2019
4d9c706
Rename copy_generator_unless_global_rng() to *_global_generator()
aleju Aug 14, 2019
965888a
Refactor in-place marking of seed()
aleju Aug 14, 2019
63150cd
Switch call of imgaug.seed() to imgaug.random.seed()
aleju Aug 14, 2019
5a70bec
Remove deprecation warning from imgaug.imgaug.seed()
aleju Aug 14, 2019
cfe083b
Refactor, fix and improve docs of old random functions
aleju Aug 14, 2019
1224234
Reactivate and adapt tests for old random functions
aleju Aug 14, 2019
76b40a5
Remove redundant deprecation markers
aleju Aug 14, 2019
a84290d
Change global RNG default seed & add seed constant
aleju Aug 14, 2019
01f0f03
Fix TruncatedNormal crashing with RNG
aleju Aug 14, 2019
2d5ee31
Add tests for TruncatedNormal
aleju Aug 14, 2019
6be0ea1
Update changelog with .random changes
aleju Aug 14, 2019
fba8ce4
Decrease stochasticity of test
aleju Aug 14, 2019
c0a11bc
Add module-level docstring for .random module
aleju Aug 14, 2019
44f9f3b
Fix test failing in py2.7
aleju Aug 14, 2019
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
67 changes: 67 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -357,6 +357,73 @@ Changes:
`Lambda.__init__()`, `AssertLambda.__init__()`, `AssertShape.__init__()`
and hence breaks if one relied on that order.

## New RNG handling #375

* Adapted library to automatically use the new `numpy.random` classes of
numpy 1.17 -- if they are available. If they are not available (i.e. numpy
version is <=1.16), the library automatically falls back to the old
interface (i.e. `numpy.random.RandomState`).
* Added module `imgaug.random`.
* Added class `imgaug.random.RNG`. This is now the preferred way to represent
RNG states (previously: `numpy.random.RandomState`). Instantiate it
via e.g. `RNG(1052912236)`, where `1052912236` is a seed.
* Added `imgaug.random.supports_new_rng_style()`.
* Added `imgaug.random.get_global_rng()`.
* Added `imgaug.random.seed()`.
* Added `imgaug.random.normalize_generator()`.
* Added `imgaug.random.normalize_generator_()`.
* Added `imgaug.random.convert_seed_to_generator()`.
* Added `imgaug.random.convert_seed_sequence_to_generator()`.
* Added `imgaug.random.create_pseudo_random_generator_()`.
* Added `imgaug.random.create_fully_random_generator()`.
* Added `imgaug.random.generate_seed_()`.
* Added `imgaug.random.generate_seeds_()`.
* Added `imgaug.random.copy_generator()`.
* Added `imgaug.random.copy_generator_unless_global_generator()`.
* Added `imgaug.random.reset_generator_cache_()`.
* Added `imgaug.random.derive_generator_()`.
* Added `imgaug.random.derive_generators_()`.
* Added `imgaug.random.get_generator_state()`.
* Added `imgaug.random.set_generator_state_()`.
* Added `imgaug.random.is_generator_equal_to()`.
* Added `imgaug.random.advance_generator_()`.
* Added `imgaug.random.polyfill_integers()`.
* Added `imgaug.random.polyfill_random()`.
* Refactored all arguments related to random state handling to also accept
`imgaug.random.RNG`, as well as the new numpy random classes. This
particularly affects `imgaug.augmenters.meta.Augmenter` and
`imgaug.parameters.StochasticParameter` (argument `random_state` for both).
* Marked old RNG related functions in `imgaug.imgaug` as deprecated.
They will now produce warnings and redirect towards corresponding functions
in `imgaug.random`. This does not yet affect `imgaug.imgaug.seed()`.
It does affect the functions listed below.
* `imgaug.imgaug.normalize_random_state()`.
* `imgaug.imgaug.current_random_state()`.
* `imgaug.imgaug.new_random_state()`.
* `imgaug.imgaug.dummy_random_state()`.
* `imgaug.imgaug.copy_random_state()`.
* `imgaug.imgaug.derive_random_state()`.
* `imgaug.imgaug.derive_random_states()`.
* `imgaug.imgaug.forward_random_state()`.
* [rarely breaking] Removed `imgaug.imgaug.CURRENT_RANDOM_STATE`.
Use `imgaug.random.get_global_rng()` instead.
* [rarely breaking] Removed `imgaug.imgaug.SEED_MIN_VALUE`.
Use `imgaug.random.SEED_MIN_VALUE` instead or sample seeds via
`imgaug.random.generate_seeds_()`.
* [rarely breaking] Removed `imgaug.imgaug.SEED_MAX_VALUE`.
Use `imgaug.random.SEED_MAX_VALUE` instead or sample seeds via
`imgaug.random.generate_seeds_()`.
* Optimized RNG handling throughout all augmenters to minimize the number of
RNG copies. RNGs are now re-used as often as possible. This improves
performance, but has the disadvantage that adding images to a batch will now
often affect the samples of the other images in the same batch. E.g.
previously for a batch of images `A,B,C` and seed `1`, the samples of `A,B,C`
would remain unchanged if the batch was changed to `A,B,C,D` (provided the
seed stayed the same). Now, if `D` is added the samples of `A,B,C` may
change.
* [breaking] The above listed changes will lead to different values being
sampled for the same seeds (compared to past versions of the library).

## Fixes

* Fixed an issue with `Polygon.clip_out_of_image()`,
3 changes: 2 additions & 1 deletion checks/check_channel_shuffle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from imgaug import augmenters as iaa
import imgaug as ia
import imgaug.random as iarandom

ia.seed(1)
iarandom.seed(1)


def main():
3 changes: 2 additions & 1 deletion checks/check_elastic_transformation.py
Original file line number Diff line number Diff line change
@@ -10,14 +10,15 @@
import imgaug as ia
from imgaug import augmenters as iaa
from imgaug.augmenters import meta
import imgaug.random as iarandom


def main():
image = data.astronaut()
image = ia.imresize_single_image(image, (128, 128))

# check if scipy and cv2 remap similarly
rs = ia.new_random_state(1)
rs = iarandom.RNG(1)
aug_scipy = ElasticTransformationScipy(alpha=30, sigma=3, random_state=rs, deterministic=True)
aug_cv2 = ElasticTransformationCv2(alpha=30, sigma=3, random_state=rs, deterministic=True)
augs_scipy = aug_scipy.augment_images([image] * 8)
3 changes: 2 additions & 1 deletion checks/check_piecewise_affine.py
Original file line number Diff line number Diff line change
@@ -3,9 +3,10 @@
import numpy as np

import imgaug as ia
import imgaug.random as iarandom
from imgaug import augmenters as iaa

ia.seed(3)
iarandom.seed(3)


def main():
7 changes: 4 additions & 3 deletions checks/check_seed.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
from skimage import data

import imgaug as ia
import imgaug.random as iarandom
from imgaug import augmenters as iaa


@@ -14,15 +15,15 @@ def main():
unseeded1 = aug.draw_grid(img, cols=8, rows=1)
unseeded2 = aug.draw_grid(img, cols=8, rows=1)

ia.seed(1000)
iarandom.seed(1000)
seeded1 = aug.draw_grid(img, cols=8, rows=1)
seeded2 = aug.draw_grid(img, cols=8, rows=1)

ia.seed(1000)
iarandom.seed(1000)
reseeded1 = aug.draw_grid(img, cols=8, rows=1)
reseeded2 = aug.draw_grid(img, cols=8, rows=1)

ia.seed(1001)
iarandom.seed(1001)
reseeded3 = aug.draw_grid(img, cols=8, rows=1)
reseeded4 = aug.draw_grid(img, cols=8, rows=1)

8 changes: 5 additions & 3 deletions imgaug/augmentables/polys.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import collections

from .. import imgaug as ia
from .. import random as iarandom
from .utils import normalize_shape, interpolate_points


@@ -532,6 +533,8 @@ def draw_on_image(self,
assert alpha is not None
assert size is not None

# FIXME due to the np.array(.) and the assert at ndim==2 below, this
# will always fail on 2D images?
color_face = color_face if color_face is not None else np.array(color)
color_lines = color_lines if color_lines is not None else np.array(color) * 0.5
color_points = color_points if color_points is not None else np.array(color) * 0.5
@@ -1367,9 +1370,8 @@ def recover_from(self, new_exterior, old_polygon, random_state=0):
if polygon.is_valid:
return polygon

if not isinstance(random_state, np.random.RandomState):
random_state = np.random.RandomState(random_state)
rss = ia.derive_random_states(random_state, 3)
random_state = iarandom.RNG(random_state)
rss = random_state.derive_rngs_(3)

# remove consecutive duplicate points
new_exterior = self._remove_consecutive_duplicate_points(new_exterior)
Loading