Skip to content

Commit

Permalink
Add missing pickling tests of augmenters
Browse files Browse the repository at this point in the history
Some new augmenters had no tests yet that verified whether these
augmenters can be pickled. This patch adds these tests to all
remaining augmenters.
  • Loading branch information
aleju committed Jan 14, 2020
1 parent a5207c7 commit 02642e9
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 5 deletions.
16 changes: 16 additions & 0 deletions test/augmenters/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,18 @@ def test_get_parameters(self):
assert params[5] is aug.cval
assert params[6] is aug.fill_per_channel

def test_pickleable(self):
aug = iaa.Cutout(
nb_iterations=1,
position=(0.5, 0.5),
size=0.1,
squared=0.6,
fill_mode=["gaussian", "constant"],
cval=(0, 255),
fill_per_channel=0.5
)
runtest_pickleable_uint8_img(aug)


# this is mostly copy-pasted cutout code from
# https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py
Expand Down Expand Up @@ -5663,6 +5675,10 @@ def test_p_is_one_some_values_above_threshold(self):
assert observed.dtype.name == "uint8"
assert np.array_equal(observed, expected)

def test_pickleable(self):
aug = iaa.pillike.Solarize(p=1.0, threshold=(100, 110))
runtest_pickleable_uint8_img(aug)


