def draw_single_sequential_images(): image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) """ 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), # horizontally flip 50% of all images iaa.Flipud(0.5), # vertically flip 50% of all images rarely(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation often(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width rarely(iaa.GaussianBlur((0, 3.0))), # blur images with a sigma between 0 and 3.0 rarely(iaa.AverageBlur(k=(2, 7))), # blur image using local means with kernel sizes between 2 and 7 rarely(iaa.MedianBlur(k=(3, 11))), # blur image using local medians with kernel sizes between 2 and 7 sometimes(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), # sharpen images sometimes(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))), # emboss images # search either for all edges or for directed edges rarely(iaa.Sometimes(0.5, iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), )), often(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5)), # add gaussian noise to images often(iaa.Dropout((0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels often(iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25), per_channel=0.5)), rarely(iaa.Invert(0.25, per_channel=True)), # invert color channels often(iaa.Add((-10, 10), per_channel=0.5)), # change brightness of images (by -10 to 10 of original value) often(iaa.Multiply((0.5, 1.5), per_channel=0.5)), # change brightness of images (50-150% of original value) often(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast sometimes(iaa.Grayscale(alpha=(0.0, 1.0))), often(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_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) 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) )), rarely(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) # move pixels locally around (with random strengths) ], random_order=True ) """ sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width sometimes(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_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) 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) )), # 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.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)), # 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 sometimes(iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), 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), # 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), # 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, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)), 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 ], random_order=True ) ], random_order=True ) grid = seq.draw_grid(image, cols=8, rows=8) misc.imsave("examples_grid.jpg", grid)
def test_keypoint_augmentation(): reseed() keypoints = [] for y in range(40 // 5): for x in range(60 // 5): keypoints.append(ia.Keypoint(y=y * 5, x=x * 5)) keypoints_oi = ia.KeypointsOnImage(keypoints, shape=(40, 60, 3)) keypoints_oi_empty = ia.KeypointsOnImage([], shape=(40, 60, 3)) augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), # iaa.BilateralBlur((3, 5), name="BilateralBlur"), # WithColorspace ? # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # ChangeColorspace ? # Grayscale cannot be tested, input not RGB # Convolve ? iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), # iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), # Sequential # SomeOf # OneOf # Sometimes # WithChannels # Noop # Lambda # AssertLambda # AssertShape iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize(0.5, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: dss = [] for i in range(10): aug_det = aug.to_deterministic() kp_fully_empty_aug = aug_det.augment_keypoints([]) assert kp_fully_empty_aug == [] kp_first_empty_aug = aug_det.augment_keypoints( [keypoints_oi_empty])[0] assert len(kp_first_empty_aug.keypoints) == 0 kp_image = keypoints_oi.to_keypoint_image(size=5) kp_image_aug = aug_det.augment_image(kp_image) kp_image_aug_rev = ia.KeypointsOnImage.from_keypoint_image( kp_image_aug, if_not_found_coords={ "x": -9999, "y": -9999 }, nb_channels=1) kp_aug = aug_det.augment_keypoints([keypoints_oi])[0] ds = [] assert len(kp_image_aug_rev.keypoints) == len(kp_aug.keypoints),\ "Lost keypoints for '%s' (%d vs expected %d)" \ % (aug.name, len(kp_aug.keypoints), len(kp_image_aug_rev.keypoints)) for kp_pred, kp_pred_img in zip(kp_aug.keypoints, kp_image_aug_rev.keypoints): kp_pred_lost = (kp_pred.x == -9999 and kp_pred.y == -9999) kp_pred_img_lost = (kp_pred_img.x == -9999 and kp_pred_img.y == -9999) if not kp_pred_lost and not kp_pred_img_lost: d = np.sqrt((kp_pred.x - kp_pred_img.x)**2 + (kp_pred.y - kp_pred_img.y)**2) ds.append(d) dss.extend(ds) if len(ds) == 0: print("[INFO] No valid keypoints found for '%s' " "in test_keypoint_augmentation()" % (str(aug), )) assert np.average(dss) < 5.0, \ "Average distance too high (%.2f, with ds: %s)" \ % (np.average(dss), str(dss))
iaa.AverageBlur( k=(3, 5) ), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur( k=(3, 5) ), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.9, 1.1)), # 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.01 * 255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout((0.01, 0.05), per_channel=0.5 ), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.01, 0.03), size_percent=(0.01, 0.02), per_channel=0.2), ]), iaa.Invert(0.01, per_channel=True), # invert color channels iaa.Add( (-2, 2), per_channel=0.5 ), # change brightness of images (by -10 to 10 of original value)
def draw_per_augmenter_images(): print("[draw_per_augmenter_images] Loading image...") image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) #image = misc.imresize(data.chelsea()[0:300, 50:350, :], (128, 128)) #image = misc.imresize(data.astronaut(), (128, 128)) #keypoints = [ia.Keypoint(x=43, y=43), ia.Keypoint(x=78, y=40), ia.Keypoint(x=64, y=73)] # left eye, right eye, mouth keypoints = [ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73)] # left ear, right ear, mouth keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)] print("[draw_per_augmenter_images] Initializing...") rows_augmenters = [ ("Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]), #("Crop", [iaa.Crop(px=vals) for vals in [(2, 4), (4, 8), (6, 16), (8, 32), (10, 64)]]), ("Crop\n(top, right,\nbottom, left)", [(str(vals), iaa.Crop(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]), ("Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]), ("Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]), ("Superpixels\np_replace=1", [("n_segments=%d" % (n_segments,), iaa.Superpixels(p_replace=1.0, n_segments=n_segments)) for n_segments in [25, 50, 75, 100, 125]]), ("Superpixels\nn_segments=100", [("p_replace=%.2f" % (p_replace,), iaa.Superpixels(p_replace=p_replace, n_segments=100)) for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]), ("Invert", [("p=%d" % (p,), iaa.Invert(p=p)) for p in [0, 0, 1, 1, 1]]), ("Invert\n(per_channel)", [("p=%.2f" % (p,), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]), ("Add", [("value=%d" % (val,), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]), ("Add\n(per channel)", [("value=(%d, %d)" % (vals[0], vals[1],), iaa.Add(vals, per_channel=True)) for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]]), ("Multiply", [("value=%.2f" % (val,), iaa.Multiply(val)) for val in [0.25, 0.5, 1.0, 1.25, 1.5]]), ("Multiply\n(per channel)", [("value=(%.2f, %.2f)" % (vals[0], vals[1],), iaa.Multiply(vals, per_channel=True)) for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]), ("GaussianBlur", [("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma)) for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]), ("AverageBlur", [("k=%d" % (k,), iaa.AverageBlur(k=k)) for k in [1, 3, 5, 7, 9]]), ("MedianBlur", [("k=%d" % (k,), iaa.MedianBlur(k=k)) for k in [1, 3, 5, 7, 9]]), ("Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness,), iaa.Sharpen(alpha=1, lightness=lightness)) for lightness in [0, 0.5, 1.0, 1.5, 2.0]]), ("Emboss\n(alpha=1)", [("strength=%.2f" % (strength,), iaa.Emboss(alpha=1, strength=strength)) for strength in [0, 0.5, 1.0, 1.5, 2.0]]), ("EdgeDetect", [("alpha=%.2f" % (alpha,), iaa.EdgeDetect(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), ("DirectedEdgeDetect\n(alpha=1)", [("direction=%.2f" % (direction,), iaa.DirectedEdgeDetect(alpha=1, direction=direction)) for direction in [0.0, 1*(360/5)/360, 2*(360/5)/360, 3*(360/5)/360, 4*(360/5)/360]]), ("AdditiveGaussianNoise", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]), ("AdditiveGaussianNoise\n(per channel)", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]), ("Dropout", [("p=%.2f" % (p,), iaa.Dropout(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), ("Dropout\n(per channel)", [("p=%.2f" % (p,), iaa.Dropout(p=p, per_channel=True)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), ("CoarseDropout\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), ("CoarseDropout\n(p=0.2, per channel)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), ("ContrastNormalization", [("alpha=%.1f" % (alpha,), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]), ("ContrastNormalization\n(per channel)", [("alpha=(%.2f, %.2f)" % (alphas[0], alphas[1],), iaa.ContrastNormalization(alpha=alphas, per_channel=True)) for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]), ("Grayscale", [("alpha=%.1f" % (alpha,), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), ("PiecewiseAffine", [("scale=%.3f" % (scale,), iaa.PiecewiseAffine(scale=scale)) for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]), ("Affine: Scale", [("%.1fx" % (scale,), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]), ("Affine: Translate", [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={"x": x, "y": y})) for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]), ("Affine: Rotate", [("%d deg" % (rotate,), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]]), ("Affine: Shear", [("%d deg" % (shear,), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]]), ("Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]), ("Affine: cval", [("%.2f" % (cval,), iaa.Affine(translate_px=-32, cval=cval, mode="constant")) for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]), ( "Affine: all", [ ( "", iaa.Affine( scale={"x": (0.5, 1.5), "y": (0.5, 1.5)}, translate_px={"x": (-32, 32), "y": (-32, 32)}, rotate=(-45, 45), shear=(-32, 32), mode=ia.ALL, cval=(0.0, 1.0) ) ) for _ in sm.xrange(5) ] ), ("ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha,), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]]) ] print("[draw_per_augmenter_images] Augmenting...") rows = [] for (row_name, augmenters) in rows_augmenters: row_images = [] row_keypoints = [] row_titles = [] for img_title, augmenter in augmenters: aug_det = augmenter.to_deterministic() row_images.append(aug_det.augment_image(image)) row_keypoints.append(aug_det.augment_keypoints(keypoints)[0]) row_titles.append(img_title) rows.append((row_name, row_images, row_keypoints, row_titles)) # matplotlib drawin routine """ print("[draw_per_augmenter_images] Plotting...") width = 8 height = int(1.5 * len(rows_augmenters)) fig = plt.figure(figsize=(width, height)) grid_rows = len(rows) grid_cols = 1 + 5 gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1]) axes = [] for i in sm.xrange(grid_rows): axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)]) fig.tight_layout() #fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22) #fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02) fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02) for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows): axes_row = axes[row_idx] for col_idx in sm.xrange(grid_cols): ax = axes_row[col_idx] ax.cla() ax.axis("off") ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if col_idx == 0: ax.text(0, 0.5, row_name, color="black") else: cell_image = row_images[col_idx-1] cell_keypoints = row_keypoints[col_idx-1] cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5) ax.imshow(cell_image_kp) x = 0 y = 145 #ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6) ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7) fig.savefig("examples.jpg", bbox_inches="tight") #plt.show() """ # simpler and faster drawing routine output_image = ExamplesImage(128, 128, 128+64, 32) for (row_name, row_images, row_keypoints, row_titles) in rows: row_images_kps = [] for image, keypoints in zip(row_images, row_keypoints): row_images_kps.append(keypoints.draw_on_image(image, size=5)) output_image.add_row(row_name, row_images_kps, row_titles) misc.imsave("examples.jpg", output_image.draw())
def draw_per_augmenter_images(imgName): print("[draw_per_augmenter_images] Loading image...") image = misc.imresize(ndimage.imread(imgName), (227, 227)) #keypoints = [ia.Keypoint(x=43, y=43), ia.Keypoint(x=78, y=40), ia.Keypoint(x=64, y=73)] # left eye, right eye, mouth #keypoints = [ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73)] # left ear, right ear, mouth #keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)] print("[draw_per_augmenter_images] Initializing...") rows_augmenters = [ #("Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]), #("Crop", [iaa.Crop(px=vals) for vals in [(2, 4), (4, 8), (6, 16), (8, 32), (10, 64)]]), ("Crop\n(top, right,\nbottom, left)", [(str(vals), iaa.Crop(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)]]), ("Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]), ("Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]), ("Superpixels\np_replace=1", [("n_segments=%d" % (n_segments,), iaa.Superpixels(p_replace=1.0, n_segments=n_segments)) for n_segments in [25, 50, 75, 100, 125]]), ("Superpixels\nn_segments=100", [("p_replace=%.2f" % (p_replace,), iaa.Superpixels(p_replace=p_replace, n_segments=100)) for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]), ("Invert", [("p=%d" % (p,), iaa.Invert(p=p)) for p in [0, 0, 1, 1, 1]]), ("Invert\n(per_channel)", [("p=%.2f" % (p,), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]), ("Add", [("value=%d" % (val,), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]), ("Add\n(per channel)", [("value=(%d, %d)" % (vals[0], vals[1],), iaa.Add(vals, per_channel=True)) for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]]), ("Multiply", [("value=%.2f" % (val,), iaa.Multiply(val)) for val in [0.25, 0.5, 1.0, 1.25, 1.5]]), ("Multiply\n(per channel)", [("value=(%.2f, %.2f)" % (vals[0], vals[1],), iaa.Multiply(vals, per_channel=True)) for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]), ("GaussianBlur", [("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma)) for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]), ("AverageBlur", [("k=%d" % (k,), iaa.AverageBlur(k=k)) for k in [1, 3, 5, 7, 9]]), ("MedianBlur", [("k=%d" % (k,), iaa.MedianBlur(k=k)) for k in [1, 3, 5, 7, 9]]), ("Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness,), iaa.Sharpen(alpha=1, lightness=lightness)) for lightness in [0, 0.5, 1.0, 1.5, 2.0]]), ("Emboss\n(alpha=1)", [("strength=%.2f" % (strength,), iaa.Emboss(alpha=1, strength=strength)) for strength in [0, 0.5, 1.0, 1.5, 2.0]]), ("EdgeDetect", [("alpha=%.2f" % (alpha,), iaa.EdgeDetect(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), ("DirectedEdgeDetect\n(alpha=1)", [("direction=%.2f" % (direction,), iaa.DirectedEdgeDetect(alpha=1, direction=direction)) for direction in [0.0, 1*(360/5)/360, 2*(360/5)/360, 3*(360/5)/360, 4*(360/5)/360]]), ("AdditiveGaussianNoise", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]), ("AdditiveGaussianNoise\n(per channel)", [("scale=%.2f*255" % (scale,), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]), ("Dropout", [("p=%.2f" % (p,), iaa.Dropout(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), ("Dropout\n(per channel)", [("p=%.2f" % (p,), iaa.Dropout(p=p, per_channel=True)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), ("CoarseDropout\n(p=0.2)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), ("CoarseDropout\n(p=0.2, per channel)", [("size_percent=%.2f" % (size_percent,), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), ("ContrastNormalization", [("alpha=%.1f" % (alpha,), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]), ("ContrastNormalization\n(per channel)", [("alpha=(%.2f, %.2f)" % (alphas[0], alphas[1],), iaa.ContrastNormalization(alpha=alphas, per_channel=True)) for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]), ("Grayscale", [("alpha=%.1f" % (alpha,), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), ("PiecewiseAffine", [("scale=%.3f" % (scale,), iaa.PiecewiseAffine(scale=scale)) for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]), ("Affine: Scale", [("%.1fx" % (scale,), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]), ("Affine: Translate", [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={"x": x, "y": y})) for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]), ("Affine: Rotate", [("%d deg" % (rotate,), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]]), ("Affine: Shear", [("%d deg" % (shear,), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]]), ("Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]), ("Affine: cval", [("%d" % (int(cval*255),), iaa.Affine(translate_px=-32, cval=int(cval*255), mode="constant")) for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]), ( "Affine: all", [ ( "", iaa.Affine( scale={"x": (0.5, 1.5), "y": (0.5, 1.5)}, translate_px={"x": (-32, 32), "y": (-32, 32)}, rotate=(-45, 45), shear=(-32, 32), mode=ia.ALL, cval=(0.0, 1.0) ) ) for _ in sm.xrange(5) ] ), ("ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha,), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]]) ] print("[draw_per_augmenter_images] Augmenting...") i = 0 for (row_name, augmenters) in rows_augmenters: for img_title, augmenter in augmenters: aug_det = augmenter.to_deterministic() image_aug = aug_det.augment_image(image) #print(outputDir + os.path.splitext(os.path.basename(imgName))[0] + "_%04d.jpg"% (i,)) misc.imsave(outputDir + os.path.splitext(os.path.basename(imgName))[0] + "_%04d.jpg" % (i,), image_aug) i += 1
def Augmentors(image, bbs): sometimes = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential( [ # apply the following augmenters to most images sometimes(iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis rotate=(-5, 5), # rotate by -45 to +45 degrees shear=(5, 5), # shear by -16 to +16 degrees order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) 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) )), #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, 2), [ sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur((0, 1.5)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 4)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 7)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 0.5), lightness=(0.15, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 0.5), strength=(0, 0.5)), # 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.Invert(0.05, per_channel=True), # invert color channels iaa.Add((-5, 5), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation((-5, 5)), # 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 iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True ) ], random_order=True ) try: image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) return image_aug, bbs_aug except: print("caught") return "caught", "caught"
def augment(): rotate = iaa.Affine(rotate=(-25, 25)) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop some of the images by 0-10% of their height/width sometimes(iaa.Crop(percent=(0, 0.1))), iaa.SomeOf( (0, 5), [ # Convert some images into their superpixel representation, # sample between 20 and 200 superpixels per image, but do # not replace all superpixels with their average, only # some of them (p_replace). sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # Blur each image with varying strength using # gaussian blur (sigma between 0 and 3.0), # average/uniform blur (kernel size between 2x2 and 7x7) # median blur (kernel size between 3x3 and 11x11). iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), iaa.MotionBlur(angle=(72, 144)), ]), # Search in some images either for all edges or for # directed edges. These edges are then marked in a black # and white image and overlayed with the original image # using an alpha of 0 to 0.7. sometimes( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.5)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ])), # Add gaussian noise to some images. # In 50% of these cases, the noise is randomly sampled per # channel and pixel. # In the other 50% of all cases it is sampled once per # pixel (i.e. brightness change). iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # Either drop randomly 1 to 10% of all pixels (i.e. set # them to black) or drop them on an image with 2-5% percent # of the original size, leading to large dropped # rectangles. iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), # Invert each image's channel with 5% probability. # This sets each pixel value v to 255-v. iaa.Invert(0.05, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.Add((-10, 10), per_channel=0.5), # Change brightness of images (50-150% of original value). iaa.Multiply((0.5, 1.5), per_channel=0.5), # Improve or worsen the contrast of images. iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # Sigmoid contrast iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)), # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. iaa.Grayscale(alpha=(0.0, 1.0)) ], # do all of the above augmentations in random order random_order=True) ], # do all of the above augmentations in random order random_order=True) return seq
def test_dtype_preservation(): reseed() size = (4, 16, 16, 3) images = [ np.random.uniform(0, 255, size).astype(np.uint8), np.random.uniform(0, 65535, size).astype(np.uint16), np.random.uniform(0, 4294967295, size).astype(np.uint32), np.random.uniform(-128, 127, size).astype(np.int16), np.random.uniform(-32768, 32767, size).astype(np.int32), np.random.uniform(0.0, 1.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float16), np.random.uniform(-1000.0, 1000.0, size).astype(np.float32), np.random.uniform(-1000.0, 1000.0, size).astype(np.float64) ] default_dtypes = set([arr.dtype for arr in images]) # Some dtypes are here removed per augmenter, because the respective # augmenter does not support them. This test currently only checks whether # dtypes are preserved from in- to output for all dtypes that are supported # per augmenter. # dtypes are here removed via list comprehension instead of # `default_dtypes - set([dtype])`, because the latter one simply never # removed the dtype(s) for some reason?! def _not_dts(dts): return [dt for dt in default_dtypes if dt not in dts] augs = [ (iaa.Add((-5, 5), name="Add"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AddElementwise((-5, 5), name="AddElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Multiply((0.95, 1.05), name="Multiply"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Dropout(0.01, name="Dropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Invert(0.01, per_channel=True, name="Invert"), default_dtypes), (iaa.ContrastNormalization( (0.95, 1.05), name="ContrastNormalization"), default_dtypes), (iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), _not_dts([np.float16])), (iaa.AverageBlur((3, 5), name="AverageBlur"), _not_dts([np.uint32, np.int32, np.float16])), (iaa.MedianBlur((3, 5), name="MedianBlur"), _not_dts([np.uint32, np.int32, np.float16, np.float64])), (iaa.BilateralBlur((3, 5), name="BilateralBlur"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float16, np.float64 ])), # WithColorspace ? # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # works only with RGB/uint8 # ChangeColorspace ? # iaa.Grayscale((0.0, 0.1), name="Grayscale"), # works only with RGB/uint8 # Convolve ? (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), _not_dts([np.uint32, np.int32, np.float16, np.uint32])), (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes), (iaa.Flipud(0.5, name="Flipud"), default_dtypes), (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), _not_dts([np.uint32, np.int32])), (iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), _not_dts([np.uint32, np.int32])), (iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), _not_dts([np.uint32, np.int32])), (iaa.Affine(shear=(-20, 20), name="Affine-shear"), _not_dts([np.uint32, np.int32])), (iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), _not_dts([np.uint32, np.int32])), (iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), default_dtypes), # (iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), not_dts([np.uint32])), (iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), _not_dts([np.float16])), (iaa.Sequential([iaa.Noop(), iaa.Noop()], name="SequentialNoop"), default_dtypes), (iaa.SomeOf(1, [iaa.Noop(), iaa.Noop()], name="SomeOfNoop"), default_dtypes), (iaa.OneOf([iaa.Noop(), iaa.Noop()], name="OneOfNoop"), default_dtypes), (iaa.Sometimes(0.5, iaa.Noop(), name="SometimesNoop"), default_dtypes), (iaa.Sequential([iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))], name="Sequential"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SomeOf(1, [iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))], name="SomeOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.OneOf([iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))], name="OneOf"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), _not_dts([np.uint32, np.int32, np.float64])), # WithChannels (iaa.Noop(name="Noop"), default_dtypes), # Lambda # AssertLambda # AssertShape (iaa.Alpha((0.0, 0.1), iaa.Noop(), name="AlphaNoop"), default_dtypes), (iaa.AlphaElementwise((0.0, 0.1), iaa.Noop(), name="AlphaElementwiseNoop"), default_dtypes), (iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlphaNoop"), default_dtypes), (iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Noop(), name="SimplexNoiseAlphaNoop"), default_dtypes), (iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.Superpixels(p_replace=0.01, n_segments=64), _not_dts([np.float16, np.float32, np.float64])), (iaa.Resize({ "height": 4, "width": 4 }, name="Resize"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])), (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])), (iaa.Pad(px=(0, 10), name="Pad"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])), (iaa.Crop(px=(0, 10), name="Crop"), _not_dts([ np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16, np.float64 ])) ] for (aug, allowed_dtypes) in augs: # print("aug", aug.name) # print("allowed_dtypes", allowed_dtypes) for images_i in images: if images_i.dtype in allowed_dtypes: # print("image dt", images_i.dtype) images_aug = aug.augment_images(images_i) assert images_aug.dtype == images_i.dtype else: # print("image dt", images_i.dtype, "[SKIPPED]") pass
def image_augmentation(selector=selector_default): sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image. seq = iaa.Sequential( [ # # Apply the following augmenters to most images. # iaa.Fliplr(0.5), # horizontally flip 50% of all images # crop some of the images by 0-10% of their height/width iaa.Sometimes(int(selector['Crop'])*0.5,(iaa.Crop(percent=(0, 0.1)))), # (iaa.Crop(percent=(0, 0.05))), # Apply affine transformations to some of the images # - scale to 80-120% of image height/width (each axis independently) # - translate by -20 to +20 relative to height/width (per axis) # - rotate by -45 to +45 degrees # - shear by -16 to +16 degrees # - order: use nearest neighbour or bilinear interpolation (fast) # - mode: use any available mode to fill newly created pixels # see API or scikit-image for which modes are available # - cval: if the mode is constant, then use a random brightness # for the newly created pixels (e.g. sometimes black, # sometimes white) iaa.Sometimes(int(selector['Affine'])*0.5,(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)}, rotate=(-15, 15), shear=(-3, 3), order=[0, 1], cval=(0), mode='constant' ))), # (iaa.Affine( # scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # rotate=(-15, 15), # shear=(-1, 1), # order=[0, 1], # cval=(0), # mode='constant' # )), # # Execute 0 to 5 of the following (less important) augmenters per # image. Don't execute all of them, as that would often be way too # strong. # iaa.SomeOf((0, 5), [ # Convert some images into their superpixel representation, # sample between 20 and 40 superpixels per image, but do # not replace all superpixels with their average, only # some of them (p_replace). iaa.Sometimes(int(selector['Superpixel'])*0.5,( iaa.Superpixels( p_replace=(0, 1.0), n_segments=(20, 40) ) )), # Blur each image with varying strength using # gaussian blur (sigma between 0 and 2.0), # average/uniform blur (kernel size between 2x2 and 4x4) # median blur (kernel size between 3x3 and 7x7). iaa.Sometimes(int(selector['Blur'])*1,iaa.OneOf([ iaa.GaussianBlur((0, 2.0)), iaa.AverageBlur(k=(2, 4)), iaa.MedianBlur(k=(3, 7)), ])), # Sharpen each image, overlay the result with the original # image using an alpha between 0 (no sharpening) and 1 # (full sharpening effect). iaa.Sometimes(int(selector['Sharpen'])*1,iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), # Same as sharpen, but for an embossing effect. iaa.Sometimes(int(selector['Emboss'])*1,iaa.Emboss(alpha=(0, 1.0), strength=(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. iaa.Sometimes(int(selector['EdgeDetect'])*0.5,(iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect( alpha=(0, 0.7), direction=(0.0, 1.0) ), ]))), # Add gaussian noise to some images. # In 50% of these cases, the noise is randomly sampled per # channel and pixel. # In the other 50% of all cases it is sampled once per # pixel (i.e. brightness change). iaa.Sometimes(int(selector['AdditiveGaussianNoise'])*0.5,iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05*255), per_channel=0.5 )), # Either drop randomly 1 to 10% of all pixels (i.e. set # them to black) or drop them on an image with 2-5% percent # of the original size, leading to large dropped # rectangles. iaa.Sometimes(int(selector['OneOf'])*1,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 ), ])), # # Invert each image's chanell with 5% probability. # # This sets each pixel value v to 255-v. # iaa.Invert(0.05, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.Sometimes(int(selector['Add'])*1,iaa.Add((-10, 10), per_channel=0.5)), # Change brightness of images (75-125% of original value). iaa.Sometimes(int(selector['Multiply'])*1,iaa.Multiply((0.75, 1.25), per_channel=0.5)), # Improve or worsen the contrast of images. iaa.Sometimes(int(selector['ContrastNormalization'])*1,iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. iaa.Sometimes(int(selector['Grayscale'])*1,iaa.Grayscale(alpha=(0.0, 1.0))), # In some images move pixels locally around (with random # strengths). iaa.Sometimes(int(selector['ElasticTransformation'])*0.5,( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) )), # In some images distort local areas with varying strength. iaa.Sometimes(int(selector['PiecewiseAffine'])*0.5,(iaa.PiecewiseAffine(scale=(0.01, 0.03)))) ], # do all of the above augmentations in random order random_order=True ) ], # do all of the above augmentations in random order random_order=True ) return seq
def augmentation(img): img = np.array(img) augment = iaa.Sequential( [ iaa.Fliplr(0.5), # 对50%的图像进行上下翻转 iaa.Flipud(0.5), # 对50%的图像做镜像翻转 iaa.Sometimes(0.5, iaa.Crop(percent=(0, 0.1))), iaa.Sometimes( 0.5, iaa.Affine( # 对一部分图像做仿射变换 scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # 图像缩放为80%到120%之间 translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # 平移±20%之间 rotate=(-45, 45), # 旋转±45度之间 shear=(-16, 16), # 剪切变换±16度,(矩形变平行四边形) order=[0, 1], # 使用最邻近差值或者双线性差值 cval=(0, 255), # 全白全黑填充 mode=ia.ALL # 定义填充图像外区域的方法 )), iaa.SomeOf( (0, 5), [ # 将部分图像进行超像素的表示 iaa.Sometimes( 0.5, iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # 用高斯模糊,均值模糊,中值模糊中的一种增强。注意OneOf的用法 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)), #边缘检测,将检测到的赋值0或者255然后叠在原图上 iaa.Sometimes( 0.5, iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), 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), # 将1%到10%的像素设置为黑色 # 或者将3%到15%的像素用原图大小2%到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), ]), #5%的概率反转像素的强度,即原来的强度为v那么现在的就是255-v iaa.Invert(0.05, per_channel=True), # 每个像素随机加减-10到10之间的数 iaa.Add((-10, 10), per_channel=0.5), # 像素乘上0.5或者1.5之间的数字. iaa.Multiply((0.5, 1.5), per_channel=0.5), # 将整个图像的对比度变为原来的一半或者二倍 iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # 将RGB变成灰度图然后乘alpha加在原图上 iaa.Grayscale(alpha=(0.0, 1.0)), #把像素移动到周围的地方 iaa.Sometimes( 0.5, iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # 扭曲图像的局部区域 iaa.Sometimes(0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))) ], # 随机的顺序把这些操作用在图像上 random_order=True) ], # 随机的顺序把这些操作用在图像上 random_order=True) return Image.fromarray(augment.augment_image(img))
def random_augment_images(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( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width 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) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) 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) )), # 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.Superpixels(p_replace=(0, 0.2), n_segments=(150, 200)) ), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur( (0, 1.0 )), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(2, 3) ), # blur image using local means with kernel sizes between 2 and 3 iaa.MedianBlur( k=(3, 5) ), # blur image using local medians with kernel sizes between 3 and 5 ]), iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 0.25)), # 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.25, 0.5)), iaa.DirectedEdgeDetect(alpha=(0.0, 0.25), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.03 * 255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout( (0.01, 0.05), per_channel=0.5 ), # randomly remove up to 5% of the pixels ]), 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.GammaContrast((0.8, 1.2)), iaa.Grayscale(alpha=(0.0, 0.2)), sometimes( iaa.ElasticTransformation(alpha=(0.01, 10.0), sigma=5.0) ), # 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.1))) ], random_order=True) ], random_order=True) return seq.augment_image(image)
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( '--only', default=None, help= "If this is set, then only the results of an augmenter with this name will be shown.", required=False) args = parser.parse_args() images = [ ia.quokka_square(size=(128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.AddToHueAndSaturation((-20, 20), per_channel=True, name="AddToHueAndSaturation"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), 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=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation"), iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"), ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name == args.only: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
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
def test_unusual_channel_numbers(): reseed() images = [(0, create_random_images((4, 16, 16))), (1, create_random_images((4, 16, 16, 1))), (2, create_random_images((4, 16, 16, 2))), (4, create_random_images((4, 16, 16, 4))), (5, create_random_images((4, 16, 16, 5))), (10, create_random_images((4, 16, 16, 10))), (20, create_random_images((4, 16, 16, 20)))] augs = [ iaa.Add((-5, 5), name="Add"), iaa.AddElementwise((-5, 5), name="AddElementwise"), iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"), iaa.Multiply((0.95, 1.05), name="Multiply"), iaa.Dropout(0.01, name="Dropout"), iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"), iaa.Invert(0.01, per_channel=True, name="Invert"), iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"), iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), # iaa.BilateralBlur((3, 5), name="BilateralBlur"), # works only with 3/RGB channels # WithColorspace ? # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"), # works only with 3/RGB channels # ChangeColorspace ? # iaa.Grayscale((0.0, 0.1), name="Grayscale"), # works only with 3 channels # Convolve ? iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"), iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"), iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"), iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"), iaa.Affine(rotate=(-20, 20), name="Affine-rotate"), iaa.Affine(shear=(-20, 20), name="Affine-shear"), iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"), iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), iaa.Sequential([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.SomeOf(1, [iaa.Add( (-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.OneOf([iaa.Add((-5, 5)), iaa.AddElementwise((-5, 5))]), iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"), # WithChannels iaa.Noop(name="Noop"), # Lambda # AssertLambda # AssertShape iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"), iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10), name="SimplexNoiseAlpha"), iaa.Superpixels(p_replace=0.01, n_segments=64), iaa.Resize({ "height": 4, "width": 4 }, name="Resize"), iaa.CropAndPad(px=(-10, 10), name="CropAndPad"), iaa.Pad(px=(0, 10), name="Pad"), iaa.Crop(px=(0, 10), name="Crop") ] for aug in augs: for (nb_channels, images_c) in images: if aug.name != "Resize": images_aug = aug.augment_images(images_c) assert images_aug.shape == images_c.shape image_aug = aug.augment_image(images_c[0]) assert image_aug.shape == images_c[0].shape else: images_aug = aug.augment_images(images_c) image_aug = aug.augment_image(images_c[0]) if images_c.ndim == 3: assert images_aug.shape == (4, 4, 4) assert image_aug.shape == (4, 4) else: assert images_aug.shape == (4, 4, 4, images_c.shape[3]) assert image_aug.shape == (4, 4, images_c.shape[3])
def data_export_MR_3D(scan, mask, save_path, p_num, struct_name, num_classes): # 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( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width 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) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) 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) )), # 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.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)), # 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((-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.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)), 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.1))) ], random_order=True) ], random_order=True) # Parameters for dataset length, height, width = np.shape(scan) train_size = 480 max_class = np.max(mask) # Create folders to store images/masks save_path = os.path.join(save_path, struct_name, 'processed') if not os.path.exists(os.path.join(save_path, 'PNGImages')): os.makedirs(os.path.join(save_path, 'PNGImages')) if not os.path.exists(os.path.join(save_path, 'SegmentationClass')): os.makedirs(os.path.join(save_path, 'SegmentationClass')) if not os.path.exists(os.path.join(save_path, 'SegmentationVis')): os.makedirs(os.path.join(save_path, 'SegmentationVis')) # Verify size of scan data and mask data equivalent if scan.shape == mask.shape: clahe = cv2.createCLAHE(clipLimit=10, tileGridSize=(int(scan.shape[1] / 16), int(scan.shape[2] / 16))) scan_norm = normalize_equalize_smooth_MR(scan, clahe, 0.999) images = scan_norm.transpose(0, 3, 2, 1) if height != train_size: images = (resize(images, (length, train_size, train_size, 3), anti_aliasing=True, order=2) * 255.0).astype('uint8') segmaps = np.expand_dims(mask, axis=3).astype(np.int32) if height != train_size: segmaps = resize(mask, (length, train_size, train_size, 1), anti_aliasing=True, order=0).astype(np.int32) segmaps = segmaps.transpose(0, 2, 1, 3).astype(np.int32) addAug = True for numAug in range(0, 10): images_aug, segmaps_aug = seq(images=images, segmentation_maps=segmaps.astype( np.int32)) if numAug == 0: for i in range(0, length): img = images[i, :, :, :] seg = segmaps[i, :, :, 0] if np.max(seg) > 0: img_name = os.path.join( 'PNGImages', 'ax' + str(p_num) + '_' + str(i)) gt_name = os.path.join( 'SegmentationClass', 'ax' + str(p_num) + '_' + str(i)) vis_name = os.path.join( 'SegmentationVis', 'ax' + str(p_num) + '_' + str(i)) PIL.Image.fromarray(img).save( os.path.join(save_path, img_name + '.png')) PIL.Image.fromarray(seg.astype('uint8')).save( os.path.join(save_path, gt_name + '.png')) PIL.Image.fromarray( (seg * (255 / num_classes)).astype('uint8')).save( os.path.join(save_path, vis_name + '.png')) if addAug: for i in range(0, length): img = images_aug[i, :, :, :] seg = segmaps_aug[i, :, :, 0] if np.max(seg) > 0: img_name = os.path.join( 'PNGImages', 'ax' + str(p_num) + '_' + str(i) + '_' + str(numAug)) gt_name = os.path.join( 'SegmentationClass', 'ax' + str(p_num) + '_' + str(i) + '_' + str(numAug)) vis_name = os.path.join( 'SegmentationVis', 'ax' + str(p_num) + '_' + str(i) + '_' + str(numAug)) PIL.Image.fromarray(img).save( os.path.join(save_path, img_name + '.png')) PIL.Image.fromarray(seg.astype('uint8')).save( os.path.join(save_path, gt_name + '.png')) PIL.Image.fromarray( (seg * (255 / num_classes)).astype('uint8')).save( os.path.join(save_path, vis_name + '.png'))
def test_determinism(): reseed() images = [ ia.quokka(size=(128, 128)), ia.quokka(size=(64, 64)), ia.imresize_single_image(skimage.data.astronaut(), (128, 256)) ] images.extend([ia.quokka(size=(16, 16))] * 20) keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=20, y=10), ia.Keypoint(x=5, y=5), ia.Keypoint(x=10, y=43) ], shape=(50, 60, 3)) ] * 20 augs = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))), # iaa.WithChannels([0], iaa.Add((-50, 50))), # iaa.Noop(name="Noop-nochange"), # iaa.Lambda( # func_images=lambda images, random_state, parents, hooks: images, # func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: keypoints_on_images, # name="Lambda-nochange" # ), # iaa.AssertLambda( # func_images=lambda images, random_state, parents, hooks: True, # func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: True, # name="AssertLambda-nochange" # ), # iaa.AssertShape( # (None, None, None, 3), # check_keypoints=False, # name="AssertShape-nochange" # ), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)), # iaa.ChangeColorspace(to_colorspace="GRAY"), iaa.Grayscale(alpha=(0.1, 1.0)), iaa.GaussianBlur((0.1, 3.0)), iaa.AverageBlur((3, 11)), iaa.MedianBlur((3, 11)), # iaa.Convolve(np.array([[0, 1, 0], # [1, -4, 1], # [0, 1, 0]])), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)), iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)), iaa.EdgeDetect(alpha=(0.1, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)), iaa.Add((-50, 50)), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)), iaa.Multiply((0.6, 1.4)), iaa.MultiplyElementwise((0.6, 1.4)), iaa.Dropout((0.3, 0.5)), iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)), iaa.Invert(0.5), iaa.ContrastNormalization((0.6, 1.4)), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=0.5) ] augs_affect_geometry = [ iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]), iaa.Sometimes(0.5, iaa.Fliplr(1.0)), iaa.Resize((0.5, 0.9)), iaa.CropAndPad(px=(-50, 50)), iaa.Pad(px=(1, 50)), iaa.Crop(px=(1, 50)), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), rotate=(-20, 20), shear=(-20, 20), order=ia.ALL, mode=ia.ALL, cval=(0, 255)), iaa.PiecewiseAffine(scale=(0.1, 0.3)), iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5)) ] for aug in augs: aug_det = aug.to_deterministic() images_aug1 = aug_det.augment_images(images) images_aug2 = aug_det.augment_images(images) aug_det = aug.to_deterministic() images_aug3 = aug_det.augment_images(images) images_aug4 = aug_det.augment_images(images) assert array_equal_lists(images_aug1, images_aug2), \ "Images (1, 2) expected to be identical for %s" % (aug.name,) assert array_equal_lists(images_aug3, images_aug4), \ "Images (3, 4) expected to be identical for %s" % (aug.name,) assert not array_equal_lists(images_aug1, images_aug3), \ "Images (1, 3) expected to be different for %s" % (aug.name,) for aug in augs_affect_geometry: aug_det = aug.to_deterministic() kps_aug1 = aug_det.augment_keypoints(keypoints) kps_aug2 = aug_det.augment_keypoints(keypoints) aug_det = aug.to_deterministic() kps_aug3 = aug_det.augment_keypoints(keypoints) kps_aug4 = aug_det.augment_keypoints(keypoints) assert keypoints_equal(kps_aug1, kps_aug2), \ "Keypoints (1, 2) expected to be identical for %s" % (aug.name,) assert keypoints_equal(kps_aug3, kps_aug4), \ "Keypoints (3, 4) expected to be identical for %s" % (aug.name,) assert not keypoints_equal(kps_aug1, kps_aug3), \ "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
def augmentation(image, mask): sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width 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) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) 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) )), 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)), # 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 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)), sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) image_heavy, mask_heavy = seq(images=image, segmentation_maps=mask) return image_heavy, mask_heavy
def __init__( self, inputdata, inputlabels, augs="basic", #["all","basic","form","valalt","pxlalt","imgalt"] num_outs=5, og_out=True, mode='G', em=0, intensity=1.0, rescaledata=None, formatd='NCHW', min_augs=0, max_augs=5): if self.mode.lower() == 'g': self.NM = self.rung elif self.mode.lower() == 'i': self.NM = self.runi() elif self.mode.lower() == 'i2': self.NM = self.runi2() else: print( "invalid mode, use 'g' for generator or 'i' for iterator or 'i2'" ) exit() self.minaug = min_augs self.maxaug = max_augs #self.affineopt=["scale","translate_percent","translate_px","rotate","shear"] #self.chnlopt=[{"per_channel":True},{"per_channel":False}] if len(inputdata.shape) == 4: self.D = 4 elif len(inputdata.shape) == 3: self.D = 3 elif len(inputdata.shape) == 2: self.D = 2 if formatd == "NCHW": if self.D == 4: self.inputd = np.transpose(inputdata, [0, 2, 3, 1]) elif self.D == 3: self.inputd = np.transpose(inputdata, [1, 2, 0]) else: self.inputd = inputdata self.Y = inputlabels leninten = 8 if isinstance(intensity, (float, int)): itensity = [intensity for _ in range(leninten)] else: assert len(intensity) == leninten self.datashape = np.array(inputdata.shape) #inputdata[0].shape if self.datashape.min() == self.datashape[-1]: self.pixls = self.datashape[:-1] elif self.datashape.min() == self.datashape[1]: self.pixls = np.delete(self.datashape, 1) elif self.datashape.shape == (3, ): self.pixls = self.datashape[1:] else: print("error cannot fin the shape of images") exit() # can use "keep-aspect-ratio" for an arg to have a relative and absolute scale #or can also use list for randomization between options self.scalevals = (0.5 / (2 * intensity), 1.0) #use % of image self.augs = augs self.Pchances = 0.44 * itensity[0] self.intrange = ((ceil(10 * intensity[1]), ceil(10 + 140 * itensity[1]))) self.windowrange = (ceil(2 * intensity[2]), ceil((min(self.pixls) / 5) - 8) * intensity[2] ) #mean/median things self.relatrange = (0.1 * intensity[3], 0.95 * intensity[3] ) #normalisation,invert self.bigfloat = ( 0.085 * intensity[4], 1.75 * intensity[4] ) #some scale values,multiply,contrastnorm,elasti trans,(sigman&alpha) self.smallfloat = (0.001 * intensity[5], 0.45 * intensity[5] ) #coarse dropout/droput(p) self.addrange = (ceil(-140 * intensity[6]), ceil(140 * intensity[6])) self.multrange = (-2.0 * intensity[7], 2.0 * intensity[7]) self.perchannelsplit = 0.75 * intensity[ 8] #used for per_channel on the mult self.allaugs = { "add": IAGA.Add(value=self.addrange, per_channel=0.75 * intensity), "scale": IAGA.Scale(size=self.scalevals), "adde": IAGA.AddElementwise(value=self.addrange, per_channel=0.75 * intensity), "addg": IAGA.AdditiveGaussianNoise(scale=(0, self.smallfloat[1] * 255), per_channel=0.75 * intensity), "addh": IAGA.AddToHueAndSaturation(value=self.addrange, per_channel=0.75 * intensity), "mult": IAGA.Multiply(mul=self.bigfloat, per_channel=0.75 * intensity), "mule": IAGA.MultiplyElementwise(mul=self.bigfloat, per_channel=0.75 * intensity), "drop": IAGA.Dropout(p=self.smallfloat, per_channel=0.75 * intensity), "cdrop": IAGA.CoarseDropout(p=self.smallfloat, size_px=None, size_percent=self.smallfloat, per_channel=True, min_size=3), "inv": IAGA.Invert(p=self.Pchances, per_channel=0.75 * intensity, min_value=-255, max_value=255), "cont": IAGA.ContrastNormalization(alpha=self.bigfloat, per_channel=0.75 * intensity), "aff": IAGA.Affine( scale=self.bigfloat, translate_percent={ 'x': (-40 * intensity, 40 * intensity), 'y': (-40 * intensity, 40 * intensity) }, translate_px=None, #moving functions rotate=(-360 * intensity, 360 * intensity), shear=(-360 * intensity, 360 * intensity), order=[0, 1] #2,3,4,5 may be too much , cval=0, #for filling mode=["constant", "edge", "reflect", "symmetric", "wrap"][em], #filling method deterministic=False, random_state=None), "paff": IAGA.PiecewiseAffine( scale=(-0.075 * intensity, 0.075 * intensity), nb_rows=(ceil(2 * intensity), ceil(7 * intensity)), nb_cols=(ceil(2 * intensity), ceil(7 * intensity)), order=[0, 1], cval=0, mode=["constant", "edge", "reflect", "symmetric", "wrap"][em], deterministic=False, random_state=None), "elas": IAGA.ElasticTransformation(alpha=self.bigfloat, sigma=self.relatrange), "noop": IAGA.Noop(name="nope"), #IAGA.Lambda:{}, "cropad": IAGA.CropAndPad( px=None, percent=(-0.65 * intensity[7], 0.65 * intensity[7]), pad_mode=[ "constant", "edge", "reflect", "symmetric", "wrap" ][em], pad_cval=0, keep_size=True, sample_independently=True, ), "fliplr": IAGA.Fliplr(p=self.Pchances), "flipud": IAGA.Flipud(p=self.Pchances), "spixel": IAGA.Superpixels(p_replace=self.Pchances, n_segments=self.intrange), #IAGA.ChangeColorspace:, "gray": IAGA.Grayscale(alpha=self.relatrange), "gblur": IAGA.GaussianBlur(sigma=self.bigfloat), "ablur": IAGA.AverageBlur(k=self.windowrange), "mblur": IAGA.MedianBlur(k=self.windowrange), #IAGA.BilateralBlur, #IAGA.Convolve:, "sharp": IAGA.Sharpen(alpha=self.relatrange, lightness=self.bigfloat), "embo": IAGA.Emboss(alpha=self.relatrange, strenght=self.bigfloat), "edge": IAGA.EdgeDetect(alpha=self.relatrange), "dedge": IAGA.DirectedEdgeDetect(alpha=self.bigfloat, direction=(-1.0 * intensity, 1.0 * intensity)), "pert": IAGA.PerspectiveTransform(scale=self.smallfloat), "salt": IAGA.Salt(p=self.Pchances, per_channel=0.75 * intensity), #IAGA.CoarseSalt(p=, size_px=None, size_percent=None,per_channel=False, min_size=4), #IAGA.CoarsePepper(p=, size_px=None, size_percent=None,"per_channel=False, min_size=4), #IAGA.CoarseSaltAndPepper(p=, size_px=None, size_percent=None,per_channel=False, min_size=4), "pep": IAGA.Pepper(p=self.Pchances, per_channel=0.75 * intensity), "salpep": IAGA.SaltAndPepper(p=self.Pchances, per_channel=0.75 * intensity), #"alph":IAGA.Alpha(factor=,first=,second=,per_channel=0.75*intensity,), #"aplhe":IAGA.AlphaElementwise(factor=,first=,second=,per_channel=0.75*intensity,), #IAGA.FrequencyNoiseAlpha(exponent=(-4, 4),first=None, second=None, per_channel=False,size_px_max=(4, 16), upscale_method=None,iterations=(1, 3), aggregation_method=["avg", "max"],sigmoid=0.5, sigmoid_thresh=None,), #IAGA.SimplexNoiseAlpha(first=None, second=None, per_channel=False,size_px_max=(2, 16), upscale_method=None,iterations=(1, 3), aggregation_method="max",sigmoid=True, sigmoid_thresh=None,), } ["all", "basic", "form", "valalt", "pxlalt", "imgalt"] self.augs = [] if (augs == "all") or ("all" in augs): self.augs = [ "add", "scale", "adde", "addg", "addh", "mult", "mule", "drop", "cdrop", "inv", "cont", "aff", "paff", "elas", "noop", "cropad", "fliplr", "flipud", "spixel", "gray", "gblur", "ablur", "mblur", "sharp", "embo", "edge", "dedge", "pert", "salt", "pep", "salpep", ] #"alph", "aplhe",] else: if (augs == "basic") or ("basic" in augs): self.augs.append([ "add", "scale", "addh", "mult", "drop", "cont", "noop" ]) if (augs == "form") or ("form" in augs): self.augs + [ "scale", "aff", "paff", "elas", "noop", "pert" ] if (augs == "valalt") or ("valalt" in augs): self.augs + [ "mult", "mule", "inv", "fliplr", "flipud", "cropad", "noop" ] if (augs == "pxlalt") or ("pxlalt" in augs): self.augs + [ "addg", "drop", "salt", "pep", "salpep", "noop" ] if (augs == "imgalt") or ("imgalt" in augs): self.augs + [ "elas", "noop", "spixel", "gblur", "ablur", "mblur", "sharp", "embo", "edge", "dedge", ] if len(augs) == 0: self.augs + [ "add", "scale", "addh", "drop", "cont", "aff", "elas", "noop", "cropad", "gray", "ablur", "sharp", "salpep", ] self.AUG = IAGA.SomeOf((self.minaug, self.maxaug), self.augs, random_order=True) """self.affineopts={"scale":self.biglfoat, "translate_percent":{'x':(-40*intensity,40*intensity),'y':(-40*intensity,40*intensity)}, "translate_px":None,#moving functions "rotate":(-360*intensity,360*intensity), "shear":(0*intensity,360*intensity), "order":[0,1]#2,3,4,5 may be too much , "cval":0,#for filling "mode":"constant",#filling method "deterministic":False, "random_state":None} self.pieceaffinev={"scale"=(-0.075*intensity,0.075*intensity), "nb_rows"=(ceil(2*intensity),ceil(7*intensity)), "nb_cols"=(ceil(2*intensity),ceil(7*intensity)), "order"=[0,1], "cval"=0, "mode"="constant", "deterministic"=False, "random_state"=None}""" self.num_outs = num_outs - og_out self.og_out = og_out self.mode = mode self.iimg = -1 self.iout = 0 try: self.len = inputdata.shape[0] except: self.len = len(inputdata) def __iter__(self): return self def __next__(self): return (self.NM()) def next(self): return (self.NM()) def runi(self): if self.iimg == self.len: raise StopIteration self.iimg += 1 img = self.inputd[self.iimg] y = self.Y[self.iimg] out = np.broadcast_to(img, (self.num_out, *img.shape[-3:])) out = self.AUG.augment_images(out[self.og_out:]) if self.og_out: if len(img.shape) == 3: out = np.concatenate(out, np.expand_dims(img, 0)) else: out = np.concatenate(out, img) if self.format == "NCHW": out = np.transpose(out, [0, 3, 1, 2]) return ([(outi, y) for outi in out]) def runi2(self): if self.iimg == self.len: raise StopIteration if (self.iout == self.num_outs) or (self.iimg == -1): self.iimg += 1 self.iout = 0 img = self.inputd[self.iimg] y = self.Y[self.iimg] out = np.broadcast_to(img, (self.num_out, *img.shape[-3:])) self.out = self.AUG.augment_images(out[self.og_out:]) if self.og_out: if len(img.shape) == 3: self.out = np.concatenate(out, np.expand_dims(img, 0)) else: self.out = np.concatenate(out, img) if self.format == "NCHW": self.out = np.transpose(out, [0, 3, 1, 2]) outp = (self.out[self.iout], y) else: self.iout += 1 outp = (self.out[self.iout], self.Y[self.iimg]) return (outp) def rung(self): for ix, img in enumerate(self.inputd): out = np.broadcast_to(img, (self.num_out, img.shape[-3:])) out = self.AUG.augment_images(out[self.og_out:]) y = self.Y[ix] if self.og_out: if len(img.shape) == 3: out = np.concatenate(out, np.expand_dims(img, 0)) else: out = np.concatenate(out, img) if self.format == "NCHW": out = (np.transpose(out, [0, 3, 1, 2])) for sout in out: yield (sout, y)
def Augmentation(input_image): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, './npy') sometimes = lambda aug: iaa.Sometimes(0.5, aug) aug_name = input_image.split("/")[-1].split(".")[0] minsize = 35 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 44 image_size = 200 nb_batches = 16 aug_faces = [] batches = [] seq = iaa.Sequential( [ iaa.Fliplr(0.5), sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine(scale={ "x": (0.8, 1.0), "y": (0.8, 1.0) }, translate_percent={ "x": (-0.2, 0.2), "y": (0, 0.2) }, rotate=(-10, 10), shear=(-16, 16), order=[0, 1], cval=(0, 255))), iaa.SomeOf( (0, 4), [ 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)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 1.0)), # emboss images iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.2, 0.5)), iaa.DirectedEdgeDetect(alpha=(0.2, 0.5), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.Add((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation((-20, 20)), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes( iaa.ElasticTransformation(alpha=(0.5, 2), sigma=0.25)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) img = misc.imread(input_image) if img.ndim < 2: print("Unable !") elif img.ndim == 2: img = facenet.to_rgb(img) img = img[:, :, 0:3] batches.append(np.array([img for _ in range(nb_batches)], dtype=np.uint8)) aug_images = seq.augment_images(batches[0]) for aug_img in aug_images: bounding_boxes, _ = detect_face.detect_face(aug_img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1]) img_center = img_size / 2 offsets = np.vstack([ (det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0] ]) offset_dist_squared = np.sum(np.power(offsets, 2.0), 0) index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) det = det[index, :] det = np.squeeze(det) bb_temp = np.zeros(4, dtype=np.int32) bb_temp[0] = det[0] bb_temp[1] = det[1] bb_temp[2] = det[2] bb_temp[3] = det[3] cropped_temp = aug_img[bb_temp[1]:bb_temp[3], bb_temp[0]:bb_temp[2], :] scaled_temp = misc.imresize(cropped_temp, (image_size, image_size), interp='bilinear') aug_faces.append(scaled_temp) return aug_faces
def _load_augmentation_aug_all(): """ Load image augmentation model """ def sometimes(aug): return iaa.Sometimes(0.5, aug) return iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode='constant', pad_cval=(0, 255))), sometimes( iaa.Affine( # scale images to 80-120% of their size, individually per axis scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # translate by -20 to +20 percent (per axis) translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees # use nearest neighbour or bilinear interpolation (fast) order=[0, 1], # if mode is constant, use a cval between 0 and 255 cval=(0, 255), # use any of scikit-image's warping modes # (see 2nd image from the top for examples) mode='constant')), # execute 0 to 5 of the following (less important) augmenters per # image don't execute all of them, as that would often be way too # strong iaa.SomeOf( (0, 5), [ # convert images into their superpixel representation sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), iaa.OneOf([ # blur images with a sigma between 0 and 3.0 iaa.GaussianBlur((0, 3.0)), # blur image using local means with kernel sizes # between 2 and 7 iaa.AverageBlur(k=(2, 7)), # blur image using local medians 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 # 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)), ])), # add gaussian noise to images iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ # randomly remove up to 10% of the pixels 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), ]), # invert color channels iaa.Invert(0.05, per_channel=True), # change brightness of images (by -10 to 10 of original value) iaa.Add((-10, 10), per_channel=0.5), # change hue and saturation iaa.AddToHueAndSaturation((-20, 20)), # 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))) ]), # improve or worsen the contrast iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), # move pixels locally around (with random strengths) sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # sometimes move parts of the image around sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True)
def __init__(self, train_path='cars_train', test_path='cars_test', devkit='devkit', batch_size=32, valid_split=.2): devkit_path = Path(devkit) meta = loadmat(devkit_path/'cars_meta.mat') train_annos = loadmat(devkit_path/'cars_train_annos.mat') test_annos = loadmat(devkit_path/'cars_test_annos_withlabels.mat') labels = [c for c in meta['class_names'][0]] labels = pd.DataFrame(labels, columns=['labels']) frame = [[i.flat[0] for i in line] for line in train_annos['annotations'][0]] columns = ['bbox_x1', 'bbox_y1', 'bbox_x2', 'bbox_y2', 'label', 'fname'] df = pd.DataFrame(frame, columns=columns) df['label'] = df['label']-1 # indexing starts on zero. df['fname'] = [f'{train_path}/{f}' for f in df['fname']] # Appending Path #df = df[df['label']<=75] # start with small sample for tuning initial hyperparams #df = df[(df['label']>3) & (df['label']<=5)] df_train, df_valid = train_test_split(df, test_size=valid_split) df_train = df_train.sort_index() df_valid = df_valid.sort_index() test_frame = [[i.flat[0] for i in line] for line in test_annos['annotations'][0]] df_test = pd.DataFrame(test_frame, columns=columns) df_test['label'] = df_test['label']-1 df_test['fname'] = [f'{test_path}/{f}' for f in df_test['fname']] # Appending Path df_test = df_test.sort_index() sometimes = lambda aug: iaa.Sometimes(0.5, aug) resizer = iaa.Sequential([ iaa.Resize({"height": IMG_SIZE, "width": IMG_SIZE}), ]) augmenter = iaa.Sequential([ iaa.Resize({"height": IMG_SIZE, "width": IMG_SIZE}), iaa.Fliplr(0.5), # horizontal flips iaa.Crop(percent=(0, 0.1)), # random crops # Apply affine transformations to each image. # Scale/zoom them, translate/move them, rotate them and shear them. iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, rotate=(-15, 15), shear=(-4, 4) ), # Execute 0 to 5 of the following (less important) augmenters per # image. Don't execute all of them, as that would often be way too # strong. iaa.SomeOf((0, 5), [ # Convert some images into their superpixel representation, # sample between 20 and 200 superpixels per image, but do # not replace all superpixels with their average, only # some of them (p_replace). sometimes( iaa.Superpixels( p_replace=(0, 1.0), n_segments=(20, 200) ) ), # Blur each image with varying strength using # gaussian blur (sigma between 0 and 3.0), # average/uniform blur (kernel size between 2x2 and 7x7) # median blur (kernel size between 3x3 and 11x11). iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), iaa.Alpha( factor=(0.2, 0.8), first=iaa.Sharpen(1.0, lightness=2), second=iaa.CoarseDropout(p=0.1, size_px=8), per_channel=.5 ), # Sharpen each image, overlay the result with the original # image using an alpha between 0 (no sharpening) and 1 # (full sharpening effect). iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # Same as sharpen, but for an embossing effect. iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.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. sometimes(iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.5)), iaa.DirectedEdgeDetect( alpha=(0, 0.5), direction=(0.0, 1.0) ), ])), # Add gaussian noise to some images. # In 50% of these cases, the noise is randomly sampled per # channel and pixel. # In the other 50% of all cases it is sampled once per # pixel (i.e. brightness change). iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05*255), per_channel=0.5 ), # Either drop randomly 1 to 10% of all pixels (i.e. set # them to black) or drop them on an image with 2-5% percent # of the original size, leading to large dropped # rectangles. iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2 ), ]), # Invert each image's channel with 5% probability. # This sets each pixel value v to 255-v. iaa.Invert(0.05, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.Add((-10, 10), per_channel=0.5), # Change brightness of images (50-150% of original value). iaa.Multiply((0.5, 1.5), per_channel=0.5), # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. iaa.Grayscale(alpha=(0.0, 1.0)), # In some images move pixels locally around (with random # strengths). sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # Strengthen or weaken the contrast in each image. iaa.LinearContrast((0.4, 1.6), per_channel=True), # In some images distort local areas with varying strength. sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) ], # do all of the above augmentations in random order random_order=True ) ], random_order=True) self.df_train = df_train self.df_valid = df_valid self.df_test = df_test self.labels = labels self.batch_size = batch_size self.augmenter = augmenter self.resizer = resizer