def augment(self, n_augm, out_fname, update=False): # Define augmenter 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.9, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # translate by -20 to +20 percent (per axis) rotate=(-20, 20), # 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) )), sometimes( iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5) # add gaussian noise to images ), ], random_order=True ) with h5py.File(self.path+out_fname, 'r+') as hf: for subject_k,subject_v in hf['train/pose6/'].items(): if subject_k.split('_')[1] in ['F017', 'F018', 'F019','F020']: for segment_k,segment_v in subject_v.items(): labels = np.asarray(segment_v['aus']) idxs = self.balance(labels) labels = labels[idxs] for i in range(n_augm): node = 'train/pose6/'+subject_k+'/'+segment_k+'_'+str(i) _ = hf.create_group(node) for tp in ['faces', 'leye', 'reye', 'beye', 'nose', 'mouth', 'lmouth', 'rmouth']: print '{}/{}/{}'.format(subject_k, segment_k, tp) images = np.asarray(segment_v[tp]) if np.max(idxs)<images.shape[0]: images = images[idxs] else: print ('-------An index error-------') labels = np.asarray(segment_v['aus']) for i in range(n_augm): augm_images = seq.augment_images(images) node = 'train/pose6/'+subject_k+'/'+segment_k+'_'+str(i) print ' Writing node {} with data of shape {}'.format(node, augm_images.shape) segment = hf[node] segment.create_dataset(tp, data=augm_images) if tp=='faces': segment.create_dataset('aus', data=labels)
def get_augment_parameters(): # 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) parameters = [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip a percentage of all images iaa.Flipud(0.2), # vertically flip a percentage of all images sometimes(iaa.Crop(percent=(0, 0.05))), # crop images by percentage of their height/width sometimes(iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # scale images to percentage of their size, individually per axis translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # translate by percent (per axis) rotate=(-15, 15), # rotate by degrees shear=(-10, 10), # shear by 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 in imgaug repo) )), # 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.5), n_segments=(100, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur((0, 1.0)), # blur images with a sigma value iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.25)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 1.0)), # emboss images # search either for all edges or for directed edges sometimes(iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.5)), 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 percentage of the pixels iaa.CoarseDropout((0.03, 0.1), size_percent=(0.02, 0.1), 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 iaa.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images 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.1, 2.0), sigma=0.2)), # move pixels locally around sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))) # sometimes move parts of the image around ], random_order=True ) ] return parameters
def get_basic_imgaug_seq(seed): determinist = { "deterministic": True, "random_state": seed } train_seq = iaa.Sequential([ iaa.Affine(translate_px=(-25, 25), scale=(0.85, 1.15), rotate=(-65, 65), mode='reflect', **determinist), ], random_order=True, **determinist ) return train_seq
def get_high_zoom_imgaug_seq(seed): determinist = { "deterministic": True, "random_state": seed } train_seq = iaa.Sequential([ # iaa.Sometimes(0.45, iaa.Sharpen(alpha=0.9, lightness=(0.5, 1.15), **determinist), **determinist), iaa.Sometimes(0.45, iaa.ContrastNormalization(alpha=(0.75, 1.15), **determinist), **determinist), # iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=(0, 0.01 * 255), # per_channel=True, **determinist), **determinist), iaa.Affine(translate_px=(-5, 5), scale=(1.0, 1.75), rotate=(-65, 65), mode='reflect', **determinist), # iaa.Add(value=(-35, 35), per_channel=True), # Probably, can change nature of label ], random_order=True, **determinist ) return train_seq