def chapter_augmenters_histogramequalization(): fn_start = "contrast/histogramequalization" aug = iaa.HistogramEqualization() run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 1)], cols=4, rows=1) aug = iaa.Alpha((0.0, 1.0), iaa.HistogramEqualization()) run_and_save_augseq(fn_start + "_alpha.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(4 * 4)], cols=4, rows=4) aug = iaa.HistogramEqualization( from_colorspace=iaa.HistogramEqualization.BGR, to_colorspace=iaa.HistogramEqualization.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 * 1)], cols=4, rows=1, image_colorspace="RGB")
def __init__(self): self.aug = iaa.Sequential([ iaa.Sometimes( 0.15, iaa.OneOf([ iaa.GammaContrast(gamma=(0, 1.75)), iaa.pillike.Autocontrast(cutoff=(0, 15.0)) ])), iaa.Sometimes( 0.15, iaa.OneOf([ iaa.HistogramEqualization(), iaa.pillike.Equalize(), ])), iaa.Sometimes(0.1, iaa.Grayscale(alpha=(0.05, 1.0))), iaa.Sometimes(0.2, iaa.JpegCompression(compression=(70, 99))), iaa.Sometimes(0.1, iaa.UniformColorQuantizationToNBits(nb_bits=(2, 8))), iaa.Sometimes( 0.3, iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30))), iaa.Sometimes( 0.2, iaa.Cutout( fill_mode="constant", cval=(0, 255), fill_per_channel=0.5)) ], random_order=True)
def main(): parser = argparse.ArgumentParser(description="Contrast check script") parser.add_argument("--per_channel", dest="per_channel", action="store_true") args = parser.parse_args() augs = [] for p in [0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]: augs.append(("GammaContrast " + str(p), iaa.GammaContrast(p, per_channel=args.per_channel))) for cutoff in [0.25, 0.5, 0.75]: for gain in [5, 10, 15, 20, 25]: augs.append(("SigmoidContrast " + str(cutoff) + " " + str(gain), iaa.SigmoidContrast(gain, cutoff, per_channel=args.per_channel))) for gain in [0.0, 0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]: augs.append(("LogContrast " + str(gain), iaa.LogContrast(gain, per_channel=args.per_channel))) for alpha in [-1.0, 0.5, 0, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]: augs.append(("LinearContrast " + str(alpha), iaa.LinearContrast(alpha, per_channel=args.per_channel))) augs.append(("AllChannelsHistogramEqualization", iaa.AllChannelsHistogramEqualization())) augs.append(("HistogramEqualization (Lab)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.Lab))) augs.append(("HistogramEqualization (HSV)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.HSV))) augs.append(("HistogramEqualization (HLS)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.HLS))) for clip_limit in [0.1, 1, 5, 10]: for tile_grid_size_px in [3, 7]: augs.append(("AllChannelsCLAHE %d %dx%d" % (clip_limit, tile_grid_size_px, tile_grid_size_px), iaa.AllChannelsCLAHE(clip_limit=clip_limit, tile_grid_size_px=tile_grid_size_px, per_channel=args.per_channel))) for clip_limit in [1, 5, 10, 100, 200]: for tile_grid_size_px in [3, 7, 15]: augs.append(("CLAHE %d %dx%d" % (clip_limit, tile_grid_size_px, tile_grid_size_px), iaa.CLAHE(clip_limit=clip_limit, tile_grid_size_px=tile_grid_size_px))) images = [data.astronaut()] * 16 images = ia.imresize_many_images(np.uint8(images), (128, 128)) for name, aug in augs: print("-----------") print(name) print("-----------") images_aug = aug.augment_images(images) images_aug[0] = images[0] grid = ia.draw_grid(images_aug, rows=4, cols=4) ia.imshow(grid)
def contrast(): return iaa.OneOf([ iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.GammaContrast((0.5, 1.5), per_channel=0.5), iaa.HistogramEqualization(), iaa.LinearContrast((0.5, 1.5), per_channel=0.5), iaa.LogContrast((0.5, 1.5), per_channel=0.5), iaa.SigmoidContrast((5, 20), (0.25, 0.75), per_channel=0.5), ])
def data_augmentation(self, image, label): crop_size = random.randint(int(0.8*self.sample_height),int(1.2*self.sample_height)) start_h = random.randint( 0,image.shape[0] - int(1.42*crop_size) - 2) start_w = random.randint( 0,image.shape[1] - int(1.42*crop_size) - 2) image = image[start_h:start_h + int(1.42*crop_size), start_w:start_w + int(1.42*crop_size)] label = label[start_h:start_h + int(1.42*crop_size), start_w:start_w + int(1.42*crop_size)] seq = iaa.Sequential([ iaa.Affine( shear=(-4, 4), rotate=(0, 360)), # rotate by -45 to 45 degrees (affects segmaps) ]) segmap = ia.SegmentationMapOnImage( label.copy(), shape=label.shape, nb_classes=self.num_classes) seq_det = seq.to_deterministic() image_rotation = seq_det.augment_image(image.copy()) segmap_aug = seq_det.augment_segmentation_maps(segmap) label_rotation = segmap_aug.get_arr_int() reduction_pixels = int(0.15*label_rotation.shape[0]) start_i = reduction_pixels stop_i = label.shape[0] - reduction_pixels image = image_rotation[start_i:stop_i, start_i:stop_i, :] label = label_rotation[start_i:stop_i, start_i:stop_i] seq = iaa.Sequential([ iaa.Resize({"height": self.sample_height, "width": self.sample_width},interpolation='nearest'), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Sometimes(0.8,iaa.HistogramEqualization()), iaa.Sometimes(0.8,iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25))), iaa.AddToHueAndSaturation((-20, 20), per_channel=True), ]) segmap = ia.SegmentationMapOnImage( label.copy(), shape=label.shape, nb_classes=self.num_classes) seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image.copy()) segmap_aug = seq_det.augment_segmentation_maps(segmap) label_aug = segmap_aug.get_arr_int() return image_aug, label_aug
def augmentor(self, images, targets): '''Augments each batch of data with random transformations''' sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), sometimes( iaa.SomeOf((0, 2), [ iaa.Affine(translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }, rotate=(-25, 25), name="Affine"), iaa.ElasticTransformation(alpha=(0.01, 0.1), sigma=0.15, name="ElasticTransformation"), iaa.PiecewiseAffine(scale=(0.001, 0.03), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=(0.01, 0.05), name="PerspectiveTransform"), ], random_order=True)), sometimes( iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 0.2)), iaa.AverageBlur(k=3), iaa.MedianBlur(k=3), ])), sometimes( iaa.OneOf([ iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255)), iaa.AddElementwise((-5, 5)), ])), sometimes( iaa.OneOf([ iaa.GammaContrast(gamma=(0.75, 1.50)), iaa.HistogramEqualization(), iaa.Multiply((0.80, 1.15)), iaa.Add((-20, 15)), iaa.Sharpen(alpha=(0, 0.5), lightness=(0.7, 1.5)), iaa.Emboss(alpha=(0, 0.5), strength=(0.7, 1.5)), ])), ], random_order=True) seq_det = seq.to_deterministic() images = seq_det.augment_images(images) targets = seq_det.augment_segmentation_maps([ ia.SegmentationMapOnImage(t.astype(bool), shape=t.shape) for t in targets ]) targets = np.array([t.get_arr_int() for t in targets]) return images, targets
def ol_aug(image, mask): # ia.seed(seed) # Example batch of images. # The array has shape (32, 64, 64, 3) and dtype uint8. images = image # B,H,W,C masks = mask # B,H,W,C # print('In Aug',images.shape,masks.shape) combo = np.concatenate((images, masks), axis=3) # print('COMBO: ',combo.shape) seq_all = iaa.Sequential([ iaa.Fliplr(0.5), # horizontal flips # iaa.PadToFixedSize(width=crop_size[0], height=crop_size[1]), # iaa.CropToFixedSize(width=crop_size[0], height=crop_size[1]), iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # scale images to 90-110% of their size, individually per axis translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # translate by -10 to +10 percent (per axis) rotate=(-5, 5), # rotate by -5 to +5 degrees shear=(-3, 3), # shear by -3 to +3 degrees ), # iaa.Cutout(nb_iterations=(1, 5), size=0.2, cval=0, squared=False), ], random_order=False) # apply augmenters in random order seq_f = iaa.Sequential([ iaa.Sometimes(0.5, iaa.OneOf([ iaa.GaussianBlur((0.0, 3.0)), iaa.MotionBlur(k=(3, 20)), ]), ), iaa.Sometimes(0.5, iaa.OneOf([ iaa.Multiply((0.8, 1.2), per_channel=0.2), iaa.MultiplyBrightness((0.5, 1.5)), iaa.LinearContrast((0.5, 2.0), per_channel=0.2), iaa.BlendAlpha((0., 1.), iaa.HistogramEqualization()), iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=0.2), ]), ), ], random_order=False) combo_aug = np.array(seq_all.augment_images(images=combo)) # print('combo_au: ', combo_aug.shape) images_aug = combo_aug[:, :, :, :3] masks_aug = combo_aug[:, :, :, 3:] images_aug = seq_f.augment_images(images=images_aug) return images_aug, masks_aug
def load_augmentations(flip=0.5, blur=0.2, crop=0.5, contrast=0.3, elastic=0.2, affine=0.5): """ Loads and configures data augmenter object Arguements: flip (float) -- probality of horizontal flip crop (float) -- probability of random crop blur (float) -- probability of gaussian blur contrast (float) -- probability of pixelwise color transformation elastic (float) -- probability of elastic distortion affine (float) -- probability of affine transform noise (float) -- probability of one of noises """ aug = iaa.Sequential([ iaa.Fliplr(flip), iaa.Sometimes(crop, iaa.Crop(px=(0, 20))), iaa.Sometimes(blur, iaa.GaussianBlur(sigma=(0.5, 5))), iaa.Sometimes( contrast, iaa.SomeOf((1, 5), [ iaa.GammaContrast(per_channel=True, gamma=(0.25, 1.75)), iaa.LinearContrast(alpha=(0.25, 1.75), per_channel=True), iaa.HistogramEqualization(to_colorspace="HLS"), iaa.LogContrast(gain=(0.5, 1.0)), iaa.CLAHE(clip_limit=(1, 10)) ]), ), iaa.Sometimes( elastic, iaa.OneOf([ iaa.ElasticTransformation(alpha=20, sigma=1), iaa.ElasticTransformation(alpha=200, sigma=20) ])), iaa.Sometimes( affine, iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, rotate=(-30, 30), order=[0, 1])) ]) return aug
def __init__(self,num_of_augms=0): self.num_of_augms=num_of_augms self.aug=iaa.OneOf([ iaa.Sequential([ iaa.LinearContrast(alpha=(0.75, 1.5)), iaa.Fliplr(0.5) ]), iaa.Sequential([ iaa.Grayscale(alpha=(0.1, 0.9)), iaa.Affine( translate_percent={"y": (-0.15, 0.15)} ) ]), iaa.Sequential([ iaa.Solarize(0.5, threshold=(0, 256)), iaa.ShearX((-10, 10)) ]), iaa.Sequential([ iaa.GaussianBlur(sigma=(0, 1)), iaa.ShearY((-10, 10)) ]), iaa.Sequential([ iaa.Multiply((0.5, 1.5), per_channel=0.25), iaa.Fliplr(0.5), ]), iaa.Sequential([ iaa.HistogramEqualization(), iaa.Affine( translate_percent={"x": (-0.25, 0.25)}, shear=(-8, 8) ) ]), iaa.Sequential([ iaa.Crop(percent=(0.01, 0.1)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, ) ]), iaa.Sequential([ iaa.GaussianBlur(sigma=(0, 0.9)), iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)} ) ]) ])
def run(image_path, segmap_path, image_aug_path, SegmentationClass_aug_path, txt_set): # 1.Load an example image. ia.seed(1) image = np.array(Image.open(image_path)) segmap = Image.open(segmap_path) segmap = SegmentationMapsOnImage(np.array(segmap), shape=image.shape) # 2.Define our augmentation pipeline. seq = iaa.Sequential( [ iaa.Sharpen((0.0, 1.0)), # sharpen the image iaa.GammaContrast((0.5, 2.0)), # 对比度增强 iaa.Alpha((0.0, 1.0), iaa.HistogramEqualization()), # 直方图均衡 iaa.Affine( rotate=(-40, 40)), # rotate by -40 to 40 degrees (affects segmaps) iaa.Fliplr(0.5) # 对百分之五十的图像进行做左右翻 ], random_order=True) file_name = image_path.split("/")[-1] file_name = file_name.split(".")[-2] count = 1 for _ in range(5): name = file_name + '_' + f"{count:04d}" #print(name) txt_set = txt_set + name + '\n' images_aug_i, segmaps_aug_i = seq(image=image, segmentation_maps=segmap) images_aug_i = Image.fromarray(images_aug_i) images_aug_i.save(os.path.join(image_aug_path, name + '.jpg')) segmaps_aug_i_ = segmaps_aug_i.get_arr() segmaps_aug_i_ = Image.fromarray(np.uint8(segmaps_aug_i_)) segmaps_aug_i_ = segmaps_aug_i_.convert("P") segmaps_aug_i_.save( os.path.join(SegmentationClass_aug_path, name + '.png')) count += 1 return txt_set
def _load_augmentation_aug_non_geometric(): return iaa.Sequential([ iaa.Sometimes(0.3, iaa.Multiply((0.5, 1.5), per_channel=0.5)), iaa.Sometimes(0.2, iaa.JpegCompression(compression=(70, 99))), iaa.Sometimes(0.2, iaa.GaussianBlur(sigma=(0, 3.0))), iaa.Sometimes(0.2, iaa.MotionBlur(k=15, angle=[-45, 45])), iaa.Sometimes(0.2, iaa.MultiplyHue((0.5, 1.5))), iaa.Sometimes(0.2, iaa.MultiplySaturation((0.5, 1.5))), iaa.Sometimes( 0.34, iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)), iaa.Sometimes(0.34, iaa.Grayscale(alpha=(0.0, 1.0))), iaa.Sometimes(0.2, iaa.ChangeColorTemperature((1100, 10000))), iaa.Sometimes(0.1, iaa.GammaContrast((0.5, 2.0))), iaa.Sometimes(0.2, iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6))), iaa.Sometimes(0.1, iaa.CLAHE()), iaa.Sometimes(0.1, iaa.HistogramEqualization()), iaa.Sometimes(0.2, iaa.LinearContrast((0.5, 2.0), per_channel=0.5)), iaa.Sometimes(0.1, iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))) ])
def da_policy(image, label): img_size = 224 #image = sample[0] policy = np.random.randint(4) #policy = 2 if policy == 0: p = np.random.random() if p <= 0.6: aug = iaa.TranslateX(px=(-60, 60), cval=128) image = aug(image=image) p = np.random.random() if p <= 0.8: aug = iaa.HistogramEqualization() image = aug(image=image) elif policy == 1: p = np.random.random() if p <= 0.2: aug = iaa.TranslateY(px=(int(-0.18 * img_size), int(0.18 * img_size)), cval=128) image = aug(image=image) p = np.random.random() if p <= 0.8: square_size = np.random.randint(48) aug = iaa.Cutout(nb_iterations=1, size=square_size / img_size, squared=True) image = aug(image=image) elif policy == 2: p = np.random.random() if p <= 1: aug = iaa.ShearY(shear=(int(-0.06 * img_size), int(0.06 * img_size)), order=1, cval=128) image = aug(image=image) p = np.random.random() if p <= 0.6: aug = iaa.TranslateX(px=(-60, 60), cval=128) image = aug(image=image) elif policy == 3: p = np.random.random() if p <= 0.6: aug = iaa.Rotate(rotate=(-30, 30), order=1, cval=128) image = aug(image=image) p = np.random.random() if p <= 1: aug = iaa.MultiplySaturation((0.54, 1.54)) image = aug(image=image) #Para EFFICIENTNET NO es necesario NORMALIZAR return (tf.cast(image, tf.float32), tf.cast(label, tf.int64))
"Blend_Alpha": lambda image_fg, image_bg, alpha: iaa.blend_alpha(image_fg, image_bg, alpha), # Blur/Denoise an image using a bilateral filter. # Bilateral filters blur homogeneous and textured areas, while trying to preserve edges. # Blurs all images using a bilateral filter with max distance d_lo to d_hi with ranges for sigma_colour # and sigma space being define by sc_lo/sc_hi and ss_lo/ss_hi "Bilateral_Blur": lambda d_lo, d_hi, sc_lo, sc_hi, ss_lo, ss_hi: iaa.BilateralBlur(d=(d_lo, d_hi), sigma_color=(sc_lo, sc_hi), sigma_space=(ss_lo, ss_hi)), # Augmenter that sharpens images and overlays the result with the original image. # Create a motion blur augmenter with kernel size of (kernel x kernel) and a blur angle of either x or y degrees # (randomly picked per image). "Motion_Blur": lambda kernel, x, y: iaa.MotionBlur(k=kernel, angle=[x, y]), # Augmenter to apply standard histogram equalization to images (similar to CLAHE) "Histogram_Equalization": iaa.HistogramEqualization(), # Augmenter to perform standard histogram equalization on images, applied to all channels of each input image "All_Channels_Histogram_Equalization": iaa.AllChannelsHistogramEqualization(), # Contrast Limited Adaptive Histogram Equalization (CLAHE). This augmenter applies CLAHE to images, a form of # histogram equalization that normalizes within local image patches. # Creates a CLAHE augmenter with clip limit uniformly sampled from [cl_lo..cl_hi], i.e. 1 is rather low contrast # and 50 is rather high contrast. Kernel sizes of SxS, where S is uniformly sampled from [t_lo..t_hi]. # Sampling happens once per image. (Note: more parameters are available for further specification) "CLAHE": lambda cl_lo, cl_hi, t_lo, t_hi: iaa.CLAHE(clip_limit=(cl_lo, cl_hi), tile_grid_size_px=(t_lo, t_hi)), # Contrast Limited Adaptive Histogram Equalization (refer above), applied to all channels of the input images. # CLAHE performs histogram equalization within image patches, i.e. over local neighbourhoods "All_Channels_CLAHE": lambda cl_lo, cl_hi, t_lo, t_hi: iaa.AllChannelsCLAHE(clip_limit=(cl_lo, cl_hi), tile_grid_size_px=(t_lo, t_hi)),
def augment(img_data, config, augment=True): assert 'filepath' in img_data assert 'bboxes' in img_data assert 'width' in img_data assert 'height' in img_data img_data_aug = copy.deepcopy(img_data) aug_list = [] img = cv2.imread(img_data_aug['filepath']) if augment: rows, cols = img.shape[:2] #[START] Pallete Augmentation pallete_augmentation(img=img, img_data=img_data_aug, config=config) #[END] Pallete Augmentation if config.use_horizontal_flips and np.random.randint(0, 2) == 0: img = cv2.flip(img, 1) for bbox in img_data_aug['bboxes']: x1 = bbox['x1'] x2 = bbox['x2'] bbox['x2'] = cols - x1 bbox['x1'] = cols - x2 if config.use_vertical_flips and np.random.randint(0, 2) == 0: img = cv2.flip(img, 0) for bbox in img_data_aug['bboxes']: y1 = bbox['y1'] y2 = bbox['y2'] bbox['y2'] = rows - y1 bbox['y1'] = rows - y2 if config.rot_90: angle = np.random.choice([0, 90, 180, 270], 1)[0] if angle == 270: img = np.transpose(img, (1, 0, 2)) img = cv2.flip(img, 0) elif angle == 180: img = cv2.flip(img, -1) elif angle == 90: img = np.transpose(img, (1, 0, 2)) img = cv2.flip(img, 1) elif angle == 0: pass for bbox in img_data_aug['bboxes']: x1 = bbox['x1'] x2 = bbox['x2'] y1 = bbox['y1'] y2 = bbox['y2'] if angle == 270: bbox['x1'] = y1 bbox['x2'] = y2 bbox['y1'] = cols - x2 bbox['y2'] = cols - x1 elif angle == 180: bbox['x2'] = cols - x1 bbox['x1'] = cols - x2 bbox['y2'] = rows - y1 bbox['y1'] = rows - y2 elif angle == 90: bbox['x1'] = rows - y2 bbox['x2'] = rows - y1 bbox['y1'] = x1 bbox['y2'] = x2 elif angle == 0: pass if config.color: aug_list.append( np.random.choice([ iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True), iaa.AddToHueAndSaturation((-50, 50), per_channel=True), iaa.KMeansColorQuantization(), iaa.UniformColorQuantization(), iaa.Grayscale(alpha=(0.0, 1.0)) ])) if config.contrast: aug_list.append( np.random.choice([ iaa.GammaContrast((0.5, 2.0), per_channel=True), iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True), iaa.LogContrast(gain=(0.6, 1.4), per_channel=True), iaa.LinearContrast((0.4, 1.6), per_channel=True), iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True), iaa.AllChannelsHistogramEqualization(), iaa.HistogramEqualization() ])) ## Augmentation aug = iaa.SomeOf((0, None), aug_list, random_order=True) seq = iaa.Sequential(aug) img = seq.augment_image(img) ## img_data_aug['width'] = img.shape[1] img_data_aug['height'] = img.shape[0] return img_data_aug, img
def __init__(self): self.hnorm = iaa.HistogramEqualization()
def create_augmenters(height, width, height_augmentable, width_augmentable, only_augmenters): def lambda_func_images(images, random_state, parents, hooks): return images def lambda_func_heatmaps(heatmaps, random_state, parents, hooks): return heatmaps def lambda_func_keypoints(keypoints, random_state, parents, hooks): return keypoints def assertlambda_func_images(images, random_state, parents, hooks): return True def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks): return True def assertlambda_func_keypoints(keypoints, random_state, parents, hooks): return True augmenters_meta = [ iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=False, name="Sequential_2xNoop"), iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=True, name="Sequential_2xNoop_random_order"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=False, name="SomeOf_3xNoop"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=True, name="SomeOf_3xNoop_random_order"), iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"), iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"), iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"), iaa.Noop(name="Noop"), iaa.Lambda(func_images=lambda_func_images, func_heatmaps=lambda_func_heatmaps, func_keypoints=lambda_func_keypoints, name="Lambda"), iaa.AssertLambda(func_images=assertlambda_func_images, func_heatmaps=assertlambda_func_heatmaps, func_keypoints=assertlambda_func_keypoints, name="AssertLambda"), iaa.AssertShape((None, height_augmentable, width_augmentable, None), name="AssertShape"), iaa.ChannelShuffle(0.5, name="ChannelShuffle") ] augmenters_arithmetic = [ iaa.Add((-10, 10), name="Add"), iaa.AddElementwise((-10, 10), name="AddElementwise"), #iaa.AddElementwise((-500, 500), name="AddElementwise"), iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"), iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"), iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Dropout((0.01, 0.05), name="Dropout"), iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseDropout"), iaa.ReplaceElementwise((0.01, 0.05), (0, 255), name="ReplaceElementwise"), #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"), iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"), iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"), iaa.CoarseSaltAndPepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.Salt((0.01, 0.05), name="Salt"), iaa.CoarseSalt((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.Pepper((0.01, 0.05), name="Pepper"), iaa.CoarsePepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarsePepper"), iaa.Invert(0.1, name="Invert"), # ContrastNormalization iaa.JpegCompression((50, 99), name="JpegCompression") ] augmenters_blend = [ iaa.Alpha((0.01, 0.99), iaa.Noop(), name="Alpha"), iaa.AlphaElementwise((0.01, 0.99), iaa.Noop(), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha((-2.0, 2.0), iaa.Noop(), name="FrequencyNoiseAlpha") ] augmenters_blur = [ iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"), iaa.MotionBlur(k=(3, 11), name="MotionBlur") ] augmenters_color = [ # InColorspace (deprecated) iaa.WithColorspace(to_colorspace="HSV", children=iaa.Noop(), name="WithColorspace"), iaa.WithHueAndSaturation(children=iaa.Noop(), name="WithHueAndSaturation"), iaa.MultiplyHueAndSaturation((0.8, 1.2), name="MultiplyHueAndSaturation"), iaa.MultiplyHue((-1.0, 1.0), name="MultiplyHue"), iaa.MultiplySaturation((0.8, 1.2), name="MultiplySaturation"), iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"), iaa.AddToHue((-10, 10), name="AddToHue"), iaa.AddToSaturation((-10, 10), name="AddToSaturation"), iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"), iaa.Grayscale((0.01, 0.99), name="Grayscale"), iaa.KMeansColorQuantization((2, 16), name="KMeansColorQuantization"), iaa.UniformColorQuantization((2, 16), name="UniformColorQuantization") ] augmenters_contrast = [ iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"), iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.25, 0.75), name="SigmoidContrast"), iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"), iaa.LinearContrast((0.5, 1.5), name="LinearContrast"), iaa.AllChannelsCLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), name="AllChannelsCLAHE"), iaa.CLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), to_colorspace="HSV", name="CLAHE"), iaa.AllChannelsHistogramEqualization( name="AllChannelsHistogramEqualization"), iaa.HistogramEqualization(to_colorspace="HSV", name="HistogramEqualization"), ] augmenters_convolutional = [ iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), name="Convolve_3x3"), iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"), iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"), iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect") ] augmenters_edges = [iaa.Canny(alpha=(0.01, 0.99), name="Canny")] augmenters_flip = [ iaa.Fliplr(1.0, name="Fliplr"), iaa.Flipud(1.0, name="Flipud") ] augmenters_geometric = [ iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=0, mode="constant", cval=(0, 255), name="Affine_order_0_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), name="Affine_order_1_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=3, mode="constant", cval=(0, 255), name="Affine_order_3_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="edge", cval=(0, 255), name="Affine_order_1_edge"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), backend="skimage", name="Affine_order_1_constant_skimage"), # TODO AffineCv2 iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="constant", name="PiecewiseAffine_4x4_order_1_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=0, mode="constant", name="PiecewiseAffine_4x4_order_0_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="edge", name="PiecewiseAffine_4x4_order_1_edge"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=8, nb_cols=8, order=1, mode="constant", name="PiecewiseAffine_8x8_order_1_constant"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=False, name="PerspectiveTransform"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=True, name="PerspectiveTransform_keep_size"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=0, mode="constant", cval=0, name="ElasticTransformation_order_0_constant"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="constant", cval=0, name="ElasticTransformation_order_1_constant"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="nearest", cval=0, name="ElasticTransformation_order_1_nearest"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="reflect", cval=0, name="ElasticTransformation_order_1_reflect"), iaa.Rot90((1, 3), keep_size=False, name="Rot90"), iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size") ] augmenters_pooling = [ iaa.AveragePooling(kernel_size=(1, 16), keep_size=False, name="AveragePooling"), iaa.AveragePooling(kernel_size=(1, 16), keep_size=True, name="AveragePooling_keep_size"), iaa.MaxPooling(kernel_size=(1, 16), keep_size=False, name="MaxPooling"), iaa.MaxPooling(kernel_size=(1, 16), keep_size=True, name="MaxPooling_keep_size"), iaa.MinPooling(kernel_size=(1, 16), keep_size=False, name="MinPooling"), iaa.MinPooling(kernel_size=(1, 16), keep_size=True, name="MinPooling_keep_size"), iaa.MedianPooling(kernel_size=(1, 16), keep_size=False, name="MedianPooling"), iaa.MedianPooling(kernel_size=(1, 16), keep_size=True, name="MedianPooling_keep_size") ] augmenters_segmentation = [ iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="cubic", name="Superpixels_max_size_64_cubic"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="linear", name="Superpixels_max_size_64_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=128, interpolation="linear", name="Superpixels_max_size_128_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=224, interpolation="linear", name="Superpixels_max_size_224_linear"), iaa.UniformVoronoi(n_points=(250, 1000), name="UniformVoronoi"), iaa.RegularGridVoronoi(n_rows=(16, 31), n_cols=(16, 31), name="RegularGridVoronoi"), iaa.RelativeRegularGridVoronoi(n_rows_frac=(0.07, 0.14), n_cols_frac=(0.07, 0.14), name="RelativeRegularGridVoronoi"), ] augmenters_size = [ iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"), iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"), iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="CropAndPad"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="CropAndPad_edge"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), name="CropAndPad_keep_size"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="Pad"), iaa.Pad(percent=(0.05, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="Pad_edge"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), name="Pad_keep_size"), iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"), iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"), iaa.PadToFixedSize(width=width + 10, height=height + 10, pad_mode="constant", pad_cval=(0, 255), name="PadToFixedSize"), iaa.CropToFixedSize(width=width - 10, height=height - 10, name="CropToFixedSize"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="nearest", name="KeepSizeByResize_CropToFixedSize_nearest"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="linear", name="KeepSizeByResize_CropToFixedSize_linear"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="cubic", name="KeepSizeByResize_CropToFixedSize_cubic"), ] augmenters_weather = [ iaa.FastSnowyLandscape(lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0), name="FastSnowyLandscape"), iaa.Clouds(name="Clouds"), iaa.Fog(name="Fog"), iaa.CloudLayer(intensity_mean=(196, 255), intensity_freq_exponent=(-2.5, -2.0), intensity_coarse_scale=10, alpha_min=0, alpha_multiplier=(0.25, 0.75), alpha_size_px_max=(2, 8), alpha_freq_exponent=(-2.5, -2.0), sparsity=(0.8, 1.0), density_multiplier=(0.5, 1.0), name="CloudLayer"), iaa.Snowflakes(name="Snowflakes"), iaa.SnowflakesLayer(density=(0.005, 0.075), density_uniformity=(0.3, 0.9), flake_size=(0.2, 0.7), flake_size_uniformity=(0.4, 0.8), angle=(-30, 30), speed=(0.007, 0.03), blur_sigma_fraction=(0.0001, 0.001), name="SnowflakesLayer") ] augmenters = (augmenters_meta + augmenters_arithmetic + augmenters_blend + augmenters_blur + augmenters_color + augmenters_contrast + augmenters_convolutional + augmenters_edges + augmenters_flip + augmenters_geometric + augmenters_pooling + augmenters_segmentation + augmenters_size + augmenters_weather) if only_augmenters is not None: augmenters_reduced = [] for augmenter in augmenters: if any([ re.search(pattern, augmenter.name) for pattern in only_augmenters ]): augmenters_reduced.append(augmenter) augmenters = augmenters_reduced return augmenters
iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 1.5)), iaa.AverageBlur(k=(1, 5)), iaa.MedianBlur(k=(1, 5)), iaa.MotionBlur(k=(3, 5)), ])) ]) contrasts = iaa.Sequential([ sometimes_010( iaa.OneOf([ iaa.LogContrast((0.8, 1.2)), iaa.GammaContrast((0.8, 1.2)), iaa.LinearContrast((0.8, 1.2)), iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()), iaa.Alpha((0.0, 1.0), iaa.HistogramEqualization()), iaa.CLAHE(clip_limit=(1, 3)), iaa.AllChannelsCLAHE(clip_limit=(1, 3)), ])) ]) dropouts = iaa.Sequential([ sometimes_010( iaa.OneOf([ iaa.Dropout(p=0.01, per_channel=True), iaa.Dropout(p=0.01, per_channel=False), iaa.Cutout(fill_mode="constant", cval=(0, 255), size=(0.1, 0.4), fill_per_channel=0.5), iaa.CoarseDropout((0.0, 0.08),
def data_augmentation(img, bounding_boxes, labels): """ Enhance the data with imgaug Largely inspired by https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html :param img: single image :param bounding_boxes: the list of bounding boxes :return: """ bbs = BoundingBoxesOnImage([ BoundingBox( x1=bbox[0], y1=bbox[1], x2=bbox[2], y2=bbox[3], label=label) for bbox, label in zip(bounding_boxes, labels) ], shape=img.shape) seq = iaa.Sequential( [ # Blur each image with varying strength using # gaussian blur (sigma between 0 and 3.0), iaa.GaussianBlur((0, 3.0)), # 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.Sometimes(p=0.5, then_list=iaa.Multiply((0.8, 1.2), per_channel=0.2)), # Increase saturation iaa.Sometimes(p=0.5, then_list=iaa.MultiplySaturation(mul=(0.5, 1.5))), # Strengthen or weaken the contrast in each image. iaa.Sometimes( p=0.5, then_list=iaa.LinearContrast((0.75, 1.5)), ), iaa.HistogramEqualization(), # horizontal flips iaa.Fliplr(0.5), # random crops iaa.Crop(percent=(0, 0.1)), # Apply affine transformations to each image. # Scale/zoom them, translate/move them, rotate them and shear them. iaa.Sometimes( p=0.5, then_list=iaa.Affine(rotate=(-15, 15), ), ) ], random_order=True) seq_det = seq.to_deterministic() # Augment BBs and images. image_aug, bbs_aug = seq_det(image=img, bounding_boxes=bbs) bbs_aug = bbs_aug.clip_out_of_image() bboxes_aug = list() height, width, _ = image_aug.shape for i in range(len(bbs_aug.bounding_boxes)): bboxes_aug.append([ bbs_aug.bounding_boxes[i].label, bbs_aug.bounding_boxes[i].x1, bbs_aug.bounding_boxes[i].y1, bbs_aug.bounding_boxes[i].x2, bbs_aug.bounding_boxes[i].y2 ]) return image_aug, np.array(bboxes_aug, dtype=np.float32)
def augmentation_of_image(self, test_image, output_path): self.test_image = test_image self.output_path = output_path #define the Augmenters #properties: A range of values signifies that one of these numbers is randmoly chosen for every augmentation for every batch # Apply affine transformations to each image. rotate = iaa.Affine(rotate=(-90, 90)) scale = iaa.Affine(scale={ "x": (0.5, 0.9), "y": (0.5, 0.9) }) translation = iaa.Affine(translate_percent={ "x": (-0.15, 0.15), "y": (-0.15, 0.15) }) shear = iaa.Affine(shear=(-2, 2)) #plagio parallhlogrammo wihthin a range (-8,8) zoom = iaa.PerspectiveTransform( scale=(0.01, 0.15), keep_size=True) # do not change the output size of the image h_flip = iaa.Fliplr(1.0) # flip horizontally all images (100%) v_flip = iaa.Flipud(1.0) #flip vertically all images padding = iaa.KeepSizeByResize( iaa.CropAndPad(percent=(0.05, 0.25)) ) #positive values correspond to padding 5%-25% of the image,but keeping the origial output size of the new image #More augmentations blur = iaa.GaussianBlur( sigma=(0, 1.22) ) # blur images with a sigma 0-2,a number ofthis range is randomly chosen everytime.Low values suggested for this application contrast = iaa.contrast.LinearContrast((0.75, 1.5)) #change the contrast by a factor of 0.75 and 1.5 sampled randomly per image contrast_channels = iaa.LinearContrast( (0.75, 1.5), per_channel=True ) #and for 50% of all images also independently per channel: sharpen = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)) #sharpen with an alpha from 0(no sharpening) - 1(full sharpening) and change the lightness form 0.75 to 1.5 gauss_noise = iaa.AdditiveGaussianNoise( scale=0.111 * 255, per_channel=True ) #some random gaussian noise might occur in cell images,especially when image quality is poor laplace_noise = iaa.AdditiveLaplaceNoise( scale=(0, 0.111 * 255) ) #we choose to be in a small range, as it is logical for training the cell images #Brightness brightness = iaa.Multiply( (0.35, 1.65 )) #change brightness between 35% or 165% of the original image brightness_channels = iaa.Multiply( (0.5, 1.5), per_channel=0.75 ) # change birghtness for 25% of images.For the remaining 75%, change it, but also channel-wise. #CHANNELS (RGB)=(Red,Green,Blue) red = iaa.WithChannels(0, iaa.Add( (10, 100))) #increase each Red-pixels value within the range 10-100 red_rot = iaa.WithChannels(0, iaa.Affine( rotate=(0, 45))) #rotate each image's red channel by 0-45 degrees green = iaa.WithChannels(1, iaa.Add( (10, 100))) #increase each Green-pixels value within the range 10-100 green_rot = iaa.WithChannels(1, iaa.Affine( rotate=(0, 45))) #rotate each image's green channel by 0-45 degrees blue = iaa.WithChannels(2, iaa.Add( (10, 100))) #increase each Blue-pixels value within the range 10-100 blue_rot = iaa.WithChannels(2, iaa.Affine( rotate=(0, 45))) #rotate each image's blue channel by 0-45 degrees #colors channel_shuffle = iaa.ChannelShuffle(1.0) #shuffle all images of the batch grayscale = iaa.Grayscale(1.0) hue_n_saturation = iaa.MultiplyHueAndSaturation( (0.5, 1.5), per_channel=True ) #change hue and saturation with this range of values for different values add_hue_saturation = iaa.AddToHueAndSaturation( (-50, 50), per_channel=True) #add more hue and saturation to its pixels #Quantize colors using k-Means clustering kmeans_color = iaa.KMeansColorQuantization( n_colors=(4, 16) ) #quantizes to k means 4 to 16 colors (randomly chosen). Quantizes colors up to 16 colors #Alpha Blending blend = iaa.AlphaElementwise((0, 1.0), iaa.Grayscale((0, 1.0))) #blend depending on which value is greater #Contrast augmentors clahe = iaa.CLAHE(tile_grid_size_px=((3, 21), [ 0, 2, 3, 4, 5, 6, 7 ])) #create a clahe contrast augmentor H=(3,21) and W=(0,7) histogram = iaa.HistogramEqualization( ) #performs histogram equalization #Augmentation list of metadata augmentors OneofRed = iaa.OneOf([red]) OneofGreen = iaa.OneOf([green]) OneofBlue = iaa.OneOf([blue]) contrast_n_shit = iaa.OneOf( [contrast, brightness, brightness_channels]) SomeAug = iaa.SomeOf( 2, [rotate, scale, translation, shear, h_flip, v_flip], random_order=True) SomeClahe = iaa.SomeOf( 2, [ clahe, iaa.CLAHE(clip_limit=(1, 10)), iaa.CLAHE(tile_grid_size_px=(3, 21)), iaa.GammaContrast((0.5, 2.0)), iaa.AllChannelsCLAHE(), iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True) ], random_order=True) #Random selection from clahe augmentors edgedetection = iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)) ]) # 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. canny_filter = iaa.OneOf([ iaa.Canny(), iaa.Canny(alpha=(0.5, 1.0), sobel_kernel_size=[3, 7]) ]) #choose one of the 2 canny filter options OneofNoise = iaa.OneOf([blur, gauss_noise, laplace_noise]) Color_1 = iaa.OneOf([ channel_shuffle, grayscale, hue_n_saturation, add_hue_saturation, kmeans_color ]) Color_2 = iaa.OneOf([ channel_shuffle, grayscale, hue_n_saturation, add_hue_saturation, kmeans_color ]) Flip = iaa.OneOf([histogram, v_flip, h_flip]) #Define the augmentors used in the DA Augmentors = [ SomeAug, SomeClahe, SomeClahe, edgedetection, sharpen, canny_filter, OneofRed, OneofGreen, OneofBlue, OneofNoise, Color_1, Color_2, Flip, contrast_n_shit ] for i in range(0, 14): img = cv2.imread(test_image) #read you image images = np.array( [img for _ in range(14)], dtype=np.uint8 ) # 12 is the size of the array that will hold 8 different images images_aug = Augmentors[i].augment_images( images ) #alternate between the different augmentors for a test image cv2.imwrite( os.path.join(output_path, test_image + "new" + str(i) + '.jpg'), images_aug[i]) #write all changed images
iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add((50, 100))), iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]) aug46 = iaa.Grayscale(alpha=(0.0, 1.0)) aug47 = iaa.ChangeColorTemperature((1100, 10000)) aug49 = iaa.UniformColorQuantization() aug50 = iaa.UniformColorQuantizationToNBits() aug51 = iaa.GammaContrast((0.5, 2.0), per_channel=True) aug52 = iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True) aug53 = iaa.LogContrast(gain=(0.6, 1.4), per_channel=True) aug54 = iaa.LinearContrast((0.4, 1.6), per_channel=True) # aug55 = iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True) aug56 = iaa.Alpha((0.0, 1.0), iaa.AllChannelsHistogramEqualization()) aug57 = iaa.HistogramEqualization( from_colorspace=iaa.HistogramEqualization.BGR, to_colorspace=iaa.HistogramEqualization.HSV) aug58 = iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.0, 0.5)) aug59 = iaa.Canny( alpha=(0.0, 0.3), colorizer=iaa.RandomColorsBinaryImageColorizer( color_true=255, color_false=0 ) ) def aug_imgaug(aug, image): image2 = image.copy() image2 = np.expand_dims(image2, axis=0)
seq2 = iaa.SomeOf( (1, 2), [ sometimes2(iaa.AdditiveGaussianNoise(scale=(10, 70))), #sometimes(iaa.AverageBlur(k=(3, 5))), # varying kernel size. sometimes2(iaa.GaussianBlur(sigma=(1.0, 3.0)) ), # not the same as gaussian noise. sometimes2(iaa.MotionBlur(k=10, angle=[-30, 30])), sometimes2( iaa.MeanShiftBlur( spatial_radius=(5.0, 40.0), color_radius=(5.0, 40.0))), # heavy but important augmenter. #These three are from contrast module, the above five from blur module. sometimes2(iaa.HistogramEqualization()), sometimes2(iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6))), sometimes2(iaa.LinearContrast(alpha=(0.6, 1.4))) ]) sometimes3 = lambda aug: iaa.Sometimes(0.93, aug) seq3 = iaa.SomeOf( (1, 2), [ #sometimes3(iaa.Affine(scale=1.5)), # cuts out part of the objects, resulting in negative coordinates.. sometimes3(iaa.ElasticTransformation(alpha=(20, 30.0), sigma=5.0)), sometimes3(iaa.Rot90((1, 3), keep_size=False) ), # On 90,270 transformation, the width and height change. sometimes3(iaa.Rotate((-30, 30))),
def generateAugmenter(augmenters, target, augmenterHistory): augmenterIndex = findAugmenterIndex(augmenters, target, augmenterHistory) if target == 'Add': augmenter = iaa.Add(int(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'AdditiveGaussianNoise': augmenter = iaa.AdditiveGaussianNoise( scale=float(augmenters[augmenterIndex][2][0].text) * 255) augmenterHistory[augmenterIndex] = 1 elif target == 'AveragePooling': augmenter = iaa.AveragePooling( kernel_size=int(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'CoarseDropout': augmenter = iaa.CoarseDropout( p=float(augmenters[augmenterIndex][2][0].text), size_percent=float(augmenters[augmenterIndex][2][1].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'Crop': augmenter = iaa.Crop( percent=(float(augmenters[augmenterIndex][2][0].text), float(augmenters[augmenterIndex][2][1].text), float(augmenters[augmenterIndex][2][2].text), float(augmenters[augmenterIndex][2][3].text)), keep_size=False) augmenterHistory[augmenterIndex] = 1 elif target == 'Dropout': augmenter = iaa.Dropout(p=float(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'ElasticTransformation': augmenter = iaa.ElasticTransformation( alpha=float(augmenters[augmenterIndex][2][0].text), sigma=float(augmenters[augmenterIndex][2][1].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'Emboss': augmenter = iaa.Emboss( alpha=float(augmenters[augmenterIndex][2][0].text), strength=float(augmenters[augmenterIndex][2][1].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'Flipud': augmenter = iaa.Flipud(p=1) augmenterHistory[augmenterIndex] = 1 elif target == 'Fliplr': augmenter = iaa.Fliplr(p=1) augmenterHistory[augmenterIndex] = 1 elif target == 'GammaContrast': augmenter = iaa.GammaContrast( gamma=float(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'GaussianBlur': augmenter = iaa.GaussianBlur( sigma=float(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'HistogramEqualization': augmenter = iaa.HistogramEqualization( to_colorspace=augmenters[augmenterIndex][2][0].text) augmenterHistory[augmenterIndex] = 1 elif target == 'JpegCompression': augmenter = iaa.JpegCompression( compression=int(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'LinearContrast': augmenter = iaa.LinearContrast( alpha=float(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'LogContrast': augmenter = iaa.LogContrast( gain=float(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'MotionBlur': augmenter = iaa.MotionBlur( k=int(augmenters[augmenterIndex][2][0].text), angle=int(augmenters[augmenterIndex][2][1].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'Pad': augmenter = iaa.Pad( percent=float(augmenters[augmenterIndex][2][0].text)) augmenterHistory[augmenterIndex] = 1 elif target == 'Rot': augmenter = iaa.Rot90(k=int(augmenters[augmenterIndex][2][0].text), keep_size=False) augmenterHistory[augmenterIndex] = 1 elif target == 'Sharpen': augmenter = iaa.Sharpen( alpha=float(augmenters[augmenterIndex][2][0].text), lightness=float(augmenters[augmenterIndex][2][1].text)) augmenterHistory[augmenterIndex] = 1 else: augmenter = iaa.SigmoidContrast( cutoff=float(augmenters[augmenterIndex][2][0].text), gain=float(augmenters[augmenterIndex][2][1].text)) augmenterHistory[augmenterIndex] = 1 return augmenter
]), # Play with the colors of the image iaa.OneOf([ iaa.Invert(0.01, per_channel=0.5), iaa.AddToHueAndSaturation((-1, 1)), iaa.MultiplyHueAndSaturation((-1, 1)) ]), # Change brightness and contrast iaa.OneOf([ iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.GammaContrast(gamma=(0.5, 1.75), per_channel=0.5), iaa.SigmoidContrast(cutoff=(0, 1), per_channel=0.5), iaa.LogContrast(gain=(0.5, 1), per_channel=0.5), iaa.LinearContrast(alpha=(0.25, 1.75), per_channel=0.5), iaa.HistogramEqualization() ]), sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.2))), iaa.JpegCompression((0.1, 1)) ] ), # With 10 % probability apply one the of the weather conditions iaa.Sometimes(0.2, iaa.OneOf([ iaa.Clouds(), iaa.Fog(), iaa.Snowflakes()
transformed_image = transform(image=image) elif augmentation == 'sigmoid_contrast': transform = iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)) transformed_image = transform(image=image) elif augmentation == 'log_contrast': transform = iaa.LogContrast(gain=(0.6, 1.4)) transformed_image = transform(image=image) elif augmentation == 'linear_contrast': transform = iaa.LinearContrast((0.4, 1.6)) transformed_image = transform(image=image) elif augmentation == 'histogram_equalization': transform = iaa.HistogramEqualization() transformed_image = transform(image=image) elif augmentation == 'all_channels_he': transform = iaa.AllChannelsHistogramEqualization() transformed_image = transform(image=image) elif augmentation == 'all_channels_clahe': transform = iaa.AllChannelsCLAHE() transformed_image = transform(image=image) ## Compression elif augmentation == 'image_compression': transform = ImageCompression(always_apply=True, quality_lower=10) transformed_image = transform(image=image)['image']
'GaussianBlur': iaa.GaussianBlur(sigma=0.50), 'AverageBlur': iaa.AverageBlur(k=3), 'AddToHueAndSaturation_p': iaa.AddToHueAndSaturation(value=25), 'AddToHueAndSaturation_n': iaa.AddToHueAndSaturation(value=-25), 'Grayscale': iaa.Grayscale(alpha=1.0), 'GammaContrast': iaa.GammaContrast(gamma=0.5, per_channel=False), 'GammaContrast_pc': iaa.GammaContrast(gamma=0.5, per_channel=True), 'HistogramEqualization': iaa.HistogramEqualization(to_colorspace='HSV'), 'Rotate_p': iaa.Affine(rotate=25), 'Rotate_n': iaa.Affine(rotate=-25) } def parse_args(): parser = argparse.ArgumentParser( description='Simple transform using imgaug') parser.add_argument('old_root', help='Root path of the original data set.') parser.add_argument('new_root', help='Root path of the transformed data set.') parser.add_argument('testlist', help='List of videos to be augmented.') parser.add_argument('--augmenter', help='Choose one kind of augmenter.')
class AugmentationScheme: # Dictionary containing all possible augmentation functions Augmentations = { # Convert images to HSV, then increase each pixel's Hue (H), Saturation (S) or Value/lightness (V) [0, 1, 2] # value by an amount in between lo and hi: "HSV": lambda channel, lo, hi: iaa.WithColorspace( to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels(channel, iaa.Add((lo, hi)))), # The augmenter first transforms images to HSV color space, then adds random values (lo to hi) # to the H and S channels and afterwards converts back to RGB. # (independently per channel and the same value for all pixels within that channel) "Add_To_Hue_And_Saturation": lambda lo, hi: iaa.AddToHueAndSaturation((lo, hi), per_channel=True), # Increase each pixel’s channel-value (redness/greenness/blueness) [0, 1, 2] by value in between lo and hi: "Increase_Channel": lambda channel, lo, hi: iaa.WithChannels(channel, iaa.Add((lo, hi))), # Rotate each image’s channel [R=0, G=1, B=2] by value in between lo and hi degrees: "Rotate_Channel": lambda channel, lo, hi: iaa.WithChannels(channel, iaa.Affine(rotate=(lo, hi))), # Augmenter that never changes input images (“no operation”). "No_Operation": iaa.Noop(), # Pads images, i.e. adds columns/rows to them. Pads image by value in between lo and hi # percent relative to its original size (only accepts positive values in range[0, 1]): # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) # NOTE: automatically resizes images back to their original size after it has augmented them. "Pad_Percent": lambda lo, hi, s_i: iaa.Pad( percent=(lo, hi), keep_size=True, sample_independently=s_i), # Pads images by a number of pixels between lo and hi # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) "Pad_Pixels": lambda lo, hi, s_i: iaa.Pad( px=(lo, hi), keep_size=True, sample_independently=s_i), # Crops/cuts away pixels at the sides of the image. # Crops images by value in between lo and hi (only accepts positive values in range[0, 1]): # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) # NOTE: automatically resizes images back to their original size after it has augmented them. "Crop_Percent": lambda lo, hi, s_i: iaa.Crop( percent=(lo, hi), keep_size=True, sample_independently=s_i), # Crops images by a number of pixels between lo and hi # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) "Crop_Pixels": lambda lo, hi, s_i: iaa.Crop( px=(lo, hi), keep_size=True, sample_independently=s_i), # Flip/mirror percent (i.e 0.5) of the input images horizontally # The default probability is 0, so to flip all images, percent=1 "Flip_lr": iaa.Fliplr(1), # Flip/mirror percent (i.e 0.5) of the input images vertically # The default probability is 0, so to flip all images, percent=1 "Flip_ud": iaa.Flipud(1), # Completely or partially transform images to their superpixel representation. # Generate s_pix_lo to s_pix_hi superpixels per image. Replace each superpixel with a probability between # prob_lo and prob_hi with range[0, 1] (sampled once per image) by its average pixel color. "Superpixels": lambda prob_lo, prob_hi, s_pix_lo, s_pix_hi: iaa.Superpixels( p_replace=(prob_lo, prob_hi), n_segments=(s_pix_lo, s_pix_hi)), # Change images to grayscale and overlay them with the original image by varying strengths, # effectively removing alpha_lo to alpha_hi of the color: "Grayscale": lambda alpha_lo, alpha_hi: iaa.Grayscale(alpha=(alpha_lo, alpha_hi)), # Blur each image with a gaussian kernel with a sigma between sigma_lo and sigma_hi: "Gaussian_Blur": lambda sigma_lo, sigma_hi: iaa.GaussianBlur(sigma=(sigma_lo, sigma_hi) ), # Blur each image using a mean over neighbourhoods that have random sizes, # which can vary between h_lo and h_hi in height and w_lo and w_hi in width: "Average_Blur": lambda h_lo, h_hi, w_lo, w_hi: iaa.AverageBlur(k=((h_lo, h_hi), (w_lo, w_hi))), # Blur each image using a median over neighbourhoods that have a random size between lo x lo and hi x hi: "Median_Blur": lambda lo, hi: iaa.MedianBlur(k=(lo, hi)), # Sharpen an image, then overlay the results with the original using an alpha between alpha_lo and alpha_hi: "Sharpen": lambda alpha_lo, alpha_hi, lightness_lo, lightness_hi: iaa. Sharpen(alpha=(alpha_lo, alpha_hi), lightness=(lightness_lo, lightness_hi)), # Emboss an image, then overlay the results with the original using an alpha between alpha_lo and alpha_hi: "Emboss": lambda alpha_lo, alpha_hi, strength_lo, strength_hi: iaa.Emboss( alpha=(alpha_lo, alpha_hi), strength=(strength_lo, strength_hi)), # Detect edges in images, turning them into black and white images and # then overlay these with the original images using random alphas between alpha_lo and alpha_hi: "Detect_Edges": lambda alpha_lo, alpha_hi: iaa.EdgeDetect(alpha=(alpha_lo, alpha_hi)), # Detect edges having random directions between dir_lo and dir_hi (i.e (0.0, 1.0) = 0 to 360 degrees) in # images, turning the images into black and white versions and then overlay these with the original images # using random alphas between alpha_lo and alpha_hi: "Directed_edge_Detect": lambda alpha_lo, alpha_hi, dir_lo, dir_hi: iaa.DirectedEdgeDetect( alpha=(alpha_lo, alpha_hi), direction=(dir_lo, dir_hi)), # Add random values between lo and hi to images. In percent of all images the values differ per channel # (3 sampled value). In the rest of the images the value is the same for all channels: "Add": lambda lo, hi, percent: iaa.Add((lo, hi), per_channel=percent), # Adds random values between lo and hi to images, with each value being sampled per pixel. # In percent of all images the values differ per channel (3 sampled value). In the rest of the images # the value is the same for all channels: "Add_Element_Wise": lambda lo, hi, percent: iaa.AddElementwise( (lo, hi), per_channel=percent), # Add gaussian noise (aka white noise) to an image, sampled once per pixel from a normal # distribution N(0, s), where s is sampled per image and varies between lo and hi*255 for percent of all # images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same normal distribution: "Additive_Gaussian_Noise": lambda lo, hi, percent: iaa.AdditiveGaussianNoise(scale=(lo, hi), per_channel=percent), # Multiply in percent of all images each pixel with random values between lo and hi and multiply # the pixels in the rest of the images channel-wise, # i.e. sample one multiplier independently per channel and pixel: "Multiply": lambda lo, hi, percent: iaa.Multiply((lo, hi), per_channel=percent), # Multiply values of pixels with possibly different values for neighbouring pixels, # making each pixel darker or brighter. Multiply each pixel with a random value between lo and hi: "Multiply_Element_Wise": lambda lo, hi, percent: iaa.MultiplyElementwise( (0.5, 1.5), per_channel=0.5), # Augmenter that sets a certain fraction of pixels in images to zero. # Sample per image a value p from the range lo<=p<=hi and then drop p percent of all pixels in the image # (i.e. convert them to black pixels), but do this independently per channel in percent of all images "Dropout": lambda lo, hi, percent: iaa.Dropout(p=(lo, hi), per_channel=percent), # Augmenter that sets rectangular areas within images to zero. # Drop d_lo to d_hi percent of all pixels by converting them to black pixels, # but do that on a lower-resolution version of the image that has s_lo to s_hi percent of the original size, # Also do this in percent of all images channel-wise, so that only the information of some # channels is set to 0 while others remain untouched: "Coarse_Dropout": lambda d_lo, d_hi, s_lo, s_hi, percent: iaa.CoarseDropout( (d_lo, d_hi), size_percent=(s_hi, s_hi), per_channel=percent), # Augmenter that inverts all values in images, i.e. sets a pixel from value v to 255-v. # For c_percent of all images, invert all pixels in these images channel-wise with probability=i_percent # (per image). In the rest of the images, invert i_percent of all channels: "Invert": lambda i_percent, c_percent: iaa.Invert(i_percent, per_channel=c_percent), # Augmenter that changes the contrast of images. # Normalize contrast by a factor of lo to hi, sampled randomly per image # and for percent of all images also independently per channel: "Contrast_Normalisation": lambda lo, hi, percent: iaa.ContrastNormalization( (lo, hi), per_channel=percent), # Scale images to a value of lo to hi percent of their original size but do this independently per axis: "Scale": lambda x_lo, x_hi, y_lo, y_hi: iaa.Affine(scale={ "x": (x_lo, x_hi), "y": (y_lo, y_hi) }), # Translate images by lo to hi percent on x-axis and y-axis independently: "Translate_Percent": lambda x_lo, x_hi, y_lo, y_hi: iaa.Affine(translate_percent={ "x": (x_lo, x_hi), "y": (y_lo, y_hi) }), # Translate images by lo to hi pixels on x-axis and y-axis independently: "Translate_Pixels": lambda x_lo, x_hi, y_lo, y_hi: iaa.Affine(translate_px={ "x": (x_lo, x_hi), "y": (y_lo, y_hi) }), # Rotate images by lo to hi degrees: "Rotate": lambda lo, hi: iaa.Affine(rotate=(lo, hi)), # Shear images by lo to hi degrees: "Shear": lambda lo, hi: iaa.Affine(shear=(lo, hi)), # Augmenter that places a regular grid of points on an image and randomly moves the neighbourhood of # these point around via affine transformations. This leads to local distortions. # Distort images locally by moving points around, each with a distance v (percent relative to image size), # where v is sampled per point from N(0, z) z is sampled per image from the range lo to hi: "Piecewise_Affine": lambda lo, hi: iaa.PiecewiseAffine(scale=(lo, hi)), # Augmenter to transform images by moving pixels locally around using displacement fields. # Distort images locally by moving individual pixels around following a distortions field with # strength sigma_lo to sigma_hi. The strength of the movement is sampled per pixel from the range # alpha_lo to alpha_hi: "Elastic_Transformation": lambda alpha_lo, alpha_hi, sigma_lo, sigma_hi: iaa. ElasticTransformation(alpha=(alpha_lo, alpha_hi), sigma=(sigma_lo, sigma_hi)), # Weather augmenters are computationally expensive and will not work effectively on certain data sets # Augmenter to draw clouds in images. "Clouds": iaa.Clouds(), # Augmenter to draw fog in images. "Fog": iaa.Fog(), # Augmenter to add falling snowflakes to images. "Snowflakes": iaa.Snowflakes(), # Replaces percent of all pixels in an image by either x or y "Replace_Element_Wise": lambda percent, x, y: iaa.ReplaceElementwise(percent, [x, y]), # Adds laplace noise (somewhere between gaussian and salt and peeper noise) to an image, sampled once per pixel # from a laplace distribution Laplace(0, s), where s is sampled per image and varies between lo and hi*255 for # percent of all images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same laplace distribution: "Additive_Laplace_Noise": lambda lo, hi, percent: iaa.AdditiveLaplaceNoise(scale=(lo, hi), per_channel=percent), # Adds poisson noise (similar to gaussian but different distribution) to an image, sampled once per pixel from # a poisson distribution Poisson(s), where s is sampled per image and varies between lo and hi for percent of # all images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same poisson distribution: "Additive_Poisson_Noise": lambda lo, hi, percent: iaa.AdditivePoissonNoise(lam=(lo, hi), per_channel=percent), # Adds salt and pepper noise to an image, i.e. some white-ish and black-ish pixels. # Replaces percent of all pixels with salt and pepper noise "Salt_And_Pepper": lambda percent: iaa.SaltAndPepper(percent), # Adds coarse salt and pepper noise to image, i.e. rectangles that contain noisy white-ish and black-ish pixels # Replaces percent of all pixels with salt/pepper in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Salt_And_Pepper": lambda percent, lo, hi: iaa.CoarseSaltAndPepper(percent, size_percent=(lo, hi)), # Adds salt noise to an image, i.e white-ish pixels # Replaces percent of all pixels with salt noise "Salt": lambda percent: iaa.Salt(percent), # Adds coarse salt noise to image, i.e. rectangles that contain noisy white-ish pixels # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Salt": lambda percent, lo, hi: iaa.CoarseSalt(percent, size_percent=(lo, hi)), # Adds Pepper noise to an image, i.e Black-ish pixels # Replaces percent of all pixels with Pepper noise "Pepper": lambda percent: iaa.Pepper(percent), # Adds coarse pepper noise to image, i.e. rectangles that contain noisy black-ish pixels # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Pepper": lambda percent, lo, hi: iaa.CoarsePepper(percent, size_percent=(lo, hi)), # In an alpha blending, two images are naively mixed. E.g. Let A be the foreground image, B be the background # image and a is the alpha value. Each pixel intensity is then computed as a * A_ij + (1-a) * B_ij. # Images passed in must be a numpy array of type (height, width, channel) "Blend_Alpha": lambda image_fg, image_bg, alpha: iaa.blend_alpha( image_fg, image_bg, alpha), # Blur/Denoise an image using a bilateral filter. # Bilateral filters blur homogeneous and textured areas, while trying to preserve edges. # Blurs all images using a bilateral filter with max distance d_lo to d_hi with ranges for sigma_colour # and sigma space being define by sc_lo/sc_hi and ss_lo/ss_hi "Bilateral_Blur": lambda d_lo, d_hi, sc_lo, sc_hi, ss_lo, ss_hi: iaa.BilateralBlur( d=(d_lo, d_hi), sigma_color=(sc_lo, sc_hi), sigma_space=(ss_lo, ss_hi)), # Augmenter that sharpens images and overlays the result with the original image. # Create a motion blur augmenter with kernel size of (kernel x kernel) and a blur angle of either x or y degrees # (randomly picked per image). "Motion_Blur": lambda kernel, x, y: iaa.MotionBlur(k=kernel, angle=[x, y]), # Augmenter to apply standard histogram equalization to images (similar to CLAHE) "Histogram_Equalization": iaa.HistogramEqualization(), # Augmenter to perform standard histogram equalization on images, applied to all channels of each input image "All_Channels_Histogram_Equalization": iaa.AllChannelsHistogramEqualization(), # Contrast Limited Adaptive Histogram Equalization (CLAHE). This augmenter applies CLAHE to images, a form of # histogram equalization that normalizes within local image patches. # Creates a CLAHE augmenter with clip limit uniformly sampled from [cl_lo..cl_hi], i.e. 1 is rather low contrast # and 50 is rather high contrast. Kernel sizes of SxS, where S is uniformly sampled from [t_lo..t_hi]. # Sampling happens once per image. (Note: more parameters are available for further specification) "CLAHE": lambda cl_lo, cl_hi, t_lo, t_hi: iaa.CLAHE( clip_limit=(cl_lo, cl_hi), tile_grid_size_px=(t_lo, t_hi)), # Contrast Limited Adaptive Histogram Equalization (refer above), applied to all channels of the input images. # CLAHE performs histogram equalization within image patches, i.e. over local neighbourhoods "All_Channels_CLAHE": lambda cl_lo, cl_hi, t_lo, t_hi: iaa.AllChannelsCLAHE( clip_limit=(cl_lo, cl_hi), tile_grid_size_px=(t_lo, t_hi)), # Augmenter that changes the contrast of images using a unique formula (using gamma). # Multiplier for gamma function is between lo and hi,, sampled randomly per image (higher values darken image) # For percent of all images values are sampled independently per channel. "Gamma_Contrast": lambda lo, hi, percent: iaa.GammaContrast( (lo, hi), per_channel=percent), # Augmenter that changes the contrast of images using a unique formula (linear). # Multiplier for linear function is between lo and hi, sampled randomly per image # For percent of all images values are sampled independently per channel. "Linear_Contrast": lambda lo, hi, percent: iaa.LinearContrast( (lo, hi), per_channel=percent), # Augmenter that changes the contrast of images using a unique formula (using log). # Multiplier for log function is between lo and hi, sampled randomly per image. # For percent of all images values are sampled independently per channel. # Values around 1.0 lead to a contrast-adjusted images. Values above 1.0 quickly lead to partially broken # images due to exceeding the datatype’s value range. "Log_Contrast": lambda lo, hi, percent: iaa.LogContrast((lo, hi), per_channel=percent), # Augmenter that changes the contrast of images using a unique formula (sigmoid). # Multiplier for sigmoid function is between lo and hi, sampled randomly per image. c_lo and c_hi decide the # cutoff value that shifts the sigmoid function in horizontal direction (Higher values mean that the switch # from dark to light pixels happens later, i.e. the pixels will remain darker). # For percent of all images values are sampled independently per channel: "Sigmoid_Contrast": lambda lo, hi, c_lo, c_hi, percent: iaa.SigmoidContrast( (lo, hi), (c_lo, c_hi), per_channel=percent), # Augmenter that calls a custom (lambda) function for each batch of input image. # Extracts Canny Edges from images (refer to description in CO) # Good default values for min and max are 100 and 200 'Custom_Canny_Edges': lambda min_val, max_val: iaa.Lambda(func_images=CO.Edges( min_value=min_val, max_value=max_val)), } # AugmentationScheme objects require images and labels. # 'augs' is a list that contains all data augmentations in the scheme def __init__(self): self.augs = [iaa.Flipud(1)] def __call__(self, image): image = np.array(image) aug_scheme = iaa.Sometimes( 0.5, iaa.SomeOf(random.randrange(1, len(self.augs) + 1), self.augs, random_order=True)) aug_img = self.aug_scheme.augment_image(image) # fixes negative strides aug_img = aug_img[..., ::1] - np.zeros_like(aug_img) return aug_img