Пример #1
0
def chapter_augmenters_removesaturation():
    fn_start = "color/removesaturation"

    aug = iaa.RemoveSaturation()
    run_and_save_augseq(fn_start + ".jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)

    aug = iaa.RemoveSaturation(1.0)
    run_and_save_augseq(fn_start + "_all.jpg",
                        aug, [ia.quokka(size=(128, 128)) for _ in range(8)],
                        cols=4,
                        rows=2)
Пример #2
0
def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = CharacterDataset()
    dataset_train.load_characters("train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = CharacterDataset()
    dataset_val.load_characters("val")
    dataset_val.prepare()

    #Augmentation
    aug = iaa.SomeOf(2, [
        iaa.AdditiveGaussianNoise(scale=(0, 0.10 * 255)),
        iaa.MotionBlur(),
        iaa.GaussianBlur(sigma=(0.0, 2.0)),
        iaa.RemoveSaturation(mul=(0, 0.5)),
        iaa.GammaContrast(),
        iaa.Rotate(rotate=(-45, 45)),
        iaa.PerspectiveTransform(scale=(0.01, 0.15)),
        iaa.JpegCompression(compression=(0, 75)),
        iaa.imgcorruptlike.Spatter(severity=(1, 4)),
        iaa.Rain(speed=(0.1, 0.3)),
        iaa.Fog()
    ])

    custom_callbacks = [
        ReduceLROnPlateau(monitor='val_loss',
                          factor=0.1,
                          patience=5,
                          verbose=1),
        EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)
    ]

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=100,
                layers='heads',
                augmentation=aug,
                custom_callbacks=custom_callbacks)
def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = PlateDataset()
    dataset_train.load_plates("train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = PlateDataset()
    dataset_val.load_plates("val")
    dataset_val.prepare()

    #Augmentation
    aug = iaa.OneOf([
        iaa.GaussianBlur(sigma=(0, 1.0)),
        iaa.MotionBlur(),
        iaa.RemoveSaturation((0.0, 0.5)),
        iaa.GammaContrast(),
        iaa.Rotate(rotate=(-45, 45)),
        iaa.PerspectiveTransform(scale=(0.01, 0.15)),
        iaa.SaltAndPepper(),
        iaa.JpegCompression(compression=(0, 75)),
        iaa.imgcorruptlike.Spatter(severity=(1, 4)),
        iaa.imgcorruptlike.DefocusBlur(severity=1)
    ])

    custom_callbacks = [
        ReduceLROnPlateau(monitor='val_loss',
                          factor=0.1,
                          patience=5,
                          verbose=1),
        EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)
    ]

    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train,
                dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=100,
                layers='all',
                augmentation=aug,
                custom_callbacks=custom_callbacks)
Пример #4
0
def main():
    urls = [
        ("https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/"
         "Sarcophilus_harrisii_taranna.jpg/"
         "320px-Sarcophilus_harrisii_taranna.jpg"),
        ("https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/"
         "Vincent_van_Gogh_-_Wheatfield_with_crows_-_Google_Art_Project.jpg/"
         "320px-Vincent_van_Gogh_-_Wheatfield_with_crows_-_Google_Art_Project"
         ".jpg"),
        ("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/"
         "Galerella_sanguinea_Zoo_Praha_2011-2.jpg/207px-Galerella_sanguinea_"
         "Zoo_Praha_2011-2.jpg"),
        ("https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/"
         "Ambrosius_Bosschaert_the_Elder_%28Dutch_-_Flower_Still_Life_-_"
         "Google_Art_Project.jpg/307px-Ambrosius_Bosschaert_the_Elder_%28"
         "Dutch_-_Flower_Still_Life_-_Google_Art_Project.jpg")
    ]

    image = imageio.imread(urls[3])

    aug = iaa.RemoveSaturation()
    images_aug = aug(images=[image] * (5*5))

    ia.imshow(ia.draw_grid(images_aug))
Пример #5
0
import imgaug.augmenters as iaa
import random

