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 __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_vh(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 = raw_img.transpose((1, 0, 2)) hfliper = iaa.HorizontalFlip() vfliper = iaa.VerticalFlip() hfliped = hfliper(images=[raw_img]) vfliped = vfliper(images=[raw_img, hfliped[0]]) yield (img.split("\\")[-1], [raw_img, hfliped[0]] + vfliped) except Exception as e: print(repr(e))
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, ), ])
# value_range=(0, None)) 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(
def test_VerticalFlip(): aug = iaa.VerticalFlip(0.5) assert isinstance(aug, iaa.Flipud) assert np.allclose(aug.p.p.value, 0.5)
def test_returns_flipud(self): aug = iaa.VerticalFlip(0.5) assert isinstance(aug, iaa.Flipud) 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)