def generate_lean_samples(): """ generate lean shelf pose samples from frontal shelf pose images :return: """ p = Augmentor.Pipeline(args['img_dir']) # p.rotate(probability=1, max_left_rotation=25, max_right_rotation=25) p.skew_left_right(probability=1, magnitude=random.randint(7, 10) / 10) # p.skew_top_bottom(probability=1, magnitude=random.randint(6, 10) / 10) # p.random_contrast(0.5, min_factor=0.5, max_factor=1.05) # p.random_distortion(probability=0.2, grid_width=2, grid_height=2, magnitude=2) # p.sample(args['sample_num']) p.process()
def augment(sourcePath,labelPath,outputPath,size): p = Augmentor.Pipeline( source_directory=sourcePath ,output_directory=outputPath ) p.ground_truth(labelPath) p.rotate(probability=0.2, max_left_rotation=2, max_right_rotation=2) p.zoom(probability=0.2, min_factor=1.1, max_factor=1.2) p.skew(probability=0.2) p.random_distortion(probability=0.2, grid_width=100, grid_height=100, magnitude=1) p.shear(probability=0.2, max_shear_left=2, max_shear_right=2) p.crop_random(probability=0.2, percentage_area=0.8) p.flip_random(probability=0.2) p.sample(n=size)
def aug(path): p = Augmentor.Pipeline(path) #p.rotate(probability=0.5,max_left_rotation=25,max_right_rotation=10) #p.skew_tilt(probability=0.1,magnitude=1) #p.skew_corner(probability=0.01,magnitude=1) #p.random_distortion(probability=0.01,grid_height=5,grid_width=16,magnitude=8) p.shear(probability=0.01, max_shear_left=15, max_shear_right=15) #p.random_erasing(probability=0.3,rectangle_area=0.5) p.zoom(probability=0.5, min_factor=1, max_factor=1.5) p.random_brightness(probability=0.2, min_factor=0.7, max_factor=1.2) p.random_color(probability=0.2, min_factor=0.7, max_factor=1.2) p.random_contrast(probability=0.2, min_factor=0.7, max_factor=1.2) p.greyscale(probability=0.4) p.sample(11700)
def _get_pipeline(self, path=None): p = Augmentor.Pipeline(path) p.crop_random(probability=0.5, percentage_area=0.7) # p.resize(probability=1.0, width=512, height=512) p.flip_left_right(probability=0.5) p.rotate(probability=0.5, max_left_rotation=10, max_right_rotation=10) p.shear(probability=0.4, max_shear_left=10, max_shear_right=10) # p.random_distortion(probability=0.3, grid_height=5, grid_width=5, magnitude=2) p.skew(probability=0.5) return p
def pro_extend_earsing(imgpath): imgpiple = p.Pipeline(imgpath) # print(imgpath) imgpiple.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10) imgpiple.zoom(probability=0.5, min_factor=1.1, max_factor=1.5) imgpiple.random_erasing(probability=1, rectangle_area=0.5) # imgpiple.crop_random(probability=0.2, percentage_area=0.5) # img = cv2.imread(imgpath) # imgHir = cv2.flip(img,1,dst = None) imgpiple.sample(10) # imgpiple.process() return 0
def test_scale_ground_truth(): standard_image_directory, ground_truth_image_directory = create_temporary_data( ) num_samples = 10 p = Augmentor.Pipeline(standard_image_directory) p.ground_truth(ground_truth_image_directory) p.scale(probability=1, scale_factor=1.4) p.sample(num_samples) generated_files = glob.glob( os.path.join(standard_image_directory, "output/*")) assert (num_samples * 2) == len(generated_files) destroy_temporary_data(standard_image_directory, ground_truth_image_directory) # Do the same for single image lists standard_image_directory, ground_truth_image_directory = create_temporary_data( ) num_samples = 10 p = Augmentor.Pipeline(standard_image_directory) p.scale(probability=1, scale_factor=1.4) p.sample(num_samples) generated_files = glob.glob( os.path.join(standard_image_directory, "output/*")) assert num_samples == len(generated_files) destroy_temporary_data(standard_image_directory, ground_truth_image_directory)
def test_crop_random_ground_truth(): standard_image_directory, ground_truth_image_directory = create_temporary_data( ) num_samples = 10 p = Augmentor.Pipeline(standard_image_directory) p.ground_truth(ground_truth_image_directory) p.add_operation(CropRandom(probability=1, percentage_area=0.5)) p.sample(num_samples) generated_files = glob.glob( os.path.join(standard_image_directory, "output/*")) assert (num_samples * 2) == len(generated_files) destroy_temporary_data(standard_image_directory, ground_truth_image_directory) # Do the same for single images standard_image_directory, ground_truth_image_directory = create_temporary_data( ) num_samples = 10 p = Augmentor.Pipeline(standard_image_directory) p.add_operation(CropRandom(probability=1, percentage_area=0.5)) p.sample(num_samples) generated_files = glob.glob( os.path.join(standard_image_directory, "output/*")) assert num_samples == len(generated_files) destroy_temporary_data(standard_image_directory, ground_truth_image_directory)
def augment_fn(dir_str): p = Augmentor.Pipeline(dir_str) #p.random_brightness(probability = 0.5, min_factor = 0.6, max_factor = 1.0) #p.random_color(probability =0.5, min_factor = 0.2, max_factor = 0.8) #p.random_contrast(probability =0.5, min_factor = 0.5, max_factor = 0.8) p.skew(probability=0.4, magnitude=0.2) p.shear(probability=0.3, max_shear_left=5, max_shear_right=5) p.rotate(probability=0.5, max_left_rotation=15, max_right_rotation=15) p.rotate90(probability=0.5) p.rotate180(probability=0.5) p.rotate270(probability=0.5) #p.random_distortion(probability = 0.2, #grid_height = 10, grid_width =10, magnitude= 2) p.zoom(probability=0.2, min_factor=1.1, max_factor=1.15) p.sample(1000)
def data_augment(path_to_images_dir, data_aug_samples): """ data augmentation :param path_to_images_dir: :param data_aug_samples: :return: """ p = Augmentor.Pipeline(path_to_images_dir) p.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10) p.zoom(probability=1, min_factor=1.1, max_factor=1.5) p.flip_left_right(probability=0.5) p.flip_top_bottom(probability=0.5) p.rotate90(0.5) p.rotate270(0.5) p.sample(data_aug_samples)
def aug_distortion(img_path, gen_num): ''' 图像扭曲 :param img_path: :param gen_num: 生成扭曲图像的数量 :return: ''' p = Augmentor.Pipeline(img_path) p.random_distortion(probability=1, grid_width=8, grid_height=8, magnitude=8) p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) p.zoom_random(probability=0.5, percentage_area=0.9) p.sample(gen_num)
def test_torch_transform(): torchvision = pytest.importorskip("torchvision") red = np.zeros([10, 10, 3], np.uint8) red[..., 0] = 255 red = Image.fromarray(red) g = Augmentor.Operations.Greyscale(1) p = Augmentor.Pipeline() p.greyscale(1) transforms = torchvision.transforms.Compose([p.torch_transform()]) assert red != transforms(red) assert g.perform_operation(red) == transforms(red)
def augmentor(dir, out_dir): p = Augmentor.Pipeline(dir, output_directory=out_dir) p.rotate(probability=0.5, max_left_rotation=20, max_right_rotation=10) p.zoom(probability=0.2, min_factor=1.0, max_factor=1.2) p.random_distortion(probability=0.1, grid_width=4, grid_height=4, magnitude=8) p.flip_left_right(probability=0.5) #p.flip_top_bottom(probability=0.5)# #p.crop_random(probability=0.5, percentage_area=0.9) p.shear(probability=0.5, max_shear_left=10, max_shear_right=10) p.skew(probability=0.5, magnitude=1) p.resize(probability=1, width=224, height=224) p.sample(500)
def get_distortion_pipline(path, num): """ 之前采用 增广策略:随机扭曲 :param path: :param num: :return: """ p = Augmentor.Pipeline(path) p.zoom(probability=0.5, min_factor=1.05, max_factor=1.05) p.random_distortion(probability=1, grid_width=6, grid_height=2, magnitude=3) p.sample(num) return p
def augment(ims_root, max_sample, size=(112, 112)): list_dir = os.listdir(ims_root) for dir_n in list_dir: data_dir = os.path.join(ims_root, dir_n) list_im = os.listdir(data_dir) p = Augmentor.Pipeline(data_dir) if max_sample // 3 < len(list_im) < max_sample: p.flip_left_right(probability=0.75) elif len(list_im) < max_sample // 3: p.flip_left_right(probability=0.3) p.shear(probability=0.5, max_shear_left=20, max_shear_right=20) p.rotate(probability=0.5, max_left_rotation=20, max_right_rotation=20) else: p.resize(probability=1.0, width=size[0], height=size[1]) p.sample(max_sample)
def genrate_images(source, output, number): p = Augmentor.Pipeline(source, output_directory=output) p.flip_random(probability=1) p.flip_left_right(probability= 1.0) p.flip_top_bottom(probability= 1.0) p.crop_random(probability=1.0, percentage_area=0.90) p.rotate_random_90(probability=0.9) p.rotate180(probability=0.8) p.rotate270(probability=0.6) p.flip_random(probability=1) p.rotate(probability=0.5, max_left_rotation=4, max_right_rotation=4) p.shear(probability=0.6, max_shear_left=8, max_shear_right=8 ) p.zoom(probability=0.8, min_factor=1, max_factor=1.3) p.sample(number) shutil.rmtree(output+"/0")
def augment(input_path, n): """ Apply a set of augmentation operations """ p = Augmentor.Pipeline(input_path) # Point to a directory containing ground truth data. # Images with the same file names will be added as ground truth data # and augmented in parallel to the original data. #p.ground_truth("/path/to/ground_truth_images") # Add operations to the pipeline as normal: p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) p.flip_left_right(probability=0.5) p.zoom_random(probability=0.5, percentage_area=0.8) p.flip_top_bottom(probability=0.5) p.sample(n)
def test_keras_generator_from_disk(): batch_size = random.randint(1, 50) width = 80 height = 80 tmpdir = tempfile.mkdtemp() tmps = create_multiple_images(tmpdir, 10, (width, height)) p = Augmentor.Pipeline(tmpdir) assert len(p.augmentor_images) == len(tmps) p.rotate(probability=0.5, max_left_rotation=5, max_right_rotation=5) p.flip_left_right(probability=0.333) p.flip_top_bottom(probability=0.5) g = p.keras_generator(batch_size=batch_size, image_data_format="channels_last") X, y = next(g) assert len(X) == batch_size assert len(X) == batch_size assert len(X) == len(y) assert np.shape(X) == (batch_size, width, height, 3) # Call next() more than the total number of images in the pipeline for i in range(20): X, y = next(g) assert len(X) == batch_size assert len(X) == batch_size assert len(X) == len(y) assert np.shape(X) == (batch_size, width, height, 3) g2 = p.keras_generator(batch_size=batch_size, image_data_format="channels_first") X2, y2 = next(g2) assert len(X2) == batch_size assert len(X2) == len(y2) assert np.shape(X2) == (batch_size, 3, width, height) # Finally remove the directory (and everything in it) as mkdtemp does # not delete itself after closing automatically shutil.rmtree(tmpdir)
def __init__(self, img_size=256): super(VHFlipRotateCrop, self).__init__(img_size) self.solt_pipeline = solt.Stream([ slt.Flip(p=self.probablity, axis=0), slt.Flip(p=self.probablity, axis=1), slt.Rotate(angle_range=(0, 20)), slt.Crop(224, crop_mode="r"), ]) self.albumentations_pipeline = albu.Compose([ albu.VerticalFlip(p=self.probablity), albu.HorizontalFlip(p=self.probablity), albu.Rotate(limit=(0, 20), p=self.probablity, border_mode=cv2.BORDER_CONSTANT, value=0), albu.RandomCrop(height=224, width=224), ToTensor(normalize={ "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225] }), ]) self.torchvision_pipeline = tv_transforms.Compose([ tv_transforms.RandomHorizontalFlip(p=self.probablity), tv_transforms.RandomRotation(degrees=(0, 20)), tv_transforms.RandomCrop(224), tv_transforms.ToTensor(), tv_transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) _augm_ppl = augmentor.Pipeline() _augm_ppl.flip_top_bottom(probability=self.probablity) _augm_ppl.flip_left_right(probability=self.probablity) _augm_ppl.rotate(probability=self.probablity, max_left_rotation=0, max_right_rotation=20) _augm_ppl.crop_random(probability=1, percentage_area=224 / float(self.img_size)) self.augmentor_pipeline = tv_transforms.Compose([ _augm_ppl.torch_transform(), tv_transforms.transforms.ToTensor(), tv_transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ])
def __init__(self, path, distortion=False, flip_horizontal=False, flip_vertical=False, random_crop=False, random_erasing=False, rotate=False, resize=False, skew=False, shear=False, brightness=False, contrast=False, color=False, target_width=299, target_height=299): self.path = path self.categories = sorted(os.listdir(self.path)) self.categories_folder = [ os.path.abspath(os.path.join(self.path, i)) for i in self.categories ] # options self.distortion = distortion self.flip_horizontal = flip_horizontal self.flip_vertical = flip_vertical self.random_crop = random_crop self.random_erasing = random_erasing self.rotate = rotate self.resize = resize self.skew = skew self.shear = shear self.brightness = brightness self.contrast = contrast self.color = color self.target_width = target_width self.target_height = target_height # Create a pipeline for each class self.pipelines = {} for folder in self.categories_folder: print("Folder %s:" % folder) self.pipelines[os.path.split(folder)[1]] = ( Augmentor.Pipeline(folder)) print("\n----------------------------\n") for p in self.pipelines.values(): print("Class %s has %s samples." % (p.augmentor_images[0].class_label, len(p.augmentor_images)))
def img_distortion(img_path): for file in os.listdir(img_path): if file == '.DS_Store': os.remove(img_path + '/' + file) else: pass p = Augmentor.Pipeline(img_path) p.rotate(probability=0.5, max_left_rotation=5, max_right_rotation=5) # p.zoom(probability=0.1, min_factor=0.5, max_factor=1.0) p.random_distortion(probability=1, grid_width=2, grid_height=2, magnitude=2) # p.flip_left_right(probability=0.2) # p.flip_top_bottom(probability=0.2) p.sample(2000)
def augmentate_data_from_path(): copy_covid_dataset() input_path = Paths.DATASET_BASE_PATH + 'data/augmented-orig/' output_path = Paths.DATASET_BASE_PATH + 'data/augmented/' p = Augmentor.Pipeline( source_directory=input_path, output_directory=output_path) p.rotate(probability=1, max_left_rotation=0.15, max_right_rotation=0.15) p.flip_left_right(probability=0.5) p.zoom_random(probability=0.2, percentage_area=0.8) p.status() p.sample(1000) p.process()
def get_Augmentor(path,batch_size,rotate=True,color=True,flip_ud=True): p = Augmentor.Pipeline(path, output_directory="/tmp/") p.skew_tilt(probability=0.5, magnitude=0.5) p.random_distortion(probability=0.5, grid_width=4, grid_height=4, magnitude=2) if rotate: p.rotate90(probability=0.3) p.rotate270(probability=0.3) p.rotate(probability=0.3, max_left_rotation=10, max_right_rotation=10) if flip_ud: p.flip_top_bottom(probability=0.3) p.flip_left_right(probability=0.3) if not color: p.greyscale(probability=1.0) g=p.keras_generator(batch_size=batch_size) return g
def augment_folder(path, generated_count=50, pixels_mean=None): # temporary margin to make sure the object won't oversize during deformation temp_margin = 2.0 # margin after operations = final_margin * size final_margin = 0.05 # temporary increases size to make erosion softer erosion_scaling = 4 # detect images p = Augmentor.Pipeline(path, output_directory="augment") # get dimension of first image im_width = 100 im_height = 100 if len(p.augmentor_images) > 0: first_path = p.augmentor_images[0].image_path im = Image.open(first_path) im_width = im.width im_height = im.height p.random_distortion(1, 2, 2, 8) p.add_operation( AutoresizeImage(probability=1, max_horizontal_loss=0.4, max_vertical_loss=0.2)) p.add_operation( ExpandImage(probability=1, width=int(temp_margin * im_width), height=int(temp_margin * im_height))) p.rotate(probability=1, max_left_rotation=7, max_right_rotation=7) p.skew(1, 0.2) # p.shear(1, 5, 5) p.add_operation(AutoCropImage(probability=1, margin=final_margin)) p.resize(1, int(erosion_scaling * im_width), int(erosion_scaling * im_height)) p.add_operation(SmoothImage(probability=1, blur=2, threshold=128)) pixels_mean_scaled = None if pixels_mean is not None: pixels_mean_scaled = pixels_mean * erosion_scaling * erosion_scaling * 255 # max of pickle data pixel = 1 p.add_operation( ErosImage(probability=1, max_erosion=3, max_dilation=4, pixels_mean=pixels_mean_scaled)) p.resize(1, int(im_width), int(im_height)) p.sample(generated_count)
def __getitem__(self, index): rng = random image1 = rng.choice(self.dataset.imgs) # get image from same class label = None if index % 2 == 1: label = 1.0 while True: image2 = rng.choice(self.dataset.imgs) if image1[1] == image2[1]: break # get image from different class else: label = 0.0 while True: image2 = rng.choice(self.dataset.imgs) if image1[1] != image2[1]: break image1 = Image.open(image1[0]) image2 = Image.open(image2[0]) image1 = image1.convert('L') image2 = image2.convert('L') # apply transformation on the fly if self.augment: p = Augmentor.Pipeline() p.rotate(probability=0.5, max_left_rotation=15, max_right_rotation=15) p.random_distortion( probability=0.5, grid_width=6, grid_height=6, magnitude=10, ) trans = transforms.Compose([ p.torch_transform(), transforms.ToTensor(), ]) else: trans = transforms.ToTensor() image1 = trans(image1) image2 = transforms.ToTensor()(image2) y = torch.from_numpy(np.array([label], dtype=np.float32)) return (image1, image2, y)
def trainingset_augmentation(data_path, output_width, output_height, samples=100, ground_truth_path=None, output_dir='output', ground_truth_output_dir=None): import os import Augmentor # create pipeline p = Augmentor.Pipeline(data_path, output_dir) if ground_truth_path: p.ground_truth(ground_truth_path) # add color operation # p.random_contrast(probability=1, min_factor=1, max_factor=1) # p.random_brightness(probability=1, min_factor=1, max_factor=1) # p.random_color(probability=1, min_factor=1, max_factor=1) # add shape operation p.rotate(probability=1, max_left_rotation=25, max_right_rotation=25) p.crop_random(probability=1, percentage_area=0.5, randomise_percentage_area=False) p.random_distortion(probability=0.8, grid_width=30, grid_height=30, magnitude=1) p.skew(probability=0.5) p.shear(probability=0.4, max_shear_left=5, max_shear_right=5) p.resize(probability=1, width=output_width, height=output_height) # generate p.sample(samples) # move ground truth to other folder if ground_truth_output_dir is not None: import shutil from dataset import get_all_file if not os.path.exists(ground_truth_output_dir): os.mkdir(ground_truth_output_dir) # read all images path imgs_path = get_all_file(output_dir) num = np.int32(len(imgs_path) / 2) # move gt gt_paths = imgs_path[num:] for path in gt_paths: shutil.move(path, ground_truth_output_dir)
def getdata(train_path, val_path, test_path): # create a data generator image_size = config.IMAGE_SIZE datagen_batch_size = config.batch_size p = Augmentor.Pipeline() p.flip_left_right(probability=0.5) p.rotate(probability=1, max_left_rotation=5, max_right_rotation=5) p.zoom_random(probability=0.5, percentage_area=0.95) p.random_distortion(probability=0.5, grid_width=2, grid_height=2, magnitude=8) p.random_color(probability=1, min_factor=0.8, max_factor=1.2) p.random_contrast(probability=1, min_factor=0.8, max_factor=1.2) p.random_brightness(probability=1, min_factor=0.8, max_factor=1.2) p.random_erasing(probability=0.5, rectangle_area=0.2) data_generator = ImageDataGenerator( preprocessing_function=p.keras_preprocess_func()) # test data shouldn't be augmented test_datagen = ImageDataGenerator() train_it = data_generator.flow_from_directory( train_path, class_mode='categorical', batch_size=datagen_batch_size, target_size=(image_size, image_size)) # load and iterate validation dataset val_it = data_generator.flow_from_directory(val_path, class_mode='categorical', batch_size=datagen_batch_size, target_size=(image_size, image_size)) # load and iterate test dataset test_it = test_datagen.flow_from_directory(test_path, class_mode='categorical', batch_size=datagen_batch_size, target_size=(image_size, image_size)) return train_it, val_it, test_it
def augmentation_new_image(input_dir, output_dir, output_number): import Augmentor # pip install Augmentor -i https://pypi.tuna.tsinghua.edu.cn/simple p = Augmentor.Pipeline(input_dir, output_directory = output_dir) p.zoom(probability=0.1, min_factor=1.1, max_factor=1.3) p.flip_left_right(probability=0.1) p.rotate(probability=0.2, max_left_rotation=15, max_right_rotation=16) p.shear(probability=0.2, max_shear_left=10, max_shear_right=10) p.skew(probability=0.1, magnitude=0.6) p.skew_tilt(probability=0.2, magnitude=0.6) p.random_distortion(probability=0.3, grid_height=4, grid_width=4, magnitude=4) # p.random_distortion(probability=0.2, grid_height=4, grid_width=4, magnitude=4) # p.rotate90(probability=1) p.sample(output_number)
def pics_aug(save_img_dir): # 确定原始图像存储路径以及掩码文件存储路径 p = Augmentor.Pipeline(save_img_dir) p.ground_truth(aug_testlabels) # 图像旋转: 按照概率0.8执行,最大左旋角度10,最大右旋角度10 p.rotate(probability=0.4, max_left_rotation=1, max_right_rotation=1) # 图像左右互换: 按照概率0.5执行 p.flip_left_right(probability=0.5) ''' # 图像放大缩小: 按照概率0.8执行,面积为原始图0.85倍 p.zoom_random(probability=0.3, percentage_area=0.9) ''' # 最终扩充的数据样本数 p.sample(100) '''
def augment(imgdir, sample_n): p = Augmentor.Pipeline(imgdir) # 加入变换操作和概率 # 旋转 & 放大 & 镜像 p.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10) p.zoom(probability=0.5, min_factor=1.1, max_factor=1.5) p.flip_left_right(probability=0.5) p.random_brightness(0.8, 0.8, 1.2) p.scale(0.4, 1.2) p.black_and_white(0.1) p.greyscale(0.5) p.random_erasing(0.9, 0.5) # p.random_distortion(0.3, 30, 30, 5) # p.skew(0.1) # 生成数据在 imgdir/output/ 下 p.sample(sample_n)
def generate_samples(src, dst, num_samples, seed): p = Augmentor.Pipeline(source_directory=src, output_directory=dst) p.set_seed(seed=seed) p.rotate(probability=0.5, max_left_rotation=5, max_right_rotation=5) # p.zoom(probability=0.5, min_factor=1.0, max_factor=1.2) # p.crop_random(probability=0.1, percentage_area=0.9) p.random_distortion(probability=0.5, grid_height=10, grid_width=10, magnitude=2) p.skew_left_right(probability=0.5, magnitude=0.1) p.skew_top_bottom(probability=0.5, magnitude=0.1) p.resize(probability=1.0, width=512, height=512) p.sample(num_samples) del p