def test__draw_samples__single_value_hysteresis(self): seed = 1 nb_images = 1000 aug = iaa.Canny( alpha=0.2, hysteresis_thresholds=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], sobel_kernel_size=[3, 5, 7], random_state=iarandom.RNG(seed)) example_image = np.zeros((5, 5, 3), dtype=np.uint8) samples = aug._draw_samples([example_image] * nb_images, random_state=iarandom.RNG(seed)) alpha_samples = samples[0] hthresh_samples = samples[1] sobel_samples = samples[2] rss = iarandom.RNG(seed).duplicate(4) alpha_expected = iap.Deterministic(0.2).draw_samples((nb_images, ), rss[0]) hthresh_expected = iap.Choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).draw_samples((nb_images, 2), rss[1]) sobel_expected = iap.Choice([3, 5, 7]).draw_samples((nb_images, ), rss[2]) invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1] assert np.any(invalid) hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]] assert hthresh_expected.shape == (nb_images, 2) assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1]) assert np.allclose(alpha_samples, alpha_expected) assert np.allclose(hthresh_samples, hthresh_expected) assert np.allclose(sobel_samples, sobel_expected)
def test__draw_samples__tuple_as_hysteresis(self): seed = 1 nb_images = 10 aug = iaa.Canny( alpha=0.2, hysteresis_thresholds=([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], iap.DiscreteUniform(5, 100)), sobel_kernel_size=[3, 5, 7], random_state=iarandom.RNG(seed)) aug.alpha = remove_prefetching(aug.alpha) aug.hysteresis_thresholds = ( remove_prefetching(aug.hysteresis_thresholds[0]), remove_prefetching(aug.hysteresis_thresholds[1]) ) aug.sobel_kernel_size = remove_prefetching(aug.sobel_kernel_size) example_image = np.zeros((5, 5, 3), dtype=np.uint8) samples = aug._draw_samples([example_image] * nb_images, random_state=iarandom.RNG(seed)) alpha_samples = samples[0] hthresh_samples = samples[1] sobel_samples = samples[2] rss = iarandom.RNG(seed).duplicate(4) alpha_expected = iap.Deterministic(0.2).draw_samples((nb_images,), rss[0]) hthresh_expected = [None, None] hthresh_expected[0] = iap.Choice( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).draw_samples((nb_images,), rss[1]) # TODO simplify this to rss[2].randint(5, 100+1) # would currenlty be a bit more ugly, because DiscrUniform # samples two values for a and b first from rss[2] hthresh_expected[1] = iap.DiscreteUniform(5, 100).draw_samples( (nb_images,), rss[2]) hthresh_expected = np.stack(hthresh_expected, axis=-1) sobel_expected = iap.Choice([3, 5, 7]).draw_samples((nb_images,), rss[3]) invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1] hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]] assert hthresh_expected.shape == (nb_images, 2) assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1]) assert np.allclose(alpha_samples, alpha_expected) assert np.allclose(hthresh_samples, hthresh_expected) assert np.allclose(sobel_samples, sobel_expected)
def test_one_image(self): rng = iarandom.RNG(0) image = rng.integers(0, 256, size=(256, 256, 3), dtype=np.uint8) debug_image = iaa.draw_debug_image([image]) assert self._image_contains(image, debug_image)
def test_extract_from_image_float_coords(self): image = iarandom.RNG(1234).integers(0, 255, size=(10, 10, 3)) bb = ia.BoundingBox(y1=1, y2=1.99999, x1=1, x2=1.99999) image_sub = bb.extract_from_image(image) assert np.array_equal(image_sub, image[1:1+1, 1:1+1, :])
def _test_augmenter(cls, augmenter_name, func_expected, dependent_on_seed): severity = 5 aug_cls = getattr(iaa.imgcorruptlike, augmenter_name) image = np.mod( np.arange(32*32*3), 256 ).reshape((32, 32, 3)).astype(np.uint8) rng = iarandom.RNG(1) # replay sampling of severities, not really necessary here as we # use a deterministic value, but still done for good style _ = iap.Deterministic(1).draw_samples((1,), rng) # As for the functions above, we can't just change the seed value # to get different augmentations as many functions are dependend # only on the severity. So we change only for some functions only # the seed and for the others severity+seed. image_aug1 = aug_cls(severity=severity, seed=1)(image=image) image_aug2 = aug_cls(severity=severity, seed=1)(image=image) if dependent_on_seed: image_aug3 = aug_cls(severity=severity, seed=2)( image=image) else: image_aug3 = aug_cls(severity=severity-1, seed=2)( image=image) image_aug_exp = func_expected( image, severity=severity, seed=rng.generate_seed_()) assert aug_cls(severity=severity).func is func_expected assert np.array_equal(image_aug1, image_aug_exp) assert np.array_equal(image_aug2, image_aug_exp) assert not np.array_equal(image_aug3, image_aug2)
def test_extract_from_image_bb_width_is_zero(self): image = iarandom.RNG(1234).integers(0, 255, size=(10, 10, 3)) bb = ia.BoundingBox(y1=1, y2=1, x1=2, x2=2) image_sub = bb.extract_from_image(image) assert np.array_equal(image_sub, image[1:1+1, 2:2+1, :])
def test_by_comparison_with_pil(self): rng = iarandom.RNG(0) shapes = [(1, 1), (10, 10), (1, 1, 3), (1, 2, 3), (2, 1, 3), (2, 2, 3), (5, 3, 3), (10, 5, 3), (5, 10, 3), (10, 10, 3), (20, 10, 3), (20, 40, 3), (50, 60, 3), (100, 100, 3), (200, 100, 3)] images = [ rng.integers(0, 255, size=shape).astype(np.uint8) for shape in shapes ] images = (images + [ np.full((1, 1, 3), 0, dtype=np.uint8), np.full((1, 1, 3), 255, dtype=np.uint8), np.full((20, 20, 3), 0, dtype=np.uint8), np.full((20, 20, 3), 255, dtype=np.uint8) ]) cutoffs = [0, 1, 2, 10, 50, 90, 99, 100] ignores = [None, 0, 1, 100, 255, [0, 1], [5, 10, 50], [99, 100]] for cutoff in cutoffs: for ignore in ignores: for i, image in enumerate(images): with self.subTest(cutoff=cutoff, ignore=ignore, image_idx=i, image_shape=image.shape): result_pil = np.asarray( PIL.ImageOps.autocontrast( PIL.Image.fromarray(image), cutoff=cutoff, ignore=ignore)) result_iaa = iaa.pillike.autocontrast(image, cutoff=cutoff, ignore=ignore) assert np.array_equal(result_pil, result_iaa)
def _test_aff_by_comparison_with_pil(self, arg_name, arg_values, matrix_gen): shapes = [(64, 64, 3), (32, 32, 3), (16, 8, 3), (1, 1, 3), (32, 32, 4)] seeds = [1, 2, 3] fillcolors = [None, 0, 128, (0, 255, 0)] for shape in shapes: for seed in seeds: for fillcolor in fillcolors: for arg_value in arg_values: with self.subTest(shape=shape, seed=seed, fillcolor=fillcolor, **{arg_name: arg_value}): image = iarandom.RNG(seed).integers(0, 256, size=shape, dtype="uint8") matrix = matrix_gen(arg_value) image_warped = iaa.pillike.warp_affine( image, fillcolor=fillcolor, center=(0.0, 0.0), **{arg_name: arg_value}) image_warped_exp = np.asarray( PIL.Image.fromarray(image).transform( shape[0:2][::-1], PIL.Image.AFFINE, matrix[:2, :].flat, fillcolor=fillcolor)) assert np.array_equal(image_warped, image_warped_exp)
def test_two_images(self): rng = iarandom.RNG(0) images = rng.integers(0, 256, size=(2, 256, 256, 3), dtype=np.uint8) debug_image = iaa.draw_debug_image(images) assert self._image_contains(images[0, ...], debug_image) assert self._image_contains(images[1, ...], debug_image)
def test_one_image_float32(self): rng = iarandom.RNG(0) image = rng.random(size=(256, 256, 3)).astype(np.float32) debug_image = iaa.draw_debug_image([image]) assert self._image_contains((image * 255).astype(np.uint8), debug_image)
def test_augment_images__random_values(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0 ) image_single_chan = iarandom.RNG(1).integers( 0, 255, size=(100, 100), dtype="uint8") image = np.tile(image_single_chan[:, :, np.newaxis], (1, 1, 3)) images_canny_uint8 = {} for thresh1, thresh2, ksize in itertools.product([100], [200], [3, 5]): if thresh1 > thresh2: continue image_canny = cv2.Canny( image, threshold1=thresh1, threshold2=thresh2, apertureSize=ksize, L2gradient=True) image_canny_uint8 = np.tile( image_canny[:, :, np.newaxis], (1, 1, 3)) similar = 0 for key, image_expected in images_canny_uint8.items(): if np.array_equal(image_canny_uint8, image_expected): similar += 1 assert similar == 0 images_canny_uint8[(thresh1, thresh2, ksize)] = image_canny_uint8 seen = {key: False for key in images_canny_uint8.keys()} for i in range(500): aug = iaa.Canny( alpha=1.0, hysteresis_thresholds=(iap.Deterministic(100), iap.Deterministic(200)), sobel_kernel_size=[3, 5], colorizer=colorizer, seed=i) image_aug = aug.augment_image(image) match_index = None for key, image_expected in images_canny_uint8.items(): if np.array_equal(image_aug, image_expected): match_index = key break assert match_index is not None seen[match_index] = True assert len(seen.keys()) == len(images_canny_uint8.keys()) if all(seen.values()): break assert np.all(seen.values())
def test_two_images_of_different_sizes(self): rng = iarandom.RNG(0) image1 = rng.integers(0, 256, size=(256, 256, 3), dtype=np.uint8) image2 = rng.integers(0, 256, size=(512, 256, 3), dtype=np.uint8) debug_image = iaa.draw_debug_image([image1, image2]) assert self._image_contains(image1, debug_image) assert self._image_contains(image2, debug_image)
def test_integrationtest_per_channel(self): image = iarandom.RNG(0).integers(50, 150, size=(100, 100, 50)) image = image.astype(np.uint8) aug = iaa.pillike.Autocontrast(10, per_channel=True) image_aug = aug(image=image) assert np.min(image_aug) < 50 assert np.max(image_aug) > 150
def test_compare_with_pil(self): def _solarize_pil(image, threshold): img = PIL.Image.fromarray(image) return np.asarray(PIL.ImageOps.solarize(img, threshold)) images = [ np.mod(np.arange(20*20*3), 255).astype(np.uint8)\ .reshape((20, 20, 3)), iarandom.RNG(0).integers(0, 256, size=(1, 1, 3), dtype="uint8"), iarandom.RNG(1).integers(0, 256, size=(20, 20, 3), dtype="uint8"), iarandom.RNG(2).integers(0, 256, size=(40, 40, 3), dtype="uint8"), iarandom.RNG(0).integers(0, 256, size=(20, 20), dtype="uint8") ] for image_idx, image in enumerate(images): for threshold in np.arange(256): with self.subTest(image_idx=image_idx, threshold=threshold): image_pil = _solarize_pil(image, threshold) image_iaa = iaa.pillike.solarize(image, threshold) assert np.array_equal(image_pil, image_iaa)
def test_extract_from_image_bb_partially_out_of_image_top_left(self): image = iarandom.RNG(1234).integers(0, 255, size=(10, 10, 3)) bb = ia.BoundingBox(y1=-1, y2=3, x1=-1, x2=4) image_sub = bb.extract_from_image(image) image_pad = np.pad( image, ((1, 0), (1, 0), (0, 0)), mode="constant", constant_values=0) # pad at top and left each 1px (black) assert np.array_equal(image_sub, image_pad[0:4, 0:5, :])
def test_extract_from_image_bb_partially_out_of_image_no_channels(self): image = iarandom.RNG(1234).integers(0, 255, size=(10, 10)) bb = ia.BoundingBox(y1=8, y2=11, x1=8, x2=11) image_sub = bb.extract_from_image(image) image_pad = np.pad( image, ((0, 1), (0, 1)), mode="constant", constant_values=0) # pad at bottom and right each 1px (black) assert np.array_equal(image_sub, image_pad[8:11, 8:11])
def test_draw_samples(self): mock_batch = mock.Mock() mock_batch.nb_rows = 50 aug = iaa.Cartoon() rs = iarandom.RNG(0) samples = aug._draw_samples(mock_batch, rs) assert len(np.unique(np.round(samples[0]*100, decimals=0))) > 1 assert len(np.unique(np.round(samples[1]*100, decimals=0))) > 1 assert len(np.unique(np.round(samples[2]*100, decimals=0))) > 1 assert len(np.unique(np.round(samples[3]*100, decimals=0))) > 1
def test_pickleable(self): colorizer = iaa.RandomColorsBinaryImageColorizer(color_true=(50, 100), color_false=(10, 50)) colorizer_pkl = pickle.loads(pickle.dumps(colorizer)) random_state = iarandom.RNG(1) color_true, color_false = colorizer._draw_samples(random_state.copy()) color_true_pkl, color_false_pkl = colorizer_pkl._draw_samples( random_state.copy()) assert np.array_equal(color_true, color_true_pkl) assert np.array_equal(color_false, color_false_pkl)
def test_one_image_float32_and_heatmap(self): rng = iarandom.RNG(0) image = rng.random(size=(256, 256, 3)).astype(np.float32) heatmap = np.zeros((256, 256, 1), dtype=np.float32) heatmap[128-25:128+25, 128-25:128+25] = 1.0 heatmap = ia.HeatmapsOnImage(heatmap, shape=image.shape) image1_w_overlay = heatmap.draw_on_image( (image*255).astype(np.uint8))[0] debug_image = iaa.draw_debug_image([image], heatmaps=[heatmap]) assert self._image_contains((image * 255).astype(np.uint8), debug_image) assert self._image_contains(image1_w_overlay, debug_image)
def test_augment_segmaps(self, mock_aug_segmaps): from imgaug.augmentables.segmaps import SegmentationMapsOnImage arr = np.int32([[1, 2, 3], [4, 5, 6]]) segmap = SegmentationMapsOnImage(arr, shape=(6, 6, 3)) rng = iarandom.RNG(0) aug = self.augmenter(2, keep_size=False, random_state=rng) _ = aug.augment_segmentation_maps(segmap) assert mock_aug_segmaps.call_count == 1 # call 0, args, arg 0, segmap 0 within segmaps list assert np.array_equal(mock_aug_segmaps.call_args_list[0][0][0][0].arr, segmap.arr)
def create_image_imgaug(cls, shape, dtype, seed, tile=None): rng = iarandom.RNG(1000 + seed) if dtype.startswith("uint"): image = rng.integers(0, 256, size=shape, dtype=dtype) else: assert dtype.startswith("float") image = rng.uniform(0.0, 1.0, size=shape) image = image.astype(dtype) if tile is not None: image = np.tile(image, tile) return image
def test_integrationtest(self): rng = iarandom.RNG(0) for size in [20, 100]: shape = (size, size, 3) image = rng.integers(50, 150, size=shape) image = image.astype(np.uint8) aug = iaa.pillike.Equalize() image_aug = aug(image=image) if size > 1: channelwise_sums = np.sum(image_aug, axis=(0, 1)) assert np.all(channelwise_sums > 0) assert np.min(image_aug) < 50 assert np.max(image_aug) > 150
def test_unusual_channel_numbers(self): nb_channels_lst = [1, 2, 4, 5, 512, 513] for nb_channels in nb_channels_lst: for size in [20, 100]: with self.subTest(nb_channels=nb_channels, size=size): shape = (size, size, nb_channels) image = iarandom.RNG(0).integers(50, 150, size=shape) image = image.astype(np.uint8) image_aug = iaa.pillike.equalize(image) if size > 1: channelwise_sums = np.sum(image_aug, axis=(0, 1)) assert np.all(channelwise_sums > 0) assert np.min(image_aug) < 50 assert np.max(image_aug) > 150
def _test_by_comparison_with_pil(self, func, pil_kernel): shapes = [(224, 224, 3), (32, 32, 3), (16, 8, 3), (1, 1, 3), (32, 32, 4)] seeds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for seed in seeds: for shape in shapes: with self.subTest(shape=shape, seed=seed): image = iarandom.RNG(seed).integers(0, 256, size=shape, dtype="uint8") image_iaa = func(image) image_pil = np.asarray( PIL.Image.fromarray(image).filter(pil_kernel)) assert np.array_equal(image_iaa, image_pil)
def _test_augmenter(cls, augmenter_name, func_expected, dependent_on_seed): # this test verifies: # - called function seems to be the expected function # - images produced by augmenter match images produced by function # - a different seed (and sometimes severity) will lead to a # different image # - augmenter can be pickled severity = 5 aug_cls = getattr(iaa.imgcorruptlike, augmenter_name) image = np.mod( np.arange(32*32*3), 256 ).reshape((32, 32, 3)).astype(np.uint8) with iap.no_prefetching(): rng = iarandom.RNG(1) # Replay sampling of severities. # Even for deterministic values this is required as currently # there is an advance() at the end of each draw_samples(). _ = iap.Deterministic(1).draw_samples((1,), rng) # As for the functions above, we can't just change the seed value # to get different augmentations as many functions are dependend # only on the severity. So we change only for some functions only # the seed and for the others severity+seed. image_aug1 = aug_cls(severity=severity, seed=1)(image=image) image_aug2 = aug_cls(severity=severity, seed=1)(image=image) if dependent_on_seed: image_aug3 = aug_cls(severity=severity, seed=2)( image=image) else: image_aug3 = aug_cls(severity=severity-1, seed=2)( image=image) image_aug_exp = func_expected( image, severity=severity, seed=rng.generate_seed_()) assert aug_cls(severity=severity).func is func_expected assert np.array_equal(image_aug1, image_aug_exp) assert np.array_equal(image_aug2, image_aug_exp) assert not np.array_equal(image_aug3, image_aug2) # pickling test aug = aug_cls(severity=(1, 5)) runtest_pickleable_uint8_img(aug, shape=(32, 32, 3))
def test_two_images_and_heatmaps(self): rng = iarandom.RNG(0) images = rng.integers(0, 256, size=(2, 256, 256, 3), dtype=np.uint8) heatmap = np.zeros((256, 256, 1), dtype=np.float32) heatmap[128 - 25:128 + 25, 128 - 25:128 + 25] = 1.0 heatmap1 = ia.HeatmapsOnImage(np.copy(heatmap), shape=images[0].shape) heatmap2 = ia.HeatmapsOnImage(1.0 - heatmap, shape=images[1].shape) image1_w_overlay = heatmap1.draw_on_image(images[0])[0] image2_w_overlay = heatmap2.draw_on_image(images[1])[0] debug_image = iaa.draw_debug_image(images, heatmaps=[heatmap1, heatmap2]) assert self._image_contains(images[0, ...], debug_image) assert self._image_contains(images[1, ...], debug_image) assert self._image_contains(image1_w_overlay, debug_image) assert self._image_contains(image2_w_overlay, debug_image)
def test_two_images_and_keypoints(self): rng = iarandom.RNG(0) images = rng.integers(0, 256, size=(2, 256, 256, 3), dtype=np.uint8) kps = [] for x in np.linspace(0, 256, 10): for y in np.linspace(0, 256, 10): kps.append(ia.Keypoint(x=x, y=y)) kpsoi1 = ia.KeypointsOnImage(kps, shape=images[0].shape) kpsoi2 = kpsoi1.shift(x=20) image1_w_overlay = kpsoi1.draw_on_image(images[0]) image2_w_overlay = kpsoi2.draw_on_image(images[1]) debug_image = iaa.draw_debug_image(images, keypoints=[kpsoi1, kpsoi2]) assert self._image_contains(images[0, ...], debug_image) assert self._image_contains(images[1, ...], debug_image) assert self._image_contains(image1_w_overlay, debug_image) assert self._image_contains(image2_w_overlay, debug_image)
def test_two_images_and_bounding_boxes(self): rng = iarandom.RNG(0) images = rng.integers(0, 256, size=(2, 256, 256, 3), dtype=np.uint8) bbs = [] for x in np.linspace(0, 256, 5): for y in np.linspace(0, 256, 5): bbs.append(ia.BoundingBox(x1=x, y1=y, x2=x + 20, y2=y + 20)) bbsoi1 = ia.BoundingBoxesOnImage(bbs, shape=images[0].shape) bbsoi2 = bbsoi1.shift(left=20) image1_w_overlay = bbsoi1.draw_on_image(images[0]) image2_w_overlay = bbsoi2.draw_on_image(images[1]) debug_image = iaa.draw_debug_image(images, bounding_boxes=[bbsoi1, bbsoi2]) assert self._image_contains(images[0, ...], debug_image) assert self._image_contains(images[1, ...], debug_image) assert self._image_contains(image1_w_overlay, debug_image) assert self._image_contains(image2_w_overlay, debug_image)
def __init__(self, n=2, m=(6, 12), cval=128, seed=None, name=None, random_state="deprecated", deterministic="deprecated"): # pylint: disable=invalid-name seed = seed if random_state == "deprecated" else random_state rng = iarandom.RNG(seed) # we don't limit the value range to 10 here, because the paper # gives several examples of using more than 10 for M m = iap.handle_discrete_param(m, "m", value_range=(0, None), tuple_to_uniform=True, list_to_choice=True, allow_floats=False) self._m = m self._cval = cval # The paper says in Appendix A.2.3 "ImageNet", that they actually # always execute Horizontal Flips and Crops first and only then a # random selection of the other transformations. # Hence, we split here into two groups. # It's not really clear what crop parameters they use, so we # choose [0..M] here. main_augs = self._create_main_augmenters_list(m, cval) # assign random state to all augmenters for augmenter in main_augs: augmenter.random_state = rng super(RandAugment, self).__init__([ meta.SomeOf( n, main_augs, random_order=True, seed=rng.derive_rng_()) ], seed=rng, name=name, random_state=random_state, deterministic=deterministic)
def test_colorize__one_channel(self): colorizer = iaa.RandomColorsBinaryImageColorizer( color_true=100, color_false=10) random_state = iarandom.RNG(42) # input image has shape (H,W,1) image = np.zeros((5, 5, 1), dtype=np.uint8) image[:, 0:3, :] = 255 image_binary = np.zeros((5, 5), dtype=bool) image_binary[:, 0:3] = True image_color = colorizer.colorize( image_binary, image, nth_image=0, random_state=random_state) assert image_color.ndim == 3 assert image_color.shape[-1] == 1 assert np.all(image_color[image_binary] == 100) assert np.all(image_color[~image_binary] == 10)