import numpy as np
import cv2
from PIL import Image

aug_transform = iaa.SomeOf((0, None), [
    iaa.OneOf([
        iaa.MultiplyAndAddToBrightness(mul=(0.3, 1.6), add=(-50, 50)),
        iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True),
        iaa.ChannelShuffle(0.5),
        iaa.RemoveSaturation(),
        iaa.Grayscale(alpha=(0.0, 1.0)),
        iaa.ChangeColorTemperature((1100, 35000)),
    ]),
    iaa.OneOf([
        iaa.MedianBlur(k=(3, 7)),
        iaa.BilateralBlur(
            d=(3, 10), sigma_color=(10, 250), sigma_space=(10, 250)),
        iaa.MotionBlur(k=(3, 9), angle=[-45, 45]),
        iaa.MeanShiftBlur(spatial_radius=(5.0, 10.0),
                          color_radius=(5.0, 10.0)),
        iaa.AllChannelsCLAHE(clip_limit=(1, 10)),
        iaa.AllChannelsHistogramEqualization(),
        iaa.GammaContrast((0.5, 1.5), per_channel=True),
        iaa.GammaContrast((0.5, 1.5)),
        iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6), per_channel=True),
        iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
        iaa.HistogramEqualization(),
        iaa.Sharpen(alpha=0.5)
Пример #6
0
                x_min = bb_box.x1
                y_min = bb_box.y1
                x_max = bb_box.x2
                y_max = bb_box.y2
                cls_id = bb_box.label
                x_cen, y_cen, w, h = xyxy2xywh(x_min, y_min, x_max, y_max)
                f.write("%d %.06f %.06f %.06f %.06f\n" %
                        (cls_id, x_cen, y_cen, w, h))


Width = 640
Height = 640

blur = iaa.AverageBlur(k=(2, 11))  #! 2~11 random
emboss = iaa.Emboss(alpha=(1.0, 1.0), strength=(2.0, 2.0))
gray = iaa.RemoveSaturation(from_colorspace=iaa.CSPACE_BGR)
contrast = iaa.AllChannelsCLAHE(clip_limit=(10, 10), per_channel=True)
bright = iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30))
color = iaa.pillike.EnhanceColor()
sharpen = iaa.Sharpen(alpha=(0.5, 1.0))  #! 0.5 ~ 1.0 random
edge = iaa.pillike.FilterEdgeEnhance()

augmentations = [[bright], [emboss], [color],
                 [edge]]  #! choice augmentation ##
rotates = [[iaa.Affine(rotate=90)], [iaa.Affine(rotate=180)],
           [iaa.Affine(rotate=270)]]
flip = iaa.Fliplr(1.0)  #! 100% left & right

dir = "C:\\Users\\jeongseokoon\\AI-hub\\data\\original\\"

