def draw_single_sequential_images(): ia.seed(44) #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) image = ia.quokka_square(size=(128, 128)) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # 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.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)), # 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 ) grid = seq.draw_grid(image, cols=8, rows=8) misc.imsave("examples_grid.jpg", grid)
def __init__(self, images, config, shuffle=True, jitter=True, norm=None): self.generator = None 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(int(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 train_transformation(image, size): #convert image to grayscale and find croping position np_img = np.asarray(image) grayImg = cv2.cvtColor(np_img, cv2.COLOR_BGR2GRAY) h, w = grayImg.shape medianImg = cv2.medianBlur(grayImg, 11) threshold = medianImg.max() / 8 mask = np.zeros((h, w), dtype=np.uint8) mask = (medianImg > threshold) * 1.0 one_mask = np.argwhere(mask == 1) max_pos = np.max(one_mask, axis=0) min_pos = np.min(one_mask, axis=0) top, bottom, left, right = min_pos[0], max_pos[0], min_pos[1], max_pos[1] np_img = np_img[top:bottom, left:right, :] # Transform image sometimes = lambda aug: iaa.Sometimes(0.5, aug) train_seq = iaa.Sequential([ iaa.Resize({ "height": size, "width": size }), sometimes(iaa.Fliplr(1)), sometimes(iaa.Flipud(1)), sometimes( iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode=["constant", "edge"], pad_cval=0)), sometimes( iaa.Affine(scale=(0.8, 1.2), translate_percent=(-0.2, 0.2), rotate=(-45, 45), order=[0, 1], cval=0, mode=["constant", "edge"])), iaa.SomeOf((0, 4), [ iaa.OneOf([ iaa.WithChannels(0, iaa.Add((5, 50))), iaa.WithChannels(1, iaa.Add((5, 20))), iaa.WithChannels(2, iaa.Add((5, 20))), ]), iaa.OneOf([ iaa.GaussianBlur(sigma=(0.0, 2.0)), iaa.MedianBlur(k=(3, 5)), iaa.AverageBlur(k=(2, 4)) ]), iaa.contrast.LinearContrast((0.5, 1.5)), iaa.Sharpen(alpha=(0.0, 0.5), lightness=1.0), iaa.Multiply((0.5, 1.5)), ], random_order=True) ], random_order=False) np_img = train_seq.augment_images(np_img.reshape(1, *np_img.shape)) # Convert to Pillow image im = Image.fromarray(np_img[0]) # Convert to Tensor convert_to_tensor = [transforms.ToTensor()] # if normalize: # convert_to_tensor.append(transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)) tensor_tranform = transforms.Compose(convert_to_tensor) return tensor_tranform(im)
def sometimes(aug): return iaa.Sometimes(0.5, aug)
def oc(aug): """Defines the "occasionally" probability value.""" return iaa.Sometimes(0.3, aug)
def __init__(self): configMain.__init__(self) st = lambda aug: iaa.Sometimes(0.4, aug) oc = lambda aug: iaa.Sometimes(0.3, aug) rl = lambda aug: iaa.Sometimes(0.09, aug) self.augment = iaa.Sequential( [ rl(iaa.GaussianBlur( (0, 1.5))), # blur images with a sigma between 0 and 1.5 rl( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05), per_channel=0.5)), # add gaussian noise to images oc(iaa.Dropout((0.0, 0.10), per_channel=0.5) ), # randomly remove up to X% of the pixels oc( iaa.CoarseDropout( (0.0, 0.10), size_percent=(0.08, 0.2), per_channel=0.5) ), # randomly remove up to X% of the pixels oc( iaa.Add((-40, 40), per_channel=0.5) ), # change brightness of images (by -X to Y of original value) st(iaa.Multiply((0.10, 2.5), per_channel=0.2) ), # change brightness of images (X-Y% of original value) rl(iaa.ContrastNormalization( (0.5, 1.5), per_channel=0.5)), # improve or worsen the contrast rl(iaa.Grayscale((0.0, 1))), # put grayscale ], random_order=True # do all of the above in random order ) self.augment_labels = True self.augment_amount = 3 #3=max, 2=mid, 1=min self.labels_to_augment = { "road": True, "buildings": True, "grass": True, "sky_n_zebra": True } # there are files with data, 200 images each, and here we select which ones to use #5self.dataset_name = 'Carla' #with open(os.path.join(self.save_data_stats, 'path'),'r') as f: # path = f.read().strip() path = '../VirtualElektraData2_Double' train_path = os.path.join(path, 'SeqTrain') val_path = os.path.join(path, 'SeqVal') print train_path, val_path self.train_db_path = [ os.path.join(train_path, f) for f in glob.glob1(train_path, "data_*.h5") ] self.val_db_path = [ os.path.join(val_path, f) for f in glob.glob1(val_path, "data_*.h5") ] # When using data with noise, remove the recording during the first half of the noise impulse # TODO Felipe: change to noise percentage. self.remove_noise = False # Speed Divide Factor #TODO: FOr now is hardcooded, but eventually we should be able to calculate this from data at the loading time. self.speed_factor = 1.0 # In KM/H FOR GTA it should be maximun 30.0 # The division is made by three diferent data kinds # in every mini-batch there will be equal number of samples with labels from each group # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2 self.labels_per_division = [[2], [2], [2]] self.dataset_names = ['targets'] self.queue_capacity = 20 * self.batch_size # TODO NOT IMPLEMENTED Felipe: True/False switches to turn data balancing on or off self.balances_val = True self.balances_train = True self.augment_and_saturate_factor = True
def example_augs(add_freq=0.1, add_value=(-10, 10), add_pc_freq=0.5, multiply_freq=0.1, multiply_value=(0.75, 1.25), multiply_pc_freq=0.5, snp_freq=0.1, snp_p=0.05, jpeg_freq=0.1, jpeg_compression=(1, 5), gaussian_freq=0.1, gaussian_sigma=(0.01, 0.7), motion_freq=0.1, motion_k=(3, 10), contrast_freq=0.1, contrast_alpha=(0.5, 1.5), fliplr=0.5, flipud=0.5, affine_freq=0.1, affine_scale=(0, 0.02), transform_freq=0.1, transform_scale=(0, 0.05), elastic_freq=0.1, elastic_sigma=(4, 6), elastic_alpha=(0, 7), rotate=1, dataset="/scratch/jw22g14/FK2018/second_set/"): augmentation = iaa.Sequential([ iaa.Sometimes(add_freq, iaa.Add(value=add_value, per_channel=add_pc_freq)), iaa.Sometimes( multiply_freq, iaa.Multiply(mul=multiply_value, per_channel=multiply_pc_freq)), iaa.Sometimes(snp_freq, iaa.SaltAndPepper(snp_p)), iaa.Sometimes(jpeg_freq, iaa.JpegCompression(compression=jpeg_compression)), iaa.Sometimes(gaussian_freq, iaa.GaussianBlur(sigma=gaussian_sigma)), iaa.Sometimes(motion_freq, iaa.MotionBlur(k=motion_k)), iaa.Sometimes(contrast_freq, iaa.LinearContrast(alpha=contrast_alpha)), iaa.Fliplr(fliplr), iaa.Flipud(flipud), iaa.Sometimes( affine_freq, iaa.PiecewiseAffine(scale=affine_scale, nb_rows=8, nb_cols=8)), iaa.Sometimes( transform_freq, iaa.PerspectiveTransform(scale=transform_scale, keep_size=True)), iaa.Sometimes( elastic_freq, iaa.ElasticTransformation(sigma=elastic_sigma, alpha=elastic_alpha)), iaa.Sometimes(rotate, iaa.Rot90([0, 1, 2, 3])) ], random_order=True) images = [] image_names = [] for filename in os.listdir(dataset): if (filename[-4:] == '.png'): image = cv2.imread(dataset + filename) images.append(image) image_names.append(filename) print("running augmentation") images_aug = augmentation(images=images) print("augmented!") for i, image in enumerate(images_aug): cv2.imwrite("./augmented/" + image_names[i], image)
def data_augmentation(self, image, label): # crop_size = random.randint(int(0.8 * self.sample_height), # int(1.2 * self.sample_height)) # # start_h = random.randint(0, image.shape[0] - int(1.42 * crop_size) - 2) # start_w = random.randint(0, image.shape[1] - int(1.42 * crop_size) - 2) # # image_crop = image[start_h:start_h + # int(1.42 * crop_size), start_w:start_w + # int(1.42 * crop_size)].copy() # label_crop = label[start_h:start_h + # int(1.42 * crop_size), start_w:start_w + # int(1.42 * crop_size)].copy() image_crop = image.copy() label_crop = label.copy() # del image # del label # gc.collect() seq = iaa.Sequential([ iaa.Affine( shear=(-4, 4), rotate=(0, 360)), # rotate by -45 to 45 degrees (affects segmaps) ]) segmap = ia.SegmentationMapOnImage(label_crop, shape=label_crop.shape, nb_classes=self.num_classes) seq_det = seq.to_deterministic() image_rotation = seq_det.augment_image(image_crop) segmap_aug = seq_det.augment_segmentation_maps(segmap) label_rotation = segmap_aug.get_arr_int() reduction_pixels = int(0.15 * label_rotation.shape[0]) start_i = reduction_pixels stop_i = label_crop.shape[0] - reduction_pixels image_crop = image_rotation[start_i:stop_i, start_i:stop_i, :] label_crop = label_rotation[start_i:stop_i, start_i:stop_i] seq = iaa.Sequential([ iaa.Resize( { "height": self.sample_height, "width": self.sample_width }, interpolation='nearest'), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Sometimes(0.8, iaa.HistogramEqualization()), iaa.Sometimes( 0.8, iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25))), iaa.AddToHueAndSaturation((-20, 20), per_channel=True), ]) segmap = ia.SegmentationMapOnImage(label_crop, shape=label_crop.shape, nb_classes=self.num_classes) seq_det = seq.to_deterministic() image_aug = seq_det.augment_image(image_crop) segmap_aug = seq_det.augment_segmentation_maps(segmap) label_aug = segmap_aug.get_arr_int() return image_aug, label_aug
def __init__(self, path='/'): configMain.__init__(self) st = lambda aug: iaa.Sometimes(0.2, aug) oc = lambda aug: iaa.Sometimes(0.1, aug) rl = lambda aug: iaa.Sometimes(0.05, aug) self.augment = iaa.Sequential( [ rl(iaa.GaussianBlur( (0, 1.3))), # blur images with a sigma between 0 and 1.5 rl( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05), per_channel=0.5)), # add gaussian noise to images rl(iaa.Dropout((0.0, 0.10), per_channel=0.5) ), # randomly remove up to X% of the pixels oc( iaa.Add((-20, 20), per_channel=0.5) ), # change brightness of images (by -X to Y of original value) st(iaa.Multiply((0.25, 2.5), per_channel=0.2) ), # change brightness of images (X-Y% of original value) rl(iaa.ContrastNormalization( (0.5, 1.5), per_channel=0.5)), # improve or worsen the contrast rl(iaa.Grayscale((0.0, 1))), # put grayscale ], random_order=True # do all of the above in random order ) # self.augment = [None] # there are files with data, 200 images each, and here we select which ones to use self.dataset_name = 'Carla' train_path = os.path.join(path, 'RC28_wpz_M_DR') val_path = os.path.join(path, 'RC28_wpz_M_DR') print(train_path) self.train_db_path = [ os.path.join(train_path, f) for f in glob.glob1(train_path, "data_*.h5") ] self.val_db_path = [ os.path.join(val_path, f) for f in glob.glob1(val_path, "data_*.h5") ] # Speed Divide Factor # TODO: FOr now is hardcooded, but eventually we should be able to calculate this from data at the loading time. self.speed_factor = 40.0 # In KM/H FOR GTA it should be maximun 30.0 # The division is made by three diferent data kinds # in every mini-batch there will be equal number of samples with labels from each group # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2 self.labels_per_division = [[0, 2, 5], [3], [4]] self.dataset_names = ['targets'] self.queue_capacity = 20 * self.batch_size
import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage from xmlParser import Parser import glob import numpy as np import tensorflow as tf from matplotlib import pyplot as plt path= 'Image A*/train/*.xml' import cv2 seq = iaa.Sequential([ iaa.Fliplr(p=0),# basically this is original one iaa.Sometimes(0.05,(iaa.Crop(px=(22, 45),keep_size=True))), # crop images from each side by 0 to 16px (randomly chosen) iaa.Sometimes(0.5,(iaa.Fliplr(1))), # horizontally flip 50% of the images iaa.Sometimes(0.02,iaa.GaussianBlur(sigma=(5, 7.0))), # blur images with a sigma of 0 to 3.0 iaa.Sometimes(0.02 ,iaa.ImpulseNoise(p=(0.6,1))), iaa.Sometimes(0.02 ,iaa.EdgeDetect(alpha=(0.09,1))), #iaa.AddToBrightness(add=(100,124)), iaa.Sometimes(0.02 ,iaa.Canny(alpha=(0.8,0.9))), iaa.Sometimes(0.5 ,iaa.Grayscale(alpha=1.00)), iaa.Sometimes(0.5 ,iaa.ChannelShuffle(p=1)), #iaa.Sometimes(0.02 ,(iaa.geometric.Affine( scale=2,rotate=22,order=1))), iaa.Sometimes(0.5 ,iaa.Cartoon(blur_ksize=(11,13))), iaa.Sometimes(0.02 ,iaa.CenterCropToAspectRatio(1)), iaa.Sometimes(0.02 ,iaa.CenterCropToFixedSize(100,100)), iaa.Sometimes(0.12 ,iaa.ChangeColorTemperature(kelvin=(2222,3333))), #iaa.segmentation(), iaa.Sometimes(0.12 ,iaa.CLAHE(clip_limit=(4,8))),
from imgaug import augmenters as iaa import numpy as np import matplotlib.pyplot as plt import cv2 import pdb import os pathC = "../CSFID/tracks_cropped/*.jpg" pathF = "../FID-300/tracks_cropped/*.jpg" outputPath = os.makedirs('../output_files') fps = glob.glob(pathC) fps.sort() images = [cv2.imread(fp) for fp in fps] sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # Small gaussian blur with random sigma between 0 and 0.5. # But we only blur about 50% of all images. iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))), 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(
def __init__(self, directory, mode='train', clip_len=8, frame_sample_rate=1): folder = Path( directory) / mode # get the directory of the specified split self.clip_len = clip_len self.short_side = [128 * 2, 160 * 2] self.crop_size = 112 * 2 self.frame_sample_rate = frame_sample_rate self.mode = mode self.fnames, labels = [], [] for label in sorted(os.listdir(folder)): for fname in os.listdir(os.path.join(folder, label)): self.fnames.append(os.path.join(folder, label, fname)) labels.append(label) # prepare a mapping between the label names (strings) and indices (ints) self.label2index = { label: index for index, label in enumerate(sorted(set(labels))) } # convert the list of label names into an array of label indices self.label_array = np.array( [self.label2index[label] for label in labels], dtype=np.int64) label_file = str(len(os.listdir(folder))) + 'class_labels.txt' with open(label_file, 'w') as f: for id, label in enumerate(sorted(self.label2index)): f.writelines(str(id + 1) + ' ' + label + '\n') # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image. sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. self.seq = iaa.Sequential( [ # apply the following augmenters to most images # iaa.Fliplr(0.5), # horizontally flip 50% of all images # iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width # sometimes(iaa.CropAndPad( # percent=(-0.05, 0.1), # pad_mode=ia.ALL, # pad_cval=(0, 255) # )), # sometimes(iaa.Affine( # scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # # scale images to 80-120% of their size, individually per axis # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # # translate by -20 to +20 percent (per axis) # rotate=(-45, 45), # rotate by -45 to +45 degrees # shear=(-16, 16), # shear by -16 to +16 degrees # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) # cval=(0, 255), # if mode is constant, use a cval between 0 and 255 # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) # )), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf( (0, 5), [ # sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # # convert images into their superpixel representation # iaa.OneOf([ # iaa.GaussianBlur((0, 1.5)), # blur images with a sigma between 0 and 3.0 # iaa.AverageBlur(k=(2, 5)), # # 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, 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)), # 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)
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.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
def example_heavy_augmentations(): print("Example: Heavy Augmentations") import imgaug as ia from imgaug import augmenters as iaa # random example images images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8) # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image. st = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. seq = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.5), # vertically flip 50% of all images st(iaa.Crop( percent=(0, 0.1))), # crop images by 0-10% of their height/width st(iaa.GaussianBlur( (0, 3.0))), # blur images with a sigma between 0 and 3.0 st( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)), # add gaussian noise to images st(iaa.Dropout( (0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels st(iaa.Add( (-10, 10), per_channel=0.5 )), # change brightness of images (by -10 to 10 of original value) st(iaa.Multiply((0.5, 1.5), per_channel=0.5) ), # change brightness of images (50-150% of original value) st(iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast st(iaa.Grayscale((0.0, 1.0))), # blend with grayscale image st( iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, # scale images to 80-120% of their size, individually per axis translate_px={ "x": (-16, 16), "y": (-16, 16) }, # translate by -16 to +16 pixels (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[ 0, 1 ], # use scikit-image's interpolation orders 0 (nearest neighbour) and 1 (bilinear) cval=( 0, 255 ), # if mode is constant, use a cval between 0 and 1.0 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ) # apply elastic transformations with random strengths ], random_order=True # do all of the above in random order ) images_aug = seq.augment_images(images) # ----- # Make sure that the example really does something assert not np.array_equal(images, images_aug)
def __getitem__(self, idx): patient_id = self.patient_ids[idx] img = self.get_image(patient_id) if self.crop_source != 1024: img_source_w = self.crop_source img_source_h = self.crop_source else: img_source_h, img_source_w = img.shape[:2] img_h, img_w = img.shape[:2] # set augmentation levels augmentation_sigma = { 10: dict(scale=0.1, angle=5.0, shear=2.5, gamma=0.2, hflip=False), 11: dict(scale=0.1, angle=0.0, shear=2.5, gamma=0.2, hflip=False), 15: dict(scale=0.15, angle=6.0, shear=4.0, gamma=0.2, hflip=np.random.choice([True, False])), 20: dict(scale=0.15, angle=6.0, shear=4.0, gamma=0.25, hflip=np.random.choice([True, False])), 21: dict(scale=0.15, angle=0.0, shear=4.0, gamma=0.25, hflip=np.random.choice([True, False])), }[self.augmentation_level] # training mode augments if self.is_training: cfg = TransformCfg( crop_size=self.img_size, src_center_x=img_w / 2 + np.random.uniform(-32, 32), src_center_y=img_h / 2 + np.random.uniform(-32, 32), scale_x=self.img_size / img_source_w * (2**np.random.normal(0, augmentation_sigma["scale"])), scale_y=self.img_size / img_source_h * (2**np.random.normal(0, augmentation_sigma["scale"])), angle=np.random.normal(0, augmentation_sigma["angle"]), shear=np.random.normal(0, augmentation_sigma["shear"]), hflip=augmentation_sigma["hflip"], vflip=False, ) # validation mode augments else: cfg = TransformCfg( crop_size=self.img_size, src_center_x=img_w / 2, src_center_y=img_h / 2, scale_x=self.img_size / img_source_w, scale_y=self.img_size / img_source_h, angle=0, shear=0, hflip=False, vflip=False, ) # add more augs in training modes crop = cfg.transform_image(img) if self.is_training: crop = np.power( crop, 2.0**np.random.normal(0, augmentation_sigma["gamma"])) if self.augmentation_level == 20 or self.augmentation_level == 21: aug = iaa.Sequential([ iaa.Sometimes( 0.1, iaa.CoarseSaltAndPepper(p=(0.01, 0.01), size_percent=(0.1, 0.2))), iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))), iaa.Sometimes( 0.5, iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255))), ]) crop = (aug.augment_image( np.clip( np.stack([crop, crop, crop], axis=2) * 255, 0, 255).astype(np.uint8))[:, :, 0].astype(np.float32) / 255.0) if self.augmentation_level == 15: aug = iaa.Sequential([ iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0.0, 1.0))), iaa.Sometimes( 0.25, iaa.AdditiveGaussianNoise(scale=(0, 0.02 * 255))) ]) crop = (aug.augment_image( np.clip( np.stack([crop, crop, crop], axis=2) * 255, 0, 255).astype(np.uint8))[:, :, 0].astype(np.float32) / 255.0) # add annotation points annotations = [] for annotation in self.annotations[patient_id]: points = cfg.transform().inverse(annotation) res = np.zeros((1, 5)) p0 = np.min(points, axis=0) p1 = np.max(points, axis=0) res[0, 0:2] = p0 res[0, 2:4] = p1 res[0, 4] = 0 annotations.append(res) if len(annotations): annotations = np.row_stack(annotations) else: annotations = np.zeros((0, 5)) # print('patient_id', patient_id) sample = { "img": crop, "annot": annotations, "scale": 1.0, "category": self.patient_categories[patient_id] } return sample
np.savetxt(stream, yolo_array, fmt=fmt) #save images. img = os.path.splitext( img )[0] + "a" + ".jpg" #saves the transformed image with "a" notation at the end. file_path = os.path.join(img_save_dir, img) sk_io.imsave(file_path, image_aug) # save the transformed image. #ia.imshow(image_aug) count += 1 print("{} annotations and images have been transformed!!".format( count)) sometimes = lambda aug: iaa.Sometimes(0.9, aug) seq = iaa.SomeOf( (1, 2), [ sometimes( iaa.BlendAlphaFrequencyNoise(foreground=iaa.EdgeDetect(0.75), upscale_method="nearest")), sometimes( iaa.BlendAlphaMask( iaa.InvertMaskGen(0.5, iaa.VerticalLinearGradientMaskGen()), iaa.Sequential([iaa.Clouds(), iaa.WithChannels([1, 2])]))), sometimes( iaa.BlendAlphaCheckerboard( nb_rows=2, nb_cols=(1, 4), foreground=iaa.AddToHue(
def augmentation(x_train, y_train, flip='horizontal', enlarge=True, droplet=False): ia.seed(88) # global random seed # flip and affine augmentations must perform on x and y at the same time due to semantic segmentation nature, # while other augmentations must only perform on x, otherwise the labels are affected # First, flip all images horizontally if flip == 'horizontal': flip = iaa.Fliplr(p=1.0) x_train_aug = flip.augment_images(images=x_train) y_train_aug = flip.augment_images(images=y_train) elif flip == 'vertical': flip = iaa.Flipud(p=1.0) x_train_aug = flip.augment_images(images=x_train) y_train_aug = flip.augment_images(images=y_train) else: x_train_aug = x_train y_train_aug = y_train assert not enlarge, 'when no flip, enlarge should be False to prevent duplicate data' # Second, apply none affine augmentations only on x seq = iaa.Sequential( [ # 50% gaussian blur with random sigma between 0 and 0.2. iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.2))), # strengthen or weaken contrast by a factor of 0.9 to 1.1 iaa.ContrastNormalization((0.9, 1.1)), # Make some images brighter and some darker, multiply each image with a random value between 0.8 and 1.2 iaa.Multiply((0.8, 1.2)), # Drop 0% to 1% of pixels as square boxed sized from 1x1 to 2x2 #iaa.CoarseDropout((0.0, 0.01), size_percent=(0.0, 0.5)) ], random_order=True) # apply augmenters in random order x_train_aug = seq.augment_images(images=x_train_aug) # Third, apply affine transformations as a new sequence to ensure the same transformation on y as well. if droplet: affine = ia.augmenters.Affine(translate_percent={ 'x': (-0.2, 0.2), 'y': (-0.2, 0.2) }, mode='constant', cval=0) else: affine = ia.augmenters.Affine(scale=(0.95, 1.05), translate_percent={ 'x': (-0.01, 0.01), 'y': (-0.01, 0.01) }, shear=(-0.5, 0.5), rotate=(-0.5, 0.5)) # combine train and mask into one array to ensure the same augmentation combine_train_mask = np.append(x_train_aug, y_train_aug, axis=-1) combine_aug = affine.augment_images(images=combine_train_mask) # extract x_train_aug and y_train_aug separately from 2-channel combine_aug x_train_aug = combine_aug[:, :, :, :1] y_train_aug = combine_aug[:, :, :, 1:] if enlarge: x_train_aug, y_train_aug = concat_aug_data(x_train, x_train_aug, y_train, y_train_aug) return x_train_aug, y_train_aug
def sometimes(aug, chance=0.5): return iaa.Sometimes(chance, aug)
def __init__(self, is_train=True, **kwargs): print("init synthetic animal super augmentation") self.animal = kwargs['animal'] self.nParts = 18 if self.animal == 'horse': self.idxs = np.array([ 1718, 1684, 1271, 1634, 1650, 1643, 1659, 925, 392, 564, 993, 726, 1585, 1556, 427, 1548, 967, 877 ]) # selected kpts w.r.t. the TigDog annotations self.idxs_mask = np.zeros(3299) # for horse elif self.animal == 'tiger': self.idxs = np.array([ 2753, 2679, 2032, 1451, 1287, 3085, 1632, 229, 1441, 1280, 2201, 1662, 266, 158, 270, 152, 219, 129 ]) self.idxs_mask = np.zeros(3299) elif self.animal == 'sheep': self.idxs = np.array([ 2046, 1944, 1267, 1875, 1900, 1868, 1894, 687, 173, 1829, 1422, 821, 624, 580, 622, 575, 1370, 716 ]) self.idxs_mask = np.zeros(3299) elif self.animal == 'elephant': self.idxs = np.array([ 1980, 2051, 1734, 2122, 2155, 2070, 2166, 681, 923, 1442, 1041, 1528, 78, 599, 25, 595, 171, 570, 1147, 1211, 1208, 1226, 1320, 1275, 1265 ]) self.idxs_mask = np.zeros(3299) elif self.animal == 'hound': self.idxs = np.array([ 2028, 2580, 912, 878, 977, 1541, 1734, 480, 799, 1575, 1446, 602, 755, 673, 780, 1580, 466, 631 ]) self.idxs_mask = np.zeros(3299) else: raise Exception('animal should be horse/tiger') self.idxs_mask[ self.idxs] = 1 # adjusting which keypoints to compute loss self.img_folder = kwargs['image_path'] # root image folders # self.jsonfile = kwargs['anno_path'] self.is_train = is_train # training set or test set self.inp_res = kwargs['inp_res'] self.out_res = kwargs['out_res'] self.sigma = kwargs['sigma'] self.scale_factor = kwargs['scale_factor'] self.rot_factor = kwargs['rot_factor'] self.label_type = kwargs['label_type'] self.train_with_occlusion = True # create train/val split self.img_list, self.anno_list, self.bbox_list = load_animal( data_dir=self.img_folder, animal=self.animal) print("total number of images:", len(self.img_list)) self.train_list, self.valid_list = train_test_split( self.img_list, self.animal) print("train images:", len(self.train_list)) print("test images:", len(self.valid_list)) self.mean, self.std = self._compute_mean(self.animal) sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.seq = iaa.Sequential( [ sometimes( iaa.Affine( scale={ "x": (0.5, 1.5), "y": (0.5, 1.5) }, # scale images to 50-150% of their size, individually per axis translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, # translate by -5 to +5 percent (per axis) rotate=(-30, 30), # rotate by -30 to +30 degrees shear=(-20, 20), # shear by -20 to +20 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= 'constant' # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), sometimes( iaa.AdditiveGaussianNoise(scale=0.5 * 255, per_channel=0.5)), sometimes(iaa.GaussianBlur(sigma=(1.0, 5.0))), sometimes( iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast #sometimes(iaa.CoarseDropout((0.2, 0.25), size_percent=0.01)), ], random_order=True)
def __init__(self, cfg, mode): super(Salt, self).__init__() self.cfg = cfg self.mode = mode self.imgs = ImageCollection(os.path.join(TRAIN_IMG_DIR, '*.png'), conserve_memory=False, load_func=_imread_img) self.masks = ImageCollection(os.path.join(TRAIN_MASK_DIR, '*.png'), conserve_memory=False, load_func=_imread_mask) H, W = self.imgs[0].shape coverages = np.array([np.sum(m) / (H * W) for m in self.masks]) coverage_labels = np.ceil(coverages * 10).astype(int) kf = StratifiedKFold(n_splits=cfg.KFOLD_N, shuffle=True, random_state=910103) train_idx, valid_idx = list(kf.split(self.imgs, coverage_labels))[cfg.KFOLD_I] if mode == 'train': self.idx_map = train_idx elif mode == 'valid': self.idx_map = valid_idx else: raise ValueError('Unknown Mode') self.aug_geo1 = iaa.Sequential( [ # General iaa.Fliplr(0.5), iaa.Crop(px=(5, 15), keep_size=False), iaa.Sometimes(0.5, iaa.Affine(rotate=(-10, 10), mode='edge')), # Deformations # iaa.Sometimes(0.3, iaa.PiecewiseAffine(scale=(0.04, 0.08))), iaa.Sometimes(0.3, iaa.PerspectiveTransform(scale=(0.05, 0.1))), ], random_order=True) self.aug_geo2 = iaa.Sequential([ iaa.Scale({ "height": 128, "width": 128 }), ], random_order=False) self.aug_intensity = iaa.Sequential( [ iaa.Invert(0.3), iaa.Sometimes(0.3, iaa.ContrastNormalization((0.5, 1.5))), iaa.OneOf([ iaa.Noop(), 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)
def enqueue(queue, stop, gen_func): gen = gen_func() while True: if stop.is_set(): return queue.put(next(gen)) try: import imgaug as ia from imgaug import augmenters as iaa except: print("Import Error, Please make sure you have imgaug") sometimes = lambda aug: iaa.Sometimes(0.5, aug) lesstimes = lambda aug: iaa.Sometimes(0.2, aug) augmentation = iaa.Sequential([ iaa.Fliplr(0.5, name="FlipLR"), iaa.Flipud(0.5, name="FlipUD"), iaa.ContrastNormalization((0.8, 1.2), name="Contrast"), iaa.Add((-15, 15), per_channel=0.5), iaa.OneOf([ iaa.Multiply((0.8, 1.2), per_channel=0.5, name="Multiply"), iaa.AddToHueAndSaturation((-15, 30), name="Hue"), ]), sometimes(iaa.GaussianBlur((0, 1.0), name="GaussianBlur")), iaa.OneOf([ iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270)
target_folder = os.path.join(to_folder, os.path.basename(folder)) if not os.path.isdir(target_folder): os.makedirs(target_folder) for file in file_list: target_file = os.path.join(target_folder, os.path.basename(file)) shutil.copy(file, target_file) print('Copied file from {} to {}'.format(file, target_file)) # random example images #images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8) # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image. sometimes = lambda aug: iaa.Sometimes(0.5, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0), # 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(
def st(aug): """Defines the "sometimes" probability value.""" return iaa.Sometimes(0.4, aug)
) return history def image_augment2(i): global seq c_i = i.copy() images = np.expand_dims(c_i, 0) images *= 255 images = seq.augment_images(images) images = np.true_divide(images, 255) return images[0] seq = iaa.Sequential([ iaa.Sometimes(0.7, [iaa.Add((-80, 80))]), iaa.Sometimes(0.3, [iaa.CoarseDropout((0.05, 0.2), size_percent=0.1)]), iaa.Sometimes(0.7, [ iaa.SomeOf((1, None), [ iaa.PiecewiseAffine(scale=(0.01, 0.04), mode='reflect'), iaa.OneOf([ iaa.AverageBlur(k=((3, 7), 1.01)), iaa.AverageBlur(k=(1.01, (3, 7))) ]) ], random_order=True) ]), iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine( scale={
def rl(aug): """Defines the "rarely" probability value.""" return iaa.Sometimes(0.09, aug)
def augment(images, boxes, batch_idx, size=556): from imgaug import parameters as iap boxes_augs = [] for box1 in boxes: for box in box1: boxes_augs.append( ia.BoundingBox(x1=box[0], y1=box[1], x2=box[2], y2=box[3])) bbs = ia.BoundingBoxesOnImage(boxes_augs, shape=images[0].shape) seq = iaa.Sequential( [ iaa.OneOf([ iaa.Fliplr(0.5), # horizontal flips iaa.Flipud(0.5), # horizontal flips iaa.CropAndPad(percent=(-0.15, 0.15)), # random crops ]), # Small gaussian blur with random sigma between 0 and 0.5. # But we only blur about 50% of all images. iaa.Sometimes( 0.5, iaa.Add((-40, 40)), iaa.GaussianBlur(sigma=(0, 0.5)), # Invert each image's chanell with 5% probability. # This sets each pixel value v to 255-v. iaa.Invert(0.05, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.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), # iaa.ContrastNormalization((0.75, 1.5)), # Improve or worsen the contrast of images. # Convert each image to grayscale and then overlay the # result with the original with random alpha. I.e. remove # colors with varying strengths. ), # Strengthen or weaken the contrast in each image. # Add gaussian noise. # For 50% of all images, we sample the noise once per pixel. # 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. # 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. # Apply affine transformations to each image. # Scale/zoom them, translate/move them, rotate them and shear them. iaa.OneOf([ 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=(-10, 10), shear=(-4, 4)), iaa.Multiply((0.5, 1.5)), iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.Sequential([ iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"), iaa.WithChannels(0, iaa.Add((50, 100))), iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB") ]), iaa.Superpixels(n_segments=100), iaa.Invert(0.2), iaa.CoarseSaltAndPepper(size_percent=0.05), iaa.ElasticTransformation(2), iaa.SimplexNoiseAlpha(first=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True)), iaa.FrequencyNoiseAlpha(first=iaa.Multiply( iap.Choice([0.5, 1.5]), per_channel=True)), iaa.Grayscale(alpha=(0.0, 1.0)), # iaa.PiecewiseAffine(scale=(0.01, 0.05)) ]), ], random_order=True) # apply augmenters in random order seq_det = seq.to_deterministic( ) # Call this once PER BATCH, otherwise you will always get the to get random images_data = images # for i,img in enumerate(images): # images_data[i,:,:,:]=img[:,:,:] aug_images = seq_det.augment_images(images_data) bbs_augs = seq_det.augment_bounding_boxes([bbs]) for i, aug_bb in enumerate(bbs_augs): idx_i = batch_idx + i imgid = train_dataset[idx_i] save_augs(JPEG_dir, anno_dir, idx_i, aug_images[i], aug_bb, imgid + "_" + str(idx_i)) return
def bbc_to_kitti_text(bbc): txt = [] for b in bbc: txt.append("{} 0 0 0 {} {} {} {} 0 0 0 0 0 0 0".format( b[0], b[1], b[2], b[3], b[4])) return '\n'.join(txt) seq = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontal flips iaa.Crop(percent=(0, 0.1)), # random crops # Small gaussian blur with random sigma between 0 and 0.5. # But we only blur about 50% of all images. iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))), # Strengthen or weaken the contrast in each image. iaa.ContrastNormalization((0.75, 1.5)), # Add gaussian noise. # For 50% of all images, we sample the noise once per pixel. # 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), iaa.Affine(scale={ "x": (0.8, 1.2),
def get_augmenter(name, c_val=255, vertical_flip=True): if name: alot = lambda aug: iaa.Sometimes(0.75, aug) alw = lambda aug: iaa.Sometimes(1, aug) sometimes = lambda aug: iaa.Sometimes(0.3, aug) few = lambda aug: iaa.Sometimes(0.10, aug) if 'classification' in name: scale = random.uniform(0.87, 1.25) seq_rgb = iaa.Sequential([ iaa.Fliplr(0.50), # horizontally flip 50% of the images iaa.Flipud(0.05), # vertically flip 50% of the images sometimes(iaa.Add((-10, 10))), sometimes(iaa.Multiply((0.95, 1.10), per_channel=False)), sometimes(iaa.GaussianBlur(sigma=(0, 0.10))), sometimes(iaa.ContrastNormalization((0.90, 1.15))), alot( iaa.Affine( scale={ "x": (scale), "y": (scale) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (-0.25, 0.25), "y": (-0.2, 0.20) }, # translate by -20 to +20 percent (per axis) rotate=(-20, 20), # rotate by -45 to +45 degrees order=1, #bilinear interpolation (fast) cval=0, mode= "reflect" # `edge`, `wrap`, `reflect` or `symmetric` # 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) )) ]) return seq_rgb if 'segmentation' in name: value_flip = round(random.random()) if value_flip > 0.5: value_flip = 1 else: value_flip = 0 value_flip2 = round(random.random()) if value_flip2 > 0.5: value_flip2 = 1 else: value_flip2 = 0 value_add = int(random.uniform(-12, 12)) value_Multiply = random.uniform(0.97, 1.05) value_GaussianBlur = random.uniform(0.0, 0.10) ContrastNormalization = random.uniform(0.93, 1.13) scale = random.uniform(0.50, 2) value_x2 = random.uniform(-0.3, 0.3) value_y2 = random.uniform(-0.10, 0.10) val_rotate = random.uniform(-5, 5) seq_image = iaa.Sequential([ iaa.Fliplr(value_flip), # horizontally flip 50% of the images # iaa.Flipud(value_flip2), # vertically flip 50% of the images iaa.Affine( scale={ "x": (scale), "y": (scale) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (value_x2), "y": (value_y2) }, # translate by -20 to +20 percent (per axis) #rotate=(val_rotate), # rotate by -45 to +45 degrees order=1, #bilinear interpolation (fast) cval=c_val, mode="reflect", # `edge`, `wrap`, `reflect` or `symmetric` # cval=c_val, # 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) ) ]) seq_image2 = iaa.Sequential([ sometimes(iaa.Add(value_add)), # sometimes(iaa.Multiply(value_Multiply, per_channel=False)), sometimes( iaa.GaussianBlur(sigma=(value_GaussianBlur, value_GaussianBlur))), sometimes(iaa.ContrastNormalization(ContrastNormalization)) ]) seq_label = iaa.Sequential([ iaa.Fliplr(value_flip), # horizontally flip 50% of the images # iaa.Flipud(value_flip2), # vertically flip 50% of the images iaa.Affine( scale={ "x": (scale), "y": (scale) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (value_x2), "y": (value_y2) }, # translate by -20 to +20 percent (per axis) #rotate=(val_rotate), # rotate by -45 to +45 degrees order=0, #bilinear interpolation (fast) cval=c_val, mode="constant" # `edge`, `wrap`, `reflect` or `symmetric` # 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) ) ]) seq_mask = iaa.Sequential([ iaa.Fliplr(value_flip), # horizontally flip 50% of the images # iaa.Flipud(value_flip2), # vertically flip 50% of the images iaa.Affine( scale={ "x": (scale), "y": (scale) }, # scale images to 80-120% of their size, individually per axis translate_percent={ "x": (value_x2), "y": (value_y2) }, # translate by -20 to +20 percent (per axis) #rotate=(val_rotate), # rotate by -45 to +45 degrees order=0, #bilinear interpolation (fast) cval=0, mode="constant" # `edge`, `wrap`, `reflect` or `symmetric` # cval=c_val, # 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) ) ]) return seq_image2, seq_image, seq_label, seq_mask else: return None
def __init__(self, args, split, input_size=224, scale=0.05, shift=0.05, flip=0.5, rand_warp=False, skip_warp=0.1, warp_scale=0.05, rotate=20): self.args = args self.split = split self.input_size = input_size self.scale = scale self.shift = shift self.flip = flip self.rand_warp = rand_warp self.skip_warp = skip_warp self.warp_scale = warp_scale self.mean = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(1, 1, 3) self.std = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(1, 1, 3) self.data_rng = np.random.RandomState(123) self.eig_val = np.array([0.2141788, 0.01817699, 0.00341571], dtype=np.float32) self.eig_vec = np.array([[-0.58752847, -0.69563484, 0.41340352], [-0.5832747, 0.00994535, -0.81221408], [-0.56089297, 0.71832671, 0.41158938]], dtype=np.float32) self.samples = [] self.seq = iaa.Sequential([ iaa.Sometimes(0.5, [ iaa.Fliplr(flip), iaa.Affine(scale={ "x": (1.0 - shift, 1.0 + shift), "y": (1.0 - shift, 1.0 + shift) }, translate_percent={ "x": (-scale, scale), "y": (-scale, scale) }, rotate=(-rotate, rotate)), iaa.GaussianBlur(sigma=(0, 1.0)), iaa.Sometimes(0.5, iaa.CoarseDropout(0.2, size_percent=0.1)), iaa.Sometimes( 0.5, iaa.PiecewiseAffine( scale=(0.01, warp_scale), nb_cols=3, nb_rows=3)), iaa.Sometimes( 0.5, iaa.OneOf([ iaa.SaltAndPepper(0.1), iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1)), ])) ]), ]) self.split = ['train', 'val'] if self.split == 'all' else [self.split] for spl in self.split: data_path = os.path.join(args.data, spl) for path, dir, files in os.walk(data_path): for filename in files: ext = os.path.splitext(filename)[-1].lower() if ext in ('.png', '.jpg', '.jpeg'): label_name = path.split('/')[-1] self.samples.append( (os.path.join(path, filename), int(label_name))) print('Loaded {} total: {}'.format(self.split[0], len(self.samples)))
def main(): images = [ misc.imresize( ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.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.Sharpen(alpha=(0.1, 1.0), strength=(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.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.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation") ] #for i, aug in enumerate(augmenters): #print(i) #aug.deepcopy() #import copy #copy.deepcopy(aug) 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: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)