Skip to content

Commit

Permalink
Merge pull request #557 from aleju/fix_seed
Browse files Browse the repository at this point in the history
Fix random.seed not always seeding in-place
  • Loading branch information
aleju authored Jan 11, 2020
2 parents c9b1c81 + 0000c38 commit 76e832c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
8 changes: 8 additions & 0 deletions changelogs/master/fixed/20200110_fixed_seed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Fixed `random.seed` not always seeding in-place #557

Fixed `imgaug.random.seed()` not seeding the global `RNG` in-place
in numpy 1.17+. The (unfixed) function instead created a new
global `RNG` with the given seed. This set the seed of augmenters
created *after* the `seed()` call, but not of augmenters created
*before* the `seed()` call as they would continue to use the old
global RNG.
9 changes: 5 additions & 4 deletions imgaug/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,11 @@ def seed(entropy):


def _seed_np117_(entropy):
# pylint: disable=global-statement
global GLOBAL_RNG
# TODO any way to seed the Generator object instead of creating a new one?
GLOBAL_RNG = RNG(entropy)
# We can't easily seed a BitGenerator in-place, nor can we easily modify
# a Generator's bit_generator in-place. So instead we create a new
# bit generator and set the current global RNG's internal bit generator
# state to a copy of the new bit generator's state.
get_global_rng().state = BIT_GENERATOR(entropy).state


def _seed_np116_(entropy):
Expand Down
39 changes: 39 additions & 0 deletions test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import numpy as np

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

Expand Down Expand Up @@ -704,6 +705,44 @@ def test_integrationtest(self):
iarandom.seed(1)
assert iarandom.GLOBAL_RNG.equals(iarandom.RNG(1))

def test_seed_affects_augmenters_created_after_its_call(self):
image = np.full((50, 50, 3), 128, dtype=np.uint8)

images_aug = []
for _ in np.arange(5):
iarandom.seed(100)
aug = iaa.AdditiveGaussianNoise(scale=50, per_channel=True)
images_aug.append(aug(image=image))

# assert all images identical
for other_image_aug in images_aug[1:]:
assert np.array_equal(images_aug[0], other_image_aug)

# but different seed must lead to different image
iarandom.seed(101)
aug = iaa.AdditiveGaussianNoise(scale=50, per_channel=True)
image_aug = aug(image=image)
assert not np.array_equal(images_aug[0], image_aug)

def test_seed_affects_augmenters_created_before_its_call(self):
image = np.full((50, 50, 3), 128, dtype=np.uint8)

images_aug = []
for _ in np.arange(5):
aug = iaa.AdditiveGaussianNoise(scale=50, per_channel=True)
iarandom.seed(100)
images_aug.append(aug(image=image))

# assert all images identical
for other_image_aug in images_aug[1:]:
assert np.array_equal(images_aug[0], other_image_aug)

# but different seed must lead to different image
aug = iaa.AdditiveGaussianNoise(scale=50, per_channel=True)
iarandom.seed(101)
image_aug = aug(image=image)
assert not np.array_equal(images_aug[0], image_aug)


class Test_normalize_generator(_Base):
@mock.patch("imgaug.random.normalize_generator_")
Expand Down

0 comments on commit 76e832c

Please sign in to comment.