def __init__(self, size=384, random_erase_p=0.0): def very_rare(aug): return iaa.Sometimes(0.1, aug) def rare(aug): return iaa.Sometimes(0.3, aug) def sometimes(aug): return iaa.Sometimes(0.5, aug) def often(aug): return iaa.Sometimes(0.8, aug) self.random_erase_p = random_erase_p self.seq = iaa.Sequential( [ rare(iaa.HorizontalFlip()), # iaa.Affine( # #scale={"x": (.5,.5), "y": (.5,.5)}, # scale images to 80-120% of their size, individually per axis # scale={"x": (0.8,1.0), "y": (0.8, 1.0)}, # 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=(-16, 16), # shear by -16 to 16 degrees # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) # cval=0, # if mode is constant, use a cval between 0 and 255 # mode="constant", # use any of scikit-image's warping modes (see 2nd image from the top for examples) # ), ], random_order=True) self.size = size
def __init__(self, window_size=None, window_expand=2, down_sample=4, equalize=False, morphology=False, pixel_size=10, sigma=1): super().__init__(window_size=window_size, window_expand=window_expand, down_sample=down_sample, equalize=equalize, morphology=morphology, pixel_size=pixel_size) self.sigma = sigma self.aug_seq = aug.Sequential([ aug.HorizontalFlip(0.5), aug.VerticalFlip(0.5), aug.Sometimes(0.5, aug.GaussianBlur(sigma=(0, 0.2))), aug.LinearContrast((0.8, 1.2)), aug.AdditiveGaussianNoise(scale=(0.0, 0.05 * 255)), aug.Multiply((0.5, 1.0)), aug.Affine(scale=(0.7, 1), translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, rotate=(-5, 5)) ], random_order=True)
def dictHorizontalFlip(baseImageListFunc, baseMaskListFunc, fullImageListFunc, segmapListFunc): print('Horizontal Flip, starting number of images:', len(segmapListFunc)) horizontalFlip_x00percent = 1 horizontalFlip = iaa.HorizontalFlip(1) alteredImageListFunc, alteredMaskListFunc = expandList(baseImageListFunc, baseMaskListFunc, horizontalFlip_x00percent) (alteredImageListFunc, alteredMaskListFunc) = horizontalFlip(images=alteredImageListFunc, segmentation_maps=alteredMaskListFunc) fullImageListFunc.extend(alteredImageListFunc) segmapListFunc.extend(alteredMaskListFunc) return fullImageListFunc, segmapListFunc
def augment_operations(crops, cropped_img, cropped_dpth, resized_img, resized_dpth, augmented_imgs, augmented_gts): for i in range(crops): crop_img, col_cj, row_ci = Intrinsic.random_crop( np.asarray(cropped_img), (224, 304)) resize_img = Image.fromarray(crop_img.astype(np.uint8), 'RGB').resize( (304, 228), Image.BILINEAR) crop_dpth = np.asarray(cropped_dpth)[row_ci:row_ci + 224, col_cj:col_cj + 304] resize_dpth = Image.fromarray(crop_dpth).resize((160, 128), Image.NEAREST) resize_dpth = np.expand_dims(resize_dpth, axis=3) augmented_imgs.append(np.asarray(resize_img)) augmented_gts.append(np.asarray(resize_dpth)) angle = np.random.randint(-10, 10) rotate = iaa.Affine(rotate=angle) rotated_img = rotate.augment_image(np.asarray(cropped_img)) rotated_img = Image.fromarray(rotated_img.astype(np.uint8), 'RGB').resize( (304, 228), Image.BILINEAR) rotated_dpth = rotate.augment_image(np.asarray(cropped_dpth)) rotated_dpth = Image.fromarray(rotated_dpth).resize((160, 128), Image.NEAREST) rotated_dpth = np.expand_dims(rotated_dpth, axis=3) augmented_imgs.append(np.asarray(rotated_img)) augmented_gts.append(np.asarray(rotated_dpth)) colorjitter = iaa.AddToHueAndSaturation((-60, 60)) enhanced_img = colorjitter.augment_image(np.asarray(resized_img)) augmented_imgs.append(np.asarray(enhanced_img)) augmented_gts.append(np.asarray(resized_dpth)) swapped_img = np.array(resized_img) red = swapped_img[:, :, 0].copy() green = swapped_img[:, :, 1].copy() swapped_img[:, :, 1] = red swapped_img[:, :, 0] = green augmented_imgs.append(np.asarray(swapped_img)) augmented_gts.append(np.asarray(resized_dpth)) brghtperturbator = iaa.Multiply( (0.8, 1.2), per_channel=0.2) #50-150% of original value perturbed_img = brghtperturbator.augment_image(np.asarray(resized_img)) augmented_imgs.append(np.asarray(perturbed_img)) augmented_gts.append(np.asarray(resized_dpth)) flp2 = iaa.HorizontalFlip(1) flipped_img2 = flp2.augment_image(np.asarray(resized_img)) flipped_dpth2 = flp2.augment_image(np.asarray(resized_dpth)) augmented_imgs.append(np.asarray(flipped_img2)) augmented_gts.append(np.asarray(flipped_dpth2))
def __init__(self, width=512, height=512, phase='train'): super(ImgAugment, self).__init__() self.phase = phase self.width = width self.height = height self.transform_train = iaa.Sequential([ iaa.HorizontalFlip(), ]) self.transform_test = iaa.Sequential( [iaa.size.Resize(size=(width, height))]) self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ])
def train_augs(self, ): return iaa.Sequential([ iaa.HorizontalFlip(0.5), iaa.Affine( scale={ "x": (0.9, 1.1), "y": (0.9, 1.1) }, translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-5, 5), cval=255, ), ])
def __init__(self, args): if isinstance(args.data, list): ds = data.sets.LMDBDataset(args.data, args.channels, 90, args.raw_image) if len(args.channels) == 0: args.channels = ds.channels_of_interest img_shape = (len(args.channels), 90, 90) channel_shape = (90, 90) else: ds = UnsupervisedFMNIST("data/datasets/", train=True, download=True, transform=unsqueeze()) channel_shape = ds.data.shape[1:] img_shape = [1] + list(channel_shape) augs = [] if args.func not in ["embed"]: ia_seq = iaa.Sequential([ iaa.Affine(rotate=(-160, 160), scale=(0.5, 1.5), translate_percent=(-0.1, 0.1)), iaa.HorizontalFlip(), iaa.VerticalFlip() ]) augs.append(IASeq(ia_seq)) augs += [ torch.Tensor, data.transformers.StandardScale(), ] augmenter = transforms.Compose(augs) self.img_shape = img_shape self.channel_shape = channel_shape self.ds = ds super().__init__(ds, batch_size=args.batch_size, shuffle=True, drop_last=False, num_workers=args.workers, collate_fn=augmenter)
def gen_batches_h(files, scale): skip = 0 l = len(files) for img in files: try: skip += 1 print(f" {skip} of {l}") raw_img = Image.open(img) new_size = (int(raw_img.width / scale), int(raw_img.height / scale)) raw_img.thumbnail(new_size) raw_img = np.array(raw_img) if raw_img.shape[0] / raw_img.shape[1] < 1: raw_img.transpose((1, 0, 2)) hfliper = iaa.HorizontalFlip() hfliped = hfliper(images=[raw_img]) yield (img.split("\\")[-1], [raw_img, hfliped[0]]) except Exception as e: print(repr(e))
def _augment_geometric( d_scale=0.05, d_translate=0.02, d_rotate=3, d_shear_x=5, d_shear_y=1, ): return iaa.Sequential( [ iaa.HorizontalFlip(0.5), # iaa.Sometimes(0.5, iaa.Affine( # scale=(1 / (1 + d_scale), (1 + d_scale)), # translate_percent=dict( # x=(-d_translate, d_translate), # y=(-d_translate, d_translate), # ), # rotate=(-d_rotate, d_rotate), # )), # iaa.Sometimes(0.5, iaa.ShearX(shear=(-d_shear_x, d_shear_x))), # iaa.Sometimes(0.5, iaa.ShearY(shear=(-d_shear_y, d_shear_y))), ], random_order=True)
def train_augs(self,): return iaa.Sequential([ iaa.HorizontalFlip(0.5), iaa.VerticalFlip(0.5), iaa.OneOf([ iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)), iaa.Noop() ]), iaa.OneOf([ iaa.Grayscale(alpha=(0.0, 1.0)), iaa.Noop() ]), iaa.OneOf([ iaa.Noop(), iaa.GammaContrast((0.5, 1.0)) ]), iaa.Affine( scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-5, 5), cval=255, ), ])
def _draw_samples(self, x, y): return np.array([90, 180, 270]) def __repr__(self): return self.__str__() def __str__(self): return "Normal(loc=%s, scale=%s)" % (0.3) seq = iaa.Sequential( [ iaa.Fliplr(0.2), iaa.VerticalFlip(0.2), iaa.HorizontalFlip(0.2), iaa.Sometimes( 0.1, iaa.GaussianBlur(sigma=(0, 3)), # iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # iaa.LinearContrast((0.75, 1.5)), ), iaa.Affine( scale=(0.7, 1.2), rotate=(-6, 6), # shear=(-8, 8) ) ], random_order=True) seq_scale = iaa.Sequential( iaa.Affine(scale=FixScale(0.4, 0.9), rotate=FixRotate()))
def test_HorizontalFlip(): aug = iaa.HorizontalFlip(0.5) assert isinstance(aug, iaa.Fliplr) assert np.allclose(aug.p.p.value, 0.5)
def transform(self, image: np.ndarray, target: str, condition: int) -> Tuple[torch.Tensor, torch.Tensor, int]: """Transforms and normalizes the data. If in training mode the data is augmentated. Args: image (np.ndarray): Image to transform target (str): Training target condition (int): Condition Returns: Tuple[torch.Tensor, torch.Tensor, int]: Augmented image, target and condition """ # Resize resize = iaa.Resize({"height": 224, "width": 224}) image = resize.augment_image(image) # Random horizontal flipping and erase if self.train: if random.random() > 0.5: # flip image flip = iaa.HorizontalFlip(1.0) image = flip.augment_image(image) # flip class if target == "a": target = "d" elif target == "d": target = "a" # flip condition if condition == 2: condition = 4 elif condition == 4: condition = 2 #imgaug seq = iaa.Sequential([ iaa.Sometimes(0.5, iaa.Affine(rotate=(-15, 15))), iaa.Sometimes(0.3, iaa.EdgeDetect(alpha=(0.3, 0.8))), iaa.Sometimes(0.5, iaa.MotionBlur(k=iap.Choice([3, 5, 7]))), iaa.OneOf([ iaa.Dropout(p=(0, 0.3), per_channel=0.5), iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.09)) ]), iaa.Sometimes(0.5, iaa.AllChannelsCLAHE(clip_limit=(1, 10))) ]) image = seq.augment_image(image) # Transform to tensor image = TF.to_tensor(image) # Transform to one hot encoding target = torch.tensor(self.target_dict[target]) #normalize image to fit pretrained vgg model normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) image = normalize(image) return image, target, condition
def test_returns_fliplr(self): aug = iaa.HorizontalFlip(0.5) assert isinstance(aug, iaa.Fliplr) assert np.allclose(aug.p.p.value, 0.5)
def img2xy(self, ds_name, img_path, gal_path, gpr_path, row_inc, col_inc, scale, h_flip, v_flip, blur, noise, augment=0, cut_pp=None): if ds_name in self.gal_cache: gal = self.gal_cache[ds_name] else: gal = make_fake_gal( Gpr(gpr_path)) if gal_path == '' else Gal(gal_path) self.gal_cache[ds_name] = gal if img_path in self.img_cache: idxs, imgs, df, scales = self.img_cache[img_path] else: idxs, imgs, df, scales = self.img2x( img_path, gal, Gpr(gpr_path)) # imgs: (N, 1, h, w) self.img_cache[img_path] = idxs, imgs, df, scales if (imgs is None) or (df is None): print(f'{img_path} failed.') return None, None, None, None h, w = imgs.shape[-2], imgs.shape[-1] xs, ys, isori = [], [], [] for (img_path, b, c), img, block_df in zip(idxs, imgs, df): nrows = gal.header[f'Block{b}'][Gal.N_ROWS] ncols = gal.header[f'Block{b}'][Gal.N_COLS] kpts = KeypointsOnImage([ Keypoint(x=block_df.loc[1, 1]['X'], y=block_df.loc[1, 1]['Y']), Keypoint(x=block_df.loc[nrows, 1]['X'], y=block_df.loc[nrows, 1]['Y']), Keypoint(x=block_df.loc[nrows, ncols]['X'], y=block_df.loc[nrows, ncols]['Y']), Keypoint(x=block_df.loc[1, ncols]['X'], y=block_df.loc[1, ncols]['Y']), ], shape=(h, w)) # bsa bsa = BlockSizeAugmentor((nrows, ncols), cut_pp) img_ = [] kpts_ = kpts.copy() for im in img: im, kpts, _ = bsa.augment(im, kpts_.to_xy_array(), row_inc, col_inc) img_.append(im) img = np.stack(img_).astype('uint8') coord = self.to_Lcoord(kpts.to_xy_array()) if self.check_out_of_bounds(img.shape[1:], coord): continue img = np.moveaxis(img, 0, -1) # (c, h, w) -> (h, w, c) # h_flip if h_flip: aug_func = aug.HorizontalFlip(1) img, kpts = aug_func(image=img, keypoints=kpts) # v_flip if v_flip: aug_func = aug.VerticalFlip(1) img, kpts = aug_func(image=img, keypoints=kpts) # scale if scale != 1: aug_func = aug.Affine(scale=scale, mode='wrap') img, kpts = aug_func(image=img, keypoints=kpts) if self.check_out_of_bounds(img.shape[:2], kpts.to_xy_array()): continue # blur if blur != 0: aug_func = aug.GaussianBlur(sigma=blur) img, kpts = aug_func(image=img, keypoints=kpts) # noise if noise != 0: aug_func = aug.AdditiveGaussianNoise(scale=noise) img, kpts = aug_func(image=img, keypoints=kpts) img = np.moveaxis(img, -1, 0) # (h, w, c) -> (c, h, w) coord = self.to_Lcoord(kpts.to_xy_array()) if self.check_out_of_bounds(img.shape[1:], coord): continue xs.append(img) ys.append(coord.flatten()) isori.append(True) # augment if augment >= 1: augxs, augys, fail_count = [], [], 0 while len(augxs) < augment: img_aug, coord = self.run_aug(img, kpts) if fail_count > 5: return None, None, None, None if img_aug is None: fail_count += 1 continue augxs.append(img_aug) augys.append(coord.flatten()) xs.extend(augxs) ys.extend(augys) isori.extend([False] * len(augxs)) elif np.random.rand() < augment: img_aug, coord = self.run_aug(img, kpts) if img_aug is not None: xs.append(img_aug) ys.append(coord.flatten()) isori.append(False) if len(xs) <= 0: return None, None, None, None return np.stack(xs) / 255, np.stack(ys), np.stack(isori), ( bsa.row_cut_pp, bsa.col_cut_pp)
result_originals = path.joinpath("AugmentedOriginals") result_masks = path.joinpath("AugmentedMasks") for i in range(IMAGE_COUNT): istr = str(i % 900) + ".jpg" original = imageio.imread(originals.joinpath(istr)) mask = imageio.imread(masks.joinpath(istr)) mask = SegmentationMapsOnImage(mask, shape=mask.shape) seq = iaa.SomeOf((0, None), random_order=True) seq.add(iaa.Add((-40, 40), per_channel=0.5)) seq.add(iaa.GaussianBlur(sigma=(0, 2))) seq.add(iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.3, 0.75), per_channel=True)) seq.add(iaa.HorizontalFlip()) seq.add(iaa.VerticalFlip()) seq.add(iaa.TranslateX(percent=(-0.7, 0.7), cval=33)) seq.add(iaa.TranslateY(percent=(-0.7, 0.7), cval=33)) seq.add(iaa.Rotate(random.randrange(-60, 60), cval=33)) seq.add(iaa.ScaleX((0.5, 1.5), cval=33)) seq.add(iaa.ScaleY((0.5, 1.5), cval=33)) seq.add(iaa.imgcorruptlike.DefocusBlur(severity=1)) aug = iaa.CropAndPad(percent=([-0.3, 0.3], [-0.3, 0.3], [-0.3, 0.3], [-0.3, 0.3])) results_o, results_m = seq(image=original, segmentation_maps=mask) istr = str(i) + ".jpg" imageio.imsave(result_originals.joinpath(istr), results_o) imageio.imsave(result_masks.joinpath(istr), results_m.arr)