def __call__(self, images): nb_images = len(images) # 120 x 3 x H x W seeds = ia.current_random_state().randint(0, 10**6, (nb_images, )) rs_image = ia.new_random_state(seeds[0]) samples = self.p.draw_samples((nb_images, ), random_state=rs_image) samples_true_index = np.where(samples == 1)[0] samples_false_index = np.where(samples == 0)[0] # Check for the zero case , related to pytorch isuee of not accepting zero size array if samples_true_index.size > 0: samples_true_index = torch.cuda.LongTensor(samples_true_index) images_to_transform = torch.index_select(images, 0, samples_true_index) if samples_false_index.size > 0: # images_not_to_transform = torch.index_select(images, 0, torch.cuda.LongTensor( # samples_false_index)) transformed_imgs = self.transform(images_to_transform) images = images.index_copy_(0, samples_true_index, transformed_imgs) return images # return torch.cat((self.transform(images_to_transform), images_not_to_transform), 0) else: return self.transform(images) else: return images
def _augment_keypoints(self, keypoints_on_images, random_state, parents, hooks): result = [] nb_images = len(keypoints_on_images) seeds = random_state.randint(0, 10**6, (nb_images, )) for i, keypoints_on_image in enumerate(keypoints_on_images): seed = seeds[i] h, w = keypoints_on_image.shape[0:2] crop_h, crop_w = self.size[0:2] random_state = ia.new_random_state(seed) which = self.choice.draw_sample(random_state=random_state) if which == 0: shifted = keypoints_on_image elif which == 1: shifted = keypoints_on_image.shift(x=-crop_w) elif which == 2: shifted = keypoints_on_image.shift(y=-crop_h) elif which == 3: shifted = keypoints_on_image.shift(x=-crop_w, y=-crop_h) else: i = int(round((h - crop_h) / 2.)) j = int(round((w - crop_w) / 2.)) shifted = keypoints_on_image.shift(x=-i, y=-j) shifted.shape = (crop_h, crop_w) result.append(shifted) return result
def __call__(self, images): # input_dtypes = copy_dtypes_for_restore(images, force_list=True) result = images nb_images = len(images) # Generate new seeds con seeds = ia.current_random_state().randint(0, 10**6, (nb_images, )) rs_image = ia.new_random_state(seeds[0]) per_channel = self.per_channel.draw_sample(random_state=rs_image) if per_channel == 1: nb_channels = images.shape[1] for c in range(nb_channels): samples = self.value.draw_samples( (nb_images, 1, 1, 1), random_state=rs_image).astype(np.float32) do_assert(samples.all() >= 0) result[:, c:(c + 1), ...] = F.add(images[:, c:(c + 1), ...], torch.cuda.FloatTensor(samples)) else: samples = self.value.draw_samples( (nb_images, 1, 1, 1), random_state=rs_image).astype(np.float32) do_assert(samples.all() >= 0) result = F.add(images, torch.cuda.FloatTensor(samples)) # image = meta.clip_augmented_image_(image, 0, 255) # TODO make value range more flexible # image = meta.restore_augmented_image_dtype_(image, input_dtypes[i]) return result
def __call__(self, images): result = images nb_images = len(images) # Generate new seeds con seeds = ia.current_random_state().randint(0, 10**6, (nb_images, )) rs_image = ia.new_random_state(seeds[0]) per_channel = self.per_channel.draw_sample(random_state=rs_image) if per_channel == 1: nb_channels = images.shape[1] #Considering (N C H W) for c in range(nb_channels): alphas = self.alpha.draw_samples( (nb_images, 1, 1, 1), random_state=rs_image).astype(np.float32) do_assert(alphas.all() >= 0) result[:, c:(c + 1), ...] = F.contrast(images[:, c:(c + 1), ...], torch.cuda.FloatTensor(alphas)) else: alphas = self.alpha.draw_samples( (nb_images, 1, 1, 1), random_state=rs_image).astype(np.float32) do_assert(alphas.all() >= 0) #print(alphas) result = F.contrast(images, torch.cuda.FloatTensor(alphas)) return result
def reseed(augmenter, deterministic=True): augmenter.random_state = ia.new_random_state(get_seed()) if deterministic: augmenter.deterministic = True for lists in augmenter.get_children_lists(): for aug in lists: aug = reseed(aug, deterministic=True) return augmenter
def _draw_sample_point(self, box_w, box_h, img_w, img_h, seed): x1 = math.ceil(box_w / 2) y1 = math.ceil(box_h / 2) x2 = math.floor(img_w - x1) y2 = math.floor(img_h - y1) random_state = ia.new_random_state(seed) x = DiscreteUniform(x1, x2) y = DiscreteUniform(y1, y2) center_x = x.draw_sample(random_state=random_state) center_y = y.draw_sample(random_state=random_state) return center_x, center_y
def __call__(self, images): # Convert to Grayscale direction nb_images = len(images) # Generate new seeds con seeds = ia.current_random_state().randint(0, 10**6, (nb_images, )) rs_image = ia.new_random_state(seeds[0]) samples = self.alpha.draw_samples( (nb_images, 1, 1, 1), random_state=rs_image).astype(np.float32) do_assert(samples.all() >= 0) result = F.grayscale(images, torch.cuda.FloatTensor(samples)) return result
def __call__(self, images): result = images nb_images = len(images) nb_channels, height, width = images[0].shape seeds = ia.current_random_state().randint(0, 10**6, (nb_images, )) rs_image = ia.new_random_state(seeds[0]) samples = self.sigma.draw_samples((nb_images, ), random_state=rs_image) # note that while gaussian_filter can be applied to all channels # at the same time, that should not be done here, because then # the blurring would also happen across channels (e.g. red # values might be mixed with blue values in RGB) for c in range(nb_channels): result[:, c:(c + 1), ...] = F.blur(result[:, c:(c + 1), ...], samples) return result
def _augment_images(self, images, random_state, parents, hooks): result = images nb_images = len(images) seeds = random_state.randint(0, 10**6, (nb_images, )) for i in ia.sm.xrange(nb_images): image = images[i].astype(np.float32) rs_image = ia.new_random_state(seeds[i]) per_channel = self.per_channel.draw_sample(random_state=rs_image) if per_channel == 1: nb_channels = image.shape[2] samples = self.gamma_log2.draw_samples((nb_channels, ), random_state=rs_image) for c, sample in enumerate(samples): image[..., c] = np.power(image[..., c] / 255.0, 2.0** sample) * 255.0 # np.clip(image, 0, 255, out=image) result[i] = image.astype(np.float32) else: sample = self.gamma_log2.draw_sample(random_state=rs_image) image = np.power(image / 255.0, 2.0**sample) * 255.0 # np.clip(image, 0, 255, out=image) result[i] = image.astype(np.float32) return result
def _draw_samples_image(self, seed, height, width): """ height = 32 width = 32 h, w = shape = (30, 30) random_state = np.random """ random_state = ia.new_random_state(seed) h, w = self.shape assert w <= width, '{} {}'.format(w, width) assert h <= height, '{} {}'.format(h, height) space_h = height - h space_w = width - w top = random_state.randint(0, space_h + 1) bot = height - (space_h - top) left = random_state.randint(0, space_w + 1) right = width - (space_w - left) sub = [top, bot, left, right] return sub
def _draw_samples(self, nb_samples, random_state): seed = random_state.randint(0, 10**6, 1)[0] if isinstance(self.scale, tuple): scale_samples = ( self.scale[0].draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 10)), self.scale[1].draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 20)), ) else: scale_samples = self.scale.draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 30)) scale_samples = (scale_samples, scale_samples) if isinstance(self.translate, tuple): translate_samples = ( self.translate[0].draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 40)), self.translate[1].draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 50)), ) else: translate_samples = self.translate.draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 60)) translate_samples = (translate_samples, translate_samples) ia.do_assert(translate_samples[0].dtype in [np.int32, np.int64, np.float32, np.float64]) ia.do_assert(translate_samples[1].dtype in [np.int32, np.int64, np.float32, np.float64]) rotate_samples = self.rotate.draw_samples( (nb_samples, ), random_state=ia.new_random_state(seed + 70)) return scale_samples, translate_samples, rotate_samples
def _augment_images(self, images, random_state, parents, hooks): result = [] nb_images = len(images) seeds = random_state.randint(0, 10**6, (nb_images, )) for i in xrange(nb_images): seed = seeds[i] img = images[i] h, w = img.shape[0:2] crop_h, crop_w = self.size if crop_w > w or crop_h > h: raise ValueError( "Requested crop size {} is bigger than input size {}". format(self.size, (h, w))) random_state = ia.new_random_state(seed) which = self.choice.draw_sample(random_state=random_state) if which == 0: # top left image_cr = img[0:crop_h, 0:crop_w] elif which == 1: # top right image_cr = img[0:crop_h, (w - crop_w):w] elif which == 2: # bottom left image_cr = img[(h - crop_h):h, 0:crop_w] elif which == 3: # bottom right image_cr = img[(h - crop_h):h, (w - crop_w):w] else: # center image_cr = self.center_crop(img, (crop_h, crop_w)) result.append(image_cr) return result
def reseed(augmenter_sequence, deterministic=True): for aug in augmenter_sequence: aug.random_state = ia.new_random_state(get_seed()) if deterministic: aug.deterministic = True return augmenter_sequence
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 chapter_alpha_masks_iterative(): # ----------------------------------------- # IterativeNoiseAggregator varying number of iterations # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] iterations_all = [1, 2, 3, 4] for iterations in iterations_all: noise = iap.IterativeNoiseAggregator(other_param=iap.FrequencyNoise( exponent=(-4.0, 4.0), upscale_method=["linear", "nearest"]), iterations=iterations, aggregation_method="max") row = [ noise.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] row = np.hstack(row) row = np.tile(row[:, :, np.newaxis], (1, 1, 3)) row = (row * 255).astype(np.uint8) row = np.pad(row, ((0, 0), (50, 0), (0, 0)), mode="constant", constant_values=255) row = ia.draw_text(row, y=24, x=2, text="%d iter." % (iterations, ), size=14, color=[0, 0, 0]) masks.append(row) # ------------ save("alpha", "iterative_vary_iterations.jpg", grid(masks, cols=1, rows=len(iterations_all))) # ----------------------------------------- # IterativeNoiseAggregator varying methods # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) iterations_all = [1, 2, 3, 4, 5, 6] methods = ["min", "avg", "max"] cell_idx = 0 rows = [] for method_idx, method in enumerate(methods): row = [] for iterations in iterations_all: noise = iap.IterativeNoiseAggregator( other_param=iap.FrequencyNoise( exponent=-2.0, size_px_max=32, upscale_method=["linear", "nearest"]), iterations=iterations, aggregation_method=method) cell = noise.draw_samples( (64, 64), random_state=ia.new_random_state(seed + 1 + method_idx)) cell = np.tile(cell[:, :, np.newaxis], (1, 1, 3)) cell = (cell * 255).astype(np.uint8) if iterations == 1: cell = np.pad(cell, ((0, 0), (40, 0), (0, 0)), mode="constant", constant_values=255) cell = ia.draw_text(cell, y=27, x=2, text="%s" % (method, ), size=14, color=[0, 0, 0]) if method_idx == 0: cell = np.pad(cell, ((20, 0), (0, 0), (0, 0)), mode="constant", constant_values=255) cell = ia.draw_text(cell, y=0, x=12 + 40 * (iterations == 1), text="%d iter." % (iterations, ), size=14, color=[0, 0, 0]) cell = np.pad(cell, ((0, 1), (0, 1), (0, 0)), mode="constant", constant_values=255) row.append(cell) cell_idx += 1 rows.append(np.hstack(row)) gridarr = np.vstack(rows) # ------------ save("alpha", "iterative_vary_methods.jpg", gridarr)
def chapter_alpha_masks_frequency(): # ----------------------------------------- # example 1 (basic) # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap ia.seed(1) # Example batch of images. # The array has shape (8, 64, 64, 3) and dtype uint8. images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)], dtype=np.uint8) seq = iaa.BlendAlphaFrequencyNoise( foreground=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)) images_aug = seq(images=images) # ------------ save("alpha", "alpha_frequency_example_basic.jpg", grid(images_aug, cols=4, rows=2)) # ----------------------------------------- # example 1 (per_channel) # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa ia.seed(1) # Example batch of images. # The array has shape (8, 128, 128, 3) and dtype uint8. images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)], dtype=np.uint8) seq = iaa.BlendAlphaFrequencyNoise(foreground=iaa.EdgeDetect(1.0), per_channel=True) images_aug = seq(images=images) # ------------ save("alpha", "alpha_frequency_example_per_channel.jpg", grid(images_aug, cols=4, rows=2)) # ----------------------------------------- # noise masks # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) seq = iaa.BlendAlphaFrequencyNoise( foreground=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)) masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = [np.tile(mask[:, :, np.newaxis], (1, 1, 3)) for mask in masks] masks = [(mask * 255).astype(np.uint8) for mask in masks] # ------------ save("alpha", "alpha_frequency_noise_masks.jpg", grid(masks, cols=8, rows=2)) # ----------------------------------------- # noise masks, varying exponent # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] nb_rows = 4 exponents = np.linspace(-4.0, 4.0, 16) for i, exponent in enumerate(exponents): seq = iaa.BlendAlphaFrequencyNoise(exponent=exponent, foreground=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True), size_px_max=32, upscale_method="linear", iterations=1, sigmoid=False) group = [] for row in range(nb_rows): mask = seq.factor.draw_samples( (64, 64), random_state=ia.new_random_state(seed + 1 + i * 10 + row)) mask = np.tile(mask[:, :, np.newaxis], (1, 1, 3)) mask = (mask * 255).astype(np.uint8) if row == nb_rows - 1: mask = np.pad(mask, ((0, 20), (0, 0), (0, 0)), mode="constant", constant_values=255) mask = ia.draw_text(mask, y=64 + 2, x=6, text="%.2f" % (exponent, ), size=10, color=[0, 0, 0]) group.append(mask) masks.append(np.vstack(group)) # ------------ save("alpha", "alpha_frequency_noise_masks_exponents.jpg", grid(masks, cols=16, rows=1)) # ----------------------------------------- # noise masks, upscale=nearest # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) seq = iaa.BlendAlphaFrequencyNoise(foreground=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True), upscale_method="nearest") masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = [np.tile(mask[:, :, np.newaxis], (1, 1, 3)) for mask in masks] masks = [(mask * 255).astype(np.uint8) for mask in masks] # ------------ save("alpha", "alpha_frequency_noise_masks_nearest.jpg", grid(masks, cols=8, rows=2)) # ----------------------------------------- # noise masks linear # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap seed = 1 ia.seed(seed) seq = iaa.BlendAlphaFrequencyNoise(foreground=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True), upscale_method="linear") masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = [np.tile(mask[:, :, np.newaxis], (1, 1, 3)) for mask in masks] masks = [(mask * 255).astype(np.uint8) for mask in masks] # ------------ save("alpha", "alpha_frequency_noise_masks_linear.jpg", grid(masks, cols=8, rows=2))
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (128, 128)) # check if scipy and cv2 remap similarly rs = ia.new_random_state(1) aug_scipy = ElasticTransformationScipy(alpha=30, sigma=3, random_state=rs, deterministic=True) aug_cv2 = ElasticTransformationCv2(alpha=30, sigma=3, random_state=rs, deterministic=True) augs_scipy = aug_scipy.augment_images([image] * 8) augs_cv2 = aug_cv2.augment_images([image] * 8) ia.imshow(ia.draw_grid(augs_scipy + augs_cv2, rows=2)) print("alpha=vary, sigma=0.25") augs = [ iaa.ElasticTransformation(alpha=alpha, sigma=0.25) for alpha in np.arange(0.0, 50.0, 0.1) ] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=vary, sigma=1.0") augs = [ iaa.ElasticTransformation(alpha=alpha, sigma=1.0) for alpha in np.arange(0.0, 50.0, 0.1) ] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=vary, sigma=3.0") augs = [ iaa.ElasticTransformation(alpha=alpha, sigma=3.0) for alpha in np.arange(0.0, 50.0, 0.1) ] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=vary, sigma=5.0") augs = [ iaa.ElasticTransformation(alpha=alpha, sigma=5.0) for alpha in np.arange(0.0, 50.0, 0.1) ] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=1.0, sigma=vary") augs = [ iaa.ElasticTransformation(alpha=1.0, sigma=sigma) for sigma in np.arange(0.0, 50.0, 0.1) ] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=10.0, sigma=vary") augs = [ iaa.ElasticTransformation(alpha=10.0, sigma=sigma) for sigma in np.arange(0.0, 50.0, 0.1) ] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) kps = ia.KeypointsOnImage([ ia.Keypoint(x=1, y=1), ia.Keypoint(x=50, y=24), ia.Keypoint(x=42, y=96), ia.Keypoint(x=88, y=106), ia.Keypoint(x=88, y=53), ia.Keypoint(x=0, y=0), ia.Keypoint(x=128, y=128), ia.Keypoint(x=-20, y=30), ia.Keypoint(x=20, y=-30), ia.Keypoint(x=-20, y=-30) ], shape=image.shape) images = [] params = [(0.0, 0.0), (0.2, 0.2), (2.0, 0.25), (0.25, 3.0), (2.0, 3.0), (6.0, 3.0), (12.0, 3.0), (50.0, 5.0), (100.0, 5.0), (100.0, 10.0)] for (alpha, sigma) in params: images_row = [] seqs_row = [ iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=2), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=2), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=2), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=3) ] for seq in seqs_row: seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image) kps_aug = seq_det.augment_keypoints([kps])[0] image_aug_kp = np.copy(image_aug) image_aug_kp = kps_aug.draw_on_image(image_aug_kp, size=3) images_row.append(image_aug_kp) images.append(np.hstack(images_row)) ia.imshow(np.vstack(images)) imageio.imwrite("elastic_transformations.jpg", np.vstack(images))
def chapter_alpha_masks_sigmoid(): # ----------------------------------------- # Sigmoid varying on/off # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] for activated in [False, True]: noise = iap.Sigmoid.create_for_noise(other_param=iap.FrequencyNoise( exponent=(-4.0, 4.0), upscale_method="linear"), activated=activated) row = [ noise.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] row = np.hstack(row) row = np.tile(row[:, :, np.newaxis], (1, 1, 3)) row = (row * 255).astype(np.uint8) row = np.pad(row, ((0, 0), (90, 0), (0, 0)), mode="constant", constant_values=255) row = ia.draw_text(row, y=17, x=2, text="activated=\n%s" % (activated, ), size=14, color=[0, 0, 0]) masks.append(row) # ------------ save("alpha", "sigmoid_vary_activated.jpg", grid(masks, cols=1, rows=2)) # ----------------------------------------- # Sigmoid varying on/off # ----------------------------------------- import imgaug as ia from imgaug import parameters as iap seed = 1 ia.seed(seed) masks = [] nb_rows = 3 class ConstantNoise(iap.StochasticParameter): def __init__(self, noise, seed): super(ConstantNoise, self).__init__() self.noise = noise self.seed = seed def _draw_samples(self, size, random_state): return self.noise.draw_samples(size, random_state=ia.new_random_state( self.seed)) for rowidx in range(nb_rows): row = [] for tidx, threshold in enumerate(np.linspace(-10.0, 10.0, 10)): noise = iap.Sigmoid.create_for_noise(other_param=ConstantNoise( iap.FrequencyNoise(exponent=(-4.0, 4.0), upscale_method="linear"), seed=seed + 100 + rowidx), activated=True, threshold=threshold) cell = noise.draw_samples( (64, 64), random_state=ia.new_random_state(seed + tidx)) cell = np.tile(cell[:, :, np.newaxis], (1, 1, 3)) cell = (cell * 255).astype(np.uint8) if rowidx == 0: cell = np.pad(cell, ((20, 0), (0, 0), (0, 0)), mode="constant", constant_values=255) cell = ia.draw_text(cell, y=2, x=15, text="%.1f" % (threshold, ), size=14, color=[0, 0, 0]) row.append(cell) row = np.hstack(row) masks.append(row) gridarr = np.vstack(masks) # ------------ save("alpha", "sigmoid_vary_threshold.jpg", gridarr)
def _augment_images(self, images, random_state, parents, hooks): iadt.gate_dtypes(images, allowed=[ "bool", "uint8", "uint16", "int8", "int16", "float16", "float32", "float64" ], disallowed=[ "uint32", "uint64", "uint128", "uint256", "int32", "int64", "int128", "int256", "float96", "float128", "float256" ], augmenter=self) seed = random_state.randint(0, 10**6, 1)[0] for i, image in enumerate(images): _height, _width, nb_channels = images[i].shape input_dtype = image.dtype if image.dtype.type in [np.bool_, np.float16]: image = image.astype(np.float32, copy=False) elif image.dtype.type == np.int8: image = image.astype(np.int16, copy=False) if self.matrix_type == "None": matrices = [None] * nb_channels elif self.matrix_type == "constant": matrices = [self.matrix] * nb_channels elif self.matrix_type == "function": matrices = self.matrix(images[i], nb_channels, ia.new_random_state(seed + i)) if ia.is_np_array(matrices) and matrices.ndim == 2: matrices = np.tile(matrices[..., np.newaxis], (1, 1, nb_channels)) ia.do_assert( (isinstance(matrices, list) and len(matrices) == nb_channels) or (ia.is_np_array(matrices) and matrices.ndim == 3 and matrices.shape[2] == nb_channels), "Callable provided to Convole must return either a list of 2D matrices (one per image channel) " "or a 2D numpy array " "or a 3D numpy array where the last dimension's size matches the number of image channels. " "Got type %s." % (type(matrices), )) if ia.is_np_array(matrices): # Shape of matrices is currently (H, W, C), but in the loop below we need the # first axis to be the channel index to unify handling of lists of arrays # and arrays. So we move the channel axis here to the start. matrices = matrices.transpose((2, 0, 1)) else: raise Exception("Invalid matrix type") image_aug = image for channel in sm.xrange(nb_channels): if matrices[channel] is not None: # ndimage.convolve caused problems here # cv2.filter2D() always returns same output dtype as input dtype image_aug[..., channel] = cv2.filter2D(image_aug[..., channel], -1, matrices[channel]) if input_dtype == np.bool_: image_aug = image_aug > 0.5 elif input_dtype in [np.int8, np.float16]: image_aug = iadt.restore_dtypes_(image_aug, input_dtype) images[i] = image_aug return images
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 # k with choice [a, b, c, ...] must error in case of non-discrete values got_exception = False try: _ = iaa.MotionBlur(k=[3, 3.5, 4]) except Exception as exc: assert "to only contain integer" in str(exc) got_exception = True assert got_exception # no error in case of (a, b), checks for #215 aug = iaa.MotionBlur(k=(3, 7)) for _ in range(10): _ = aug.augment_image(np.zeros((11, 11, 3), dtype=np.uint8)) # 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 _draw_samples(self, size, random_state): return self.noise.draw_samples(size, random_state=ia.new_random_state( self.seed))
def chapter_alpha_masks_simplex(): # ----------------------------------------- # example 1 (basic) # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa ia.seed(1) # Example batch of images. # The array has shape (8, 128, 128, 3) and dtype uint8. images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)], dtype=np.uint8) seq = iaa.BlendAlphaSimplexNoise( foreground=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)) images_aug = seq(images=images) # ------------ save("alpha", "alpha_simplex_example_basic.jpg", grid(images_aug, cols=4, rows=2)) # ----------------------------------------- # example 1 (per_channel) # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa ia.seed(1) # Example batch of images. # The array has shape (8, 128, 128, 3) and dtype uint8. images = np.array([ia.quokka(size=(128, 128)) for _ in range(8)], dtype=np.uint8) seq = iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0), per_channel=True) images_aug = seq(images=images) # ------------ save("alpha", "alpha_simplex_example_per_channel.jpg", grid(images_aug, cols=4, rows=2)) # ----------------------------------------- # noise masks # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa seed = 1 ia.seed(seed) seq = iaa.BlendAlphaSimplexNoise( foreground=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True)) masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = np.hstack(masks) masks = np.tile(masks[:, :, np.newaxis], (1, 1, 1, 3)) masks = (masks * 255).astype(np.uint8) # ------------ save("alpha", "alpha_simplex_noise_masks.jpg", grid(masks, cols=16, rows=1)) # ----------------------------------------- # noise masks, upscale=nearest # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa seed = 1 ia.seed(seed) seq = iaa.SimplexNoiseAlpha(first=iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True), upscale_method="nearest") masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = np.hstack(masks) masks = np.tile(masks[:, :, np.newaxis], (1, 1, 1, 3)) masks = (masks * 255).astype(np.uint8) # ------------ save("alpha", "alpha_simplex_noise_masks_nearest.jpg", grid(masks, cols=16, rows=1)) # ----------------------------------------- # noise masks linear # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa seed = 1 ia.seed(seed) seq = iaa.BlendAlphaSimplexNoise(foreground=iaa.Multiply(iap.Choice( [0.5, 1.5]), per_channel=True), upscale_method="linear") masks = [ seq.factor.draw_samples((64, 64), random_state=ia.new_random_state(seed + 1 + i)) for i in range(16) ] masks = np.hstack(masks) masks = np.tile(masks[:, :, np.newaxis], (1, 1, 1, 3)) masks = (masks * 255).astype(np.uint8) # ------------ save("alpha", "alpha_simplex_noise_masks_linear.jpg", grid(masks, cols=16, rows=1))
def main(): image = data.astronaut() image = ia.imresize_single_image(image, (128, 128)) # check if scipy and cv2 remap similarly rs = ia.new_random_state(1) aug_scipy = ElasticTransformationScipy(alpha=30, sigma=3, random_state=rs, deterministic=True) aug_cv2 = ElasticTransformationCv2(alpha=30, sigma=3, random_state=rs, deterministic=True) augs_scipy = aug_scipy.augment_images([image] * 8) augs_cv2 = aug_cv2.augment_images([image] * 8) ia.imshow(ia.draw_grid(augs_scipy + augs_cv2, rows=2)) print("alpha=vary, sigma=0.25") augs = [iaa.ElasticTransformation(alpha=alpha, sigma=0.25) for alpha in np.arange(0.0, 50.0, 0.1)] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=vary, sigma=1.0") augs = [iaa.ElasticTransformation(alpha=alpha, sigma=1.0) for alpha in np.arange(0.0, 50.0, 0.1)] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=vary, sigma=3.0") augs = [iaa.ElasticTransformation(alpha=alpha, sigma=3.0) for alpha in np.arange(0.0, 50.0, 0.1)] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=vary, sigma=5.0") augs = [iaa.ElasticTransformation(alpha=alpha, sigma=5.0) for alpha in np.arange(0.0, 50.0, 0.1)] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=1.0, sigma=vary") augs = [iaa.ElasticTransformation(alpha=1.0, sigma=sigma) for sigma in np.arange(0.0, 50.0, 0.1)] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) print("alpha=10.0, sigma=vary") augs = [iaa.ElasticTransformation(alpha=10.0, sigma=sigma) for sigma in np.arange(0.0, 50.0, 0.1)] images_aug = [aug.augment_image(image) for aug in augs] ia.imshow(ia.draw_grid(images_aug, cols=10)) kps = ia.KeypointsOnImage( [ia.Keypoint(x=1, y=1), ia.Keypoint(x=50, y=24), ia.Keypoint(x=42, y=96), ia.Keypoint(x=88, y=106), ia.Keypoint(x=88, y=53), ia.Keypoint(x=0, y=0), ia.Keypoint(x=128, y=128), ia.Keypoint(x=-20, y=30), ia.Keypoint(x=20, y=-30), ia.Keypoint(x=-20, y=-30)], shape=image.shape ) images = [] params = [ (0.0, 0.0), (0.2, 0.2), (2.0, 0.25), (0.25, 3.0), (2.0, 3.0), (6.0, 3.0), (12.0, 3.0), (50.0, 5.0), (100.0, 5.0), (100.0, 10.0) ] for (alpha, sigma) in params: images_row = [] seqs_row = [ iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=0, order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=128, order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="constant", cval=255, order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=2), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="nearest", order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=2), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="reflect", order=3), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=0), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=1), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=2), iaa.ElasticTransformation(alpha=alpha, sigma=sigma, mode="wrap", order=3) ] for seq in seqs_row: seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image) kps_aug = seq_det.augment_keypoints([kps])[0] image_aug_kp = np.copy(image_aug) image_aug_kp = kps_aug.draw_on_image(image_aug_kp, size=3) images_row.append(image_aug_kp) images.append(np.hstack(images_row)) ia.imshow(np.vstack(images)) imageio.imwrite("elastic_transformations.jpg", np.vstack(images))