def apply_elastic_transform(data, truth, prev_truth, pred_data, mask, alpha, sigma): rs = np.random.RandomState() vol_et_transform = iaa.ElasticTransformation(alpha=alpha, sigma=sigma, order=1, random_state=rs, deterministic=True, mode="nearest") truth_et_transform = iaa.ElasticTransformation(alpha=alpha, sigma=sigma, order=0, random_state=rs, deterministic=True, mode="nearest") data = vol_et_transform.augment_image(data) truth = truth_et_transform.augment_image(truth) if prev_truth is not None: prev_truth_et_transform = iaa.ElasticTransformation(alpha=alpha, sigma=sigma, order=0, random_state=rs, deterministic=True, mode="nearest") prev_truth = prev_truth_et_transform.augment_image(prev_truth) # NOTE order is 1 like data, check if relevant if pred_data is not None: pred_data_et_transform = iaa.ElasticTransformation(alpha=alpha, sigma=sigma, order=1, random_state=rs, deterministic=True, mode="nearest") pred_data = pred_data_et_transform.augment_image(pred_data) if mask is not None: mask_et_transform = iaa.ElasticTransformation(alpha=alpha, sigma=sigma, order=0, random_state=rs, deterministic=True, mode="nearest") mask = mask_et_transform.augment_image(mask) return data, truth, prev_truth, pred_data, mask
def img_aug(image): result = image case = random.randint(0, 4) # case = 4 if (case == 0): q = random.randint(5, 20) cv2.imwrite('1.jpg', result, [int(cv2.IMWRITE_JPEG_QUALITY), q]) result = cv2.imread('1.jpg') elif (case == 1): blurer = iaa.MedianBlur(k=(3, 5)) result = blurer.augment_image(result) # blur image 2 by a sigma of 3.0 blurer = iaa.ElasticTransformation(alpha=(0.5, 1.0), sigma=(0.1, 0.2)) result = blurer.augment_image( result) # blur image 3 by a sigma of 3.0 too elif (case == 2): q = random.randint(5, 20) cv2.imwrite('1.jpg', result, [int(cv2.IMWRITE_JPEG_QUALITY), q]) result = cv2.imread('1.jpg') blurer = iaa.MedianBlur(k=(3, 5)) result = blurer.augment_image(result) # blur image 2 by a sigma of 3.0 blurer = iaa.ElasticTransformation(alpha=(0.5, 1.0), sigma=(0.1, 0.2)) result = blurer.augment_image( result) # blur image 3 by a sigma of 3.0 too elif (case == 3): blurer = iaa.MedianBlur(k=(3, 5)) result = blurer.augment_image(result) # blur image 2 by a sigma of 3.0 blurer = iaa.ElasticTransformation(alpha=(0.5, 1.0), sigma=(0.1, 0.2)) result = blurer.augment_image(result) # blur image 3 by a sigma of 3.0 q = random.randint(5, 20) cv2.imwrite('1.jpg', result, [int(cv2.IMWRITE_JPEG_QUALITY), q]) result = cv2.imread('1.jpg') elif (case == 4): x = random.randint(-8, 8) y = random.randint(-8, 8) while (x == 0 and y == 0): x = random.randint(-8, 8) y = random.randint(-8, 8) M = np.float32([[1, 0, x], [0, 1, y]]) # 10 result = cv2.warpAffine(result, M, (result.shape[1], result.shape[0])) # 11 # alpha = 0.5 beta = 1 - alpha gamma = 0 result = cv2.addWeighted(image, alpha, result, beta, gamma) return result
def load_augmentations(flip=0.5, blur=0.2, crop=0.5, contrast=0.3, elastic=0.2, affine=0.5): """ Loads and configures data augmenter object Arguements: flip (float) -- probality of horizontal flip crop (float) -- probability of random crop blur (float) -- probability of gaussian blur contrast (float) -- probability of pixelwise color transformation elastic (float) -- probability of elastic distortion affine (float) -- probability of affine transform noise (float) -- probability of one of noises """ aug = iaa.Sequential([ iaa.Fliplr(flip), iaa.Sometimes(crop, iaa.Crop(px=(0, 20))), iaa.Sometimes(blur, iaa.GaussianBlur(sigma=(0.5, 5))), iaa.Sometimes( contrast, iaa.SomeOf((1, 5), [ iaa.GammaContrast(per_channel=True, gamma=(0.25, 1.75)), iaa.LinearContrast(alpha=(0.25, 1.75), per_channel=True), iaa.HistogramEqualization(to_colorspace="HLS"), iaa.LogContrast(gain=(0.5, 1.0)), iaa.CLAHE(clip_limit=(1, 10)) ]), ), iaa.Sometimes( elastic, iaa.OneOf([ iaa.ElasticTransformation(alpha=20, sigma=1), iaa.ElasticTransformation(alpha=200, sigma=20) ])), iaa.Sometimes( affine, iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, rotate=(-30, 30), order=[0, 1])) ]) return aug
def Online_Augmentation(inputs, unsup_preds): seq = iaa.Sequential([ iaa.Affine(scale=(0.8, 1.2), translate_percent=0.03, rotate=4.6), iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.ElasticTransformation( alpha=(28.0, 30.0), sigma=3.5 ) #alpha: the strength of the displacement. sigma: the smoothness of the displacement. ]) #suggestions - alpha:sigma = 10 : 1 inputs_size = inputs.size()[0] inputs_numpy = inputs.squeeze(1).cpu().detach().numpy() unsup_preds_numpy = unsup_preds.cpu().detach().numpy() img_patches_aug = seq.augment_images(inputs_numpy) gt_patches_aug = [] for c in range(args.num_classes): gt_patches_aug.append( seq.augment_images(unsup_preds_numpy[:, c, :, :, :])) gt_patches_aug = np.asarray(gt_patches_aug) return torch.from_numpy(img_patches_aug).unsqueeze( 1).cuda(), torch.from_numpy(gt_patches_aug).permute( 0, 2, 3, 4, 1).contiguous().view(-1, args.num_classes).float().cuda()
def draw_per_augmenter_images(): for path in PATH: num = 0 tag_code = path.split('/')[7] name = glob(path+'/*') for img_name in name: count = 0 file_name = img_name.split('/')[8] image = ndimage.imread(img_name) rows_augmenters = [ ("Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0]]), ("GaussianBlur", [("sigma=%.2f" % (sigma,), iaa.GaussianBlur(sigma=sigma)) for sigma in [2.0]]), ("ElasticTransformation\n(sigma=0.2)", [("alpha=%.1f" % (alpha,), iaa.ElasticTransformation(alpha=alpha, sigma=0.2)) for alpha in [3.0]]) ] count = 0 for (_, augmenters) in rows_augmenters: for _, augmenter in augmenters: aug_det = augmenter.to_deterministic() im = PIL.Image.fromarray(aug_det.augment_image(image)) im = im.resize((64, 64), PIL.Image.ANTIALIAS) if(os.path.exists(TARGET_PATH + tag_code)): filename = TARGET_PATH + tag_code + '/' + str(count) + "_" + file_name im.save(filename) else: os.makedirs(TARGET_PATH + tag_code) filename = TARGET_PATH + tag_code + '/' + str(count) + "_" + file_name im.save(filename) count += 1 print(num)
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 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 __init__(self, batch_size=32, dim=(576, 576), n_channel=1, n_class=1, shuffle=True): self.dim = dim self.batch_size = batch_size self.n_channel = n_channel self.n_class = n_class self.shuffle = shuffle self.image_path = './images/train/images/' self.label_path = './images/train/label/' self.lis_names = [ x.split('/')[-1].split('.')[0] for x in glob.glob(self.image_path + '*.*') ] self.aug = iaa.Sequential([ iaa.ElasticTransformation(alpha=50, sigma=5), iaa.Fliplr(p=0.5), iaa.Flipud(p=0.5) ], random_order=True) # self.aug = None self.on_epoch_end()
def elastic_transformation(img): seq = iaa.Sequential([ iaa.ElasticTransformation(alpha=(random.uniform(0, 0.5), random.uniform(3, 3.5)), sigma=0.25) ]) return seq.augment_image(img)
def __init__(self, df, data_dir, num_categories, image_size, crop_images, augment, normalize_images=True): super().__init__() self.df = df self.data_dir = data_dir self.num_categories = num_categories self.image_size = image_size self.crop_images = crop_images self.augment = augment self.normalize_images = normalize_images self.augmentor = iaa.Sequential([ iaa.Sometimes( 0.5, iaa.OneOf([ iaa.Fliplr(0.5), iaa.Flipud(0.5), iaa.Affine(rotate=90), iaa.Affine(rotate=180), iaa.Affine(rotate=270), iaa.Affine(rotate=(-20, +20)) ])), iaa.Sometimes(0.5, iaa.Affine(shear=(-16, 16))), iaa.Sometimes( 0.5, iaa.ElasticTransformation(alpha=(20.0, 50.0), sigma=5.0)), iaa.Sometimes(0.5, iaa.Multiply((0.7, 1.3), per_channel=True)) ])
def amaugimg(image): #数据增强 image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential([ iaa.SomeOf((0, 4), [ iaa.CoarseDropout( (0.03, 0.15), size_percent=(0.01, 0.05), per_channel=0.2), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.01 * 255), per_channel=0.5), iaa.OneOf([ iaa.GaussianBlur((0, 1.5)), iaa.AverageBlur(k=(2, 7)), ]), iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) ], random_state=True) ]) image = seq.augment_image(image) image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) return image
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 img_aug(img): """Do augmentation with different combination on each training batch """ # without additional operations # according to the paper, operations such as shearing, fliping horizontal/vertical, # rotating, zooming and channel shifting will be apply random = iaa.Sequential([ iaa.SomeOf( (0, 7), [ iaa.Fliplr(1), iaa.Flipud(1), iaa.Affine(shear=(-16, 16)), iaa.Affine(scale={ 'x': (0.8, 1.2), 'y': (0.8, 1.2) }), iaa.Affine(rotate=(-90, 90)), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) #iaa.PerspectiveTransform(scale=(0.01, 0.1)) ]) ]) aug = random.augment_image(img) return aug
def _imgAugumentation(img): resImgs = [] fliplr = [iaa.Fliplr(1), 1] # horizontally flip 50% of all images flipud = [iaa.Flipud(1), 1] # vertically flip 50% of all images crop = [iaa.Crop(percent=(0, 0.2)), 4] # crop images by 0-10% of their height/width blur = [iaa.GaussianBlur((0, 3.0)), 4] # blur images with a sigma between 0 and 3.0 noise = [ iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5), 2 ] # add gaussian noise to images dropout = [iaa.Dropout((0.0, 0.1), per_channel=0.5), 2] # randomly remove up to 10% of the pixels brightness1 = [ iaa.Add((-45, 45), per_channel=0.5), 2 ] # change brightness of images (by -10 to 10 of original value) brightness2 = [iaa.Multiply((0.25, 1.5), per_channel=0.5), 2 ] # change brightness of images (50-150% of original value) contrast = [iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), 2] # improve or worsen the contrast elastic = [iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25), 2] operations = [ elastic, contrast, noise, brightness1, brightness2, blur, dropout, crop ] resImgs.append(img) i = 1 for operation in operations: print(i) imgsForOperation = [] for image in resImgs: imgsForOperation.extend( e_xecuteTransform(operation[0], image, operation[1])) resImgs.extend(imgsForOperation) i += 1 return resImgs
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 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, classes, target_size, grid_shape=(7,7), nbox=2): self.classes = classes self.target_size = target_size self.nbox = nbox self.grid_shape = grid_shape self.augmentation_pipeline = iaa.Sequential([ iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.Sometimes(0.9, iaa.Sequential([ iaa.SomeOf(2, [ iaa.Sometimes(0.5, iaa.Affine(translate_px={"x": (-100, 100), "y": (-100, 100)})), iaa.Sometimes(0.5, iaa.Affine(scale=(0.954, 1.25), )), iaa.Sometimes(0.8, iaa.Affine(rotate=(-5., 5), scale=1.2)), iaa.Crop(percent=(0, 0.25)) ]), iaa.ElasticTransformation(alpha=(0, 0.2)), iaa.Multiply((0.6, 1.5)) ]) ), iaa.Scale({"height":self.target_size[0], "width":self.target_size[1]}) ]) self.scale_pipeline = iaa.Sequential([ iaa.Scale({"height":self.target_size[0], "width":self.target_size[1]}) ])
def seg_seq_augmenter(crop_h=512, crop_w=1024): seg_seq_aug = iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Flipud(0.2), sometimes(iaa.GaussianBlur(sigma=(0.0, 2.0))), # sometimes(iaa.) sometimes(iaa.Sharpen((0.0, 1.0))), sometimes( iaa.SomeOf(2, [ iaa.Affine(scale=[0.5, 0.75, 1.0, 1.25, 1.5]), iaa.Affine(rotate=(-15, 15)), iaa.Affine(translate_px=(-2, 2)), iaa.Affine(shear=(-15, 15)) ])), sometimes(iaa.ElasticTransformation(alpha=10, sigma=5)), sometimes(iaa.contrast.LinearContrast( (0.75, 1.5), per_channel=0.5)), sometimes(iaa.Multiply((0.9, 1.1), per_channel=0.2)), iaa.CropToFixedSize(width=crop_w, height=crop_h), # sometimes(iaa.Dropout([0.05, 0.2])), ], random_order=False) return seg_seq_aug
def get_seq(params): """ Main filters and augmentations for pilotnet data augmentation. """ filters = iaa.SomeOf(params.filters_repeat, [ iaa.ChangeColorspace("BGR"), iaa.ChangeColorspace("GRAY"), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AverageBlur(k=(2, 9)), iaa.MedianBlur(k=(3, 9)), iaa.Add((-40, 40), per_channel=0.5), iaa.Add((-40, 40)), iaa.AdditiveGaussianNoise(scale=0.05 * 255, per_channel=0.5), iaa.AdditiveGaussianNoise(scale=0.05 * 255), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.Multiply((0.5, 1.5)), iaa.MultiplyElementwise((0.5, 1.5)), iaa.ContrastNormalization((0.5, 1.5)), iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), iaa.ElasticTransformation(alpha=(0, 2.5), sigma=0.25), iaa.Sharpen(alpha=(0.6, 1.0)), iaa.Emboss(alpha=(0.0, 0.5)), iaa.CoarseDropout(0.2, size_percent=0.00001, per_channel=1.0), ]) affine = iaa.Affine( rotate=(-7, 7), scale=(0.9, 1.1), translate_percent=dict(x=(-0.05, 0.05)), mode="symmetric", ) return iaa.Sequential([ filters, affine, ])
def imageaug(img_label): seq = iaa.Sequential([ iaa.Fliplr(0.5), # 左右翻转 iaa.Flipud(0.5), # 上下翻转 iaa.Sometimes( 0.3, iaa.Affine( rotate=(-10, 10), # 旋转一定角度 shear=(-10, 10), # 拉伸一定角度(矩形变为平行四边形状) order=0, # order=[0, 1], #使用最邻近差值或者双线性差值 cval=0, # cval=(0, 255), #全白全黑填充 mode='constant' # mode=ia.ALL #定义填充图像外区域的方法 )), # iaa.Crop(percent=(0, 0.1)), iaa.Sometimes(0.3, iaa.ElasticTransformation(alpha=(0, 10.0), sigma=(4.0, 6.0)) ) # 把像素移动到周围的地方 ]) label = img_label[1] imglab_aug = seq.augment_images(img_label) imglab_aug = imglab_aug.transpose((3, 0, 1, 2)) img_aug = imglab_aug[0] img_aug_shape = img_aug.shape img_aug = img_aug.reshape(img_aug_shape[0], 1, img_aug_shape[1], img_aug_shape[2]) lab_aug = imglab_aug[1] lab_aug_shape = lab_aug.shape lab_aug = lab_aug.reshape(lab_aug_shape[0], 1, lab_aug_shape[1], lab_aug_shape[2]) # print(img_aug.shape) lab_aug = np.clip(np.round(lab_aug), np.min(label), np.max(label)) return img_aug, lab_aug
def __init__(self, samples: list = [], crop_size: int = 512, test: bool = False, mean_arr_path: typing.AnyStr = None, transforms: typing.Any = None, phase="train"): self.test = test class_names = ["background", "buildings"] self.nclasses = len(class_names) self.crop_size = crop_size self.input_shape = (self.crop_size, self.crop_size) self.elastic = iaa.ElasticTransformation(alpha=(0.25, 1.2), sigma=0.2) self.samples = samples self.phase = phase self.mean = [127, 127, 127] if not mean_arr_path is None: if not P(mean_arr_path).exists(): raise Exception(f"file not found: {mean_arr_path}") self.mean = np.load(mean_arr_path) self.transforms = transforms self.norm = torchvision.transforms.F.normalize
def make_augmentations(): return iaa.Sequential( [ sometimes(iaa.CoarseDropout(0.1, size_percent=0.2)), sometimes( iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, # scale images to 80-120% of their size, # individually per axis translate_percent={ "x": (-0.1, 0.1), "y": (-0.1, 0.1) }, # translate by -20 to +20 percent (per axis) # rotate by -45 to +45 degrees rotate=(-10, 10), shear=(-5, 5), # shear by -16 to +16 degrees ), ), sometimes(iaa.ElasticTransformation(alpha=10, sigma=1)) ], random_order=True)
def make_rect_mask(w, h, border=10, elastic=False, kernel='linear'): img = np.zeros((h, w, 1), dtype=np.float) h, w, c = img.shape for i in range(border): # gradient border ratio = float(i + 1) / border thick = 1 # linear kernel if kernel == 'none': alpha = 1 elif kernel == 'linear': alpha = ratio elif kernel.find('quad') == 0: alpha = math.pow(ratio, 2) elif kernel == 'cubic': alpha = math.pow(ratio, 3) elif kernel.find('cos') == 0: alpha = (1 - math.cos(ratio * math.pi)) * 0.5 else: alpha = ratio # draw inner block if i == border - 1: thick = -1 alpha = 1 alpha = max(1e-5, alpha) cv2.rectangle(img, (i, i), (w - i, h - i), (alpha, alpha, alpha), thick) if elastic: import imgaug.augmenters as iaa img = iaa.ElasticTransformation(alpha=border, sigma=5).augment_image(img) np.clip(img, 0.0, 1.0) return img
def chapter_examples_segmentation_maps_bool_full(): import imgaug as ia from imgaug import augmenters as iaa import imageio import numpy as np ia.seed(1) # Load an example image (uint8, 128x128x3). image = ia.quokka(size=(128, 128), extract="square") # Create an example mask (bool, 128x128). # Here, we just randomly place a square on the image. segmap = np.zeros((128, 128), dtype=bool) segmap[28:71, 35:85] = True segmap = ia.SegmentationMapOnImage(segmap, shape=image.shape) # Define our augmentation pipeline. seq = iaa.Sequential( [ iaa.Dropout([0.05, 0.2]), # drop 5% or 20% of all pixels iaa.Sharpen((0.0, 1.0)), # sharpen the image iaa.Affine( rotate=(-45, 45)), # rotate by -45 to 45 degrees (affects heatmaps) iaa.ElasticTransformation( alpha=50, sigma=5) # apply water effect (affects heatmaps) ], random_order=True) # Augment images and heatmaps. images_aug = [] segmaps_aug = [] for _ in range(5): seq_det = seq.to_deterministic() images_aug.append(seq_det.augment_image(image)) segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0]) # We want to generate an image of original input images and heatmaps before/after augmentation. # It is supposed to have five columns: (1) original image, (2) augmented image, # (3) augmented heatmap on top of augmented image, (4) augmented heatmap on its own in jet # color map, (5) augmented heatmap on its own in intensity colormap, # We now generate the cells of these columns. # # Note that we add a [0] after each heatmap draw command. That's because the heatmaps object # can contain many sub-heatmaps and hence we draw command returns a list of drawn sub-heatmaps. # We only used one sub-heatmap, so our lists always have one entry. cells = [] for image_aug, segmap_aug in zip(images_aug, segmaps_aug): cells.append(image) # column 1 cells.append(segmap.draw_on_image(image)) # column 2 cells.append(image_aug) # column 3 cells.append(segmap_aug.draw_on_image(image_aug)) # column 4 cells.append(segmap_aug.draw(size=image_aug.shape[:2])) # column 5 # Convert cells to grid image and save. grid_image = ia.draw_grid(cells, cols=5) #imageio.imwrite("example_segmaps_bool.jpg", grid_image) save("examples_segmentation_maps", "bool_full.jpg", grid_image, quality=90)
def img_aug(img_name, base_dir): image = mpimg.imread(Path(base_dir, 'datasets', 'images', img_name)) ran = random.randint(1, 5) if ran == 1: aug = iaa.Sequential([ iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge"), iaa.AddToHueAndSaturation((-60, 60)), iaa.ElasticTransformation(alpha=90, sigma=9), iaa.Cutout() ], random_order=True) image = aug(image=image) elif ran == 2: aug = iaa.BlendAlphaRegularGrid(nb_rows=2, nb_cols=2, foreground=iaa.Multiply(0.0), background=iaa.AveragePooling(8), alpha=[0.0, 0.0, 1.0]) image = aug(image=image) elif ran == 3: image = tf.image.adjust_brightness(image, 0.4) elif ran == 4: image = tf.image.random_flip_left_right(image) image = tf.image.rot90(image) image = tf.image.random_flip_up_down(image) elif ran == 5: image = tf.image.central_crop(image, central_fraction=0.5) return image
def image_aug(image): """ @param image: @return: """ seq = iaa.SomeOf( (1, 3), [ iaa.Crop(px=(0, 16)), # 裁剪 iaa.Multiply((0.7, 1.3)), # 改变色调 iaa.Affine(scale=(0.5, 0.7)), # 放射变换 iaa.GaussianBlur(sigma=(0, 1.5)), # 高斯模糊 iaa.AddToHueAndSaturation(value=(25, -25)), iaa.ChannelShuffle(1), # RGB三通道随机交换 iaa.ElasticTransformation(alpha=0.1), iaa.Grayscale(alpha=(0.2, 0.5)), iaa.Pepper(p=0.03), iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255)), iaa.Dropout(p=(0.03, 0.05)), iaa.Salt(p=(0.03, 0.05)), iaa.AverageBlur(k=(1, 3)), iaa.Add((-10, 10)), iaa.CoarseSalt(size_percent=0.01) ]) seq_det = seq.to_deterministic() image_aug = seq_det.augment_images([image])[0] return image_aug
def imageaug(img_label): seq = iaa.Sequential([ iaa.Fliplr(0.5), # 左右翻转 iaa.Flipud(0), # 上下翻转 iaa.Sometimes( 0, iaa.Affine( rotate=(-10, 10), # 旋转一定角度 shear=(-10, 10), # 拉伸一定角度(矩形变为平行四边形状) order=0, # order=[0, 1], #使用最邻近差值或者双线性差值 cval=0, # cval=(0, 255), #全白全黑填充 mode='constant' # mode=ia.ALL #定义填充图像外区域的方法 )), # iaa.Crop(percent=(0, 0.1)), iaa.Sometimes( 0, iaa.ElasticTransformation(alpha=(0, 10.0), sigma=(4.0, 6.0))), # 把像素移动到周围的地方 iaa.Sometimes(0, iaa.GaussianBlur(sigma=0.1)), iaa.Sometimes(0, iaa.ContrastNormalization(1.1)) ]) label = img_label[1] imglab_aug = seq.augment_images(img_label) img_aug = imglab_aug[0] lab_aug = imglab_aug[1] lab_aug = np.clip(np.round(lab_aug), np.min(label), np.max(label)) return img_aug, lab_aug
def __init__(self, batch_size, input_shape, anchors, num_classes, real_annotation_lines, is_training=True): self.batch_size = batch_size self.input_shape = input_shape self.anchors = anchors self.num_classes = num_classes self.real_annotation_lines = real_annotation_lines self.is_training = is_training sometimes = lambda aug: iaa.Sometimes(0.5, aug) self.aug_pipe = iaa.Sequential([ iaa.SomeOf((0, 5), [ iaa.Sharpen(alpha=1.0, lightness=(0.75, 1.5)), iaa.EdgeDetect(alpha=(0, 0.5)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255)), iaa.OneOf([ iaa.Dropout((0.01, 0.05)), iaa.Salt((0.03, 0.15)), ]), iaa.Add((-10, 10)), iaa.Multiply((0.5, 1.5)), iaa.ContrastNormalization((0.5, 2.0)), sometimes( iaa.ElasticTransformation(alpha=(0.1, 2.0), sigma=0.25)), ], random_order=True) ], random_order=True)
def __init__(self, random_seed: int = None, elastic_transf_sigma: int = 10): self._augmentor = iaa.Sequential( [ iaa.Fliplr(0.5), # horizontal flips iaa.Crop(percent=(0, 0.1)), iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))), iaa.ContrastNormalization((0.999, 1.001)), iaa.Multiply((0.8, 1.2), per_channel=0.2), iaa.Affine( scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent=self._TRANSLATE_PERCENT, rotate=self._ROTATION_RANGE, shear=(-8, 8), mode="symmetric", ), iaa.ElasticTransformation( alpha=200, sigma=elastic_transf_sigma, mode="reflect"), ], random_order=True).to_deterministic() if random_seed: self._augmentor.reseed(random_seed, deterministic_too=True)
def test(self): ia.seed(1) # Load an example image (uint8, 128x128x3). image = ia.quokka(size=(128, 128), extract="square") # Create an example segmentation map (int32, 128x128). # Here, we just randomly place some squares on the image. # Class 0 is the background class. segmap = np.zeros((128, 128), dtype=np.int32) segmap[28:71, 35:85] = 1 segmap[10:25, 30:45] = 2 segmap[10:25, 70:85] = 3 segmap[10:110, 5:10] = 4 segmap[118:123, 10:110] = 5 segmap = ia.SegmentationMapOnImage(segmap, shape=image.shape, nb_classes=1 + 5) # Define our augmentation pipeline. seq = iaa.Sequential( [ iaa.Dropout([0.05, 0.2]), # drop 5% or 20% of all pixels iaa.Sharpen((0.0, 1.0)), # sharpen the image iaa.Affine(rotate=( -45, 45)), # rotate by -45 to 45 degrees (affects heatmaps) iaa.ElasticTransformation( alpha=50, sigma=5) # apply water effect (affects heatmaps) ], random_order=True) # Augment images and heatmaps. images_aug = [] segmaps_aug = [] for _ in range(5): seq_det = seq.to_deterministic() images_aug.append(seq_det.augment_image(image)) segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0]) # We want to generate an image of original input images and heatmaps before/after augmentation. # It is supposed to have five columns: (1) original image, (2) augmented image, # (3) augmented heatmap on top of augmented image, (4) augmented heatmap on its own in jet # color map, (5) augmented heatmap on its own in intensity colormap, # We now generate the cells of these columns. # # Note that we add a [0] after each heatmap draw command. That's because the heatmaps object # can contain many sub-heatmaps and hence we draw command returns a list of drawn sub-heatmaps. # We only used one sub-heatmap, so our lists always have one entry. cells = [] for image_aug, segmap_aug in zip(images_aug, segmaps_aug): cells.append(image) # column 1 cells.append(segmap.draw_on_image(image)) # column 2 cells.append(image_aug) # column 3 cells.append(segmap_aug.draw_on_image(image_aug)) # column 4 cells.append(segmap_aug.draw(size=image_aug.shape[:2])) # column 5 # Convert cells to grid image and save. grid_image = ia.draw_grid(cells, cols=5) ia.show_grid(cells, cols=5)