def draw_single_sequential_images(): image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) st = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Flipud(0.5), st(iaa.Crop(percent=(0, 0.1))), st(iaa.GaussianBlur((0, 3.0))), st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5)), st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), st(iaa.Add((-10, 10), per_channel=0.5)), st(iaa.Multiply((0.5, 1.5), per_channel=0.5)), st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), st(iaa.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, 1.0), mode=ia.ALL )), st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) ], random_order=True ) grid = seq.draw_grid(image, cols=8, rows=8) misc.imsave("examples_grid.jpg", grid)
def main(): images = [ misc.imresize( ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), 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.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.ContrastNormalization(alpha=(0.5, 2.0)), 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, 1.0), mode=ia.ALL, name="Affine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0) ] #for i, aug in enumerate(augmenters): #print(i) #aug.deepcopy() #import copy #copy.deepcopy(aug) seq = iaa.Sequential([aug.copy() for aug in augmenters], name="Sequential") st = iaa.Sometimes(0.5, seq.copy(), name="Sometimes") augmenters.append(seq) augmenters.append(st) for augmenter in augmenters: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
def augment_data(): aug_data = np.zeros((4000, 227, 227, 3)) st = 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 st(iaa.Crop( percent=(0, 0.1))), # crop images by 0-10% of their height/width st(iaa.GaussianBlur( (0, 3.0))), # blur images with a sigma between 0 and 3.0 st( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.2), per_channel=0.5)), # add gaussian noise to images st(iaa.Dropout( (0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels st(iaa.Add( (-10, 10), per_channel=0.5 )), # change brightness of images (by -10 to 10 of original value) st(iaa.Multiply((0.5, 1.5), per_channel=0.5) ), # change brightness of images (50-150% of original value) st(iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast st( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # scale images to 80-120% of their size, individually per axis translate_px={ "x": (-16, 16), "y": (-16, 16) }, # translate by -16 to +16 pixels (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees #order=ia.ALL, # use any of scikit-image's interpolation methods cval=( 0, 1.0 ), # if mode is constant, use a cval between 0 and 1.0 #mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ) # apply elastic transformations with random strengths ], random_order=True # do all of the above in random order ) for i in range(4000): img = plt.imread(trainfileName(i + 1)) image_aug = seq.augment_image(img) #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #gray = rgb2gray(img) aug_data[i, ...] = image_aug.astype(float) / 255 np.save("aug_data_1.npy", aug_data) #augment_data()
def example_heavy_augmentations(): print("Example: Heavy Augmentations") import imgaug as ia #from imgaug import augmenters as iaa import augmenters as iaa # random example images images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8) # 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. st = 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( [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.5), # vertically flip 50% of all images st(iaa.Crop( percent=(0, 0.1))), # crop images by 0-10% of their height/width st(iaa.GaussianBlur( (0, 3.0))), # blur images with a sigma between 0 and 3.0 st( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)), # add gaussian noise to images st(iaa.Dropout( (0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels st(iaa.Add( (-10, 10), per_channel=0.5 )), # change brightness of images (by -10 to 10 of original value) st(iaa.Multiply((0.5, 1.5), per_channel=0.5) ), # change brightness of images (50-150% of original value) st(iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast st(iaa.Grayscale((0.0, 1.0))), # blend with grayscale image st( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # scale images to 80-120% of their size, individually per axis translate_px={ "x": (-16, 16), "y": (-16, 16) }, # translate by -16 to +16 pixels (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use scikit-image's interpolation orders 0 (nearest neighbour) and 1 (bilinear) cval=( 0, 1.0 ), # if mode is constant, use a cval between 0 and 1.0 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ) # apply elastic transformations with random strengths ], random_order=True # do all of the above in random order ) images_aug = seq.augment_images(images) # ----- # Make sure that the example really does something assert not np.array_equal(images, images_aug)