Returns: A Tensor. Has the same type as input. Has the shape of tensor.shape * repeats """ expanded_tensor = tf.expand_dims(tensor, -1) multiples = [1] + repeats tiled_tensor = tf.tile(expanded_tensor, multiples = multiples) repeated_tesnor = tf.reshape(tiled_tensor, tf.shape(tensor) * repeats) return repeated_tesnor # data aug seq = iaa.Sequential([ iaa.Sometimes(0.5, iaa.Affine( translate_percent={"x": (-0.3, 0.3), "y": (-0.3, 0.3)}, rotate=(-10, 10), scale={"x": (0.5, 1.1), "y": (0.5, 1.1)}, ) ), iaa.Fliplr(1.0), # Horizonatl flips ], random_order=True) # apply augmenters in random order # class class CNN(): def __init__(self,k,inc,out,stddev): self.w = tf.Variable(tf.random_normal([k,k,inc,out],stddev=stddev)) self.m,self.v_prev = tf.Variable(tf.zeros_like(self.w)),tf.Variable(tf.zeros_like(self.w)) self.v_hat_prev = tf.Variable(tf.zeros_like(self.w)) def getw(self): return self.w
def get_operation_pool(): """ Get list of operations """ def equalize_wrap(images, random_state, parents, hooks): return equalize(images) def color_wrap(images, random_state, parents, hooks): return color(images) def solarize_60_wrap(images, random_state, parents, hooks): return solarize(images, threshold=60) def solarize_80_wrap(images, random_state, parents, hooks): return solarize(images, threshold=80) def posterize_wrap_3(images, random_state, parents, hooks): return posterize(images, bit=3) def posterize_wrap_6( images, random_state, parents, hooks, ): return posterize(images, bit=6) return [ iaa.Fliplr(.5, name="fliplr_0.5"), iaa.Sometimes(.3, iaa.Lambda(func_images=equalize_wrap, func_keypoints=None), name="equalize_0.3"), iaa.Sometimes( .7, iaa.Lambda(func_images=equalize_wrap, func_keypoints=None, name="euqalize_0.7")), iaa.Sometimes(.3, iaa.Affine(rotate=(-30, 30)), name="rotate_30_0.3"), iaa.Sometimes(.7, iaa.Affine(rotate=(-30, 30)), name="rotate_30_0.7"), iaa.Sometimes(.3, iaa.Affine(shear=(-16, 16)), name="shear_16_0.3"), iaa.Sometimes(.7, iaa.Affine(shear=(-16, 16)), name="shear_16_0.7"), iaa.Sometimes(.3, iaa.Lambda(func_images=color_wrap, func_keypoints=None), name="color_0.3"), iaa.Sometimes(.7, iaa.Lambda(func_images=color_wrap, func_keypoints=None), name="color_0.7"), iaa.Sometimes(.3, iaa.Lambda(func_images=solarize_60_wrap, func_keypoints=None), name="solarize_60_0.3"), iaa.Sometimes(.7, iaa.Lambda(func_images=solarize_60_wrap, func_keypoints=None), name="solarize_60_0.7"), iaa.Sometimes(.3, iaa.Lambda(func_images=solarize_80_wrap, func_keypoints=None), name="solarize_80_0.3"), iaa.Sometimes(.7, iaa.Lambda(func_images=solarize_80_wrap, func_keypoints=None), name="solarize_80_0.7"), iaa.Sometimes(.3, iaa.Lambda(func_images=posterize_wrap_3, func_keypoints=None), name="posterize_3_0.3"), iaa.Sometimes(.7, iaa.Lambda(func_images=posterize_wrap_3, func_keypoints=None), name="posterize_3_0.7"), iaa.Sometimes(.3, iaa.Lambda(func_images=posterize_wrap_6, func_keypoints=None), name="posterize_6_0.3"), iaa.Sometimes(.7, iaa.Lambda(func_images=posterize_wrap_6, func_keypoints=None), name="posterize_6_0.7"), iaa.Sometimes(.3, iaa.Lambda(func_images=cutout_wrap, func_keypoints=None), name="cutout_0.3"), iaa.Sometimes(.7, iaa.Lambda(func_images=cutout_wrap, func_keypoints=None), name="cutout_0.7") ]
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( "--only", default=None, help= "If this is set, then only the results of an augmenter with this name will be shown. " "Optionally, comma-separated list.", required=False) args = parser.parse_args() images = [ ia.quokka_square(size=(128, 128)), ia.imresize_single_image(data.astronaut(), (128, 128)) ] keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=50, y=40, vis=None, label=None), ia.Keypoint(x=70, y=38, vis=None, label=None), ia.Keypoint(x=62, y=52, vis=None, label=None) ], shape=images[0].shape), ia.KeypointsOnImage([ ia.Keypoint(x=55, y=32, vis=None, label=None), ia.Keypoint(x=42, y=95, vis=None, label=None), ia.Keypoint(x=75, y=89, vis=None, label=None) ], shape=images[1].shape) ] bounding_boxes = [ ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=10, y1=10, x2=20, y2=20), ia.BoundingBox(x1=40, y1=50, x2=70, y2=60) ], shape=images[0].shape), ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=10, y1=10, x2=20, y2=20), ia.BoundingBox(x1=40, y1=50, x2=70, y2=60) ], shape=images[1].shape) ] augmenters = [ iaa.Sequential([ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="Sequential"), iaa.SomeOf(2, children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="SomeOf"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=0.1 * 255), name="Sometimes"), iaa.WithColorspace("HSV", children=[iaa.Add(20)], name="WithColorspace"), iaa.WithChannels([0], children=[iaa.Add(20)], name="WithChannels"), iaa.AddToHueAndSaturation((-20, 20), per_channel=True, name="AddToHueAndSaturation"), iaa.Noop(name="Noop"), iaa.Resize({ "width": 64, "height": 64 }, name="Resize"), iaa.CropAndPad(px=(-8, 8), name="CropAndPad-px"), iaa.Pad(px=(0, 8), name="Pad-px"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=10, name="BilateralBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.SaltAndPepper(p=0.05, name="SaltAndPepper"), iaa.Salt(p=0.05, name="Salt"), iaa.Pepper(p=0.05, name="Pepper"), iaa.CoarseSaltAndPepper(p=0.05, size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.CoarseSalt(p=0.05, size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.CoarsePepper(p=0.05, size_percent=(0.01, 0.1), name="CoarsePepper"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Add(100), second=iaa.Dropout(0.5), per_channel=False, name="Alpha"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Add(100), second=iaa.Dropout(0.5), per_channel=True, name="AlphaPerChannel"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Affine(rotate=(-45, 45)), per_channel=True, name="AlphaAffine"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Add(50), second=iaa.ContrastNormalization(2.0), per_channel=False, name="AlphaElementwise"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Add(50), second=iaa.ContrastNormalization(2.0), per_channel=True, name="AlphaElementwisePerChannel"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Affine(rotate=(-45, 45)), per_channel=True, name="AlphaElementwiseAffine"), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=False, name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=False, name="FrequencyNoiseAlpha") ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name in [ v.strip() for v in args.only.split(",") ]: print("Augmenter: %s" % (augmenter.name, )) grid = [] for image, kps, bbs in zip(images, keypoints, bounding_boxes): aug_det = augmenter.to_deterministic() imgs_aug = aug_det.augment_images( np.tile(image[np.newaxis, ...], (16, 1, 1, 1))) kps_aug = aug_det.augment_keypoints([kps] * 16) bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16) imgs_aug_drawn = [ kps_aug_one.draw_on_image(img_aug) for img_aug, kps_aug_one in zip(imgs_aug, kps_aug) ] imgs_aug_drawn = [ bbs_aug_one.draw_on_image(img_aug) for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug) ] grid.append(np.hstack(imgs_aug_drawn)) ia.imshow(np.vstack(grid))
def pan(image): pan = iaa.Affine(translate_percent= {"x" : (-0.1, 0.1), "y": (-0.1, 0.1)}) image = pan.augment_image(image) return image
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( '--only', default=None, help= "If this is set, then only the results of an augmenter with this name will be shown.", required=False) args = parser.parse_args() images = [ ia.quokka_square(size=(128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.AddToHueAndSaturation((-20, 20), per_channel=True, name="AddHueAndSaturation"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation") ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name == args.only: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
# - 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), [
# iaa.Sequential([iaa.size.Scale(0.9),iaa.size.Scale(1/0.9)]),])), #This inverts completely inverts the text ( make black white etc.) sometimes_1(iaa.Invert(1, per_channel=True)), #This does some affine transformations sometimes_2( iaa.Affine( scale={ "x": (0.8, 1), "y": (0.8, 1) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0, 0), "y": (-0.1, 0.1) }, # translate by -20 to +20 percent (per axis) rotate=(-2, 2), # rotate by -45 to +45 degrees shear=(-2, 2), # 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=[ "edge" ] # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), sometimes_2( iaa.OneOf([ iaa.GaussianBlur( (0, 1.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(2, 3) ), # blur image using local means with kernel sizes between 2 and 7
from collections import defaultdict import pandas as pd from sklearn.model_selection import train_test_split import numpy as np import cv2 from PIL import Image from imgaug import augmenters as iaa from config import * train_seq = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontal flips iaa.Flipud(0.5), iaa.Affine(scale={ "x": (1.0, 1.2), "y": (1.0, 1.2) }, ) ], random_order=True) # apply augmenters in random order class sample_gen(object): def __init__(self, file_class_mapping): self.file_class_mapping = file_class_mapping self.class_to_list_files = defaultdict(list) self.list_all_files = file_class_mapping.keys() self.range_all_files = range(len(self.list_all_files)) for file, class_ in file_class_mapping.items():
with open(os.path.join(DATA_DIR, "label_descriptions.json"), "r") as fh: label_description = json.load(fh) fh.close() n_classes = len(label_description['categories']) n_attributes = len(label_description['attributes']) print("Number of Classes: %d, Attributes: %d" % (n_classes, n_attributes)) DATA_AUG = iaa.Sequential([ ## ref. Kaggle starter code ## Image rotation, translation, flipping: iaa.OneOf([ iaa.Fliplr(0.2), iaa.Affine(scale={ "x": (0.99, 1.01), "y": (0.98, 1.03) }, translate_percent={ "x": (-0.025, 0.025), "y": (-0.05, 0.05) }), ]), ## Adjustment of contrast or resolution: iaa.OneOf([ iaa.ContrastNormalization((0.75, 1.05)), iaa.GaussianBlur(sigma=(0.0, 0.1)), ]), ]) def train_val_test_split(data, seed=SEED, val_size=0.10, test_size=3200): """ Shared wrapper to split data """ AGG_FUNC = lambda vec: list(vec)
for index in range(6): # 한장당 늘리려는 개수 print(len(images)) for idx in range(len(images)): image = images[idx] # 이 옵션들이 다 랜덤 범위값으로 모두 랜덤하게 생겨서 한장을 복제함 seq = iaa.Sequential([ iaa.Fliplr(0.5), # horizontally flip 50% of all images, 좌우반전 확률 # iaa.Flipud(0.5), # vertically flip 20% of all images, 상하반전 확률 # iaa.LinearContrast((0.95, 1.05)), #대조 범위 약간 색감 찐하게하거나 흐리게 색을 변형 iaa.Multiply((0.75, 1), per_channel=0.1), #밝기 범위 iaa.Affine( scale={ "x": (0.75, 1.0), "y": (0.75, 1.0) }, #줌 아웃 # translate_percent={"x": (-0.01, 0.01), "y": (-0.01 ,0.01)}, #줌 아웃시 px 이동, scale이랑 같게줘야함 rotate=(-10, 10) # 회전 >> 이걸 크게주면 없어지는 이미지의 범위 있을수잇음, 모서리 # shear=(-20, 20) ) ]) seq_det = seq.to_deterministic() image_aug = seq_det.augment_images([image])[0] new_image_file = save_data_dir + 'after_' + str(index) + '.jpg' cv2.imwrite(new_image_file, image_aug)
def dataAugment(origimgfilepath, annofilepath, objectList, numImgGen, destdir): try: images = [] image = cv2.imread(origimgfilepath) imgpath, imgname = os.path.split(origimgfilepath) annopath, annoname = os.path.split(annofilepath) keypoints_on_images = [] keypoints = [] objclassprocessed = [] #Add keypoints for all Objects for labelItem in objectList: key = str(labelItem.label) xmin = float(labelItem.xmin) ymin = float(labelItem.ymin) xmax = float(labelItem.xmax) ymax = float(labelItem.ymax) objclassprocessed.append(key) x = xmin y = ymin keypoints.append(ia.Keypoint(x=x, y=y)) x = xmax y = ymax keypoints.append(ia.Keypoint(x=x, y=y)) x = xmin y = ymax keypoints.append(ia.Keypoint(x=x, y=y)) x = xmax y = ymin keypoints.append(ia.Keypoint(x=x, y=y)) keypoints_on_images.append( ia.KeypointsOnImage(keypoints, shape=image.shape)) # Generate required number of Images for i in range(0, numImgGen): images.append(image) keypoints_on_images.append( ia.KeypointsOnImage(keypoints, shape=image.shape)) # Add Image Augmentation Properties seq = iaa.Sequential([ # iaa.Multiply((1.2,1.5)), # Adds Brightness, Doesn't Affect Keypoints # iaa.Grayscale(alpha=(0.0, 1.0)), # , Doesn't Affect Keypoints # iaa.Dropout((0.01, 0.1), per_channel=0.5) iaa.GaussianBlur( (0, 3.0)), #Adds Gausian Blur, Doesn't Affect keypoints iaa.Affine( scale=(0.9, 1.1), #Transforms to 50 % to 110%, Affects keypoints rotate=(-1, 0)) ]) # Make our sequence deterministic. # We can now apply it to the image and then to the keypoints and it will # lead to the same augmentations. # IMPORTANT: Call this once PER BATCH, otherwise you will always get the # exactly same augmentations for every batch! seq_det = seq.to_deterministic() # Augment keypoints and images images_aug = seq_det.augment_images(images) keypoints_aug = seq_det.augment_keypoints(keypoints_on_images) # print("images_aug", images_aug) # print("keypoints_aug", keypoints_aug) # Globally Modified keypoints and Index # gblmodifiedkeypoints = {} # gblidx = 1 # Example code to show each image and print the new keypoints coordinates for img_idx, (image_before, image_after, keypoints_before, keypoints_after) in enumerate( zip(images, images_aug, keypoints_on_images, keypoints_aug)): image_before = keypoints_before.draw_on_image(image_before) image_after = keypoints_after.draw_on_image(image_after) # Write Augmented Image to a file image_after_name = '_Augmented_' + str(img_idx) + imgname image_after_path = os.path.join(destdir, image_after_name) cv2.imwrite(image_after_path, image_after) # Image File Props imgSize = os.path.getsize(image_after_path) imgHeight, imgWidth, imgChannel = image_after.shape fileProps = fileProperties(image_after_name, destdir, image_after_path, imgWidth, imgHeight, imgChannel, imgSize) # Show Image Before And After Augmentation # ia.imshow(np.concatenate((image_before, image_after), axis=1)) # before and after # Print All the Keypoints for each of the images # for kp_idx, keypoint in enumerate(keypoints_after.keypoints): # keypoint_old = keypoints_on_images[img_idx].keypoints[kp_idx] # x_old, y_old = keypoint_old.x, keypoint_old.y # x_new, y_new = keypoint.x, keypoint.y # print("[Keypoints for image #%d] before aug: x=%d y=%d | after aug: x=%d y=%d" % (img_idx, x_old, y_old, x_new, y_new)) #Convert Keypoints back to Image Co-ordinates to be written to XML modifiedrect = [] modifiedkeypoints = {} idx = 1 cntr = 1 print("Generating Augmented Image {} of {} for {}".format( img_idx + 1, len(images), imgname)) for kp_idx, keypoint in enumerate(keypoints_after.keypoints): x_new, y_new = keypoint.x, keypoint.y modifiedrect.append([int(x_new), int(y_new)]) cntr += 1 if (cntr > 4): modifiedkeypoints[idx] = modifiedrect modifiedrect = [] cntr = 1 idx += 1 # objclassidx = 0 objectList = [] # print("Total Processed",len(objclassprocessed)) for key, modkey in modifiedkeypoints.items(): newimgrect = getImgRect(image_after_path, modkey) # objclassidx += 1 # objclassidex = int(objclassidx) % len(objclassprocessed) # objclass = objclassprocessed[objclassidex] objclass = objclassprocessed[key - 1] # print("Label", objclass,"Rectangle", newimgrect) labelObject = returnObject(objclass, newimgrect[0], newimgrect[1], newimgrect[2], newimgrect[3]) objectList.append(labelObject) setAugAnnotationXML(fileProps, objectList) except Exception as e: print("Error" + str(e)) print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
import cv2 from PIL import Image from imgaug import augmenters as iaa ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Image path") args = vars(ap.parse_args()) imagepath = args["image"] if not os.path.exists(imagepath): print("Not found {}...".format(imagepath)) exit(1) image = cv2.imread(imagepath) seq = iaa.Sequential( [iaa.Affine(rotate=[0], shear={ 'x': 5, 'y': 15 }, mode='edge')]) image = seq.augment_image(image) Image.fromarray(image).show() if input("Save? ") in ["yes", "y", "yep"]: cv2.imwrite("shear-" + os.path.basename(imagepath), image)
def compute_cluster(image_path): """each cluster can be computed independently""" image_path_data = Path(image_path) stem = image_path_data.stem basename = image_path_data.name image_annotation_path = get_image_annotation_path(stem) image_annotation_path_data = Path(image_annotation_path) print("Processing {}...".format(stem)) if image_annotation_path: labels_df = decode_image_annotation(image_annotation_path) shutil.copy(image_path, "{}/{}".format(DATASET_AUGM_IMAGES_DIR, basename)) shutil.copy( image_annotation_path, "{}/{}".format(DATASET_AUGM_ANNOTS_DIR, image_annotation_path_data.name)) for i in range(0, AUG_NB_AUGMENTATION_PER_IMAGE): # This setup of augmentation parameters will pick 1 to 4 # of the given augmenters and apply them in random order. sometimes = lambda aug: augmenters.Sometimes(0.5, aug) aug_config = augmenters.SomeOf( (1, 4), [ augmenters.Affine(scale=(0.1, 1.5)), augmenters.Affine(rotate=(-45, 45)), augmenters.Affine(translate_percent={ "x": (-0.3, 0.3), "y": (-0.3, 0.3) }), # augmenters.Affine(shear=(-16, 16)), augmenters.OneOf([ augmenters.Fliplr(1), augmenters.Flipud(1), ]), augmenters.Rot90(1), # sometimes( # augmenters.Superpixels( # p_replace=(0, 1.0), # n_segments=(20, 200) # ) # ), augmenters.OneOf([ augmenters.GaussianBlur((0, 3.0)), augmenters.AverageBlur(k=(2, 7)), augmenters.MedianBlur(k=(3, 11)), augmenters.MotionBlur(k=3), ]), augmenters.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sometimes(augmenters.OneOf([ # augmenters.EdgeDetect(alpha=(0, 0.7)), # augmenters.DirectedEdgeDetect( # alpha=(0, 0.7), direction=(0.0, 1.0) # ), # ])), augmenters.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), augmenters.OneOf([ augmenters.Dropout((0.05, 0.3), per_channel=0.5), augmenters.CoarseDropout((0.02, 0.05), size_percent=(0.01, 0.02), per_channel=0.2), ]), augmenters.OneOf([ augmenters.pillike.EnhanceColor(), augmenters.Add((-10, 10)), augmenters.Multiply((0.5, 1.5)), augmenters.LinearContrast((0.5, 2.0), per_channel=0.5), augmenters.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)), # augmenters.Grayscale(alpha=(0.0, 1.0)), ]), sometimes( augmenters.ElasticTransformation(alpha=(0.2, 1.5), sigma=0.25)), sometimes(augmenters.PiecewiseAffine(scale=(0.01, 0.05))) ], random_order=True) aug_file_suffix = '_aug_{}'.format(i) try: augmented_image_df = augment_image(labels_df, DATASET_IMAGES_DIR, DATASET_AUGM_IMAGES_DIR, aug_file_suffix, aug_config) image_annotation_dest_path = "{}/{}{}{}".format( DATASET_AUGM_ANNOTS_DIR, stem, aug_file_suffix, image_annotation_path_data.suffix) if (len(augmented_image_df.index)): xml_annotations_et = get_xml_from_dataframe( image_annotation_path, augmented_image_df) xml_annotations_et.write(image_annotation_dest_path) except: print("Failed to augment {}".format(aug_file_suffix)) os.remove(image_annotation_path) os.remove(image_path) print("Removed: {}".format(image_path.split('/')[-1]))
import os import random import numpy as np import cv2 from imgaug import augmenters as iaa seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), ]), iaa.Flipud(0.2), # horizontally flip iaa.SomeOf(2, [ iaa.Affine(rotate=(-40, 40), translate_percent={"x": (-0.25, 0.25)}, mode='symmetric', cval=(0)), iaa.PerspectiveTransform(scale=(0.06, 0.12)), iaa.PiecewiseAffine(scale=(0.02, 0.04), mode='edge', cval=(0)), ]), ]) seq_det = seq.to_deterministic() def do_augmentation(sequent, image): augimage = np.array(sequent.augment_image(image)) return np.reshape(augimage, (-1, 299, 299, 3)) def getimagefrompath(path, imgbyclass): classnumb = len(os.listdir(path))
seed_everything(SEED) # --- # ### data # #### augmentation # In[3]: augs = iaa.SomeOf((0, 3), [ iaa.OneOf([ iaa.Affine(scale={ "x": (0.8, 1.), "y": (0.8, 1.) }, rotate=(-15, 15), shear=(-15, 15)), iaa.PerspectiveTransform(scale=.08, keep_size=True), ]), iaa.PiecewiseAffine(scale=(0.02, 0.04)), iaa.CoarseDropout(p=(.1, .15), size_percent=(0.07, 0.1)) ], random_order=True) # augs = iaa.OneOf( # [ # iaa.Affine(scale={"x": (0.8, 1.), "y": (0.8, 1.)}, rotate=(-15, 15), shear=(-15, 15)), # iaa.PerspectiveTransform(scale=.08, keep_size=True), # iaa.PiecewiseAffine(scale=(0.02, 0.04)), # iaa.CoarseDropout(p=(.1, .2), size_percent=(0.05, 0.1))
################### def keypoint_func(keypoints_on_images, random_state, parents, hooks): return keypoints_on_images ### Common Usage Setting ### sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5, name="FlipLR"), iaa.Flipud(0.5, name="FlipUD"), iaa.Multiply((0.8, 1.2), name="Multiply"), iaa.ContrastNormalization((0.8, 1.1), name="Contrast"), iaa.AddToHueAndSaturation((-30, 30), name="Hue"), iaa.OneOf([ iaa.AdditiveGaussianNoise(scale=0.02 * 255, name="Noise"), iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.025), name='Cdrop') ]), iaa.Affine(scale=(0.75, 1.25), cval=0, mode='constant'), # wrap iaa.OneOf([ iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270) ]), sometimes(iaa.GaussianBlur((0, 1.5), name="GaussianBlur")), iaa.Lambda(img_func, keypoint_func, name='channel_swap'), sometimes(iaa.PerspectiveTransform(scale=(0.025, 0.100))) ])
def generate_clip_from_image(raw_frame, raw_mask, temporal_window, **kwargs): """ :param raw_frame: The frame to be augmented: h x w x 3 :param raw_mask: h x w x 1 :param temporal_window: Number of frames in the output clip :return: clip_frames - list of frames with values 0-255 clip_masks - corresponding masks """ global TRANSLATION, ROTATION, SHEAR if 'translation' in kwargs: TRANSLATION = kwargs['translation'] if 'rotation' in kwargs: ROTATION = kwargs['rotation'] if 'shear' in kwargs: SHEAR = kwargs['shear'] clip_frames = np.repeat(raw_frame[np.newaxis], temporal_window, axis=0) clip_masks = np.repeat(raw_mask[np.newaxis], temporal_window, axis=0) # 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.05, aug) blur = sometimes( iaa.OneOf([ iaa.GaussianBlur((0.0, 0.5)), # iaa.AverageBlur(k=(2, 7)), # iaa.MedianBlur(k=(3, 11)), ])) seq = iaa.Sequential( [ # iaa.Fliplr(FLIP / 100.), # horizontal flips sometimes( iaa.ElasticTransformation(alpha=(200, 220), sigma=(17.0, 19.0)) ), iaa.Affine( scale={ "x": (0.7, 1.3), "y": (0.7, 1.3) }, translate_percent={ "x": (-TRANSLATION, TRANSLATION), "y": (-TRANSLATION, TRANSLATION) }, rotate=(-ROTATION, ROTATION), shear=(-SHEAR, SHEAR), mode='edge', ) ], random_order=True) frame_aug = raw_frame[np.newaxis] mask_aug = raw_mask[np.newaxis] # create sequence of transformations of the current image for t in range(temporal_window - 1): frame_aug, mask_aug = seq(images=frame_aug.astype(np.uint8), segmentation_maps=mask_aug.astype(np.uint8)) frame_aug = blur(images=frame_aug) clip_frames[t + 1] = frame_aug[0] clip_masks[t + 1] = mask_aug[0] return clip_frames, clip_masks
A Tensor. Has the same type as input. Has the shape of tensor.shape * repeats """ expanded_tensor = tf.expand_dims(tensor, -1) multiples = [1] + repeats tiled_tensor = tf.tile(expanded_tensor, multiples=multiples) repeated_tesnor = tf.reshape(tiled_tensor, tf.shape(tensor) * repeats) return repeated_tesnor # data aug seq = iaa.Sequential( [ iaa.Sometimes( 0.1, iaa.Affine(translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, )), iaa.Sometimes(0.1, iaa.Affine(rotate=(-25, 25), )), iaa.Sometimes(0.1, iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, )), iaa.Fliplr(1.0), # Horizonatl flips ], random_order=True) # apply augmenters in random order # class class CNN(): def __init__(self, k, inc, out):
# load the validation dataset valDataset = LesionBoundaryDataset(IMAGE_PATHS, CLASS_NAMES) valDataset.load_lesions(valIdxs) valDataset.prepare() # initialize the training configuration config = LesionBoundaryConfig() config.display() # initialize the image augmentation process aug = iaa.SomeOf( (0, 2), [iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(rotate=(-10, 10))]) # initialize the model and load the COCO weights so we can # perform fine-tuning model = modellib.MaskRCNN(mode="training", config=config, model_dir=LOGS_AND_MODEL_DIR) model.load_weights(COCO_PATH, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) # train *just* the layer heads model.train(trainDataset,
train = carlaDataset() train.load_images(dir=RGB_TRAIN_DIR, type='train') # mask, a = train.load_mask(50) # print(a) train.prepare() val = carlaDataset() val.load_images(RGB_VAL_DIR, type='valid') val.prepare() augmentation = aug.SomeOf((0, None), [ aug.Fliplr(0.5), aug.Flipud(0.5), aug.OneOf([aug.Affine(rotate=90), aug.Affine(rotate=180), aug.Affine(rotate=270)]), aug.Multiply((0.8, 1.5)), aug.GaussianBlur(sigma=(0.0, 5.0)), aug.Affine(scale=(0.5, 1.5)), aug.Affine(scale={"x": (0.5, 1.5), "y": (0.5, 1.5)}), ]) config = CarlaConfig() config.STEPS_PER_EPOCH = NUMBER_OF_TRAIN_DATA//config.BATCH_SIZE config.VALIDATION_STEPS = NUMBER_OF_VAL_DATA//config.BATCH_SIZE config.display() model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR)
def imgaug_img(img): # 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.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 ) images_aug = seq.augment_images([img]) return images_aug[0]
help="Path to weights .h5 file") parser.add_argument('--logs', required=False, default=DEFAULT_LOGS_DIR, metavar="/path/to/logs/", help='Logs and checkpoints directory (default=logs/)') parser.add_argument('--limit', required=False, default=500, metavar="<image count>", help='Images to use for evaluation (default=500)') parser.add_argument('--augment', required=False, action='store_true', help='add augmentations') parser.add_argument('--stage', required=False, default=1, type=int, help='Choose stage of training (1-heads, 2-4+, 3-full)') parser.add_argument('--num_gpus', default=1, type=int, help='Number of GPUs available') args = parser.parse_args() if args.augment: aug = iaa.OneOf([iaa.CropAndPad(percent=(-0.5, 0.5)), iaa.Fliplr(0.5), iaa.Affine(translate_percent={"x": (-0.4, 0.4), "y": (-0.4, 0.4)})]) else: aug = None ############################################################ # Configurations ############################################################ with open(args.json_file, 'r') as f: obj = json.load(f) # Configurations if args.command == "train": config = BagsConfig(len(obj['classes']), args.num_gpus) else: class InferenceConfig(): # Set batch size to 1 since we'll be running inference on
def zoom(image): zoom = iaa.Affine(scale=(1, 1.3)) image = zoom.augment_image(image) return image
def load_aug(): import imgaug as ia from imgaug import augmenters as iaa sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq[0] = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), sometimes( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) cval=( 0, 255 ), # if mode is constant, use a cval between 0 and 255 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf( (0, 5), [ sometimes( iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)) ), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur( (0, 3.0 )), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(2, 7) ), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur( k=(3, 11) ), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout( (0.01, 0.1), per_channel=0.5 ), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add( (-10, 10), per_channel=0.5 ), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation( (-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.contrast.LinearContrast((0.5, 2.0))) ]), iaa.contrast.LinearContrast( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)), sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # move pixels locally around (with random strengths) sometimes(iaa.PiecewiseAffine(scale=( 0.01, 0.05))), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True)
val_fns = [f for f in val_fns if val_fns not in exclude_list] logger.info('Prepare train dataset') dataset_train = DetectorDataset( trn_fns, df_trn, TRAIN_PATH, ORIG_SZ, ORIG_SZ) dataset_train.prepare() logger.info('Prepare validation dataset') dataset_val = DetectorDataset( val_fns, df_val, TRAIN_PATH, ORIG_SZ, ORIG_SZ) dataset_val.prepare() # Image augmentation (light but constant) augmentation = iaa.Sequential([ iaa.OneOf([ # rotate iaa.Affine(rotate=0), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270), ]), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.OneOf([ # brightness or contrast iaa.Multiply((0.9, 1.1)), iaa.ContrastNormalization((0.9, 1.1)), ]), iaa.OneOf([ # blur or sharpen iaa.GaussianBlur(sigma=(0.0, 0.1)), iaa.Sharpen(alpha=(0.0, 0.1)), ]), ])
def py_decode_mask(counts, mask_h, mask_w): mask = map( lambda x: maskUtils.decode({ 'size': [mask_h, mask_w], 'counts': x }), counts) mask = list(mask) mask = np.array(mask) mask = mask.astype(np.uint8) return mask seq = iaa.Sequential([ iaa.Affine( rotate=[-45, 45], cval=127, ) # translate by 40/60px on x/y axis, and scale to 50-70%, affects BBs ]) def rotate(img, bboxes, masks): o_img = img o_bboxes = bboxes o_masks = masks seq_det = seq.to_deterministic() bbox = [ ia.BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2, label=l) for x1, y1, x2, y2, l in list(bboxes[:, :5]) ] bbs = ia.BoundingBoxesOnImage(bbox, shape=img.shape)
def Augmentation(input_image, label): 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_label = [label] * nb_batches 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_label, aug_faces)
print('shape:', xx_train.shape, 'val shape:', xx_val.shape) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ iaa.SomeOf( (0, 3), [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=['reflect'])), sometimes( iaa.OneOf([ iaa.Affine(rotate=0), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270) ])), sometimes( iaa.Affine( scale={ "x": (0.1, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }, rotate=(-45,
def draw_per_augmenter_images(): print("[draw_per_augmenter_images] Loading image...") #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) image = ia.quokka_square(size=(128, 128)) keypoints = [ ia.Keypoint(x=34, y=15), ia.Keypoint(x=85, y=13), ia.Keypoint(x=63, y=73) ] # left ear, right ear, mouth keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)] print("[draw_per_augmenter_images] Initializing...") rows_augmenters = [ (0, "Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]), (0, "Crop\n(top, right,\nbottom, left)", [ (str(vals), iaa.Crop(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)] ]), (0, "Pad\n(top, right,\nbottom, left)", [ (str(vals), iaa.Pad(px=vals)) for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16, 4), (8, 0, 0, 32), (32, 64, 0, 0)] ]), (0, "Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]), (0, "Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]), (0, "Superpixels\np_replace=1", [("n_segments=%d" % (n_segments, ), iaa.Superpixels(p_replace=1.0, n_segments=n_segments)) for n_segments in [25, 50, 75, 100, 125]]), (0, "Superpixels\nn_segments=100", [("p_replace=%.2f" % (p_replace, ), iaa.Superpixels(p_replace=p_replace, n_segments=100)) for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]), (0, "Invert", [("p=%d" % (p, ), iaa.Invert(p=p)) for p in [0, 0, 1, 1, 1]]), (0, "Invert\n(per_channel)", [("p=%.2f" % (p, ), iaa.Invert(p=p, per_channel=True)) for p in [0.5, 0.5, 0.5, 0.5, 0.5]]), (0, "Add", [("value=%d" % (val, ), iaa.Add(val)) for val in [-45, -25, 0, 25, 45]]), (0, "Add\n(per channel)", [ ("value=(%d, %d)" % ( vals[0], vals[1], ), iaa.Add(vals, per_channel=True)) for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)] ]), (0, "AddToHueAndSaturation", [("value=%d" % (val, ), iaa.AddToHueAndSaturation(val)) for val in [-45, -25, 0, 25, 45]]), (0, "Multiply", [("value=%.2f" % (val, ), iaa.Multiply(val)) for val in [0.25, 0.5, 1.0, 1.25, 1.5]]), (1, "Multiply\n(per channel)", [("value=(%.2f, %.2f)" % ( vals[0], vals[1], ), iaa.Multiply(vals, per_channel=True)) for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]), (0, "GaussianBlur", [("sigma=%.2f" % (sigma, ), iaa.GaussianBlur(sigma=sigma)) for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]), (0, "AverageBlur", [("k=%d" % (k, ), iaa.AverageBlur(k=k)) for k in [1, 3, 5, 7, 9]]), (0, "MedianBlur", [("k=%d" % (k, ), iaa.MedianBlur(k=k)) for k in [1, 3, 5, 7, 9]]), (0, "BilateralBlur\nsigma_color=250,\nsigma_space=250", [("d=%d" % (d, ), iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250)) for d in [1, 3, 5, 7, 9]]), (0, "Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness, ), iaa.Sharpen(alpha=1, lightness=lightness)) for lightness in [0, 0.5, 1.0, 1.5, 2.0]]), (0, "Emboss\n(alpha=1)", [("strength=%.2f" % (strength, ), iaa.Emboss(alpha=1, strength=strength)) for strength in [0, 0.5, 1.0, 1.5, 2.0]]), (0, "EdgeDetect", [("alpha=%.2f" % (alpha, ), iaa.EdgeDetect(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), (0, "DirectedEdgeDetect\n(alpha=1)", [("direction=%.2f" % (direction, ), iaa.DirectedEdgeDetect(alpha=1, direction=direction)) for direction in [ 0.0, 1 * (360 / 5) / 360, 2 * (360 / 5) / 360, 3 * (360 / 5) / 360, 4 * (360 / 5) / 360 ]]), (0, "AdditiveGaussianNoise", [("scale=%.2f*255" % (scale, ), iaa.AdditiveGaussianNoise(scale=scale * 255)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]), (0, "AdditiveGaussianNoise\n(per channel)", [("scale=%.2f*255" % (scale, ), iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True)) for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]), (0, "Dropout", [("p=%.2f" % (p, ), iaa.Dropout(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), (0, "Dropout\n(per channel)", [("p=%.2f" % (p, ), iaa.Dropout(p=p, per_channel=True)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), (3, "CoarseDropout\n(p=0.2)", [("size_percent=%.2f" % (size_percent, ), iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), (0, "CoarseDropout\n(p=0.2, per channel)", [("size_percent=%.2f" % (size_percent, ), iaa.CoarseDropout(p=0.2, size_percent=size_percent, per_channel=True, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), (0, "SaltAndPepper", [("p=%.2f" % (p, ), iaa.SaltAndPepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), (0, "Salt", [("p=%.2f" % (p, ), iaa.Salt(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), (0, "Pepper", [("p=%.2f" % (p, ), iaa.Pepper(p=p)) for p in [0.025, 0.05, 0.1, 0.2, 0.4]]), (0, "CoarseSaltAndPepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent, ), iaa.CoarseSaltAndPepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), (0, "CoarseSalt\n(p=0.2)", [("size_percent=%.2f" % (size_percent, ), iaa.CoarseSalt(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), (0, "CoarsePepper\n(p=0.2)", [("size_percent=%.2f" % (size_percent, ), iaa.CoarsePepper(p=0.2, size_percent=size_percent, min_size=2)) for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]), (0, "ContrastNormalization", [("alpha=%.1f" % (alpha, ), iaa.ContrastNormalization(alpha=alpha)) for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]), (0, "ContrastNormalization\n(per channel)", [("alpha=(%.2f, %.2f)" % ( alphas[0], alphas[1], ), iaa.ContrastNormalization(alpha=alphas, per_channel=True)) for alphas in [(0.4, 0.6), (0.65, 0.85), (0.9, 1.1), (1.15, 1.35), (1.4, 1.6)]]), (0, "Grayscale", [("alpha=%.1f" % (alpha, ), iaa.Grayscale(alpha=alpha)) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), (6, "PerspectiveTransform", [("scale=%.3f" % (scale, ), iaa.PerspectiveTransform(scale=scale)) for scale in [0.025, 0.05, 0.075, 0.10, 0.125]]), (0, "PiecewiseAffine", [("scale=%.3f" % (scale, ), iaa.PiecewiseAffine(scale=scale)) for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]), (0, "Affine: Scale", [("%.1fx" % (scale, ), iaa.Affine(scale=scale)) for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]), (0, "Affine: Translate", [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={ "x": x, "y": y })) for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]), (0, "Affine: Rotate", [("%d deg" % (rotate, ), iaa.Affine(rotate=rotate)) for rotate in [-90, -45, 0, 45, 90]]), (0, "Affine: Shear", [("%d deg" % (shear, ), iaa.Affine(shear=shear)) for shear in [-45, -25, 0, 25, 45]]), (0, "Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode)) for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]), (0, "Affine: cval", [("%d" % (int(cval * 255), ), iaa.Affine(translate_px=-32, cval=int(cval * 255), mode="constant")) for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]), (2, "Affine: all", [("", iaa.Affine(scale={ "x": (0.5, 1.5), "y": (0.5, 1.5) }, translate_px={ "x": (-32, 32), "y": (-32, 32) }, rotate=(-45, 45), shear=(-32, 32), mode=ia.ALL, cval=(0.0, 1.0))) for _ in sm.xrange(5)]), (1, "ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha, ), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]]), (0, "Alpha\nwith EdgeDetect(1.0)", [("factor=%.1f" % (factor, ), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0))) for factor in [0.0, 0.25, 0.5, 0.75, 1.0]]), (4, "Alpha\nwith EdgeDetect(1.0)\n(per channel)", [ ("factor=(%.2f, %.2f)" % (factor[0], factor[1]), iaa.Alpha(factor=factor, first=iaa.EdgeDetect(1.0), per_channel=0.5)) for factor in [(0.0, 0.2), (0.15, 0.35), (0.4, 0.6), (0.65, 0.85), (0.8, 1.0)] ]), (15, "SimplexNoiseAlpha\nwith EdgeDetect(1.0)", [("", iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0))) for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]), (9, "FrequencyNoiseAlpha\nwith EdgeDetect(1.0)", [("exponent=%.1f" % (exponent, ), iaa.FrequencyNoiseAlpha(exponent=exponent, first=iaa.EdgeDetect(1.0), size_px_max=16, upscale_method="linear", sigmoid=False)) for exponent in [-4, -2, 0, 2, 4]]) ] print("[draw_per_augmenter_images] Augmenting...") rows = [] for (row_seed, row_name, augmenters) in rows_augmenters: ia.seed(row_seed) #for img_title, augmenter in augmenters: # #aug.reseed(1000) # pass row_images = [] row_keypoints = [] row_titles = [] for img_title, augmenter in augmenters: aug_det = augmenter.to_deterministic() row_images.append(aug_det.augment_image(image)) row_keypoints.append(aug_det.augment_keypoints(keypoints)[0]) row_titles.append(img_title) rows.append((row_name, row_images, row_keypoints, row_titles)) # matplotlib drawin routine """ print("[draw_per_augmenter_images] Plotting...") width = 8 height = int(1.5 * len(rows_augmenters)) fig = plt.figure(figsize=(width, height)) grid_rows = len(rows) grid_cols = 1 + 5 gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1]) axes = [] for i in sm.xrange(grid_rows): axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)]) fig.tight_layout() #fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22) #fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02) fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02) for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows): axes_row = axes[row_idx] for col_idx in sm.xrange(grid_cols): ax = axes_row[col_idx] ax.cla() ax.axis("off") ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if col_idx == 0: ax.text(0, 0.5, row_name, color="black") else: cell_image = row_images[col_idx-1] cell_keypoints = row_keypoints[col_idx-1] cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5) ax.imshow(cell_image_kp) x = 0 y = 145 #ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6) ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7) fig.savefig("examples.jpg", bbox_inches="tight") #plt.show() """ # simpler and faster drawing routine """ output_image = ExamplesImage(128, 128, 128+64, 32) for (row_name, row_images, row_keypoints, row_titles) in rows: row_images_kps = [] for image, keypoints in zip(row_images, row_keypoints): row_images_kps.append(keypoints.draw_on_image(image, size=5)) output_image.add_row(row_name, row_images_kps, row_titles) misc.imsave("examples.jpg", output_image.draw()) """ # routine to draw many single files seen = defaultdict(lambda: 0) markups = [] for (row_name, row_images, row_keypoints, row_titles) in rows: output_image = ExamplesImage(128, 128, 128 + 64, 32) row_images_kps = [] for image, keypoints in zip(row_images, row_keypoints): row_images_kps.append(keypoints.draw_on_image(image, size=5)) output_image.add_row(row_name, row_images_kps, row_titles) if "\n" in row_name: row_name_clean = row_name[0:row_name.find("\n") + 1] else: row_name_clean = row_name row_name_clean = re.sub(r"[^a-z0-9]+", "_", row_name_clean.lower()) row_name_clean = row_name_clean.strip("_") if seen[row_name_clean] > 0: row_name_clean = "%s_%d" % (row_name_clean, seen[row_name_clean] + 1) fp = os.path.join(IMAGES_DIR, "examples_%s.jpg" % (row_name_clean, )) #misc.imsave(fp, output_image.draw()) save(fp, output_image.draw()) seen[row_name_clean] += 1 markup_descr = row_name.replace('"', '') \ .replace("\n", " ") \ .replace("(", "") \ .replace(")", "") markup = '![%s](%s?raw=true "%s")' % (markup_descr, fp, markup_descr) markups.append(markup) for markup in markups: print(markup)
# For the other 50% of all images, we sample the noise per pixel AND # channel. This can change the color (not only brightness) of the # pixels. iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # Make some images brighter and some darker. # In 20% of all cases, we sample the multiplier once per channel, # which can end up changing the color of the images. iaa.Multiply((0.8, 1.2), per_channel=0.2), # Apply affine transformations to each image. # Scale/zoom them, translate/move them, rotate them and shear them. 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=(-25, 25), shear=(-8, 8)), iaa.Multiply((0.8, 1.2), per_channel=0.2), #iaa.Sometimes(0.5, iaa.Lambda(func_images=func_images, func_keypoints=func_keypoints)), #iaa.Sometimes(0.5, iaa.AverageBlur(k=10, name=None, deterministic=False, random_state=True)), # iaa.Sometimes(0.5, iaa.PiecewiseAffine(scale=0, # nb_rows=4, # nb_cols=4, # order=1, # cval=0,