def test_AddToHueAndSaturation(): reseed() # interestingly, when using this RGB2HSV and HSV2RGB conversion from skimage, the results # differ quite a bit from the cv2 ones """ def _add_hue_saturation(img, value): img_hsv = color.rgb2hsv(img / 255.0) img_hsv[..., 0:2] += (value / 255.0) return color.hsv2rgb(img_hsv) * 255 """ def _add_hue_saturation(img, value): img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) img_hsv = img_hsv.astype(np.int32) img_hsv[..., 0] = np.mod(img_hsv[..., 0] + (value/255)*180, 180) img_hsv[..., 1] = np.clip(img_hsv[..., 1] + value, 0, 255) img_hsv = img_hsv.astype(np.uint8) return cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB) base_img = np.zeros((2, 2, 3), dtype=np.uint8) base_img[..., 0] += 20 base_img[..., 1] += 40 base_img[..., 2] += 60 aug = iaa.AddToHueAndSaturation(0) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.AddToHueAndSaturation(30) observed = aug.augment_image(base_img) expected = _add_hue_saturation(base_img, 30) diff = np.abs(observed.astype(np.float32) - expected) assert np.all(diff <= 3) aug = iaa.AddToHueAndSaturation([0, 10, 20]) base_img = base_img[0:1, 0:1, :] expected_imgs = [ iaa.AddToHueAndSaturation(0).augment_image(base_img), iaa.AddToHueAndSaturation(10).augment_image(base_img), iaa.AddToHueAndSaturation(20).augment_image(base_img) ] assert not np.array_equal(expected_imgs[0], expected_imgs[1]) assert not np.array_equal(expected_imgs[1], expected_imgs[2]) assert not np.array_equal(expected_imgs[0], expected_imgs[2]) nb_iterations = 300 seen = dict([(i, 0) for i, _ in enumerate(expected_imgs)]) for _ in sm.xrange(nb_iterations): observed = aug.augment_image(base_img) for i, expected_img in enumerate(expected_imgs): if np.allclose(observed, expected_img): seen[i] += 1 assert np.sum(list(seen.values())) == nb_iterations n_exp = nb_iterations / 3 n_exp_tol = nb_iterations * 0.1 assert all([n_exp - n_exp_tol < v < n_exp + n_exp_tol for v in seen.values()])
def test_Grayscale(): reseed() def _compute_luminosity(r, g, b): return 0.21 * r + 0.72 * g + 0.07 * b base_img = np.zeros((4, 4, 3), dtype=np.uint8) base_img[..., 0] += 10 base_img[..., 1] += 20 base_img[..., 2] += 30 aug = iaa.Grayscale(0.0) observed = aug.augment_image(base_img) expected = np.copy(base_img) assert np.allclose(observed, expected) aug = iaa.Grayscale(1.0) observed = aug.augment_image(base_img) luminosity = _compute_luminosity(10, 20, 30) expected = np.zeros_like(base_img) + luminosity assert np.allclose(observed, expected.astype(np.uint8)) aug = iaa.Grayscale(0.5) observed = aug.augment_image(base_img) luminosity = _compute_luminosity(10, 20, 30) expected = 0.5 * base_img + 0.5 * luminosity assert np.allclose(observed, expected.astype(np.uint8)) aug = iaa.Grayscale((0.0, 1.0)) base_img = np.uint8([255, 0, 0]).reshape((1, 1, 3)) base_img_float = base_img.astype(np.float64) / 255.0 base_img_gray = iaa.Grayscale(1.0).augment_image(base_img).astype( np.float64) / 255.0 distance_max = np.linalg.norm(base_img_gray.flatten() - base_img_float.flatten()) nb_iterations = 1000 distances = [] for _ in sm.xrange(nb_iterations): observed = aug.augment_image(base_img).astype(np.float64) / 255.0 distance = np.linalg.norm(observed.flatten() - base_img_float.flatten()) / distance_max distances.append(distance) assert 0 - 1e-4 < min(distances) < 0.1 assert 0.4 < np.average(distances) < 0.6 assert 0.9 < max(distances) < 1.0 + 1e-4 nb_bins = 5 hist, _ = np.histogram(distances, 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
def test_BackgroundAugmenter__augment_images_worker(): reseed() warnings.simplefilter("always") with warnings.catch_warnings(record=True) as caught_warnings: def gen(): yield ia.Batch(images=np.zeros((1, 4, 4, 3), dtype=np.uint8)) bl = multicore.BatchLoader(gen(), queue_size=2) bgaug = multicore.BackgroundAugmenter(bl, iaa.Noop(), queue_size=1, nb_workers=1) queue_source = multiprocessing.Queue(2) queue_target = multiprocessing.Queue(2) queue_source.put( pickle.dumps( ia.Batch(images=np.zeros((1, 4, 8, 3), dtype=np.uint8)), protocol=-1)) queue_source.put(pickle.dumps(None, protocol=-1)) bgaug._augment_images_worker(iaa.Add(1), queue_source, queue_target, 1) batch_aug = pickle.loads(queue_target.get()) assert isinstance(batch_aug, ia.Batch) assert batch_aug.images_unaug is not None assert batch_aug.images_unaug.dtype == np.uint8 assert batch_aug.images_unaug.shape == (1, 4, 8, 3) assert np.array_equal(batch_aug.images_unaug, np.zeros((1, 4, 8, 3), dtype=np.uint8)) assert batch_aug.images_aug is not None assert batch_aug.images_aug.dtype == np.uint8 assert batch_aug.images_aug.shape == (1, 4, 8, 3) assert np.array_equal(batch_aug.images_aug, np.zeros((1, 4, 8, 3), dtype=np.uint8) + 1) finished_signal = pickle.loads(queue_target.get()) assert finished_signal is None source_finished_signal = pickle.loads(queue_source.get()) assert source_finished_signal is None assert queue_source.empty() assert queue_target.empty() queue_source.close() queue_target.close() queue_source.join_thread() queue_target.join_thread() bl.terminate() bgaug.terminate() assert len(caught_warnings) > 0 for warning in caught_warnings: assert ("BatchLoader is deprecated" in str(warning.message) or "BackgroundAugmenter is deprecated" in str(warning.message))
def test_BatchLoader(): reseed() def _load_func(): for _ in sm.xrange(20): yield ia.Batch(images=np.zeros((2, 4, 4, 3), dtype=np.uint8)) warnings.simplefilter("always") with warnings.catch_warnings(record=True) as caught_warnings: for nb_workers in [1, 2]: # repeat these tests many times to catch rarer race conditions for _ in sm.xrange(5): loader = multicore.BatchLoader(_load_func, queue_size=2, nb_workers=nb_workers, threaded=True) loaded = [] counter = 0 while (not loader.all_finished() or not loader.queue.empty()) and counter < 1000: try: batch = loader.queue.get(timeout=0.001) loaded.append(batch) except: pass counter += 1 assert len(loaded) == 20*nb_workers, \ "Expected %d to be loaded by threads, got %d for %d workers at counter %d." % ( 20*nb_workers, len(loaded), nb_workers, counter ) loader = multicore.BatchLoader(_load_func, queue_size=200, nb_workers=nb_workers, threaded=True) loader.terminate() assert loader.all_finished() loader = multicore.BatchLoader(_load_func, queue_size=2, nb_workers=nb_workers, threaded=False) loaded = [] counter = 0 while (not loader.all_finished() or not loader.queue.empty()) and counter < 1000: try: batch = loader.queue.get(timeout=0.001) loaded.append(batch) except: pass counter += 1 assert len(loaded) == 20*nb_workers, \ "Expected %d to be loaded by background processes, got %d for %d workers at counter %d." % ( 20*nb_workers, len(loaded), nb_workers, counter ) loader = multicore.BatchLoader(_load_func, queue_size=200, nb_workers=nb_workers, threaded=False) loader.terminate() assert loader.all_finished() assert len(caught_warnings) > 0 for warning in caught_warnings: assert "is deprecated" in str(warning.message)
def test_BackgroundAugmenter__augment_images_worker(): reseed() warnings.simplefilter("always") with warnings.catch_warnings(record=True) as caught_warnings: def gen(): yield ia.Batch(images=np.zeros((1, 4, 4, 3), dtype=np.uint8)) bl = multicore.BatchLoader(gen(), queue_size=2) bgaug = multicore.BackgroundAugmenter(bl, iaa.Noop(), queue_size=1, nb_workers=1) queue_source = multiprocessing.Queue(2) queue_target = multiprocessing.Queue(2) queue_source.put( pickle.dumps( ia.Batch(images=np.zeros((1, 4, 8, 3), dtype=np.uint8)), protocol=-1 ) ) queue_source.put(pickle.dumps(None, protocol=-1)) bgaug._augment_images_worker(iaa.Add(1), queue_source, queue_target, 1) batch_aug = pickle.loads(queue_target.get()) assert isinstance(batch_aug, ia.Batch) assert batch_aug.images_unaug is not None assert batch_aug.images_unaug.dtype == np.uint8 assert batch_aug.images_unaug.shape == (1, 4, 8, 3) assert np.array_equal(batch_aug.images_unaug, np.zeros((1, 4, 8, 3), dtype=np.uint8)) assert batch_aug.images_aug is not None assert batch_aug.images_aug.dtype == np.uint8 assert batch_aug.images_aug.shape == (1, 4, 8, 3) assert np.array_equal(batch_aug.images_aug, np.zeros((1, 4, 8, 3), dtype=np.uint8) + 1) finished_signal = pickle.loads(queue_target.get()) assert finished_signal is None source_finished_signal = pickle.loads(queue_source.get()) assert source_finished_signal is None assert queue_source.empty() assert queue_target.empty() queue_source.close() queue_target.close() queue_source.join_thread() queue_target.join_thread() bl.terminate() bgaug.terminate() assert len(caught_warnings) > 0 for warning in caught_warnings: assert "is deprecated" in str(warning.message)
def test_Snowflakes(): # rather rough test as fairly hard to test more detailed reseed() img = np.zeros((100, 100, 3), dtype=np.uint8) img_aug = iaa.Snowflakes().augment_image(img) assert 0.01 < np.average(img_aug) < 100 assert np.max(img_aug) > 100 grad_x = img_aug[:, 1:].astype(np.float32) - img_aug[:, :-1].astype( np.float32) grad_y = img_aug[1:, :].astype(np.float32) - img_aug[:-1, :].astype( np.float32) assert np.sum(np.abs(grad_x)) > 5 * img.shape[1] assert np.sum(np.abs(grad_y)) > 5 * img.shape[0] # test density img_aug_undense = iaa.Snowflakes( density=0.001, density_uniformity=0.99).augment_image(img) img_aug_dense = iaa.Snowflakes(density=0.1, density_uniformity=0.99).augment_image(img) assert np.average(img_aug_undense) < np.average(img_aug_dense) # test density_uniformity img_aug_ununiform = iaa.Snowflakes( density=0.4, density_uniformity=0.1).augment_image(img) img_aug_uniform = iaa.Snowflakes(density=0.4, density_uniformity=0.9).augment_image(img) def _measure_uniformity(image, patch_size=5, n_patches=100): pshalf = (patch_size - 1) // 2 grad_x = image[:, 1:].astype(np.float32) - image[:, :-1].astype( np.float32) grad_y = image[1:, :].astype(np.float32) - image[:-1, :].astype( np.float32) grad = np.abs(grad_x[1:, :] + grad_y[:, 1:]) points_y = np.random.randint(0, image.shape[0], size=(n_patches, )) points_x = np.random.randint(0, image.shape[0], size=(n_patches, )) stds = [] for y, x in zip(points_y, points_x): bb = ia.BoundingBox(x1=x - pshalf, y1=y - pshalf, x2=x + pshalf, y2=y + pshalf) patch = bb.extract_from_image(grad) stds.append(np.std(patch)) return 1 / (1 + np.std(stds)) assert _measure_uniformity(img_aug_ununiform) < _measure_uniformity( img_aug_uniform)
def test_Fog(): # rough test as fairly hard to test more detailed reseed() img = np.zeros((100, 100, 3), dtype=np.uint8) img_aug = iaa.Clouds().augment_image(img) assert 50 < np.average(img_aug) < 255 assert np.max(img_aug) > 100 grad_x = img_aug[:, 1:].astype(np.float32) - img_aug[:, :-1].astype(np.float32) grad_y = img_aug[1:, :].astype(np.float32) - img_aug[:-1, :].astype(np.float32) assert np.sum(np.abs(grad_x)) > 1 * img.shape[1] assert np.sum(np.abs(grad_y)) > 1 * img.shape[0]
def test_Snowflakes(): # rather rough test as fairly hard to test more detailed reseed() img = np.zeros((100, 100, 3), dtype=np.uint8) img_aug = iaa.Snowflakes().augment_image(img) assert 0.01 < np.average(img_aug) < 100 assert np.max(img_aug) > 100 grad_x = img_aug[:, 1:].astype(np.float32) - img_aug[:, :-1].astype(np.float32) grad_y = img_aug[1:, :].astype(np.float32) - img_aug[:-1, :].astype(np.float32) assert np.sum(np.abs(grad_x)) > 5 * img.shape[1] assert np.sum(np.abs(grad_y)) > 5 * img.shape[0] # test density img_aug_undense = iaa.Snowflakes(density=0.001, density_uniformity=0.99).augment_image(img) img_aug_dense = iaa.Snowflakes(density=0.1, density_uniformity=0.99).augment_image(img) assert np.average(img_aug_undense) < np.average(img_aug_dense) # test density_uniformity img_aug_ununiform = iaa.Snowflakes(density=0.4, density_uniformity=0.1).augment_image(img) img_aug_uniform = iaa.Snowflakes(density=0.4, density_uniformity=0.9).augment_image(img) def _measure_uniformity(image, patch_size=5, n_patches=100): pshalf = (patch_size-1) // 2 grad_x = image[:, 1:].astype(np.float32) - image[:, :-1].astype(np.float32) grad_y = image[1:, :].astype(np.float32) - image[:-1, :].astype(np.float32) grad = np.abs(grad_x[1:, :] + grad_y[:, 1:]) points_y = np.random.randint(0, image.shape[0], size=(n_patches,)) points_x = np.random.randint(0, image.shape[0], size=(n_patches,)) stds = [] for y, x in zip(points_y, points_x): bb = ia.BoundingBox(x1=x-pshalf, y1=y-pshalf, x2=x+pshalf, y2=y+pshalf) patch = bb.extract_from_image(grad) stds.append(np.std(patch)) return 1 / (1+np.std(stds)) assert _measure_uniformity(img_aug_ununiform) < _measure_uniformity(img_aug_uniform)
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_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_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_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_BatchLoader(): reseed() def _load_func(): for _ in sm.xrange(20): yield ia.Batch(images=np.zeros((2, 4, 4, 3), dtype=np.uint8)) warnings.simplefilter("always") with warnings.catch_warnings(record=True) as caught_warnings: for nb_workers in [1, 2]: # repeat these tests many times to catch rarer race conditions for _ in sm.xrange(5): loader = multicore.BatchLoader(_load_func, queue_size=2, nb_workers=nb_workers, threaded=True) loaded = [] counter = 0 while (not loader.all_finished() or not loader.queue.empty()) and counter < 1000: try: batch = loader.queue.get(timeout=0.001) loaded.append(batch) except: pass counter += 1 assert len(loaded) == 20*nb_workers, \ "Expected %d to be loaded by threads, got %d for %d workers at counter %d." % ( 20*nb_workers, len(loaded), nb_workers, counter ) loader = multicore.BatchLoader(_load_func, queue_size=200, nb_workers=nb_workers, threaded=True) loader.terminate() assert loader.all_finished() loader = multicore.BatchLoader(_load_func, queue_size=2, nb_workers=nb_workers, threaded=False) loaded = [] counter = 0 while (not loader.all_finished() or not loader.queue.empty()) and counter < 1000: try: batch = loader.queue.get(timeout=0.001) loaded.append(batch) except: pass counter += 1 assert len(loaded) == 20*nb_workers, \ "Expected %d to be loaded by background processes, got %d for %d workers at counter %d." % ( 20*nb_workers, len(loaded), nb_workers, counter ) loader = multicore.BatchLoader(_load_func, queue_size=200, nb_workers=nb_workers, threaded=False) loader.terminate() assert loader.all_finished() assert len(caught_warnings) > 0 for warning in caught_warnings: assert "BatchLoader is deprecated" in str(warning.message)
def test_MotionBlur(): reseed() # simple scenario aug = iaa.MotionBlur(k=3, angle=0, direction=0.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(10) ] expected = np.float32([[0, 1.0 / 3, 0], [0, 1.0 / 3, 0], [0, 1.0 / 3, 0]]) for matrices_image in matrices: for matrix_channel in matrices_image: assert np.allclose(matrix_channel, expected) # 90deg angle aug = iaa.MotionBlur(k=3, angle=90, direction=0.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(10) ] expected = np.float32([[0, 0, 0], [1.0 / 3, 1.0 / 3, 1.0 / 3], [0, 0, 0]]) for matrices_image in matrices: for matrix_channel in matrices_image: assert np.allclose(matrix_channel, expected) # 45deg angle aug = iaa.MotionBlur(k=3, angle=45, direction=0.0, order=0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(10) ] expected = np.float32([[0, 0, 1.0 / 3], [0, 1.0 / 3, 0], [1.0 / 3, 0, 0]]) for matrices_image in matrices: for matrix_channel in matrices_image: assert np.allclose(matrix_channel, expected) # random angle aug = iaa.MotionBlur(k=3, angle=[0, 90], direction=0.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(50) ] expected1 = np.float32([[0, 1.0 / 3, 0], [0, 1.0 / 3, 0], [0, 1.0 / 3, 0]]) expected2 = np.float32([ [0, 0, 0], [1.0 / 3, 1.0 / 3, 1.0 / 3], [0, 0, 0], ]) nb_seen = [0, 0] for matrices_image in matrices: assert np.allclose(matrices_image[0], matrices_image[1]) assert np.allclose(matrices_image[1], matrices_image[2]) for matrix_channel in matrices_image: if np.allclose(matrix_channel, expected1): nb_seen[0] += 1 elif np.allclose(matrix_channel, expected2): nb_seen[1] += 1 assert nb_seen[0] > 0 assert nb_seen[1] > 0 # 5x5 aug = iaa.MotionBlur(k=5, angle=90, direction=0.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(10) ] expected = np.float32([ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ]) for matrices_image in matrices: for matrix_channel in matrices_image: assert np.allclose(matrix_channel, expected) # random k aug = iaa.MotionBlur(k=[3, 5], angle=90, direction=0.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(50) ] expected1 = np.float32([ [0, 0, 0], [1.0 / 3, 1.0 / 3, 1.0 / 3], [0, 0, 0], ]) expected2 = np.float32([ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ]) nb_seen = [0, 0] for matrices_image in matrices: assert np.allclose(matrices_image[0], matrices_image[1]) assert np.allclose(matrices_image[1], matrices_image[2]) for matrix_channel in matrices_image: if matrix_channel.shape == expected1.shape and np.allclose( matrix_channel, expected1): nb_seen[0] += 1 elif matrix_channel.shape == expected2.shape and np.allclose( matrix_channel, expected2): nb_seen[1] += 1 assert nb_seen[0] > 0 assert nb_seen[1] > 0 # direction 1.0 aug = iaa.MotionBlur(k=3, angle=0, direction=1.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(10) ] expected = np.float32([[0, 1.0 / 1.5, 0], [0, 0.5 / 1.5, 0], [0, 0.0 / 1.5, 0]]) for matrices_image in matrices: for matrix_channel in matrices_image: assert np.allclose(matrix_channel, expected, rtol=0, atol=1e-2) # direction -1.0 aug = iaa.MotionBlur(k=3, angle=0, direction=-1.0) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(10) ] expected = np.float32([[0, 0.0 / 1.5, 0], [0, 0.5 / 1.5, 0], [0, 1.0 / 1.5, 0]]) for matrices_image in matrices: for matrix_channel in matrices_image: assert np.allclose(matrix_channel, expected, rtol=0, atol=1e-2) # random direction aug = iaa.MotionBlur(k=3, angle=[0, 90], direction=[-1.0, 1.0]) matrix_func = aug.matrix matrices = [ matrix_func(np.zeros((128, 128, 3), dtype=np.uint8), 3, ia.new_random_state(i)) for i in range(50) ] expected1 = np.float32([[0, 1.0 / 1.5, 0], [0, 0.5 / 1.5, 0], [0, 0.0 / 1.5, 0]]) expected2 = np.float32([[0, 0.0 / 1.5, 0], [0, 0.5 / 1.5, 0], [0, 1.0 / 1.5, 0]]) nb_seen = [0, 0] for matrices_image in matrices: assert np.allclose(matrices_image[0], matrices_image[1]) assert np.allclose(matrices_image[1], matrices_image[2]) for matrix_channel in matrices_image: if np.allclose(matrix_channel, expected1, rtol=0, atol=1e-2): nb_seen[0] += 1 elif np.allclose(matrix_channel, expected2, rtol=0, atol=1e-2): nb_seen[1] += 1 assert nb_seen[0] > 0 assert nb_seen[1] > 0 # test of actual augmenter img = np.zeros((7, 7, 3), dtype=np.uint8) img[3 - 1:3 + 2, 3 - 1:3 + 2, :] = 255 aug = iaa.MotionBlur(k=3, angle=90, direction=0.0) img_aug = aug.augment_image(img) v1 = (255 * (1 / 3)) v2 = (255 * (1 / 3)) * 2 v3 = (255 * (1 / 3)) * 3 expected = np.float32([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, v1, v2, v3, v2, v1, 0], [0, v1, v2, v3, v2, v1, 0], [0, v1, v2, v3, v2, v1, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]).astype(np.uint8) expected = np.tile(expected[..., np.newaxis], (1, 1, 3)) assert np.allclose(img_aug, expected)
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_FastSnowyLandscape(): reseed() # check parameters aug = iaa.FastSnowyLandscape(lightness_threshold=[100, 200], lightness_multiplier=[1.0, 4.0]) assert isinstance(aug.lightness_threshold, iap.Choice) assert len(aug.lightness_threshold.a) == 2 assert aug.lightness_threshold.a[0] == 100 assert aug.lightness_threshold.a[1] == 200 assert isinstance(aug.lightness_multiplier, iap.Choice) assert len(aug.lightness_multiplier.a) == 2 assert np.allclose(aug.lightness_multiplier.a[0], 1.0) assert np.allclose(aug.lightness_multiplier.a[1], 4.0) # basic functionality test aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0) image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(np.round(expected), 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2RGB) observed = aug.augment_image(image) assert np.array_equal(observed, expected) # test when varying lightness_threshold between images image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) class _TwoValueParam(iap.StochasticParameter): def __init__(self, v1, v2): super(_TwoValueParam, self).__init__() self.v1 = v1 self.v2 = v2 def _draw_samples(self, size, random_state): arr = np.full(size, self.v1, dtype=np.float32) arr[1::2] = self.v2 return arr aug = iaa.FastSnowyLandscape(lightness_threshold=_TwoValueParam(75, 125), lightness_multiplier=2.0) mask = (image_hls[..., 1] < 75) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 2.0 expected1 = np.clip(np.round(expected1), 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 125) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(np.round(expected2), 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2) # test when varying lightness_multiplier between images aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=_TwoValueParam(1.5, 2.0)) mask = (image_hls[..., 1] < 100) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 1.5 expected1 = np.clip(np.round(expected1), 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 100) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(np.round(expected2), 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2) # test BGR colorspace aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0, from_colorspace="BGR") image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(np.round(expected), 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2BGR) observed = aug.augment_image(image) assert np.array_equal(observed, expected)
def test_BoundingBoxesOnImage(): reseed() # test height/width bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) assert bbsoi.height == 40 assert bbsoi.width == 50 bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=np.zeros((40, 50, 3), dtype=np.uint8)) assert bbsoi.height == 40 assert bbsoi.width == 50 # empty bb = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bbsoi = ia.BoundingBoxesOnImage([bb], shape=(40, 50, 3)) assert not bbsoi.empty bbsoi = ia.BoundingBoxesOnImage([], shape=(40, 50, 3)) assert bbsoi.empty # on() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=np.zeros((40, 50, 3), dtype=np.uint8)) bbsoi_projected = bbsoi.on((40, 50)) assert bbsoi_projected.bounding_boxes[0].y1 == 10 assert bbsoi_projected.bounding_boxes[0].x1 == 20 assert bbsoi_projected.bounding_boxes[0].y2 == 30 assert bbsoi_projected.bounding_boxes[0].x2 == 40 assert bbsoi_projected.bounding_boxes[1].y1 == 15 assert bbsoi_projected.bounding_boxes[1].x1 == 25 assert bbsoi_projected.bounding_boxes[1].y2 == 35 assert bbsoi_projected.bounding_boxes[1].x2 == 45 bbsoi_projected = bbsoi.on((40 * 2, 50 * 2, 3)) assert bbsoi_projected.bounding_boxes[0].y1 == 10 * 2 assert bbsoi_projected.bounding_boxes[0].x1 == 20 * 2 assert bbsoi_projected.bounding_boxes[0].y2 == 30 * 2 assert bbsoi_projected.bounding_boxes[0].x2 == 40 * 2 assert bbsoi_projected.bounding_boxes[1].y1 == 15 * 2 assert bbsoi_projected.bounding_boxes[1].x1 == 25 * 2 assert bbsoi_projected.bounding_boxes[1].y2 == 35 * 2 assert bbsoi_projected.bounding_boxes[1].x2 == 45 * 2 bbsoi_projected = bbsoi.on(np.zeros((40 * 2, 50 * 2, 3), dtype=np.uint8)) assert bbsoi_projected.bounding_boxes[0].y1 == 10 * 2 assert bbsoi_projected.bounding_boxes[0].x1 == 20 * 2 assert bbsoi_projected.bounding_boxes[0].y2 == 30 * 2 assert bbsoi_projected.bounding_boxes[0].x2 == 40 * 2 assert bbsoi_projected.bounding_boxes[1].y1 == 15 * 2 assert bbsoi_projected.bounding_boxes[1].x1 == 25 * 2 assert bbsoi_projected.bounding_boxes[1].y2 == 35 * 2 assert bbsoi_projected.bounding_boxes[1].x2 == 45 * 2 # from_xyxy_array() bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array(np.float32( [[0.0, 0.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]), shape=(40, 50, 3)) assert len(bbsoi.bounding_boxes) == 2 assert np.allclose(bbsoi.bounding_boxes[0].x1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].y1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].x2, 1.0) assert np.allclose(bbsoi.bounding_boxes[0].y2, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].x1, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].y1, 2.0) assert np.allclose(bbsoi.bounding_boxes[1].x2, 3.0) assert np.allclose(bbsoi.bounding_boxes[1].y2, 4.0) assert bbsoi.shape == (40, 50, 3) bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array(np.int32([[0, 0, 1, 1], [1, 2, 3, 4]]), shape=(40, 50, 3)) assert len(bbsoi.bounding_boxes) == 2 assert np.allclose(bbsoi.bounding_boxes[0].x1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].y1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].x2, 1.0) assert np.allclose(bbsoi.bounding_boxes[0].y2, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].x1, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].y1, 2.0) assert np.allclose(bbsoi.bounding_boxes[1].x2, 3.0) assert np.allclose(bbsoi.bounding_boxes[1].y2, 4.0) assert bbsoi.shape == (40, 50, 3) bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array(np.zeros((0, 4), dtype=np.float32), shape=(40, 50, 3)) assert len(bbsoi.bounding_boxes) == 0 assert bbsoi.shape == (40, 50, 3) # to_xyxy_array() xyxy_arr = np.float32([[0.0, 0.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0]]) bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array(xyxy_arr, shape=(40, 50, 3)) xyxy_arr_out = bbsoi.to_xyxy_array() assert np.allclose(xyxy_arr, xyxy_arr_out) assert xyxy_arr_out.dtype == np.float32 xyxy_arr_out = bbsoi.to_xyxy_array(dtype=np.int32) assert np.allclose(xyxy_arr.astype(np.int32), xyxy_arr_out) assert xyxy_arr_out.dtype == np.int32 xyxy_arr_out = ia.BoundingBoxesOnImage( [], shape=(40, 50, 3)).to_xyxy_array(dtype=np.int32) assert xyxy_arr_out.shape == (0, 4) # draw_on_image() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) image = bbsoi.draw_on_image(np.zeros(bbsoi.shape, dtype=np.uint8), color=[0, 255, 0], alpha=1.0, thickness=1, copy=True, raise_if_out_of_image=False) assert np.all(image[10 - 1, 20 - 1, :] == [0, 0, 0]) assert np.all(image[10 - 1, 20 - 0, :] == [0, 0, 0]) assert np.all(image[10 - 0, 20 - 1, :] == [0, 0, 0]) assert np.all(image[10 - 0, 20 - 0, :] == [0, 255, 0]) assert np.all(image[10 + 1, 20 + 1, :] == [0, 0, 0]) assert np.all(image[30 - 1, 40 - 1, :] == [0, 0, 0]) assert np.all(image[30 + 1, 40 - 0, :] == [0, 0, 0]) assert np.all(image[30 + 0, 40 + 1, :] == [0, 0, 0]) assert np.all(image[30 + 0, 40 + 0, :] == [0, 255, 0]) assert np.all(image[30 + 1, 40 + 1, :] == [0, 0, 0]) assert np.all(image[15 - 1, 25 - 1, :] == [0, 0, 0]) assert np.all(image[15 - 1, 25 - 0, :] == [0, 0, 0]) assert np.all(image[15 - 0, 25 - 1, :] == [0, 0, 0]) assert np.all(image[15 - 0, 25 - 0, :] == [0, 255, 0]) assert np.all(image[15 + 1, 25 + 1, :] == [0, 0, 0]) assert np.all(image[35 - 1, 45 - 1, :] == [0, 0, 0]) assert np.all(image[35 + 1, 45 + 0, :] == [0, 0, 0]) assert np.all(image[35 + 0, 45 + 1, :] == [0, 0, 0]) assert np.all(image[35 + 0, 45 + 0, :] == [0, 255, 0]) assert np.all(image[35 + 1, 45 + 1, :] == [0, 0, 0]) # remove_out_of_image() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_slim = bbsoi.remove_out_of_image(fully=True, partly=True) assert len(bbsoi_slim.bounding_boxes) == 1 assert bbsoi_slim.bounding_boxes[0] == bb1 # clip_out_of_image() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) eps = np.finfo(np.float32).eps bbsoi_clip = bbsoi.clip_out_of_image() assert len(bbsoi_clip.bounding_boxes) == 2 assert bbsoi_clip.bounding_boxes[0].y1 == 10 assert bbsoi_clip.bounding_boxes[0].x1 == 20 assert bbsoi_clip.bounding_boxes[0].y2 == 30 assert bbsoi_clip.bounding_boxes[0].x2 == 40 assert bbsoi_clip.bounding_boxes[1].y1 == 15 assert bbsoi_clip.bounding_boxes[1].x1 == 25 assert bbsoi_clip.bounding_boxes[1].y2 == 35 assert 50 - 2 * eps < bbsoi_clip.bounding_boxes[1].x2 < 50 # shift() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_shifted = bbsoi.shift(right=1) assert len(bbsoi_shifted.bounding_boxes) == 2 assert bbsoi_shifted.bounding_boxes[0].y1 == 10 assert bbsoi_shifted.bounding_boxes[0].x1 == 20 - 1 assert bbsoi_shifted.bounding_boxes[0].y2 == 30 assert bbsoi_shifted.bounding_boxes[0].x2 == 40 - 1 assert bbsoi_shifted.bounding_boxes[1].y1 == 15 assert bbsoi_shifted.bounding_boxes[1].x1 == 25 - 1 assert bbsoi_shifted.bounding_boxes[1].y2 == 35 assert bbsoi_shifted.bounding_boxes[1].x2 == 51 - 1 # copy() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_copy = bbsoi.copy() assert len(bbsoi.bounding_boxes) == 2 assert bbsoi_copy.bounding_boxes[0].y1 == 10 assert bbsoi_copy.bounding_boxes[0].x1 == 20 assert bbsoi_copy.bounding_boxes[0].y2 == 30 assert bbsoi_copy.bounding_boxes[0].x2 == 40 assert bbsoi_copy.bounding_boxes[1].y1 == 15 assert bbsoi_copy.bounding_boxes[1].x1 == 25 assert bbsoi_copy.bounding_boxes[1].y2 == 35 assert bbsoi_copy.bounding_boxes[1].x2 == 51 bbsoi.bounding_boxes[0].y1 = 0 assert bbsoi_copy.bounding_boxes[0].y1 == 0 # deepcopy() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_copy = bbsoi.deepcopy() assert len(bbsoi.bounding_boxes) == 2 assert bbsoi_copy.bounding_boxes[0].y1 == 10 assert bbsoi_copy.bounding_boxes[0].x1 == 20 assert bbsoi_copy.bounding_boxes[0].y2 == 30 assert bbsoi_copy.bounding_boxes[0].x2 == 40 assert bbsoi_copy.bounding_boxes[1].y1 == 15 assert bbsoi_copy.bounding_boxes[1].x1 == 25 assert bbsoi_copy.bounding_boxes[1].y2 == 35 assert bbsoi_copy.bounding_boxes[1].x2 == 51 bbsoi.bounding_boxes[0].y1 = 0 assert bbsoi_copy.bounding_boxes[0].y1 == 10 # repr() / str() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bb1_expected = "BoundingBox(x1=20.0000, y1=10.0000, x2=40.0000, y2=30.0000, label=None)" bb2_expected = "BoundingBox(x1=25.0000, y1=15.0000, x2=51.0000, y2=35.0000, label=None)" expected = "BoundingBoxesOnImage([%s, %s], shape=(40, 50, 3))" % ( bb1_expected, bb2_expected) assert bbsoi.__repr__() == bbsoi.__str__() == expected
def test_Superpixels(): reseed() def _array_equals_tolerant(a, b, tolerance): diff = np.abs(a.astype(np.int32) - b.astype(np.int32)) return np.all(diff <= tolerance) base_img = [[255, 255, 255, 0, 0, 0], [255, 235, 255, 0, 20, 0], [250, 250, 250, 5, 5, 5]] base_img = np.tile( np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels = [[251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4]] base_img_superpixels = np.tile( np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels_left = np.copy(base_img_superpixels) base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :] base_img_superpixels_right = np.copy(base_img_superpixels) base_img_superpixels_right[:, :3, :] = base_img[:, :3, :] aug = iaa.Superpixels(p_replace=0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Superpixels(p_replace=1.0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2)) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(base_img) assert np.allclose(observed, base_img) or _array_equals_tolerant( observed, base_img_superpixels, 2) aug = iaa.Superpixels(p_replace=0.5, n_segments=2) seen = {"none": False, "left": False, "right": False, "both": False} for _ in sm.xrange(100): observed = aug.augment_image(base_img) if _array_equals_tolerant(observed, base_img, 2): seen["none"] = True elif _array_equals_tolerant(observed, base_img_superpixels_left, 2): seen["left"] = True elif _array_equals_tolerant(observed, base_img_superpixels_right, 2): seen["right"] = True elif _array_equals_tolerant(observed, base_img_superpixels, 2): seen["both"] = True else: raise Exception( "Generated superpixels image does not match any expected image." ) if all(seen.values()): break assert all(seen.values()) # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Superpixels(p_replace="test", n_segments=100) except Exception: got_exception = True assert got_exception got_exception = False try: _ = iaa.Superpixels(p_replace=1, n_segments="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Superpixels(p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest") params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 assert params[1].value == 2 assert params[2] == 100 assert params[3] == "nearest" ################### # test other dtypes ################### # bool aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([[False, False, True, True], [False, False, True, True]], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug == img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([[True, True, True, True], [False, True, True, True]], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug) 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) if np.dtype(dtype).kind == "i": values = [ int(center_value), int(0.1 * max_value), int(0.2 * max_value), int(0.5 * max_value), max_value - 100 ] values = [((-1) * value, value) for value in values] else: values = [(0, int(center_value)), (10, int(0.1 * max_value)), (10, int(0.2 * max_value)), (10, int(0.5 * max_value)), (0, max_value), (int(center_value), max_value)] for v1, v2 in values: aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([[v1, v1, v2, v2], [v1, v1, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.array_equal(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([[v2, v2, v2, v2], [v1, v2, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.all(img_aug == int(np.round((7 / 8) * v2 + (1 / 8) * v1))) for dtype in []: def _allclose(a, b): atol = 1e-4 if dtype == np.float16 else 1e-8 return np.allclose(a, b, atol=atol, rtol=0) isize = np.dtype(dtype).itemsize for value in [0, 1.0, 10.0, 1000**(isize - 1)]: v1 = (-1) * value v2 = value aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([[v1, v1, v2, v2], [v1, v1, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([[v2, v2, v2, v2], [v1, v2, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, (7 / 8) * v2 + (1 / 8) * v1)
def test_Superpixels(): reseed() def _array_equals_tolerant(a, b, tolerance): diff = np.abs(a.astype(np.int32) - b.astype(np.int32)) return np.all(diff <= tolerance) base_img = [[255, 255, 255, 0, 0, 0], [255, 235, 255, 0, 20, 0], [250, 250, 250, 5, 5, 5]] base_img = np.tile( np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels = [[251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4]] base_img_superpixels = np.tile( np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels_left = np.copy(base_img_superpixels) base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :] base_img_superpixels_right = np.copy(base_img_superpixels) base_img_superpixels_right[:, :3, :] = base_img[:, :3, :] aug = iaa.Superpixels(p_replace=0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Superpixels(p_replace=1.0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2)) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(base_img) assert np.allclose(observed, base_img) or _array_equals_tolerant( observed, base_img_superpixels, 2) aug = iaa.Superpixels(p_replace=0.5, n_segments=2) seen = {"none": False, "left": False, "right": False, "both": False} for _ in sm.xrange(100): observed = aug.augment_image(base_img) if _array_equals_tolerant(observed, base_img, 2): seen["none"] = True elif _array_equals_tolerant(observed, base_img_superpixels_left, 2): seen["left"] = True elif _array_equals_tolerant(observed, base_img_superpixels_right, 2): seen["right"] = True elif _array_equals_tolerant(observed, base_img_superpixels, 2): seen["both"] = True else: raise Exception( "Generated superpixels image does not match any expected image." ) if all(seen.values()): break assert all(seen.values()) # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Superpixels(p_replace="test", n_segments=100) except Exception: got_exception = True assert got_exception got_exception = False try: _ = iaa.Superpixels(p_replace=1, n_segments="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Superpixels(p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest") params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 assert params[1].value == 2 assert params[2] == 100 assert params[3] == "nearest"
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_Convolve(): reseed() img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] img = np.uint8(img) # matrix is None aug = iaa.Convolve(matrix=None) observed = aug.augment_image(img) assert np.array_equal(observed, img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: [None]) observed = aug.augment_image(img) assert np.array_equal(observed, img) # matrix is [[1]] aug = iaa.Convolve(matrix=np.float32([[1]])) observed = aug.augment_image(img) assert np.array_equal(observed, img) aug = iaa.Convolve( matrix=lambda _img, nb_channels, random_state: np.float32([[1]])) observed = aug.augment_image(img) assert np.array_equal(observed, img) # matrix is [[0, 0, 0], [0, 1, 0], [0, 0, 0]] m = np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img) assert np.array_equal(observed, img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img) assert np.array_equal(observed, img) # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]] m = np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img) assert np.array_equal(observed, 2 * img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img) assert np.array_equal(observed, 2 * img) # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]] # with 3 channels m = np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]]) img3 = np.tile(img[..., np.newaxis], (1, 1, 3)) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img3) assert np.array_equal(observed, 2 * img3) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img3) assert np.array_equal(observed, 2 * img3) # matrix is [[0, -1, 0], [0, 10, 0], [0, 0, 0]] m = np.float32([[0, -1, 0], [0, 10, 0], [0, 0, 0]]) expected = np.uint8( [[10 * 1 + (-1) * 4, 10 * 2 + (-1) * 5, 10 * 3 + (-1) * 6], [10 * 4 + (-1) * 1, 10 * 5 + (-1) * 2, 10 * 6 + (-1) * 3], [10 * 7 + (-1) * 4, 10 * 8 + (-1) * 5, 10 * 9 + (-1) * 6]]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img) assert np.array_equal(observed, expected) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img) assert np.array_equal(observed, expected) # changing matrices when using callable expected = [] for i in sm.xrange(5): expected.append(img * i) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: np. float32([[random_state.randint(0, 5)]])) seen = [False] * 5 for _ in sm.xrange(200): observed = aug.augment_image(img) found = False for i, expected_i in enumerate(expected): if np.array_equal(observed, expected_i): seen[i] = True found = True break assert found if all(seen): break assert all(seen) # bad datatype for matrix got_exception = False try: aug = iaa.Convolve(matrix=False) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # get_parameters() matrix = np.int32([[1]]) aug = iaa.Convolve(matrix=matrix) params = aug.get_parameters() assert np.array_equal(params[0], matrix) assert params[1] == "constant" # TODO add test for keypoints once their handling was improved in Convolve ################### # test other dtypes ################### identity_matrix = np.int64([[1]]) aug = iaa.Convolve(matrix=identity_matrix) image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image_aug = aug.augment_image(image) assert image.dtype.type == np.bool_ assert np.all(image_aug == image) for dtype in [np.uint8, np.uint16, np.int8, np.int16]: image = np.zeros((3, 3), dtype=dtype) image[1, 1] = 100 image_aug = aug.augment_image(image) assert image.dtype.type == dtype assert np.all(image_aug == image) for dtype in [np.float16, np.float32, np.float64]: image = np.zeros((3, 3), dtype=dtype) image[1, 1] = 100.0 image_aug = aug.augment_image(image) assert image.dtype.type == dtype assert np.allclose(image_aug, image) # ---- # non-identity matrix # ---- matrix = np.float64([[0, 0.6, 0], [0, 0.4, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=matrix) image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image[2, 1] = True expected = np.zeros((3, 3), dtype=bool) expected[0, 1] = True expected[2, 1] = True image_aug = aug.augment_image(image) assert image.dtype.type == np.bool_ assert np.all(image_aug == expected) matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=matrix) # 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, 1] = 100 image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = int(np.round(100 * 0.5)) expected[1, 1] = int(np.round(100 * 0.5)) expected[2, 1] = int(np.round(100 * 0.5 + 100 * 0.5)) 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, 1] = 100.0 image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = 100 * 0.5 expected[1, 1] = 100 * 0.5 expected[2, 1] = 100 * 0.5 + 100 * 0.5 diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.dtype.type == dtype assert np.max(diff) < 1.0 # ---- # non-identity matrix, higher values # ---- matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=matrix) # 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, 1] = value image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = int(np.round(value * 0.5)) expected[1, 1] = int(np.round(value * 0.5)) expected[2, 1] = int(np.round(value * 0.5 + value * 0.5)) 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, 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, 1] = value image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = value * 0.5 expected[1, 1] = value * 0.5 expected[2, 1] = value * 0.5 + value * 0.5 diff = np.abs( image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.dtype.type == dtype assert np.max(diff) < 1.0 # assert failure on invalid dtypes aug = iaa.Convolve(matrix=identity_matrix) 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_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 # 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_Emboss(): reseed() base_img = [[10, 10, 10], [10, 20, 10], [10, 10, 15]] base_img = np.uint8(base_img) def _compute_embossed_base_img(img, alpha, strength): img = np.copy(img) base_img_embossed = np.zeros((3, 3), dtype=np.float32) m = np.float32([[-1, 0, 0], [0, 1, 0], [0, 0, 1]]) strength_matrix = strength * np.float32([ [-1, -1, 0], [-1, 0, 1], [0, 1, 1] ]) ms = m + strength_matrix for i in range(base_img_embossed.shape[0]): for j in range(base_img_embossed.shape[1]): for u in range(ms.shape[0]): for v in range(ms.shape[1]): weight = ms[u, v] inputs_i = abs(i + (u - (ms.shape[0]-1)//2)) inputs_j = abs(j + (v - (ms.shape[1]-1)//2)) if inputs_i >= img.shape[0]: diff = inputs_i - (img.shape[0]-1) inputs_i = img.shape[0] - 1 - diff if inputs_j >= img.shape[1]: diff = inputs_j - (img.shape[1]-1) inputs_j = img.shape[1] - 1 - diff inputs = img[inputs_i, inputs_j] base_img_embossed[i, j] += inputs * weight return np.clip((1-alpha) * img + alpha * base_img_embossed, 0, 255).astype(np.uint8) def _allclose(a, b): return np.max(a.astype(np.float32) - b.astype(np.float32)) <= 2.1 aug = iaa.Emboss(alpha=0, strength=1) observed = aug.augment_image(base_img) expected = base_img assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=1) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=1) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=0.5, strength=1) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=0.5, strength=1) assert _allclose(observed, expected.astype(np.uint8)) aug = iaa.Emboss(alpha=0.75, strength=1) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=0.75, strength=1) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=iap.Choice([0.5, 1.0]), strength=1) observed = aug.augment_image(base_img) expected1 = _compute_embossed_base_img(base_img, alpha=0.5, strength=1) expected2 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1) assert _allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Emboss(alpha="test", strength=1) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception aug = iaa.Emboss(alpha=1.0, strength=2) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=2) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=3) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=3) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=6) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=6) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=iap.Choice([1.0, 1.5])) observed = aug.augment_image(base_img) expected1 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1.0) expected2 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1.5) assert _allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Emboss(alpha=1.0, strength="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception
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_Emboss(): reseed() base_img = [[10, 10, 10], [10, 20, 10], [10, 10, 15]] base_img = np.uint8(base_img) def _compute_embossed_base_img(img, alpha, strength): img = np.copy(img) base_img_embossed = np.zeros((3, 3), dtype=np.float32) m = np.float32([[-1, 0, 0], [0, 1, 0], [0, 0, 1]]) strength_matrix = strength * np.float32([[-1, -1, 0], [-1, 0, 1], [0, 1, 1]]) ms = m + strength_matrix for i in range(base_img_embossed.shape[0]): for j in range(base_img_embossed.shape[1]): for u in range(ms.shape[0]): for v in range(ms.shape[1]): weight = ms[u, v] inputs_i = abs(i + (u - (ms.shape[0] - 1) // 2)) inputs_j = abs(j + (v - (ms.shape[1] - 1) // 2)) if inputs_i >= img.shape[0]: diff = inputs_i - (img.shape[0] - 1) inputs_i = img.shape[0] - 1 - diff if inputs_j >= img.shape[1]: diff = inputs_j - (img.shape[1] - 1) inputs_j = img.shape[1] - 1 - diff inputs = img[inputs_i, inputs_j] base_img_embossed[i, j] += inputs * weight return np.clip((1 - alpha) * img + alpha * base_img_embossed, 0, 255).astype(np.uint8) def _allclose(a, b): return np.max(a.astype(np.float32) - b.astype(np.float32)) <= 2.1 aug = iaa.Emboss(alpha=0, strength=1) observed = aug.augment_image(base_img) expected = base_img assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=1) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=1) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=0.5, strength=1) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=0.5, strength=1) assert _allclose(observed, expected.astype(np.uint8)) aug = iaa.Emboss(alpha=0.75, strength=1) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=0.75, strength=1) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=iap.Choice([0.5, 1.0]), strength=1) observed = aug.augment_image(base_img) expected1 = _compute_embossed_base_img(base_img, alpha=0.5, strength=1) expected2 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1) assert _allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Emboss(alpha="test", strength=1) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception aug = iaa.Emboss(alpha=1.0, strength=2) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=2) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=3) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=3) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=6) observed = aug.augment_image(base_img) expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=6) assert _allclose(observed, expected) aug = iaa.Emboss(alpha=1.0, strength=iap.Choice([1.0, 1.5])) observed = aug.augment_image(base_img) expected1 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1.0) expected2 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1.5) assert _allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Emboss(alpha=1.0, strength="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception
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_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_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_Superpixels(): reseed() def _array_equals_tolerant(a, b, tolerance): diff = np.abs(a.astype(np.int32) - b.astype(np.int32)) return np.all(diff <= tolerance) base_img = [ [255, 255, 255, 0, 0, 0], [255, 235, 255, 0, 20, 0], [250, 250, 250, 5, 5, 5] ] base_img = np.tile(np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels = [ [251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4] ] base_img_superpixels = np.tile(np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels_left = np.copy(base_img_superpixels) base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :] base_img_superpixels_right = np.copy(base_img_superpixels) base_img_superpixels_right[:, :3, :] = base_img[:, :3, :] aug = iaa.Superpixels(p_replace=0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Superpixels(p_replace=1.0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2)) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(base_img) assert np.allclose(observed, base_img) or _array_equals_tolerant(observed, base_img_superpixels, 2) aug = iaa.Superpixels(p_replace=0.5, n_segments=2) seen = {"none": False, "left": False, "right": False, "both": False} for _ in sm.xrange(100): observed = aug.augment_image(base_img) if _array_equals_tolerant(observed, base_img, 2): seen["none"] = True elif _array_equals_tolerant(observed, base_img_superpixels_left, 2): seen["left"] = True elif _array_equals_tolerant(observed, base_img_superpixels_right, 2): seen["right"] = True elif _array_equals_tolerant(observed, base_img_superpixels, 2): seen["both"] = True else: raise Exception("Generated superpixels image does not match any expected image.") if all(seen.values()): break assert all(seen.values()) # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Superpixels(p_replace="test", n_segments=100) except Exception: got_exception = True assert got_exception got_exception = False try: _ = iaa.Superpixels(p_replace=1, n_segments="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Superpixels(p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest") params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 assert params[1].value == 2 assert params[2] == 100 assert params[3] == "nearest" ################### # test other dtypes ################### # bool aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([ [False, False, True, True], [False, False, True, True] ], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug == img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([ [True, True, True, True], [False, True, True, True] ], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug) 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) if np.dtype(dtype).kind == "i": values = [int(center_value), int(0.1 * max_value), int(0.2 * max_value), int(0.5 * max_value), max_value-100] values = [((-1)*value, value) for value in values] else: values = [(0, int(center_value)), (10, int(0.1 * max_value)), (10, int(0.2 * max_value)), (10, int(0.5 * max_value)), (0, max_value), (int(center_value), max_value)] for v1, v2 in values: aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([ [v1, v1, v2, v2], [v1, v1, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.array_equal(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([ [v2, v2, v2, v2], [v1, v2, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.all(img_aug == int(np.round((7/8)*v2 + (1/8)*v1))) for dtype in []: def _allclose(a, b): atol = 1e-4 if dtype == np.float16 else 1e-8 return np.allclose(a, b, atol=atol, rtol=0) isize = np.dtype(dtype).itemsize for value in [0, 1.0, 10.0, 1000 ** (isize - 1)]: v1 = (-1) * value v2 = value aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([ [v1, v1, v2, v2], [v1, v1, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([ [v2, v2, v2, v2], [v1, v2, v2, v2] ], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, (7/8)*v2 + (1/8)*v1)
def test_FastSnowyLandscape(): reseed() # check parameters aug = iaa.FastSnowyLandscape(lightness_threshold=[100, 200], lightness_multiplier=[1.0, 4.0]) assert isinstance(aug.lightness_threshold, iap.Choice) assert len(aug.lightness_threshold.a) == 2 assert aug.lightness_threshold.a[0] == 100 assert aug.lightness_threshold.a[1] == 200 assert isinstance(aug.lightness_multiplier, iap.Choice) assert len(aug.lightness_multiplier.a) == 2 assert np.allclose(aug.lightness_multiplier.a[0], 1.0) assert np.allclose(aug.lightness_multiplier.a[1], 4.0) # basic functionality test aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0) image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(expected, 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2RGB) observed = aug.augment_image(image) assert np.array_equal(observed, expected) # test when varying lightness_threshold between images image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) class _TwoValueParam(iap.StochasticParameter): def __init__(self, v1, v2): super(_TwoValueParam, self).__init__() self.v1 = v1 self.v2 = v2 def _draw_samples(self, size, random_state): arr = np.full(size, self.v1, dtype=np.float32) arr[1::2] = self.v2 return arr aug = iaa.FastSnowyLandscape(lightness_threshold=_TwoValueParam(75, 125), lightness_multiplier=2.0) mask = (image_hls[..., 1] < 75) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 2.0 expected1 = np.clip(expected1, 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 125) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(expected2, 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2) # test when varying lightness_multiplier between images aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=_TwoValueParam(1.5, 2.0)) mask = (image_hls[..., 1] < 100) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 1.5 expected1 = np.clip(expected1, 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 100) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(expected2, 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2) # test BGR colorspace aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0, from_colorspace="BGR") image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(expected, 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2BGR) observed = aug.augment_image(image) assert np.array_equal(observed, 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_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_keypoint_augmentation(): reseed() keypoints = [] for y in sm.xrange(40 // 5): for x in sm.xrange(60 // 5): keypoints.append(ia.Keypoint(y=y * 5, x=x * 5)) keypoints_oi = ia.KeypointsOnImage(keypoints, shape=(40, 60, 3)) keypoints_oi_empty = ia.KeypointsOnImage([], shape=(40, 60, 3)) augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"), iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10), name="BlendAlphaElementwise"), iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"), iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Add(10), name="BlendAlphaSimplexNoise"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize(0.5, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: dss = [] for i in sm.xrange(10): aug_det = aug.to_deterministic() kp_fully_empty_aug = aug_det.augment_keypoints([]) assert kp_fully_empty_aug == [] kp_first_empty_aug = aug_det.augment_keypoints(keypoints_oi_empty) assert len(kp_first_empty_aug.keypoints) == 0 kp_image = keypoints_oi.to_keypoint_image(size=5) kp_image_aug = aug_det.augment_image(kp_image) kp_image_aug_rev = ia.KeypointsOnImage.from_keypoint_image( kp_image_aug, if_not_found_coords={ "x": -9999, "y": -9999 }, nb_channels=1) kp_aug = aug_det.augment_keypoints([keypoints_oi])[0] ds = [] assert len(kp_image_aug_rev.keypoints) == len(kp_aug.keypoints), ( "Lost keypoints for '%s' (%d vs expected %d)" % (aug.name, len( kp_aug.keypoints), len(kp_image_aug_rev.keypoints))) gen = zip(kp_aug.keypoints, kp_image_aug_rev.keypoints) for kp_pred, kp_pred_img in gen: kp_pred_lost = (kp_pred.x == -9999 and kp_pred.y == -9999) kp_pred_img_lost = (kp_pred_img.x == -9999 and kp_pred_img.y == -9999) if not kp_pred_lost and not kp_pred_img_lost: d = np.sqrt((kp_pred.x - kp_pred_img.x)**2 + (kp_pred.y - kp_pred_img.y)**2) ds.append(d) dss.extend(ds) if len(ds) == 0: print("[INFO] No valid keypoints found for '%s' " "in test_keypoint_augmentation()" % (str(aug), )) assert np.average(dss) < 5.0, \ "Average distance too high (%.2f, with ds: %s)" \ % (np.average(dss), str(dss))
def setUp(self): reseed()
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_BoundingBoxesOnImage(): reseed() # test height/width bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) assert bbsoi.height == 40 assert bbsoi.width == 50 bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=np.zeros((40, 50, 3), dtype=np.uint8)) assert bbsoi.height == 40 assert bbsoi.width == 50 # empty bb = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bbsoi = ia.BoundingBoxesOnImage([bb], shape=(40, 50, 3)) assert not bbsoi.empty bbsoi = ia.BoundingBoxesOnImage([], shape=(40, 50, 3)) assert bbsoi.empty # on() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=np.zeros((40, 50, 3), dtype=np.uint8)) bbsoi_projected = bbsoi.on((40, 50)) assert bbsoi_projected.bounding_boxes[0].y1 == 10 assert bbsoi_projected.bounding_boxes[0].x1 == 20 assert bbsoi_projected.bounding_boxes[0].y2 == 30 assert bbsoi_projected.bounding_boxes[0].x2 == 40 assert bbsoi_projected.bounding_boxes[1].y1 == 15 assert bbsoi_projected.bounding_boxes[1].x1 == 25 assert bbsoi_projected.bounding_boxes[1].y2 == 35 assert bbsoi_projected.bounding_boxes[1].x2 == 45 bbsoi_projected = bbsoi.on((40*2, 50*2, 3)) assert bbsoi_projected.bounding_boxes[0].y1 == 10*2 assert bbsoi_projected.bounding_boxes[0].x1 == 20*2 assert bbsoi_projected.bounding_boxes[0].y2 == 30*2 assert bbsoi_projected.bounding_boxes[0].x2 == 40*2 assert bbsoi_projected.bounding_boxes[1].y1 == 15*2 assert bbsoi_projected.bounding_boxes[1].x1 == 25*2 assert bbsoi_projected.bounding_boxes[1].y2 == 35*2 assert bbsoi_projected.bounding_boxes[1].x2 == 45*2 bbsoi_projected = bbsoi.on(np.zeros((40*2, 50*2, 3), dtype=np.uint8)) assert bbsoi_projected.bounding_boxes[0].y1 == 10*2 assert bbsoi_projected.bounding_boxes[0].x1 == 20*2 assert bbsoi_projected.bounding_boxes[0].y2 == 30*2 assert bbsoi_projected.bounding_boxes[0].x2 == 40*2 assert bbsoi_projected.bounding_boxes[1].y1 == 15*2 assert bbsoi_projected.bounding_boxes[1].x1 == 25*2 assert bbsoi_projected.bounding_boxes[1].y2 == 35*2 assert bbsoi_projected.bounding_boxes[1].x2 == 45*2 # from_xyxy_array() bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array( np.float32([ [0.0, 0.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0] ]), shape=(40, 50, 3) ) assert len(bbsoi.bounding_boxes) == 2 assert np.allclose(bbsoi.bounding_boxes[0].x1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].y1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].x2, 1.0) assert np.allclose(bbsoi.bounding_boxes[0].y2, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].x1, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].y1, 2.0) assert np.allclose(bbsoi.bounding_boxes[1].x2, 3.0) assert np.allclose(bbsoi.bounding_boxes[1].y2, 4.0) assert bbsoi.shape == (40, 50, 3) bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array( np.int32([ [0, 0, 1, 1], [1, 2, 3, 4] ]), shape=(40, 50, 3) ) assert len(bbsoi.bounding_boxes) == 2 assert np.allclose(bbsoi.bounding_boxes[0].x1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].y1, 0.0) assert np.allclose(bbsoi.bounding_boxes[0].x2, 1.0) assert np.allclose(bbsoi.bounding_boxes[0].y2, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].x1, 1.0) assert np.allclose(bbsoi.bounding_boxes[1].y1, 2.0) assert np.allclose(bbsoi.bounding_boxes[1].x2, 3.0) assert np.allclose(bbsoi.bounding_boxes[1].y2, 4.0) assert bbsoi.shape == (40, 50, 3) bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array( np.zeros((0, 4), dtype=np.float32), shape=(40, 50, 3) ) assert len(bbsoi.bounding_boxes) == 0 assert bbsoi.shape == (40, 50, 3) # to_xyxy_array() xyxy_arr = np.float32([ [0.0, 0.0, 1.0, 1.0], [1.0, 2.0, 3.0, 4.0] ]) bbsoi = ia.BoundingBoxesOnImage.from_xyxy_array(xyxy_arr, shape=(40, 50, 3)) xyxy_arr_out = bbsoi.to_xyxy_array() assert np.allclose(xyxy_arr, xyxy_arr_out) assert xyxy_arr_out.dtype == np.float32 xyxy_arr_out = bbsoi.to_xyxy_array(dtype=np.int32) assert np.allclose(xyxy_arr.astype(np.int32), xyxy_arr_out) assert xyxy_arr_out.dtype == np.int32 xyxy_arr_out = ia.BoundingBoxesOnImage([], shape=(40, 50, 3)).to_xyxy_array(dtype=np.int32) assert xyxy_arr_out.shape == (0, 4) # draw_on_image() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=45, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) image = bbsoi.draw_on_image(np.zeros(bbsoi.shape, dtype=np.uint8), color=[0, 255, 0], alpha=1.0, size=1, copy=True, raise_if_out_of_image=False) assert np.all(image[10-1, 20-1, :] == [0, 0, 0]) assert np.all(image[10-1, 20-0, :] == [0, 0, 0]) assert np.all(image[10-0, 20-1, :] == [0, 0, 0]) assert np.all(image[10-0, 20-0, :] == [0, 255, 0]) assert np.all(image[10+1, 20+1, :] == [0, 0, 0]) assert np.all(image[30-1, 40-1, :] == [0, 0, 0]) assert np.all(image[30+1, 40-0, :] == [0, 0, 0]) assert np.all(image[30+0, 40+1, :] == [0, 0, 0]) assert np.all(image[30+0, 40+0, :] == [0, 255, 0]) assert np.all(image[30+1, 40+1, :] == [0, 0, 0]) assert np.all(image[15-1, 25-1, :] == [0, 0, 0]) assert np.all(image[15-1, 25-0, :] == [0, 0, 0]) assert np.all(image[15-0, 25-1, :] == [0, 0, 0]) assert np.all(image[15-0, 25-0, :] == [0, 255, 0]) assert np.all(image[15+1, 25+1, :] == [0, 0, 0]) assert np.all(image[35-1, 45-1, :] == [0, 0, 0]) assert np.all(image[35+1, 45+0, :] == [0, 0, 0]) assert np.all(image[35+0, 45+1, :] == [0, 0, 0]) assert np.all(image[35+0, 45+0, :] == [0, 255, 0]) assert np.all(image[35+1, 45+1, :] == [0, 0, 0]) # remove_out_of_image() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_slim = bbsoi.remove_out_of_image(fully=True, partly=True) assert len(bbsoi_slim.bounding_boxes) == 1 assert bbsoi_slim.bounding_boxes[0] == bb1 # clip_out_of_image() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) eps = np.finfo(np.float32).eps bbsoi_clip = bbsoi.clip_out_of_image() assert len(bbsoi_clip.bounding_boxes) == 2 assert bbsoi_clip.bounding_boxes[0].y1 == 10 assert bbsoi_clip.bounding_boxes[0].x1 == 20 assert bbsoi_clip.bounding_boxes[0].y2 == 30 assert bbsoi_clip.bounding_boxes[0].x2 == 40 assert bbsoi_clip.bounding_boxes[1].y1 == 15 assert bbsoi_clip.bounding_boxes[1].x1 == 25 assert bbsoi_clip.bounding_boxes[1].y2 == 35 assert 50 - 2*eps < bbsoi_clip.bounding_boxes[1].x2 < 50 # shift() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_shifted = bbsoi.shift(right=1) assert len(bbsoi_shifted.bounding_boxes) == 2 assert bbsoi_shifted.bounding_boxes[0].y1 == 10 assert bbsoi_shifted.bounding_boxes[0].x1 == 20 - 1 assert bbsoi_shifted.bounding_boxes[0].y2 == 30 assert bbsoi_shifted.bounding_boxes[0].x2 == 40 - 1 assert bbsoi_shifted.bounding_boxes[1].y1 == 15 assert bbsoi_shifted.bounding_boxes[1].x1 == 25 - 1 assert bbsoi_shifted.bounding_boxes[1].y2 == 35 assert bbsoi_shifted.bounding_boxes[1].x2 == 51 - 1 # copy() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_copy = bbsoi.copy() assert len(bbsoi.bounding_boxes) == 2 assert bbsoi_copy.bounding_boxes[0].y1 == 10 assert bbsoi_copy.bounding_boxes[0].x1 == 20 assert bbsoi_copy.bounding_boxes[0].y2 == 30 assert bbsoi_copy.bounding_boxes[0].x2 == 40 assert bbsoi_copy.bounding_boxes[1].y1 == 15 assert bbsoi_copy.bounding_boxes[1].x1 == 25 assert bbsoi_copy.bounding_boxes[1].y2 == 35 assert bbsoi_copy.bounding_boxes[1].x2 == 51 bbsoi.bounding_boxes[0].y1 = 0 assert bbsoi_copy.bounding_boxes[0].y1 == 0 # deepcopy() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bbsoi_copy = bbsoi.deepcopy() assert len(bbsoi.bounding_boxes) == 2 assert bbsoi_copy.bounding_boxes[0].y1 == 10 assert bbsoi_copy.bounding_boxes[0].x1 == 20 assert bbsoi_copy.bounding_boxes[0].y2 == 30 assert bbsoi_copy.bounding_boxes[0].x2 == 40 assert bbsoi_copy.bounding_boxes[1].y1 == 15 assert bbsoi_copy.bounding_boxes[1].x1 == 25 assert bbsoi_copy.bounding_boxes[1].y2 == 35 assert bbsoi_copy.bounding_boxes[1].x2 == 51 bbsoi.bounding_boxes[0].y1 = 0 assert bbsoi_copy.bounding_boxes[0].y1 == 10 # repr() / str() bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None) bb2 = ia.BoundingBox(y1=15, x1=25, y2=35, x2=51, label=None) bbsoi = ia.BoundingBoxesOnImage([bb1, bb2], shape=(40, 50, 3)) bb1_expected = "BoundingBox(x1=20.0000, y1=10.0000, x2=40.0000, y2=30.0000, label=None)" bb2_expected = "BoundingBox(x1=25.0000, y1=15.0000, x2=51.0000, y2=35.0000, label=None)" expected = "BoundingBoxesOnImage([%s, %s], shape=(40, 50, 3))" % (bb1_expected, bb2_expected) assert bbsoi.__repr__() == bbsoi.__str__() == expected
def test_unusual_channel_numbers(): reseed() images = [ (0, create_random_images((4, 16, 16))), (1, create_random_images((4, 16, 16, 1))), (2, create_random_images((4, 16, 16, 2))), (4, create_random_images((4, 16, 16, 4))), (5, create_random_images((4, 16, 16, 5))), (10, create_random_images((4, 16, 16, 10))), (20, create_random_images((4, 16, 16, 20))) ] augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.SomeOf(1, [iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), iaa.Identity(name="Noop"), iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"), iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10), name="BlendAlphaElementwise"), iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"), iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Add(10), name="BlendAlphaSimplexNoise"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize({"height": 4, "width": 4}, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: for (nb_channels, images_c) in images: if aug.name != "Resize": images_aug = aug.augment_images(images_c) assert images_aug.shape == images_c.shape image_aug = aug.augment_image(images_c[0]) assert image_aug.shape == images_c[0].shape else: images_aug = aug.augment_images(images_c) image_aug = aug.augment_image(images_c[0]) if images_c.ndim == 3: assert images_aug.shape == (4, 4, 4) assert image_aug.shape == (4, 4) else: assert images_aug.shape == (4, 4, 4, images_c.shape[3]) assert image_aug.shape == (4, 4, images_c.shape[3])
def test_keypoint_augmentation(): reseed() keypoints = [] for y in range(40//5): for x in range(60//5): keypoints.append(ia.Keypoint(y=y*5, x=x*5)) keypoints_oi = ia.KeypointsOnImage(keypoints, shape=(40, 60, 3)) keypoints_oi_empty = ia.KeypointsOnImage([], shape=(40, 60, 3)) augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), # iaa.BilateralBlur((3, 5), name="BilateralBlur"), # WithColorspace ? # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # ChangeColorspace ? # Grayscale cannot be tested, input not RGB # Convolve ? iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), # iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), # Sequential # SomeOf # OneOf # Sometimes # WithChannels # Noop # Lambda # AssertLambda # AssertShape iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize(0.5, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: dss = [] for i in range(10): aug_det = aug.to_deterministic() kp_fully_empty_aug = aug_det.augment_keypoints([]) assert kp_fully_empty_aug == [] kp_first_empty_aug = aug_det.augment_keypoints([keypoints_oi_empty])[0] assert len(kp_first_empty_aug.keypoints) == 0 kp_image = keypoints_oi.to_keypoint_image(size=5) kp_image_aug = aug_det.augment_image(kp_image) kp_image_aug_rev = ia.KeypointsOnImage.from_keypoint_image( kp_image_aug, if_not_found_coords={"x": -9999, "y": -9999}, nb_channels=1 ) kp_aug = aug_det.augment_keypoints([keypoints_oi])[0] ds = [] assert len(kp_image_aug_rev.keypoints) == len(kp_aug.keypoints),\ "Lost keypoints for '%s' (%d vs expected %d)" \ % (aug.name, len(kp_aug.keypoints), len(kp_image_aug_rev.keypoints)) for kp_pred, kp_pred_img in zip(kp_aug.keypoints, kp_image_aug_rev.keypoints): kp_pred_lost = (kp_pred.x == -9999 and kp_pred.y == -9999) kp_pred_img_lost = (kp_pred_img.x == -9999 and kp_pred_img.y == -9999) if not kp_pred_lost and not kp_pred_img_lost: d = np.sqrt((kp_pred.x - kp_pred_img.x) ** 2 + (kp_pred.y - kp_pred_img.y) ** 2) ds.append(d) dss.extend(ds) if len(ds) == 0: print("[INFO] No valid keypoints found for '%s' " "in test_keypoint_augmentation()" % (str(aug),)) assert np.average(dss) < 5.0, \ "Average distance too high (%.2f, with ds: %s)" \ % (np.average(dss), str(dss))
def test_dtype_preservation(): reseed() size = (4, 16, 16, 3) images = [ np.random.uniform(0, 255, size).astype(np.uint8), np.random.uniform(0, 65535, size).astype(np.uint16), np.random.uniform(0, 4294967295, size).astype(np.uint32), np.random.uniform(-128, 127, size).astype(np.int16), np.random.uniform(-32768, 32767, size).astype(np.int32), np.random.uniform(0.0, 1.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float16), np.random.uniform(-1000.0, 1000.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float64) ] default_dtypes = set([arr.dtype for arr in images]) # Some dtypes are here removed per augmenter, because the respective # augmenter does not support them. This test currently only checks whether # dtypes are preserved from in- to output for all dtypes that are supported # per augmenter. # dtypes are here removed via list comprehension instead of # `default_dtypes - set([dtype])`, because the latter one simply never # removed the dtype(s) for some reason def _not_dts(dts): return [dt for dt in default_dtypes if dt not in dts] augs = [ (iaa.Add((-5, 5), name="Add"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AddElementwise((-5, 5), name="AddElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Multiply((0.95, 1.05), name="Multiply"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Dropout(0.01, name="Dropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Invert(0.01, per_channel=True, name="Invert"), default_dtypes), (iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), _not_dts([np.float16])), (iaa.AverageBlur((3, 5), name="AverageBlur"), _not_dts([np.uint32, np.int32, np.float16])), (iaa.MedianBlur((3, 5), name="MedianBlur"), _not_dts([np.uint32, np.int32, np.float16, np.float64])), (iaa.BilateralBlur((3, 5), name="BilateralBlur"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float16, np.float64])), (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes), (iaa.Flipud(0.5, name="Flipud"), default_dtypes), (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), _not_dts([np.uint32, np.int32])), (iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), _not_dts([np.uint32, np.int32])), (iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), _not_dts([np.uint32, np.int32])), (iaa.Affine(shear=(-20, 20), name="Affine-shear"), _not_dts([np.uint32, np.int32])), (iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), _not_dts([np.uint32, np.int32])), (iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), default_dtypes), (iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), _not_dts([np.float16])), (iaa.Sequential([iaa.Identity(), iaa.Identity()], name="SequentialNoop"), default_dtypes), (iaa.SomeOf(1, [iaa.Identity(), iaa.Identity()], name="SomeOfNoop"), default_dtypes), (iaa.OneOf([iaa.Identity(), iaa.Identity()], name="OneOfNoop"), default_dtypes), (iaa.Sometimes(0.5, iaa.Identity(), name="SometimesNoop"), default_dtypes), (iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))], name="Sequential"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SomeOf(1, [iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))], name="SomeOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))], name="OneOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Identity(name="Identity"), default_dtypes), (iaa.BlendAlpha((0.0, 0.1), iaa.Identity(), name="BlendAlphaIdentity"), _not_dts([np.float64])), # float64 requires float128 support (iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Identity(), name="BlendAlphaElementwiseIdentity"), _not_dts([np.float64])), # float64 requires float128 support (iaa.BlendAlphaSimplexNoise(iaa.Identity(), name="BlendAlphaSimplexNoiseIdentity"), _not_dts([np.float64])), # float64 requires float128 support (iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Identity(), name="BlendAlphaFrequencyNoiseIdentity"), _not_dts([np.float64])), (iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10), name="BlendAlphaElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Add(10), name="BlendAlphaFrequencyNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Superpixels(p_replace=0.01, n_segments=64), _not_dts([np.float16, np.float32, np.float64])), (iaa.Resize({"height": 4, "width": 4}, name="Resize"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])), (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])), (iaa.Pad(px=(0, 10), name="Pad"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])), (iaa.Crop(px=(0, 10), name="Crop"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])) ] for (aug, allowed_dtypes) in augs: for images_i in images: if images_i.dtype in allowed_dtypes: images_aug = aug.augment_images(images_i) assert images_aug.dtype == images_i.dtype
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_Convolve(): reseed() img = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] img = np.uint8(img) # matrix is None aug = iaa.Convolve(matrix=None) observed = aug.augment_image(img) assert np.array_equal(observed, img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: [None]) observed = aug.augment_image(img) assert np.array_equal(observed, img) # matrix is [[1]] aug = iaa.Convolve(matrix=np.float32([[1]])) observed = aug.augment_image(img) assert np.array_equal(observed, img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: np.float32([[1]])) observed = aug.augment_image(img) assert np.array_equal(observed, img) # matrix is [[0, 0, 0], [0, 1, 0], [0, 0, 0]] m = np.float32([ [0, 0, 0], [0, 1, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img) assert np.array_equal(observed, img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img) assert np.array_equal(observed, img) # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]] m = np.float32([ [0, 0, 0], [0, 2, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img) assert np.array_equal(observed, 2*img) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img) assert np.array_equal(observed, 2*img) # matrix is [[0, 0, 0], [0, 2, 0], [0, 0, 0]] # with 3 channels m = np.float32([ [0, 0, 0], [0, 2, 0], [0, 0, 0] ]) img3 = np.tile(img[..., np.newaxis], (1, 1, 3)) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img3) assert np.array_equal(observed, 2*img3) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img3) assert np.array_equal(observed, 2*img3) # matrix is [[0, -1, 0], [0, 10, 0], [0, 0, 0]] m = np.float32([ [0, -1, 0], [0, 10, 0], [0, 0, 0] ]) expected = np.uint8([ [10*1+(-1)*4, 10*2+(-1)*5, 10*3+(-1)*6], [10*4+(-1)*1, 10*5+(-1)*2, 10*6+(-1)*3], [10*7+(-1)*4, 10*8+(-1)*5, 10*9+(-1)*6] ]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(img) assert np.array_equal(observed, expected) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(img) assert np.array_equal(observed, expected) # changing matrices when using callable expected = [] for i in sm.xrange(5): expected.append(img * i) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: np.float32([[random_state.randint(0, 5)]])) seen = [False] * 5 for _ in sm.xrange(200): observed = aug.augment_image(img) found = False for i, expected_i in enumerate(expected): if np.array_equal(observed, expected_i): seen[i] = True found = True break assert found if all(seen): break assert all(seen) # bad datatype for matrix got_exception = False try: aug = iaa.Convolve(matrix=False) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # get_parameters() matrix = np.int32([[1]]) aug = iaa.Convolve(matrix=matrix) params = aug.get_parameters() assert np.array_equal(params[0], matrix) assert params[1] == "constant" # TODO add test for keypoints once their handling was improved in Convolve ################### # test other dtypes ################### identity_matrix = np.int64([[1]]) aug = iaa.Convolve(matrix=identity_matrix) image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image_aug = aug.augment_image(image) assert image.dtype.type == np.bool_ assert np.all(image_aug == image) for dtype in [np.uint8, np.uint16, np.int8, np.int16]: image = np.zeros((3, 3), dtype=dtype) image[1, 1] = 100 image_aug = aug.augment_image(image) assert image.dtype.type == dtype assert np.all(image_aug == image) for dtype in [np.float16, np.float32, np.float64]: image = np.zeros((3, 3), dtype=dtype) image[1, 1] = 100.0 image_aug = aug.augment_image(image) assert image.dtype.type == dtype assert np.allclose(image_aug, image) # ---- # non-identity matrix # ---- matrix = np.float64([ [0, 0.6, 0], [0, 0.4, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=matrix) image = np.zeros((3, 3), dtype=bool) image[1, 1] = True image[2, 1] = True expected = np.zeros((3, 3), dtype=bool) expected[0, 1] = True expected[2, 1] = True image_aug = aug.augment_image(image) assert image.dtype.type == np.bool_ assert np.all(image_aug == expected) matrix = np.float64([ [0, 0.5, 0], [0, 0.5, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=matrix) # 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, 1] = 100 image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = int(np.round(100 * 0.5)) expected[1, 1] = int(np.round(100 * 0.5)) expected[2, 1] = int(np.round(100 * 0.5 + 100 * 0.5)) 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, 1] = 100.0 image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = 100 * 0.5 expected[1, 1] = 100 * 0.5 expected[2, 1] = 100 * 0.5 + 100 * 0.5 diff = np.abs(image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.dtype.type == dtype assert np.max(diff) < 1.0 # ---- # non-identity matrix, higher values # ---- matrix = np.float64([ [0, 0.5, 0], [0, 0.5, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=matrix) # 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, 1] = value image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = int(np.round(value * 0.5)) expected[1, 1] = int(np.round(value * 0.5)) expected[2, 1] = int(np.round(value * 0.5 + value * 0.5)) 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, 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, 1] = value image_aug = aug.augment_image(image) expected = np.zeros((3, 3), dtype=dtype) expected[0, 1] = value * 0.5 expected[1, 1] = value * 0.5 expected[2, 1] = value * 0.5 + value * 0.5 diff = np.abs(image_aug.astype(np.float128) - expected.astype(np.float128)) assert image_aug.dtype.type == dtype assert np.max(diff) < 1.0 # assert failure on invalid dtypes aug = iaa.Convolve(matrix=identity_matrix) 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_unusual_channel_numbers(): reseed() images = [ (0, create_random_images((4, 16, 16))), (1, create_random_images((4, 16, 16, 1))), (2, create_random_images((4, 16, 16, 2))), (4, create_random_images((4, 16, 16, 4))), (5, create_random_images((4, 16, 16, 5))), (10, create_random_images((4, 16, 16, 10))), (20, create_random_images((4, 16, 16, 20))) ] augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), # iaa.BilateralBlur((3, 5), name="BilateralBlur"), # works only with 3/RGB channels # WithColorspace ? # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # works only with 3/RGB channels # ChangeColorspace ? # iaa.Grayscale((0.0, 0.1), name="Grayscale"), # works only with 3 channels # Convolve ? iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.SomeOf(1, [iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), # WithChannels iaa.Noop(name="Noop"), # Lambda # AssertLambda # AssertShape iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize({"height": 4, "width": 4}, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: for (nb_channels, images_c) in images: if aug.name != "Resize": images_aug = aug.augment_images(images_c) assert images_aug.shape == images_c.shape image_aug = aug.augment_image(images_c[0]) assert image_aug.shape == images_c[0].shape else: images_aug = aug.augment_images(images_c) image_aug = aug.augment_image(images_c[0]) if images_c.ndim == 3: assert images_aug.shape == (4, 4, 4) assert image_aug.shape == (4, 4) else: assert images_aug.shape == (4, 4, 4, images_c.shape[3]) assert image_aug.shape == (4, 4, images_c.shape[3])
def test_Sharpen(): reseed() def _compute_sharpened_base_img(lightness, m): base_img_sharpened = np.zeros((3, 3), dtype=np.float32) k = 1 # note that cv2 uses reflection padding by default base_img_sharpened[0, 0] = (m[1, 1] + lightness) / k * 10 + 4 * ( m[0, 0] / k) * 10 + 4 * (m[2, 2] / k) * 20 base_img_sharpened[0, 2] = base_img_sharpened[0, 0] base_img_sharpened[2, 0] = base_img_sharpened[0, 0] base_img_sharpened[2, 2] = base_img_sharpened[0, 0] base_img_sharpened[0, 1] = (m[1, 1] + lightness) / k * 10 + 6 * ( m[0, 1] / k) * 10 + 2 * (m[2, 2] / k) * 20 base_img_sharpened[1, 0] = base_img_sharpened[0, 1] base_img_sharpened[1, 2] = base_img_sharpened[0, 1] base_img_sharpened[2, 1] = base_img_sharpened[0, 1] base_img_sharpened[ 1, 1] = (m[1, 1] + lightness) / k * 20 + 8 * (m[0, 1] / k) * 10 base_img_sharpened = np.clip(base_img_sharpened, 0, 255).astype(np.uint8) return base_img_sharpened base_img = [[10, 10, 10], [10, 20, 10], [10, 10, 10]] base_img = np.uint8(base_img) m = np.float32([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]) m_noop = np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) base_img_sharpened = _compute_sharpened_base_img(1, m) aug = iaa.Sharpen(alpha=0, lightness=1) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=1.0, lightness=1) observed = aug.augment_image(base_img) expected = base_img_sharpened assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=0.5, lightness=1) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(0.5 * 1, 0.5 * m_noop + 0.5 * m) assert np.allclose(observed, expected.astype(np.uint8)) aug = iaa.Sharpen(alpha=0.75, lightness=1) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(0.75 * 1, 0.25 * m_noop + 0.75 * m) assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=iap.Choice([0.5, 1.0]), lightness=1) observed = aug.augment_image(base_img) expected1 = _compute_sharpened_base_img(0.5 * 1, m) expected2 = _compute_sharpened_base_img(1.0 * 1, m) assert np.allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Sharpen(alpha="test", lightness=1) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception aug = iaa.Sharpen(alpha=1.0, lightness=2) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(1.0 * 2, m) assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=1.0, lightness=3) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(1.0 * 3, m) assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=1.0, lightness=iap.Choice([1.0, 1.5])) observed = aug.augment_image(base_img) expected1 = _compute_sharpened_base_img(1.0 * 1.0, m) expected2 = _compute_sharpened_base_img(1.0 * 1.5, m) assert np.allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Sharpen(alpha=1.0, lightness="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # this part doesnt really work so far due to nonlinearities resulting from clipping to uint8 """
def test_dtype_preservation(): reseed() size = (4, 16, 16, 3) images = [ np.random.uniform(0, 255, size).astype(np.uint8), np.random.uniform(0, 65535, size).astype(np.uint16), np.random.uniform(0, 4294967295, size).astype(np.uint32), np.random.uniform(-128, 127, size).astype(np.int16), np.random.uniform(-32768, 32767, size).astype(np.int32), np.random.uniform(0.0, 1.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float16), np.random.uniform(-1000.0, 1000.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float64) ] default_dtypes = set([arr.dtype for arr in images]) # Some dtypes are here removed per augmenter, because the respective # augmenter does not support them. This test currently only checks whether # dtypes are preserved from in- to output for all dtypes that are supported # per augmenter. # dtypes are here removed via list comprehension instead of # `default_dtypes - set([dtype])`, because the latter one simply never # removed the dtype(s) for some reason?! def _not_dts(dts): return [dt for dt in default_dtypes if dt not in dts] augs = [ (iaa.Add((-5, 5), name="Add"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AddElementwise((-5, 5), name="AddElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Multiply((0.95, 1.05), name="Multiply"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Dropout(0.01, name="Dropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Invert(0.01, per_channel=True, name="Invert"), default_dtypes), (iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"), default_dtypes), (iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), _not_dts([np.float16])), (iaa.AverageBlur((3, 5), name="AverageBlur"), _not_dts([np.uint32, np.int32, np.float16])), (iaa.MedianBlur((3, 5), name="MedianBlur"), _not_dts([np.uint32, np.int32, np.float16, np.float64])), (iaa.BilateralBlur((3, 5), name="BilateralBlur"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float16, np.float64])), # WithColorspace ? # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # works only with RGB/uint8 # ChangeColorspace ? # iaa.Grayscale((0.0, 0.1), name="Grayscale"), # works only with RGB/uint8 # Convolve ? (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes), (iaa.Flipud(0.5, name="Flipud"), default_dtypes), (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), _not_dts([np.uint32, np.int32])), (iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), _not_dts([np.uint32, np.int32])), (iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), _not_dts([np.uint32, np.int32])), (iaa.Affine(shear=(-20, 20), name="Affine-shear"), _not_dts([np.uint32, np.int32])), (iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), _not_dts([np.uint32, np.int32])), (iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), default_dtypes), # (iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), not_dts([np.uint32])), (iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), _not_dts([np.float16])), (iaa.Sequential([iaa.Noop(), iaa.Noop()], name="SequentialNoop"), default_dtypes), (iaa.SomeOf(1, [iaa.Noop(), iaa.Noop()], name="SomeOfNoop"), default_dtypes), (iaa.OneOf([iaa.Noop(), iaa.Noop()], name="OneOfNoop"), default_dtypes), (iaa.Sometimes(0.5, iaa.Noop(), name="SometimesNoop"), default_dtypes), (iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))], name="Sequential"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SomeOf(1, [iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))], name="SomeOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))], name="OneOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), _not_dts([np.uint32, np.int32, np.float64])), # WithChannels (iaa.Noop(name="Noop"), default_dtypes), # Lambda # AssertLambda # AssertShape (iaa.Alpha((0.0, 0.1), iaa.Noop(), name="AlphaNoop"), default_dtypes), (iaa.AlphaElementwise((0.0, 0.1), iaa.Noop(), name="AlphaElementwiseNoop"), default_dtypes), (iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlphaNoop"), default_dtypes), (iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Noop(), name="SimplexNoiseAlphaNoop"), default_dtypes), (iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Superpixels(p_replace=0.01, n_segments=64), _not_dts([np.float16, np.float32, np.float64])), (iaa.Resize({"height": 4, "width": 4}, name="Resize"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])), (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])), (iaa.Pad(px=(0, 10), name="Pad"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])), (iaa.Crop(px=(0, 10), name="Crop"), _not_dts([np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64])) ] for (aug, allowed_dtypes) in augs: # print("aug", aug.name) # print("allowed_dtypes", allowed_dtypes) for images_i in images: if images_i.dtype in allowed_dtypes: # print("image dt", images_i.dtype) images_aug = aug.augment_images(images_i) assert images_aug.dtype == images_i.dtype else: # print("image dt", images_i.dtype, "[SKIPPED]") pass
def test_Sharpen(): reseed() def _compute_sharpened_base_img(lightness, m): base_img_sharpened = np.zeros((3, 3), dtype=np.float32) k = 1 # note that cv2 uses reflection padding by default base_img_sharpened[0, 0] = (m[1, 1] + lightness)/k * 10 + 4 * (m[0, 0]/k) * 10 + 4 * (m[2, 2]/k) * 20 base_img_sharpened[0, 2] = base_img_sharpened[0, 0] base_img_sharpened[2, 0] = base_img_sharpened[0, 0] base_img_sharpened[2, 2] = base_img_sharpened[0, 0] base_img_sharpened[0, 1] = (m[1, 1] + lightness)/k * 10 + 6 * (m[0, 1]/k) * 10 + 2 * (m[2, 2]/k) * 20 base_img_sharpened[1, 0] = base_img_sharpened[0, 1] base_img_sharpened[1, 2] = base_img_sharpened[0, 1] base_img_sharpened[2, 1] = base_img_sharpened[0, 1] base_img_sharpened[1, 1] = (m[1, 1] + lightness)/k * 20 + 8 * (m[0, 1]/k) * 10 base_img_sharpened = np.clip(base_img_sharpened, 0, 255).astype(np.uint8) return base_img_sharpened base_img = [[10, 10, 10], [10, 20, 10], [10, 10, 10]] base_img = np.uint8(base_img) m = np.float32([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]) m_noop = np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) base_img_sharpened = _compute_sharpened_base_img(1, m) aug = iaa.Sharpen(alpha=0, lightness=1) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=1.0, lightness=1) observed = aug.augment_image(base_img) expected = base_img_sharpened assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=0.5, lightness=1) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(0.5*1, 0.5 * m_noop + 0.5 * m) assert np.allclose(observed, expected.astype(np.uint8)) aug = iaa.Sharpen(alpha=0.75, lightness=1) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(0.75*1, 0.25 * m_noop + 0.75 * m) assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=iap.Choice([0.5, 1.0]), lightness=1) observed = aug.augment_image(base_img) expected1 = _compute_sharpened_base_img(0.5*1, m) expected2 = _compute_sharpened_base_img(1.0*1, m) assert np.allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Sharpen(alpha="test", lightness=1) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception aug = iaa.Sharpen(alpha=1.0, lightness=2) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(1.0*2, m) assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=1.0, lightness=3) observed = aug.augment_image(base_img) expected = _compute_sharpened_base_img(1.0*3, m) assert np.allclose(observed, expected) aug = iaa.Sharpen(alpha=1.0, lightness=iap.Choice([1.0, 1.5])) observed = aug.augment_image(base_img) expected1 = _compute_sharpened_base_img(1.0*1.0, m) expected2 = _compute_sharpened_base_img(1.0*1.5, m) assert np.allclose(observed, expected1) or np.allclose(observed, expected2) got_exception = False try: _ = iaa.Sharpen(alpha=1.0, lightness="test") except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception # this part doesnt really work so far due to nonlinearities resulting from clipping to uint8 """