def test_keypoints_p_is_0(self): aug = self.create_aug(0) for _ in sm.xrange(3): observed = aug.augment_keypoints(self.kpsoi) expected = self.kpsoi assert keypoints_equal(observed, expected)
def test_keypoints_p_is_1__deterministic(self): aug = self.create_aug(1.0).to_deterministic() for _ in sm.xrange(3): observed = aug.augment_keypoints(self.kpsoi) expected = self.kpsoi_flipped assert keypoints_equal(observed, expected)
def test_keypoints_p_is_050__deterministic(self): aug = self.create_aug(0.5).to_deterministic() nb_iterations = 10 nb_keypoints_flipped_det = 0 for _ in sm.xrange(nb_iterations): observed = aug.augment_keypoints(self.kpsoi) if keypoints_equal(observed, self.kpsoi_flipped): nb_keypoints_flipped_det += 1 assert nb_keypoints_flipped_det in [0, nb_iterations]
def test_keypoints_p_is_050(self): aug = self.create_aug(0.5) nb_iterations = 1000 nb_keypoints_flipped = 0 for _ in sm.xrange(nb_iterations): observed = aug.augment_keypoints(self.kpsoi) if keypoints_equal(observed, self.kpsoi_flipped): nb_keypoints_flipped += 1 assert np.isclose(nb_keypoints_flipped / nb_iterations, 0.5, rtol=0, atol=0.1)
def test_SigmoidContrast(): reseed() img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] img = np.uint8(img) img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3)) # check basic functionality with per_chanenl on/off (makes no difference due to deterministic # parameters) for per_channel in [False, 0, 0.0, True, 1, 1.0]: for gain, cutoff in itertools.product([5, 10], [0.25, 0.75]): aug = iaa.SigmoidContrast(gain=iap.Deterministic(gain), cutoff=iap.Deterministic(cutoff), per_channel=per_channel) img_aug = aug.augment_image(img) img3d_aug = aug.augment_image(img3d) assert img_aug.dtype.type == np.uint8 assert img3d_aug.dtype.type == np.uint8 assert np.array_equal( img_aug, skimage.exposure.adjust_sigmoid(img, gain=gain, cutoff=cutoff)) assert np.array_equal( img3d_aug, skimage.exposure.adjust_sigmoid(img3d, gain=gain, cutoff=cutoff)) # check that tuple to uniform works # note that gain and cutoff are saved in inverted order in _ContrastFuncWrapper to match # the order of skimage's function aug = iaa.SigmoidContrast(gain=(1, 2), cutoff=(0.25, 0.75)) assert isinstance(aug.params1d[0], iap.Uniform) assert isinstance(aug.params1d[0].a, iap.Deterministic) assert isinstance(aug.params1d[0].b, iap.Deterministic) assert np.allclose(aug.params1d[0].a.value, 0.25) assert np.allclose(aug.params1d[0].b.value, 0.75) assert isinstance(aug.params1d[1], iap.Uniform) assert isinstance(aug.params1d[1].a, iap.Deterministic) assert isinstance(aug.params1d[1].b, iap.Deterministic) assert aug.params1d[1].a.value == 1 assert aug.params1d[1].b.value == 2 # check that list to choice works # note that gain and cutoff are saved in inverted order in _ContrastFuncWrapper to match # the order of skimage's function aug = iaa.SigmoidContrast(gain=[1, 2], cutoff=[0.25, 0.75]) assert isinstance(aug.params1d[0], iap.Choice) assert all([ np.allclose(val, val_choice) for val, val_choice in zip([0.25, 0.75], aug.params1d[0].a) ]) assert isinstance(aug.params1d[1], iap.Choice) assert all([val in aug.params1d[1].a for val in [1, 2]]) # check that per_channel at 50% prob works aug = iaa.SigmoidContrast(gain=(1, 10), cutoff=(0.25, 0.75), per_channel=0.5) seen = [False, False] img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128 for _ in sm.xrange(100): img_aug = aug.augment_image(img1000d) assert img_aug.dtype.type == np.uint8 l = len(set(img_aug.flatten().tolist())) if l == 1: seen[0] = True else: seen[1] = True if all(seen): break assert all(seen) # check that keypoints are not changed kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3)) kpsoi_aug = iaa.SigmoidContrast(gain=10, cutoff=0.5).augment_keypoints([kpsoi]) assert keypoints_equal([kpsoi], kpsoi_aug) # check that heatmaps are not changed heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5, shape=(3, 3, 3)) heatmaps_aug = iaa.SigmoidContrast(gain=10, cutoff=0.5).augment_heatmaps([heatmaps ])[0] assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1) ################### # test other dtypes ################### # uint, int for dtype in [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64 ]: min_value, center_value, max_value = meta.get_value_range_of_dtype( dtype) gains = [5, 20] cutoffs = [0.25, 0.75] values = [0, 100, int(center_value + 0.1 * max_value)] tmax = 1e-8 * max_value if dtype in [np.uint64, np.int64] else 0 tolerances = [tmax, tmax, tmax] for gain, cutoff in itertools.product(gains, cutoffs): aug = iaa.SigmoidContrast(gain=gain, cutoff=cutoff) for value, tolerance in zip(values, tolerances): image = np.full((3, 3), value, dtype=dtype) # 1/(1 + exp(gain*(cutoff - I_ij/max))) expected = (1 / (1 + np.exp( gain * (cutoff - image.astype(np.float128) / max_value)))) expected = (expected * max_value).astype(dtype) image_aug = aug.augment_image(image) assert image_aug.dtype == np.dtype(dtype) assert len(np.unique(image_aug)) == 1 value_aug = int(image_aug[0, 0]) value_expected = int(expected[0, 0]) diff = abs(value_aug - value_expected) assert diff <= tolerance # float for dtype in [np.float16, np.float32, np.float64]: def _allclose(a, b): atol = 1e-3 if dtype == np.float16 else 1e-8 return np.allclose(a, b, atol=atol, rtol=0) gains = [5, 20] cutoffs = [0.25, 0.75] isize = np.dtype(dtype).itemsize values = [0, 1.0, 50.0, 100**(isize - 1)] for gain, cutoff in itertools.product(gains, cutoffs): aug = iaa.SigmoidContrast(gain=gain, cutoff=cutoff) for value in values: image = np.full((3, 3), value, dtype=dtype) expected = (1 / (1 + np.exp(gain * (cutoff - image.astype(np.float128)))) ).astype(dtype) image_aug = aug.augment_image(image) assert image_aug.dtype == np.dtype(dtype) assert _allclose(image_aug, expected)
def test_GammaContrast(): reseed() img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] img = np.uint8(img) img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3)) # check basic functionality with gamma=1 or 2 (deterministic) and per_chanenl on/off (makes # no difference due to deterministic gamma) for per_channel in [False, 0, 0.0, True, 1, 1.0]: for gamma in [1, 2]: aug = iaa.GammaContrast(gamma=iap.Deterministic(gamma), per_channel=per_channel) img_aug = aug.augment_image(img) img3d_aug = aug.augment_image(img3d) assert img_aug.dtype.type == np.uint8 assert img3d_aug.dtype.type == np.uint8 assert np.array_equal( img_aug, skimage.exposure.adjust_gamma(img, gamma=gamma)) assert np.array_equal( img3d_aug, skimage.exposure.adjust_gamma(img3d, gamma=gamma)) # check that tuple to uniform works aug = iaa.GammaContrast((1, 2)) assert isinstance(aug.params1d[0], iap.Uniform) assert isinstance(aug.params1d[0].a, iap.Deterministic) assert isinstance(aug.params1d[0].b, iap.Deterministic) assert aug.params1d[0].a.value == 1 assert aug.params1d[0].b.value == 2 # check that list to choice works aug = iaa.GammaContrast([1, 2]) assert isinstance(aug.params1d[0], iap.Choice) assert all([val in aug.params1d[0].a for val in [1, 2]]) # check that per_channel at 50% prob works aug = iaa.GammaContrast((0.5, 2.0), per_channel=0.5) seen = [False, False] img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128 for _ in sm.xrange(100): img_aug = aug.augment_image(img1000d) assert img_aug.dtype.type == np.uint8 l = len(set(img_aug.flatten().tolist())) if l == 1: seen[0] = True else: seen[1] = True if all(seen): break assert all(seen) # check that keypoints are not changed kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3)) kpsoi_aug = iaa.GammaContrast(gamma=2).augment_keypoints([kpsoi]) assert keypoints_equal([kpsoi], kpsoi_aug) # check that heatmaps are not changed heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5, shape=(3, 3, 3)) heatmaps_aug = iaa.GammaContrast(gamma=2).augment_heatmaps([heatmaps])[0] assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1) ################### # test other dtypes ################### # uint, int for dtype in [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64 ]: min_value, center_value, max_value = meta.get_value_range_of_dtype( dtype) exps = [1, 2, 3] values = [0, 100, int(center_value + 0.1 * max_value)] tolerances = [ 0, 0, 1e-8 * max_value if dtype in [np.uint64, np.int64] else 0 ] for exp in exps: aug = iaa.GammaContrast(exp) for value, tolerance in zip(values, tolerances): image = np.full((3, 3), value, dtype=dtype) expected = (((image.astype(np.float128) / max_value)**exp) * max_value).astype(dtype) image_aug = aug.augment_image(image) assert image_aug.dtype == np.dtype(dtype) assert len(np.unique(image_aug)) == 1 value_aug = int(image_aug[0, 0]) value_expected = int(expected[0, 0]) diff = abs(value_aug - value_expected) assert diff <= tolerance # float for dtype in [np.float16, np.float32, np.float64]: def _allclose(a, b): atol = 1e-3 if dtype == np.float16 else 1e-8 return np.allclose(a, b, atol=atol, rtol=0) exps = [1, 2] isize = np.dtype(dtype).itemsize values = [0, 1.0, 50.0, 100**(isize - 1)] for exp in exps: aug = iaa.GammaContrast(exp) for value in values: image = np.full((3, 3), value, dtype=dtype) expected = (image.astype(np.float128)**exp).astype(dtype) image_aug = aug.augment_image(image) assert image_aug.dtype == np.dtype(dtype) assert _allclose(image_aug, expected)
def test_Alpha(): reseed() base_img = np.zeros((3, 3, 1), dtype=np.uint8) heatmaps_arr = np.float32([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 1.0, 1.0]]) heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3)) aug = iaa.Alpha(1, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 10).astype(np.uint8) assert np.allclose(observed, expected) for per_channel in [False, True]: aug = iaa.Alpha(1, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=per_channel) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == heatmaps.shape assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6 assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_r1) aug = iaa.Alpha(0, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 20).astype(np.uint8) assert np.allclose(observed, expected) for per_channel in [False, True]: aug = iaa.Alpha(0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=per_channel) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == heatmaps.shape assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6 assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_l1) aug = iaa.Alpha(0.75, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8) assert np.allclose(observed, expected) aug = iaa.Alpha(0.75, None, iaa.Add(20)) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype(np.uint8) assert np.allclose(observed, expected) aug = iaa.Alpha(0.75, iaa.Add(10), None) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype(np.uint8) assert np.allclose(observed, expected) base_img = np.zeros((1, 2, 1), dtype=np.uint8) nb_iterations = 1000 aug = iaa.Alpha((0.0, 1.0), iaa.Add(10), iaa.Add(110)) values = [] for _ in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) observed_val = np.round(np.average(observed)) - 10 values.append(observed_val / 100) nb_bins = 5 hist, _ = np.histogram(values, bins=nb_bins, range=(0.0, 1.0), density=False) density_expected = 1.0/nb_bins density_tolerance = 0.05 for nb_samples in hist: density = nb_samples / nb_iterations assert density_expected - density_tolerance < density < density_expected + density_tolerance # bad datatype for factor got_exception = False try: _ = iaa.Alpha(False, iaa.Add(10), None) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # per_channel aug = iaa.Alpha(1.0, iaa.Add((0, 100), per_channel=True), None, per_channel=True) observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8)) uq = np.unique(observed) assert len(uq) > 1 assert np.max(observed) > 80 assert np.min(observed) < 20 aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), None, per_channel=True) observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8)) uq = np.unique(observed) assert len(uq) > 1 assert np.max(observed) > 80 assert np.min(observed) < 20 aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), iaa.Add(0), per_channel=0.5) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_image(np.zeros((1, 1, 100), dtype=np.uint8)) uq = np.unique(observed) if len(uq) == 1: seen[0] += 1 elif len(uq) > 1: seen[1] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 # bad datatype for per_channel got_exception = False try: _ = iaa.Alpha(0.5, iaa.Add(10), None, per_channel="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # propagating aug = iaa.Alpha(0.5, iaa.Add(100), iaa.Add(50), name="AlphaTest") def propagator(images, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksImages(propagator=propagator) image = np.zeros((10, 10, 3), dtype=np.uint8) + 1 observed = aug.augment_image(image, hooks=hooks) assert np.array_equal(observed, image) # ----- # keypoints # ----- kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)] kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3)) aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) # per_channel aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) expected_same = kpsoi.deepcopy() expected_shifted = kpsoi.shift(x=1) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_keypoints([kpsoi])[0] if keypoints_equal([observed], [expected_same]): seen[0] += 1 elif keypoints_equal([observed], [expected_shifted]): seen[1] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 # empty keypoints aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints(ia.KeypointsOnImage([], shape=(1, 2, 3))) assert len(observed.keypoints) == 0 assert observed.shape == (1, 2, 3) # propagating aug = iaa.Alpha(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaTest") def propagator(kpsoi_to_aug, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksKeypoints(propagator=propagator) observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0] assert keypoints_equal([observed], [kpsoi]) # ----- # polygons # ----- ps = [ia.Polygon([(5, 5), (10, 5), (10, 10)])] psoi = ia.PolygonsOnImage(ps, shape=(20, 20, 3)) aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) expected = psoi.shift(left=1) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) expected = psoi.shift(left=1) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0]) assert observed[0].polygons[0].is_valid # per_channel aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_polygons([psoi]) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_polygons([psoi]) expected = psoi.shift(left=1) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) expected_same = psoi.deepcopy() expected_shifted = psoi.shift(left=1) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_polygons([psoi])[0] if observed.polygons[0].exterior_almost_equals(expected_same.polygons[0]): seen[0] += 1 elif observed.polygons[0].exterior_almost_equals(expected_shifted.polygons[0]): seen[1] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 # empty polygons aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons(ia.PolygonsOnImage([], shape=(1, 2, 3))) assert len(observed.polygons) == 0 assert observed.shape == (1, 2, 3) # propagating aug = iaa.Alpha(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaTest") def propagator(psoi_to_aug, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksKeypoints(propagator=propagator) # no hooks for polygons yet, so we use HooksKeypoints observed = aug.augment_polygons([psoi], hooks=hooks)[0] assert observed.polygons[0].exterior_almost_equals(psoi.polygons[0]) # ----- # get_parameters() # ----- first = iaa.Noop() second = iaa.Sequential([iaa.Add(1)]) aug = iaa.Alpha(0.65, first, second, per_channel=1) params = aug.get_parameters() assert isinstance(params[0], iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.65 - 1e-6 < params[0].value < 0.65 + 1e-6 assert params[1].value == 1 # ----- # get_children_lists() # ----- first = iaa.Noop() second = iaa.Sequential([iaa.Add(1)]) aug = iaa.Alpha(0.65, first, second, per_channel=1) children_lsts = aug.get_children_lists() assert len(children_lsts) == 2 assert ia.is_iterable([lst for lst in children_lsts]) assert first in children_lsts[0] assert second == children_lsts[1]
def test_AlphaElementwise(): reseed() base_img = np.zeros((3, 3, 1), dtype=np.uint8) heatmaps_arr = np.float32([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 1.0, 1.0]]) heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3)) aug = iaa.AlphaElementwise(1, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = base_img + 10 assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(1, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1})) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_r1) aug = iaa.AlphaElementwise(0, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = base_img + 20 assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1})) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_l1) aug = iaa.AlphaElementwise(0.75, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8) assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(0.75, None, iaa.Add(20)) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype(np.uint8) assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(0.75, iaa.Add(10), None) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype(np.uint8) assert np.allclose(observed, expected) base_img = np.zeros((100, 100), dtype=np.uint8) aug = iaa.AlphaElementwise((0.0, 1.0), iaa.Add(10), iaa.Add(110)) observed = (aug.augment_image(base_img) - 10) / 100 nb_bins = 10 hist, _ = np.histogram(observed.flatten(), bins=nb_bins, range=(0.0, 1.0), density=False) density_expected = 1.0/nb_bins density_tolerance = 0.05 for nb_samples in hist: density = nb_samples / observed.size assert density_expected - density_tolerance < density < density_expected + density_tolerance base_img = np.zeros((1, 1, 100), dtype=np.uint8) aug = iaa.AlphaElementwise((0.0, 1.0), iaa.Add(10), iaa.Add(110), per_channel=True) observed = aug.augment_image(base_img) assert len(set(observed.flatten())) > 1 # propagating aug = iaa.AlphaElementwise(0.5, iaa.Add(100), iaa.Add(50), name="AlphaElementwiseTest") def propagator(images, augmenter, parents, default): if "AlphaElementwise" in augmenter.name: return False else: return default hooks = ia.HooksImages(propagator=propagator) image = np.zeros((10, 10, 3), dtype=np.uint8) + 1 observed = aug.augment_image(image, hooks=hooks) assert np.array_equal(observed, image) # ----- # heatmaps and per_channel # ----- class _DummyMaskParameter(iap.StochasticParameter): def __init__(self, inverted=False): super(_DummyMaskParameter, self).__init__() self.nb_calls = 0 self.inverted = inverted def _draw_samples(self, size, random_state): self.nb_calls += 1 h, w = size ones = np.ones((h, w), dtype=np.float32) zeros = np.zeros((h, w), dtype=np.float32) if self.nb_calls == 1: return zeros if not self.inverted else ones elif self.nb_calls in [2, 3]: return ones if not self.inverted else zeros else: assert False aug = iaa.AlphaElementwise( _DummyMaskParameter(inverted=False), iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=True ) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_r1) aug = iaa.AlphaElementwise( _DummyMaskParameter(inverted=True), iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=True ) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_l1) # ----- # keypoints # ----- kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)] kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3)) aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) # per_channel aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) """ TODO this test currently doesn't work as AlphaElementwise augments keypoints without sampling overlay factors per (x, y) location. (i.e. similar behaviour to Alpha) aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) expected_same = kpsoi.deepcopy() expected_both_shifted = kpsoi.shift(x=1) expected_first_shifted = KeypointsOnImage([kps[0].shift(x=1), kps[1]], shape=kpsoi.shape) expected_second_shifted = KeypointsOnImage([kps[0], kps[1].shift(x=1)], shape=kpsoi.shape) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_keypoints([kpsoi])[0] if keypoints_equal([observed], [expected_same]): seen[0] += 1 elif keypoints_equal([observed], [expected_both_shifted]): seen[1] += 1 elif keypoints_equal([observed], [expected_first_shifted]): seen[2] += 1 elif keypoints_equal([observed], [expected_second_shifted]): seen[3] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 """ # propagating aug = iaa.AlphaElementwise(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaElementwiseTest") def propagator(kpsoi_to_aug, augmenter, parents, default): if "AlphaElementwise" in augmenter.name: return False else: return default hooks = ia.HooksKeypoints(propagator=propagator) observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0] assert keypoints_equal([observed], [kpsoi]) # ----- # polygons # ----- ps = [ia.Polygon([(5, 5), (10, 5), (10, 10)])] psoi = ia.PolygonsOnImage(ps, shape=(20, 20, 3)) aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.AlphaElementwise(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) expected = psoi.shift(left=1) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.AlphaElementwise(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons([psoi]) expected = psoi.shift(left=1) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0]) assert observed[0].polygons[0].is_valid # per_channel aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_polygons([psoi]) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_polygons([psoi]) expected = psoi.shift(left=1) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == psoi.shape assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0]) assert observed[0].polygons[0].is_valid aug = iaa.AlphaElementwise(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) expected_same = psoi.deepcopy() expected_shifted = psoi.shift(left=1) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_polygons([psoi])[0] if observed.polygons[0].exterior_almost_equals(expected_same.polygons[0]): seen[0] += 1 elif observed.polygons[0].exterior_almost_equals(expected_shifted.polygons[0]): seen[1] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 # empty polygons aug = iaa.AlphaElementwise(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_polygons(ia.PolygonsOnImage([], shape=(1, 2, 3))) assert len(observed.polygons) == 0 assert observed.shape == (1, 2, 3) # propagating aug = iaa.AlphaElementwise(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaTest") def propagator(psoi_to_aug, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksKeypoints(propagator=propagator) # no hooks for polygons yet, so we use HooksKeypoints observed = aug.augment_polygons([psoi], hooks=hooks)[0] assert observed.polygons[0].exterior_almost_equals(psoi.polygons[0])
def test_AverageBlur(): reseed() base_img = np.zeros((11, 11, 1), dtype=np.uint8) base_img[5, 5, 0] = 200 base_img[4, 5, 0] = 100 base_img[6, 5, 0] = 100 base_img[5, 4, 0] = 100 base_img[5, 6, 0] = 100 blur3x3 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0], [0, 0, 0, 11, 44, 56, 44, 11, 0, 0, 0], [0, 0, 0, 11, 56, 67, 56, 11, 0, 0, 0], [0, 0, 0, 11, 44, 56, 44, 11, 0, 0, 0], [0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] blur3x3 = np.array(blur3x3, dtype=np.uint8)[..., np.newaxis] blur4x4 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 6, 6, 6, 6, 0, 0, 0], [0, 0, 0, 6, 25, 31, 31, 25, 6, 0, 0], [0, 0, 0, 6, 31, 38, 38, 31, 6, 0, 0], [0, 0, 0, 6, 31, 38, 38, 31, 6, 0, 0], [0, 0, 0, 6, 25, 31, 31, 25, 6, 0, 0], [0, 0, 0, 0, 6, 6, 6, 6, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] blur4x4 = np.array(blur4x4, dtype=np.uint8)[..., np.newaxis] blur5x5 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0], [0, 0, 4, 16, 20, 20, 20, 16, 4, 0, 0], [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0], [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0], [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0], [0, 0, 4, 16, 20, 20, 20, 16, 4, 0, 0], [0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] blur5x5 = np.array(blur5x5, dtype=np.uint8)[..., np.newaxis] keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2) ], shape=base_img.shape) ] # no blur, shouldnt change anything aug = iaa.AverageBlur(k=0) observed = aug.augment_image(base_img) assert np.array_equal(observed, base_img) # k=3 aug = iaa.AverageBlur(k=3) observed = aug.augment_image(base_img) assert np.array_equal(observed, blur3x3) # k=5 aug = iaa.AverageBlur(k=5) observed = aug.augment_image(base_img) assert np.array_equal(observed, blur5x5) # k as (3, 4) aug = iaa.AverageBlur(k=(3, 4)) nb_iterations = 100 nb_seen = [0, 0] for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): nb_seen[0] += 1 elif np.array_equal(observed, blur4x4): nb_seen[1] += 1 else: raise Exception("Unexpected result in AverageBlur@1") p_seen = [v / nb_iterations for v in nb_seen] assert 0.4 <= p_seen[0] <= 0.6 assert 0.4 <= p_seen[1] <= 0.6 # k as (3, 5) aug = iaa.AverageBlur(k=(3, 5)) nb_iterations = 100 nb_seen = [0, 0, 0] for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): nb_seen[0] += 1 elif np.array_equal(observed, blur4x4): nb_seen[1] += 1 elif np.array_equal(observed, blur5x5): nb_seen[2] += 1 else: raise Exception("Unexpected result in AverageBlur@2") p_seen = [v / nb_iterations for v in nb_seen] assert 0.23 <= p_seen[0] <= 0.43 assert 0.23 <= p_seen[1] <= 0.43 assert 0.23 <= p_seen[2] <= 0.43 # k as stochastic parameter aug = iaa.AverageBlur(k=iap.Choice([3, 5])) nb_iterations = 100 nb_seen = [0, 0] for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): nb_seen[0] += 1 elif np.array_equal(observed, blur5x5): nb_seen[1] += 1 else: raise Exception("Unexpected result in AverageBlur@3") p_seen = [v / nb_iterations for v in nb_seen] assert 0.4 <= p_seen[0] <= 0.6 assert 0.4 <= p_seen[1] <= 0.6 # k as ((3, 5), (3, 5)) aug = iaa.AverageBlur(k=((3, 5), (3, 5))) possible = dict() for kh in [3, 4, 5]: for kw in [3, 4, 5]: key = (kh, kw) if kh == 0 or kw == 0: possible[key] = np.copy(base_img) else: possible[key] = cv2.blur(base_img, (kh, kw))[..., np.newaxis] nb_iterations = 250 nb_seen = dict([(key, 0) for key, val in possible.items()]) for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) for key, img_aug in possible.items(): if np.array_equal(observed, img_aug): nb_seen[key] += 1 # dont check sum here, because 0xX and Xx0 are all the same, i.e. much # higher sum than nb_iterations assert all([v > 0 for v in nb_seen.values()]) # keypoints shouldnt be changed aug = iaa.AverageBlur(k=3) aug_det = aug.to_deterministic() observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) ############################# # test other dtypes below ############################# # -- # blur of various dtypes at k=0 # -- aug = iaa.AverageBlur(k=0) # bool image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image[2, 2] = True image_aug = aug.augment_image(image) assert image_aug.dtype.type == np.bool_ assert np.all(image_aug == image) # uint, int for dtype in [np.uint8, np.uint16, np.int8, np.int16]: _min_value, center_value, max_value = iadt.get_value_range_of_dtype( dtype) image = np.zeros((3, 3), dtype=dtype) image[1, 1] = int(center_value + 0.4 * max_value) image[2, 2] = int(center_value + 0.4 * max_value) image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.all(image_aug == image) # float for dtype, value in zip([np.float16, np.float32, np.float64], [5000, 1000 * 1000, 1000 * 1000 * 1000]): image = np.zeros((3, 3), dtype=dtype) image[1, 1] = value image[2, 2] = value image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.allclose(image_aug, image) # -- # blur of various dtypes at k=3 # and using an example value of 100 for int/uint/float and True for bool # -- aug = iaa.AverageBlur(k=3) # prototype mask # we place values in a 3x3 grid at positions (row=1, col=1) and (row=2, col=2) (beginning with 0) # AverageBlur uses cv2.blur(), which uses BORDER_REFLECT_101 as its default padding mode, # see https://docs.opencv.org/3.1.0/d2/de8/group__core__array.html # the matrix below shows the 3x3 grid and the padded row/col values around it # [1, 0, 1, 0, 1] # [0, 0, 0, 0, 0] # [1, 0, 1, 0, 1] # [0, 0, 0, 1, 0] # [1, 0, 1, 0, 1] mask = np.float64([[4 / 9, 2 / 9, 4 / 9], [2 / 9, 2 / 9, 3 / 9], [4 / 9, 3 / 9, 5 / 9]]) # bool image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image[2, 2] = True image_aug = aug.augment_image(image) expected = mask > 0.5 assert image_aug.dtype.type == np.bool_ assert np.all(image_aug == expected) # uint, int for dtype in [np.uint8, np.uint16, np.int8, np.int16]: image = np.zeros((3, 3), dtype=dtype) image[1, 1] = 100 image[2, 2] = 100 image_aug = aug.augment_image(image) expected = np.round(mask * 100).astype( dtype) # cv2.blur() applies rounding for int/uint dtypes diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64)) assert image_aug.dtype.type == dtype assert np.max(diff) <= 2 # float for dtype in [np.float16, np.float32, np.float64]: image = np.zeros((3, 3), dtype=dtype) image[1, 1] = 100.0 image[2, 2] = 100.0 image_aug = aug.augment_image(image) expected = (mask * 100.0).astype(dtype) diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.dtype.type == dtype assert np.max(diff) < 1.0 # -- # blur of various dtypes at k=3 # and values being half-way between center and maximum for each dtype (bool is skipped as it doesnt make any # sense here) # The goal of this test is to verify that no major loss of resolution happens for large dtypes. # -- aug = iaa.AverageBlur(k=3) # prototype mask (see above) mask = np.float64([[4 / 9, 2 / 9, 4 / 9], [2 / 9, 2 / 9, 3 / 9], [4 / 9, 3 / 9, 5 / 9]]) # uint, int for dtype in [np.uint8, np.uint16, np.int8, np.int16]: _min_value, center_value, max_value = iadt.get_value_range_of_dtype( dtype) value = int(center_value + 0.4 * max_value) image = np.zeros((3, 3), dtype=dtype) image[1, 1] = value image[2, 2] = value image_aug = aug.augment_image(image) expected = (mask * value).astype(dtype) diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64)) assert image_aug.dtype.type == dtype # accepts difference of 4, 8, 16 (at 1, 2, 4 bytes, i.e. 8, 16, 32 bit) assert np.max(diff) <= 2**(1 + np.dtype(dtype).itemsize) # float for dtype, value in zip([np.float16, np.float32, np.float64], [5000, 1000 * 1000, 1000 * 1000 * 1000]): image = np.zeros((3, 3), dtype=dtype) image[1, 1] = value image[2, 2] = value image_aug = aug.augment_image(image) expected = (mask * value).astype(dtype) diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.dtype.type == dtype # accepts difference of 2.0, 4.0, 8.0, 16.0 (at 1, 2, 4, 8 bytes, i.e. 8, 16, 32, 64 bit) assert np.max(diff) < 2**(1 + np.dtype(dtype).itemsize) # assert failure on invalid dtypes aug = iaa.AverageBlur(k=3) for dt in [np.uint32, np.uint64, np.int32, np.int64]: got_exception = False try: _ = aug.augment_image(np.zeros((1, 1), dtype=dt)) except Exception as exc: assert "forbidden dtype" in str(exc) got_exception = True assert got_exception
def test_determinism(): reseed() images = [ ia.quokka(size=(128, 128)), ia.quokka(size=(64, 64)), ia.imresize_single_image(skimage.data.astronaut(), (128, 256)) ] images.extend([ia.quokka(size=(16, 16))] * 20) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=20, y=10, vis=None, label=None), ia.Keypoint(x=5, y=5, vis=None, label=None), ia.Keypoint(x=10, y=43, vis=None, label=None)], shape=(50, 60, 3)) ] * 20 augs = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))), # iaa.WithChannels([0], iaa.Add((-50, 50))), # iaa.Noop(name="Noop-nochange"), # iaa.Lambda( # func_images=lambda images, random_state, parents, hooks: images, # func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: keypoints_on_images, # name="Lambda-nochange" # ), # iaa.AssertLambda( # func_images=lambda images, random_state, parents, hooks: True, # func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: True, # name="AssertLambda-nochange" # ), # iaa.AssertShape( # (None, None, None, 3), # check_keypoints=False, # name="AssertShape-nochange" # ), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)), # iaa.ChangeColorspace(to_colorspace="GRAY"), iaa.Grayscale(alpha=(0.1, 1.0)), iaa.GaussianBlur((0.1, 3.0)), iaa.AverageBlur((3, 11)), iaa.MedianBlur((3, 11)), # iaa.Convolve(np.array([[0, 1, 0], # [1, -4, 1], # [0, 1, 0]])), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)), iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)), iaa.EdgeDetect(alpha=(0.1, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)), iaa.Add((-50, 50)), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)), iaa.Multiply((0.6, 1.4)), iaa.MultiplyElementwise((0.6, 1.4)), iaa.Dropout((0.3, 0.5)), iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)), iaa.Invert(0.5), iaa.ContrastNormalization((0.6, 1.4)), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=0.5) ] augs_affect_geometry = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5)) ] for aug in augs: aug_det = aug.to_deterministic() images_aug1 = aug_det.augment_images(images) images_aug2 = aug_det.augment_images(images) aug_det = aug.to_deterministic() images_aug3 = aug_det.augment_images(images) images_aug4 = aug_det.augment_images(images) assert array_equal_lists(images_aug1, images_aug2), \ "Images (1, 2) expected to be identical for %s" % (aug.name,) assert array_equal_lists(images_aug3, images_aug4), \ "Images (3, 4) expected to be identical for %s" % (aug.name,) assert not array_equal_lists(images_aug1, images_aug3), \ "Images (1, 3) expected to be different for %s" % (aug.name,) for aug in augs_affect_geometry: aug_det = aug.to_deterministic() kps_aug1 = aug_det.augment_keypoints(keypoints) kps_aug2 = aug_det.augment_keypoints(keypoints) aug_det = aug.to_deterministic() kps_aug3 = aug_det.augment_keypoints(keypoints) kps_aug4 = aug_det.augment_keypoints(keypoints) assert keypoints_equal(kps_aug1, kps_aug2), \ "Keypoints (1, 2) expected to be identical for %s" % (aug.name,) assert keypoints_equal(kps_aug3, kps_aug4), \ "Keypoints (3, 4) expected to be identical for %s" % (aug.name,) assert not keypoints_equal(kps_aug1, kps_aug3), \ "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
def test_AlphaElementwise(): reseed() base_img = np.zeros((3, 3, 1), dtype=np.uint8) heatmaps_arr = np.float32([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 1.0, 1.0]]) heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3)) aug = iaa.AlphaElementwise(1, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = base_img + 10 assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(1, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1})) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_r1) aug = iaa.AlphaElementwise(0, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = base_img + 20 assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1})) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_l1) aug = iaa.AlphaElementwise(0.75, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8) assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(0.75, None, iaa.Add(20)) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype( np.uint8) assert np.allclose(observed, expected) aug = iaa.AlphaElementwise(0.75, iaa.Add(10), None) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype( np.uint8) assert np.allclose(observed, expected) base_img = np.zeros((100, 100), dtype=np.uint8) aug = iaa.AlphaElementwise((0.0, 1.0), iaa.Add(10), iaa.Add(110)) observed = (aug.augment_image(base_img) - 10) / 100 nb_bins = 10 hist, _ = np.histogram(observed.flatten(), bins=nb_bins, range=(0.0, 1.0), density=False) density_expected = 1.0 / nb_bins density_tolerance = 0.05 for nb_samples in hist: density = nb_samples / observed.size assert density_expected - density_tolerance < density < density_expected + density_tolerance base_img = np.zeros((1, 1, 100), dtype=np.uint8) aug = iaa.AlphaElementwise((0.0, 1.0), iaa.Add(10), iaa.Add(110), per_channel=True) observed = aug.augment_image(base_img) assert len(set(observed.flatten())) > 1 # propagating aug = iaa.AlphaElementwise(0.5, iaa.Add(100), iaa.Add(50), name="AlphaElementwiseTest") def propagator(images, augmenter, parents, default): if "AlphaElementwise" in augmenter.name: return False else: return default hooks = ia.HooksImages(propagator=propagator) image = np.zeros((10, 10, 3), dtype=np.uint8) + 1 observed = aug.augment_image(image, hooks=hooks) assert np.array_equal(observed, image) # ----- # heatmaps and per_channel # ----- class _DummyMaskParameter(iap.StochasticParameter): def __init__(self, inverted=False): super(_DummyMaskParameter, self).__init__() self.nb_calls = 0 self.inverted = inverted def _draw_samples(self, size, random_state): self.nb_calls += 1 h, w = size ones = np.ones((h, w), dtype=np.float32) zeros = np.zeros((h, w), dtype=np.float32) if self.nb_calls == 1: return zeros if not self.inverted else ones elif self.nb_calls in [2, 3]: return ones if not self.inverted else zeros else: assert False aug = iaa.AlphaElementwise(_DummyMaskParameter(inverted=False), iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=True) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_r1) aug = iaa.AlphaElementwise(_DummyMaskParameter(inverted=True), iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=True) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == (3, 3, 3) assert 0 - 1e-6 < observed.min_value < 0 + 1e-6 assert 1 - 1e-6 < observed.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_l1) # ----- # keypoints # ----- kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)] kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3)) aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) # per_channel aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) """ TODO this test currently doesn't work as AlphaElementwise augments keypoints without sampling overlay factors per (x, y) location. (i.e. similar behaviour to Alpha) aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) expected_same = kpsoi.deepcopy() expected_both_shifted = kpsoi.shift(x=1) expected_first_shifted = KeypointsOnImage([kps[0].shift(x=1), kps[1]], shape=kpsoi.shape) expected_second_shifted = KeypointsOnImage([kps[0], kps[1].shift(x=1)], shape=kpsoi.shape) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_keypoints([kpsoi])[0] if keypoints_equal([observed], [expected_same]): seen[0] += 1 elif keypoints_equal([observed], [expected_both_shifted]): seen[1] += 1 elif keypoints_equal([observed], [expected_first_shifted]): seen[2] += 1 elif keypoints_equal([observed], [expected_second_shifted]): seen[3] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 """ # propagating aug = iaa.AlphaElementwise(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaElementwiseTest") def propagator(kpsoi_to_aug, augmenter, parents, default): if "AlphaElementwise" in augmenter.name: return False else: return default hooks = ia.HooksKeypoints(propagator=propagator) observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0] assert keypoints_equal([observed], [kpsoi])
def test_determinism(): reseed() images = [ ia.quokka(size=(128, 128)), ia.quokka(size=(64, 64)), ia.quokka((128, 256)) ] images.extend([ia.quokka(size=(16, 16))] * 20) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=20, y=10), ia.Keypoint(x=5, y=5), ia.Keypoint(x=10, y=43)], shape=(50, 60, 3)) ] * 20 augs = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)), iaa.Grayscale(alpha=(0.1, 1.0)), iaa.GaussianBlur((0.1, 3.0)), iaa.AverageBlur((3, 11)), iaa.MedianBlur((3, 11)), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)), iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)), iaa.EdgeDetect(alpha=(0.1, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)), iaa.Add((-50, 50)), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)), iaa.Multiply((0.6, 1.4)), iaa.MultiplyElementwise((0.6, 1.4)), iaa.Dropout((0.3, 0.5)), iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)), iaa.Invert(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=10.0) ] augs_affect_geometry = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5)) ] for aug in augs: aug_det = aug.to_deterministic() images_aug1 = aug_det.augment_images(images) images_aug2 = aug_det.augment_images(images) aug_det = aug.to_deterministic() images_aug3 = aug_det.augment_images(images) images_aug4 = aug_det.augment_images(images) assert array_equal_lists(images_aug1, images_aug2), \ "Images (1, 2) expected to be identical for %s" % (aug.name,) assert array_equal_lists(images_aug3, images_aug4), \ "Images (3, 4) expected to be identical for %s" % (aug.name,) assert not array_equal_lists(images_aug1, images_aug3), \ "Images (1, 3) expected to be different for %s" % (aug.name,) for aug in augs_affect_geometry: aug_det = aug.to_deterministic() kps_aug1 = aug_det.augment_keypoints(keypoints) kps_aug2 = aug_det.augment_keypoints(keypoints) aug_det = aug.to_deterministic() kps_aug3 = aug_det.augment_keypoints(keypoints) kps_aug4 = aug_det.augment_keypoints(keypoints) assert keypoints_equal(kps_aug1, kps_aug2), \ "Keypoints (1, 2) expected to be identical for %s" % (aug.name,) assert keypoints_equal(kps_aug3, kps_aug4), \ "Keypoints (3, 4) expected to be identical for %s" % (aug.name,) assert not keypoints_equal(kps_aug1, kps_aug3), \ "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
def test_LinearContrast(): reseed() img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] img = np.uint8(img) img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3)) # check basic functionality with alpha=1 or 2 (deterministic) and per_chanenl on/off (makes # no difference due to deterministic alpha) for per_channel in [False, 0, 0.0, True, 1, 1.0]: for alpha in [1, 2]: aug = iaa.LinearContrast(alpha=iap.Deterministic(alpha), per_channel=per_channel) img_aug = aug.augment_image(img) img3d_aug = aug.augment_image(img3d) assert img_aug.dtype.type == np.uint8 assert img3d_aug.dtype.type == np.uint8 assert np.array_equal( img_aug, contrast_lib._adjust_linear(img, alpha=alpha)) assert np.array_equal( img3d_aug, contrast_lib._adjust_linear(img3d, alpha=alpha)) # check that tuple to uniform works aug = iaa.LinearContrast((1, 2)) assert isinstance(aug.params1d[0], iap.Uniform) assert isinstance(aug.params1d[0].a, iap.Deterministic) assert isinstance(aug.params1d[0].b, iap.Deterministic) assert aug.params1d[0].a.value == 1 assert aug.params1d[0].b.value == 2 # check that list to choice works aug = iaa.LinearContrast([1, 2]) assert isinstance(aug.params1d[0], iap.Choice) assert all([val in aug.params1d[0].a for val in [1, 2]]) # check that per_channel at 50% prob works aug = iaa.LinearContrast((0.5, 2.0), per_channel=0.5) seen = [False, False] # must not use just value 128 here, otherwise nothing will change as all values would have # distance 0 to 128 img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128 + 20 for _ in sm.xrange(100): img_aug = aug.augment_image(img1000d) assert img_aug.dtype.type == np.uint8 l = len(set(img_aug.flatten().tolist())) if l == 1: seen[0] = True else: seen[1] = True if all(seen): break assert all(seen) # check that keypoints are not changed kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3)) kpsoi_aug = iaa.LinearContrast(alpha=2).augment_keypoints([kpsoi]) assert keypoints_equal([kpsoi], kpsoi_aug) # check that heatmaps are not changed heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5, shape=(3, 3, 3)) heatmaps_aug = iaa.LinearContrast(alpha=2).augment_heatmaps([heatmaps])[0] assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)
def test_Fliplr(): reseed() base_img = np.array([[0, 0, 1], [0, 0, 1], [0, 1, 1]], dtype=np.uint8) base_img = base_img[:, :, np.newaxis] base_img_flipped = np.array([[1, 0, 0], [1, 0, 0], [1, 1, 0]], dtype=np.uint8) base_img_flipped = base_img_flipped[:, :, np.newaxis] images = np.array([base_img]) images_flipped = np.array([base_img_flipped]) keypoints = [ia.KeypointsOnImage([ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2)], shape=base_img.shape)] keypoints_flipped = [ia.KeypointsOnImage([ia.Keypoint(x=2, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=0, y=2)], shape=base_img.shape)] polygons = [ia.PolygonsOnImage( [ia.Polygon([(0, 0), (2, 0), (2, 2)])], shape=base_img.shape)] polygons_flipped = [ia.PolygonsOnImage( [ia.Polygon([(2, 0), (0, 0), (0, 2)])], shape=base_img.shape)] # 0% chance of flip aug = iaa.Fliplr(0) aug_det = aug.to_deterministic() for _ in sm.xrange(10): observed = aug.augment_images(images) expected = images assert np.array_equal(observed, expected) observed = aug_det.augment_images(images) expected = images assert np.array_equal(observed, expected) observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) for aug_ in [aug, aug_det]: observed = aug_.augment_polygons(polygons) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == polygons[0].shape assert observed[0].polygons[0].exterior_almost_equals( polygons[0].polygons[0]) assert observed[0].polygons[0].is_valid # 0% chance of flip, heatmaps aug = iaa.Fliplr(0) heatmaps = ia.HeatmapsOnImage( np.float32([ [0, 0.5, 0.75], [0, 0.5, 0.75], [0.75, 0.75, 0.75], ]), shape=(3, 3, 3) ) observed = aug.augment_heatmaps([heatmaps])[0] expected = heatmaps.get_arr() assert observed.shape == heatmaps.shape assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6 assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6 assert np.array_equal(observed.get_arr(), expected) # 100% chance of flip aug = iaa.Fliplr(1.0) aug_det = aug.to_deterministic() for _ in sm.xrange(10): observed = aug.augment_images(images) expected = images_flipped assert np.array_equal(observed, expected) observed = aug_det.augment_images(images) expected = images_flipped assert np.array_equal(observed, expected) observed = aug.augment_keypoints(keypoints) expected = keypoints_flipped assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints_flipped assert keypoints_equal(observed, expected) for aug_ in [aug, aug_det]: observed = aug_.augment_polygons(polygons) assert len(observed) == 1 assert len(observed[0].polygons) == 1 assert observed[0].shape == polygons[0].shape assert observed[0].polygons[0].exterior_almost_equals( polygons_flipped[0].polygons[0]) assert observed[0].polygons[0].is_valid # 100% chance of flip, heatmaps aug = iaa.Fliplr(1.0) heatmaps = ia.HeatmapsOnImage( np.float32([ [0, 0.5, 0.75], [0, 0.5, 0.75], [0.75, 0.75, 0.75], ]), shape=(3, 3, 3) ) observed = aug.augment_heatmaps([heatmaps])[0] expected = np.fliplr(heatmaps.get_arr()) assert observed.shape == heatmaps.shape assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6 assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6 assert np.array_equal(observed.get_arr(), expected) # 50% chance of flip aug = iaa.Fliplr(0.5) aug_det = aug.to_deterministic() nb_iterations = 1000 nb_images_flipped = 0 nb_images_flipped_det = 0 nb_keypoints_flipped = 0 nb_keypoints_flipped_det = 0 nb_polygons_flipped = 0 nb_polygons_flipped_det = 0 for _ in sm.xrange(nb_iterations): observed = aug.augment_images(images) if np.array_equal(observed, images_flipped): nb_images_flipped += 1 observed = aug_det.augment_images(images) if np.array_equal(observed, images_flipped): nb_images_flipped_det += 1 observed = aug.augment_keypoints(keypoints) if keypoints_equal(observed, keypoints_flipped): nb_keypoints_flipped += 1 observed = aug_det.augment_keypoints(keypoints) if keypoints_equal(observed, keypoints_flipped): nb_keypoints_flipped_det += 1 observed = aug.augment_polygons(polygons) if observed[0].polygons[0].exterior_almost_equals( polygons_flipped[0].polygons[0]): nb_polygons_flipped += 1 observed = aug_det.augment_polygons(polygons) if observed[0].polygons[0].exterior_almost_equals( polygons_flipped[0].polygons[0]): nb_polygons_flipped_det += 1 assert int(nb_iterations * 0.3) <= nb_images_flipped <= int(nb_iterations * 0.7) assert int(nb_iterations * 0.3) <= nb_keypoints_flipped <= int(nb_iterations * 0.7) assert int(nb_iterations * 0.3) <= nb_polygons_flipped <= int(nb_iterations * 0.7) assert nb_images_flipped_det in [0, nb_iterations] assert nb_keypoints_flipped_det in [0, nb_iterations] assert nb_polygons_flipped_det in [0, nb_iterations] # 50% chance of flipped, multiple images, list as input images_multi = [base_img, base_img] aug = iaa.Fliplr(0.5) aug_det = aug.to_deterministic() nb_iterations = 1000 nb_flipped_by_pos = [0] * len(images_multi) nb_flipped_by_pos_det = [0] * len(images_multi) for _ in sm.xrange(nb_iterations): observed = aug.augment_images(images_multi) for i in sm.xrange(len(images_multi)): if np.array_equal(observed[i], base_img_flipped): nb_flipped_by_pos[i] += 1 observed = aug_det.augment_images(images_multi) for i in sm.xrange(len(images_multi)): if np.array_equal(observed[i], base_img_flipped): nb_flipped_by_pos_det[i] += 1 for val in nb_flipped_by_pos: assert int(nb_iterations * 0.3) <= val <= int(nb_iterations * 0.7) for val in nb_flipped_by_pos_det: assert val in [0, nb_iterations] # test StochasticParameter as p aug = iaa.Fliplr(p=iap.Choice([0, 1], p=[0.7, 0.3])) seen = [0, 0] for _ in sm.xrange(1000): observed = aug.augment_image(base_img) if np.array_equal(observed, base_img): seen[0] += 1 elif np.array_equal(observed, base_img_flipped): seen[1] += 1 else: assert False assert 700 - 75 < seen[0] < 700 + 75 assert 300 - 75 < seen[1] < 300 + 75 # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Fliplr(p="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Fliplr(p=0.5) params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 ################### # test other dtypes ################### aug = iaa.Fliplr(1.0) image = np.zeros((3, 3), dtype=bool) image[0, 0] = True expected = np.zeros((3, 3), dtype=bool) expected[0, 2] = True image_aug = aug.augment_image(image) assert image_aug.dtype.type == image.dtype.type assert np.all(image_aug == expected) for dtype in [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int32, np.int64]: min_value, center_value, max_value = iadt.get_value_range_of_dtype(dtype) value = max_value image = np.zeros((3, 3), dtype=dtype) image[0, 0] = value expected = np.zeros((3, 3), dtype=dtype) expected[0, 2] = value image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.array_equal(image_aug, expected) for dtype, value in zip([np.float16, np.float32, np.float64, np.float128], [5000, 1000**2, 1000**3, 1000**4]): atol = 1e-9 * max_value if dtype != np.float16 else 1e-3 * max_value image = np.zeros((3, 3), dtype=dtype) image[0, 0] = value expected = np.zeros((3, 3), dtype=dtype) expected[0, 2] = value image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.allclose(image_aug, expected, atol=atol)
def test_AverageBlur(): reseed() base_img = np.zeros((11, 11, 1), dtype=np.uint8) base_img[5, 5, 0] = 200 base_img[4, 5, 0] = 100 base_img[6, 5, 0] = 100 base_img[5, 4, 0] = 100 base_img[5, 6, 0] = 100 blur3x3 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0], [0, 0, 0, 11, 44, 56, 44, 11, 0, 0, 0], [0, 0, 0, 11, 56, 67, 56, 11, 0, 0, 0], [0, 0, 0, 11, 44, 56, 44, 11, 0, 0, 0], [0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] blur3x3 = np.array(blur3x3, dtype=np.uint8)[..., np.newaxis] blur4x4 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 6, 6, 6, 6, 0, 0, 0], [0, 0, 0, 6, 25, 31, 31, 25, 6, 0, 0], [0, 0, 0, 6, 31, 38, 38, 31, 6, 0, 0], [0, 0, 0, 6, 31, 38, 38, 31, 6, 0, 0], [0, 0, 0, 6, 25, 31, 31, 25, 6, 0, 0], [0, 0, 0, 0, 6, 6, 6, 6, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] blur4x4 = np.array(blur4x4, dtype=np.uint8)[..., np.newaxis] blur5x5 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0], [0, 0, 4, 16, 20, 20, 20, 16, 4, 0, 0], [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0], [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0], [0, 0, 4, 20, 24, 24, 24, 20, 4, 0, 0], [0, 0, 4, 16, 20, 20, 20, 16, 4, 0, 0], [0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] blur5x5 = np.array(blur5x5, dtype=np.uint8)[..., np.newaxis] keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2) ], shape=base_img.shape) ] # no blur, shouldnt change anything aug = iaa.AverageBlur(k=0) observed = aug.augment_image(base_img) assert np.array_equal(observed, base_img) # k=3 aug = iaa.AverageBlur(k=3) observed = aug.augment_image(base_img) assert np.array_equal(observed, blur3x3) # k=5 aug = iaa.AverageBlur(k=5) observed = aug.augment_image(base_img) assert np.array_equal(observed, blur5x5) # k as (3, 4) aug = iaa.AverageBlur(k=(3, 4)) nb_iterations = 100 nb_seen = [0, 0] for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): nb_seen[0] += 1 elif np.array_equal(observed, blur4x4): nb_seen[1] += 1 else: raise Exception("Unexpected result in AverageBlur@1") p_seen = [v / nb_iterations for v in nb_seen] assert 0.4 <= p_seen[0] <= 0.6 assert 0.4 <= p_seen[1] <= 0.6 # k as (3, 5) aug = iaa.AverageBlur(k=(3, 5)) nb_iterations = 100 nb_seen = [0, 0, 0] for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): nb_seen[0] += 1 elif np.array_equal(observed, blur4x4): nb_seen[1] += 1 elif np.array_equal(observed, blur5x5): nb_seen[2] += 1 else: raise Exception("Unexpected result in AverageBlur@2") p_seen = [v / nb_iterations for v in nb_seen] assert 0.23 <= p_seen[0] <= 0.43 assert 0.23 <= p_seen[1] <= 0.43 assert 0.23 <= p_seen[2] <= 0.43 # k as stochastic parameter aug = iaa.AverageBlur(k=iap.Choice([3, 5])) nb_iterations = 100 nb_seen = [0, 0] for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): nb_seen[0] += 1 elif np.array_equal(observed, blur5x5): nb_seen[1] += 1 else: raise Exception("Unexpected result in AverageBlur@3") p_seen = [v / nb_iterations for v in nb_seen] assert 0.4 <= p_seen[0] <= 0.6 assert 0.4 <= p_seen[1] <= 0.6 # k as ((3, 5), (3, 5)) aug = iaa.AverageBlur(k=((3, 5), (3, 5))) possible = dict() for kh in [3, 4, 5]: for kw in [3, 4, 5]: key = (kh, kw) if kh == 0 or kw == 0: possible[key] = np.copy(base_img) else: possible[key] = cv2.blur(base_img, (kh, kw))[..., np.newaxis] nb_iterations = 250 nb_seen = dict([(key, 0) for key, val in possible.items()]) for i in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) for key, img_aug in possible.items(): if np.array_equal(observed, img_aug): nb_seen[key] += 1 # dont check sum here, because 0xX and Xx0 are all the same, i.e. much # higher sum than nb_iterations assert all([v > 0 for v in nb_seen.values()]) # keypoints shouldnt be changed aug = iaa.AverageBlur(k=3) aug_det = aug.to_deterministic() observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected)
def test_GaussianBlur(): reseed() base_img = np.array([[0, 0, 0], [0, 255, 0], [0, 0, 0]], dtype=np.uint8) base_img = base_img[:, :, np.newaxis] images = np.array([base_img]) images_list = [base_img] outer_pixels = ([], []) for i in sm.xrange(base_img.shape[0]): for j in sm.xrange(base_img.shape[1]): if i != j: outer_pixels[0].append(i) outer_pixels[1].append(j) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2) ], shape=base_img.shape) ] # no blur, shouldnt change anything aug = iaa.GaussianBlur(sigma=0) aug_det = aug.to_deterministic() observed = aug.augment_images(images) expected = images assert np.array_equal(observed, expected) # weak blur of center pixel aug = iaa.GaussianBlur(sigma=0.5) aug_det = aug.to_deterministic() # images as numpy array observed = aug.augment_images(images) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() observed = aug_det.augment_images(images) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() # images as list observed = aug.augment_images(images_list) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() observed = aug_det.augment_images(images_list) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() # keypoints shouldnt be changed observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) # varying blur sigmas aug = iaa.GaussianBlur(sigma=(0, 1)) aug_det = aug.to_deterministic() last_aug = None last_aug_det = None nb_changed_aug = 0 nb_changed_aug_det = 0 nb_iterations = 1000 for i in sm.xrange(nb_iterations): observed_aug = aug.augment_images(images) observed_aug_det = aug_det.augment_images(images) if i == 0: last_aug = observed_aug last_aug_det = observed_aug_det else: if not np.array_equal(observed_aug, last_aug): nb_changed_aug += 1 if not np.array_equal(observed_aug_det, last_aug_det): nb_changed_aug_det += 1 last_aug = observed_aug last_aug_det = observed_aug_det assert nb_changed_aug >= int(nb_iterations * 0.8) assert nb_changed_aug_det == 0
def test_MedianBlur(): reseed() base_img = np.zeros((11, 11, 1), dtype=np.uint8) base_img[3:8, 3:8, 0] = 1 base_img[4:7, 4:7, 0] = 2 base_img[5:6, 5:6, 0] = 3 blur3x3 = np.zeros_like(base_img) blur3x3[3:8, 3:8, 0] = 1 blur3x3[4:7, 4:7, 0] = 2 blur3x3[4, 4, 0] = 1 blur3x3[4, 6, 0] = 1 blur3x3[6, 4, 0] = 1 blur3x3[6, 6, 0] = 1 blur3x3[3, 3, 0] = 0 blur3x3[3, 7, 0] = 0 blur3x3[7, 3, 0] = 0 blur3x3[7, 7, 0] = 0 blur5x5 = np.copy(blur3x3) blur5x5[4, 3, 0] = 0 blur5x5[3, 4, 0] = 0 blur5x5[6, 3, 0] = 0 blur5x5[7, 4, 0] = 0 blur5x5[4, 7, 0] = 0 blur5x5[3, 6, 0] = 0 blur5x5[6, 7, 0] = 0 blur5x5[7, 6, 0] = 0 blur5x5[blur5x5 > 1] = 1 keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2) ], shape=base_img.shape) ] # no blur, shouldnt change anything aug = iaa.MedianBlur(k=1) observed = aug.augment_image(base_img) assert np.array_equal(observed, base_img) # k=3 aug = iaa.MedianBlur(k=3) observed = aug.augment_image(base_img) assert np.array_equal(observed, blur3x3) # k=5 aug = iaa.MedianBlur(k=5) observed = aug.augment_image(base_img) assert np.array_equal(observed, blur5x5) # k as (3, 5) aug = iaa.MedianBlur(k=(3, 5)) seen = [False, False] for i in sm.xrange(100): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): seen[0] = True elif np.array_equal(observed, blur5x5): seen[1] = True else: raise Exception("Unexpected result in MedianBlur@1") if all(seen): break assert all(seen) # k as stochastic parameter aug = iaa.MedianBlur(k=iap.Choice([3, 5])) seen = [False, False] for i in sm.xrange(100): observed = aug.augment_image(base_img) if np.array_equal(observed, blur3x3): seen[0] += True elif np.array_equal(observed, blur5x5): seen[1] += True else: raise Exception("Unexpected result in MedianBlur@2") if all(seen): break assert all(seen) # keypoints shouldnt be changed aug = iaa.MedianBlur(k=3) aug_det = aug.to_deterministic() observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected)
def test_Alpha(): reseed() base_img = np.zeros((3, 3, 1), dtype=np.uint8) heatmaps_arr = np.float32([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 1.0, 1.0]]) heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]) heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3)) aug = iaa.Alpha(1, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 10).astype(np.uint8) assert np.allclose(observed, expected) for per_channel in [False, True]: aug = iaa.Alpha(1, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=per_channel) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == heatmaps.shape assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6 assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_r1) aug = iaa.Alpha(0, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 20).astype(np.uint8) assert np.allclose(observed, expected) for per_channel in [False, True]: aug = iaa.Alpha(0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"x": -1}), per_channel=per_channel) observed = aug.augment_heatmaps([heatmaps])[0] assert observed.shape == heatmaps.shape assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6 assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6 assert np.allclose(observed.get_arr(), heatmaps_arr_l1) aug = iaa.Alpha(0.75, iaa.Add(10), iaa.Add(20)) observed = aug.augment_image(base_img) expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8) assert np.allclose(observed, expected) aug = iaa.Alpha(0.75, None, iaa.Add(20)) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype( np.uint8) assert np.allclose(observed, expected) aug = iaa.Alpha(0.75, iaa.Add(10), None) observed = aug.augment_image(base_img + 10) expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype( np.uint8) assert np.allclose(observed, expected) base_img = np.zeros((1, 2, 1), dtype=np.uint8) nb_iterations = 1000 aug = iaa.Alpha((0.0, 1.0), iaa.Add(10), iaa.Add(110)) values = [] for _ in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) observed_val = np.round(np.average(observed)) - 10 values.append(observed_val / 100) nb_bins = 5 hist, _ = np.histogram(values, bins=nb_bins, range=(0.0, 1.0), density=False) density_expected = 1.0 / nb_bins density_tolerance = 0.05 for nb_samples in hist: density = nb_samples / nb_iterations assert density_expected - density_tolerance < density < density_expected + density_tolerance # bad datatype for factor got_exception = False try: _ = iaa.Alpha(False, iaa.Add(10), None) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # per_channel aug = iaa.Alpha(1.0, iaa.Add((0, 100), per_channel=True), None, per_channel=True) observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8)) uq = np.unique(observed) assert len(uq) > 1 assert np.max(observed) > 80 assert np.min(observed) < 20 aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), None, per_channel=True) observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8)) uq = np.unique(observed) assert len(uq) > 1 assert np.max(observed) > 80 assert np.min(observed) < 20 aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), iaa.Add(0), per_channel=0.5) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_image(np.zeros((1, 1, 100), dtype=np.uint8)) uq = np.unique(observed) if len(uq) == 1: seen[0] += 1 elif len(uq) > 1: seen[1] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 # bad datatype for per_channel got_exception = False try: _ = iaa.Alpha(0.5, iaa.Add(10), None, per_channel="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # propagating aug = iaa.Alpha(0.5, iaa.Add(100), iaa.Add(50), name="AlphaTest") def propagator(images, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksImages(propagator=propagator) image = np.zeros((10, 10, 3), dtype=np.uint8) + 1 observed = aug.augment_image(image, hooks=hooks) assert np.array_equal(observed, image) # ----- # keypoints # ----- kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)] kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3)) aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1})) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) # per_channel aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.deepcopy() assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) observed = aug.augment_keypoints([kpsoi])[0] expected = kpsoi.shift(x=1) assert keypoints_equal([observed], [expected]) aug = iaa.Alpha(iap.Choice([0.49, 0.51]), iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True) expected_same = kpsoi.deepcopy() expected_shifted = kpsoi.shift(x=1) seen = [0, 0] for _ in sm.xrange(200): observed = aug.augment_keypoints([kpsoi])[0] if keypoints_equal([observed], [expected_same]): seen[0] += 1 elif keypoints_equal([observed], [expected_shifted]): seen[1] += 1 else: assert False assert 100 - 50 < seen[0] < 100 + 50 assert 100 - 50 < seen[1] < 100 + 50 # propagating aug = iaa.Alpha(0.0, iaa.Affine(translate_px={"x": 1}), iaa.Affine(translate_px={"y": 1}), name="AlphaTest") def propagator(kpsoi_to_aug, augmenter, parents, default): if "Alpha" in augmenter.name: return False else: return default hooks = ia.HooksKeypoints(propagator=propagator) observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0] assert keypoints_equal([observed], [kpsoi]) # ----- # get_parameters() # ----- first = iaa.Noop() second = iaa.Sequential([iaa.Add(1)]) aug = iaa.Alpha(0.65, first, second, per_channel=1) params = aug.get_parameters() assert isinstance(params[0], iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.65 - 1e-6 < params[0].value < 0.65 + 1e-6 assert params[1].value == 1 # ----- # get_children_lists() # ----- first = iaa.Noop() second = iaa.Sequential([iaa.Add(1)]) aug = iaa.Alpha(0.65, first, second, per_channel=1) children_lsts = aug.get_children_lists() assert len(children_lsts) == 2 assert ia.is_iterable([lst for lst in children_lsts]) assert first in children_lsts[0] assert second == children_lsts[1]
def test_Flipud(): reseed() base_img = np.array([[0, 0, 1], [0, 0, 1], [0, 1, 1]], dtype=np.uint8) base_img = base_img[:, :, np.newaxis] base_img_flipped = np.array([[0, 1, 1], [0, 0, 1], [0, 0, 1]], dtype=np.uint8) base_img_flipped = base_img_flipped[:, :, np.newaxis] images = np.array([base_img]) images_flipped = np.array([base_img_flipped]) keypoints = [ia.KeypointsOnImage([ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2)], shape=base_img.shape)] keypoints_flipped = [ia.KeypointsOnImage([ia.Keypoint(x=0, y=2), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=0)], shape=base_img.shape)] # 0% chance of flip aug = iaa.Flipud(0) aug_det = aug.to_deterministic() for _ in sm.xrange(10): observed = aug.augment_images(images) expected = images assert np.array_equal(observed, expected) observed = aug_det.augment_images(images) expected = images assert np.array_equal(observed, expected) observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) # 0% chance of flip, heatmaps aug = iaa.Flipud(0) heatmaps = ia.HeatmapsOnImage( np.float32([ [0, 0.5, 0.75], [0, 0.5, 0.75], [0.75, 0.75, 0.75], ]), shape=(3, 3, 3) ) observed = aug.augment_heatmaps([heatmaps])[0] expected = heatmaps.get_arr() assert observed.shape == heatmaps.shape assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6 assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6 assert np.array_equal(observed.get_arr(), expected) # 100% chance of flip aug = iaa.Flipud(1.0) aug_det = aug.to_deterministic() for _ in sm.xrange(10): observed = aug.augment_images(images) expected = images_flipped assert np.array_equal(observed, expected) observed = aug_det.augment_images(images) expected = images_flipped assert np.array_equal(observed, expected) observed = aug.augment_keypoints(keypoints) expected = keypoints_flipped assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints_flipped assert keypoints_equal(observed, expected) # 100% chance of flip, heatmaps aug = iaa.Flipud(1.0) heatmaps = ia.HeatmapsOnImage( np.float32([ [0, 0.5, 0.75], [0, 0.5, 0.75], [0.75, 0.75, 0.75], ]), shape=(3, 3, 3) ) observed = aug.augment_heatmaps([heatmaps])[0] expected = np.flipud(heatmaps.get_arr()) assert observed.shape == heatmaps.shape assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6 assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6 assert np.array_equal(observed.get_arr(), expected) # 50% chance of flip aug = iaa.Flipud(0.5) aug_det = aug.to_deterministic() nb_iterations = 1000 nb_images_flipped = 0 nb_images_flipped_det = 0 nb_keypoints_flipped = 0 nb_keypoints_flipped_det = 0 for _ in sm.xrange(nb_iterations): observed = aug.augment_images(images) if np.array_equal(observed, images_flipped): nb_images_flipped += 1 observed = aug_det.augment_images(images) if np.array_equal(observed, images_flipped): nb_images_flipped_det += 1 observed = aug.augment_keypoints(keypoints) if keypoints_equal(observed, keypoints_flipped): nb_keypoints_flipped += 1 observed = aug_det.augment_keypoints(keypoints) if keypoints_equal(observed, keypoints_flipped): nb_keypoints_flipped_det += 1 assert int(nb_iterations * 0.3) <= nb_images_flipped <= int(nb_iterations * 0.7) assert int(nb_iterations * 0.3) <= nb_keypoints_flipped <= int(nb_iterations * 0.7) assert nb_images_flipped_det in [0, nb_iterations] assert nb_keypoints_flipped_det in [0, nb_iterations] # 50% chance of flipped, multiple images, list as input images_multi = [base_img, base_img] aug = iaa.Flipud(0.5) aug_det = aug.to_deterministic() nb_iterations = 1000 nb_flipped_by_pos = [0] * len(images_multi) nb_flipped_by_pos_det = [0] * len(images_multi) for _ in sm.xrange(nb_iterations): observed = aug.augment_images(images_multi) for i in sm.xrange(len(images_multi)): if np.array_equal(observed[i], base_img_flipped): nb_flipped_by_pos[i] += 1 observed = aug_det.augment_images(images_multi) for i in sm.xrange(len(images_multi)): if np.array_equal(observed[i], base_img_flipped): nb_flipped_by_pos_det[i] += 1 for val in nb_flipped_by_pos: assert int(nb_iterations * 0.3) <= val <= int(nb_iterations * 0.7) for val in nb_flipped_by_pos_det: assert val in [0, nb_iterations] # test StochasticParameter as p aug = iaa.Flipud(p=iap.Choice([0, 1], p=[0.7, 0.3])) seen = [0, 0] for _ in sm.xrange(1000): observed = aug.augment_image(base_img) if np.array_equal(observed, base_img): seen[0] += 1 elif np.array_equal(observed, base_img_flipped): seen[1] += 1 else: assert False assert 700 - 75 < seen[0] < 700 + 75 assert 300 - 75 < seen[1] < 300 + 75 # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Flipud(p="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Flipud(p=0.5) params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4
def test_Fliplr(): reseed() base_img = np.array([[0, 0, 1], [0, 0, 1], [0, 1, 1]], dtype=np.uint8) base_img = base_img[:, :, np.newaxis] base_img_flipped = np.array([[1, 0, 0], [1, 0, 0], [1, 1, 0]], dtype=np.uint8) base_img_flipped = base_img_flipped[:, :, np.newaxis] images = np.array([base_img]) images_flipped = np.array([base_img_flipped]) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2) ], shape=base_img.shape) ] keypoints_flipped = [ ia.KeypointsOnImage([ ia.Keypoint(x=2, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=0, y=2) ], shape=base_img.shape) ] # 0% chance of flip aug = iaa.Fliplr(0) aug_det = aug.to_deterministic() for _ in sm.xrange(10): observed = aug.augment_images(images) expected = images assert np.array_equal(observed, expected) observed = aug_det.augment_images(images) expected = images assert np.array_equal(observed, expected) observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) # 0% chance of flip, heatmaps aug = iaa.Fliplr(0) heatmaps = ia.HeatmapsOnImage(np.float32([ [0, 0.5, 0.75], [0, 0.5, 0.75], [0.75, 0.75, 0.75], ]), shape=(3, 3, 3)) observed = aug.augment_heatmaps([heatmaps])[0] expected = heatmaps.get_arr() assert observed.shape == heatmaps.shape assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6 assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6 assert np.array_equal(observed.get_arr(), expected) # 100% chance of flip aug = iaa.Fliplr(1.0) aug_det = aug.to_deterministic() for _ in sm.xrange(10): observed = aug.augment_images(images) expected = images_flipped assert np.array_equal(observed, expected) observed = aug_det.augment_images(images) expected = images_flipped assert np.array_equal(observed, expected) observed = aug.augment_keypoints(keypoints) expected = keypoints_flipped assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints_flipped assert keypoints_equal(observed, expected) # 100% chance of flip, heatmaps aug = iaa.Fliplr(1.0) heatmaps = ia.HeatmapsOnImage(np.float32([ [0, 0.5, 0.75], [0, 0.5, 0.75], [0.75, 0.75, 0.75], ]), shape=(3, 3, 3)) observed = aug.augment_heatmaps([heatmaps])[0] expected = np.fliplr(heatmaps.get_arr()) assert observed.shape == heatmaps.shape assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6 assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6 assert np.array_equal(observed.get_arr(), expected) # 50% chance of flip aug = iaa.Fliplr(0.5) aug_det = aug.to_deterministic() nb_iterations = 1000 nb_images_flipped = 0 nb_images_flipped_det = 0 nb_keypoints_flipped = 0 nb_keypoints_flipped_det = 0 for _ in sm.xrange(nb_iterations): observed = aug.augment_images(images) if np.array_equal(observed, images_flipped): nb_images_flipped += 1 observed = aug_det.augment_images(images) if np.array_equal(observed, images_flipped): nb_images_flipped_det += 1 observed = aug.augment_keypoints(keypoints) if keypoints_equal(observed, keypoints_flipped): nb_keypoints_flipped += 1 observed = aug_det.augment_keypoints(keypoints) if keypoints_equal(observed, keypoints_flipped): nb_keypoints_flipped_det += 1 assert int(nb_iterations * 0.3) <= nb_images_flipped <= int( nb_iterations * 0.7) assert int(nb_iterations * 0.3) <= nb_keypoints_flipped <= int( nb_iterations * 0.7) assert nb_images_flipped_det in [0, nb_iterations] assert nb_keypoints_flipped_det in [0, nb_iterations] # 50% chance of flipped, multiple images, list as input images_multi = [base_img, base_img] aug = iaa.Fliplr(0.5) aug_det = aug.to_deterministic() nb_iterations = 1000 nb_flipped_by_pos = [0] * len(images_multi) nb_flipped_by_pos_det = [0] * len(images_multi) for _ in sm.xrange(nb_iterations): observed = aug.augment_images(images_multi) for i in sm.xrange(len(images_multi)): if np.array_equal(observed[i], base_img_flipped): nb_flipped_by_pos[i] += 1 observed = aug_det.augment_images(images_multi) for i in sm.xrange(len(images_multi)): if np.array_equal(observed[i], base_img_flipped): nb_flipped_by_pos_det[i] += 1 for val in nb_flipped_by_pos: assert int(nb_iterations * 0.3) <= val <= int(nb_iterations * 0.7) for val in nb_flipped_by_pos_det: assert val in [0, nb_iterations] # test StochasticParameter as p aug = iaa.Fliplr(p=iap.Choice([0, 1], p=[0.7, 0.3])) seen = [0, 0] for _ in sm.xrange(1000): observed = aug.augment_image(base_img) if np.array_equal(observed, base_img): seen[0] += 1 elif np.array_equal(observed, base_img_flipped): seen[1] += 1 else: assert False assert 700 - 75 < seen[0] < 700 + 75 assert 300 - 75 < seen[1] < 300 + 75 # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Fliplr(p="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Fliplr(p=0.5) params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 ################### # test other dtypes ################### aug = iaa.Fliplr(1.0) image = np.zeros((3, 3), dtype=bool) image[0, 0] = True expected = np.zeros((3, 3), dtype=bool) expected[0, 2] = True image_aug = aug.augment_image(image) assert image_aug.dtype.type == image.dtype.type assert np.all(image_aug == expected) for dtype in [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int32, np.int64 ]: min_value, center_value, max_value = iadt.get_value_range_of_dtype( dtype) value = max_value image = np.zeros((3, 3), dtype=dtype) image[0, 0] = value expected = np.zeros((3, 3), dtype=dtype) expected[0, 2] = value image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.array_equal(image_aug, expected) for dtype, value in zip([np.float16, np.float32, np.float64, np.float128], [5000, 1000**2, 1000**3, 1000**4]): atol = 1e-9 * max_value if dtype != np.float16 else 1e-3 * max_value image = np.zeros((3, 3), dtype=dtype) image[0, 0] = value expected = np.zeros((3, 3), dtype=dtype) expected[0, 2] = value image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.allclose(image_aug, expected, atol=atol)
def test_SigmoidContrast(): reseed() img = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] img = np.uint8(img) img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3)) # check basic functionality with per_chanenl on/off (makes no difference due to deterministic # parameters) for per_channel in [False, 0, 0.0, True, 1, 1.0]: for gain, cutoff in itertools.product([5, 10], [0.25, 0.75]): aug = iaa.SigmoidContrast(gain=iap.Deterministic(gain), cutoff=iap.Deterministic(cutoff), per_channel=per_channel) img_aug = aug.augment_image(img) img3d_aug = aug.augment_image(img3d) assert img_aug.dtype.type == np.uint8 assert img3d_aug.dtype.type == np.uint8 assert np.array_equal(img_aug, skimage.exposure.adjust_sigmoid(img, gain=gain, cutoff=cutoff)) assert np.array_equal(img3d_aug, skimage.exposure.adjust_sigmoid(img3d, gain=gain, cutoff=cutoff)) # check that tuple to uniform works # note that gain and cutoff are saved in inverted order in _ContrastFuncWrapper to match # the order of skimage's function aug = iaa.SigmoidContrast(gain=(1, 2), cutoff=(0.25, 0.75)) assert isinstance(aug.params1d[0], iap.Uniform) assert isinstance(aug.params1d[0].a, iap.Deterministic) assert isinstance(aug.params1d[0].b, iap.Deterministic) assert np.allclose(aug.params1d[0].a.value, 0.25) assert np.allclose(aug.params1d[0].b.value, 0.75) assert isinstance(aug.params1d[1], iap.Uniform) assert isinstance(aug.params1d[1].a, iap.Deterministic) assert isinstance(aug.params1d[1].b, iap.Deterministic) assert aug.params1d[1].a.value == 1 assert aug.params1d[1].b.value == 2 # check that list to choice works # note that gain and cutoff are saved in inverted order in _ContrastFuncWrapper to match # the order of skimage's function aug = iaa.SigmoidContrast(gain=[1, 2], cutoff=[0.25, 0.75]) assert isinstance(aug.params1d[0], iap.Choice) assert all([np.allclose(val, val_choice) for val, val_choice in zip([0.25, 0.75], aug.params1d[0].a)]) assert isinstance(aug.params1d[1], iap.Choice) assert all([val in aug.params1d[1].a for val in [1, 2]]) # check that per_channel at 50% prob works aug = iaa.SigmoidContrast(gain=(1, 10), cutoff=(0.25, 0.75), per_channel=0.5) seen = [False, False] img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128 for _ in sm.xrange(100): img_aug = aug.augment_image(img1000d) assert img_aug.dtype.type == np.uint8 l = len(set(img_aug.flatten().tolist())) if l == 1: seen[0] = True else: seen[1] = True if all(seen): break assert all(seen) # check that keypoints are not changed kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3)) kpsoi_aug = iaa.SigmoidContrast(gain=10, cutoff=0.5).augment_keypoints([kpsoi]) assert keypoints_equal([kpsoi], kpsoi_aug) # check that heatmaps are not changed heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5, shape=(3, 3, 3)) heatmaps_aug = iaa.SigmoidContrast(gain=10, cutoff=0.5).augment_heatmaps([heatmaps])[0] assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)
def test_determinism(): reseed() images = [ ia.quokka(size=(128, 128)), ia.quokka(size=(64, 64)), ia.imresize_single_image(skimage.data.astronaut(), (128, 256)) ] images.extend([ia.quokka(size=(16, 16))] * 20) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=20, y=10), ia.Keypoint(x=5, y=5), ia.Keypoint(x=10, y=43)], shape=(50, 60, 3)) ] * 20 augs = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))), # iaa.WithChannels([0], iaa.Add((-50, 50))), # iaa.Noop(name="Noop-nochange"), # iaa.Lambda( # func_images=lambda images, random_state, parents, hooks: images, # func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: keypoints_on_images, # name="Lambda-nochange" # ), # iaa.AssertLambda( # func_images=lambda images, random_state, parents, hooks: True, # func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: True, # name="AssertLambda-nochange" # ), # iaa.AssertShape( # (None, None, None, 3), # check_keypoints=False, # name="AssertShape-nochange" # ), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)), # iaa.ChangeColorspace(to_colorspace="GRAY"), iaa.Grayscale(alpha=(0.1, 1.0)), iaa.GaussianBlur((0.1, 3.0)), iaa.AverageBlur((3, 11)), iaa.MedianBlur((3, 11)), # iaa.Convolve(np.array([[0, 1, 0], # [1, -4, 1], # [0, 1, 0]])), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)), iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)), iaa.EdgeDetect(alpha=(0.1, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)), iaa.Add((-50, 50)), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)), iaa.Multiply((0.6, 1.4)), iaa.MultiplyElementwise((0.6, 1.4)), iaa.Dropout((0.3, 0.5)), iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)), iaa.Invert(0.5), iaa.ContrastNormalization((0.6, 1.4)), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=0.5) ] augs_affect_geometry = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5)) ] for aug in augs: aug_det = aug.to_deterministic() images_aug1 = aug_det.augment_images(images) images_aug2 = aug_det.augment_images(images) aug_det = aug.to_deterministic() images_aug3 = aug_det.augment_images(images) images_aug4 = aug_det.augment_images(images) assert array_equal_lists(images_aug1, images_aug2), \ "Images (1, 2) expected to be identical for %s" % (aug.name,) assert array_equal_lists(images_aug3, images_aug4), \ "Images (3, 4) expected to be identical for %s" % (aug.name,) assert not array_equal_lists(images_aug1, images_aug3), \ "Images (1, 3) expected to be different for %s" % (aug.name,) for aug in augs_affect_geometry: aug_det = aug.to_deterministic() kps_aug1 = aug_det.augment_keypoints(keypoints) kps_aug2 = aug_det.augment_keypoints(keypoints) aug_det = aug.to_deterministic() kps_aug3 = aug_det.augment_keypoints(keypoints) kps_aug4 = aug_det.augment_keypoints(keypoints) assert keypoints_equal(kps_aug1, kps_aug2), \ "Keypoints (1, 2) expected to be identical for %s" % (aug.name,) assert keypoints_equal(kps_aug3, kps_aug4), \ "Keypoints (3, 4) expected to be identical for %s" % (aug.name,) assert not keypoints_equal(kps_aug1, kps_aug3), \ "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
def test_GaussianBlur(): reseed() base_img = np.array([[0, 0, 0], [0, 255, 0], [0, 0, 0]], dtype=np.uint8) base_img = base_img[:, :, np.newaxis] images = np.array([base_img]) images_list = [base_img] outer_pixels = ([], []) for i in sm.xrange(base_img.shape[0]): for j in sm.xrange(base_img.shape[1]): if i != j: outer_pixels[0].append(i) outer_pixels[1].append(j) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=2, y=2) ], shape=base_img.shape) ] # no blur, shouldnt change anything aug = iaa.GaussianBlur(sigma=0) observed = aug.augment_images(images) expected = images assert np.array_equal(observed, expected) # weak blur of center pixel aug = iaa.GaussianBlur(sigma=0.5) aug_det = aug.to_deterministic() # images as numpy array observed = aug.augment_images(images) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() observed = aug_det.augment_images(images) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() # images as list observed = aug.augment_images(images_list) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() observed = aug_det.augment_images(images_list) assert 100 < observed[0][1, 1] < 255 assert (observed[0][outer_pixels[0], outer_pixels[1]] > 0).all() assert (observed[0][outer_pixels[0], outer_pixels[1]] < 50).all() # keypoints shouldnt be changed observed = aug.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) observed = aug_det.augment_keypoints(keypoints) expected = keypoints assert keypoints_equal(observed, expected) # varying blur sigmas aug = iaa.GaussianBlur(sigma=(0, 1)) aug_det = aug.to_deterministic() last_aug = None last_aug_det = None nb_changed_aug = 0 nb_changed_aug_det = 0 nb_iterations = 1000 for i in sm.xrange(nb_iterations): observed_aug = aug.augment_images(images) observed_aug_det = aug_det.augment_images(images) if i == 0: last_aug = observed_aug last_aug_det = observed_aug_det else: if not np.array_equal(observed_aug, last_aug): nb_changed_aug += 1 if not np.array_equal(observed_aug_det, last_aug_det): nb_changed_aug_det += 1 last_aug = observed_aug last_aug_det = observed_aug_det assert nb_changed_aug >= int(nb_iterations * 0.8) assert nb_changed_aug_det == 0 ############################# # test other dtypes below ############################# # -- # blur of various dtypes at sigma=0 # -- aug = iaa.GaussianBlur(sigma=0) # bool image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image_aug = aug.augment_image(image) assert image_aug.dtype.type == np.bool_ assert np.all(image_aug == image) # uint, int for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]: _min_value, center_value, _max_value = iadt.get_value_range_of_dtype( dtype) image = np.zeros((3, 3), dtype=dtype) image[1, 1] = int(center_value) image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.all(image_aug == image) # float for dtype in [np.float16, np.float32, np.float64]: _min_value, center_value, _max_value = iadt.get_value_range_of_dtype( dtype) image = np.zeros((3, 3), dtype=dtype) image[1, 1] = center_value image_aug = aug.augment_image(image) assert image_aug.dtype.type == dtype assert np.allclose(image_aug, image) # -- # blur of bool input at sigma=0.6 # -- # here we use a special mask and sigma as otherwise the only values ending up with >0.5 would be the ones that # were before the blur already at >0.5 # prototype kernel, generated via: # mask = np.zeros((5, 5), dtype=np.float64) # mask[1, 0] = 255 # mask[2, 0] = 255 # mask[2, 2] = 255 # mask[2, 4] = 255 # mask[3, 0] = 255 # mask = ndimage.gaussian_filter(mask, 1.0, mode="mirror") aug = iaa.GaussianBlur(sigma=0.6) mask_bool = np.float64([[57, 14, 2, 1, 1], [142, 42, 29, 14, 28], [169, 69, 114, 56, 114], [142, 42, 29, 14, 28], [57, 14, 2, 1, 1]]) / 255.0 image = np.zeros((5, 5), dtype=bool) image[1, 0] = True image[2, 0] = True image[2, 2] = True image[2, 4] = True image[3, 0] = True image_aug = aug.augment_image(image) expected = mask_bool > 0.5 assert image_aug.shape == mask_bool.shape assert image_aug.dtype.type == np.bool_ assert np.all(image_aug == expected) # -- # blur of various dtypes at sigma=1.0 # and using an example value of 100 for int/uint/float and True for bool # -- # prototype kernel, generated via: # mask = np.zeros((5, 5), dtype=np.float64) # mask[2, 2] = 100 # mask = ndimage.gaussian_filter(mask, 1.0, mode="mirror") aug = iaa.GaussianBlur(sigma=1.0) mask = np.float64([[1, 2, 3, 2, 1], [2, 5, 9, 5, 2], [4, 9, 15, 9, 4], [2, 5, 9, 5, 2], [1, 2, 3, 2, 1]]) # uint, int for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]: image = np.zeros((5, 5), dtype=dtype) image[2, 2] = 100 image_aug = aug.augment_image(image) expected = mask.astype(dtype) diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64)) assert image_aug.shape == mask.shape assert image_aug.dtype.type == dtype assert np.max(diff) <= 4 assert np.average(diff) <= 2 # float for dtype in [np.float16, np.float32, np.float64]: image = np.zeros((5, 5), dtype=dtype) image[2, 2] = 100.0 image_aug = aug.augment_image(image) expected = mask.astype(dtype) diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.shape == mask.shape assert image_aug.dtype.type == dtype assert np.max(diff) < 4 assert np.average(diff) < 2.0 # -- # blur of various dtypes at sigma=0.4 # and using an example value of 100 for int/uint/float and True for bool # -- aug = iaa.GaussianBlur(sigma=0.4) # prototype kernel, generated via: # mask = np.zeros((5, 5), dtype=np.uint8) # mask[2, 2] = 100 # kernel = ndimage.gaussian_filter(mask, 0.4, mode="mirror") mask = np.float64([[0, 0, 0, 0, 0], [0, 0, 3, 0, 0], [0, 3, 83, 3, 0], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0]]) # uint, int for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]: image = np.zeros((5, 5), dtype=dtype) image[2, 2] = 100 image_aug = aug.augment_image(image) expected = mask.astype(dtype) diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64)) assert image_aug.shape == mask.shape assert image_aug.dtype.type == dtype assert np.max(diff) <= 4 # float for dtype in [np.float16, np.float32, np.float64]: image = np.zeros((5, 5), dtype=dtype) image[2, 2] = 100.0 image_aug = aug.augment_image(image) expected = mask.astype(dtype) diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.shape == mask.shape assert image_aug.dtype.type == dtype assert np.max(diff) < 4.0 # -- # blur of various dtypes at sigma=0.75 # and values being half-way between center and maximum for each dtype # The goal of this test is to verify that no major loss of resolution happens for large dtypes. # Such inaccuracies appear for float64 if used. # -- aug = iaa.GaussianBlur(sigma=0.75) # prototype kernel, generated via: # mask = np.zeros((5, 5), dtype=np.int32) # mask[2, 2] = 1000 * 1000 # kernel = ndimage.gaussian_filter(mask, 0.75) mask = np.float64([[923, 6650, 16163, 6650, 923], [6650, 47896, 116408, 47896, 6650], [16163, 116408, 282925, 116408, 16163], [6650, 47896, 116408, 47896, 6650], [923, 6650, 16163, 6650, 923]]) / (1000.0 * 1000.0) # uint, int for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]: min_value, center_value, max_value = iadt.get_value_range_of_dtype( dtype) dynamic_range = max_value - min_value value = int(center_value + 0.4 * max_value) image = np.zeros((5, 5), dtype=dtype) image[2, 2] = value image_aug = aug.augment_image(image) expected = (mask * value).astype(dtype) diff = np.abs(image_aug.astype(np.int64) - expected.astype(np.int64)) assert image_aug.shape == mask.shape assert image_aug.dtype.type == dtype if np.dtype(dtype).itemsize <= 1: assert np.max(diff) <= 4 else: assert np.max(diff) <= 0.01 * dynamic_range # float for dtype, value in zip([np.float16, np.float32, np.float64], [5000, 1000 * 1000, 1000 * 1000 * 1000]): image = np.zeros((5, 5), dtype=dtype) image[2, 2] = value image_aug = aug.augment_image(image) expected = (mask * value).astype(dtype) diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.shape == mask.shape assert image_aug.dtype.type == dtype # accepts difference of 2.0, 4.0, 8.0, 16.0 (at 1, 2, 4, 8 bytes, i.e. 8, 16, 32, 64 bit) assert np.max( diff) < np.dtype(dtype).itemsize * 0.01 * np.float128(value) # assert failure on invalid dtypes aug = iaa.GaussianBlur(sigma=1.0) for dt in [np.float128]: got_exception = False try: _ = aug.augment_image(np.zeros((1, 1), dtype=dt)) except Exception as exc: assert "forbidden dtype" in str(exc) got_exception = True assert got_exception