def augument(self, image, bbox_list): seq = iaa.Sequential([ # 变形 iaa.Sometimes( 0.6, [ iaa.OneOf([ iaa.Affine(shear={ 'x': (-1.5, 1.5), 'y': (-1.5, 1.5) }, mode="edge"), # 仿射变化程度,单位像素 iaa.Rotate(rotate=(-1, 1), mode="edge"), # 旋转,单位度 ]) ]), # 扭曲 iaa.Sometimes( 0.5, [ iaa.OneOf([ iaa.PiecewiseAffine( scale=(0, 0.02), nb_rows=2, nb_cols=2), # 局部仿射 iaa.ElasticTransformation( # distort扭曲变形 alpha=(0, 3), # 扭曲程度 sigma=(0.8, 1), # 扭曲后的平滑程度 mode="nearest"), ]), ]), # 模糊 iaa.Sometimes( 0.5, [ iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 0.7)), iaa.AverageBlur(k=(1, 3)), iaa.MedianBlur(k=(1, 3)), iaa.BilateralBlur( d=(1, 5), sigma_color=(10, 200), sigma_space=(10, 200)), # 既噪音又模糊,叫双边, iaa.MotionBlur(k=(3, 5)), iaa.Snowflakes(flake_size=(0.1, 0.2), density=(0.005, 0.025)), iaa.Rain(nb_iterations=1, drop_size=(0.05, 0.1), speed=(0.04, 0.08)), ]) ]), # 锐化 iaa.Sometimes(0.3, [ iaa.OneOf([ iaa.Sharpen(), iaa.GammaContrast(), iaa.SigmoidContrast() ]) ]), # 噪音 iaa.Sometimes(0.3, [ iaa.OneOf([ iaa.AdditiveGaussianNoise(scale=(1, 5)), iaa.AdditiveLaplaceNoise(scale=(1, 5)), iaa.AdditivePoissonNoise(lam=(1, 5)), iaa.Salt((0, 0.02)), iaa.Pepper((0, 0.02)) ]) ]), # 剪切 iaa.Sometimes( 0.8, [ iaa.OneOf([ iaa.Crop((0, 2)), # 切边, (0到10个像素采样) ]) ]), ]) assert bbox_list is None or type(bbox_list) == list if bbox_list is None or len(bbox_list) == 0: polys = None else: polys = [ia.Polygon(pos) for pos in bbox_list] polys = ia.PolygonsOnImage(polys, shape=image.shape) # 处理部分或者整体出了图像的范围的多边形,参考:https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html polys = polys.remove_out_of_image().clip_out_of_image() images_aug, polygons_aug = seq(images=[image], polygons=polys) image = images_aug[0] if polygons_aug is None: polys = None else: polys = [] for p in polygons_aug.polygons: polys.append(p.coords) polys = np.array(polys, np.int32).tolist() # (N,2) return image, polys
def __init__(self, imgdirs_list, annfiles_list, train, transform, device, little_train=False, with_file_path=False, S=7, B=2, C=20, test_mode=False): print('data init') self.imgdirs_list = imgdirs_list self.anns_list = annfiles_list self.train = train self.transform = transform self.fnames = [] self.boxes = [] self.labels = [] self.dataset_list = [] self.resize = 448 self.S = S self.B = B self.C = C self.device = device self._test = test_mode self.with_file_path = with_file_path self.img_augsometimes = lambda aug: iaa.Sometimes(0.25, aug) self.bbox_augsometimes = lambda aug: iaa.Sometimes(0.5, aug) self.augmentation = iaa.Sequential( [ # augment without change bboxes self.img_augsometimes( iaa.SomeOf( (1, 3), [ iaa.Dropout([0.05, 0.2 ]), # drop 5% or 20% of all pixels iaa.Sharpen((0.1, .8)), # sharpen the image # iaa.GaussianBlur(sigma=(2., 3.5)), iaa.OneOf([ iaa.GaussianBlur(sigma=(2., 3.5)), iaa.AverageBlur(k=(2, 5)), iaa.BilateralBlur(d=(7, 12), sigma_color=(10, 250), sigma_space=(10, 250)), iaa.MedianBlur(k=(3, 7)), ]), iaa.AddElementwise((-50, 50)), iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)), # iaa.JpegCompression(compression=(80, 95)), iaa.Multiply((0.5, 1.5)), iaa.MultiplyElementwise((0.5, 1.5)), iaa.ReplaceElementwise(0.05, [0, 255]), # iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", # children=iaa.WithChannels(2, iaa.Add((-10, 50)))), iaa.OneOf([ iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 1, iaa.Add((-10, 50)))), iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 2, iaa.Add((-10, 50)))), ]), ], random_order=True)), iaa.Fliplr(.5), iaa.Flipud(.125), # augment changing bboxes self.bbox_augsometimes( iaa.Affine( # translate_px={"x": 40, "y": 60}, scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }, rotate=(-5, 5), )) ], random_order=True) for imgdir, annfile in zip(self.imgdirs_list, self.anns_list): print('handle dataset:\n\t' + imgdir + '\n\t' + annfile) annfile_json = json.load(open(annfile, 'r')) images = annfile_json['images'] annotations = annfile_json['annotations'] ann_dicts = {} for ann in annotations: if ann['image_id'] not in ann_dicts.keys(): ann_dicts[ann['image_id']] = [] ann_dicts[ann['image_id']].append(ann) for img in images: img['file_name'] = os.path.join(imgdir, img['file_name']) if img['id'] in ann_dicts.keys(): anns = ann_dicts[img['id']] else: continue image_ann = {'image_info': img, 'ann': anns} self.dataset_list.append(image_ann) self.num_samples = len(self.dataset_list) print('There are %d pics in datasets.' % (self.num_samples))
def processor(self): return iaa.AdditiveGaussianNoise(self.loc, self.scale, self.per_channel)
iaa.SomeOf((0, 6), [ (iaa.Superpixels(p_replace=(0, 1.0), n_segments=(100, 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, 9)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), 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 iaa.Invert(0.5, per_channel=True), # invert color channels iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25), # move pixels locally around (with random strengths) ], random_order=True ), sometimes(iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels iaa.SaltAndPepper(p=(0.03, 0.3)), ])), ] ) def uneven_light(): def norm(Z):
def img_augmentation(images, save_name_prefix, img_augmentation_pre_image=60, save_dir='data/augmentation_img'): if not os.path.isdir(save_dir): os.mkdir(save_dir) save_dir += '/' + save_name_prefix if not os.path.isdir(save_dir): os.mkdir(save_dir) 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.LinearContrast((0.5, 2.0))) ]), iaa.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) for i, img in enumerate(images): images_aug = seq( images=np.array([img for _ in range(img_augmentation_pre_image)])) for j, img_aug in enumerate(images_aug): grid_image = ia.draw_grid(np.array([img_aug]), cols=1) imageio.imwrite( '%s/%s_i%d_j%d.jpg' % (save_dir, save_name_prefix, i, j), grid_image)
str(types) + str(bird_specie_counter) + image_number + ".jpg", ), augmentated_image) elif number_of_images >= 10: cv2.imwrite( join( destination, str(types) + str(bird_specie_counter) + image_number + ".jpg", ), augmentated_image) # Dataset Augmentation gauss = iaa.AdditiveGaussianNoise(scale=0.2 * 255) # blur = iaa.GaussianBlur(sigma=(3.0)) # flip = iaa.Fliplr(1.0) # contrast = iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5) sharp = iaa.Sharpen(alpha=(0, 0.3), lightness=(0.7, 1.3)) affine = iaa.Affine(translate_px={"x": (-50, 50), "y": (-50, 50)}) # add = iaa.Add((-20, 20), per_channel=0.5) # multiply = iaa.Multiply((0.8, 1.2), per_channel=0.5) hue = 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"), ]) aug = iaa.Sequential([
def soft(image_iteration): iteration = image_iteration / (120 * 1.5) frequency_factor = 0.05 + float(iteration) / 1200000.0 color_factor = float(iteration) / 1200000.0 dropout_factor = 0.198667 + (0.03856658 - 0.198667) / (1 + (iteration / 196416.6)**1.863486) blur_factor = 0.5 + (0.5 * iteration / 120000.0) add_factor = 10 + 10 * iteration / 170000.0 multiply_factor_pos = 1 + (2.5 * iteration / 800000.0) multiply_factor_neg = 1 - (0.91 * iteration / 800000.0) contrast_factor_pos = 1 + (0.5 * iteration / 800000.0) contrast_factor_neg = 1 - (0.5 * iteration / 800000.0) #print ('iteration',iteration,'Augment Status ',frequency_factor,color_factor,dropout_factor,blur_factor,add_factor, # multiply_factor_pos,multiply_factor_neg,contrast_factor_pos,contrast_factor_neg) augmenter = iaa.Sequential( [ iaa.Sometimes(frequency_factor, iaa.GaussianBlur( (0, blur_factor))), # blur images with a sigma between 0 and 1.5 iaa.Sometimes( frequency_factor, iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, dropout_factor), per_channel=color_factor)), # add gaussian noise to images iaa.Sometimes( frequency_factor, iaa.CoarseDropout((0.0, dropout_factor), size_percent=(0.08, 0.2), per_channel=color_factor)), # randomly remove up to X% of the pixels iaa.Sometimes( frequency_factor, iaa.Dropout((0.0, dropout_factor), per_channel=color_factor)), # randomly remove up to X% of the pixels iaa.Sometimes( frequency_factor, iaa.Add((-add_factor, add_factor), per_channel=color_factor)), # change brightness of images (by -X to Y of original value) iaa.Sometimes( frequency_factor, iaa.Multiply((multiply_factor_neg, multiply_factor_pos), per_channel=color_factor)), # change brightness of images (X-Y% of original value) iaa.Sometimes( frequency_factor, iaa.ContrastNormalization( (contrast_factor_neg, contrast_factor_pos), per_channel=color_factor)), # improve or worsen the contrast iaa.Sometimes(frequency_factor, iaa.Grayscale( (0.0, 1))), # put grayscale ], random_order=True # do all of the above in random order ) return augmenter
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description="image augmentor") parser.add_argument("--image-dir", dest="imagedir", default="", required=True) parser.add_argument("--output-dir", dest="outputdir", default="output") args = parser.parse_args() seq = iaa.Sequential([ iaa.Crop(px=(0, 16)), iaa.Fliplr(0.5), iaa.GaussianBlur(sigma=(0, 3.0)) ]) def sometimes(aug): return iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Flipud(0.2), sometimes(iaa.Crop(percent=(0, 0.1))), 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)), iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), 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)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), sometimes( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.7)), iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), 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), ]), iaa.Invert(0.05, per_channel=True), iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) ], random_order=True) ], random_order=True) if not os.path.exists(args.outputdir): os.makedirs(args.outputdir) for batch_index, batch in enumerate( batch_images(load_images(args.imagedir), batch_size=20)): for index, image in enumerate(seq.augment_images(batch)): imagefile = os.path.join( args.outputdir, "{:0>6d}{:0>2d}-image.png".format(batch_index, index)) print("saving {}".format(imagefile)) cv2.imwrite(imagefile, image)
def __init__(self, data_dir, batch_size, image_shape): """ Keras Sequence object to train a model on larger-than-memory data. @:param: data_dir: directory in which we have got the kitti images and the corresponding masks @:param: batch_size: define the number of training samples to be propagated. @:param: image_shape: shape of the input image """ self.batch_size = batch_size self.image_shape = image_shape # define the image and mask paths self.image_paths = glob(data_dir + "/images/*.png") self.mask_paths = glob(data_dir + "data_dir/masks/*.png") # sort the image and mask paths to maintain the coherence self.image_paths = sorted(self.image_paths) self.mask_paths = sorted(self.mask_paths) self.sometimes = lambda aug: iaa.Sometimes(0.5, aug) # alter it as per the labels self.color_dict = { 0: (255, 0, 0), # building 1: (0, 0, 255), # road 2: (255, 255, 255) } # background # create the augmentation pipeline self.aug_pipe = iaa.Sequential( [ iaa.SomeOf( (0, 5), [ iaa.OneOf([ iaa.GaussianBlur( (0, 3.0) ), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout( (0.01, 0.1), per_channel=0.5 ), # randomly remove up to 10% of the pixels ]), 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), iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5), # improve or worsen the contrast ], random_order=True) ], random_order=True)
def __init__(self, config, mode="all"): assert mode in ["train", "validation", "all"], f"Should be train, validation or all, got {mode}" self.config = config self.sequence_length = 2 # if config.get("sequence_length", False) == False else config["sequence_length"] self.sc = SequenceDataset(Animal_Sequence(config), self.sequence_length, step=config["sequence_step_size"]) # works if dataroot like "VOC2011/cats_meta" # TODO PROBABLY NOT CORRECT HERE self.animal = config["dataroot"].split("/")[1].split("_")[0] self.train = int(config["train_size"] * len(self.sc)) self.test = 1 - self.train self.sigma = config["sigma"] self.augmentation = config["augmentation"] self.aug_factor = 0.5 self.logger = get_logger(self) self.resize = iaa.Resize(self.config["resize_to"]) if self.augmentation: self.seq = iaa.Sequential([ iaa.Sometimes(self.aug_factor, iaa.AdditiveGaussianNoise(scale=0.05 * 255)), iaa.Sometimes(self.aug_factor, iaa.SaltAndPepper(0.01, per_channel=False)), iaa.Sometimes(self.aug_factor, iaa.CoarseDropout(0.01, size_percent=0.5)), iaa.Fliplr(self.aug_factor), iaa.Flipud(self.aug_factor), iaa.Sometimes(self.aug_factor, iaa.GaussianBlur(sigma=(0, 3.0))), iaa.LinearContrast((0.75, 1.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)), ], random_order=True) self.joints = [ [2, 0], # Nose - L_Eye [2, 1], # Nose - R_Eye [0, 3], # L_Eye - L_EarBase [1, 4], # R_Eye - R_EarBase [2, 8], # Nose - Throat [8, 9], # Throat - L_F_Elbow [8, 5], # Throat - R_F_Elbow [9, 16], # L_F_Elbow - L_F_Knee [16, 6], # L_F_Knee - L_F_Paw [5, 17], # R_F_Elbow - R_F_Knee [17, 7], # R_F_Knee - R_F_Paw [14, 18], # L_B_Elbow - L_B_Knee [18, 13], # L_B_Knee - L_B_Paw [15, 19], # R_B_Elbow - R_B_Knee [19, 13], # R_B_Knee - R_B_Paw [10, 11], # Withers - TailBase ] if mode != "all": # split_indices = np.arange(self.train) if mode == "train" else np.arange(self.train + 1, len(self.sc)) dset_indices = np.arange(len(self.sc)) train_indices, test_indices = sklearn.model_selection.train_test_split(dset_indices, train_size=float( config["train_size"]), random_state=int( config["random_state"])) if mode == "train": self.data = SubDataset(self.sc, train_indices) else: self.data = SubDataset(self.sc, test_indices) else: self.data = self.sc
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
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 get_aug_methods(): # options: ["constant", "edge", "symmetric", "reflect", "wrap"] mode = "edge" seq = ( # rotate + shear + translate + scale iaa.Affine(rotate=(-20, 20), shear=(-20, 20), translate_percent=(-.2, .2), scale=(0.9, 1.0), order=[0, 1, 2, 3], mode=mode), # shear + rotate + translate + scale iaa.Affine(shear=(-20, 20), rotate=(-20, 20), translate_percent=(-.2, .2), scale=(0.9, 1.0), order=[0, 1, 2, 3], mode=mode), # scale + shear + rotate + translate + scale iaa.Affine(scale=(0.85, 1.0), shear=(-20, 20), rotate=(-20, 20), translate_percent=(-.2, .2), order=[0, 1, 2, 3], mode=mode), # rotate + translate + shear + scale iaa.Affine(rotate=(-20, 20), translate_percent=(-.2, .2), shear=(-20, 20), scale=(0.9, 1.0), order=[0, 1, 2, 3], mode=mode), # rotate + translate + scale iaa.Affine(rotate=(-20, 20), translate_percent=(-.2, .2), scale=(0.9, 1.0), order=[0, 1, 2], mode=mode), # scale + rotate + translate iaa.Affine(scale=(0.9, 1.0), rotate=(-20, 20), translate_percent=(-.2, .2), order=[0, 1, 2], mode=mode), # crop + translate + shear iaa.Sequential([ iaa.Crop(percent=(0, 0.1)), iaa.Affine(translate_percent=(-.1, .1), shear=(-15, 15), order=[0, 1], mode=mode) ]), # crop + rotate + translate + shear iaa.Sequential([ iaa.Crop(percent=(0, 0.1)), iaa.Affine(rotate=(-10, 10), translate_percent=(-.1, .1), shear=(-15, 15), order=[0, 1, 2], mode=mode) ]), # crop + rotate + translate + shear + flipr iaa.Sequential([ iaa.Crop(percent=(0, 0.1)), iaa.Affine(rotate=(-10, 10), translate_percent=(-.1, .1), shear=(-15, 15), order=[0, 1, 2], mode=mode), iaa.Fliplr(0.9) ]), # gaussian noise iaa.AdditiveGaussianNoise(scale=(0, 0.08 * 255)), # sharpen iaa.Sharpen(alpha=(0.5, 1.0), lightness=(0.7, 2.)), # crop iaa.Crop(percent=(0, 0.15)), # gaussian + flipr iaa.Sequential([ iaa.AdditiveGaussianNoise(scale=(0, 0.08 * 255)), iaa.Fliplr(0.9) ]), # sharpen + flipr iaa.Sequential([ iaa.Sharpen(alpha=(0.5, 1.0), lightness=(0.7, 2.)), iaa.Fliplr(0.9) ]), # crop + flipr iaa.Sequential([iaa.Crop(percent=(0, 0.15)), iaa.Fliplr(0.9)]), # gaussian blur iaa.GaussianBlur(sigma=(1., 1.5)), # add iaa.Add((-20, 40)), # contrast normalization iaa.ContrastNormalization((1., 1.5)), # piecewise affine 1 iaa.PiecewiseAffine(scale=(0.01, 0.05)), # piecewise affine 2 iaa.PiecewiseAffine(scale=(0.01, 0.05)), # piecewise affine 3 iaa.PiecewiseAffine(scale=(0.01, 0.05)), # rotate + translate + scale + piecewise affine iaa.Sequential([ iaa.Affine(rotate=(-20, 20), translate_percent=(-.2, .2), scale=(0.9, 1.0), order=[0, 1, 2], mode=mode), iaa.PiecewiseAffine(scale=(0.01, 0.05)) ])) return seq
from skimage import io from imgaug import augmenters as iaa import argparse from tqdm import tqdm import os aug_dict = { 'AdditiveGaussianNoise': iaa.AdditiveGaussianNoise(loc=0, scale=0.05 * 255, per_channel=False), 'AdditiveGaussianNoise_pc': iaa.AdditiveGaussianNoise(loc=0, scale=0.05 * 255, per_channel=True), 'AdditiveLaplaceNoise': iaa.AdditiveLaplaceNoise(loc=0, scale=0.05 * 255, per_channel=False), 'AdditiveLaplaceNoise_pc': iaa.AdditiveLaplaceNoise(loc=0, scale=0.05 * 255, per_channel=True), 'AdditivePoissonNoise': iaa.AdditivePoissonNoise(lam=16.00, per_channel=False), 'AdditivePoissonNoise_pc': iaa.AdditivePoissonNoise(lam=16.00, per_channel=True), 'ImpulseNoise': iaa.ImpulseNoise(p=0.05), 'SaltAndPepper': iaa.SaltAndPepper(p=0.05), 'GaussianBlur': iaa.GaussianBlur(sigma=0.50), 'AverageBlur': iaa.AverageBlur(k=3), 'AddToHueAndSaturation_p': iaa.AddToHueAndSaturation(value=25), 'AddToHueAndSaturation_n': iaa.AddToHueAndSaturation(value=-25),
def gaussian_noise(img): # 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. gaussian_noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.08*255), per_channel=0.5) return gaussian_noise.augment_image(img)
def load_aug(): def sometimes(aug): return iaa.Sometimes(0.2, aug) seq[0] = iaa.Sequential( [ iaa.Crop(percent=(0, 0.3)), # random crops iaa.Fliplr(0.5), # crop images by -5% to 10% of their height/width sometimes(iaa.CropAndPad( percent=(-0.2, 0.3), pad_mode=ia.ALL, pad_cval=(0, 255) )), sometimes(iaa.Affine( scale={"x": (0.7, 1.3), "y": (0.7, 1.3)}, translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-5, 5), shear=(-5, 5), order=[0, 1], # if mode is constant, use a cval between 0 and 255 cval=(0, 255), # use any of scikit-image's warping modes (see 2nd image from the top for examples) mode=ia.ALL )), iaa.Sometimes(0.1, iaa.MotionBlur(k=15, angle=[-45, 45])), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf((0, 5), [ iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 5)), iaa.MedianBlur(k=(3, 5)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=( 0.75, 1.5)), # sharpen images # add gaussian noise to images iaa.AdditiveGaussianNoise(loc=0, scale=( 0.0, 0.05*255), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.Add((-10, 10), per_channel=0.5), # change hue and saturation iaa.AddToHueAndSaturation((-20, 20)), # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.LinearContrast((0.5, 2.0)) ) ]), # improve or worsen the contrast iaa.LinearContrast((0.3, 3.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.Multiply((0.333, 3), per_channel=0.5), ], random_order=True ) ], random_order=True )
def __init__(self, image_root, data_list, is_train, transform=None): super(CLDC_Dataset, self).__init__() self.image_root = image_root self.data_list = data_list self.is_train = is_train self.transform = transform self.image_list = [] self.label_list = [] for data in self.data_list: self.image_list.append(data[0]) self.label_list.append(data[1]) self.seq = iaa.SomeOf( (3, 11), { # self.seq = iaa.SomeOf((0, 5), { # iaa.Fliplr(0.5), iaa.Flipud(0.5), # iaa.Crop(percent=(0, 0.1)), # 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)), # 先将图片从RGB变换到HSV,然后将H值增加10,然后再变换回RGB iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels( 2, iaa.Add((10, 50)))), iaa.AverageBlur(k=((2, 5), (1, 3))), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect((0.0, 0.2)), second=iaa.ContrastNormalization( (0.5, 2.0)), per_channel=True), # 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.ImpulseNoise(p=0.02), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.02 * 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.PerspectiveTransform(scale=0.06), # # 图像扭曲 # iaa.PiecewiseAffine(scale=(0.01, 0.05)), # 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=(-45, 45), shear=(-8, 8)) }, random_order=True)
def __init__(self, timestep=5, steps_per_epoch=1000, taux_validation=0.2, input_shape=(480, 720)): self.timestep = timestep self.taux_val = taux_validation self.input_shape = input_shape self.steps_per_epoch = steps_per_epoch sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.augmentation = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% 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.05, 0.05), "y": (-0.05, 0.05) }, # translate by -5 to +5 percent (per axis) rotate=(-10, 10), # rotate by -45 to +45 degrees order=[ 0, 1 ], # use nearest neighbour or bilinear interpolation (fast) cval=( 0, 255 ), # if mode is constant, use a cval between 0 and 255 mode=ia. ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf( (0, 5), [ iaa.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 iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # add gaussian noise to images iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add( (-10, 10), per_channel=0.5 ), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation( (-10, 10)), # change hue and saturation iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.LinearContrast( (0.5, 1.5), 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.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True)
import imgaug from imgaug import augmenters as iaa imgaug.seed(1) seg = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Affine(rotate=(-20, 20)), iaa.OneOf([ iaa.GaussianBlur(sigma=(0., 4.0)), iaa.Dropout((0., 0.15), per_channel=0.5), ]), iaa.OneOf([ iaa.Add((-60, 60), per_channel=True), iaa.Sharpen(alpha=(0., 1.0), lightness=(0.5, 1.5)), iaa.GammaContrast(gamma=(0.5, 1.), per_channel=True), iaa.AdditiveGaussianNoise(loc=0, scale=(0.14 * 255, 0.15 * 255), per_channel=0.5) ]) ])
def apply_transform(augm, matrix, image, params): """ Apply a transformation to an image. The origin of transformation is at the top left corner of the image. The matrix is interpreted such that a point (x, y) on the original image is moved to transform * (x, y) in the generated image. Mathematically speaking, that means that the matrix is a transformation from the transformed image space to the original image space. Args matrix: A homogeneous 3 by 3 matrix holding representing the transformation to apply. image: The image to transform. params: The transform parameters (see TransformParameters) """ output = cv2.warpAffine( image, matrix[:2, :], dsize = (image.shape[1], image.shape[0]), flags = params.cvInterpolation(), borderMode = params.cvBorderMode(), borderValue = params.cval, ) if augm == 1: # in 1/9 cases gaussian noise will be added to the image if randint(0, 8) == 1: seq = iaa.Sequential([ iaa.AdditiveGaussianNoise(scale=0.5*255) ]) output = seq.augment_image(output) if augm == 11: # in 1/5 cases gaussian noise will be added to the image if randint(0, 4) == 1: seq = iaa.Sequential([ iaa.AdditiveGaussianNoise(scale=0.5*255) ]) output = seq.augment_image(output) if augm == 2: # in 1/9 of cases, color will be changed # between 50 to 100 will be added to channels 0 to 2 (BGR) if randint(0, 8) == 1: seq = iaa.Sequential([ iaa.WithChannels(randint(0, 2), iaa.Add((50, 200))) ]) output = seq.augment_image(output) if augm == 22: # in 1/5 of cases, color will be changed # between 50 to 100 will be added to channels 0 to 2 (BGR) if randint(0, 4) == 1: seq = iaa.Sequential([ iaa.WithChannels(randint(0, 2), iaa.Add((50, 200))) ]) output = seq.augment_image(output) if augm == 3: if randint(0, 8) == 1: seq = iaa.Sequential([ iaa.WithChannels(randint(0, 2), iaa.Add((50, 200))) ]) output = seq.augment_image(output) if randint(0, 8) == 1: seq = iaa.Sequential([ iaa.AdditiveGaussianNoise(scale=0.5*255) ]) output = seq.augment_image(output) return output
bbs_array = bbs_object.to_xyxy_array() df_bbs = pd.DataFrame(bbs_array, columns=['xmin', 'ymin', 'xmax', 'ymax']) return df_bbs aug = iaa.SomeOf(2, [ iaa.Affine(scale=(0.5, 1.5)), iaa.Affine(rotate=(-60, 60)), iaa.Affine(translate_percent={ "x": (-0.3, 0.3), "y": (-0.3, 0.3) }), iaa.Fliplr(1), iaa.Multiply((0.5, 1.5)), iaa.GaussianBlur(sigma=(1.0, 3.0)), iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)) ]) def image_aug(df, images_path, aug_images_path, image_prefix, augmentor): aug_bbs_xy = pd.DataFrame(columns=[ 'filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax' ]) grouped = df.groupby('filename') for filename in df['filename'].unique(): group_df = grouped.get_group(filename) group_df = group_df.reset_index() group_df = group_df.drop(['index'], axis=1) image = imageio.imread(images_path + filename) bb_array = group_df.drop(['filename', 'width', 'height', 'class'],
def __init__(self): 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 ) ] * 3 all_files = [] self.val_db_path = [] ids = [ "steer103_v5_way_v2", 'steer103_v5_way_v2_town02', 'rfs_sim_v2_way', 'rfs_sim_v4_extra_way', 'rfs_sim_v5_extra_way' ] for id in ids: all_files += glob.glob( "/data/yang/code/aws/scratch/carla_collect/" + id + "/*/data_*.h5") for valid in range(1, 15, 3): self.val_db_path += glob.glob( "/data/yang/code/aws/scratch/carla_collect/" + id + "/*WeatherId=" + str(valid).zfill(2) + "/data_*.h5") self.train_db_path = list(set(all_files) - set(self.val_db_path)) self.speed_factor = 40.0 # In KM/H # 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 = 5 # now measured in how many batches
def get_inner_bbs(image_path, dst_img_dir, array_info, p_numbers): ''' :param image_path: src img path :param dst_img_dir: img save path :param coor_array: label coor array :param p_numbers: Numbers of images to enhance :return: [(bbs_array, img_info), (bbs_array, img_info)] ''' try: assert array_info.shape[1] == 5 coor_array = array_info[:, :-1] cls_array = array_info[:, -1] image = Image.open(image_path) image = np.array(image) img_name = os.path.split(image_path)[-1].split(".")[0] bbs = BoundingBoxesOnImage.from_xyxy_array(coor_array, shape=image.shape) except Exception as e: print(f"err:{e}") print(array_info.shape) print(image_path) return None # # Draw the original picture # image_before = draw_bbs(image, bbs, 100) # ia.imshow(image_before) # Image augmentation sequence seq = iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Crop(percent=(0, 0.1)), iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))), # Strengthen or weaken the contrast in each image. iaa.LinearContrast((0.75, 1.5)), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), # change illumination iaa.Multiply((0.3, 1.2), per_channel=0.2), # affine transformation 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=(-5, 5), shear=(-8, 8)) ], random_order=True) # apply augmenters in random order res_list = [] # gen img and coor try: for epoch in range(p_numbers): image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) # bbs_aug = bbs_aug.remove_out_of_image().clip_out_of_image() # # draw aug img and label image_after = bbs_aug.draw_on_image(image_aug, size=2, color=[0, 0, 255]) ia.imshow(image_after) # save img h, w, c = image_aug.shape img_aug_name = rf'{dst_img_dir}/{img_name}_{epoch}.jpg' im = Image.fromarray(image_aug) im.save(img_aug_name) bbs_array = bbs_aug.to_xyxy_array() result_array = np.column_stack((bbs_array, cls_array)) res_list.append([result_array, (img_aug_name, h, w, c)]) except Exception as e: print(e) print(img_aug_name) return None # return coor and img info return res_list
def __init__(self, height, width, split): sometimes = lambda aug: iaa.Sometimes(0.5, aug) if split == 'train': self.seq = iaa.Sequential( [ # 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.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) )), iaa.Resize({ "height": height, "width": width }), sometimes( 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). sometimes( iaa.Sharpen(alpha=(0, 0.5), lightness=(0.75, 1.5))), # Add gaussian noise to some images. sometimes( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)), # Add a value of -5 to 5 to each pixel. sometimes(iaa.Add((-5, 5), per_channel=0.5)), # Change brightness of images (80-120% of original value). sometimes(iaa.Multiply((0.8, 1.2), per_channel=0.5)), # Improve or worsen the contrast of images. sometimes( iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5)), ], # do all of the above augmentations in random order random_order=True) else: self.seq = iaa.Resize({"height": height, "width": width})
def __init__(self, images_height=32, image_width=256, max_string_lenght=35, background_type=['real', 'const'], dataset_type='random', backgrounds_path='./backgrounds/', fonts_path='./valid_fonts/', valid_charset_path='./valid_charset.txt', text_examples='./text_examples.txt', font_size_bound=(30, 18)): ''' background_type - list consist used types of bg, e.g. ['real', 'random', 'const']. If use 'real', then backgrounds_path should be set to used backgrounds. dataset - type of dataset 'random' or 'samples'. If 'samples' then text_examples should be defined. If 'random' then text string will be generated from valid charset randomly. valid_char_set - path to text file with valid chars. text_examples - file of text string examples backgrounds_path - path with background images font_size_bound - bounds of font size range, from which value will be chosen ''' assert set([ 'real', 'random', 'const' ]).intersection(set(background_type)) == set( background_type ), 'background_type argument should be list consisting only "real", "random" or "const"' assert dataset_type in [ 'random', 'samples' ], 'dataset_type argument should be one of "random" or "samples"' self.images_height = images_height self.image_width = image_width self.max_string_lenght = max_string_lenght self.background_type = background_type self.dataset_type = dataset_type self.backgrounds_files = None if 'real' in background_type: self.backgrounds_files = (backgrounds_path, self.set_backgrounds(backgrounds_path)) self.backgrounds = self.cache_backgrounds() self.text_samples = None if dataset_type == 'samples': self.text_samples = self.set_texts_samples(text_examples) self.fonts_files = (fonts_path, self.set_fonts(fonts_path)) logger.info('{} fonts to produce data'.format(len( self.fonts_files[1]))) self.valid_charset = self.set_valid_charset(valid_charset_path) self.char_to_indx = dict( zip(self.valid_charset, range(len(self.valid_charset)))) self.font_size_bound = font_size_bound #init augumentation pipline self.aug_seq_noise = iaa.Sequential([ iaa.GaussianBlur(sigma=(0, 0.05)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2 * 255)), iaa.AverageBlur(k=((1, 5), (1, 5))) ]) self.aug_seq_distortion = iaa.Sequential([ iaa.Affine(rotate=(-3, 3), order=[1], scale={ "x": (0.8, 1.1), "y": (0.8, 1.1) }, shear=(-10, 10), mode='constant', cval=(0)), iaa.PerspectiveTransform(scale=(0, 0.02)), ])
def __call__(self, img, labels, mode=None): if random.random() < self.p: noise_aug = iaa.AdditiveGaussianNoise(scale=(0, self.intensity * 255)) img = noise_aug.augment_image(img) return img, labels
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)
from PIL import Image from keras.preprocessing.image import ImageDataGenerator import keras import pandas as pd import numpy as np import glob import matplotlib.pyplot as plt import imgaug.augmenters as iaa import os #preparing data_augmentation generator seq = iaa.SomeOf((0,2), [iaa.GaussianBlur(sigma = (0, 3.0)), iaa.AdditiveGaussianNoise(scale = (0, 10))]) def imgaug(image): return seq.augment_image(image) train_gen = ImageDataGenerator( rescale=1. / 255, vertical_flip = 1, horizontal_flip = 1, rotation_range = 90, brightness_range = [0.4, 1.2], # zca_whitening = True, shear_range = 7.5, width_shift_range = .1, height_shift_range = .1, fill_mode ='constant', preprocessing_function = imgaug) valid_gen = ImageDataGenerator(rescale=1. / 255)
def get_segments(label_list, image_dir, output_dir, max_len, pixel_constant=1): if not os.path.exists(output_dir): os.makedirs(output_dir) base_seq = iaa.Sequential([ sometimes(0.3, iaa.AdditiveGaussianNoise(scale=(0.05, 0.4))), sometimes(0.3, iaa.GammaContrast(gamma=(0.8, 1.2))), sometimes(0.2, iaa.AddToSaturation(value=(-10, 10))), sometimes(0.2, iaa.GaussianBlur(sigma=(0.05, 0.3))), ], random_order=True) seq_origin = iaa.Sequential([ base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom") ]) seq_lr = iaa.Sequential([ iaa.Fliplr(), base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom") ]) seq_ud = iaa.Sequential([ iaa.Flipud(), base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom") ]) seq_rot90 = iaa.Sequential([ # iaa.Rotate(rotate=90), iaa.Rot90(k=1, keep_size=False), base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom") ]) seq_rot180 = iaa.Sequential([ # iaa.Rotate(rotate=180), iaa.Rot90(k=2, keep_size=False), base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom") ]) seq_rot270 = iaa.Sequential([ iaa.Rot90(k=3, keep_size=False), base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="right-bottom") ]) seq_normal = iaa.Sequential([ base_seq, iaa.PadToFixedSize(width=max_len, height=max_len, position="normal") ]) seq_resize = iaa.Sequential([ base_seq, iaa.Resize((max_len, max_len)) # iaa.PadToFixedSize(width=max_len, height=max_len, position="normal") ]) with open(label_list, 'r') as fr: for i, label_path in enumerate(fr): label_path = label_path.strip() name = os.path.split(label_path)[-1] shortname = os.path.splitext(name)[0] # get points content = json.load(open(label_path, 'r')) points = get_points_from_label(content) psoi = Polygon(points[0]) # get image image_path = glob.glob(os.path.join(image_dir, shortname + ".*"))[0] image = cv2.imread(image_path) # resize new_width, new_height = resize_by_scale(image, max_len) seq = iaa.Sequential([ iaa.Resize({ "height": new_height, "width": new_width }), ]) image, psoi = seq(image=image, polygons=psoi) # image_aug, psoi_aug = seq_origin(image=image, polygons=psoi) mask = np.zeros((max_len, max_len), dtype=np.uint8) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_origion.jpg") save_label_path = os.path.join(output_dir, shortname + "_origion.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_lr(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_horizontal.jpg") save_label_path = os.path.join(output_dir, shortname + "_horizontal.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_ud(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_vertical.jpg") save_label_path = os.path.join(output_dir, shortname + "_vertical.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_rot90(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_rot90.jpg") save_label_path = os.path.join(output_dir, shortname + "_rot90.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_rot180(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_rot180.jpg") save_label_path = os.path.join(output_dir, shortname + "_rot180.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_rot270(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_rot270.jpg") save_label_path = os.path.join(output_dir, shortname + "_rot270.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_normal(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_normal.jpg") save_label_path = os.path.join(output_dir, shortname + "_normal.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_aug, psoi_aug = seq_resize(image=image, polygons=psoi) mask_new = convert_to_segment( mask.copy(), psoi_aug.coords.round().astype(np.int), pixel_constant) save_image_path = os.path.join(output_dir, shortname + "_resize.jpg") save_label_path = os.path.join(output_dir, shortname + "_resize.png") cv2.imwrite(save_image_path, image_aug) cv2.imwrite(save_label_path, mask_new) # image_polys_aug = psoi_aug.draw_on_image(image_aug) # cv2.imwrite("output.jpg", image_polys_aug) # exit() if i % 10 == 0: print("{} has done".format(i))
def build_augmentation_pipeline(self, height=None, width=None, apply_prob=0.5): sometimes = lambda aug: iaa.Sometimes(apply_prob, aug) pipeline = iaa.Sequential(random_order=False) cfg = self.cfg if cfg.get('fliplr', False): opt = cfg.get('fliplr', False) if type(opt) == int: pipeline.add(sometimes(iaa.Fliplr(opt))) else: pipeline.add(sometimes(iaa.Fliplr(0.5))) if cfg.get('rotation', False): opt = cfg.get('rotation', False) if type(opt) == int: pipeline.add(sometimes(iaa.Affine(rotate=(-opt, opt)))) else: pipeline.add(sometimes(iaa.Affine(rotate=(-10, 10)))) if cfg.get('motion_blur', False): opts = cfg.get('motion_blur', False) if type(opts) == list: opts = dict(opts) pipeline.add(sometimes(iaa.MotionBlur(**opts))) else: pipeline.add(sometimes(iaa.MotionBlur(k=7, angle=(-90, 90)))) if cfg.get('covering', False): pipeline.add( sometimes( iaa.CoarseDropout(0.02, size_percent=0.3, per_channel=0.5))) if cfg.get('elastic_transform', False): pipeline.add(sometimes(iaa.ElasticTransformation(sigma=5))) if cfg.get('gaussian_noise', False): opt = cfg.get('gaussian_noise', False) if type(opt) == int or type(opt) == float: pipeline.add( sometimes( iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, opt), per_channel=0.5))) else: pipeline.add( sometimes( iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5))) if cfg.get('grayscale', False): pipeline.add(sometimes(iaa.Grayscale(alpha=(0.5, 1.0)))) if cfg.get('hist_eq', False): pipeline.add(sometimes(iaa.AllChannelsHistogramEqualization())) if height is not None and width is not None: if not cfg.get('crop_by', False): crop_by = 0.15 else: crop_by = cfg.get('crop_by', False) pipeline.add( iaa.Sometimes( cfg.cropratio, iaa.CropAndPad(percent=(-crop_by, crop_by), keep_size=False))) pipeline.add(iaa.Resize({"height": height, "width": width})) if cfg.get('gamma', False): pipeline.add( sometimes(iaa.GammaContrast((0.5, 2.0), per_channel=True))) if cfg.get('logcontrast', False): pipeline.add( sometimes(iaa.LogContrast(gain=(0.6, 1.4), per_channel=True))) if cfg.get('allchannelsclahe', False): pipeline.add( sometimes( iaa.AllChannelsCLAHE(clip_limit=(1, 10), per_channel=True))) return pipeline