def __init__( self, instances, anchors, # for Feature Pyramid Networks we need 9 anchors, 3 for each scale labels, downsample=32, # ratio between network input's size and network output's size, 32 for YOLOv1-3 max_box_per_image=30, batch_size=1, # min_net_size=224, # max_net_size=224, shuffle=True, jitter=True, norm=None): self.instances = instances self.batch_size = batch_size self.labels = labels self.downsample = downsample self.max_box_per_image = max_box_per_image # self.min_net_size = (min_net_size // self.downsample) * self.downsample # self.max_net_size = (max_net_size // self.downsample) * self.downsample self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [ BoundBox(0, 0, anchors[2 * i], anchors[2 * i + 1]) for i in range(len(anchors) // 2) ] self.net_h = 224 self.net_w = 224 # augmentors by https://github.com/aleju/imgaug sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. self.aug_pipe = iaa.Sequential( [ sometimes(iaa.Affine()), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf( (0, 5), [ iaa.OneOf([ iaa.GaussianBlur( (0, 3.0) ), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.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 ], random_order=True) ], random_order=True) if shuffle: np.random.shuffle(self.instances)
"y": (-0.1, 0.1) }, rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-5, 5), order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) mode=['reflect'])), lesssometimes( iaa.SomeOf( (0, 5), [ iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 5)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), 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),
sometimes(iaa.Affine( scale={"x": (0.65, 1.15), "y": (0.65, 1.15)}, # scale images to 80-120% of their size, individually per axis translate_percent={"x": (-0.15, 0.15), "y": (-0.15, 0.15)}, # translate by -20 to +20 percent (per axis) rotate=(-30, 30), # 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, 3), [ iaa.OneOf([ iaa.GaussianBlur((0, 2.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(1, 5)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(1, 5)), # blur image using local medians with kernel sizes between 2 and 7 ]), 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.1), per_channel=0.5), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent=(0.01, 0.03), per_channel=0.2), ]), iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.ContrastNormalization((0.3, 1.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 ],
def black_and_white_aug(): alpha_seconds = iaa.OneOf([ iaa.Affine(rotate=(-3, 3)), iaa.Affine(translate_percent={ "x": (0.95, 1.05), "y": (0.95, 1.05) }), iaa.Affine(scale={ "x": (0.95, 1.05), "y": (0.95, 1.05) }), iaa.Affine(shear=(-2, 2)), iaa.CoarseDropout(p=0.1, size_percent=(0.08, 0.02)), ]) first_set = iaa.OneOf([ iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True), iaa.EdgeDetect((0.1, 1)), ]) second_set = iaa.OneOf([ iaa.AddToHueAndSaturation((-40, 40)), iaa.ContrastNormalization((0.5, 2.0), per_channel=True) ]) color_aug = iaa.Sequential( [ # Original Image Domain ================================================== # Geometric Rigid iaa.Fliplr(0.5), iaa.OneOf([ iaa.Noop(), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270), ]), iaa.OneOf([ iaa.Noop(), iaa.Crop(percent=(0, 0.1)), # Random Crops iaa.PerspectiveTransform(scale=(0.05, 0.15)), ]), # Affine sometimes( iaa.PiecewiseAffine( scale=(0.01, 0.07), nb_rows=(3, 6), nb_cols=(3, 6))), fewtimes( 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=(-45, 45), shear=(-16, 16), order=[0, 1], cval=0)), # Transformations outside Image domain ============================================== # COLOR, CONTRAST, HUE iaa.Invert(0.5, name='Invert'), fewtimes(iaa.Add((-10, 10), per_channel=0.5, name='Add')), fewtimes( iaa.AddToHueAndSaturation( (-40, 40), per_channel=0.5, name='AddToHueAndSaturation')), # Intensity / contrast fewtimes( iaa.ContrastNormalization( (0.8, 1.1), name='ContrastNormalization')), # Add to hue and saturation fewtimes( iaa.Multiply( (0.5, 1.5), per_channel=0.5, name='HueAndSaturation')), # Noise =========================================================================== fewtimes( iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.15 * 255), per_channel=0.5, name='AdditiveGaussianNoise')), fewtimes( iaa.Alpha(factor=(0.5, 1), first=iaa.ContrastNormalization( (0.5, 2.0), per_channel=True), second=alpha_seconds, per_channel=0.5, name='AlphaNoise'), ), fewtimes( iaa.SimplexNoiseAlpha(first=first_set, second=second_set, per_channel=0.5, aggregation_method="max", sigmoid=False, upscale_method='cubic', size_px_max=(2, 12), name='SimplexNoiseAlpha'), ), fewtimes( iaa.FrequencyNoiseAlpha(first=first_set, second=second_set, per_channel=0.5, aggregation_method="max", sigmoid=False, upscale_method='cubic', size_px_max=(2, 12), name='FrequencyNoiseAlpha'), ), # Blur fewtimes( iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), iaa.BilateralBlur(d=(3, 10), sigma_color=(10, 250), sigma_space=(10, 250)) ], name='Blur')), # Regularization ====================================================================== unlikely( iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5, name='Dropout'), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.5, name='CoarseDropout'), ], )), ], random_order=True) seq = iaa.Sequential( [ iaa.Sequential( [ # Texture rarely( iaa.Superpixels(p_replace=(0.3, 1.0), n_segments=(500, 1000), name='Superpixels')), rarely( iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.0), name='Sharpen')), rarely( iaa.Emboss( alpha=(0, 1.0), strength=(0, 1.0), name='Emboss')), rarely( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.5)), iaa.DirectedEdgeDetect(alpha=(0, 0.5), direction=(0.0, 1.0)), ], name='EdgeDetect')), rarely( iaa.ElasticTransformation( alpha=(0.5, 3.5), sigma=0.25, name='ElasticTransformation')), ], random_order=True), color_aug, iaa.Grayscale(alpha=1.0, name='Grayscale') ], random_order=False) def activator_masks(images, augmenter, parents, default): if 'Unnamed' not in augmenter.name: return False else: return default hooks_masks = ia.HooksImages(activator=activator_masks) return seq, hooks_masks
def __init__(self, images, config, shuffle=True, jitter=True, norm=True): self.images = images self.config = config self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [BoundBox(0, 0, config['ANCHORS'][2*i], config['ANCHORS'][2*i+1]) for i in range(len(config['ANCHORS'])/2)] ### augmentors by https://github.com/aleju/imgaug sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. self.aug_pipe = iaa.Sequential( [ # 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=(-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, 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 ) if shuffle: np.random.shuffle(self.images)
def __image_augmentation(image, markup): """ Искажения изображения без разметки """ seq = iaa.SomeOf( (0, 5), [ # (может очень сильно испортить текст на изображении, поэтому лучше не использовать) # была гипотеза что включением следующей строчки увеличивается recall ценой precision # но НЕТ - на последнем тесте это не подтвердилось, впрочем, окончательно сказать нельзя # так что строчка оставлена, если recall низкий - можно попробовать раскомментить # sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # # convert images into their superpixel representation # # p_replace-вероятность объединения соседних superpixel # # n_segments-количество superpixel 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.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.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.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) ], random_order=True) images_aug = Image.fromarray( seq.augment_image(np.array(image, dtype=np.uint8))) return images_aug, markup
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
'y': (-0.1, 0.1) }, shear=(-10, 10), rotate=(-30, 30))), iaa.OneOf([ iaa.GammaContrast((0.5, 1.5)), iaa.LinearContrast((0.5, 1.5)), iaa.ContrastNormalization((0.70, 1.30)), ]) ], random_order=True) Aug3 = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Sometimes( 0.5, iaa.OneOf([iaa.AverageBlur(k=(3, 5)), iaa.MotionBlur(k=(3, 5))])), iaa.Add((-15, 15), per_channel=0.5), iaa.Multiply((0.8, 1.2), per_channel=0.5), iaa.Sometimes( 0.5, iaa.Affine(scale={ 'x': (0.8, 1.2), 'y': (0.8, 1.2) }, translate_percent={ 'x': (-0.15, 0.15), 'y': (-0.15, 0.15) }, shear=(-15, 15), rotate=(-30, 30))),
def get_train_augmenters_seq1(mode='constant'): # 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 iaa.Flipud(0.2), # vertically flip 20% of all images # 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 -90 to +90 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) sometimes( iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, rotate=(-90, 90), shear=(-16, 16), order=[0, 1], cval=0, mode=mode)), # In some images distort local areas with varying strength. sometimes( iaa.PiecewiseAffine( scale=(0.01, 0.1), order=[0, 1], cval=0, mode=mode)), # Execute 0 to 2 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), [ # Blur each image with varying strength using # gaussian blur (sigma between 0 and 2.0), # average/uniform blur (kernel size between 2x2 and 5x5) # median blur (kernel size between 3x3 and 7x7). iaa.OneOf([ iaa.GaussianBlur((0, 2.0), name='GaussianBlur'), iaa.AverageBlur(k=(2, 5), name='AverageBlur'), iaa.MedianBlur(k=(3, 7), name='MedianBlur'), ]), # Sharpen or emboss each image, overlay the result with the original # image using an alpha between 0 (no sharpening) and 0.25. iaa.OneOf([ iaa.Sharpen(alpha=(0, 0.25), name='Sharpen'), iaa.Emboss(alpha=(0, 0.25), strength=(0, 0.75), name='Emboss'), ]), # Add gaussian noise to some images. # The noise is randomly sampled per pixel. # (i.e. brightness change). iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), name='AdditiveGaussianNoise'), # Add a value of -10 to 10 to each pixel for # multiply them to a number between 0.8 to 1.2. iaa.OneOf([ iaa.Add((-10, 10), name='Add_Value_to_each_Pixel'), iaa.Multiply((0.8, 1.2), name='Change_Brightness'), ]), # Improve or worsen the contrast of images. iaa.ContrastNormalization( (0.8, 1.2), name='ContrastNormalization'), ], # 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
for i in range(len(bbs_aug.bounding_boxes)): bb_box = bbs_aug.bounding_boxes[i] x_min = bb_box.x1 y_min = bb_box.y1 x_max = bb_box.x2 y_max = bb_box.y2 cls_id = bb_box.label x_cen, y_cen, w, h = xyxy2xywh(x_min, y_min, x_max, y_max) f.write("%d %.06f %.06f %.06f %.06f\n" % (cls_id, x_cen, y_cen, w, h)) Width = 640 Height = 640 blur = iaa.AverageBlur(k=(2, 11)) #! 2~11 random emboss = iaa.Emboss(alpha=(1.0, 1.0), strength=(2.0, 2.0)) gray = iaa.RemoveSaturation(from_colorspace=iaa.CSPACE_BGR) contrast = iaa.AllChannelsCLAHE(clip_limit=(10, 10), per_channel=True) bright = iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)) color = iaa.pillike.EnhanceColor() sharpen = iaa.Sharpen(alpha=(0.5, 1.0)) #! 0.5 ~ 1.0 random edge = iaa.pillike.FilterEdgeEnhance() augmentations = [[bright], [emboss], [color], [edge]] #! choice augmentation ## rotates = [[iaa.Affine(rotate=90)], [iaa.Affine(rotate=180)], [iaa.Affine(rotate=270)]] flip = iaa.Fliplr(1.0) #! 100% left & right dir = "C:\\Users\\jeongseokoon\\AI-hub\\data\\original\\"
def heavy_aug_batch(self, imgpath, n): if isinstance(imgpath, str): img = cv2.imread(imgpath) else: img = imgpath images = np.array([img for _ in range(n)], dtype=np.uint8) sometimes = lambda aug: iaa.Sometimes(0.3, 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 # crop some of the images by 0-10% of their height/width sometimes(iaa.Crop(percent=(0, 0.1))), # 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) sometimes( iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, rotate=(-45, 45), shear=(-16, 16), order=[0, 1], cval=(0, 255), mode=ia.ALL)), # # 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)), ]), # 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.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.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), # 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)), # 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) ], # do all of the above augmentations in random order random_order=True) return seq(images=images) #Example on how to use this code: #import matplotlib.pyplot as plt # (import this code) #datagen = DataAugmentor() #resimg = datagen.perform_rotate("C:\\Users\\Jared Habermehl\\Documents\\2020-Image-Super-Resolution\\big_im_SR.png") #resimg = datagen.perform_flip(resimg) #plt.axis("off") #plt.imshow(cv2.cvtColor(resimg, cv2.COLOR_BGR2RGB)) #plt.show()
if not line: break line = line.strip("\n") + ".png" lo = float(f.readline().strip("\n")) la = float(f.readline().strip("\n")) ret[line] = np.array([lo, la], dtype=np.float32) return ret imgaugment = iaa.SomeOf((0, 5), [ iaa.Noop(), iaa.Sometimes(0.2, iaa.CropAndPad(percent=(-0.05, 0.05)), # random crops ), iaa.GaussianBlur(sigma=(0, 1.8)), iaa.Sometimes(0.2, iaa.AverageBlur(k=(1, 3)) ), iaa.Sometimes(0.05, iaa.Sharpen(alpha=(0.0, 0.3), lightness=(0.9, 1.1)) ), iaa.ContrastNormalization((0.8, 1.22)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255)), ], random_order=True) imgflip = iaa.Fliplr(1) def gazeImageLoader(img_list, root_dir='data/train', batch_size=BATCH_SIZE, img_size=224, train_mode=False, imgaug=False): gaze_label = None if train_mode:
import imgaug as ia NUMBER = 7 #画像1枚当たりの牌の枚数 size_x = 512 size_y = 512 dict={"0":"1m","1":"2m","2":"3m","3":"4m","4":"5m","5":"6m","6":"7m","7":"8m","8":"9m","9":"1p","10":"2p","11":"3p","12":"4p","13":"5p","14":"6p","15":"7p","16":"8p","17":"9p","18":"1s","19":"2s","20":"3s","21":"4s","22":"5s","23":"6s","24":"7s","25":"8s","26":"9s","27":"east","28":"south","29":"west","30":"north","31":"white","32":"hatsu","33":"tyun"} # In[2]: aug1 = iaa.Dropout(p=0.2) aug2 = iaa.AverageBlur(k=(5, 20)) aug3 = iaa.Add((-40, 40), per_channel=0.5) aug4 = iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5) aug5 = iaa.Affine(rotate=(0,20)) # In[3]: #画像のロバスト def augment( img , bb , aug ): # 画像とバウンディングボックスを変換 aug_img = aug.augment_image( img ) aug_bb = aug.augment_bounding_boxes([bb])[0].remove_out_of_image().cut_out_of_image() '''
def __init__(self, config, split, batch_size, shuffle=True, jitter=False): 'Initialization' self.config = config self.split = split self.batch_size = batch_size self.image_h = config.DATA.IMG_H self.image_w = config.DATA.IMG_W self.n_channels = 3 ## TODO changed to config fold_df = pd.read_csv(self.config.FOLD_DF, engine='python') self.dataset = fold_df.loc[fold_df['split'] == self.split].reset_index(drop=True) if config.DEBUG: self.fold_df = self.fold_df[:100] print(self.split, 'set:', len(self.dataset)) self.shuffle = shuffle self.on_epoch_end() self.jitter = jitter sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.aug_pipe = 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=(-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, 3), [ # 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 )
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.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 ])), (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.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), _not_dts([np.float16])), (iaa.Sequential([iaa.Identity(), iaa.Identity()], name="SequentialNoop"), default_dtypes), (iaa.SomeOf(1, [iaa.Identity(), iaa.Identity()], name="SomeOfNoop"), default_dtypes), (iaa.OneOf([iaa.Identity(), iaa.Identity()], name="OneOfNoop"), default_dtypes), (iaa.Sometimes(0.5, iaa.Identity(), 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])), (iaa.Identity(name="Identity"), default_dtypes), (iaa.BlendAlpha((0.0, 0.1), iaa.Identity(), name="BlendAlphaIdentity"), default_dtypes), (iaa.BlendAlphaElementwise( (0.0, 0.1), iaa.Identity(), name="BlendAlphaElementwiseIdentity"), default_dtypes), (iaa.BlendAlphaSimplexNoise(iaa.Identity(), name="BlendAlphaSimplexNoiseIdentity"), default_dtypes), (iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Identity(), name="BlendAlphaFrequencyNoiseIdentity"), default_dtypes), (iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10), name="BlendAlphaElementwise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"), _not_dts([np.uint32, np.int32, np.float64])), (iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Add(10), name="BlendAlphaFrequencyNoise"), _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: for images_i in images: if images_i.dtype in allowed_dtypes: images_aug = aug.augment_images(images_i) assert images_aug.dtype == images_i.dtype
intensity_seq = iaa.Sequential([ #iaa.Invert(0.3), iaa.Sometimes(0.3, iaa.ContrastNormalization((0.5, 1.5))), iaa.OneOf([ iaa.Noop(), iaa.Sequential([ iaa.OneOf([ iaa.Add((-10, 10)), iaa.AddElementwise((-10, 10)), iaa.Multiply((0.95, 1.05)), iaa.MultiplyElementwise((0.95, 1.05)), ]), ]), iaa.OneOf([ iaa.GaussianBlur(sigma=(0.0, 1.0)), iaa.AverageBlur(k=(2, 5)), #iaa.MedianBlur(k=(3, 5)) ]) ]) ], random_order=False) tta_intensity_seq = iaa.Sequential([ iaa.Noop() ], random_order=False) def compute_random_pad(limit=(-4,4)): dy = IMG_TAR_SIZE - IMG_ORI_SIZE*SCALE dy0 = dy//2 + np.random.randint(limit[0],limit[1]) # np.random.choice(dy) dy1 = dy - dy0 dx0 = dy//2 + np.random.randint(limit[0],limit[1]) # np.random.choice(dy) dx1 = dy - dx0
def __init__(self, list_file, train, transform, device, little_train=False, S=7): print('data init') self.train = train self.transform = transform self.fnames = [] self.boxes = [] self.labels = [] self.S = S self.B = 2 self.C = 20 self.device = device self.augmentation = iaa.Sometimes( 0.5, iaa.SomeOf( (1, 6), [ iaa.Dropout([0.05, 0.2]), # drop 5% or 20% of all pixels iaa.Sharpen((0.1, 1.0)), # sharpen the image iaa.GaussianBlur(sigma=(2., 3.5)), iaa.OneOf([ iaa.GaussianBlur(sigma=(2., 3.5)), iaa.AverageBlur(k=(2, 5)), iaa.BilateralBlur(d=(7, 12), sigma_color=(10, 250), sigma_space=(10, 250)), iaa.MedianBlur(k=(3, 7)), ]), # iaa.Fliplr(1.0), # iaa.Flipud(1.0), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)), iaa.JpegCompression(compression=(80, 95)), iaa.Multiply((0.5, 1.5)), iaa.MultiplyElementwise((0.5, 1.5)), iaa.ReplaceElementwise(0.05, [0, 255]), iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 2, iaa.Add((-10, 50)))), iaa.OneOf([ iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 1, iaa.Add((-10, 50)))), iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 2, iaa.Add((-10, 50)))), ]), ], random_order=True)) torch.manual_seed(23) with open(list_file) as f: lines = f.readlines() if little_train: lines = lines[:64] for line in lines: splited = line.strip().split() self.fnames.append(splited[0]) self.num_samples = len(self.fnames)
def apply_transform(matrix, image, params, cpara): # rgb # seq describes an object for rgb image augmentation using aleju/imgaug seq = iaa.Sequential( [ # blur iaa.SomeOf((0, 2), [ iaa.GaussianBlur((0.0, 2.0)), iaa.AverageBlur(k=(3, 7)), iaa.MedianBlur(k=(3, 7)), iaa.BilateralBlur(d=(1, 7)), iaa.MotionBlur(k=(3, 7)) ]), # color iaa.SomeOf( (0, 2), [ # iaa.WithColorspace(), iaa.AddToHueAndSaturation((-15, 15)), # iaa.ChangeColorspace(to_colorspace[], alpha=0.5), iaa.Grayscale(alpha=(0.0, 0.2)) ]), # brightness iaa.OneOf([ iaa.Sequential([ iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.75, 1.25), per_channel=0.5) ]), iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.75, 1.25), per_channel=0.5), iaa.FrequencyNoiseAlpha(exponent=(-4, 0), first=iaa.Multiply( (0.75, 1.25), per_channel=0.5), second=iaa.LinearContrast( (0.7, 1.3), per_channel=0.5)) ]), # contrast iaa.SomeOf((0, 2), [ iaa.GammaContrast((0.75, 1.25), per_channel=0.5), iaa.SigmoidContrast( gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5), iaa.LogContrast(gain=(0.75, 1), per_channel=0.5), iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5) ]), ], random_order=True) image = seq.augment_image(image) ''' seq = iaa.Sequential([ iaa.Sometimes(0.5, iaa.CoarseDropout(p=0.2, size_percent=(0.1, 0.25))), iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())), iaa.Sometimes(0.5, iaa.Add((-25, 25), per_channel=0.3)), iaa.Sometimes(0.5, iaa.Invert(0.2, per_channel=True)), iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)), iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))), iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3)) ], random_order=False) image = seq.augment_image(image) ''' image = cv2.warpAffine( image, matrix[:2, :], dsize=(image.shape[1], image.shape[0]), flags=params.cvInterpolation(), borderMode=params.cvBorderMode(), borderValue=params.cval, ) return image
def imgAugmentor(images): # 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. 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 some of the images by 0-10% of their height/width sometimes(iaa.Crop(percent=(0, 0.1))), # 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) sometimes( 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=(-45, 45), shear=(-5, 5), order=[0, 1], cval=(0, 255), mode=ia.ALL)), # # 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, 1.0)), iaa.AverageBlur(k=(3, 5)), iaa.MedianBlur(k=(3, 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.8, 1.2)), # 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.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.5, 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.01 * 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.05), per_channel=0.5), iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.03), per_channel=0.2), ]), # Invert each image's channel with 5% probability. # This sets each pixel value v to 255-v. iaa.Invert(0.01, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.Add((-5, 5), per_channel=0.5), # Change brightness of images (50-150% of original value). iaa.Multiply((0.8, 1.2), per_channel=0.5), # Improve or worsen the contrast of images. iaa.LinearContrast((0.8, 1.2), 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.0), sigma=0.25) ), # 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) ], # do all of the above augmentations in random order random_order=True) images_aug = seq(images=images) return images_aug
def simple_imgaug_example(): image_dir_path = dataset_home_dir_path + '/phenotyping/cvppp2017_lsc_lcc_challenge/package/CVPPP2017_LSC_training/training/A1' label_dir_path = dataset_home_dir_path + '/phenotyping/cvppp2017_lsc_lcc_challenge/package/CVPPP2017_LSC_training/training/A1' images, labels = prepare_dataset(image_dir_path, label_dir_path) image_width, image_height = 200, 200 # FIXME [decide] >> Before or after random transformation? # Preprocessing (normalization, standardization, etc). images_pp = images.astype(np.float) #images_pp /= 255.0 images_pp = standardize_samplewise(images_pp) #images_pp = standardize_featurewise(images_pp) if True: augmenter = iaa.SomeOf( (1, 2), [ iaa.OneOf([ 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.1, 0.1), 'y': (-0.1, 0.1) }, # Translate by -10 to +10 percent (per axis). rotate=(-10, 10), # Rotate by -10 to +10 degrees. shear=(-5, 5), # Shear by -5 to +5 degrees. #order=[0, 1], # Use nearest neighbour or bilinear interpolation (fast). order= 0, # 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). #mode='edge' # Use any of scikit-image's warping modes (see 2nd image from the top for examples). ), #iaa.PiecewiseAffine(scale=(0.01, 0.05)), # Move parts of the image around. Slow. iaa.PerspectiveTransform(scale=(0.01, 0.1)), iaa.ElasticTransformation( alpha=(20.0, 50.0), sigma=(6.5, 8.5) ), # Move pixels locally around (with random strengths). ]), iaa.OneOf([ iaa.GaussianBlur(sigma=( 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.MotionBlur(k=(5, 11), angle=(0, 360), direction=(-1.0, 1.0), order=1), ]), iaa.OneOf([ iaa.AdditiveGaussianNoise( loc=0, scale=(0.1 * 255, 0.5 * 255), per_channel=False), # Add Gaussian noise to images. iaa.AdditiveLaplaceNoise(loc=0, scale=(0.1 * 255, 0.4 * 255), per_channel=False), iaa.AdditivePoissonNoise(lam=(32, 96), per_channel=False), iaa.CoarseSaltAndPepper(p=(0.1, 0.3), size_percent=(0.2, 0.9), per_channel=False), iaa.CoarseSalt(p=(0.1, 0.3), size_percent=(0.2, 0.9), per_channel=False), iaa.CoarsePepper(p=(0.1, 0.3), size_percent=(0.2, 0.9), per_channel=False), iaa.CoarseDropout(p=(0.1, 0.3), size_percent=(0.05, 0.3), per_channel=False), ]), iaa.OneOf([ iaa.MultiplyHueAndSaturation(mul=(-10, 10), per_channel=False), iaa.AddToHueAndSaturation(value=(-255, 255), per_channel=False), iaa.LinearContrast( alpha=(0.5, 1.5), per_channel=False), # Improve or worsen the contrast. iaa.Invert(p=1, per_channel=False), # Invert color channels. 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. ]), ], random_order=True) elif False: augmenter = 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. iaa.Sometimes( 0.5, iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), iaa.Sometimes( 0.5, 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), [ iaa.Sometimes( 0.5, 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.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)), iaa.Sometimes( 0.5, iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # Move pixels locally around (with random strengths). iaa.Sometimes( 0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05)) ), # Sometimes move parts of the image around. iaa.Sometimes( 0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) else: augmenter = iaa.Sequential([ iaa.SomeOf( 1, [ #iaa.Sometimes(0.5, iaa.Crop(px=(0, 100))), # Crop images from each side by 0 to 16px (randomly chosen). iaa.Sometimes(0.5, iaa.Crop(percent=( 0, 0.1))), # Crop images by 0-10% of their height/width. iaa.Fliplr(0.5), # Horizontally flip 50% of the images. iaa.Flipud(0.5), # Vertically flip 50% of the images. iaa.Sometimes( 0.5, 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). order= 0, # 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). #mode='edge' # Use any of scikit-image's warping modes (see 2nd image from the top for examples). )), iaa.Sometimes(0.5, iaa.GaussianBlur( sigma=(0, 3.0))) # Blur images with a sigma of 0 to 3.0. ]), iaa.Scale(size={ 'height': image_height, 'width': image_width }) # Resize. ]) for idx in range(images.shape[0]): images_pp[idx] = (images_pp[idx] - np.min(images_pp[idx])) / ( np.max(images_pp[idx]) - np.min(images_pp[idx])) * 255 images_pp = images_pp.astype(np.uint8) # Test 1 (good). augmenter_det = augmenter.to_deterministic( ) # Call this for each batch again, NOT only once at the start. #images_aug1 = augmenter_det.augment_images(images) images_aug1 = augmenter_det.augment_images(images_pp) labels_aug1 = augmenter_det.augment_images(labels) augmenter_det = augmenter.to_deterministic( ) # Call this for each batch again, NOT only once at the start. #images_aug2 = augmenter_det.augment_images(images) images_aug2 = augmenter_det.augment_images(images_pp) labels_aug2 = augmenter_det.augment_images(labels) #export_images(images, labels, './augmented1/img', '') export_images(images_pp, labels, './augmented1/img', '') export_images(images_aug1, labels_aug1, './augmented1/img', '_aug1') export_images(images_aug2, labels_aug2, './augmented1/img', '_aug2') # Test 2 (bad). augmenter_det = augmenter.to_deterministic( ) # Call this for each batch again, NOT only once at the start. #images_aug1 = augmenter_det.augment_images(images) images_aug1 = augmenter_det.augment_images(images_pp) labels_aug1 = augmenter_det.augment_images(labels) #images_aug2 = augmenter_det.augment_images(images) images_aug2 = augmenter_det.augment_images(images_pp) labels_aug2 = augmenter_det.augment_images(labels) #export_images(images, labels, './augmented2/img', '') export_images(images_pp, labels, './augmented2/img', '') export_images(images_aug1, labels_aug1, './augmented2/img', '_aug1') export_images(images_aug2, labels_aug2, './augmented2/img', '_aug2') print('*********************************', images_pp.dtype)
def get_optimistic_img_aug(): texture = iaa.OneOf([ iaa.Superpixels(p_replace=(0.1, 0.3), n_segments=(500, 1000), interpolation="cubic", name='Superpixels'), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.5, 1.0), name='Sharpen'), iaa.Emboss(alpha=(0, 1.0), strength=(0.1, 0.3), name='Emboss'), iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.4)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ], name='EdgeDetect'), iaa.ElasticTransformation(alpha=(0.5, 1.0), sigma=0.2, name='ElasticTransformation'), ]) blur = iaa.OneOf([ iaa.GaussianBlur((1, 5.0), name='GaussianBlur'), iaa.AverageBlur(k=(2, 15), name='AverageBlur'), iaa.MedianBlur(k=(3, 15), name='MedianBlur'), iaa.BilateralBlur(d=(3, 15), sigma_color=(10, 250), sigma_space=(10, 250), name='BilaBlur'), ]) affine = iaa.OneOf([ iaa.Affine(rotate=(-3, 3)), iaa.Affine(translate_percent={ "x": (0.95, 1.05), "y": (0.95, 1.05) }), iaa.Affine(scale={ "x": (0.95, 1.05), "y": (0.95, 1.05) }), iaa.Affine(shear=(-2, 2)), ]) factors = iaa.OneOf([ iaa.Multiply(iap.Choice([0.75, 1.25]), per_channel=False), iaa.EdgeDetect(1.0), ]) seq = iaa.Sequential( [ # Size and shape ================================================== iaa.Sequential([ iaa.Fliplr(0.5), iaa.OneOf([ iaa.Noop(), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270), ]), half_times( iaa.SomeOf( (1, 2), [ iaa.Crop(percent=(0.1, 0.4)), # Random Crops iaa.PerspectiveTransform(scale=(0.10, 0.175)), iaa.PiecewiseAffine(scale=(0.01, 0.06), nb_rows=(3, 6), nb_cols=(3, 6)), ])), ]), # Texture ================================================== sometimes( iaa.SomeOf((1, 2), [ texture, iaa.Alpha((0.0, 1.0), first=texture, per_channel=False) ], random_order=True, name='Texture')), half_times( iaa.SomeOf((1, 2), [ blur, iaa.Alpha((0.0, 1.0), first=blur, per_channel=False), iaa.Alpha(factor=(0.2, 0.8), first=iaa.Sequential([ affine, blur, ]), per_channel=False), ], random_order=True, name='Blur')), # Noise ================================================== sometimes( iaa.SomeOf( (1, 2), [ # Just noise iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.15 * 255), per_channel=False, name='AdditiveGaussianNoise'), iaa.SaltAndPepper( 0.05, per_channel=False, name='SaltAndPepper'), # Regularization iaa.Dropout( (0.01, 0.1), per_channel=False, name='Dropout'), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=False, name='CoarseDropout'), iaa.Alpha( factor=(0.2, 0.8), first=texture, second=iaa.CoarseDropout( p=0.1, size_percent=(0.02, 0.05)), per_channel=False, ), # Perlin style noise iaa.SimplexNoiseAlpha(first=factors, per_channel=False, aggregation_method="max", sigmoid=False, upscale_method='cubic', size_px_max=(2, 12), name='SimplexNoiseAlpha'), iaa.FrequencyNoiseAlpha(first=factors, per_channel=False, aggregation_method="max", sigmoid=False, upscale_method='cubic', size_px_max=(2, 12), name='FrequencyNoiseAlpha'), ], random_order=True, name='Noise')), ], random_order=False) def activator_masks(images, augmenter, parents, default): if 'Unnamed' not in augmenter.name: return False else: return default hooks_masks = ia.HooksImages(activator=activator_masks) return seq, hooks_masks
new_img = Image.fromarray(new_img) new_label = Image.fromarray(new_label) new_inter = Image.fromarray(new_inter) new_img.save("../data/GenData/TrainData/images/" + str("%04d" % j) + "_flip_" + str(i) + "_.png") new_label.save("../data/GenData/TrainData/labels/" + str("%04d" % j) + "_flip_" + str(i) + "_.png") new_inter.save("../data/GenData/TrainData/watershed/" + str("%04d" % j) + "_flip_" + str(i) + "_.png") print("Finished augmentations for rotations and cropping..") # Blur = 5 blur = [ iaa.GaussianBlur(sigma=0.9), iaa.GaussianBlur(sigma=2.9), iaa.AverageBlur(k=7), iaa.AverageBlur(k=9), iaa.MedianBlur(k=7), ] for i in tqdm(range(len(blur)), total=len(blur)): for j, sample in tqdm(enumerate(data), total=len(data)): img, label, inter = sample new_img = blur[i].augment_image(img) new_img = Image.fromarray(new_img) label = Image.fromarray(label) inter = Image.fromarray(inter) new_img.save("../data/GenData/TrainData/images/" + str("%04d" % j) + "_blur_" + str(i) + "_.png") label.save("../data/GenData/TrainData/labels/" + str("%04d" % j) + "_blur_" + str(i) + "_.png")
def main(): # datapath为存放训练图片的地方 datapath = '/home/zhex/data/yuncong/' # original_file为需要被增强的 original_file = '/home/zhex/data/yuncong/UCSD_train.txt' # 需要被增强的训练真值txt # aug_file只记录了新增的增强后图片的box,要得到原始+增强的所有label:cat original_file augfile>finalfile(txt拼接) # aug_file输出是pdpd需要的格式,pytorch需要另行转换(可以拼接得到finalfile后直接将finalfile转换) aug_file = 'augfile_UCSD.txt' dict_before = readlist(original_file) new_fp = open(aug_file, 'w') # augscene = {'Mall': 3, 'Part_B': 10, 'Part_A': 13} # 需要哪些场景,新增几倍数量的新数据 augscene = {'UCSD': 2} for scene in augscene: for i in range(augscene[scene]): for img_id in dict_before.keys(): if scene in img_id: print(img_id) img = Image.open(datapath + img_id) img = np.array(img) bbs = ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=x, y1=y, x2=x + w, y2=y + h) for [x, y, w, h] in dict_before[img_id] ], shape=img.shape) # 设置数据增强方式 seq = iaa.SomeOf( (1, 3), [ iaa.Crop(px=(0, 16)), # 裁剪 iaa.Multiply((0.7, 1.3)), # 改变色调 iaa.Affine(scale=(0.5, 0.7)), # 放射变换 iaa.GaussianBlur(sigma=(0, 1.5)), # 高斯模糊 # iaa.AddToHueAndSaturation(value=(25,-25)), iaa.ChannelShuffle(1), # RGB三通道随机交换 iaa.ElasticTransformation(alpha=0.1), # iaa.Grayscale(alpha=(0.2, 0.5)), iaa.Pepper(p=0.03), iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)), iaa.Dropout(p=(0.03, 0.05)), iaa.Salt(p=(0.03, 0.05)), iaa.AverageBlur(k=(1, 3)), iaa.Add((-10, 10)), iaa.CoarseSalt(size_percent=0.01) ]) seq_det = seq.to_deterministic( ) # 保持坐标和图像同步改变,每个batch都要调用一次,不然每次的增强都是一样的 image_aug = seq_det.augment_images([img])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] pic_name = img_id.split('/')[-1].split('.')[0] pic_dir = img_id.split(pic_name)[0] if not os.path.exists(datapath + 'myaug/' + pic_dir): os.makedirs(datapath + 'myaug/' + pic_dir) new_img_id = 'myaug/' + pic_dir + pic_name + '_{}'.format( i) + '.jpg' Image.fromarray(image_aug).save(datapath + new_img_id) new_fp = writelist(new_fp, new_img_id, bbs_aug.bounding_boxes)
def test_many_augmenters(self): keypoints = [] for y in sm.xrange(40 // 5): for x in sm.xrange(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.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), 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.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"), iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"), iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10), name="BlendAlphaElementwise"), iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"), iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Add(10), name="BlendAlphaSimplexNoise"), 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 sm.xrange(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) assert len(kp_first_empty_aug.keypoints) == 0 kp_image = keypoints_oi.to_keypoint_image(size=5) with assertWarns(self, iaa.SuspiciousSingleImageShapeWarning): 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))) gen = zip(kp_aug.keypoints, kp_image_aug_rev.keypoints) for kp_pred, kp_pred_img in gen: 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))
def __init__(self): super(ImgAugTransform, self).__init__() from imgaug import augmenters as iaa from imgaug import parameters as iap self.seq = iaa.Sequential(children=[ iaa.Sequential(children=[ iaa.Sequential(children=[ iaa.OneOf(children=[ iaa.Sometimes( p=0.95, then_list=iaa.Affine(scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-30, 30), shear=(-15, 15), order=iap.Choice( [0, 1, 3], p=[0.15, 0.80, 0.05]), mode="reflect", name="Affine")), iaa.Sometimes(p=0.05, then_list=iaa.PerspectiveTransform( scale=(0.01, 0.1))) ], name="Blur"), iaa.Sometimes(p=0.01, then_list=iaa.PiecewiseAffine( scale=(0.0, 0.01), nb_rows=(4, 20), nb_cols=(4, 20), order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]), mode="reflect", name="PiecewiseAffine")) ], random_order=True, name="GeomTransform"), iaa.Sequential(children=[ iaa.Sometimes(p=0.75, then_list=iaa.Add(value=(-10, 10), per_channel=0.5, name="Brightness")), iaa.Sometimes(p=0.05, then_list=iaa.Emboss(alpha=(0.0, 0.5), strength=(0.5, 1.2), name="Emboss")), iaa.Sometimes(p=0.1, then_list=iaa.Sharpen(alpha=(0.0, 0.5), lightness=(0.5, 1.2), name="Sharpen")), iaa.Sometimes(p=0.25, then_list=iaa.ContrastNormalization( alpha=(0.5, 1.5), per_channel=0.5, name="ContrastNormalization")) ], random_order=True, name="ColorTransform"), iaa.Sequential(children=[ iaa.Sometimes(p=0.5, then_list=iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 10.0), per_channel=0.5, name="AdditiveGaussianNoise")), iaa.Sometimes(p=0.1, then_list=iaa.SaltAndPepper( p=(0, 0.001), per_channel=0.5, name="SaltAndPepper")) ], random_order=True, name="Noise"), iaa.OneOf(children=[ iaa.Sometimes(p=0.05, then_list=iaa.MedianBlur(k=3, name="MedianBlur")), iaa.Sometimes(p=0.05, then_list=iaa.AverageBlur( k=(2, 4), name="AverageBlur")), iaa.Sometimes(p=0.5, then_list=iaa.GaussianBlur( sigma=(0.0, 2.0), name="GaussianBlur")) ], name="Blur"), ], random_order=True, name="MainProcess") ])
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.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"), iaa.AverageBlur((3, 5), name="AverageBlur"), iaa.MedianBlur((3, 5), name="MedianBlur"), 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"), iaa.Identity(name="Noop"), iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"), iaa.BlendAlphaElementwise((0.0, 0.1), iaa.Add(10), name="BlendAlphaElementwise"), iaa.BlendAlphaSimplexNoise(iaa.Add(10), name="BlendAlphaSimplexNoise"), iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2), foreground=iaa.Add(10), name="BlendAlphaSimplexNoise"), 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_augmentation(self, sequence): """ square or 16x9 """ random.seed(self.seed) self.inc_seed() #square = random.randint(0,1) square = 0 """ hflip """ random.seed(self.seed) self.inc_seed() hflip = random.randint(0, 1) hflip_aug = iaa.Sequential([iaa.Fliplr(1)]) hflip_det = hflip_aug.to_deterministic() zoom_scale_rot_aug = iaa.Sequential([ 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=(-10, 10)) ], random_order=True) zoom_scale_rot_det = zoom_scale_rot_aug.to_deterministic() counter = 0 for frame in sequence: if square: """ square or 16x9 """ blank_image = np.zeros((256, 256, 3), np.uint8) blank_image[:, :] = (79, 225, 7) frame = cv2.resize(frame, (144, 144), interpolation=cv2.INTER_AREA) x_offset = y_offset = 56 blank_image[y_offset:y_offset + frame.shape[0], x_offset:x_offset + frame.shape[1]] = frame frame = blank_image if hflip: """ hflip """ frame = hflip_det.augment_image(frame) """ Zoom & Verschiebung """ #frame = zoom_scale_rot_det.augment_image(frame) """ black Background after rotation to green """ #hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #sensitivity is a int, typically set to 15 - 20 #lower_black = np.array([0, 0, 0]) #upper_black = np.array([180, 255, 10]) #mask = cv2.inRange(hsv, lower_black, upper_black) #frame[mask>0]=(79,255,7) """ Background Pic or Color """ sequence[counter] = frame counter = counter + 1 sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # # Execute 0 to 5 of the following (less important) augmenters per # image. Don't execute all of them, as that would often be way too # strong. # iaa.SomeOf( (0, 1), [ # 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)), ]), # 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.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.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.10), size_percent=(0.02, 0.03), per_channel=0.2), ]), 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), # 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)), #sometimes( # iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) #), # 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) ], # do all of the above augmentations in random order random_order=True) sequence = seq(images=sequence) return sequence
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.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.Grayscale(alpha=(0.1, 1.0)), iaa.GaussianBlur((0.1, 3.0)), iaa.AverageBlur((3, 11)), iaa.MedianBlur((3, 11)), 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.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 _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.BlendAlphaSimplexNoise( 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.BlendAlphaFrequencyNoise( exponent=(-4, 0), foreground=iaa.Multiply( (0.5, 1.5), per_channel=True), background=iaa.contrast.LinearContrast((0.5, 2.0))) ]), # improve or worsen the contrast iaa.contrast.LinearContrast((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 Augmentors(orgimage, 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( 1, [ 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=orgimage, bounding_boxes=bbs) return image_aug, bbs_aug except: print("caught") return "caught", "caught"