def chapter_augmenters_piecewiseaffine(): aug = iaa.PiecewiseAffine(scale=(0.01, 0.05)) run_and_save_augseq("piecewiseaffine.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=75) aug = iaa.PiecewiseAffine(scale=(0.01, 0.05)) run_and_save_augseq("piecewiseaffine_checkerboard.jpg", aug, [checkerboard(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=75) scales = np.linspace(0.0, 0.3, num=8) run_and_save_augseq("piecewiseaffine_vary_scales.jpg", [iaa.PiecewiseAffine(scale=scale) for scale in scales], [checkerboard(size=(128, 128)) for _ in range(8)], cols=8, rows=1, quality=75) gridvals = [2, 4, 6, 8, 10, 12, 14, 16] run_and_save_augseq("piecewiseaffine_vary_grid.jpg", [ iaa.PiecewiseAffine(scale=0.05, nb_rows=g, nb_cols=g) for g in gridvals ], [checkerboard(size=(128, 128)) for _ in range(8)], cols=8, rows=1, quality=75)
def main(): image = ia.quokka(size=0.5) print(image.shape) kps = [ ia.KeypointsOnImage( [ ia.Keypoint(x=123, y=102), ia.Keypoint(x=182, y=98), ia.Keypoint(x=155, y=134), #ia.Keypoint(x=255, y=213), #ia.Keypoint(x=375, y=205), #ia.Keypoint(x=323, y=279), #ia.Keypoint(x=265, y=223), #ia.Keypoint(x=385, y=215), #ia.Keypoint(x=333, y=289), #ia.Keypoint(x=275, y=233), #ia.Keypoint(x=395, y=225), #ia.Keypoint(x=343, y=299), ia.Keypoint(x=-20, y=20) ], shape=(image.shape[0], image.shape[1])) ] #kps[0] = kps[0].on(image.shape) print("image shape:", image.shape) augs = [ #iaa.PiecewiseAffine(scale=0), iaa.PiecewiseAffine(scale=0.05), iaa.PiecewiseAffine(scale=0.1), iaa.PiecewiseAffine(scale=0.2) ] #print("original", image.shape) ia.imshow(kps[0].draw_on_image(image)) print("-----------------") print("Random aug per image") print("-----------------") for aug in augs: images_aug = [] for _ in range(16): aug_det = aug.to_deterministic() img_aug = aug_det.augment_image(image) kps_aug = aug_det.augment_keypoints(kps)[0] #img_aug_kps = kps_aug.draw_on_image(img_aug) img_aug_kps = keypoints_draw_on_image(kps_aug, img_aug) img_aug_kps = np.pad(img_aug_kps, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=255) #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0]) images_aug.append(img_aug_kps) #ia.imshow(img_aug_kps) print(aug.name) ia.imshow(ia.draw_grid(images_aug))
def __init__(self): sometimes = lambda aug: iaa.Sometimes(0.3, aug) self.aug = iaa.Sequential( iaa.SomeOf( (1, 5), [ # blur sometimes( iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 1.0)), iaa.MotionBlur(k=3), iaa.AdditiveGaussianNoise(scale=(0, .1 * 255)), iaa.AdditiveLaplaceNoise(scale=(0, .1 * 255)) ])), # color sometimes( iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True)), sometimes( iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True)), sometimes(iaa.Invert(0.25, per_channel=0.5)), sometimes(iaa.Solarize(0.5, threshold=(32, 128))), sometimes(iaa.Dropout2d(p=0.5)), sometimes(iaa.Multiply((0.5, 1.5), per_channel=0.5)), sometimes(iaa.Add((-40, 40), per_channel=0.5)), sometimes(iaa.JpegCompression(compression=(5, 80))), # distort sometimes( iaa.Crop(percent=(0.01, 0.05), sample_independently=True)), sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.01))), sometimes( iaa.Affine( scale=(0.7, 1.3), translate_percent=(-0.1, 0.1), # rotate=(-5, 5), shear=(-5, 5), order=[0, 1], cval=(0, 255), mode=ia.ALL)), sometimes(iaa.ElasticTransformation(alpha=50, sigma=20)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.02))), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01))), sometimes( iaa.OneOf([ iaa.Dropout(p=(0, 0.1)), iaa.CoarseDropout(p=(0, 0.1), size_percent=(0.02, 0.25)) ])), ], random_order=True), random_order=True)
def __init__(self): self.seq = iaa.Sequential( [ iaa.Sometimes( 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.Sometimes( 0.5, iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5)), iaa.Sometimes(0.5, iaa.Add((-10, 10), per_channel=0.5)), iaa.Sometimes(0.5, iaa.AddToHueAndSaturation((-20, 20))), iaa.Sometimes( 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.Sometimes(0.5, iaa.PiecewiseAffine(scale=(0.01, 0.05))), iaa.Sometimes(0.5, iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True)
def img_aug_fun(img): img1 = img seq = iaa.Sequential([ #iaa.Fliplr(0.5), #iaa.Flipud(0.5), iaa.GaussianBlur(sigma=(0, 2.5)), iaa.CropAndPad(px=((-2, 2), (-2, 2), (-2, 2), (-2, 2))), iaa.PiecewiseAffine(scale=(0.01, 0.05)), #iaa.ContrastNormalization((0.75, 1.5)), #iaa.CoarseDropout(0.02, size_percent=0.5), #iaa.AdditiveGaussianNoise(scale=(0, 0.1*255)), #iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.Affine(scale={ "x": (0.95, 1.05), "y": (0.95, 1.05) }, translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-5, 5), shear=(-8, 8)) ]) img_aug = seq.augment_image(img1) return img_aug
def augmentation_sequence(params): if params is None: params = dicto.load_("params.yml") n_augmenters = params.data_augmentation.n_augmenters return iaa.SomeOf(n_augmenters, [ iaa.Grayscale(alpha=(0.0, 1.0)), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AverageBlur(k=(2, 9)), iaa.MedianBlur(k=(3, 9)), iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)), iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)), iaa.Add((-40, 40), per_channel=0.5), iaa.AddElementwise((-40, 40), per_channel=0.5), iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.MultiplyElementwise((0.5, 1.5), per_channel=0.5), iaa.Dropout(p=(0, 0.2), per_channel=0.5), iaa.CoarseDropout(0.05, size_percent=0.1), iaa.Invert(1.0, per_channel=0.5), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.ElasticTransformation(alpha=(0, 5.0), sigma=0.25), iaa.PiecewiseAffine(scale=(0.01, 0.05)), ])
def __init__(self, txt_path, transform=None, target_transform=None): fh = open(txt_path, 'r') imgs = [] for line in fh: line = line.rstrip() words = line.split() # 返回一组数据和标签的列�? imgs.append((words[0], 0)) #合成一个元组添加到列表�? self.imgs = imgs # 最主要就是要生成这个list�?然后DataLoader中给index,通过getitem读取图片数据 self.transform = transform self.target_transform = target_transform with open('point.json', 'r') as f: self.landmarks_record = json.load(f) for k, v in self.landmarks_record.items(): self.landmarks_record[k] = np.array(v) # extract all frame from all video in the name of {videoid}_{frameid} # 近邻搜索得到的结�? with open('found_video.json', 'r') as f: self.video_record = json.load(f) ''' self.data_list = [ '000_0000.png', '001_0000.png' ] * 10000 ''' # predefine mask distortion #############该处有参数################################################################ self.distortion = iaa.Sequential([iaa.PiecewiseAffine(scale=(0.01, 0.1))])
def generator(image_list): for name in image_list: fileName = name name = os.path.join(PATH, name) images = cv2.imread(name) sometimes = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential([ iaa.Flipud(p=0.5), iaa.Fliplr(p=0.5), sometimes(iaa.Pepper(p=0.10)), sometimes(iaa.Salt(p=0.03)), sometimes(iaa.AdditivePoissonNoise(lam=8.0)), sometimes(iaa.JpegCompression(compression=50)), sometimes(iaa.PiecewiseAffine(scale=0.015)), sometimes(iaa.MotionBlur(k=7, angle=0)), sometimes(iaa.MotionBlur(k=5, angle=144)) ], random_order=False) for i in range(10): images_aug = seq.augment_image(images) name = 'aug_' + fileName.split('.')[0] + "-" + str(i) + '.jpg' name = os.path.join(PATH, name) cv2.imwrite(name, images_aug) print(name + " is saved.")
def someAug(): bg = iap.Uniform(16, 18) #Creating a series of augmentations shearXY = iaa.Sequential([ iaa.ShearY(iap.Uniform(-10, 10), cval=bg), iaa.ShearX(iap.Uniform(-10, 10), cval=bg) ]) rotate = iaa.Rotate(rotate=iap.Choice([-30, -15, 15, 30], p=[0.25, 0.25, 0.25, 0.25]), cval=bg) pwAff = iaa.PiecewiseAffine(scale=(0.01, 0.06), cval=bg) affine = iaa.Affine(scale={ "x": iap.Uniform(1.1, 1.2), "y": iap.Uniform(1.1, 1.2) }, cval=bg) noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0, 0.025 * 255)) #Using SomeOf to randomly select some augmentations someAug = iaa.SomeOf(iap.Choice([2, 3, 4], p=[1 / 3, 1 / 3, 1 / 3]), [affine, shearXY, pwAff, rotate, noise], random_order=True) return someAug
def __init__(self, dataset_path, scale=(352, 352), augmentations=True, hasEdg=False): super().__init__() self.augmentations = augmentations self.img_path = dataset_path + '/images/' self.mask_path = dataset_path + '/masks/' #self.edge_path = dataset_path +'/edgs/' self.scale = scale self.edge_flage = hasEdg self.images = [ self.img_path + f for f in os.listdir(self.img_path) if f.endswith('.jpg') or f.endswith('.png') ] self.gts = [ self.mask_path + f for f in os.listdir(self.mask_path) if f.endswith('.png') or f.endswith(".jpg") ] # self.edges = [self.edge_path + f for f in os.listdir(self.edge_path) if f.endswith('.png') or f.endswith(".jpg")] self.flip = iaa.SomeOf( (2, 5), [ iaa.ElasticTransformation(alpha=(0, 50), sigma=(4.0, 6.0)), iaa.PiecewiseAffine( scale=(0, 0.1), nb_rows=4, nb_cols=4, cval=0), iaa.Fliplr(0.5), iaa.Flipud(0.1), # iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Affine(rotate=(-10, 10), scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }), iaa.OneOf([ iaa.GaussianBlur( (0, 1.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur( k=(3, 5) ), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur( k=(3, 5) ), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.contrast.LinearContrast((0.5, 1.5)) ], random_order=True) self.img_transform = transforms.Compose([ transforms.Resize(scale, Image.BILINEAR), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) self.gt_transform = transforms.Compose( [transforms.Resize(scale, Image.BILINEAR), transforms.ToTensor()])
def heavy_augment(image): augment_img = iaa.Sequential( [ iaa.SomeOf( 3, [ #iaa.Affine(scale=(0.5,2.0)), iaa.Affine(shear=20), iaa.Affine(rotate=(-180, 180)), #iaa.Affine(rotate=(-180, 180)), #iaa.Affine(rotate=(-180, 180)), iaa.Affine(translate_percent=0.2), iaa.Fliplr(0.5), iaa.Flipud(0.5), #iaa.Sharpen(alpha=0.5), #iaa.GaussianBlur(sigma=1.0), iaa.CropAndPad(percent=(-0.5, 0.5)), iaa.Add((-40, 40)), iaa.PiecewiseAffine(scale=(0.01, 0.1)) ]) ], random_order=True) image_aug = augment_img.augment_image(image) return image_aug
def __init__(self, dataset_path, batch_size): self.size = 128 self.batch_size = batch_size self.names = {} self.boxes = self.load_bbox(dataset_path) self.count = len(self.boxes) self.num_classes = len(self.names) self.seq = np.arange(self.count) np.random.shuffle(self.seq) sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.aug = iaa.Sequential([ iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.GaussianBlur( sigma=(0, 2.0)), # blur images with a sigma of 0 to 2.0 iaa.GammaContrast(gamma=(0.3, 2.0)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), ]) # Train eval split self.count_train = int(self.count * 0.9) self.count_eval = self.count - self.count_train self.eval_dataset = self.EvalDataset(self)
def aug_image(image, is_infer=False, augment=None): if is_infer: flip_code = augment[0] if flip_code == 1: seq = iaa.Sequential([iaa.Fliplr(1.0)]) elif flip_code == 2: seq = iaa.Sequential([iaa.Flipud(1.0)]) elif flip_code == 3: seq = iaa.Sequential([iaa.Flipud(1.0), iaa.Fliplr(1.0)]) elif flip_code == 0: return image else: seq = iaa.Sequential([ iaa.Affine(rotate=(-15, 15), shear=(-15, 15), mode='edge'), iaa.SomeOf( (0, 2), [ iaa.GaussianBlur((0, 1.5)), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5), iaa.AddToHueAndSaturation( (-5, 5)), # change hue and saturation iaa.PiecewiseAffine(scale=(0.01, 0.03)), iaa.PerspectiveTransform(scale=(0.01, 0.1)) ], random_order=True) ]) image = seq.augment_image(image) return image
def customizedImgAug(input_img): rarely = lambda aug: iaa.Sometimes(0.1, aug) sometimes = lambda aug: iaa.Sometimes(0.25, aug) often = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.Fliplr(0.5), often( iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.1, 0.1), "y": (-0.12, 0) }, rotate=(-10, 10), shear=(-8, 8), order=[0, 1], cval=(0, 255), )), iaa.SomeOf((0, 4), [ rarely(iaa.Superpixels(p_replace=(0, 0.3), n_segments=(20, 200))), iaa.OneOf([ iaa.GaussianBlur((0, 2.0)), iaa.AverageBlur(k=(2, 4)), iaa.MedianBlur(k=(3, 5)), ]), iaa.Sharpen(alpha=(0, 0.3), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 0.5)), rarely( iaa.OneOf([ iaa.EdgeDetect(alpha=(0, 0.3)), 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.0, 0.05), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.05), size_percent=(0.01, 0.05), per_channel=0.2), ]), rarely(iaa.Invert(0.05, per_channel=True)), often(iaa.Add((-40, 40), per_channel=0.5)), iaa.Multiply((0.7, 1.3), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.03))), sometimes(iaa.ElasticTransformation(alpha=(0.5, 1.5), sigma=0.25)), ], random_order=True), iaa.Fliplr(0.5), iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True) ], random_order=True) # apply augmenters in random order output_img = seq.augment_image(input_img) return output_img
def aug_image(image): seq = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Affine(rotate= (-8, 8), shear = (-8, 8), mode='edge'), iaa.SomeOf((0, 2), [ iaa.GaussianBlur((0, 0.3)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5), iaa.AddToHueAndSaturation((-5, 5)), # change hue and saturation iaa.PiecewiseAffine(scale=(0.01, 0.03)), iaa.PerspectiveTransform(scale=(0.01, 0.1)), iaa.JpegCompression(20, 40) ], random_order=True ), iaa.Cutout(nb_iterations=1, size=(0.02, 0.2), squared=False) ]) image = seq.augment_image(image) return image
def aug_transforms(): sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ 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 shear=(-6, 6), # shear by -16 to +16 degrees )), iaa.SomeOf( (0, 5), [ iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images # add noise to pixels iaa.Add((-10, 10)), iaa.OneOf([ iaa.Multiply((0.5, 1.5)), ]), iaa.ContrastNormalization( (0.1, 2.0)), # improve or worsen the contrast sometimes(iaa.PiecewiseAffine( scale=(0.01, 0.05))), # affine transform ], random_order=True) ], random_order=True) return seq
def __init__(self, txt_path, transform=None, target_transform=None): fh = open(txt_path, 'r') imgs = [] for line in fh: line = line.rstrip() words = line.split() # 返回一组数据和标签的列表 imgs.append((words[0], 0)) #合成一个元组添加到列表中 self.imgs = imgs # 最主要就是要生成这个list, 然后DataLoader中给index,通过getitem读取图片数据 self.transform = transform self.target_transform = target_transform #with open('last/point.json', 'r') as f: with open('point.json', 'r') as f: self.dict = json.load(f) with open('point.json', 'r') as f: self.landmarks_record = json.load(f) for k, v in self.landmarks_record.items(): self.landmarks_record[k] = np.array(v) with open('found_video.json', 'r') as f: self.video_record = json.load(f) self.distortion = iaa.Sequential( [iaa.PiecewiseAffine(scale=(0.01, 0.1))]) self.augMethod = 1
def build_seqlist (): seq_list = [] # choose aug method here seq_list.append(iaa.Sequential([iaa.Fliplr(1)])) seq_list.append(iaa.Sequential([iaa.Flipud(1)])) seq_list.append(iaa.Sequential([iaa.Crop(percent=(0, 0.1))])) seq_list.append(iaa.Sequential([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) )])) seq_list.append(iaa.Sequential([iaa.GaussianBlur((0, 3.0))])) seq_list.append(iaa.Sequential([iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))])) seq_list.append(iaa.Sequential([iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))])) seq_list.append(iaa.Sequential([iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5)])) seq_list.append(iaa.Sequential([iaa.Dropout((0.01, 0.1), per_channel=0.5)])) seq_list.append(iaa.Sequential([iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2)])) seq_list.append(iaa.Sequential([iaa.Invert(0.05, per_channel=True)])) seq_list.append(iaa.Sequential([iaa.Add((-10, 10), per_channel=0.5)])) seq_list.append(iaa.Sequential([iaa.Multiply((0.5, 1.5), per_channel=0.5)])) seq_list.append(iaa.Sequential([iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)])) seq_list.append(iaa.Sequential([iaa.Grayscale(alpha=(0.0, 1.0))])) seq_list.append(iaa.Sequential([iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)])) seq_list.append(iaa.Sequential([iaa.PiecewiseAffine(scale=(0.01, 0.05))])) return seq_list
def __init__(self, blur=True, flip=False, rotate=10, shear=10, **kwargs): from imgaug import augmenters as iaa sequence = [] sequence += [ iaa.Scale((INPUT_SIZE, INPUT_SIZE)), iaa.ContrastNormalization((0.75, 1.25)), iaa.AddElementwise((-10, 10), per_channel=0.5), iaa.AddToHueAndSaturation(value=(-20, 20), per_channel=True), iaa.Multiply((0.75, 1.25)), ] sequence += [ iaa.PiecewiseAffine(scale=(0.0005, 0.005)), iaa.Affine(rotate=(-rotate, rotate), shear=(-shear, shear), mode='symmetric'), iaa.Grayscale(alpha=(0.0, 0.5)), ] if flip: sequence += [ iaa.Fliplr(0.5), ] if blur: sequence += [ iaa.Sometimes(0.01, iaa.GaussianBlur(sigma=(0, 1.0))), ] self.aug = iaa.Sequential(sequence)
def get_augmentation_sequence(): sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ sometimes(iaa.Fliplr(0.5)), iaa.Sometimes(0.1, iaa.Add((-70, 70))), 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 )), sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.01))), iaa.Sometimes( 0.1, iaa.SimplexNoiseAlpha(iaa.OneOf( [iaa.Add((150, 255)), iaa.Add((-100, 100))]), sigmoid_thresh=5)), iaa.Sometimes( 0.1, iaa.OneOf([ iaa.CoarseDropout((0.01, 0.15), size_percent=(0.02, 0.08)), iaa.CoarseSaltAndPepper(p=0.2, size_percent=0.01), iaa.CoarseSalt(p=0.2, size_percent=0.02) ])), iaa.Sometimes(0.25, slice_thickness_augmenter) ]) return seq
def imgaug(args): # Number of batches and batch size for this example filename, root, fold_A = args img = cv2.imread(os.path.join(root,filename)) print('image opened ' + os.path.join(root,filename)) batch_size = 4 for i in range(0,batch_size): imageio.imwrite(os.path.join(root, os.path.splitext(filename)[0] + '_' + str(i) + '.jpg'), img) #convert the current image in B into a jpg from png nb_batches = 1 # Example augmentation sequence to run in the background sometimes = lambda aug: iaa.Sometimes(0.4, aug) augseq = iaa.Sequential( [ iaa.PiecewiseAffine(scale=(0.01, 0.01005)) ] ) # Make batches out of the example image (here: 10 batches, each 32 times # the example image) batches = [] for _ in range(nb_batches): batches.append(Batch(images=[img] * batch_size)) #Save images for batch in augseq.augment_batches(batches, background=False): count = 0 for img in batch.images_aug: path = os.path.join(fold_A,root.rsplit('/', 1)[-1], os.path.splitext(filename)[0] + '_' + str(count) + '.jpg') cv2.imwrite(path, img) print('image saved as: ' + path) count +=1
def logic(self, image): for param in self.augmentation_params: self.augmentation_data.append([ str(param.augmentation_value), iaa.PiecewiseAffine(scale=param.augmentation_value). to_deterministic().augment_image(image), param.detection_tag ])
def augment_images(source_dir, dest_dir, cycles): print("-" * 50) print("making", cycles, "rotated and deformed versions of each image in:", source_dir) print("saving new images in: ", dest_dir) makedirs(dest_dir, exist_ok=True) rotate = iaa.Affine(rotate=(0, 360), mode='reflect') deform = iaa.PiecewiseAffine(scale=0.02, mode='reflect') total = 0 for path in sorted(glob.glob(source_dir + "/*.tif")): orig = Image.open(path) base = basename(path) name, __ = splitext(base) norig = np.array(orig) # random rotation (with reflection to fill sided), # followed by piecewise affine deformations # I've had more success with imgaug's PiecewiseAffine than ElasticTransformation, # and results from applytin PiecewiseAffine look to me more like the distortions achieved by the # "elastic deformations" in Simard 2003 paper than results from applying ElasticTransformations print(" making rotated and deformed versions of:", path) for i in range(0, cycles): rad = deform.augment_image(rotate.augment_image(norig)) new_image = Image.fromarray(rad) path = dest_dir + "/rad" + str(i) + "_" + name + ".tif" new_image.save(path) total += 1 print("total new images:", total) print("-" * 50) return
def main(): train = pd.read_csv('train.csv') train_images = train.values[:, 1:] / 255 train_labels = train.values[:, :1].tolist() train_images = np.reshape(train_images, (train_images.shape[0], 28, 28, 1)) train_images, train_labels = get_crops(train_images, train_labels) save_csv(train, train_images, train_labels, 'train_org_crops.csv') rotate_augmenter = iaa.OneOf([ iaa.Affine(rotate=(-15, 15)) ]) train_images, train_labels = augment(train_images, train_labels, rotate_augmenter) save_csv(train, train_images, train_labels, 'train_org_crops_rot.csv') piecewise_affine_augmenter = iaa.OneOf([ iaa.PiecewiseAffine(scale=(0.0, 0.1)) ]) train_images, train_labels = augment(train_images, train_labels, piecewise_affine_augmenter) save_csv(train, train_images, train_labels, 'train_org_crops_rot_pw.csv') sharpen_augmenter = iaa.OneOf([ iaa.Sharpen(alpha=1, lightness=500) ]) train_images, train_labels = augment(train_images, train_labels, sharpen_augmenter) save_csv(train, train_images, train_labels, 'train_org_crops_rot_pw_sharpen.csv')
def train_augmentors(self): shape_augs = [ iaa.Resize((512, 512), interpolation='nearest'), # iaa.CropToFixedSize(width=800, height=800), ] # sometimes = lambda aug: iaa.Sometimes(0.2, aug) input_augs = [ iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # gaussian blur with random sigma iaa.MedianBlur(k=(3, 5)), # median with random kernel sizes iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), ]), 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))), iaa.Sequential([ iaa.Add((-26, 26)), iaa.AddToHueAndSaturation((-20, 20)), iaa.LinearContrast((0.75, 1.25), per_channel=1.0), ], random_order=True), sometimes([ iaa.CropAndPad(percent=(-0.05, 0.1), pad_mode="reflect", pad_cval=(0, 255)), ]), ] return shape_augs, input_augs
def test_TGS_salt_aug_callback(): # image + mask # from 101*101 x, y = load_sample_image() x = x[:5] y = y[:5] # x = np.concatenate([x[:1] for i in range(5)]) # y = np.concatenate([y[:1] for i in range(5)]) import random ia.seed(random.randint(1, 10000)) sometimes = lambda aug: iaa.Sometimes(0.5, aug) # bright add # contrast multiply seq = iaa.Sequential([ iaa.OneOf([ iaa.PiecewiseAffine((0.002, 0.1), name='PiecewiseAffine'), iaa.Affine(rotate=(-20, 20)), iaa.Affine(shear=(-45, 45)), iaa.Affine(translate_percent=(0, 0.3), mode='symmetric'), iaa.Affine(translate_percent=(0, 0.3), mode='wrap'), iaa.PerspectiveTransform((0.0, 0.3)) ], name='affine'), iaa.Fliplr(0.5, name="horizontal flip"), iaa.Crop(percent=(0, 0.3), name='crop'), # image only iaa.OneOf([ iaa.Add((-45, 45), name='bright'), iaa.Multiply((0.5, 1.5), name='contrast')] ), iaa.OneOf([ iaa.AverageBlur((1, 5), name='AverageBlur'), # iaa.BilateralBlur(), iaa.GaussianBlur((0.1, 2), name='GaussianBlur'), iaa.MedianBlur((1, 7), name='MedianBlur'), ], name='blur'), # scale to 128 * 128 iaa.Scale((128, 128), name='to 128 * 128'), ]) activator = ActivatorMask(['bright', 'contrast', 'AverageBlur', 'GaussianBlur', 'MedianBlur']) hook_func = ia.HooksImages(activator=activator) n_iter = 5 tile = [] for idx in range(n_iter): print(idx) seq_det = seq.to_deterministic() image_aug = seq_det.augment_images(x) mask_aug = seq_det.augment_images(y, hooks=hook_func) tile += [image_aug] tile += [mask_aug] tile = np.concatenate(tile) plot.plot_image_tile(tile, title=f'test_image_aug', column=5, )
def affine(self, image_in, image_out): affine = iaa.PiecewiseAffine(scale=self.affine_scale, nb_rows=2, nb_cols=2, deterministic=True) image_in = affine.augment_image(image_in) image_out = affine.augment_image(image_out) return image_in, image_out
def __init__(self): sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.aug_pipe = iaa.Sequential( [ # apply the following augmenters to most images # iaa.Fliplr(0.5), # horizontally flip 50% of all images # iaa.Flipud(0.2), # vertically flip 20% of all images # sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width sometimes(iaa.Affine( # scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) # rotate=(-5, 5), # rotate by -45 to +45 degrees shear=(-5, 5), # shear by -16 to +16 degrees # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) # cval=(0, 255), # if mode is constant, use a cval between 0 and 255 # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf((0, 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.005, 0.012))) # sometimes move parts of the image around ], random_order=True ) ], random_order=True )
def __init__(self, options=_DEFAULT_AUG_OPTIONS): """Initialize the Dataset object Args: options: see below Options: aug_labels: Augment images and labels or just images? data_aug: Are we augmenting 3-channel or 4-channel inputs fliplr: Horizontal flip probability (set to 0. to disable) flipud: Vertical flip probability (set to 0. to disable) rotate: Rotation range (set to (0,0) to disable) random_seed: Random seed used to init augmenters """ # Save copy of options self._options = options # Create and init augmenter using provided options # Restrict order of operations to the one defined here (random_state=False) self._aug = iaa.Sequential(random_state=False) order = 3 backend = 'cv2' mode = 'constant' if options['fliplr'] > 0.: self._aug.append(iaa.Fliplr(options['fliplr'])) if options['flipud'] > 0.: self._aug.append(iaa.Fliplr(options['flipud'])) # NOTE: The combination below does not yield any speed improvement # if options['rotate'] != (0, 0) and options['translate'] > 0. and options['scale'] != (0., 0.): # sc = options['translate'] # self._aug.append(iaa.Affine(rotate=options['rotate'], # translate_percent={"x": (-sc, sc), "y": (-sc, sc)}, # scale={"x": options['scale'], "y": options['scale']}, # mode=mode, order=order, backend=backend)) if options['rotate'] != (0, 0): self._aug.append(iaa.Affine(rotate=options['rotate'], mode=mode, order=order, backend=backend)) if options['translate'] > 0.: sc = options['translate'] self._aug.append(iaa.Affine(translate_percent={"x": (-sc, sc), "y": (-sc, sc)}, mode=mode, order=order, backend=backend)) if options['piecewise_affine'] != (0., 0.): self._aug.append(iaa.PiecewiseAffine(scale=options['piecewise_affine'], mode=mode, order=order)) if options['scale'] != (0., 0.): self._aug.append(iaa.Affine(scale={"x": options['scale'], "y": options['scale']}, mode=mode, order=order, backend=backend)) # BUG BUG this fails is scale factor > 1.0: self._aug.append(iaa.Scale(options['scale'])) self._seed = options['random_seed'] # Augement labels as well, if requested if options['input_channels'] == 4: self._aug_extra_channel=self._aug.deepcopy() if options['aug_labels']: self._aug_labels=self._aug.deepcopy()
def __init__(self, options, data_path=None, sample_list=None, mode='train', encode_label=False): self.path = data_path if data_path != None else options.data_path self.output_scale = options.output_scale self.img_height = options.img_height self.img_width = options.img_width self.mode = mode self.encode_label = encode_label ## data samples sample_path = sample_list if sample_list != None else options.sample_path self.sample_list = open(sample_path).read().splitlines() self.n_class = 3 ia.seed(int(time.time())) self.aug_seq = iaa.OneOf([ iaa.Sequential([ iaa.Fliplr(0.5), iaa.PiecewiseAffine(scale=(0.0, 0.03)), iaa.PerspectiveTransform(scale=(0.0, 0.1)) ], random_order=True), iaa.Sequential([ iaa.Flipud(0.5), iaa.PiecewiseAffine(scale=(0.0, 0.03)), iaa.PerspectiveTransform(scale=(0.0, 0.1)) ], random_order=True), iaa.Sequential([ iaa.Fliplr(0.25), iaa.Dropout([0.05, 0.15]), iaa.PiecewiseAffine(scale=(0.0, 0.03)), iaa.PerspectiveTransform(scale=(0.0, 0.1)) ], random_order=True) ]) # transforms self.to_tensor = transforms.ToTensor() self.normalize = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))