save_aug_dir = "C:\\Users\\jeongseokoon\\AI-hub\\data\\images\\"  #! Absolute path
Пример #7
0
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 main():

    try:
        config_dirs_file = sys.argv[1] # directories file
        config_file = sys.argv[2]      # main params file
    except:
        print("Config file names not specified, setting them to default namess")
        config_dirs_file = "config_dirs.json"
        config_file = "config760.json"
    print(f'USING CONFIG FILES: config dirs:{config_dirs_file}  main config:{config_file}')    
    
    #print(type(feature_directory))
    C = cs760.loadas_json('config760.json')
    print("Running with parameters:", C)
    
    Cdirs = cs760.loadas_json(config_dirs_file)
    print("Directories:", Cdirs)
    
    C['dirs'] = Cdirs
    video_directory = C['dirs']['indir']
    feature_directory = C['dirs']['outdir']
    
    print(f'Creating feature file Dir: {feature_directory}')
    os.makedirs(feature_directory, exist_ok=True)        #if dir already exists will continue and WILL NOT delete existing files in that directory


    sometimes = lambda aug: iaa.Sometimes(C["augmentation_chance"][0], aug)
    sequential_list = [iaa.Sequential([sometimes(iaa.Fliplr(1.0))]), # horizontal flip
    iaa.Sequential([sometimes(iaa.Rotate(-5, 5))]), # rotate 5 degrees +/-
    iaa.Sequential([sometimes(iaa.CenterCropToAspectRatio(1.15))]),
    iaa.Sequential([sometimes(iaa.MultiplyBrightness((2.0, 2.0)))]), # increase brightness
    iaa.Sequential([sometimes(iaa.MultiplyHue((0.5, 1.5)))]), # change hue random
    iaa.Sequential([sometimes(iaa.RemoveSaturation(1.0))]), # effectively greyscale
    iaa.Sequential([sometimes(iaa.pillike.FilterContour())]), # edge detection
    iaa.Sequential([sometimes(iaa.AdditiveLaplaceNoise(scale=0.05*255, per_channel=True))]), # add colourful noise
    iaa.Sequential([sometimes(iaa.Invert(1))]) # invert colours
    ]


    print("Reading videos from " + video_directory)
    print("Outputting features to " + feature_directory)

    print("Loading pretrained CNN...")
    model = hub.KerasLayer(C["module_url"])  # can be used like any other kera layer including in other layers...
    print("Pretrained CNN Loaded OK")

    vids = cs760.list_files_pattern(video_directory, C["vid_type"])
    print(f'Processing {len(vids)} videos...')

    for i, vid in enumerate(vids):
        print(f'{i} Processing: {vid}')    
        vid_np = cs760.get_vid_frames(vid, 
                        video_directory, 
                        writejpgs=False,
                        writenpy=False,
                        returnnp=True)
        (framecount, frameheight, framewidth, channels) = vid_np.shape
        res_key = str(frameheight) + "-" + str(framewidth)
        #print(vid, vid_np.shape)
        outfile = os.path.splitext(vid)[0]
        
        print(f"Vid frames, h, w, c = {(framecount, frameheight, framewidth, channels)}")
        
        if C["crop_by_res"].get(res_key) is not None:
            vid_np_top = cs760.crop_image(vid_np, C["crop_by_res"][res_key])
            print(f"Cropped by resolution to {C['crop_by_res'][res_key]}")
        else:    
            vid_np_top = cs760.crop_image(vid_np, C["crop_top"])
            print(f"Cropped by default to {C['crop_top']}")

        outfile_top = outfile + "__TOP.pkl"

        for n in range((len(sequential_list) + 1)):
            if n != 0:
                vid_aug = sequential_list[n - 1](images=vid_np_top) # augments frames
                if type(vid_aug) is list:
                    vid_aug = np.asarray(vid_aug)
                batch = cs760.resize_batch(vid_aug, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                            inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                            simplenormalize=True,
                            imagenetmeansubtract=False)
                temp_outfile = outfile_top[:-4] + C["augmentation_type"][n - 1] + ".pkl"
                features = extract(C, model, batch)
                cs760.saveas_pickle(features, os.path.join(feature_directory, temp_outfile))
            else:
                batch = cs760.resize_batch(vid_np_top, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                                inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                                simplenormalize=True,
                                imagenetmeansubtract=False)
                features = extract(C, model, batch)
                cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_top))
                print(f'Features output shape: {features.shape}')
                
        if C["crop_type"] == 'B':  # only for boston vids
            vid_np_bot = cs760.crop_image(vid_np, C["crop_bottom"])
            outfile_bot = outfile + "__BOT.pkl"  
            batch = cs760.resize_batch(vid_np_bot, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L',
                        inter=cv2.INTER_CUBIC, BGRtoRGB=False, 
                        simplenormalize=True,
                        imagenetmeansubtract=False)
            features = extract(C, model, batch)
            cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_bot))

    print('Finished outputting features!!')