class TestContrastNormalization(unittest.TestCase):
@unittest.skipIf(sys.version_info[0] <= 2,
Expand Down
6 changes: 5 additions & 1 deletion test/augmenters/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np

from imgaug import augmenters as iaa
from imgaug.testutils import reseed
from imgaug.testutils import reseed, runtest_pickleable_uint8_img


class TestRandAugment(unittest.TestCase):
Expand Down Expand Up @@ -93,3 +93,7 @@ def test_get_parameters(self):
assert params[0] is aug[1].n
assert params[1] is aug._m
assert params[2] is aug._cval

def test_pickleable(self):
aug = iaa.RandAugment(m=(0, 10), n=(1, 2))
runtest_pickleable_uint8_img(aug, iterations=50)
40 changes: 39 additions & 1 deletion test/augmenters/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import mock
import tempfile
import os
try:
import cPickle as pickle
except ImportError:
import pickle

import matplotlib
matplotlib.use('Agg') # fix execution of tests involving matplotlib on travis
Expand All @@ -23,7 +27,7 @@
import imgaug as ia
from imgaug import augmenters as iaa
from imgaug import random as iarandom
from imgaug.testutils import reseed
from imgaug.testutils import reseed, TemporaryDirectory
import imgaug.augmenters.debug as debuglib


Expand Down Expand Up @@ -305,3 +309,37 @@ def test_temp_directory(self):
assert np.array_equal(imageio.imread(path1), expected)
assert np.array_equal(imageio.imread(path2), expected)
assert np.array_equal(imageio.imread(path_latest), expected)

def test_pickleable(self):
shape = (16, 16, 3)
image = np.mod(np.arange(int(np.prod(shape))), 256).astype(np.uint8)
image = image.reshape(shape)

with TemporaryDirectory() as folder_path:
path1 = os.path.join(folder_path, "batch_000000.png")
path2 = os.path.join(folder_path, "batch_000010.png")

augmenter = iaa.SaveDebugImageEveryNBatches(folder_path, 10)
augmenter_pkl = pickle.loads(pickle.dumps(augmenter, protocol=-1))

# save two images via augmenter without pickling
for _ in np.arange(20):
_ = augmenter(image=image)

img11 = imageio.imread(path1)
img12 = imageio.imread(path2)

# reset folder content
os.remove(path1)
os.remove(path2)

# save two images via augmenter that was pickled
for _ in np.arange(20):
_ = augmenter_pkl(image=image)

img21 = imageio.imread(path1)
img22 = imageio.imread(path2)

# compare the two images of original/pickled augmenters
assert np.array_equal(img11, img21)
assert np.array_equal(img12, img22)
4 changes: 4 additions & 0 deletions test/augmenters/test_geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -10082,3 +10082,7 @@ def test_get_parameters(self):
assert params[1] is aug.nb_cols
assert params[2] is aug.max_steps
assert params[3] is True

def test_pickleable(self):
aug = iaa.Jigsaw(nb_rows=(1, 4), nb_cols=(1, 4), max_steps=(1, 3))
runtest_pickleable_uint8_img(aug, iterations=20, shape=(32, 32, 3))
16 changes: 14 additions & 2 deletions test/augmenters/test_imgcorruptlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from imgaug import augmenters as iaa
from imgaug import random as iarandom
from imgaug import parameters as iap
from imgaug.testutils import runtest_pickleable_uint8_img

# imagecorruptions cannot be installed in <=3.4 due to their
# scikit-image requirement
Expand Down Expand Up @@ -245,15 +246,22 @@ class TestAugmenters(unittest.TestCase):
@classmethod
def _test_augmenter(cls, augmenter_name, func_expected,
dependent_on_seed):
# this test verifies:
# - called function seems to be the expected function
# - images produced by augmenter match images produced by function
# - a different seed (and sometimes severity) will lead to a
# different image
# - augmenter can be pickled
severity = 5
aug_cls = getattr(iaa.imgcorruptlike, augmenter_name)
image = np.mod(
np.arange(32*32*3), 256
).reshape((32, 32, 3)).astype(np.uint8)

rng = iarandom.RNG(1)
# replay sampling of severities, not really necessary here as we
# use a deterministic value, but still done for good style
# Replay sampling of severities.
# Even for deterministic values this is required as currently
# there is an advance() at the end of each draw_samples().
_ = iap.Deterministic(1).draw_samples((1,), rng)

# As for the functions above, we can't just change the seed value
Expand All @@ -278,6 +286,10 @@ def _test_augmenter(cls, augmenter_name, func_expected,
assert np.array_equal(image_aug2, image_aug_exp)
assert not np.array_equal(image_aug3, image_aug2)

# pickling test
aug = aug_cls(severity=(1, 5))
runtest_pickleable_uint8_img(aug, shape=(32, 32, 3))

def test_gaussian_noise(self):
self._test_augmenter("GaussianNoise",
iaa.imgcorruptlike.apply_gaussian_noise,
Expand Down
30 changes: 30 additions & 0 deletions test/augmenters/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import unittest.mock as mock
except ImportError:
import mock
try:
import cPickle as pickle
except ImportError:
import pickle

import matplotlib
matplotlib.use('Agg') # fix execution of tests involving matplotlib on travis
Expand Down Expand Up @@ -8681,6 +8685,19 @@ def test_get_parameters(self):
assert len(params) == 1
assert np.isclose(params[0], 0.51)

def test_pickleable(self):
item1 = ia.Keypoint(x=5, y=1)
item2 = ia.Keypoint(x=15, y=1)
cbaoi = ia.KeypointsOnImage([item1, item2], shape=(10, 10, 3))

augmenter = iaa.RemoveCBAsByOutOfImageFraction(0.51)
augmenter_pkl = pickle.loads(pickle.dumps(augmenter, protocol=-1))

for _ in np.arange(3):
cbaoi_aug = augmenter(keypoints=cbaoi)
cbaoi_aug_pkl = augmenter_pkl(keypoints=cbaoi)
assert np.allclose(cbaoi_aug.to_xy_array(), cbaoi_aug_pkl.to_xy_array())


class TestClipCBAsToImagePlanes(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -8774,3 +8791,16 @@ def test_get_parameters(self):
aug = iaa.ClipCBAsToImagePlanes()
params = aug.get_parameters()
assert len(params) == 0

def test_pickleable(self):
item1 = ia.Keypoint(x=5, y=1)
item2 = ia.Keypoint(x=15, y=1)
cbaoi = ia.KeypointsOnImage([item1, item2], shape=(10, 10, 3))

augmenter = iaa.ClipCBAsToImagePlanes()
augmenter_pkl = pickle.loads(pickle.dumps(augmenter, protocol=-1))

for _ in np.arange(3):
cbaoi_aug = augmenter(keypoints=cbaoi)
cbaoi_aug_pkl = augmenter_pkl(keypoints=cbaoi)
assert np.allclose(cbaoi_aug.to_xy_array(), cbaoi_aug_pkl.to_xy_array())
Loading

0 comments on commit 02642e9

Please sign in to comment.