def scale(image): # scale image randomly between 0.5 and 1.5 aug1 = iaa.ScaleX((0.5, 1.5)) aug2 = iaa.ScaleY((0.5, 1.5)) image_aug = aug1(image=image) image_aug = aug2(image=image_aug) return image_aug
def initialise_augmenter(): # Horizontal and Vertical Flips (set to 1 as the SomeOf function will choose when to apply these itself) horizontal_flip = iaa.Fliplr(1) #0.5) vertical_flip = iaa.Flipud(1) #0.5) # 90, 180 and 270 degree rotations rotate_90 = iaa.Affine(rotate=90) rotate_180 = iaa.Affine(rotate=180) rotate_270 = iaa.Affine(rotate=270) # Translations of -10% to 10% of the image's pixels translate_x = iaa.TranslateX(percent=(-0.1, 0.1)) translate_y = iaa.TranslateY(percent=(-0.1, 0.1)) # Scale the image between 0.75 and 1.1 of the original size scale_x = iaa.ScaleX((0.75, 1.1)) scale_y = iaa.ScaleY((0.75, 1.1)) # Shear the image between -20 and 20 degrees shear_x = iaa.ShearX((-20, 20)) shear_y = iaa.ShearY((-20, 20)) augmentation = iaa.SomeOf((0, None), [ horizontal_flip, vertical_flip, iaa.OneOf([rotate_90, rotate_180, rotate_270]), translate_x, translate_y, scale_x, scale_y, shear_x, shear_y ], random_order=True) return augmentation
def chapter_augmenters_scaley(): fn_start = "geometric/scaley" image = ia.quokka(size=(128, 128)) aug = iaa.ScaleY((0.5, 1.5)) run_and_save_augseq( fn_start + ".jpg", aug, [image for _ in range(4*2)], cols=4, rows=2)
# iaa.AdditiveGaussianNoise( # loc=0, scale=(0.0, 0.02*255), per_channel=0.5 # ), # iaa.Add((-15, 15), per_channel=0.5), # iaa.Multiply((0.8, 1.2), per_channel=0.5), # iaa.imgcorruptlike.Contrast(severity=1), # iaa.imgcorruptlike.Brightness(severity=2), iaa.ContrastNormalization((0.1, 1.5), per_channel=0.5), iaa.WithHueAndSaturation([ iaa.WithChannels(0, iaa.Add((-15, 15))), iaa.WithChannels(1, iaa.Add((-20, 20))), ]), iaa.GammaContrast((0.3, 1.5)), iaa.WithBrightnessChannels(iaa.Add((-30, 70))), iaa.ScaleX((0.5, 1.5)), iaa.ScaleY((0.5, 1.5)), iaa.ShearX((-10, 10)), iaa.ShearY((-10, 10)), ], random_order=True) ], random_order=True) def augment_pair(fg, label): # print('Augment start') label_i, segmaps_aug_i = seq(images=fg, segmentation_maps=label) # print('Augment ok') return label_i, segmaps_aug_i
def resize_image(image, target): image = tf.image.resize(image, INPUT_SHAPE[:2]) return image, target # augmentation options AUG = iaa.Sequential([ iaa.SomeOf((0, 1), [ iaa.AddToBrightness((-30, 30)), iaa.MultiplyBrightness((0.5, 1.5)), iaa.MultiplySaturation((0.5, 1.5)), iaa.AddToSaturation((-50, 50)) ]), iaa.OneOf([ iaa.ScaleX((1.0, 1.5)), iaa.ScaleY((1.0, 1.5)), iaa.Affine(scale={ "x": (1.0, 1.2), "y": (1.0, 1.2) }), iaa.Affine(rotate=(-20, 20)), iaa.PiecewiseAffine(scale=(0.01, 0.05)), iaa.Affine(shear=(-16, 16)) ]), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Rot90([1, 2, 3, 4]) ]) # cutmix augmentation beta = 0.5
row_images.append(row[:]) image = row_images[0] for i in range(1, rows): image = np.vstack((image, row_images[i])) # Display plt.figure(figsize=(cols*3, rows*3)) plt.imshow(image) ia.seed(GROUP_NUMBER) #Using augmenter to augment the image sequential_augmenter = iaa.Sequential([ iaa.ChannelShuffle(0.35), iaa.ScaleX((0.9, 1.1)), iaa.ScaleY((0.9, 1.1)), iaa.Sometimes(0.5, iaa.OneOf([iaa.Fliplr(), iaa.Flipud()])), iaa.Sometimes(0.5, iaa.Rot90((1,3))), ], random_order = True) augmented_images = sequential_augmenter.augment_images(images) # Sample 15 images cols = 9 rows = 5 row_images = [] for i in range(rows): indices = np.random.rand(cols) indices = (indices*NUMBER_IMAGES).astype(np.int64)
iaa.SigmoidContrast(gain=10, cutoff=0.3), iaa.LogContrast(0.7), iaa.LogContrast(1.3), iaa.Sharpen(alpha=0.2, lightness=0.9), iaa.Sharpen(alpha=0.2, lightness=1.2), iaa.Fliplr(1), iaa.Flipud(1), iaa.Rotate(15), iaa.Rotate(-15), iaa.ShearX(-10), iaa.ShearX(10), iaa.ShearY(-10), iaa.ShearY(10), iaa.ScaleX(0.7), iaa.ScaleX(1.3), iaa.ScaleY(0.7), iaa.ScaleY(1.3), ] def custom_imshow(imgList, labels): fig = plt.figure() rows = 2 cols = 5 for i in range(10): img = imgList[i] temp = fig.add_subplot(rows, cols, i+1) temp.set_title(classes[labels[i]]) temp.imshow(np.transpose(img, (1, 2, 0)))
elif augmentation == 'piecewise_affine': transform = iaa.PiecewiseAffine(scale=(0.05, 0.09)) transformed_image = transform(image=image) elif augmentation == 'shift_scale_rotate': transform = ShiftScaleRotate(always_apply=True, shift_limit=0.1, scale_limit=0.5) transformed_image = transform(image=image)['image'] elif augmentation == 'scalex': transform = iaa.ScaleX((0.5, 1.5)) transformed_image = transform(image=image) elif augmentation == 'scaley': transform = iaa.ScaleY((0.5, 1.5)) transformed_image = transform(image=image) elif augmentation == 'translatex': transform = iaa.TranslateX(px=(200)) transformed_image = transform(image=image) elif augmentation == 'translatey': transform = iaa.TranslateY(px=(-200)) transformed_image = transform(image=image) ## Crop elif augmentation == 'crop': transform = Crop(always_apply=True, x_max=400, y_max=400) transformed_image = transform(image=image)['image']
result_originals = path.joinpath("AugmentedOriginals") result_masks = path.joinpath("AugmentedMasks") for i in range(IMAGE_COUNT): istr = str(i % 900) + ".jpg" original = imageio.imread(originals.joinpath(istr)) mask = imageio.imread(masks.joinpath(istr)) mask = SegmentationMapsOnImage(mask, shape=mask.shape) seq = iaa.SomeOf((0, None), random_order=True) seq.add(iaa.Add((-40, 40), per_channel=0.5)) seq.add(iaa.GaussianBlur(sigma=(0, 2))) seq.add(iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.3, 0.75), per_channel=True)) seq.add(iaa.HorizontalFlip()) seq.add(iaa.VerticalFlip()) seq.add(iaa.TranslateX(percent=(-0.7, 0.7), cval=33)) seq.add(iaa.TranslateY(percent=(-0.7, 0.7), cval=33)) seq.add(iaa.Rotate(random.randrange(-60, 60), cval=33)) seq.add(iaa.ScaleX((0.5, 1.5), cval=33)) seq.add(iaa.ScaleY((0.5, 1.5), cval=33)) seq.add(iaa.imgcorruptlike.DefocusBlur(severity=1)) aug = iaa.CropAndPad(percent=([-0.3, 0.3], [-0.3, 0.3], [-0.3, 0.3], [-0.3, 0.3])) results_o, results_m = seq(image=original, segmentation_maps=mask) istr = str(i) + ".jpg" imageio.imsave(result_originals.joinpath(istr), results_o) imageio.imsave(result_masks.joinpath(istr), results_m.arr)