def generate_blending():
    image, segmap = load_cityscapes_data()

    # color car lights
    ia.seed(10)  # 2 = blue lights, 8 = pink, 10 = green lights
    image_aug, _segmap_aug = iaa.BlendAlphaSegMapClassIds(
        1,
        foreground=iaa.BlendAlphaSomeColors(
            iaa.AddToHueAndSaturation(value_hue=(-200, 200),
                                      value_saturation=(-100, 100))))(
                                          image=image,
                                          segmentation_maps=segmap)

    _save("cityscapes5-car-lights-changed.jpg", image_aug, size=0.3)

    # color train
    ia.seed(37)
    image_aug, _segmap_aug = iaa.BlendAlphaSegMapClassIds(
        2,
        foreground=iaa.AddToHueAndSaturation(value_hue=(-200, 200),
                                             value_saturation=(-100, 100)))(
                                                 image=image,
                                                 segmentation_maps=segmap)

    _save("cityscapes5-train-color.jpg", image_aug, size=0.3)

    # emboss street
    image_aug, _segmap_aug = iaa.BlendAlphaSegMapClassIds(
        4, foreground=iaa.Emboss(1.0, strength=1.0))(image=image,
                                                     segmentation_maps=segmap)

    _save("cityscapes5-street-embossed.jpg", image_aug, size=0.3)

    # replace street with gaussian noise
    ia.seed(3)
    image_aug, _segmap_aug = iaa.BlendAlphaSegMapClassIds(
        4,
        foreground=iaa.Sequential([
            iaa.Multiply(0.0),
            iaa.AdditiveGaussianNoise(loc=128, scale=40, per_channel=True)
        ]),
    )(image=image, segmentation_maps=segmap)

    _save("cityscapes5-street-gaussian-noise.jpg", image_aug, size=0.3)

    # regular grid dropout
    ia.seed(1)
    image_aug = iaa.BlendAlphaRegularGrid(
        nb_rows=(8, 12), nb_cols=(8, 12),
        foreground=iaa.Multiply(0.0))(image=image)

    _save("cityscapes5-regular-grid-dropout.jpg", image_aug, size=0.3)

    # checkerboard dropout
    ia.seed(1)
    image_aug = iaa.BlendAlphaCheckerboard(
        nb_rows=(8, 12), nb_cols=(8, 12),
        foreground=iaa.Multiply(0.0))(image=image)

    _save("cityscapes5-checkerboard-dropout.jpg", image_aug, size=0.3)

    # somecolors + removesaturation
    ia.seed(1)
    image_gogh = imageio.imread(
        os.path.join(INPUT_IMAGES_DIR,
                     "1280px-Vincent_Van_Gogh_-_Wheatfield_with_Crows.jpg"))
    image_gogh = iaa.Resize({
        "width": 256,
        "height": "keep-aspect-ratio"
    })(image=image_gogh)
    images_aug = ([image_gogh] + iaa.BlendAlphaSomeColors(
        iaa.RemoveSaturation(1.0))(images=[image_gogh] * (2 * 4 - 1)))
    _save("blendalphasomecolors_removesaturation.jpg",
          ia.draw_grid(images_aug, cols=4, rows=2))
Пример #10
0
        transformed_image = transform(image=image)
    
    elif augmentation == 'multiply_saturation':
        transform = iaa.MultiplySaturation((0.5, 1.5))
        transformed_image = transform(image=image)
    
    elif augmentation == 'addto_saturation':
        transform = iaa.AddToSaturation((-100, 100))
        transformed_image = transform(image=image)
    
    elif augmentation == 'saturate':
        transform = iaa.imgcorruptlike.Saturate(severity=5)
        transformed_image = transform(image=image)

    elif augmentation == 'remove_saturation':
        transform = iaa.RemoveSaturation()
        transformed_image = transform(image=image)
    
    elif augmentation == 'multiply_hue_and_saturation':
        transform = iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True)
        transformed_image = transform(image=image)

    elif augmentation == 'brightness_contrast':
        transform = RandomBrightnessContrast(always_apply=True, 
                                             brightness_limit=0.5)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'brightness':
        transform = iaa.imgcorruptlike.Brightness(severity=2)
        transformed_image = transform(image=image)