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 chapter_alpha_masks_introduction(): # ----------------------------------------- # example introduction # ----------------------------------------- import imgaug as ia from imgaug import augmenters as iaa ia.seed(2) # 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) seqs = [ iaa.BlendAlpha((0.0, 1.0), foreground=iaa.MedianBlur(11), per_channel=True), iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0), per_channel=False), iaa.BlendAlphaSimplexNoise(foreground=iaa.EdgeDetect(1.0), background=iaa.LinearContrast((0.5, 2.0)), per_channel=0.5), iaa.BlendAlphaFrequencyNoise(foreground=iaa.Affine(rotate=(-10, 10), translate_px={ "x": (-4, 4), "y": (-4, 4) }), background=iaa.AddToHueAndSaturation( (-40, 40)), per_channel=0.5), iaa.BlendAlphaSimplexNoise(foreground=iaa.BlendAlphaSimplexNoise( foreground=iaa.EdgeDetect(1.0), background=iaa.LinearContrast((0.5, 2.0)), per_channel=True), background=iaa.BlendAlphaFrequencyNoise( exponent=(-2.5, -1.0), foreground=iaa.Affine(rotate=(-10, 10), translate_px={ "x": (-4, 4), "y": (-4, 4) }), background=iaa.AddToHueAndSaturation( (-40, 40)), per_channel=True), per_channel=True, aggregation_method="max", sigmoid=False) ] cells = [] for seq in seqs: images_aug = seq(images=images) cells.extend(images_aug) # ------------ save("alpha", "introduction.jpg", grid(cells, cols=8, rows=5))
def main(): nb_rows = 8 nb_cols = 8 h, w = (128, 128) sample_size = 128 noise_gens = [ iap.SimplexNoise(), iap.FrequencyNoise(exponent=-4, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=-2, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=0, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=2, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=4, size_px_max=sample_size, upscale_method="cubic"), iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]), iap.IterativeNoiseAggregator( other_param=iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]), iterations=(1, 3), aggregation_method=["max", "avg"] ), iap.IterativeNoiseAggregator( other_param=iap.Sigmoid( iap.FrequencyNoise(exponent=(-4, 4), size_px_max=(4, sample_size), upscale_method=["nearest", "linear", "cubic"]), threshold=(-10, 10), activated=0.33, mul=20, add=-10 ), iterations=(1, 3), aggregation_method=["max", "avg"] ) ] samples = [[] for _ in range(len(noise_gens))] for _ in range(nb_rows * nb_cols): for i, noise_gen in enumerate(noise_gens): samples[i].append(noise_gen.draw_samples((h, w))) rows = [np.hstack(row) for row in samples] grid = np.vstack(rows) misc.imshow((grid*255).astype(np.uint8)) images = [ia.quokka_square(size=(128, 128)) for _ in range(16)] seqs = [ iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0)), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=True), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0)), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=True) ] images_aug = [] for seq in seqs: images_aug.append(np.hstack(seq.augment_images(images))) images_aug = np.vstack(images_aug) misc.imshow(images_aug)
def chapter_augmenters_edgedetect(): aug = iaa.EdgeDetect(alpha=(0.0, 1.0)) run_and_save_augseq("edgedetect.jpg", aug, [ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2) #alphas = [1/8*i for i in range(8)] alphas = np.linspace(0, 1.0, num=8) run_and_save_augseq("edgedetect_vary_alpha.jpg", [iaa.EdgeDetect(alpha=alpha) for alpha in alphas], [ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1)
def customizedImgAug(input_img): rarely = lambda aug: iaa.Sometimes(0.1, aug) sometimes = lambda aug: iaa.Sometimes(0.25, aug) often = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), often( iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.1, 0.1), "y": (-0.12, 0) }, rotate=(-10, 10), shear=(-8, 8), order=[0, 1], cval=(0, 255), )), iaa.SomeOf((0, 4), [ rarely(iaa.Superpixels(p_replace=(0, 0.3), n_segments=(20, 200))), iaa.OneOf([ iaa.GaussianBlur((0, 2.0)), iaa.AverageBlur(k=(2, 4)), iaa.MedianBlur(k=(3, 5)), ]), iaa.Sharpen(alpha=(0, 0.3), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 0.5)), rarely( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.3)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.0, 0.05), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.05), size_percent=(0.01, 0.05), per_channel=0.2), ]), rarely(iaa.Invert(0.05, per_channel=True)), often(iaa.Add((-40, 40), per_channel=0.5)), iaa.Multiply((0.7, 1.3), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))), sometimes(iaa.ElasticTransformation(alpha=(0.5, 1.5), sigma=0.25)), ], random_order=True), iaa.Fliplr(0.5), iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True) ], random_order=True) # apply augmenters in random order output_img = seq.augment_image(input_img) return output_img
def aug_image(image, is_infer=False, augment = 1): if is_infer: flip_code = augment if flip_code == 1: seq = iaa.Sequential([iaa.Fliplr(1.0)]) elif flip_code == 2: seq = iaa.Sequential([iaa.Flipud(1.0)]) elif flip_code == 3: seq = iaa.Sequential([iaa.Flipud(1.0), iaa.Fliplr(1.0)]) elif flip_code ==0: return image else: seq = iaa.Sequential([ iaa.Affine(rotate= (-15, 15), shear = (-15, 15), mode='edge'), iaa.SomeOf((0, 2), [ iaa.GaussianBlur((0, 1.5)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5), iaa.AddToHueAndSaturation((-5, 5)), iaa.EdgeDetect(alpha=(0, 0.5)), iaa.CoarseSaltAndPepper(0.2, size_percent=(0.05, 0.1)), ], random_order=True ) ]) image = seq.augment_image(image) return image
def rgbmore(self, im): return_im = [] add_return_im = lambda im: return_im.extend(im) grey = np.array(im.convert(mode='L')) im = np.array(im) rgb_grey = np.dstack((im, grey)) edge = iaa.EdgeDetect(alpha=1)(images=rgb_grey) dir_edge = lambda d: iaa.DirectedEdgeDetect(alpha=1, direction=d)(images= grey) dir_edges = np.array( [dir_edge(d) for d in np.linspace(0, 1, num=3, endpoint=False)]) dir_edges = np.transpose(dir_edges, (1, 2, 0)) canny = iaa.Canny(alpha=1.0, hysteresis_thresholds=128, sobel_kernel_size=4, deterministic=True, colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0))(images=grey) avg_pool = iaa.AveragePooling(2)(images=grey) max_pool = iaa.MaxPooling(2)(images=grey) min_pool = iaa.MinPooling(2)(images=grey) add_return_im([im, grey]) add_return_im([edge, dir_edges, canny]) add_return_im([avg_pool, max_pool, min_pool]) return np.dstack(return_im)
def augment_sequential(): return iaa.Sequential([ iaa.SomeOf( (0, 3), [ # 每次使用0~3个Augmenter来处理图片 iaa.DirectedEdgeDetect(alpha=(0.0, 0.3), direction=(0.0, 1.0)), # 边缘检测,只检测某些方向的 iaa.OneOf([ # 每次以下Augmenters中选择一个来变换 iaa.GaussianBlur((0, 1.0)), iaa.AverageBlur(k=(2, 3)), ]), iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.0)), # 锐化 iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.0, 0.5)), iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.5, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.01, 0.3), per_channel=0.5), # 随机丢弃像素 iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.1), per_channel=0.2), # 随机丢弃某位置某通道像素 ]), iaa.Add((-50, 50), per_channel=0.5), # 像素值成比例增加/减小(特指亮度) iaa.AddToHueAndSaturation((-50, 50)), # 增加色相、饱和度 iaa.LinearContrast((0.8, 1.2), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), ]) ])
def __init__(self, batch_size, input_shape, anchors, num_classes, real_annotation_lines, is_training=True): self.batch_size = batch_size self.input_shape = input_shape self.anchors = anchors self.num_classes = num_classes self.real_annotation_lines = real_annotation_lines self.is_training = is_training sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.aug_pipe = iaa.Sequential([ iaa.SomeOf((0, 5), [ iaa.Sharpen(alpha=1.0, lightness=(0.75, 1.5)), iaa.EdgeDetect(alpha=(0, 0.5)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255)), iaa.OneOf([ iaa.Dropout((0.01, 0.05)), iaa.Salt((0.03, 0.15)), ]), iaa.Add((-10, 10)), iaa.Multiply((0.5, 1.5)), iaa.ContrastNormalization((0.5, 2.0)), sometimes( iaa.ElasticTransformation(alpha=(0.1, 2.0), sigma=0.25)), ], random_order=True) ], random_order=True)
def sin2set(x,sets=3): oper0 = iaa.Invert(0.50, per_channel=False) oper1 = iaa.Invert(0.25, per_channel=True) oper2 = iaa.Add((-15,15),per_channel=0.5) #oper3 = iaa.AddToHueAndSaturation((-10)) oper4 = iaa.Multiply((0.75, 1.25), per_channel=0.5) oper5 = iaa.GaussianBlur((0, 1.25)) oper6 = iaa.AverageBlur(k=(2, 7)) #oper7 = iaa.MedianBlur(k=(3, 5)) #oper8 = iaa.BilateralBlur(d=7,sigma_color=250,sigma_space=250) oper9 = iaa.Emboss(alpha=(0,1.0), strength=(0,2.0)) oper10 = iaa.EdgeDetect(alpha=(0, 0.25)) oper11 = iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.25*255), per_channel=True) oper12 = iaa.Dropout((0.01, 0.1), per_channel=0.5) l0 = [oper0,oper1,oper2,oper4,oper5,oper6,oper9,oper10,oper11,oper12] l={} for si in range(1,sets+1): tol=len(l0) t=random.randint(1,tol-1) l[si]=l0[t] l0.pop(t) img_aug1 = l[1].augment_image(x.reshape(32,280,3).cpu().numpy()) img_aug2 = l[2].augment_image(x.reshape(32,280,3).cpu().numpy()) img_aug3 = l[3].augment_image(x.reshape(32,280,3).cpu().numpy()) transform2 = transforms.Compose([transforms.ToTensor(), ]) x=transform2(img_aug1).unsqueeze(0) x2=transform2(img_aug2).unsqueeze(0) x3=transform2(img_aug3).unsqueeze(0) x=torch.cat([x,x2],0) x=torch.cat([x,x3],0) #print(x.size()) #(3,3,32,280) return x
def generateAugSeq(): sometimes = lambda aug: iaa.Sometimes(0.5, aug) return iaa.Sequential([ sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( 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) }, order=[0, 1], cval=(0, 255), mode=ia.ALL)), iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.05, per_channel=True), iaa.Add((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation((-20, 20)), iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha(exponent=(-4, 0), first=iaa.Multiply( (0.5, 1.5), per_channel=True), second=iaa.ContrastNormalization( (0.5, 2.0))) ]), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), ], random_order=True) ], random_order=True)
def augmentation2(image, mask): sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), iaa.SomeOf( (0, 5), [ sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur( (0, 3.0) ), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout( (0.01, 0.1), per_channel=0.5 ), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation( (-20, 20)), # change hue and saturation iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), ]), iaa.Grayscale(alpha=(0.0, 1.0)), ], random_order=True) ], random_order=True) image_heavy, mask_heavy = seq(images=image, segmentation_maps=mask) return image_heavy, mask_heavy
def logic(self, image): for param in self.augmentation_params: self.augmentation_data.append([ str(param.augmentation_value), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect( 1.0)).to_deterministic().augment_image(image), param.detection_tag ])
def augmenter_1(p=0.99): """Create augmenter. Contains no coordinate transforms. Parameters ---------- p : float Number in [0, 1] representing the probability of a random augmentation happening. Returns ------- seq : iaa.Augmenter Augmenter where each augmentation was manually inspected and makes sense. """ subsubseq_1 = iaa.Multiply(mul=(0.8, 1.2)) subsubseq_2 = iaa.Sequential([iaa.Sharpen(alpha=(0, 1))]) subsubseq_3 = iaa.Sequential([iaa.EdgeDetect(alpha=(0, 0.9))]) subsubseq_4 = iaa.OneOf( [iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7))]) subsubseq_5 = iaa.AdditiveGaussianNoise(loc=(0, 0.5), scale=(0, 0.2)) subsubseq_6 = iaa.Add((-0.3, 0.3)) subsubseq_7 = iaa.Invert(p=1) subsubseq_8 = iaa.CoarseDropout(p=0.25, size_percent=(0.005, 0.06)) subsubseq_9 = iaa.SigmoidContrast(gain=(0.8, 1.2)) subsubseq_10 = iaa.LinearContrast(alpha=(0.8, 1.2)) subsubseq_11 = iaa.Sequential([iaa.Emboss(alpha=(0, 1))]) seq = iaa.Sometimes( p, iaa.OneOf([ subsubseq_1, subsubseq_2, subsubseq_3, subsubseq_4, subsubseq_5, subsubseq_6, subsubseq_7, subsubseq_8, subsubseq_9, subsubseq_10, subsubseq_11, ]), ) return seq
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 get_preview(images, augmentationList): """ Accepts a list of images and augmentationList as input. Provides a list of augmented images in that order as ouptut. """ augmented = [] for image in images: for augmentation in augmentationList: aug_id = augmentation['id'] params = augmentation['params'] if (aug_id == 1): image = iaa.SaltAndPepper(p=params[0], per_channel=params[1])(image=image) elif (aug_id == 2): image = iaa.imgcorruptlike.GaussianNoise( severity=(params[0], params[1]))(image=image) elif (aug_id == 3): image = iaa.Rain(speed=(params[0], params[1]), drop_size=(params[2], params[3]))(image=image) elif (aug_id == 4): image = iaa.imgcorruptlike.Fog( severity=(params[0], params[1]))(image=image) elif (aug_id == 5): image = iaa.imgcorruptlike.Snow( severity=(params[0], params[1]))(image=image) elif (aug_id == 6): image = iaa.imgcorruptlike.Spatter( severity=(params[0], params[1]))(image=image) elif (aug_id == 7): image = iaa.BlendAlphaSimplexNoise( iaa.EdgeDetect(1))(image=image) elif (aug_id == 8): image = iaa.Rotate(rotate=(params[0], params[1]))(image=image) elif (aug_id == 9): image = iaa.Affine()(image=image) #to be implemented elif (aug_id == 10): image = iaa.MotionBlur(k=params[0], angle=(params[1], params[2]))(image=image) elif (aug_id == 11): image = iaa.imgcorruptlike.ZoomBlur( severity=(params[0], params[1]))(image=image) elif (aug_id == 12): image = iaa.AddToBrightness()(image=image) #to be implemented elif (aug_id == 13): image = iaa.ChangeColorTemperature( kelvin=(params[0], params[1]))(image=image) elif (aug_id == 14): image = iaa.SigmoidContrast()(image=image) #to be implemented elif (aug_id == 15): image = iaa.Cutout(nb_iterations=(params[0], params[1]), size=params[2], squared=params[3])(image=image) else: print("Not implemented") augmented.append(image) return augmented
def __call__(self, sample): image, polygon, labels = sample["image"], sample["polygon"], sample[ "labels"] image = np.array(image) t = iaa.EdgeDetect(alpha=self.alpha) img = t(image=image) image = Image.fromarray(img) sample = {'image': image, 'polygon': polygon, 'labels': labels} return sample
def data_augmentation(ratio, images): ''' Augmentation of images [batch_size, h, w, channel] :param ratio: the number of augmentation of each image :param images: :return: [batch_size * ratio, h, w, channel], normed ''' images_aug = None seq = [] st = lambda aug: iaa.Sometimes(0.3, aug) for i in range(ratio): seq_one = iaa.Sequential([ # iaa.Fliplr(0.5), # horizontally flip 50% of all images # iaa.Flipud(0.5), # vertically flip 50% of all images # st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation st(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width # st(iaa.GaussianBlur((0, 3.0))), # blur images with a sigma between 0 and 3.0 st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), # sharpen images st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))), # emboss images # search either for all edges or for directed edges st(iaa.Sometimes(0.5, iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), )), # st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5)), # add gaussian noise to images # st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels # st(iaa.Invert(0.25, per_channel=True)), # invert color channels st(iaa.Add((-10, 10), per_channel=0.5)), # change brightness of images (by -10 to 10 of original value) st(iaa.Multiply((0.5, 1.5), per_channel=0.5)), # change brightness of images (50-150% of original value) st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast st(iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis translate_px={"x": (-16, 16), "y": (-16, 16)}, # translate by -16 to +16 pixels (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=ia.ALL, # use any of scikit-image's interpolation methods cval=(0, 255), # if mode is constant, use a cval between 0 and 255 mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) # apply elastic transformations with random strengths ], random_order=True # do all of the above in random order ) seq.append(seq_one) if i == 0: images_aug = seq[0].augment_images(images) else: aug_temp = seq[i].augment_images(images) images_aug = np.concatenate((images_aug, aug_temp), axis=0) print('Augmentation shape', images_aug.shape) images_aug = np.array(images_aug, dtype=np.float32) return images_aug/255
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 logic(self, image): for param in self.augmentation_params: self.augmentation_data.append([ "%d-%d" % (param.augmentation_value[0], param.augmentation_value[1]), iaa.Alpha( factor=param.augmentation_value, first=iaa.EdgeDetect(1.0), per_channel=0.5).to_deterministic().augment_image(image), param.detection_tag ])
def get_augmentations(): # 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) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. seq = iaa.Sequential([ # 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), [ iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha(iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.ContrastNormalization((0.5, 2.0)) ) ]), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) ], random_order=True ) ], random_order=True ) return seq
def logic(self, image): for param in self.augmentation_params: self.augmentation_data.append([ str(param.augmentation_value), iaa.FrequencyNoiseAlpha( exponent=param.augmentation_value, first=iaa.EdgeDetect(1.0), size_px_max=16, upscale_method="linear", sigmoid=False).to_deterministic().augment_image(image), param.detection_tag ])
def __init__(self, images, config, shuffle=True, jitter=True, norm=None): self.generator = None self.images = images self.config = config self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [ BoundBox(0, 0, config['ANCHORS'][2 * i], config['ANCHORS'][2 * i + 1]) for i in range(int(len(config['ANCHORS']) // 2)) ] sometimes = lambda aug: iaa.Sometimes(1., aug) self.aug_pipe = iaa.Sequential( [ iaa.SomeOf( (0, 5), [ iaa.OneOf([ iaa.GaussianBlur((0, 2.0)), iaa.AverageBlur(k=(2, 5)), iaa.MedianBlur(k=(1, 7)), ]), iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.5)), # sharpen images sometimes( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.5)), iaa.DirectedEdgeDetect(alpha=(0, 0.5), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.005 * 255), per_channel=0.5), iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.8, 1.2), per_channel=0.5), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 0.5)), sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), ], random_order=True) ], random_order=True) if shuffle: np.random.shuffle(self.images['images_with_annotations'])
def aug(imgs): import imgaug as ia import imgaug.augmenters as iaa sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ sometimes(iaa.Crop(percent=(0, 0.01))), iaa.Affine( scale={ "x": (0.995, 1.01), "y": (0.995, 1.01) }, translate_percent={ "x": (-0.01, 0.01), "y": (-0.01, 0.01) }, rotate=(-3, 3), shear=(-3, 3), #cval=(0, 255) ), iaa.SomeOf((0, 5), [ sometimes( iaa.OneOf([ iaa.OneOf([ iaa.GaussianBlur((1, 1.2)), iaa.AverageBlur(k=(1, 3)), iaa.MedianBlur(k=(1, 3)) ]), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ]), ])), sometimes(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), sometimes( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.005 * 255), per_channel=0.001)), sometimes(iaa.Dropout((0.001, 0.01), per_channel=0.5)), sometimes(iaa.Add((-10, 10), per_channel=0.5)), sometimes(iaa.Multiply((0.5, 1.5), per_channel=0.5)), sometimes( iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=0.05)), sometimes(iaa.PiecewiseAffine(scale=(0.001, 0.005))) ], random_order=True) ], random_order=True) return seq.augment_images(imgs)
def __init__(self): self.aug = iaa.Sequential([ iaa.Resize((224, 224)), # blur images with a sigma of 0 to 3.0 iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0, 3.0))), # horizontally flip 50% of the images iaa.Fliplr(0.5), # rotate by -20 to +20 degrees. The default mode is 'constant' which displays a constant value where the # picture was 'rotated out'. A better mode is 'symmetric' which #'Pads with the reflection of the vector mirrored along the edge of the array' (see docs) iaa.Affine(rotate=(-20, 20), mode='symmetric'), # do edge detection on 25% of the pictures iaa.Sometimes(0.10, iaa.EdgeDetect(alpha=(0.5, 1.0))), ])
def draw_single_sequential_images(): image = misc.imresize( ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) st = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Flipud(0.5), st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), st(iaa.Crop(percent=(0, 0.1))), st(iaa.GaussianBlur((0, 3.0))), st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))), st( iaa.Sometimes( 0.5, iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), )), st( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)), st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), st(iaa.Invert(0.25, per_channel=True)), st(iaa.Add((-10, 10), per_channel=0.5)), st(iaa.Multiply((0.5, 1.5), per_channel=0.5)), st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), st(iaa.Grayscale(alpha=(0.0, 1.0), name="Grayscale")), st( iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=[0, 1], cval=(0, 255), mode=ia.ALL)), st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) ], random_order=True) grid = seq.draw_grid(image, cols=8, rows=8) misc.imsave("examples_grid.jpg", grid)
def __init__(self, images, config, shuffle=True, jitter=True, norm=None): self.generator = None self.images = images self.config = config self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [BoundBox(0, 0, config['ANCHORS'][2*i], config['ANCHORS'][2*i+1]) for i in range(int(len(config['ANCHORS'])//2))] ### augmentors by https://github.com/aleju/imgaug sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. self.aug_pipe = iaa.Sequential( [ # 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), [ sometimes(iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), # iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ])), iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images (50-150% of original value) iaa.ContrastNormalization((0.5, 1.0), per_channel=0.5), # improve or worsen the contrast ], random_order=True ) ], random_order=True ) if shuffle: np.random.shuffle(self.images)
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 __init__(self,with_mask=True): self.with_mask = with_mask self.seq = iaa.Sequential( [ iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 0.5), n_segments=(100, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha(iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images #iaa.OneOf([ # iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels # iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), #]), iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add((-5, 5), per_channel=0.5), # change brightness of images iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.LinearContrast((0.5, 2.0)) ) ]), iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)) ], random_order=True ) ], random_order=True )
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