def YOLO(): """ Data augmentation model for YOLOv3 training """ return iaa.Sequential([ iaa.KeepSizeByResize( iaa.Affine( scale=iap.Normal(1, 0.125), translate_percent=0.1, cval=128, )), iaa.Fliplr(0.5), iaa.Resize({ "height": iap.Normal(1, 0.1), "width": iap.Normal(1, 0.1) }), iaa.Resize({ "longer-side": 416, "shorter-side": "keep-aspect-ratio" }), iaa.PadToFixedSize(416, 416, pad_cval=128), iaa.MultiplyHueAndSaturation(mul_hue=iap.Uniform(0, 2), mul_saturation=iap.Uniform(1 / 1.5, 1.5)), iaa.AssertShape((None, 416, 416, 3)), ])
def epistemic(self, model, iterations=1000): """ estimates data uncertanity iterations: montecarlo sample iterations """ self.aug = iaa.SomeOf( 1, [ iaa.Affine(rotate=iap.Normal(0.0, 3), translate_px=iap.Normal(0.0, 3)), # iaa.AdditiveGaussianNoise(scale=0.3 * np.ptp(test_image) - 9), iaa.Noop(), iaa.MotionBlur(k=3, angle=[-2, 2]) ], random_order=True) predictions = [] for i in range(iterations): aug_image = self.aug.augment_images(self.test_image) predictions.append(model.predict(aug_image[None, ...])) predictions = np.array(predictions) mean = np.mean(predictions, axis=0) var = np.var(predictions, axis=0) return mean, var
def example_unusual_distributions(): print("Example: Unusual Distributions") from imgaug import augmenters as iaa from imgaug import parameters as iap images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8) # Blur by a value sigma which is sampled from a uniform distribution # of range 0.1 <= x < 3.0. # The convenience shortcut for this is: iaa.GaussianBlur((0.1, 3.0)) blurer = iaa.GaussianBlur(iap.Uniform(0.1, 3.0)) images_aug = blurer.augment_images(images) # Blur by a value sigma which is sampled from a normal distribution N(1.0, 0.1), # i.e. sample a value that is usually around 1.0. # Clip the resulting value so that it never gets below 0.1 or above 3.0. blurer = iaa.GaussianBlur(iap.Clip(iap.Normal(1.0, 0.1), 0.1, 3.0)) images_aug = blurer.augment_images(images) # Same again, but this time the mean of the normal distribution is not constant, # but comes itself from a uniform distribution between 0.5 and 1.5. blurer = iaa.GaussianBlur( iap.Clip(iap.Normal(iap.Uniform(0.5, 1.5), 0.1), 0.1, 3.0)) images_aug = blurer.augment_images(images) # Use for sigma one of exactly three allowed values: 0.5, 1.0 or 1.5. blurer = iaa.GaussianBlur(iap.Choice([0.5, 1.0, 1.5])) images_aug = blurer.augment_images(images) # Sample sigma from a discrete uniform distribution of range 1 <= sigma <= 5, # i.e. sigma will have any of the following values: 1, 2, 3, 4, 5. blurer = iaa.GaussianBlur(iap.DiscreteUniform(1, 5)) images_aug = blurer.augment_images(images)
def _get_jitter_augmentation(self): return iaa.Affine( translate_px={ "x": iap.Normal(0, int(self.deck.width * 0.03)), "y": iap.Normal(int(self.deck.height * 0.02), int(self.deck.height * 0.03)), })
def augment(*args, **kwargs): alpha = alpha_parameter.draw_sample() sigma = alpha / 2.5 augmenter = iaa.Sequential([ iaa.Affine( scale=iap.Normal(loc=1, scale=0.1), translate_percent=iap.Normal(loc=0, scale=0.1), shear=iap.Normal(loc=0, scale=5), backend='cv2' ), iaa.Sometimes(0.3, iaa.ElasticTransformation(alpha=alpha, sigma=sigma)) ], random_order=False) return augmenter(*args, **kwargs)
def chapter_augmenters_blendalphasimplexnoise(): fn_start = "blend/blendalphasimplexnoise" aug = iaa.SimplexNoiseAlpha(iaa.EdgeDetect(1.0)) run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.SimplexNoiseAlpha(iaa.EdgeDetect(1.0), upscale_method="nearest") run_and_save_augseq(fn_start + "_nearest.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.SimplexNoiseAlpha(iaa.EdgeDetect(1.0), upscale_method="linear") run_and_save_augseq(fn_start + "_linear.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.SimplexNoiseAlpha(iaa.EdgeDetect(1.0), sigmoid_thresh=iap.Normal(10.0, 5.0)) run_and_save_augseq(fn_start + "_sigmoid_thresh_normal.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2)
def __init__(self, **kwargs): super().__init__(**kwargs) assert kwargs['h'] == 144 assert kwargs['w'] == 256 self.augmentations = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Affine(scale={ 'x': (0.95, 1.05), 'y': 1 }, rotate=iap.Normal(0, 10)), iaa.Sometimes( 0.2, iaa.OneOf([ iaa.Sequential([ iaa.pillike.EnhanceBrightness((0.2, 0.9)), iaa.LinearContrast((0.75, 1.5)), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.1 * 255), per_channel=0.5), iaa.GaussianBlur(sigma=(0, 1)), ]), iaa.pillike.EnhanceBrightness((1.1, 1.6)) ])) ], random_order=False)
def chapter_parameters_introduction(): ia.seed(1) from imgaug import augmenters as iaa from imgaug import parameters as iap seq = iaa.Sequential([ iaa.GaussianBlur(sigma=iap.Uniform(0.0, 1.0)), iaa.ContrastNormalization( iap.Choice([1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2])), iaa.Affine(rotate=iap.Normal(0.0, 30), translate_px=iap.RandomSign(iap.Poisson(3))), iaa.AddElementwise(iap.Discretize( (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64)), iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.1)) + 1.0) ]) images = np.array([ia.quokka_square(size=(128, 128)) for i in range(16)]) images_aug = [seq.augment_image(images[i]) for i in range(len(images))] save("parameters", "introduction.jpg", grid(images_aug, cols=4, rows=4))
def get_noise_augmenters(augmenter_level): """ Gets an augmenter object of a given level """ # 0 heavy , 1 medium,2 simple if augmenter_level == 0: ####################################################### heavy augmentation ######################################################################### return [ iaa.Sometimes( 0.9, iaa.Crop(percent=((iap.Clip(iap.Normal(0, .5), 0, .6), ) * 4) # random crops top,right,bottom,left )), # some noise iaa.Sometimes(0.9, [ iaa.GaussianBlur(sigma=(0, 0.3)), iaa.Sharpen(alpha=(0.0, .15), lightness=(0.5, 1.5)), iaa.Emboss(alpha=(0.0, 1.0), strength=(0.1, 0.2)) ]), iaa.Sometimes(0.9, iaa.Add((-12, 12), per_channel=1)) ] # rgb jittering elif augmenter_level == 1: ####################################################### medium augmentation ####################################################################### return [ iaa.Sometimes( 0.9, iaa.Crop( percent=((0.0, 0.15), (0.0, 0.15), (0.0, 0.15), (0.0, 0.15)))), # random crops top,right,bottom,left # some noise iaa.Sometimes(0.5, [ iaa.GaussianBlur(sigma=(0, 0.25)), iaa.Sharpen(alpha=(0.0, .1), lightness=(0.5, 1.25)), iaa.Emboss(alpha=(0.0, 1.0), strength=(0.05, 0.1)) ]), iaa.Sometimes(.7, iaa.Add((-10, 10), per_channel=1)) ] # rgb jittering elif augmenter_level == 2: ######################################################## simple augmentation ####################################################################### return [ iaa.Sometimes( 0.6, iaa.Crop( percent=((0.0, 0.1), (0.0, 0.1), (0.0, 0.1), (0.0, 0.1)))), # random crops top,right,bottom,left # some noise iaa.Sometimes(0.35, [ iaa.GaussianBlur(sigma=(0, 0.17)), iaa.Sharpen(alpha=(0.0, .07), lightness=(0.35, 1)), iaa.Emboss(alpha=(0.0, .7), strength=(0.1, 0.7)) ]), iaa.Sometimes(.45, iaa.Add((-7, 7), per_channel=1)) ] # rgb jittering
def make_synthetic_prev_mask_complex_mask_augmenter(crop_size): h, w = crop_size return iaa.Sequential([ iaa.Sometimes(0.5, iaa.Lambda(func_segmentation_maps=choose_random_objects_mask_augmenter)), iaa.Lambda(func_segmentation_maps=morph_close_mask_augmenter), iaa.Sometimes(0.3, # failed mask iaa.OneOf([ iaa.TotalDropout(1.0), # fill image iaa.Sequential([ # fail mask iaa.OneOf([ iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.erode, min_coef=0.2, max_coef=0.5)), iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.dilate, min_coef=0.2, max_coef=0.5)), ]), iaa.Affine(translate_percent=iap.Choice([iap.Uniform(-0.5, -0.2), iap.Uniform(0.2, 0.5)])) ]) ]), # normal mask iaa.Sequential([ iaa.Sometimes(0.1, iaa.OneOf([ iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.erode)), # smaller mask iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.dilate)), # larger mask ])), iaa.Sometimes(1.0, iaa.Affine( scale=iap.Normal(loc=1, scale=0.02), translate_percent=iap.Normal(loc=0, scale=0.03), shear=iap.Normal(loc=0, scale=1), backend='cv2' )), iaa.Sometimes(0.1, iaa.ElasticTransformation(alpha=2000, sigma=50) ), iaa.Sometimes(0.1, iaa.PiecewiseAffine() ) ]) ) ], random_order=False)
def get_aug(aug_cfg): seq = iaa.Sequential( [ iaa.Sometimes( aug_cfg["Shear"]["p"], iaa.OneOf([ iaa.ShearX(aug_cfg["Shear"]["X"]), iaa.ShearY(aug_cfg["Shear"]["Y"]), ]), ), iaa.Sometimes( aug_cfg["GaussianBlur"]["p"], iaa.GaussianBlur(sigma=aug_cfg["GaussianBlur"]["sigma"]), ), iaa.OneOf([ iaa.LinearContrast(aug_cfg["LinearContrast"]["alpha"]), iaa.AdditiveGaussianNoise( loc=aug_cfg["AdditiveGaussianNoise"]["loc"], scale=aug_cfg["AdditiveGaussianNoise"]["scale"], per_channel=aug_cfg["AdditiveGaussianNoise"] ["per_channel"], ), iaa.Multiply( aug_cfg["Multiply"]["mul"], per_channel=aug_cfg["Multiply"]["per_channel"], ), ]), iaa.Sometimes( aug_cfg["Affine"]["p"], iaa.Affine( translate_percent=iap.Normal( *aug_cfg["Affine"]["translate_percent"]), rotate=iap.Normal(*aug_cfg["Affine"]["rotate"]), scale=None, ), ), ], random_order=True, ) return seq
def chapter_augmenters_replaceelementwise(): aug_cls = iaa.ReplaceElementwise fn_start = "arithmetic/replaceelementwise" aug = aug_cls(0.05, [0, 255]) run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2) aug = aug_cls(0.05, [0, 255], per_channel=0.5) run_and_save_augseq(fn_start + "_per_channel_050.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2) aug = aug_cls(0.1, iap.Normal(128, 0.4 * 128), per_channel=0.5) run_and_save_augseq(fn_start + "_gaussian_noise.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2) aug = aug_cls(iap.FromLowerResolution(iap.Binomial(0.1), size_px=8), iap.Normal(128, 0.4 * 128), per_channel=0.5) run_and_save_augseq(fn_start + "_gaussian_noise_coarse.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2)
def stochastic(): return iaa.Sequential([ iaa.GaussianBlur( sigma=iap.Uniform(0.0, 1.0) ), iaa.ContrastNormalization( iap.Choice( [1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2] ) ), iaa.Affine( rotate=iap.Normal(0.0, 30), translate_px=iap.RandomSign(iap.Poisson(3)) ), iaa.AddElementwise( iap.Discretize( (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64 ) ), iaa.Multiply( iap.Positive(iap.Normal(0.0, 0.1)) + 1.0 ) ])
def __init__(self, folders_train, folders_val, num_classes): ''' All paths to the images to train and validate. Inputs --------- folders_train : list A list of folders where are the dataset to train. folders_val : list A list of folders where are the dataset to validation. num_classes : int Number of classes ''' self.num_classes = num_classes # map paths and split dataset self.path_train = [] self.path_test = [] for path in folders_train: self.path_train.extend(glob(os.path.join(path, '*_image.jpg'))) for path in folders_val: self.path_test.extend(glob(os.path.join(path, '*_image.jpg'))) print( f'Total train: {len(self.path_train)}, Total val: {len(self.path_test)}' ) # options for augmentation self.aug = iaa.SomeOf((0, 3), [ iaa.Affine(rotate=(-10, 10), scale={ "x": (0.5, 1.2), "y": (0.5, 1.2) }), iaa.AdditiveGaussianNoise(scale=0.2 * 255), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.BlendAlphaSimplexNoise(iaa.EdgeDetect(1.0), sigmoid_thresh=iap.Normal(10.0, 5.0)), iaa.Add(50, per_channel=True), iaa.WithChannels(0, iaa.Add((10, 100))), iaa.Sharpen(alpha=0.2), iaa.Fliplr(), iaa.Flipud() ])
def chapter_augmenters_clahe(): fn_start = "contrast/clahe" aug = iaa.CLAHE() run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.CLAHE(clip_limit=(1, 10)) run_and_save_augseq(fn_start + "_clip_limit.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.CLAHE(tile_grid_size_px=(3, 21)) run_and_save_augseq(fn_start + "_grid_sizes_uniform.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.CLAHE(tile_grid_size_px=iap.Discretize(iap.Normal(loc=7, scale=2)), tile_grid_size_px_min=3) run_and_save_augseq(fn_start + "_grid_sizes_gaussian.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.CLAHE(tile_grid_size_px=((3, 21), [3, 5, 7])) run_and_save_augseq(fn_start + "_grid_sizes.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.CLAHE(from_colorspace=iaa.CLAHE.BGR, to_colorspace=iaa.CLAHE.HSV) quokka_bgr = cv2.cvtColor(ia.quokka(size=(128, 128)), cv2.COLOR_RGB2BGR) run_and_save_augseq(fn_start + "_bgr_to_hsv.jpg", aug, [quokka_bgr for _ in range(4 * 2)], cols=4, rows=2, image_colorspace="BGR")
def example_probability_distributions_as_parameters(): print("Example: Probability Distributions as Parameters") import numpy as np from imgaug import augmenters as iaa from imgaug import parameters as iap images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8) # Blur by a value sigma which is sampled from a uniform distribution # of range 10.1 <= x < 13.0. # The convenience shortcut for this is: GaussianBlur((10.1, 13.0)) blurer = iaa.GaussianBlur(10 + iap.Uniform(0.1, 3.0)) images_aug = blurer(images=images) # Blur by a value sigma which is sampled from a gaussian distribution # N(1.0, 0.1), i.e. sample a value that is usually around 1.0. # Clip the resulting value so that it never gets below 0.1 or above 3.0. blurer = iaa.GaussianBlur(iap.Clip(iap.Normal(1.0, 0.1), 0.1, 3.0)) images_aug = blurer(images=images)
def __init__(self): self.aug = iaa.Sequential([ iaa.Scale((224, 224)), iaa.Sometimes(0.30, iaa.GaussianBlur(sigma=(0, 3.0))), iaa.Sometimes(0.25, iaa.Multiply((0.5, 1.5), per_channel=0.5)), iaa.Sometimes(0.20, iaa.Invert(0.25, per_channel=0.5)), iaa.Sometimes(0.25, iaa.ReplaceElementwise( iap.FromLowerResolution(iap.Binomial(0.1), size_px=8), iap.Normal(128, 0.4*128), per_channel=0.5) ), iaa.Sometimes(0.30, iaa.AdditivePoissonNoise(40)), iaa.Fliplr(0.5), iaa.Affine(rotate=(-20, 20), mode='symmetric'), iaa.Sometimes(0.30, iaa.OneOf([iaa.Dropout(p=(0, 0.1)), iaa.CoarseDropout(0.1, size_percent=0.5)])), iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True) ])
def chapter_augmenters_blendalphafrequencynoise(): fn_start = "blend/blendalphafrequencynoise" aug = iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0)) run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), upscale_method="nearest") run_and_save_augseq(fn_start + "_nearest.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), upscale_method="linear") run_and_save_augseq(fn_start + "_linear.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), upscale_method="linear", exponent=-2, sigmoid=False) run_and_save_augseq(fn_start + "_clouds.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2) aug = iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), sigmoid_thresh=iap.Normal(10.0, 5.0)) run_and_save_augseq(fn_start + "_sigmoid_thresh_normal.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 2)], cols=4, rows=2)
def _get_tilt_augmentation(self, degrees: int): # 0.9 -> sometimes players hold their cards slightly overlapping # 1.3 -> but more often they leave a lot of extra space min_degrees, max_degrees = degrees * 0.9, min(degrees * 1.3, MAX_FAN_ANGLE) # we want to rotate from the bottom-left corner, so have to translate back and forth return iaa.Sequential([ iaa.Affine(translate_px={ "x": int(self.deck.width / 2), "y": int(-self.deck.height / 2), }), iaa.Affine(rotate=iap.Normal((min_degrees + max_degrees) / 2, (max_degrees - min_degrees) / 2)), iaa.Affine(translate_px={ "x": int(-self.deck.width / 2), "y": int(self.deck.height / 2), }), ])
def aug_data(x_data, y_data, percent, aug_percent): rand_vector = np.linspace(0, np.shape(x_data)[0], num=int(percent*np.shape(x_data)[0]/100), endpoint=False, dtype = int) for count in range(0, len(rand_vector)): ppl = Image.fromarray(x_data[rand_vector[count],:,:,:].astype('uint8')) ppl = np.array(ppl) seq1 = iaa.Sequential([ iaa.ReplaceElementwise( iap.FromLowerResolution(iap.Binomial(aug_percent), size_px=4), iap.Normal(128, 0.4*128), per_channel=1) ]) aug_image = seq1(images=ppl) x_data[rand_vector[count],:,:,:] = np.copy(aug_image) return x_data, y_data
def chapter_parameters_continuous(): ia.seed(1) # ----------------------- # Normal # ----------------------- from imgaug import parameters as iap params = [ iap.Normal(0, 1), iap.Normal(5, 3), iap.Normal(iap.Choice([-3, 3]), 1), iap.Normal(iap.Uniform(-3, 3), 1) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_normal.jpg", gridarr) # ----------------------- # Laplace # ----------------------- from imgaug import parameters as iap params = [ iap.Laplace(0, 1), iap.Laplace(5, 3), iap.Laplace(iap.Choice([-3, 3]), 1), iap.Laplace(iap.Uniform(-3, 3), 1) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_laplace.jpg", gridarr) # ----------------------- # ChiSquare # ----------------------- from imgaug import parameters as iap params = [ iap.ChiSquare(1), iap.ChiSquare(3), iap.ChiSquare(iap.Choice([1, 5])), iap.RandomSign(iap.ChiSquare(3)) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_chisquare.jpg", gridarr) # ----------------------- # Weibull # ----------------------- from imgaug import parameters as iap params = [ iap.Weibull(0.5), iap.Weibull(1), iap.Weibull(1.5), iap.Weibull((0.5, 1.5)) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_weibull.jpg", gridarr) # ----------------------- # Uniform # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1), iap.Uniform(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Uniform([-1, 0], 1), iap.Uniform((-1, 0), 1) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_uniform.jpg", gridarr) # ----------------------- # Beta # ----------------------- from imgaug import parameters as iap params = [ iap.Beta(0.5, 0.5), iap.Beta(2.0, 2.0), iap.Beta(1.0, 0.5), iap.Beta(0.5, 1.0) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_beta.jpg", gridarr)
iaa.OneOf([ iaa.imgcorruptlike.ShotNoise(severity=(1, 2)), iaa.imgcorruptlike.ImpulseNoise(severity=(1, 2)), iaa.imgcorruptlike.SpeckleNoise(severity=(1, 2)), iaa.imgcorruptlike.Spatter(severity=(1, 3)), iaa.AdditivePoissonNoise((1, 20), per_channel=0.5), iaa.AdditiveLaplaceNoise(scale=(0.005 * 255, 0.03 * 255), per_channel=0.5), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03 * 255), per_channel=0.5), iaa.BlendAlphaElementwise((0.0, 1.0), foreground=iaa.Add((-15, 15)), background=iaa.Multiply((0.8, 1.2))), iaa.ReplaceElementwise(0.05, iap.Normal(128, 0.4 * 128), per_channel=0.5), iaa.Dropout(p=(0, 0.05), per_channel=0.5), ])), # Brightness + Color + Contrast iaa.Sometimes( 0.5, iaa.OneOf([ iaa.Add(iap.Normal(iap.Choice([-30, 30]), 10)), iaa.Multiply((0.75, 1.25)), iaa.AddToBrightness((-35, 35)), iaa.MultiplyBrightness((0.85, 1.15)), iaa.MultiplyAndAddToBrightness(mul=(0.85, 1.15), add=(-10, 10)), iaa.BlendAlphaHorizontalLinearGradient(iaa.Add( iap.Normal(iap.Choice([-40, 40]), 10)), start_at=(0, 0.2),
def chapter_parameters_special(): ia.seed(1) # ----------------------- # Choice # ----------------------- from imgaug import parameters as iap params = [ iap.Choice([0, 1, 2]), iap.Choice([0, 1, 2], p=[0.15, 0.5, 0.35]), iap.Choice([iap.Normal(-3, 1), iap.Normal(3, 1)]), iap.Choice([iap.Normal(-3, 1), iap.Poisson(3)]) ] gridarr = draw_distributions_grid(params) save("parameters", "special_choice.jpg", gridarr) # ----------------------- # Clip # ----------------------- from imgaug import parameters as iap params = [ iap.Clip(iap.Normal(0, 1), -2, 2), iap.Clip(iap.Normal(0, 1), -2, None) ] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "special_clip.jpg", gridarr) # ----------------------- # Discretize # ----------------------- from imgaug import parameters as iap params = [ iap.Discretize(iap.Normal(0, 1)), iap.Discretize(iap.ChiSquare(3)) ] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "special_discretize.jpg", gridarr) # ----------------------- # Absolute # ----------------------- from imgaug import parameters as iap params = [iap.Absolute(iap.Normal(0, 1)), iap.Absolute(iap.Laplace(0, 1))] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "special_absolute.jpg", gridarr) # ----------------------- # RandomSign # ----------------------- from imgaug import parameters as iap params = [ iap.ChiSquare(3), iap.RandomSign(iap.ChiSquare(3)), iap.RandomSign(iap.ChiSquare(3), p_positive=0.75), iap.RandomSign(iap.ChiSquare(3), p_positive=0.9) ] gridarr = draw_distributions_grid(params) save("parameters", "special_randomsign.jpg", gridarr) # ----------------------- # ForceSign # ----------------------- from imgaug import parameters as iap params = [ iap.ForceSign(iap.Normal(0, 1), positive=True), iap.ChiSquare(3) - 3.0, iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="invert"), iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="reroll") ] gridarr = draw_distributions_grid(params) save("parameters", "special_forcesign.jpg", gridarr)
def chapter_parameters_arithmetic(): ia.seed(1) # ----------------------- # Add # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1) + 1, # identical to: Add(Uniform(0, 1), 1) iap.Add(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])), iap.Normal(0, 1) + iap.Uniform(-5.5, -5) + iap.Uniform(5, 5.5), iap.Normal(0, 1) + iap.Uniform(-7, 5) + iap.Poisson(3), iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True) ] gridarr = draw_distributions_grid( params, rows=2, sample_sizes=[ # (iterations, samples per iteration) (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000), (1, 100000), (1, 100000) ]) save("parameters", "arithmetic_add.jpg", gridarr) # ----------------------- # Multiply # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1) * 2, # identical to: Multiply(Uniform(0, 1), 2) iap.Multiply(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])), (iap.Normal(0, 1) * iap.Uniform(-5.5, -5)) * iap.Uniform(5, 5.5), (iap.Normal(0, 1) * iap.Uniform(-7, 5)) * iap.Poisson(3), iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True) ] gridarr = draw_distributions_grid( params, rows=2, sample_sizes=[ # (iterations, samples per iteration) (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000), (1, 100000), (1, 100000) ]) save("parameters", "arithmetic_multiply.jpg", gridarr) # ----------------------- # Divide # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1) / 2, # identical to: Divide(Uniform(0, 1), 2) iap.Divide(iap.Uniform(0, 1), iap.Choice([0, 2], p=[0.7, 0.3])), (iap.Normal(0, 1) / iap.Uniform(-5.5, -5)) / iap.Uniform(5, 5.5), (iap.Normal(0, 1) * iap.Uniform(-7, 5)) / iap.Poisson(3), iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1)), iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True) ] gridarr = draw_distributions_grid( params, rows=2, sample_sizes=[ # (iterations, samples per iteration) (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000), (1, 100000), (1, 100000) ]) save("parameters", "arithmetic_divide.jpg", gridarr) # ----------------------- # Power # ----------------------- from imgaug import parameters as iap params = [ iap.Uniform(0, 1)**2, # identical to: Power(Uniform(0, 1), 2) iap.Clip(iap.Uniform(-1, 1)**iap.Normal(0, 1), -4, 4) ] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "arithmetic_power.jpg", gridarr)
def apply_transform_on_an_image(image, bbox=None): # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second # image. sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBs # Small gaussian blur with random sigma between 0 and 0.5. # But we only blur about 50% of all images. # iaa.Sometimes(0.5, # iaa.GaussianBlur(sigma=iap.Uniform(0.0, 1.0)) # ), iaa.ContrastNormalization( iap.Choice([1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2])), # Add gaussian noise. # For 50% of all images, we sample the noise once per pixel. # For the other 50% of all images, we sample the noise per pixel AND # channel. This can change the color (not only brightness) of the # pixels. # iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # Make some images brighter and some darker. # In 20% of all cases, we sample the multiplier once per channel, # which can end up changing the color of the images. iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.1)) + 1.0, per_channel=0.2), # Apply affine transformations to each image. # - scale to 80-120% of image height/width (each axis independently) # - translate by -20 to +20 relative to height/width (per axis) # - rotate by -45 to +45 degrees # - shear by -16 to +16 degrees # - order: use nearest neighbour or bilinear interpolation (fast) # - mode: use any available mode to fill newly created pixels # see API or scikit-image for which modes are available # - cval: if the mode is constant, then use a random brightness # for the newly created pixels (e.g. sometimes black, # sometimes white) # iaa.Affine( # scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # # translate_percent=0.1, # rotate=iap.Normal(-45, 45), # shear=(-8, 8), # order=[0, 1], # cval=(0, 255), # # translate_px=iap.RandomSign(iap.Poisson(3)), # # translate_px=3, # # mode=["constant", "edge"] # mode=ia.ALL # ), # iaa.AddElementwise( # iap.Discretize( # (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64 # ) # ), # # Execute 0 to 5 of the following (less important) augmenters per # image. Don't execute all of them, as that would often be way too # strong. # iaa.SomeOf( (0, 5), [ # Convert some images into their superpixel representation, # sample between 20 and 200 superpixels per image, but do # not replace all superpixels with their average, only # some of them (p_replace). # sometimes( # iaa.Superpixels( # p_replace=(0, 0.5), # n_segments=(1, 4) # ) # ), # Blur each image with varying strength using # gaussian blur (sigma between 0 and 3.0), # average/uniform blur (kernel size between 2x2 and 7x7) # median blur (kernel size between 3x3 and 11x11). iaa.OneOf([ iaa.GaussianBlur((0, 0.3)), iaa.AverageBlur(k=(1, 2)), iaa.MedianBlur(k=(1, 3)), ]), # iaa.OneOf([ # iaa.GaussianBlur((0, 3.0)), # iaa.AverageBlur(k=(2, 7)), # iaa.MedianBlur(k=(3, 11)), # ]), # Sharpen each image, overlay the result with the original # image using an alpha between 0 (no sharpening) and 1 # (full sharpening effect). iaa.Sharpen(alpha=(0, 0.01), lightness=(0, 0.01)), # Same as sharpen, but for an embossing effect. iaa.Emboss(alpha=(0, 0.01), strength=(0, 0.01)), # Search in some images either for all edges or for # directed edges. These edges are then marked in a black # and white image and overlayed with the original image # using an alpha of 0 to 0.7. sometimes( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.005)), iaa.DirectedEdgeDetect(alpha=(0, 0.001), direction=(0.0, 0.001)), ])), # Add gaussian noise to some images. # In 50% of these cases, the noise is randomly sampled per # channel and pixel. # In the other 50% of all cases it is sampled once per # pixel (i.e. brightness change). # iaa.AdditiveGaussianNoise( # loc=0, scale=(0.0, 0.001 * 255), per_channel=0.5 # ), # Either drop randomly 1 to 10% of all pixels (i.e. set # them to black) or drop them on an image with 2-5% percent # of the original size, leading to large dropped # rectangles. # iaa.OneOf([ # iaa.Dropout((0, 0.05), per_channel=0.5), # iaa.CoarseDropout( # (0, 0.01), size_percent=(0.1, 0.2), # per_channel=0.2 # ), # ]), # Invert each image's channel with 5% probability. # This sets each pixel value v to 255-v. iaa.Invert(0.1, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. #iaa.Add((-40, 40), per_channel=0.5), # Change brightness of images (50-150% of original value). # iaa.Multiply((0.5, 1.5), per_channel=0.5), # Improve or worsen the contrast of images. # iaa.contrast.LinearContrast((0.5, 2.0), per_channel=0.5), # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. # iaa.Grayscale(alpha=(0.0, 1.0)), # In some images move pixels locally around (with random # strengths). # sometimes( # iaa.ElasticTransformation(alpha=(0.5, 1.5), sigma=0.25) # ), # In some images distort local areas with varying strength. # sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) ], # do all of the above augmentations in random order random_order=True) ], random_order=True) # Augment BBs and images. aug_image = seq(image=image) return aug_image
import pandas as pd import glob import imgaug as ia import imgaug.augmenters as iaa import imgaug.parameters as iap from imgaug.augmentables.kps import Keypoint, KeypointsOnImage from imgaug.augmenters import Sequential import sys seq = iaa.Sequential( [ iaa.Affine( # rotate=iap.Normal(0.0, 20), translate_px=iap.RandomSign(iap.Poisson(3))), #iaa.Crop(percent=(0, 0.2)), iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.4)) + 0.8), iaa.ContrastNormalization(iap.Uniform(0.5, 1.5)) ], random_order=True) class DataAugmentator(object): """ Generates new images in form of arrays for the given parameters. :param seq: a sequention of different augmenters applied to single augmentatiion call :param landmarks_num: number of image's landmarks :param batch_size: number of images that would be generated during augmentation for single image :param img_dir: directory to images to be augmented :param annotation_dir: directory to images' landmarks :param mask_dir: directory to images' masks :param output_dir: directory for augmented images
new_bndbox = [] new_bndbox_list = [] # 影像增强 seq = iaa.Sequential( iaa.SomeOf( (1, 6), [ iaa.Flipud(0.2), # vertically flip 20% of all images iaa.Resize((0.5, 1.0)), iaa.CropAndPad(percent=(-0.2, 0.2)), iaa.Fliplr(0.2), # 镜像 iaa.WithColorspace(to_colorspace="HSV", from_colorspace="BGR", children=iaa.WithChannels( 0, iaa.Add(40 * iap.Normal(0, 1)))), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.Multiply( (1.2, 1.5)), # change brightness, doesn't affect BBs iaa.GaussianBlur(sigma=(0, 3.0)), # iaa.GaussianBlur(0.5), # iaa.ContrastNormalization(iap.Normal(0.5, 1), per_channel=0.5) ], random_order=True)) for root, sub_folders, files in os.walk(XML_DIR): newid = 2000 for name in files: bndbox = read_xml_annotation(XML_DIR, name)