def test_lambda_with_changing_matrices(self): # changing matrices when using callable def _matrix_generator(_img, _nb_channels, random_state): return np.float32([[ iarandom.polyfill_integers(random_state, 0, 5) ]]) expected = [] for i in sm.xrange(5): expected.append(self.img * i) aug = iaa.Convolve(matrix=_matrix_generator) seen = [False] * 5 for _ in sm.xrange(200): observed = aug.augment_image(self.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 np.all(seen)
def test_other_dtypes_uint_int_non_identity_matrix_with_large_values(self): matrix = np.float64([ [0, 0.5, 0], [0, 0.5, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=matrix) 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
def test_matrix_is_lambda_none(self): def _matrix_generator(_img, _nb_channels, _random_state): return [None] aug = iaa.Convolve(matrix=_matrix_generator) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)
def test_matrix_is_lambda_1x1_identity(self): def _matrix_generator(_img, _nb_channels, _random_state): return np.float32([[1]]) aug = iaa.Convolve(matrix=_matrix_generator) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)
def test_matrix_is_lambda_3x3_two_in_center(self): def _matrix_generator(_img, _nb_channels, _random_state): return np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=_matrix_generator) observed = aug.augment_image(self.img) assert np.array_equal(observed, 2 * self.img)
def test_matrix_is_lambda_3x3_two_in_center_3_channels(self): def _matrix_generator(_img, _nb_channels, _random_state): return np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=_matrix_generator) img3 = np.tile(self.img[..., np.newaxis], (1, 1, 3)) # 3 channels observed = aug.augment_image(img3) assert np.array_equal(observed, 2 * img3)
def test_other_dtypes_bool_identity_matrix(self): 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)
def test_matrix_is_3x3_identity(self): m = np.float32([ [0, 0, 0], [0, 1, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)
def test_matrix_is_3x3_two_in_center(self): m = np.float32([ [0, 0, 0], [0, 2, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=m) observed = aug.augment_image(self.img) assert np.array_equal(observed, 2*self.img)
def test_matrix_has_bad_datatype(self): # don't use assertRaisesRegex, because it doesnt exist in 2.7 got_exception = False try: _aug = iaa.Convolve(matrix=False) except Exception as exc: assert "Expected " in str(exc) got_exception = True assert got_exception
def augment(): with open(CONFIG, "r") as file: config = json.loads(file.read()) if config[ImageAugmentation.AUGMENT_DATA]: matrix = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) return iaa.Sometimes(ImageAugmentation.AUG_PERCENTAGE, [ iaa.GaussianBlur(sigma=2.0), iaa.Sequential([iaa.Affine(rotate=45), iaa.Sharpen(alpha=1.0)]), iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 0, iaa.Add((10, 50)))), iaa.AdditiveGaussianNoise(scale=0.2 * 255), iaa.Add(50, per_channel=True), iaa.Sharpen(alpha=0.5), iaa.WithChannels(0, iaa.Add((10, 100))), iaa.WithChannels(0, iaa.Affine(rotate=(0, 45))), iaa.Noop(), iaa.Superpixels(p_replace=0.5, n_segments=64), iaa.Superpixels(p_replace=(0.1, 1.0), n_segments=(16, 128)), iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add((50, 100))), iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB"), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AverageBlur(k=(2, 11)), iaa.AverageBlur(k=((5, 11), (1, 3))), iaa.MedianBlur(k=(3, 11)), iaa.Convolve(matrix=matrix), iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)), iaa.EdgeDetect(alpha=(0.0, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.0, 1.0), direction=(0.0, 1.0)), iaa.Add((-40, 40)), iaa.Add((-40, 40), per_channel=0.5), iaa.AddElementwise((-40, 40)), iaa.AddElementwise((-40, 40), per_channel=0.5), iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)), iaa.AdditiveGaussianNoise(scale=0.05 * 255), iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.Dropout(p=(0, 0.2)), iaa.Dropout(p=(0, 0.2), per_channel=0.5), iaa.CoarseDropout(0.02, size_percent=0.5), iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)), iaa.CoarseDropout(0.02, size_percent=0.15, per_channel=0.5), iaa.Invert(0.25, per_channel=0.5), iaa.Invert(0.5), iaa.ContrastNormalization((0.5, 1.5)), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25) ]) else: return None
def test_matrix_is_3x3_with_multiple_nonzero_values(self): 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(self.img) assert np.array_equal(observed, expected)
def test_other_dtypes_float_identity_matrix(self): identity_matrix = np.int64([[1]]) aug = iaa.Convolve(matrix=identity_matrix) 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)
def test_other_dtypes_uint_int_identity_matrix(self): identity_matrix = np.int64([[1]]) aug = iaa.Convolve(matrix=identity_matrix) 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)
def test_matrix_is_3x3_two_in_center_3_channels(self): m = np.float32([ [0, 0, 0], [0, 2, 0], [0, 0, 0] ]) aug = iaa.Convolve(matrix=m) img3 = np.tile(self.img[..., np.newaxis], (1, 1, 3)) # 3 channels observed = aug.augment_image(img3) assert np.array_equal(observed, 2*img3)
def test_failure_on_invalid_dtypes(self): # don't use assertRaisesRegex, because it doesnt exist in 2.7 identity_matrix = np.int64([[1]]) 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_matrix_is_lambda_3x3_with_multiple_nonzero_values(self): def _matrix_generator(_img, _nb_channels, _random_state): return 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=_matrix_generator) observed = aug.augment_image(self.img) assert np.array_equal(observed, expected)
def test_zero_sized_axes(self): shapes = [(0, 0), (0, 1), (1, 0), (0, 1, 0), (1, 0, 0), (0, 1, 1), (1, 0, 1)] for shape in shapes: with self.subTest(shape=shape): image = np.zeros(shape, dtype=np.uint8) aug = iaa.Convolve(matrix=np.float32([[1]])) image_aug = aug(image=image) assert image_aug.shape == image.shape
def chapter_augmenters_convolve(): matrix = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) aug = iaa.Convolve(matrix=matrix) run_and_save_augseq("convolve.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=50) def gen_matrix(image, nb_channels, random_state): matrix_A = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) matrix_B = np.array([[0, 0, 0], [0, -4, 1], [0, 2, 1]]) if random_state.rand() < 0.5: return [matrix_A] * nb_channels else: return [matrix_B] * nb_channels aug = iaa.Convolve(matrix=gen_matrix) run_and_save_augseq("convolve_callable.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2)
def test_other_dtypes_bool_non_identity_matrix_with_small_values(self): 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)
def augment_channels(images, aug_config): """ Augment each image in images with the channel transformation given in aug_config. """ augmented_images = [] # Instantiate transformations if aug_config.do_blur: blur = iaa.GaussianBlur(sigma=aug_config.blur_sigma) if aug_config.do_edge: edge = iaa.EdgeDetect(alpha=1) if aug_config.do_contrast: contrast = iaa.ContrastNormalization((0.5, 1.5)) if aug_config.do_convolve: convolve = iaa.Convolve(matrix=np.array([[0, -1, 0],[-1, 4, -1],[0, -1, 0]])) if aug_config.do_invert: invert = iaa.Invert(1) # Augment each image for im in images: augmented = im if aug_config.do_blur: aug = img_as_float(blur.augment_image(img_as_ubyte(im))) augmented = np.dstack((augmented, aug)) if aug_config.do_edge: aug = img_as_float(edge.augment_image(img_as_ubyte(im))) augmented = np.dstack((augmented, aug)) if aug_config.do_contrast: aug = img_as_float(contrast.augment_image(img_as_ubyte(im))) augmented = np.dstack((augmented, aug)) if aug_config.do_convolve: aug = img_as_float(convolve.augment_image(img_as_ubyte(im))) augmented = np.dstack((augmented, aug)) if aug_config.do_invert: aug = img_as_float(invert.augment_image(img_as_ubyte(im))) augmented = np.dstack((augmented, aug)) augmented_images.append(augmented) return augmented_images
def test_other_dtypes_float_non_identity_matrix_with_small_values(self): matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=matrix) 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
def test_other_dtypes_uint_int_non_identity_matrix_with_small_values(self): matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=matrix) 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
def test_other_dtypes_float_non_identity_matrix_with_large_values(self): matrix = np.float64([[0, 0.5, 0], [0, 0.5, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=matrix) 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.float64) - expected.astype(np.float64)) assert image_aug.dtype.type == dtype assert np.max(diff) < 1.0
def test_lambda_with_changing_matrices(self): # changing matrices when using callable expected = [] for i in sm.xrange(5): expected.append(self.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(self.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 np.all(seen)
def test_matrix_is_lambda_3x3_two_in_center(self): m = np.float32([[0, 0, 0], [0, 2, 0], [0, 0, 0]]) aug = iaa.Convolve(matrix=lambda _img, nb_channels, random_state: m) observed = aug.augment_image(self.img) assert np.array_equal(observed, 2 * self.img)
def test_matrix_is_lambda_1x1_identity(self): aug = iaa.Convolve( matrix=lambda _img, nb_channels, random_state: np.float32([[1]])) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)
def test_matrix_is_1x1_identity(self): # matrix is [[1]] aug = iaa.Convolve(matrix=np.float32([[1]])) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)
def test_matrix_is_lambda_none(self): aug = iaa.Convolve( matrix=lambda _img, nb_channels, random_state: [None]) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)
def test_matrix_is_none(self): aug = iaa.Convolve(matrix=None) observed = aug.augment_image(self.img) assert np.array_equal(observed, self.img)