def augument(self, image, bbox_list): seq = iaa.Sequential([ # 变形 iaa.Sometimes( 0.6, [ iaa.OneOf([ iaa.Affine(shear={ 'x': (-1.5, 1.5), 'y': (-1.5, 1.5) }, mode="edge"), # 仿射变化程度,单位像素 iaa.Rotate(rotate=(-1, 1), mode="edge"), # 旋转,单位度 ]) ]), # 扭曲 iaa.Sometimes( 0.5, [ iaa.OneOf([ iaa.PiecewiseAffine( scale=(0, 0.02), nb_rows=2, nb_cols=2), # 局部仿射 iaa.ElasticTransformation( # distort扭曲变形 alpha=(0, 3), # 扭曲程度 sigma=(0.8, 1), # 扭曲后的平滑程度 mode="nearest"), ]), ]), # 模糊 iaa.Sometimes( 0.5, [ iaa.OneOf([ iaa.GaussianBlur(sigma=(0, 0.7)), iaa.AverageBlur(k=(1, 3)), iaa.MedianBlur(k=(1, 3)), iaa.BilateralBlur( d=(1, 5), sigma_color=(10, 200), sigma_space=(10, 200)), # 既噪音又模糊,叫双边, iaa.MotionBlur(k=(3, 5)), iaa.Snowflakes(flake_size=(0.1, 0.2), density=(0.005, 0.025)), iaa.Rain(nb_iterations=1, drop_size=(0.05, 0.1), speed=(0.04, 0.08)), ]) ]), # 锐化 iaa.Sometimes(0.3, [ iaa.OneOf([ iaa.Sharpen(), iaa.GammaContrast(), iaa.SigmoidContrast() ]) ]), # 噪音 iaa.Sometimes(0.3, [ iaa.OneOf([ iaa.AdditiveGaussianNoise(scale=(1, 5)), iaa.AdditiveLaplaceNoise(scale=(1, 5)), iaa.AdditivePoissonNoise(lam=(1, 5)), iaa.Salt((0, 0.02)), iaa.Pepper((0, 0.02)) ]) ]), # 剪切 iaa.Sometimes( 0.8, [ iaa.OneOf([ iaa.Crop((0, 2)), # 切边, (0到10个像素采样) ]) ]), ]) assert bbox_list is None or type(bbox_list) == list if bbox_list is None or len(bbox_list) == 0: polys = None else: polys = [ia.Polygon(pos) for pos in bbox_list] polys = ia.PolygonsOnImage(polys, shape=image.shape) # 处理部分或者整体出了图像的范围的多边形,参考:https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html polys = polys.remove_out_of_image().clip_out_of_image() images_aug, polygons_aug = seq(images=[image], polygons=polys) image = images_aug[0] if polygons_aug is None: polys = None else: polys = [] for p in polygons_aug.polygons: polys.append(p.coords) polys = np.array(polys, np.int32).tolist() # (N,2) return image, polys
from imgaug import augmenters as iaa import argparse from tqdm import tqdm import os aug_dict = { 'AdditiveGaussianNoise': iaa.AdditiveGaussianNoise(loc=0, scale=0.05 * 255, per_channel=False), 'AdditiveGaussianNoise_pc': iaa.AdditiveGaussianNoise(loc=0, scale=0.05 * 255, per_channel=True), 'AdditiveLaplaceNoise': iaa.AdditiveLaplaceNoise(loc=0, scale=0.05 * 255, per_channel=False), 'AdditiveLaplaceNoise_pc': iaa.AdditiveLaplaceNoise(loc=0, scale=0.05 * 255, per_channel=True), 'AdditivePoissonNoise': iaa.AdditivePoissonNoise(lam=16.00, per_channel=False), 'AdditivePoissonNoise_pc': iaa.AdditivePoissonNoise(lam=16.00, per_channel=True), 'ImpulseNoise': iaa.ImpulseNoise(p=0.05), 'SaltAndPepper': iaa.SaltAndPepper(p=0.05), 'GaussianBlur': iaa.GaussianBlur(sigma=0.50), 'AverageBlur': iaa.AverageBlur(k=3), 'AddToHueAndSaturation_p': iaa.AddToHueAndSaturation(value=25), 'AddToHueAndSaturation_n': iaa.AddToHueAndSaturation(value=-25), 'Grayscale':
def main(args): # Print settings for k, v in vars(args).items(): print(f'{k}: {v}') num_classes = 8 size = (224, 224, 3) # size of images # Runtime initialization will not allocate all memory on GPU physical_devices = tf.config.list_physical_devices('GPU') try: tf.config.experimental.set_memory_growth(physical_devices[0], True) except: # Invalid device or cannot modify virtual devices once initialized. pass # Create checkpoints dir os.makedirs('saved_models', exist_ok=True) optimizer = optimizers.SGD(learning_rate=args.learning_rate, momentum=0.9) loss = keras.losses.SparseCategoricalCrossentropy(from_logits=False) metrics = [keras.metrics.SparseCategoricalAccuracy()] # model = models.vgg16(input_shape=size, num_classes=num_classes, classifier_activation='softmax') model = models.resnet50(input_shape=size, num_classes=num_classes, classifier_activation='softmax') model.compile(optimizer=optimizer, loss=loss, metrics=metrics) model.summary() if args.checkpoints: if os.path.exists(args.checkpoints): print(f'Loading checkpoints: {args.checkpoints}') model.load_weights(args.checkpoints) else: print(f'Checkpoints `{args.checkpoints}` not found', file=sys.stderr) os.makedirs("logs/scalars/", exist_ok=True) logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S") # Log loss/metrics for training and validation tensorboard = keras.callbacks.TensorBoard(log_dir=logdir) if args.train: # Same augs as C++ train_aug = iaa.Sequential([ iaa.Resize(size=size[:-1], interpolation='cubic'), iaa.Fliplr(p=0.5), iaa.Flipud(p=0.5), iaa.Rotate(rotate=(-180, 180)), iaa.AdditivePoissonNoise(lam=(0, 10)), iaa.GammaContrast(gamma=(.8, 1.5)), iaa.GaussianBlur(sigma=(.0, .8)), iaa.CoarseDropout(p=(.02, .1), size_px=(0.02, 0.05), size_percent=0.5), ]) val_aug = iaa.Sequential([iaa.Resize(size=size[:-1], interpolation='cubic')]) training_dataset = ISICClassification(args.dataset, 'training', args.batch_size, train_aug) training_tfdata = training_dataset.map_samples(args.epochs) validation_dataset = ISICClassification(args.dataset, 'validation', args.batch_size, val_aug, shuffle=False) validation_tfdata = validation_dataset.map_samples(args.epochs) # Save checkpoints checkpoint = ModelCheckpoint(f'saved_models/{args.name}.h5', monitor='val_sparse_categorical_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', save_freq='epoch') # Stop training after 20 epochs of no improvement early = EarlyStopping(monitor='val_sparse_categorical_accuracy', min_delta=0, patience=args.epochs // 4, verbose=1, mode='auto') # Train the model model.fit( x=training_tfdata, epochs=args.epochs, verbose=1, callbacks=[checkpoint, early, tensorboard], validation_data=validation_tfdata, steps_per_epoch=len(training_dataset), validation_steps=len(validation_dataset), ) if args.test: # Test model on test set test_aug = iaa.Sequential([iaa.Resize(size=size[:-1], interpolation='cubic')]) test_dataset = ISICClassification(args.dataset, 'test', args.batch_size, test_aug) test_tfdata = test_dataset.map_samples(1) results = model.evaluate(test_tfdata, verbose=1, callbacks=[tensorboard]) print("Test set loss and accuracy:", results)
def data_aug(images): seq = iaa.Sometimes( 0.5, iaa.Identity(), iaa.Sometimes( 0.5, iaa.Sequential([ iaa.Fliplr(0.5), iaa.Sometimes( 0.5, iaa.OneOf([ iaa.Add((-40, 40)), iaa.AddElementwise((-40, 40)), iaa.AdditiveGaussianNoise(scale=(0, 0.2 * 255)), iaa.AdditiveLaplaceNoise(scale=(0, 0.2 * 255)), iaa.AdditivePoissonNoise((0, 40)), iaa.MultiplyElementwise((0.5, 1.5)), iaa.ReplaceElementwise(0.1, [0, 255]), iaa.SaltAndPepper(0.1) ])), iaa.OneOf([ iaa.Cutout(nb_iterations=2, size=0.15, cval=0, squared=False), iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)), iaa.Dropout(p=(0, 0.2)), iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1)), iaa.Cartoon(), iaa.BlendAlphaVerticalLinearGradient(iaa.TotalDropout(1.0), min_value=0.2, max_value=0.8), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.AverageBlur(k=(2, 11)), iaa.MedianBlur(k=(3, 11)), iaa.BilateralBlur(d=(3, 10), sigma_color=(10, 250), sigma_space=(10, 250)), iaa.MotionBlur(k=20), iaa.AllChannelsCLAHE(), 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.Affine(scale=(0.5, 1.5)), iaa.Affine(translate_px={ "x": (-20, 20), "y": (-20, 20) }), iaa.Affine(shear=(-16, 16)), iaa.pillike.EnhanceSharpness() ]), iaa.OneOf([ iaa.GammaContrast((0.5, 2.0)), iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)), iaa.LogContrast(gain=(0.6, 1.4)), iaa.LinearContrast((0.4, 1.6)), iaa.pillike.EnhanceBrightness() ]) ]), iaa.Sometimes(0.5, iaa.RandAugment(n=2, m=9), iaa.RandAugment(n=(0, 3), m=(0, 9))))) images = seq(images=images) return images
def imgaug_augment(self, target_dir='default', mode='native'): if target_dir == 'default': data, label = self.npzLoader(self.train_file) else: data, label = self.getStackedData(target_dir=target_dir) if mode == 'native': return data, label elif mode == 'rotation': imgaug_aug = iaa.Affine(rotate=(-90, 90), order=1, mode="edge") # 90度回転 # keras と仕様が異なることに注意 # keras は変化量 / imgaug は 変化の最大角を指定している # 開いた部分の穴埋めができない..?? mode="edge" にするとそれなり.. elif mode == 'hflip': imgaug_aug = iaa.Fliplr(1.0) # 左右反転 elif mode == 'width_shift': imgaug_aug = iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge") # 1/8 平行移動(左右) elif mode == 'height_shift': imgaug_aug = iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge") # 1/8 平行移動(上下) # imgaug_aug = iaa.Crop(px=(0, 40)) <= 平行移動ではなく、切り抜き elif mode == 'zoom': imgaug_aug = iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, mode="edge") # 80~120% ズーム # これも keras と仕様が違って、縦横独立に拡大・縮小されるようである。 elif mode == 'logcon': imgaug_aug = iaa.LogContrast(gain=(5, 15)) elif mode == 'linecon': imgaug_aug = iaa.LinearContrast((0.5, 2.0)) # 明度変換 elif mode == 'gnoise': imgaug_aug = iaa.AdditiveGaussianNoise(scale=[0.05*255, 0.2*255]) # Gaussian Noise elif mode == 'lnoise': imgaug_aug = iaa.AdditiveLaplaceNoise(scale=[0.05*255, 0.2*255]) # LaplaceNoise elif mode == 'pnoise': imgaug_aug = iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True) # PoissonNoise elif mode == 'flatten': imgaug_aug = iaa.GaussianBlur(sigma=(0.5, 1.0)) # blur: ぼかし (平滑化) elif mode == 'sharpen': imgaug_aug = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)) # sharpen images (鮮鋭化) elif mode == 'emboss': imgaug_aug = iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)) # Edge 強調 elif mode == 'invert': #imgaug_aug = iaa.Invert(1.0) # 色反転 <= これがうまく行かないので自分で作った。 aug_data = [] for b in range(data.shape[0]): aug_data.append(255-data[b]) return np.array(aug_data), label elif mode == 'someof': # 上記のうちのどれか1つ imgaug_aug = iaa.SomeOf(1, [ iaa.Affine(rotate=(-90, 90), order=1, mode="edge"), iaa.Fliplr(1.0), iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge"), iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge"), iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, order=1, mode="edge"), iaa.LogContrast((0.5, 1.5)), iaa.LinearContrast((0.5, 2.0)), iaa.AdditiveGaussianNoise(scale=[0.05*255, 0.25*255]), iaa.AdditiveLaplaceNoise(scale=[0.05*255, 0.25*255]), iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True), iaa.GaussianBlur(sigma=(0.5, 1.0)), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), iaa.Invert(1.0) # 14 ]) #elif mode == 'plural': # 異なる系統の変換を複数(1つの変換あとに画素値がマイナスになるとError..) # imgaug_aug = self.randomDataAugument(2) else: print("現在 imgaug で選択できる DA のモードは以下の通りです。") print(self.imgaug_mode_list, "\n") raise ValueError("予期されないモードが選択されています。") aug_data = imgaug_aug.augment_images(data) aug_data = np.clip(aug_data, 0, 255) # 注意: 戻り値の範囲は [0, 255] です。 return aug_data, label
def imgaug_augment(self, TARGET_DIR, INPUT_SIZE, NORMALIZE=False, AUGMENTATION='native'): data, label = self.img2array(TARGET_DIR, INPUT_SIZE, NORMALIZE) if AUGMENTATION == 'native': return data, label elif AUGMENTATION == 'rotation': imgaug_aug = iaa.Affine(rotate=(-90, 90), order=1, mode="edge") # 90度 "まで" 回転 elif AUGMENTATION == 'hflip': imgaug_aug = iaa.Fliplr(1.0) # 左右反転 elif AUGMENTATION == 'width_shift': imgaug_aug = iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge") # 1/8 平行移動(左右) elif AUGMENTATION == 'height_shift': imgaug_aug = iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge") # 1/8 平行移動(上下) # imgaug_aug = iaa.Crop(px=(0, 40)) <= 平行移動ではなく、切り抜き elif AUGMENTATION == 'zoom': imgaug_aug = iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, order=1, mode="edge") # 80~120% ズーム # これも keras と仕様が違って、縦横独立に拡大・縮小されるようである。 elif AUGMENTATION == 'logcon': imgaug_aug = iaa.LogContrast((0.5, 1.5)) elif AUGMENTATION == 'linecon': imgaug_aug = iaa.LinearContrast((0.5, 2.0)) # 明度変換 elif AUGMENTATION == 'gnoise': imgaug_aug = iaa.AdditiveGaussianNoise( scale=[0.05 * 255, 0.2 * 255]) # Gaussian Noise elif AUGMENTATION == 'lnoise': imgaug_aug = iaa.AdditiveLaplaceNoise( scale=[0.05 * 255, 0.2 * 255]) # LaplaceNoise elif AUGMENTATION == 'pnoise': imgaug_aug = iaa.AdditivePoissonNoise( lam=(16.0, 48.0), per_channel=True) # PoissonNoise elif AUGMENTATION == 'flatten': imgaug_aug = iaa.GaussianBlur(sigma=(0.5, 1.0)) # blur: ぼかし (平滑化) elif AUGMENTATION == 'sharpen': imgaug_aug = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)) # sharpen images (鮮鋭化) elif AUGMENTATION == 'emboss': imgaug_aug = iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)) # Edge 強調 elif AUGMENTATION == 'invert': imgaug_aug = iaa.Invert(1.0) # 色反転 <= これがうまく行かないので自分で作った。 elif AUGMENTATION == 'someof': # 上記のうちのどれか1つ imgaug_aug = iaa.SomeOf( 1, [ iaa.Affine(rotate=(-90, 90), order=1, mode="edge"), iaa.Fliplr(1.0), iaa.Affine(translate_percent={"x": (-0.125, 0.125)}, order=1, mode="edge"), iaa.Affine(translate_percent={"y": (-0.125, 0.125)}, order=1, mode="edge"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, order=1, mode="edge"), iaa.LogContrast((0.5, 1.5)), iaa.LinearContrast((0.5, 2.0)), iaa.AdditiveGaussianNoise(scale=[0.05 * 255, 0.25 * 255]), iaa.AdditiveLaplaceNoise(scale=[0.05 * 255, 0.25 * 255]), iaa.AdditivePoissonNoise(lam=(16.0, 48.0), per_channel=True), iaa.GaussianBlur(sigma=(0.5, 1.0)), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), iaa.Invert(1.0) # 14 ]) elif AUGMENTATION == 'plural': # 異なる系統の変換を複数(1つの変換あとに画素値がマイナスになるとError..) imgaug_aug = self.randomDataAugument(2) elif AUGMENTATION == 'fortest': # plural - invert (色反転) (test 用) imgaug_aug = self.randomTestAugument(2) else: print("現在 imgaug で選択できる DA のモードは以下の通りです。") print(self.imgaug_aug_list, "\n") raise ValueError("予期されないモードが選択されています。") aug_data = imgaug_aug.augment_images(data) aug_data = np.clip(aug_data, 0, 255) return aug_data, label
transformed_image = transform(image=image) elif augmentation == 'add_elementwise': transform = iaa.AddElementwise((-75, 75)) transformed_image = transform(image=image) elif augmentation == 'additive_gaussian_noise': transform = iaa.AdditiveGaussianNoise(scale=(0, 0.2*255)) transformed_image = transform(image=image) elif augmentation == 'additive_laplace_noise': transform = iaa.AdditiveLaplaceNoise(scale=(0, 0.2*255)) transformed_image = transform(image=image) elif augmentation == 'additive_poisson_noise': transform = iaa.AdditivePoissonNoise(lam=(0, 40)) transformed_image = transform(image=image) elif augmentation == 'multiply': transform = iaa.Multiply((0.1, 2.0)) transformed_image = transform(image=image) elif augmentation == 'multiply_elementwise': transform = iaa.MultiplyElementwise((0.1, 2.0)) transformed_image = transform(image=image) elif augmentation == 'dropout': transform = iaa.Dropout(p=(0, 0.2)) transformed_image = transform(image=image) elif augmentation == 'coarse_dropout':
def __init__(self, path, start_size, do_rgb=False, preload=False, augmentations=None, center_crop=False, nonpreserving_scale=False): self.do_center_crop = center_crop self.non_preserving_scale = nonpreserving_scale self.extensions = ('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp') self.classes = [d.name for d in os.scandir(path) if d.is_dir()] self.classes.sort() self.class_to_idx = { self.classes[i]: i for i in range(len(self.classes)) } self.samples = self.make_dataset(path, self.class_to_idx, self.extensions) if len(self.samples) == 0: raise (RuntimeError("Found 0 files in subfolders of: " + path + "\n" + "Supported extensions are: " + ",".join(self.extensions))) self.path = path self.size = start_size if augmentations is None: self.augmenter = iaa.OneOf([ iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)), iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255), per_channel=True), iaa.AdditiveLaplaceNoise(scale=(0, 0.05 * 255)), iaa.AdditiveLaplaceNoise(scale=(0, 0.05 * 255), per_channel=True), iaa.AdditivePoissonNoise(lam=(0, 16)), iaa.AdditivePoissonNoise(lam=(0, 16), per_channel=True) ]) else: self.augmenter = augmentations self.rgb = do_rgb self.images = {} self.preloaded = False if preload: self.preloaded = True for s, (img_path, target) in enumerate(self.samples): if self.rgb: img = cv.imread(img_path, cv.IMREAD_COLOR) img = cv.cvtColor(img, cv.COLOR_BGR2RGB) else: img = cv.imread(img_path, cv.IMREAD_GRAYSCALE) self.images[img_path] = img if self.rgb: self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True) ]) else: self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, ), inplace=True) ])
def get_noise(): return ia.OneOf([ ia.AdditiveGaussianNoise(scale=(20, 40), per_channel=True), ia.AdditiveLaplaceNoise(scale=(20, 40), per_channel=True), ia.AdditivePoissonNoise(lam=(15, 30), per_channel=True), ])
import random meta = {'noop': iaa.Noop(), 'shuffle': iaa.ChannelShuffle(p=1.0)} arithmetic = { 'add-45': iaa.Add(value=-45), 'add-25': iaa.Add(value=-25), 'add+25': iaa.Add(value=25), 'add+45': iaa.Add(value=45), 'addp-': iaa.Add(value=(-35, -15), per_channel=True), 'addp+': iaa.Add(value=(15, 35), per_channel=True), 'addGN': iaa.AdditiveGaussianNoise(scale=0.10 * 255), 'addGNp': iaa.AdditiveGaussianNoise(scale=0.10 * 255, per_channel=True), 'addLN': iaa.AdditiveLaplaceNoise(scale=0.10 * 255), 'addLNp': iaa.AdditiveLaplaceNoise(scale=0.10 * 255, per_channel=True), 'addPN': iaa.AdditivePoissonNoise(lam=16.00), 'addPNp': iaa.AdditivePoissonNoise(lam=16.00, per_channel=True), 'mul-': iaa.Multiply(mul=0.50), 'mul+': iaa.Multiply(mul=1.50), 'mulp-': iaa.Multiply(mul=0.50, per_channel=True), 'mulp+': iaa.Multiply(mul=1.50, per_channel=True), 'jpeg': iaa.JpegCompression(compression=62), 'jpeg+': iaa.JpegCompression(compression=75), 'jpeg++': iaa.JpegCompression(compression=87) } blur = { 'GBlur': iaa.GaussianBlur(sigma=1.00), 'ABlur': iaa.AverageBlur(k=3), 'MBlur': iaa.MedianBlur(k=3), 'BBlur': iaa.BilateralBlur(sigma_color=250, sigma_space=250, d=5),
def __init__(self, scale_limit=(0, 20), determint=False): assert len(scale_limit) == 2 self.scale_limit = scale_limit self.determint = determint self.func = iaa.AdditivePoissonNoise((scale_limit[0], scale_limit[1]))
import imgaug as ia import imgaug.augmenters as iaa import imgaug.parameters as iap aug_img = iaa.Sequential([ # Noise iaa.Sometimes( 0.15, iaa.OneOf([ iaa.imgcorruptlike.ShotNoise(severity=(1, 2)), iaa.imgcorruptlike.ImpulseNoise(severity=(1, 2)), iaa.imgcorruptlike.SpeckleNoise(severity=(1, 2)), iaa.imgcorruptlike.Spatter(severity=(1, 3)), iaa.AdditivePoissonNoise((1, 20), per_channel=0.5), iaa.AdditiveLaplaceNoise(scale=(0.005 * 255, 0.03 * 255), per_channel=0.5), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.03 * 255), per_channel=0.5), iaa.BlendAlphaElementwise((0.0, 1.0), foreground=iaa.Add((-15, 15)), background=iaa.Multiply((0.8, 1.2))), iaa.ReplaceElementwise(0.05, iap.Normal(128, 0.4 * 128), per_channel=0.5), iaa.Dropout(p=(0, 0.05), per_channel=0.5), ])), # Brightness + Color + Contrast iaa.Sometimes(
def do_random(image, pos_list): # 1.先任选5种影响位置的效果之一做位置变换 seq = iaa.Sequential([ iaa.Sometimes( 0.5, [ iaa.Crop((0, 10)), # 切边, (0到10个像素采样) ]), iaa.Sometimes( 0.5, [ iaa.Affine(shear={ 'x': (-10, 10), 'y': (-10, 10) }, mode="edge"), iaa.Rotate(rotate=(-10, 10), mode="edge"), # 旋转 ]), iaa.Sometimes( 0.5, [ iaa.PiecewiseAffine(), # 局部仿射 iaa.ElasticTransformation( # distort扭曲变形 alpha=(0.0, 20.0), sigma=(3.0, 5.0), mode="nearest"), ]), # 18种位置不变的效果 iaa.SomeOf( (1, 3), [ iaa.GaussianBlur(), iaa.AverageBlur(), iaa.MedianBlur(), iaa.Sharpen(), iaa.BilateralBlur(), # 既噪音又模糊,叫双边, iaa.MotionBlur(), iaa.MeanShiftBlur(), iaa.GammaContrast(), iaa.SigmoidContrast(), iaa.Fog(), iaa.Clouds(), iaa.Snowflakes(flake_size=(0.1, 0.2), density=(0.005, 0.025)), iaa.Rain(nb_iterations=1, drop_size=(0.05, 0.1), speed=(0.04, 0.08)), iaa.AdditiveGaussianNoise(scale=(0, 10)), iaa.AdditiveLaplaceNoise(scale=(0, 10)), iaa.AdditivePoissonNoise(lam=(0, 10)), iaa.Salt((0, 0.02)), iaa.Pepper((0, 0.02)) ]) ]) polys = [ia.Polygon(pos) for pos in pos_list] polygons = ia.PolygonsOnImage(polys, shape=image.shape) images_aug, polygons_aug = seq(images=[image], polygons=polygons) image = images_aug[0] image = polygons_aug.draw_on_image(image, size=2) new_polys = [] for p in polygons_aug.polygons: new_polys.append(p.coords) polys = np.array(new_polys, np.int32).tolist() return image, polys
def main(args): writer = SummaryWriter(comment=args.exp_name) os.makedirs(args.weights, exist_ok=True) train_transform = iaa.Sequential([ iaa.Resize((args.size, args.size)), iaa.Fliplr(p=0.5), iaa.Flipud(p=0.5), iaa.Rotate(rotate=(-180, 180)), iaa.AdditivePoissonNoise(lam=(0, 10.,)), iaa.GammaContrast(gamma=(.5, 1.5)), iaa.GaussianBlur(sigma=(.0, .8)), iaa.Sometimes(0.25, iaa.CoarseDropout(p=(0, 0.03), size_percent=(0, 0.05))), ]) valid_transform = iaa.Sequential([ iaa.Resize((args.size, args.size)), ]) train_dataset = YAMLClassificationDataset(dataset=args.in_ds, transform=train_transform, split=['training'], normalization=normalization_isic) valid_dataset = YAMLClassificationDataset(dataset=args.in_ds, transform=valid_transform, split=['validation'], normalization=normalization_isic) test_dataset = YAMLClassificationDataset(dataset=args.in_ds, transform=valid_transform, split=['test'], normalization=normalization_isic) train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, drop_last=True) valid_dataloader = DataLoader(valid_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, drop_last=False) test_dataloader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, drop_last=False) dataloaders = {"train": train_dataloader, "valid": valid_dataloader, 'test': test_dataloader} device = torch.device('cpu' if not args.gpu else 'cuda') # Model, loss, optimizer print('Loading model...') model = SkinLesionModel(args.model) if args.onnx_export: # export onnx dummy_input = torch.ones(4, 3, args.size, args.size, device='cpu') model.train() torch.onnx.export(model, dummy_input, f'{args.model}.onnx', verbose=True, export_params=True, training=torch.onnx.TrainingMode.TRAINING, opset_version=12, do_constant_folding=False, input_names=['input'], output_names=['output'], dynamic_axes={'input': {0: 'batch_size'}, # variable length axes 'output': {0: 'batch_size'}}) # Change last linear layer model.fc = torch.nn.Linear(model.fc.in_features, args.num_classes) if torch.cuda.device_count() > 1 and args.gpu: model = torch.nn.DataParallel(model, device_ids=np.where(np.array(args.gpu) == 1)[0]) print(f'Move model to {device}') model = model.to(device) # loss_fn = nn.modules.loss.CrossEntropyLoss(weight=torch.from_numpy(get_weights()).to(device)) loss_fn = nn.modules.loss.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=args.learning_rate) if args.ckpts is None: best_valid_acc = 0. load_epoch = 0 else: checkpoint = torch.load(args.ckpts) model.load_state_dict(checkpoint['state_dict']) load_epoch = checkpoint['epoch'] optimizer.load_state_dict(checkpoint['optimizer']) best_valid_acc = checkpoint['best_metric'] print("Loaded checkpoint epoch ", load_epoch, " with best metric ", best_valid_acc) train_acc = 0 valid_acc = 0 print('Starting training') for epoch in range(load_epoch, args.epochs): loss_train = [] loss_valid = [] for phase in ["train", "valid"]: if phase == "train": model.train() else: model.eval() correct = 0 total = 0 pred_list = [] gt_list = [] with tqdm(desc=f"{phase} {epoch}/{args.epochs}", unit="batch", total=len(dataloaders[phase]), file=sys.stdout) as pbar: for i, (x, gt, names) in enumerate(dataloaders[phase]): # torchvision.utils.save_image(x, f'batch_{i}.jpg') x, gt = x.to(device), gt.to(device) with torch.set_grad_enabled(phase == "train"): pred = model(x) loss = loss_fn(pred, gt) loss_item = loss.item() pred = torch.nn.functional.softmax(pred, dim=1) pred_np = pred.detach().cpu().numpy() pred_np = pred_np.argmax(axis=1) pred_list.extend(pred_np) gt_np = gt.detach().cpu().numpy() gt_list.extend(gt_np) correct += (pred_np == gt_np).sum() total += pred_np.shape[0] if phase == "train": optimizer.zero_grad() loss.backward() optimizer.step() loss_train.append(loss_item) elif phase == "valid": loss_valid.append(loss_item) pbar.set_postfix(loss=loss_item, accuracy=correct / total) pbar.update() accuracy = correct / total cm = confusion_matrix(np.array(pred_list).reshape(-1), np.array(gt_list).reshape(-1)) print(f'{phase} {epoch}/{args.epochs}: accuracy={accuracy:.4f}') fig = plt.figure(figsize=(args.num_classes, args.num_classes)) plot_confusion_matrix(cm, [0, 1, 2, 3, 4, 5, 6, 7]) writer.add_figure(f'{phase}/confusion', fig, epoch) if phase == 'train': train_acc = accuracy writer.add_scalar(f'{phase}/loss', np.mean(loss_train), epoch) writer.add_scalar(f'{phase}/accuracy', train_acc, epoch) else: valid_acc = accuracy writer.add_scalar(f'{phase}/loss', np.mean(loss_valid), epoch) writer.add_scalar(f'{phase}/accuracy', valid_acc, epoch) if valid_acc > best_valid_acc: best_valid_acc = valid_acc state = { 'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'best_metric': best_valid_acc } torch.save(state, os.path.join(args.weights, f'{args.model}.pth'))
def get_aug(self): #sometimes_bg = lambda aug: iaa.Sometimes(0.3, aug) sometimes_contrast = lambda aug: iaa.Sometimes(0.3, aug) sometimes_noise = lambda aug: iaa.Sometimes(0.6, aug) sometimes_blur = lambda aug: iaa.Sometimes(0.6, aug) sometimes_degrade_quality = lambda aug: iaa.Sometimes(0.9, aug) sometimes_blend = lambda aug: iaa.Sometimes(0.2, aug) seq = iaa.Sequential( [ # crop some of the images by 0-30% of their height/width # Execute 0 to 4 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, 4), # [ # change the background color of some of the images chosing any one technique # sometimes_bg(iaa.OneOf([ # iaa.AddToHueAndSaturation((-60, 60)), # iaa.Multiply((0.6, 1), per_channel=True), # ])), #change the contrast of the input images chosing any one technique sometimes_contrast(iaa.OneOf([ iaa.LinearContrast((0.5,1.5)), iaa.SigmoidContrast(gain=(3, 5), cutoff=(0.4, 0.6)), iaa.CLAHE(tile_grid_size_px=(3, 21)), iaa.GammaContrast((0.5,1.0)) ])), #add noise to the input images chosing any one technique sometimes_noise(iaa.OneOf([ iaa.AdditiveGaussianNoise(scale=(3,8)), iaa.CoarseDropout((0.001,0.01), size_percent=0.5), iaa.AdditiveLaplaceNoise(scale=(3,10)), iaa.CoarsePepper((0.001,0.01), size_percent=(0.5)), iaa.AdditivePoissonNoise(lam=(3.0,10.0)), iaa.Pepper((0.001,0.01)), iaa.Snowflakes(), iaa.Dropout(0.01,0.01), ])), #add blurring techniques to the input image sometimes_blur(iaa.OneOf([ iaa.AverageBlur(k=(3)), iaa.GaussianBlur(sigma=(1.0)), ])), # add techniques to degrade the iamge quality sometimes_degrade_quality(iaa.OneOf([ iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), iaa.Sharpen(alpha=(0.5), lightness=(0.75,1.5)), iaa.BlendAlphaSimplexNoise( foreground=iaa.Multiply(iap.Choice([1.5]), per_channel=False) ) ])), # blend some patterns in the background sometimes_blend(iaa.OneOf([ iaa.BlendAlpha( factor=(0.6,0.8), foreground=iaa.Sharpen(1.0, lightness=1), background=iaa.CoarseDropout(p=0.1, size_px=np.random.randint(30))), iaa.BlendAlphaFrequencyNoise(exponent=(-4), foreground=iaa.Multiply(iap.Choice([0.5]), per_channel=False) ), iaa.BlendAlphaSimplexNoise( foreground=iaa.Multiply(iap.Choice([0.5]), per_channel=True) ) ])), ]) return seq
def __init__(self, configuration): """ Initialized the configuration prameters Arguments: configuration: file pointer The hitif configuration file """ import configparser config = configparser.ConfigParser() config.read(configuration) #Parse the augmentation parameters aug_prms = config['augmentation'] self.CLAHE = eval(aug_prms['AllChannelsCLAHE']) self.Saturation = eval(aug_prms['Saturation']) self.impulse_noise = eval(aug_prms['ImpulseNoise']) self.gaussian_blur = eval(aug_prms['GaussianBlur']) self.poisson = eval(aug_prms['AdditivePoissonNoise']) self.median = eval(aug_prms['MedianBlur']) self.flip = float(aug_prms["flip"]) self.rotate = eval(aug_prms["rotate"]) self.gamma = eval(aug_prms["GammaContrast"]) self.gaussian_noise = eval(aug_prms["AdditiveGaussianNoise"]) self.dropout = eval(aug_prms["Dropout"]) self.salt_peper = eval(aug_prms["SaltAndPepper"]) from imgaug import augmenters as iaa import imgaug as ia import numpy as np seed = np.random.randint(0, 2**31 - 1) ia.seed(seed) self.augmenters = {} augmenters = self.augmenters #Affine augmentation augmenters["fliplr"] = iaa.Fliplr(self.flip) augmenters["flipud"] = iaa.Flipud(self.flip) augmenters["rotate"] = iaa.Affine(rotate=[self.rotate[0],\ self.rotate[1],\ self.rotate[2]]) #Contrast augmentation #augmenters["CLAHE"] = iaa.AllChannelsCLAHE(self.CLAHE) augmenters["CLAHE"] = iaa.CLAHE(self.CLAHE) #augmenters["CLAHE"] = iaa.AllChannelsCLAHE(self.CLAHE[0], self.CLAHE[1], self.CLAHE[2],self.CLAHE[3]) augmenters["gamma"] = iaa.GammaContrast(self.gamma, True) #augmenters['saturation'] = iaa.Lambda(func_images=self.saturate_images, func_heatmaps=self.func_heatmaps, func_keypoints=self.func_keypoints) augmenters['Saturation'] = iaa.Saturation(self.Saturation) #Blur augmenters augmenters["median_blur"] = iaa.MedianBlur(self.median) augmenters["gaussian_blur"] = iaa.GaussianBlur(self.gaussian_blur) #Noise augmenters augmenters["impulse_noise"] = iaa.ImpulseNoise(self.impulse_noise) augmenters["poisson_noise"] = iaa.AdditivePoissonNoise(self.poisson) augmenters["gaussian_noise"] = iaa.AdditiveGaussianNoise( scale=self.gaussian_noise) augmenters["dropout"] = iaa.Dropout(self.dropout)
def create_augmenters(height, width, height_augmentable, width_augmentable, only_augmenters): def lambda_func_images(images, random_state, parents, hooks): return images def lambda_func_heatmaps(heatmaps, random_state, parents, hooks): return heatmaps def lambda_func_keypoints(keypoints, random_state, parents, hooks): return keypoints def assertlambda_func_images(images, random_state, parents, hooks): return True def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks): return True def assertlambda_func_keypoints(keypoints, random_state, parents, hooks): return True augmenters_meta = [ iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=False, name="Sequential_2xNoop"), iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=True, name="Sequential_2xNoop_random_order"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=False, name="SomeOf_3xNoop"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=True, name="SomeOf_3xNoop_random_order"), iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"), iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"), iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"), iaa.Noop(name="Noop"), iaa.Lambda(func_images=lambda_func_images, func_heatmaps=lambda_func_heatmaps, func_keypoints=lambda_func_keypoints, name="Lambda"), iaa.AssertLambda(func_images=assertlambda_func_images, func_heatmaps=assertlambda_func_heatmaps, func_keypoints=assertlambda_func_keypoints, name="AssertLambda"), iaa.AssertShape((None, height_augmentable, width_augmentable, None), name="AssertShape"), iaa.ChannelShuffle(0.5, name="ChannelShuffle") ] augmenters_arithmetic = [ iaa.Add((-10, 10), name="Add"), iaa.AddElementwise((-10, 10), name="AddElementwise"), #iaa.AddElementwise((-500, 500), name="AddElementwise"), iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"), iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"), iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Dropout((0.01, 0.05), name="Dropout"), iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseDropout"), iaa.ReplaceElementwise((0.01, 0.05), (0, 255), name="ReplaceElementwise"), #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"), iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"), iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"), iaa.CoarseSaltAndPepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.Salt((0.01, 0.05), name="Salt"), iaa.CoarseSalt((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.Pepper((0.01, 0.05), name="Pepper"), iaa.CoarsePepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarsePepper"), iaa.Invert(0.1, name="Invert"), # ContrastNormalization iaa.JpegCompression((50, 99), name="JpegCompression") ] augmenters_blur = [ iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"), iaa.MotionBlur(k=(3, 11), name="MotionBlur") ] augmenters_color = [ # InColorspace (deprecated) iaa.WithColorspace(to_colorspace="HSV", children=iaa.Noop(), name="WithColorspace"), iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"), iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"), iaa.Grayscale((0.01, 0.99), name="Grayscale") ] augmenters_contrast = [ iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"), iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.25, 0.75), name="SigmoidContrast"), iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"), iaa.LinearContrast((0.5, 1.5), name="LinearContrast"), iaa.AllChannelsHistogramEqualization( name="AllChannelsHistogramEqualization"), iaa.HistogramEqualization(to_colorspace="HSV", name="HistogramEqualization"), iaa.AllChannelsCLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), name="AllChannelsCLAHE"), iaa.CLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), to_colorspace="HSV", name="CLAHE"), ] augmenters_convolutional = [ iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), name="Convolve_3x3"), iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"), iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"), iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect") ] augmenters_flip = [ iaa.Fliplr(1.0, name="Fliplr"), iaa.Flipud(1.0, name="Flipud") ] augmenters_geometric = [ iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=0, mode="constant", cval=(0, 255), name="Affine_order_0_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), name="Affine_order_1_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=3, mode="constant", cval=(0, 255), name="Affine_order_3_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="edge", cval=(0, 255), name="Affine_order_1_edge"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), backend="skimage", name="Affine_order_1_constant_skimage"), # TODO AffineCv2 iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="constant", name="PiecewiseAffine_4x4_order_1_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=0, mode="constant", name="PiecewiseAffine_4x4_order_0_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="edge", name="PiecewiseAffine_4x4_order_1_edge"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=8, nb_cols=8, order=1, mode="constant", name="PiecewiseAffine_8x8_order_1_constant"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=False, name="PerspectiveTransform"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=True, name="PerspectiveTransform_keep_size"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=0, mode="constant", cval=0, name="ElasticTransformation_order_0_constant"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="constant", cval=0, name="ElasticTransformation_order_1_constant"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="nearest", cval=0, name="ElasticTransformation_order_1_nearest"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="reflect", cval=0, name="ElasticTransformation_order_1_reflect"), iaa.Rot90((1, 3), keep_size=False, name="Rot90"), iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size") ] augmenters_segmentation = [ iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="cubic", name="Superpixels_max_size_64_cubic"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="linear", name="Superpixels_max_size_64_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=128, interpolation="linear", name="Superpixels_max_size_128_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=224, interpolation="linear", name="Superpixels_max_size_224_linear"), ] augmenters_size = [ iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"), iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"), iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="CropAndPad"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="CropAndPad_edge"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), name="CropAndPad_keep_size"), iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"), iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="Pad"), iaa.Pad(percent=(0.05, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="Pad_edge"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), name="Pad_keep_size"), iaa.PadToFixedSize(width=width + 10, height=height + 10, pad_mode="constant", pad_cval=(0, 255), name="PadToFixedSize"), iaa.CropToFixedSize(width=width - 10, height=height - 10, name="CropToFixedSize"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="nearest", name="KeepSizeByResize_CropToFixedSize_nearest"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="linear", name="KeepSizeByResize_CropToFixedSize_linear"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="cubic", name="KeepSizeByResize_CropToFixedSize_cubic"), ] augmenters_weather = [ iaa.FastSnowyLandscape(lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0), name="FastSnowyLandscape"), iaa.Clouds(name="Clouds"), iaa.Fog(name="Fog"), iaa.CloudLayer(intensity_mean=(196, 255), intensity_freq_exponent=(-2.5, -2.0), intensity_coarse_scale=10, alpha_min=0, alpha_multiplier=(0.25, 0.75), alpha_size_px_max=(2, 8), alpha_freq_exponent=(-2.5, -2.0), sparsity=(0.8, 1.0), density_multiplier=(0.5, 1.0), name="CloudLayer"), iaa.Snowflakes(name="Snowflakes"), iaa.SnowflakesLayer(density=(0.005, 0.075), density_uniformity=(0.3, 0.9), flake_size=(0.2, 0.7), flake_size_uniformity=(0.4, 0.8), angle=(-30, 30), speed=(0.007, 0.03), blur_sigma_fraction=(0.0001, 0.001), name="SnowflakesLayer") ] augmenters = augmenters_meta + augmenters_arithmetic + augmenters_blur + augmenters_color + augmenters_contrast \ + augmenters_convolutional + augmenters_flip + augmenters_geometric + augmenters_segmentation \ + augmenters_size + augmenters_weather if only_augmenters is not None: augmenters_reduced = [] for augmenter in augmenters: if any([ re.search(pattern, augmenter.name) for pattern in only_augmenters ]): augmenters_reduced.append(augmenter) augmenters = augmenters_reduced return augmenters
# # aug58 = iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.0, 0.5)) # # aug59 = iaa.Canny( # # alpha=(0.0, 0.1), # # colorizer=iaa.RandomColorsBinaryImageColorizer( # # color_true=255, # # color_false=0 # # ) # # ) scale=(0, 20) aug1 = iaa.Add((-20, 20)) aug2 = iaa.AddElementwise((-20, 20), per_channel=0.5) aug3 = aug = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)) aug4 = iaa.AdditiveLaplaceNoise(scale=(0, 0.2*255)) aug5 = iaa.AdditivePoissonNoise(scale) aug6 = iaa.Multiply((0.5, 1.5), per_channel=0.5) aug7 = iaa.Cutout(nb_iterations=2, size=0.05) aug8 = iaa.Cutout(fill_mode="constant", size=0.05, cval=255) aug9 = iaa.Cutout(fill_mode="gaussian", fill_per_channel=True, size=0.05) aug10 = iaa.Dropout(p=(0, 0.05)) aug11 = iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.2)) aug12 = iaa.Dropout2d(p=0.05, nb_keep_channels=0) aug13 = iaa.ImpulseNoise(0.1) aug14 = iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.6)) aug15 = iaa.CoarsePepper(0.05, size_percent=(0.01, 0.2)) # aug16 = iaa.Invert(0.25, per_channel=0.4) # aug17 = iaa.Invert(0.1) # aug18 = iaa.Solarize(0.05, threshold=(32, 128))
def __init__(self): self.gaussian = iaa.AdditiveGaussianNoise(loc=0, scale=0.04 * 255) self.poisson = iaa.AdditivePoissonNoise(lam=5.0, per_channel=True) if not os.path.isdir('tmp'): os.makedirs('tmp')
def create_augmenters(height, width, height_augmentable, width_augmentable, only_augmenters): def lambda_func_images(images, random_state, parents, hooks): return images def lambda_func_heatmaps(heatmaps, random_state, parents, hooks): return heatmaps def lambda_func_keypoints(keypoints, random_state, parents, hooks): return keypoints def assertlambda_func_images(images, random_state, parents, hooks): return True def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks): return True def assertlambda_func_keypoints(keypoints, random_state, parents, hooks): return True augmenters_meta = [ iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=False, name="Sequential_2xNoop"), iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=True, name="Sequential_2xNoop_random_order"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=False, name="SomeOf_3xNoop"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=True, name="SomeOf_3xNoop_random_order"), iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"), iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"), iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"), iaa.Identity(name="Identity"), iaa.Noop(name="Noop"), iaa.Lambda(func_images=lambda_func_images, func_heatmaps=lambda_func_heatmaps, func_keypoints=lambda_func_keypoints, name="Lambda"), iaa.AssertLambda(func_images=assertlambda_func_images, func_heatmaps=assertlambda_func_heatmaps, func_keypoints=assertlambda_func_keypoints, name="AssertLambda"), iaa.AssertShape((None, height_augmentable, width_augmentable, None), name="AssertShape"), iaa.ChannelShuffle(0.5, name="ChannelShuffle") ] augmenters_arithmetic = [ iaa.Add((-10, 10), name="Add"), iaa.AddElementwise((-10, 10), name="AddElementwise"), #iaa.AddElementwise((-500, 500), name="AddElementwise"), iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"), iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"), iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Cutout(nb_iterations=1, name="Cutout-fill_constant"), iaa.Dropout((0.01, 0.05), name="Dropout"), iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseDropout"), iaa.Dropout2d(0.1, name="Dropout2d"), iaa.TotalDropout(0.1, name="TotalDropout"), iaa.ReplaceElementwise((0.01, 0.05), (0, 255), name="ReplaceElementwise"), #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"), iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"), iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"), iaa.CoarseSaltAndPepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.Salt((0.01, 0.05), name="Salt"), iaa.CoarseSalt((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.Pepper((0.01, 0.05), name="Pepper"), iaa.CoarsePepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarsePepper"), iaa.Invert(0.1, name="Invert"), # ContrastNormalization iaa.JpegCompression((50, 99), name="JpegCompression") ] augmenters_artistic = [ iaa.Cartoon(name="Cartoon") ] augmenters_blend = [ iaa.BlendAlpha((0.01, 0.99), iaa.Identity(), name="Alpha"), iaa.BlendAlphaElementwise((0.01, 0.99), iaa.Identity(), name="AlphaElementwise"), iaa.BlendAlphaSimplexNoise(iaa.Identity(), name="SimplexNoiseAlpha"), iaa.BlendAlphaFrequencyNoise((-2.0, 2.0), iaa.Identity(), name="FrequencyNoiseAlpha"), iaa.BlendAlphaSomeColors(iaa.Identity(), name="BlendAlphaSomeColors"), iaa.BlendAlphaHorizontalLinearGradient(iaa.Identity(), name="BlendAlphaHorizontalLinearGradient"), iaa.BlendAlphaVerticalLinearGradient(iaa.Identity(), name="BlendAlphaVerticalLinearGradient"), iaa.BlendAlphaRegularGrid(nb_rows=(2, 8), nb_cols=(2, 8), foreground=iaa.Identity(), name="BlendAlphaRegularGrid"), iaa.BlendAlphaCheckerboard(nb_rows=(2, 8), nb_cols=(2, 8), foreground=iaa.Identity(), name="BlendAlphaCheckerboard"), # TODO BlendAlphaSegMapClassId # TODO BlendAlphaBoundingBoxes ] augmenters_blur = [ iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"), iaa.MotionBlur(k=(3, 11), name="MotionBlur"), iaa.MeanShiftBlur(spatial_radius=(5.0, 40.0), color_radius=(5.0, 40.0), name="MeanShiftBlur") ] augmenters_collections = [ iaa.RandAugment(n=2, m=(6, 12), name="RandAugment") ] augmenters_color = [ # InColorspace (deprecated) iaa.WithColorspace(to_colorspace="HSV", children=iaa.Noop(), name="WithColorspace"), iaa.WithBrightnessChannels(iaa.Identity(), name="WithBrightnessChannels"), iaa.MultiplyAndAddToBrightness(mul=(0.7, 1.3), add=(-30, 30), name="MultiplyAndAddToBrightness"), iaa.MultiplyBrightness((0.7, 1.3), name="MultiplyBrightness"), iaa.AddToBrightness((-30, 30), name="AddToBrightness"), iaa.WithHueAndSaturation(children=iaa.Noop(), name="WithHueAndSaturation"), iaa.MultiplyHueAndSaturation((0.8, 1.2), name="MultiplyHueAndSaturation"), iaa.MultiplyHue((-1.0, 1.0), name="MultiplyHue"), iaa.MultiplySaturation((0.8, 1.2), name="MultiplySaturation"), iaa.RemoveSaturation((0.01, 0.99), name="RemoveSaturation"), iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"), iaa.AddToHue((-10, 10), name="AddToHue"), iaa.AddToSaturation((-10, 10), name="AddToSaturation"), iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"), iaa.Grayscale((0.01, 0.99), name="Grayscale"), iaa.KMeansColorQuantization((2, 16), name="KMeansColorQuantization"), iaa.UniformColorQuantization((2, 16), name="UniformColorQuantization"), iaa.UniformColorQuantizationToNBits((1, 7), name="UniformQuantizationToNBits"), iaa.Posterize((1, 7), name="Posterize") ] augmenters_contrast = [ iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"), iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.25, 0.75), name="SigmoidContrast"), iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"), iaa.LinearContrast((0.5, 1.5), name="LinearContrast"), iaa.AllChannelsCLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), name="AllChannelsCLAHE"), iaa.CLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), to_colorspace="HSV", name="CLAHE"), iaa.AllChannelsHistogramEqualization(name="AllChannelsHistogramEqualization"), iaa.HistogramEqualization(to_colorspace="HSV", name="HistogramEqualization"), ] augmenters_convolutional = [ iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), name="Convolve_3x3"), iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"), iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"), iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect") ] augmenters_edges = [ iaa.Canny(alpha=(0.01, 0.99), name="Canny") ] augmenters_flip = [ iaa.Fliplr(1.0, name="Fliplr"), iaa.Flipud(1.0, name="Flipud") ] augmenters_geometric = [ iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10), shear=(-10, 10), order=0, mode="constant", cval=(0, 255), name="Affine_order_0_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), name="Affine_order_1_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10), shear=(-10, 10), order=3, mode="constant", cval=(0, 255), name="Affine_order_3_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10), shear=(-10, 10), order=1, mode="edge", cval=(0, 255), name="Affine_order_1_edge"), iaa.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), backend="skimage", name="Affine_order_1_constant_skimage"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="constant", name="PiecewiseAffine_4x4_order_1_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=0, mode="constant", name="PiecewiseAffine_4x4_order_0_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="edge", name="PiecewiseAffine_4x4_order_1_edge"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=8, nb_cols=8, order=1, mode="constant", name="PiecewiseAffine_8x8_order_1_constant"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=False, name="PerspectiveTransform"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=True, name="PerspectiveTransform_keep_size"), iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=0, mode="constant", cval=0, name="ElasticTransformation_order_0_constant"), iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="constant", cval=0, name="ElasticTransformation_order_1_constant"), iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="nearest", cval=0, name="ElasticTransformation_order_1_nearest"), iaa.ElasticTransformation(alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="reflect", cval=0, name="ElasticTransformation_order_1_reflect"), iaa.Rot90((1, 3), keep_size=False, name="Rot90"), iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size"), iaa.WithPolarWarping(iaa.Identity(), name="WithPolarWarping"), iaa.Jigsaw(nb_rows=(3, 8), nb_cols=(3, 8), max_steps=1, name="Jigsaw") ] augmenters_pooling = [ iaa.AveragePooling(kernel_size=(1, 16), keep_size=False, name="AveragePooling"), iaa.AveragePooling(kernel_size=(1, 16), keep_size=True, name="AveragePooling_keep_size"), iaa.MaxPooling(kernel_size=(1, 16), keep_size=False, name="MaxPooling"), iaa.MaxPooling(kernel_size=(1, 16), keep_size=True, name="MaxPooling_keep_size"), iaa.MinPooling(kernel_size=(1, 16), keep_size=False, name="MinPooling"), iaa.MinPooling(kernel_size=(1, 16), keep_size=True, name="MinPooling_keep_size"), iaa.MedianPooling(kernel_size=(1, 16), keep_size=False, name="MedianPooling"), iaa.MedianPooling(kernel_size=(1, 16), keep_size=True, name="MedianPooling_keep_size") ] augmenters_imgcorruptlike = [ iaa.imgcorruptlike.GaussianNoise(severity=(1, 5), name="imgcorruptlike.GaussianNoise"), iaa.imgcorruptlike.ShotNoise(severity=(1, 5), name="imgcorruptlike.ShotNoise"), iaa.imgcorruptlike.ImpulseNoise(severity=(1, 5), name="imgcorruptlike.ImpulseNoise"), iaa.imgcorruptlike.SpeckleNoise(severity=(1, 5), name="imgcorruptlike.SpeckleNoise"), iaa.imgcorruptlike.GaussianBlur(severity=(1, 5), name="imgcorruptlike.GaussianBlur"), iaa.imgcorruptlike.GlassBlur(severity=(1, 5), name="imgcorruptlike.GlassBlur"), iaa.imgcorruptlike.DefocusBlur(severity=(1, 5), name="imgcorruptlike.DefocusBlur"), iaa.imgcorruptlike.MotionBlur(severity=(1, 5), name="imgcorruptlike.MotionBlur"), iaa.imgcorruptlike.ZoomBlur(severity=(1, 5), name="imgcorruptlike.ZoomBlur"), iaa.imgcorruptlike.Fog(severity=(1, 5), name="imgcorruptlike.Fog"), iaa.imgcorruptlike.Frost(severity=(1, 5), name="imgcorruptlike.Frost"), iaa.imgcorruptlike.Snow(severity=(1, 5), name="imgcorruptlike.Snow"), iaa.imgcorruptlike.Spatter(severity=(1, 5), name="imgcorruptlike.Spatter"), iaa.imgcorruptlike.Contrast(severity=(1, 5), name="imgcorruptlike.Contrast"), iaa.imgcorruptlike.Brightness(severity=(1, 5), name="imgcorruptlike.Brightness"), iaa.imgcorruptlike.Saturate(severity=(1, 5), name="imgcorruptlike.Saturate"), iaa.imgcorruptlike.JpegCompression(severity=(1, 5), name="imgcorruptlike.JpegCompression"), iaa.imgcorruptlike.Pixelate(severity=(1, 5), name="imgcorruptlike.Pixelate"), iaa.imgcorruptlike.ElasticTransform(severity=(1, 5), name="imgcorruptlike.ElasticTransform") ] augmenters_pillike = [ iaa.pillike.Solarize(p=1.0, threshold=(32, 128), name="pillike.Solarize"), iaa.pillike.Posterize((1, 7), name="pillike.Posterize"), iaa.pillike.Equalize(name="pillike.Equalize"), iaa.pillike.Autocontrast(name="pillike.Autocontrast"), iaa.pillike.EnhanceColor((0.0, 3.0), name="pillike.EnhanceColor"), iaa.pillike.EnhanceContrast((0.0, 3.0), name="pillike.EnhanceContrast"), iaa.pillike.EnhanceBrightness((0.0, 3.0), name="pillike.EnhanceBrightness"), iaa.pillike.EnhanceSharpness((0.0, 3.0), name="pillike.EnhanceSharpness"), iaa.pillike.FilterBlur(name="pillike.FilterBlur"), iaa.pillike.FilterSmooth(name="pillike.FilterSmooth"), iaa.pillike.FilterSmoothMore(name="pillike.FilterSmoothMore"), iaa.pillike.FilterEdgeEnhance(name="pillike.FilterEdgeEnhance"), iaa.pillike.FilterEdgeEnhanceMore(name="pillike.FilterEdgeEnhanceMore"), iaa.pillike.FilterFindEdges(name="pillike.FilterFindEdges"), iaa.pillike.FilterContour(name="pillike.FilterContour"), iaa.pillike.FilterEmboss(name="pillike.FilterEmboss"), iaa.pillike.FilterSharpen(name="pillike.FilterSharpen"), iaa.pillike.FilterDetail(name="pillike.FilterDetail"), iaa.pillike.Affine(scale=(0.9, 1.1), translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, rotate=(-10, 10), shear=(-10, 10), fillcolor=(0, 255), name="pillike.Affine"), ] augmenters_segmentation = [ iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="cubic", name="Superpixels_max_size_64_cubic"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="linear", name="Superpixels_max_size_64_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=128, interpolation="linear", name="Superpixels_max_size_128_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=224, interpolation="linear", name="Superpixels_max_size_224_linear"), iaa.UniformVoronoi(n_points=(250, 1000), name="UniformVoronoi"), iaa.RegularGridVoronoi(n_rows=(16, 31), n_cols=(16, 31), name="RegularGridVoronoi"), iaa.RelativeRegularGridVoronoi(n_rows_frac=(0.07, 0.14), n_cols_frac=(0.07, 0.14), name="RelativeRegularGridVoronoi"), ] augmenters_size = [ iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"), iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"), iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="CropAndPad"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="CropAndPad_edge"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), name="CropAndPad_keep_size"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="Pad"), iaa.Pad(percent=(0.05, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="Pad_edge"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), name="Pad_keep_size"), iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"), iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"), iaa.PadToFixedSize(width=width+10, height=height+10, pad_mode="constant", pad_cval=(0, 255), name="PadToFixedSize"), iaa.CropToFixedSize(width=width-10, height=height-10, name="CropToFixedSize"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height-10, width=width-10), interpolation="nearest", name="KeepSizeByResize_CropToFixedSize_nearest"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height-10, width=width-10), interpolation="linear", name="KeepSizeByResize_CropToFixedSize_linear"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height-10, width=width-10), interpolation="cubic", name="KeepSizeByResize_CropToFixedSize_cubic"), ] augmenters_weather = [ iaa.FastSnowyLandscape(lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0), name="FastSnowyLandscape"), iaa.Clouds(name="Clouds"), iaa.Fog(name="Fog"), iaa.CloudLayer(intensity_mean=(196, 255), intensity_freq_exponent=(-2.5, -2.0), intensity_coarse_scale=10, alpha_min=0, alpha_multiplier=(0.25, 0.75), alpha_size_px_max=(2, 8), alpha_freq_exponent=(-2.5, -2.0), sparsity=(0.8, 1.0), density_multiplier=(0.5, 1.0), name="CloudLayer"), iaa.Snowflakes(name="Snowflakes"), iaa.SnowflakesLayer(density=(0.005, 0.075), density_uniformity=(0.3, 0.9), flake_size=(0.2, 0.7), flake_size_uniformity=(0.4, 0.8), angle=(-30, 30), speed=(0.007, 0.03), blur_sigma_fraction=(0.0001, 0.001), name="SnowflakesLayer"), iaa.Rain(name="Rain"), iaa.RainLayer(density=(0.03, 0.14), density_uniformity=(0.8, 1.0), drop_size=(0.01, 0.02), drop_size_uniformity=(0.2, 0.5), angle=(-15, 15), speed=(0.04, 0.20), blur_sigma_fraction=(0.001, 0.001), name="RainLayer") ] augmenters = ( augmenters_meta + augmenters_arithmetic + augmenters_artistic + augmenters_blend + augmenters_blur + augmenters_collections + augmenters_color + augmenters_contrast + augmenters_convolutional + augmenters_edges + augmenters_flip + augmenters_geometric + augmenters_pooling + augmenters_imgcorruptlike + augmenters_pillike + augmenters_segmentation + augmenters_size + augmenters_weather ) if only_augmenters is not None: augmenters_reduced = [] for augmenter in augmenters: if any([re.search(pattern, augmenter.name) for pattern in only_augmenters]): augmenters_reduced.append(augmenter) augmenters = augmenters_reduced return augmenters
def merge_bg(self, img, label, bg_dir): sometimes = lambda aug: iaa.Sometimes(0.5, aug) # merge bg random_bg = np.random.choice([0, 1, 2]) list_sample_bg = glob.glob(bg_dir+'/*.jpg') + glob.glob(bg_dir+'/*/*.jpg') + glob.glob(bg_dir+'/*/*/*.jpg') + glob.glob(bg_dir+'/*/*/*/*.jpg') # 0: original # 1: list_sample_bg # 2: pure bg if random_bg == 1: bg_path = np.random.choice(list_sample_bg) bg = cv2.imread(bg_path, cv2.IMREAD_COLOR) bg = cv2.resize(bg, (self.input_size, self.input_size), interpolation=cv2.INTER_LINEAR) # bg = bg.astype(np.float32) # [:, :, ::-1] # RGB to BGR!!! elif random_bg == 2: # Generate Bg bg = np.random.randint(255, size=3) bg = bg.reshape(1,1,3) bg = np.repeat(bg, self.input_size, axis=0) bg = np.repeat(bg, self.input_size, axis=1) bg = bg.astype(np.uint8) # Augmentation bg_seq = iaa.Sequential( [ # 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((1, 6), [ 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, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha(iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.OneOf([ iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images iaa.AdditiveLaplaceNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), iaa.AdditivePoissonNoise(lam=(0.0, 4.0), per_channel=0.5) ]), 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.2), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.1, per_channel=True), # invert color channels iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.ContrastNormalization((0.5, 2.0)) ) ]), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) iaa.Add((-25, 25), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.OneOf([ iaa.ImpulseNoise((0.01, 0.1)), iaa.SaltAndPepper((0.01, 0.1), per_channel=0.2), ]), iaa.JpegCompression(), ], random_order=True ), 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.JpegCompression(), iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), ]), ], random_order=False ) bg = bg_seq.augment_image(bg) if random_bg >= 1: bg = torch.as_tensor(bg.astype(np.float32)) bg = torch.transpose(torch.transpose(bg, 1, 2), 0, 1) img = img * label + bg * (1 - label) return img # Bacon
def __call__(self, sample): image, pose = sample['image'], sample['pose'].reshape([-1, 2]) sometimes = lambda aug: iaa.Sometimes(0.3, aug) seq = iaa.Sequential( [ # Apply the following augmenters to most images. sometimes( iaa.CropAndPad(percent=(-0.25, 0.25), pad_mode=["edge"], keep_size=False)), sometimes( iaa.Affine(scale={ "x": (0.75, 1.25), "y": (0.75, 1.25) }, translate_percent={ "x": (-0.25, 0.25), "y": (-0.25, 0.25) }, rotate=(-45, 45), shear=(-5, 5), order=[0, 1], cval=(0, 255), mode=ia.ALL)), iaa.SomeOf( (0, 3), [ iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), iaa.MotionBlur(k=5, angle=[-45, 45]) ]), iaa.OneOf([ iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.AdditivePoissonNoise(lam=(0, 8), per_channel=True), ]), iaa.OneOf([ iaa.Add((-10, 10), per_channel=0.5), iaa.Multiply((0.2, 1.2), per_channel=0.5), iaa.ContrastNormalization( (0.5, 2.0), per_channel=0.5), ]), ], # do all of the above augmentations in random order random_order=True) ], # do all of the above augmentations in random order random_order=True) # augmentation choices seq_det = seq.to_deterministic() image_aug = seq_det.augment_images([image])[0] keypoints_aug = seq_det.augment_keypoints( [self.pose2keypoints(image, pose)])[0] return {'image': image_aug, 'pose': self.keypoints2pose(keypoints_aug)}
def main(args): # Print settings for k, v in vars(args).items(): print(f'{k}: {v}') display_step = 5 num_classes = 8 size = (224, 224, 3) # size of images # Runtime initialization will not allocate all memory on GPU physical_devices = tf.config.list_physical_devices('GPU') try: tf.config.experimental.set_memory_growth(physical_devices[0], True) except: # Invalid device or cannot modify virtual devices once initialized. pass # Create checkpoints dir os.makedirs('saved_models', exist_ok=True) optimizer = optimizers.SGD(learning_rate=args.learning_rate, momentum=0.9) loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=False) # model = models.vgg16(input_shape=size, num_classes=num_classes, classifier_activation='softmax') model = models.resnet50(input_shape=size, num_classes=num_classes, classifier_activation='softmax') model.build(input_shape=size) model.summary() if args.checkpoints: if os.path.exists(args.checkpoints): print(f'Loading checkpoints: {args.checkpoints}') model.load_weights(args.checkpoints) else: print(f'Checkpoints `{args.checkpoints}` not found', file=sys.stderr) os.makedirs("logs/scalars/", exist_ok=True) logdir = "logs/scalars/" + datetime.now().strftime( "%Y%m%d-%H%M%S") + f"-{args.name}" summary_writer = tf.summary.create_file_writer(logdir) if args.train: # Same augs as C++ train_aug = iaa.Sequential([ iaa.Resize(size=size[:-1], interpolation='cubic'), iaa.Fliplr(p=0.5), iaa.Flipud(p=0.5), iaa.Rotate(rotate=(-180, 180)), iaa.AdditivePoissonNoise(lam=(0, 10)), iaa.GammaContrast(gamma=(.8, 1.5)), iaa.GaussianBlur(sigma=(.0, .8)), iaa.CoarseDropout(p=(.02, .1), size_percent=(0.02, 0.05), per_channel=0.5), ]) val_aug = iaa.Sequential( [iaa.Resize(size=size[:-1], interpolation='cubic')]) training_dataset = ISICClassification(args.dataset, 'training', args.batch_size, train_aug) training_tfdata = training_dataset.map_samples(args.epochs) training_iter = iter(training_tfdata) validation_dataset = ISICClassification(args.dataset, 'validation', args.batch_size, val_aug, shuffle=False) validation_tfdata = validation_dataset.map_samples(args.epochs) validation_iter = iter(validation_tfdata) train_loss = tf.keras.metrics.Mean(name='train_loss') train_metric = tf.keras.metrics.SparseCategoricalAccuracy( name='train_accuracy') val_metric = tf.keras.metrics.SparseCategoricalAccuracy( name='val_accuracy') best_accuracy = 0. for e in range(1, args.epochs + 1): train_loss.reset_states() train_metric.reset_states() val_metric.reset_states() total_preds = [] total_labels = [] for step in range(1, len(training_dataset)): images, labels = next(training_iter) # Run the optimization to update W and b values with tf.GradientTape() as tape: pred = model(images) loss = loss_fn(labels, pred) total_preds.append(pred) total_labels.append(labels) gradients = tape.gradient(loss, model.trainable_variables) # Update W and b following gradients optimizer.apply_gradients( zip(gradients, model.trainable_variables)) # Log loss and metric train_loss.update_state(loss) train_metric.update_state(labels, pred) if step % display_step == 0: print( "\rTraining {:d}/{:d} (batch {:d}/{:d}) - Loss: {:.4f} - Accuracy: {:.4f}" .format(e, args.epochs, step, len(training_dataset), train_loss.result(), train_metric.result()), end="", flush=True) cm = utils.calculate_confusion_matrix( tf.concat(total_labels, axis=0), tf.concat(total_preds, axis=0)) with summary_writer.as_default(): tf.summary.scalar('loss/' + train_loss.name, train_loss.result(), step=e - 1) tf.summary.scalar('accuracy/' + train_metric.name, train_metric.result(), step=e - 1) tf.summary.image("cm/training_cm", cm, step=e) total_preds = [] total_labels = [] # Do validation print("\nValidation {:d}/{:d}".format(e, args.epochs), end="", flush=True) for step in range(1, len(validation_dataset)): images, labels = next(validation_iter) pred = model(images) val_metric.update_state(labels, pred) total_preds.append(pred) total_labels.append(labels) cm = utils.calculate_confusion_matrix( tf.concat(total_labels, axis=0), tf.concat(total_preds, axis=0)) with summary_writer.as_default(): tf.summary.scalar('accuracy/' + val_metric.name, val_metric.result(), step=e - 1) tf.summary.image("cm/validation_cm", cm, step=e) # Compute accuracy and save checkpoints accuracy = val_metric.result() print(" - Accuracy: {:.4f}".format(accuracy), flush=True) if accuracy > best_accuracy: print( f"Saving checkpoints (accuracy: {accuracy:.4f} > {best_accuracy:.4f})", flush=True) best_accuracy = accuracy model.save_weights(f'saved_models/{args.name}.h5') if args.test: # Test model on test set test_aug = iaa.Sequential( [iaa.Resize(size=size[:-1], interpolation='cubic')]) test_dataset = ISICClassification(args.dataset, 'test', args.batch_size, test_aug) test_tfdata = test_dataset.map_samples(1) tensorboard = keras.callbacks.TensorBoard(log_dir=logdir) results = model.evaluate(test_tfdata, verbose=1, callbacks=[tensorboard]) print("Test set loss and accuracy:", results)
lambda percent, x, y: iaa.ReplaceElementwise(percent, [x, y]), # Adds laplace noise (somewhere between gaussian and salt and peeper noise) to an image, sampled once per pixel # from a laplace distribution Laplace(0, s), where s is sampled per image and varies between lo and hi*255 for # percent of all images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same laplace distribution: "Additive_Laplace_Noise": lambda lo, hi, percent: iaa.AdditiveLaplaceNoise(scale=(lo, hi), per_channel=percent), # Adds poisson noise (similar to gaussian but different distribution) to an image, sampled once per pixel from # a poisson distribution Poisson(s), where s is sampled per image and varies between lo and hi for percent of # all images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same poisson distribution: "Additive_Poisson_Noise": lambda lo, hi, percent: iaa.AdditivePoissonNoise(lam=(lo, hi), per_channel=percent), # Adds salt and pepper noise to an image, i.e. some white-ish and black-ish pixels. # Replaces percent of all pixels with salt and pepper noise "Salt_And_Pepper": lambda percent: iaa.SaltAndPepper(percent), # Adds coarse salt and pepper noise to image, i.e. rectangles that contain noisy white-ish and black-ish pixels # Replaces percent of all pixels with salt/pepper in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Salt_And_Pepper": lambda percent, lo, hi: iaa.CoarseSaltAndPepper(percent, size_percent=(lo, hi)), # Adds salt noise to an image, i.e white-ish pixels # Replaces percent of all pixels with salt noise
class AugmentationScheme: # Dictionary containing all possible augmentation functions Augmentations = { # Convert images to HSV, then increase each pixel's Hue (H), Saturation (S) or Value/lightness (V) [0, 1, 2] # value by an amount in between lo and hi: "HSV": lambda channel, lo, hi: iaa.WithColorspace( to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels(channel, iaa.Add((lo, hi)))), # The augmenter first transforms images to HSV color space, then adds random values (lo to hi) # to the H and S channels and afterwards converts back to RGB. # (independently per channel and the same value for all pixels within that channel) "Add_To_Hue_And_Saturation": lambda lo, hi: iaa.AddToHueAndSaturation((lo, hi), per_channel=True), # Increase each pixel’s channel-value (redness/greenness/blueness) [0, 1, 2] by value in between lo and hi: "Increase_Channel": lambda channel, lo, hi: iaa.WithChannels(channel, iaa.Add((lo, hi))), # Rotate each image’s channel [R=0, G=1, B=2] by value in between lo and hi degrees: "Rotate_Channel": lambda channel, lo, hi: iaa.WithChannels(channel, iaa.Affine(rotate=(lo, hi))), # Augmenter that never changes input images (“no operation”). "No_Operation": iaa.Noop(), # Pads images, i.e. adds columns/rows to them. Pads image by value in between lo and hi # percent relative to its original size (only accepts positive values in range[0, 1]): # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) # NOTE: automatically resizes images back to their original size after it has augmented them. "Pad_Percent": lambda lo, hi, s_i: iaa.Pad( percent=(lo, hi), keep_size=True, sample_independently=s_i), # Pads images by a number of pixels between lo and hi # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) "Pad_Pixels": lambda lo, hi, s_i: iaa.Pad( px=(lo, hi), keep_size=True, sample_independently=s_i), # Crops/cuts away pixels at the sides of the image. # Crops images by value in between lo and hi (only accepts positive values in range[0, 1]): # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) # NOTE: automatically resizes images back to their original size after it has augmented them. "Crop_Percent": lambda lo, hi, s_i: iaa.Crop( percent=(lo, hi), keep_size=True, sample_independently=s_i), # Crops images by a number of pixels between lo and hi # If s_i is false, The value will be sampled once per image and used for all sides # (i.e. all sides gain/lose the same number of rows/columns) "Crop_Pixels": lambda lo, hi, s_i: iaa.Crop( px=(lo, hi), keep_size=True, sample_independently=s_i), # Flip/mirror percent (i.e 0.5) of the input images horizontally # The default probability is 0, so to flip all images, percent=1 "Flip_lr": iaa.Fliplr(1), # Flip/mirror percent (i.e 0.5) of the input images vertically # The default probability is 0, so to flip all images, percent=1 "Flip_ud": iaa.Flipud(1), # Completely or partially transform images to their superpixel representation. # Generate s_pix_lo to s_pix_hi superpixels per image. Replace each superpixel with a probability between # prob_lo and prob_hi with range[0, 1] (sampled once per image) by its average pixel color. "Superpixels": lambda prob_lo, prob_hi, s_pix_lo, s_pix_hi: iaa.Superpixels( p_replace=(prob_lo, prob_hi), n_segments=(s_pix_lo, s_pix_hi)), # Change images to grayscale and overlay them with the original image by varying strengths, # effectively removing alpha_lo to alpha_hi of the color: "Grayscale": lambda alpha_lo, alpha_hi: iaa.Grayscale(alpha=(alpha_lo, alpha_hi)), # Blur each image with a gaussian kernel with a sigma between sigma_lo and sigma_hi: "Gaussian_Blur": lambda sigma_lo, sigma_hi: iaa.GaussianBlur(sigma=(sigma_lo, sigma_hi) ), # Blur each image using a mean over neighbourhoods that have random sizes, # which can vary between h_lo and h_hi in height and w_lo and w_hi in width: "Average_Blur": lambda h_lo, h_hi, w_lo, w_hi: iaa.AverageBlur(k=((h_lo, h_hi), (w_lo, w_hi))), # Blur each image using a median over neighbourhoods that have a random size between lo x lo and hi x hi: "Median_Blur": lambda lo, hi: iaa.MedianBlur(k=(lo, hi)), # Sharpen an image, then overlay the results with the original using an alpha between alpha_lo and alpha_hi: "Sharpen": lambda alpha_lo, alpha_hi, lightness_lo, lightness_hi: iaa. Sharpen(alpha=(alpha_lo, alpha_hi), lightness=(lightness_lo, lightness_hi)), # Emboss an image, then overlay the results with the original using an alpha between alpha_lo and alpha_hi: "Emboss": lambda alpha_lo, alpha_hi, strength_lo, strength_hi: iaa.Emboss( alpha=(alpha_lo, alpha_hi), strength=(strength_lo, strength_hi)), # Detect edges in images, turning them into black and white images and # then overlay these with the original images using random alphas between alpha_lo and alpha_hi: "Detect_Edges": lambda alpha_lo, alpha_hi: iaa.EdgeDetect(alpha=(alpha_lo, alpha_hi)), # Detect edges having random directions between dir_lo and dir_hi (i.e (0.0, 1.0) = 0 to 360 degrees) in # images, turning the images into black and white versions and then overlay these with the original images # using random alphas between alpha_lo and alpha_hi: "Directed_edge_Detect": lambda alpha_lo, alpha_hi, dir_lo, dir_hi: iaa.DirectedEdgeDetect( alpha=(alpha_lo, alpha_hi), direction=(dir_lo, dir_hi)), # Add random values between lo and hi to images. In percent of all images the values differ per channel # (3 sampled value). In the rest of the images the value is the same for all channels: "Add": lambda lo, hi, percent: iaa.Add((lo, hi), per_channel=percent), # Adds random values between lo and hi to images, with each value being sampled per pixel. # In percent of all images the values differ per channel (3 sampled value). In the rest of the images # the value is the same for all channels: "Add_Element_Wise": lambda lo, hi, percent: iaa.AddElementwise( (lo, hi), per_channel=percent), # Add gaussian noise (aka white noise) to an image, sampled once per pixel from a normal # distribution N(0, s), where s is sampled per image and varies between lo and hi*255 for percent of all # images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same normal distribution: "Additive_Gaussian_Noise": lambda lo, hi, percent: iaa.AdditiveGaussianNoise(scale=(lo, hi), per_channel=percent), # Multiply in percent of all images each pixel with random values between lo and hi and multiply # the pixels in the rest of the images channel-wise, # i.e. sample one multiplier independently per channel and pixel: "Multiply": lambda lo, hi, percent: iaa.Multiply((lo, hi), per_channel=percent), # Multiply values of pixels with possibly different values for neighbouring pixels, # making each pixel darker or brighter. Multiply each pixel with a random value between lo and hi: "Multiply_Element_Wise": lambda lo, hi, percent: iaa.MultiplyElementwise( (0.5, 1.5), per_channel=0.5), # Augmenter that sets a certain fraction of pixels in images to zero. # Sample per image a value p from the range lo<=p<=hi and then drop p percent of all pixels in the image # (i.e. convert them to black pixels), but do this independently per channel in percent of all images "Dropout": lambda lo, hi, percent: iaa.Dropout(p=(lo, hi), per_channel=percent), # Augmenter that sets rectangular areas within images to zero. # Drop d_lo to d_hi percent of all pixels by converting them to black pixels, # but do that on a lower-resolution version of the image that has s_lo to s_hi percent of the original size, # Also do this in percent of all images channel-wise, so that only the information of some # channels is set to 0 while others remain untouched: "Coarse_Dropout": lambda d_lo, d_hi, s_lo, s_hi, percent: iaa.CoarseDropout( (d_lo, d_hi), size_percent=(s_hi, s_hi), per_channel=percent), # Augmenter that inverts all values in images, i.e. sets a pixel from value v to 255-v. # For c_percent of all images, invert all pixels in these images channel-wise with probability=i_percent # (per image). In the rest of the images, invert i_percent of all channels: "Invert": lambda i_percent, c_percent: iaa.Invert(i_percent, per_channel=c_percent), # Augmenter that changes the contrast of images. # Normalize contrast by a factor of lo to hi, sampled randomly per image # and for percent of all images also independently per channel: "Contrast_Normalisation": lambda lo, hi, percent: iaa.ContrastNormalization( (lo, hi), per_channel=percent), # Scale images to a value of lo to hi percent of their original size but do this independently per axis: "Scale": lambda x_lo, x_hi, y_lo, y_hi: iaa.Affine(scale={ "x": (x_lo, x_hi), "y": (y_lo, y_hi) }), # Translate images by lo to hi percent on x-axis and y-axis independently: "Translate_Percent": lambda x_lo, x_hi, y_lo, y_hi: iaa.Affine(translate_percent={ "x": (x_lo, x_hi), "y": (y_lo, y_hi) }), # Translate images by lo to hi pixels on x-axis and y-axis independently: "Translate_Pixels": lambda x_lo, x_hi, y_lo, y_hi: iaa.Affine(translate_px={ "x": (x_lo, x_hi), "y": (y_lo, y_hi) }), # Rotate images by lo to hi degrees: "Rotate": lambda lo, hi: iaa.Affine(rotate=(lo, hi)), # Shear images by lo to hi degrees: "Shear": lambda lo, hi: iaa.Affine(shear=(lo, hi)), # Augmenter that places a regular grid of points on an image and randomly moves the neighbourhood of # these point around via affine transformations. This leads to local distortions. # Distort images locally by moving points around, each with a distance v (percent relative to image size), # where v is sampled per point from N(0, z) z is sampled per image from the range lo to hi: "Piecewise_Affine": lambda lo, hi: iaa.PiecewiseAffine(scale=(lo, hi)), # Augmenter to transform images by moving pixels locally around using displacement fields. # Distort images locally by moving individual pixels around following a distortions field with # strength sigma_lo to sigma_hi. The strength of the movement is sampled per pixel from the range # alpha_lo to alpha_hi: "Elastic_Transformation": lambda alpha_lo, alpha_hi, sigma_lo, sigma_hi: iaa. ElasticTransformation(alpha=(alpha_lo, alpha_hi), sigma=(sigma_lo, sigma_hi)), # Weather augmenters are computationally expensive and will not work effectively on certain data sets # Augmenter to draw clouds in images. "Clouds": iaa.Clouds(), # Augmenter to draw fog in images. "Fog": iaa.Fog(), # Augmenter to add falling snowflakes to images. "Snowflakes": iaa.Snowflakes(), # Replaces percent of all pixels in an image by either x or y "Replace_Element_Wise": lambda percent, x, y: iaa.ReplaceElementwise(percent, [x, y]), # Adds laplace noise (somewhere between gaussian and salt and peeper noise) to an image, sampled once per pixel # from a laplace distribution Laplace(0, s), where s is sampled per image and varies between lo and hi*255 for # percent of all images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same laplace distribution: "Additive_Laplace_Noise": lambda lo, hi, percent: iaa.AdditiveLaplaceNoise(scale=(lo, hi), per_channel=percent), # Adds poisson noise (similar to gaussian but different distribution) to an image, sampled once per pixel from # a poisson distribution Poisson(s), where s is sampled per image and varies between lo and hi for percent of # all images (sampled once for all channels) and sampled three (RGB) times (channel-wise) # for the rest from the same poisson distribution: "Additive_Poisson_Noise": lambda lo, hi, percent: iaa.AdditivePoissonNoise(lam=(lo, hi), per_channel=percent), # Adds salt and pepper noise to an image, i.e. some white-ish and black-ish pixels. # Replaces percent of all pixels with salt and pepper noise "Salt_And_Pepper": lambda percent: iaa.SaltAndPepper(percent), # Adds coarse salt and pepper noise to image, i.e. rectangles that contain noisy white-ish and black-ish pixels # Replaces percent of all pixels with salt/pepper in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Salt_And_Pepper": lambda percent, lo, hi: iaa.CoarseSaltAndPepper(percent, size_percent=(lo, hi)), # Adds salt noise to an image, i.e white-ish pixels # Replaces percent of all pixels with salt noise "Salt": lambda percent: iaa.Salt(percent), # Adds coarse salt noise to image, i.e. rectangles that contain noisy white-ish pixels # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Salt": lambda percent, lo, hi: iaa.CoarseSalt(percent, size_percent=(lo, hi)), # Adds Pepper noise to an image, i.e Black-ish pixels # Replaces percent of all pixels with Pepper noise "Pepper": lambda percent: iaa.Pepper(percent), # Adds coarse pepper noise to image, i.e. rectangles that contain noisy black-ish pixels # Replaces percent of all pixels with salt in an image that has lo to hi percent of the input image size, # then upscales the results to the input image size, leading to large rectangular areas being replaced. "Coarse_Pepper": lambda percent, lo, hi: iaa.CoarsePepper(percent, size_percent=(lo, hi)), # In an alpha blending, two images are naively mixed. E.g. Let A be the foreground image, B be the background # image and a is the alpha value. Each pixel intensity is then computed as a * A_ij + (1-a) * B_ij. # Images passed in must be a numpy array of type (height, width, channel) "Blend_Alpha": lambda image_fg, image_bg, alpha: iaa.blend_alpha( image_fg, image_bg, alpha), # Blur/Denoise an image using a bilateral filter. # Bilateral filters blur homogeneous and textured areas, while trying to preserve edges. # Blurs all images using a bilateral filter with max distance d_lo to d_hi with ranges for sigma_colour # and sigma space being define by sc_lo/sc_hi and ss_lo/ss_hi "Bilateral_Blur": lambda d_lo, d_hi, sc_lo, sc_hi, ss_lo, ss_hi: iaa.BilateralBlur( d=(d_lo, d_hi), sigma_color=(sc_lo, sc_hi), sigma_space=(ss_lo, ss_hi)), # Augmenter that sharpens images and overlays the result with the original image. # Create a motion blur augmenter with kernel size of (kernel x kernel) and a blur angle of either x or y degrees # (randomly picked per image). "Motion_Blur": lambda kernel, x, y: iaa.MotionBlur(k=kernel, angle=[x, y]), # Augmenter to apply standard histogram equalization to images (similar to CLAHE) "Histogram_Equalization": iaa.HistogramEqualization(), # Augmenter to perform standard histogram equalization on images, applied to all channels of each input image "All_Channels_Histogram_Equalization": iaa.AllChannelsHistogramEqualization(), # Contrast Limited Adaptive Histogram Equalization (CLAHE). This augmenter applies CLAHE to images, a form of # histogram equalization that normalizes within local image patches. # Creates a CLAHE augmenter with clip limit uniformly sampled from [cl_lo..cl_hi], i.e. 1 is rather low contrast # and 50 is rather high contrast. Kernel sizes of SxS, where S is uniformly sampled from [t_lo..t_hi]. # Sampling happens once per image. (Note: more parameters are available for further specification) "CLAHE": lambda cl_lo, cl_hi, t_lo, t_hi: iaa.CLAHE( clip_limit=(cl_lo, cl_hi), tile_grid_size_px=(t_lo, t_hi)), # Contrast Limited Adaptive Histogram Equalization (refer above), applied to all channels of the input images. # CLAHE performs histogram equalization within image patches, i.e. over local neighbourhoods "All_Channels_CLAHE": lambda cl_lo, cl_hi, t_lo, t_hi: iaa.AllChannelsCLAHE( clip_limit=(cl_lo, cl_hi), tile_grid_size_px=(t_lo, t_hi)), # Augmenter that changes the contrast of images using a unique formula (using gamma). # Multiplier for gamma function is between lo and hi,, sampled randomly per image (higher values darken image) # For percent of all images values are sampled independently per channel. "Gamma_Contrast": lambda lo, hi, percent: iaa.GammaContrast( (lo, hi), per_channel=percent), # Augmenter that changes the contrast of images using a unique formula (linear). # Multiplier for linear function is between lo and hi, sampled randomly per image # For percent of all images values are sampled independently per channel. "Linear_Contrast": lambda lo, hi, percent: iaa.LinearContrast( (lo, hi), per_channel=percent), # Augmenter that changes the contrast of images using a unique formula (using log). # Multiplier for log function is between lo and hi, sampled randomly per image. # For percent of all images values are sampled independently per channel. # Values around 1.0 lead to a contrast-adjusted images. Values above 1.0 quickly lead to partially broken # images due to exceeding the datatype’s value range. "Log_Contrast": lambda lo, hi, percent: iaa.LogContrast((lo, hi), per_channel=percent), # Augmenter that changes the contrast of images using a unique formula (sigmoid). # Multiplier for sigmoid function is between lo and hi, sampled randomly per image. c_lo and c_hi decide the # cutoff value that shifts the sigmoid function in horizontal direction (Higher values mean that the switch # from dark to light pixels happens later, i.e. the pixels will remain darker). # For percent of all images values are sampled independently per channel: "Sigmoid_Contrast": lambda lo, hi, c_lo, c_hi, percent: iaa.SigmoidContrast( (lo, hi), (c_lo, c_hi), per_channel=percent), # Augmenter that calls a custom (lambda) function for each batch of input image. # Extracts Canny Edges from images (refer to description in CO) # Good default values for min and max are 100 and 200 'Custom_Canny_Edges': lambda min_val, max_val: iaa.Lambda(func_images=CO.Edges( min_value=min_val, max_value=max_val)), } # AugmentationScheme objects require images and labels. # 'augs' is a list that contains all data augmentations in the scheme def __init__(self): self.augs = [iaa.Flipud(1)] def __call__(self, image): image = np.array(image) aug_scheme = iaa.Sometimes( 0.5, iaa.SomeOf(random.randrange(1, len(self.augs) + 1), self.augs, random_order=True)) aug_img = self.aug_scheme.augment_image(image) # fixes negative strides aug_img = aug_img[..., ::1] - np.zeros_like(aug_img) return aug_img