Esempio n. 1
0
def get_seq(flag_normal, flag_affine, flag_noise, flag_snow, flag_cloud,
            flag_fog, flag_snowflakes, flag_rain, flag_dropout):
    if flag_normal:
        seq_list = [
            iaa.SomeOf((1, 2), [
                iaa.LinearContrast((0.5, 2.0), per_channel=0.5),
                iaa.Grayscale(alpha=(0.0, 1.0)),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
            ])
        ]
    else:
        seq_list = []

    if flag_affine:
        seq_list.append(
            iaa.Sometimes(
                0.7,
                iaa.Affine(scale={
                    "x": (0.8, 1.2),
                    "y": (0.8, 1.2)
                },
                           translate_percent={
                               "x": (-0.2, 0.2),
                               "y": (-0.2, 0.2)
                           },
                           rotate=(-25, 25),
                           shear=(-8, 8))))

    if flag_noise:
        seq_list.append(
            iaa.OneOf([
                iaa.GaussianBlur((0, 3.0)),
                iaa.AverageBlur(k=(2, 7)),
                iaa.MedianBlur(k=(3, 11)),
            ]))

    if flag_snow:
        seq_list.append(
            iaa.FastSnowyLandscape(lightness_threshold=(100, 255),
                                   lightness_multiplier=(1.0, 4.0)))
    elif flag_cloud:
        seq_list.append(iaa.Clouds())
    elif flag_fog:
        seq_list.append(iaa.Fog())
    elif flag_snowflakes:
        seq_list.append(
            iaa.Snowflakes(flake_size=(0.2, 0.7), speed=(0.007, 0.03)))
    elif flag_rain:
        seq_list.append(iaa.Rain())

    if flag_dropout:
        seq_list.append(
            iaa.OneOf([
                iaa.Dropout((0.01, 0.1), per_channel=0.5),
                iaa.CoarseDropout((0.03, 0.15),
                                  size_percent=(0.02, 0.05),
                                  per_channel=0.2),
            ]))

    return iaa.Sequential(seq_list, random_order=True)
Esempio n. 2
0
def chapter_augmenters_fog():
    fn_start = "weather/fog"
    image = LANDSCAPE_IMAGE

    aug = iaa.Fog()
    run_and_save_augseq(
        fn_start + ".jpg", aug,
        [image for _ in range(4*2)], cols=4, rows=2)
Esempio n. 3
0
def adding_fog(image1, image2, name, metadata):
    fog = iaa.Fog()
    image_fog1 = fog.augment_image(image1)
    image_fog2 = fog.augment_image(image2)
    name_1 = name + '_A_f.jpg'
    name_2 = name + '_B_f.jpg'
    cv.imwrite(name_1, image_fog1)
    cv.imwrite(name_2, image_fog2)
    metadata = write_to_metadata(metadata, "fog")

    return metadata
Esempio n. 4
0
def preprocess(image, mode='plain', d_theta=0):
    """
    Combine all preprocess functions into one
    """
    if mode == 'random_rotate_and_shift':
        angle = 30
        dx = 32
        dy = 16
        d_theta = round(np.random.random() * angle * 2 - angle)
        d_x = np.random.random() * dx * 2 - dx
        d_y = np.random.random() * dy * 2 - dy
        y, x = image.shape[:2]

        translation_matrix = np.float32([[1, 0, d_x], [0, 1, d_y]])
        from PIL import Image

        image = Image.fromarray(image)
        image = image.rotate(d_theta)
        image = np.array(image)
        image = cv2.warpAffine(image, translation_matrix, (x, y))
    elif mode == 'exact_rotate':
        from PIL import Image
        image = Image.fromarray(image)
        image = image.rotate(d_theta)
        image = np.array(image)
    elif mode == 'rainy_foggy_automold':
        import Automold as am
        import Helpers as hp
        if np.random.random() > 0.5:
            image = am.add_rain(image, rain_type='heavy')
        else:
            image = am.add_fog(image)
    elif mode == 'rainy_foggy_iaa':
        from imgaug import augmenters as iaa
        seq = iaa.Rain()
        if np.random.random() > 0.5:
            seq = iaa.Rain()
        else:
            seq = iaa.Fog()
        image = seq(images=image)

    # for i in range(len(image)):
    #     image[i] = crop(image[i])
    #     image[i] = resize(image[i])
    #     image[i] = rgb2yuv(image[i])
    #     image[i] = image[i][np.newaxis, :, :, :]
    # image = np.concatenate(image, axis=0)

    image = crop(image)
    image = resize(image)
    image = rgb2yuv(image)

    return image
Esempio n. 5
0
    def test_zero_sized_axes(self):
        shapes = [(0, 0), (0, 1), (1, 0), (0, 1, 0), (1, 0, 0), (0, 1, 1),
                  (1, 0, 1)]

        for shape in shapes:
            with self.subTest(shape=shape):
                image = np.zeros(shape, dtype=np.uint8)
                aug = iaa.Fog()

                image_aug = aug(image=image)

                assert image_aug.dtype.name == "uint8"
                assert image_aug.shape == shape
Esempio n. 6
0
    def test_unusual_channel_numbers(self):
        shapes = [(1, 1, 4), (1, 1, 5), (1, 1, 512), (1, 1, 513)]

        for shape in shapes:
            with self.subTest(shape=shape):
                image = np.zeros(shape, dtype=np.uint8)
                aug = iaa.Fog()

                image_aug = aug(image=image)

                assert np.any(image_aug > 0)
                assert image_aug.dtype.name == "uint8"
                assert image_aug.shape == shape
Esempio n. 7
0
def main():
    for size in [0.1, 0.2, 1.0]:
        image = imageio.imread(
            "https://upload.wikimedia.org/wikipedia/commons/8/89/Kukle%2CCzech_Republic..jpg",
            format="jpg")
        image = ia.imresize_single_image(image, size, "cubic")
        print(image.shape)
        augs = [("iaa.Fog()", iaa.Fog())]

        for descr, aug in augs:
            print(descr)
            images_aug = aug.augment_images([image] * 64)
            ia.imshow(ia.draw_grid(images_aug))
Esempio n. 8
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)
Esempio n. 9
0
def do_all_aug(image):
    do_aug(image, iaa.Noop(name="origin"))
    do_aug(image, iaa.Crop((0, 10)))  # 切边
    do_aug(image, iaa.GaussianBlur((0, 3)))
    do_aug(image, iaa.AverageBlur(1, 7))
    do_aug(image, iaa.MedianBlur(1, 7))
    do_aug(image, iaa.Sharpen())
    do_aug(image, iaa.BilateralBlur())  # 既噪音又模糊,叫双边
    do_aug(image, iaa.MotionBlur())
    do_aug(image, iaa.MeanShiftBlur())
    do_aug(image, iaa.GammaContrast())
    do_aug(image, iaa.SigmoidContrast())
    do_aug(image,
           iaa.Affine(shear={
               'x': (-10, 10),
               'y': (-10, 10)
           }, mode="edge"))  # shear:x轴往左右偏离的像素书,(a,b)是a,b间随机值,[a,b]是二选一
    do_aug(image,
           iaa.Affine(shear={
               'x': (-10, 10),
               'y': (-10, 10)
           }, mode="edge"))  # shear:x轴往左右偏离的像素书,(a,b)是a,b间随机值,[a,b]是二选一
    do_aug(image, iaa.Rotate(rotate=(-10, 10), mode="edge"))
    do_aug(image, iaa.PiecewiseAffine())  # 局部点变形
    do_aug(image, iaa.Fog())
    do_aug(image, iaa.Clouds())
    do_aug(image, iaa.Snowflakes(flake_size=(0.1, 0.2),
                                 density=(0.005, 0.025)))
    do_aug(
        image,
        iaa.Rain(
            nb_iterations=1,
            drop_size=(0.05, 0.1),
            speed=(0.04, 0.08),
        ))
    do_aug(
        image,
        iaa.ElasticTransformation(alpha=(0.0, 20.0),
                                  sigma=(3.0, 5.0),
                                  mode="nearest"))
    do_aug(image, iaa.AdditiveGaussianNoise(scale=(0, 10)))
    do_aug(image, iaa.AdditiveLaplaceNoise(scale=(0, 10)))
    do_aug(image, iaa.AdditivePoissonNoise(lam=(0, 10)))
    do_aug(image, iaa.Salt((0, 0.02)))
    do_aug(image, iaa.Pepper((0, 0.02)))
    def __init__(self, choice_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]):

        self.choice_mux = {
            0: self.do_nothing,
            1: self.gauss_blur,
            2: self.motion_blur,
            3: self.modify_gamma,
            4: self.modify_exposure,
            5: self.gauss_noise,
            6: self.circular_lines,
            7: self.scale_image,
            8: self.rotate_image,
            9: self.translate_image,
            10: self.add_fog
        }
        self.choice_list = choice_list

        self.fog_augger = iaa.Fog()
Esempio n. 11
0
    def random_noise(self, shape, color=(55, 55, 55)):
        import random
        import cv2
        # get image shape
        height, width, channels = shape

        # create blank white image
        overlay = 255 * np.ones(shape=[height, width, channels], dtype=np.uint8)

        # randomize crappy shape and position
        ## 0 - rectangle
        ## 1 - line
        ## 2 - circle
        for i in range(3):
            # get randomic shape position
            x1, y1 = [random.choice(range(0, width)), random.choice(range(0, height))]
            x2, y2 = [random.choice(range(0, width)), random.choice(range(0, height))]

            # select random shape
            shape_range = range(0, 2)
            randomic_shape = random.choice(shape_range)
            if randomic_shape == 0:
            cv2.rectangle(overlay, (x1, y1), (x2, y2), color, -1)
            elif randomic_shape == 1:
            cv2.line(overlay, (x1, y1), (x2, y2), color, 80)
            else:
            cv2.circle(overlay, (x1, y1), (x2, y2), color, -1)
        overlay = cv2.blur(overlay, (100, 100))
        return overlay


    def sequential_cv2_noise(self, image):
            from imgaug import augmenters as iaa

        seq = iaa.Sequential([
            iaa.PerspectiveTransform(random_state=1, scale=0.05),
            iaa.Fog(),
            iaa.Affine(rotate=0.01),
            iaa.GammaContrast(3)
        ])
        return seq.augment_image
Esempio n. 12
0
def get_augmenter():
    sometimes = lambda aug: iaa.Sometimes(0.7, aug)
    augmenter = iaa.Sequential([
        sometimes(
            iaa.OneOf([
                iaa.GaussianBlur((0.8, 1.2)),
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
                iaa.AveragePooling([2, 2]),
                iaa.Sharpen(alpha=(0.0, 0.5), lightness=(0.75, 2.0)),
                iaa.AdditiveLaplaceNoise(scale=0.05 * 255, per_channel=True),
                iaa.LinearContrast((0.5, 2.0), per_channel=True),
                iaa.Clouds(),
                iaa.Fog(),
                iaa.PiecewiseAffine(scale=0.02),
                iaa.Affine(scale={
                    "x": (0.8, 1),
                    "y": (0.8, 1)
                },
                           translate_percent={
                               "x": (-0.1, 0.1),
                               "y": (-0.1, 0.1)
                           },
                           rotate=(-10, 10),
                           shear=(-5, 5),
                           order=[0, 1],
                           cval=(0, 255),
                           mode='constant'),
            ])),
        sometimes(
            iaa.OneOf([
                iaa.Crop(px=(2, 6)),
                iaa.CoarseDropout((0.0, 0.01), size_percent=(0.02, 0.1)),
            ])),
        sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.08),
                                           keep_size=False))
    ],
                               random_order=True)

    return augmenter
Esempio n. 13
0
 def test_pickleable(self):
     aug = iaa.Fog(random_state=1)
     runtest_pickleable_uint8_img(aug, iterations=3, shape=(20, 20, 3))
Esempio n. 14
0
def fogaug(img):
    images = np.expand_dims(img, axis=0)
    aug = iaa.Fog()
    images_aug = aug(images=images)
    img_aug = np.squeeze(images_aug)
    return img_aug
Esempio n. 15
0
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
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
Esempio n. 17
0
 def aug3(self, img):
     seq = iaa.Sequential([iaa.Fog(0)])
     img_au = seq(image=img)
     return img_au
Esempio n. 18
0
def imageAugmentation(dir_in, dir_out):
    random.seed(42)
    try:
        if not os.path.isdir(dir_out):
            os.mkdir(dir_out)
    except:
        pass

    list_jpg = glob(dir_in + r"\**\*.jpg", recursive=True)

    ## https://github.com/albumentations-team/albumentations
    list_aug = [
        A.CLAHE(),
        A.OpticalDistortion(),
        # A.GridDistortion(),
        # A.HueSaturationValue(),
        A.GaussNoise(),
        A.MotionBlur(p=.2),
        A.RandomBrightnessContrast(p=0.1),
        # A.InvertImg(),        # Not Accepted
        # A.ISONoise(),        # Not Accepted
        # A.RandomFog(),                  # Not Accepted
        # # A.RandomRain(),      # Not Accepted
        # # A.RandomSnow()      # Not Accepted
    ]
    list_aug_name = [
        'CLAHE',
        'OpticalDist',
        # 'GridDist',
        # 'HueSat',
        'GaussNoise',
        'MotionBlur',
        'RandomBright',
        # 'InvertImg',
        # 'IsoNoise',
        # 'RandomFog',
        # # 'RandomRain',
        # # 'RandomSnow',
    ]

    ## https://github.com/aleju/imgaug

    list_aa = [
        # iaa.MedianBlur(k=(3, 11)),      # Not Accepted
        # iaa.Dropout((0.05, 0.06), per_channel=0.5),      # Not Accepted
        # iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),      # Not Accepted
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.05 * 255),
                                  per_channel=0.5),
        iaa.ElasticTransformation(alpha=8.2, sigma=4.0),
        # iaa.ElasticTransformation(alpha=15.5, sigma=4.0),
        # iaa.ElasticTransformation(alpha=22.8, sigma=4.0),
        iaa.PiecewiseAffine(scale=0.015),
        iaa.PiecewiseAffine(scale=0.030),
        iaa.PiecewiseAffine(scale=0.045),
        # iaa.PiecewiseAffine(scale=0.060),
        # iaa.PiecewiseAffine(scale=0.075),
        iaa.EdgeDetect(alpha=0.3),
        # iaa.Sharpen(alpha=(0.0, 1.0)),      # Not Accepted
        # iaa.DirectedEdgeDetect(alpha=0.5, direction=0),      # Not Accepted
        iaa.Affine(scale=0.8, mode='edge', cval=64),
        iaa.Affine(scale=1.2, mode='edge'),
        iaa.Affine(rotate=5, cval=64),
        iaa.Affine(rotate=10, cval=64),
        iaa.Affine(rotate=-5, cval=64),
        iaa.Affine(rotate=-10, cval=64),
        iaa.Affine(shear=8, cval=64, mode='edge'),
        iaa.Affine(shear=-8, cval=64, mode='edge'),
        iaa.Affine(scale=0.8, rotate=3, mode='edge', cval=64),
        iaa.Affine(scale=0.8, rotate=-3, mode='edge', cval=64),
        iaa.Affine(scale=1.2, rotate=3, mode='edge', cval=64),
        iaa.Affine(scale=1.2, rotate=-3, mode='edge', cval=64),
        iaa.GaussianBlur(sigma=1.0),
        # iaa.MaxPooling(kernel_size=2, keep_size=True),
        iaa.Fog(),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(scale=0.8, mode='edge', cval=64),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(scale=1.2, mode='edge'),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(rotate=5, cval=64),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(rotate=10, cval=64),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(rotate=-5, cval=64),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(rotate=-10, cval=64),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(shear=8, cval=64, mode='edge'),
        ]),
        iaa.Sequential([
            iaa.GaussianBlur(sigma=1.0),
            iaa.Affine(shear=-8, cval=64, mode='edge'),
        ]),

        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(scale=0.8, mode='edge', cval=64), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(scale=1.2, mode='edge'), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(rotate=5, cval=64), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(rotate=10, cval=64), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(rotate=-5, cval=64), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(rotate=-10, cval=64), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(shear=8, cval=64, mode='edge'), ]),
        # iaa.Sequential([iaa.MaxPooling(2, keep_size=True), iaa.Affine(shear=-8, cval=64, mode='edge'), ]),

        # iaa.Sequential([iaa.Fog(), iaa.Affine(scale=0.8, mode='edge', cval=64), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(scale=1.2, mode='edge'), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(rotate=5, cval=64), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(rotate=10, cval=64), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(rotate=-5, cval=64), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(rotate=-10, cval=64), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(shear=8, cval=64, mode='edge'), ]),
        # iaa.Sequential([iaa.Fog(), iaa.Affine(shear=-8, cval=64, mode='edge'), ]),
    ]
    list_aa_name = [
        # 'MedianBlur',
        # 'Dropout',
        # 'Emboss',
        'AdditiveGaussianNoise',
        'ElasticTransformation8',
        # 'ElasticTransformation15',
        # 'ElasticTransformation22',
        'PiecewiseAffine15',
        'PiecewiseAffine30',
        'PiecewiseAffine45',
        # 'PiecewiseAffine60',
        # 'PiecewiseAffine75',
        'EdgeDetect',
        # 'Sharpen',
        # 'DirectedEdgeDetect',
        'scale8',
        'scale12',
        'rotate5',
        'rotate10',
        'rotate_5',
        'rotate_10',
        'shear8',
        'shear_8',
        'scale8rotate3',
        'scale8rotate_3',
        'scale12rotate3',
        'scale12rotate_3',
        'GaussianBlur',
        # 'MaxPooling2',
        'Fog',
        'GaussianBlurscale8',
        'GaussianBlurscale12',
        'GaussianBlurrotate5',
        'GaussianBlurrotate10',
        'GaussianBlurrotate_5',
        'GaussianBlurrotate_10',
        'GaussianBlurshear8',
        'GaussianBlurshear_8',

        # 'MaxPooling2scale8',
        # 'MaxPooling2scale12',
        # 'MaxPooling2rotate5',
        # 'MaxPooling2rotate10',
        # 'MaxPooling2rotate_5',
        # 'MaxPooling2rotate_10',
        # 'MaxPooling2shear8',
        # 'MaxPooling2shear_8',

        # 'Fogscale8',
        # 'Fogscale12',
        # 'Fogrotate5',
        # 'Fogrotate10',
        # 'Fogrotate_5',
        # 'Fogrotate_10',
        # 'Fogshear8',
        # 'Fogshear_8',
    ]

    list_torch_tf = [
        MyRotationTransform(angles=[-10, -5, 0, 5, 10]),
    ]

    list_torch_tf_name = [
        'Rotate',
    ]

    for jpg in list_jpg:
        jpg_out = jpg.replace(dir_in, dir_out)
        dir_out_jpg = os.path.dirname(jpg_out)
        try:
            if not os.path.exists(dir_out_jpg):
                os.mkdir(dir_out_jpg)
        except:
            pass
        jpg_out_basename = os.path.splitext(jpg_out)[0]
        image = imageread(jpg)
        imagesave(image, jpg_out_basename + '.jpg')  # save the orignal image.
        for i in range(len(list_aug)):
            augmented_image = list_aug[i](image=image)['image']
            imagesave(augmented_image,
                      jpg_out_basename + '_' + list_aug_name[i] + '.jpg')

        for i in range(len(list_aa)):
            augmented_image = list_aa[i](image=image)
            imagesave(augmented_image,
                      jpg_out_basename + '_' + list_aa_name[i] + '.jpg')
Esempio n. 19
0
def transform(aug_type, magnitude, X):
    if aug_type == "crop":
        X_aug = iaa.Crop(px=(0, int(magnitude * 32))).augment_images(X)
    elif aug_type == "gaussian-blur":
        X_aug = iaa.GaussianBlur(sigma=(0, magnitude * 25.0)).augment_images(X)
    elif aug_type == "rotate":
        X_aug = iaa.Affine(rotate=(-180 * magnitude, 180 * magnitude)).augment_images(X)
    elif aug_type == "shear":
        X_aug = iaa.Affine(shear=(-90 * magnitude, 90 * magnitude)).augment_images(X)
    elif aug_type == "translate-x":
        X_aug = iaa.Affine(
            translate_percent={"x": (-magnitude, magnitude), "y": (0, 0)}
        ).augment_images(X)
    elif aug_type == "translate-y":
        X_aug = iaa.Affine(
            translate_percent={"x": (0, 0), "y": (-magnitude, magnitude)}
        ).augment_images(X)
    elif aug_type == "horizontal-flip":
        X_aug = iaa.Fliplr(magnitude).augment_images(X)
    elif aug_type == "vertical-flip":
        X_aug = iaa.Flipud(magnitude).augment_images(X)
    elif aug_type == "sharpen":
        X_aug = iaa.Sharpen(
            alpha=(0, 1.0), lightness=(0.50, 5 * magnitude)
        ).augment_images(X)
    elif aug_type == "emboss":
        X_aug = iaa.Emboss(
            alpha=(0, 1.0), strength=(0.0, 20.0 * magnitude)
        ).augment_images(X)
    elif aug_type == "additive-gaussian-noise":
        X_aug = iaa.AdditiveGaussianNoise(
            loc=0, scale=(0.0, magnitude * 255), per_channel=0.5
        ).augment_images(X)
    elif aug_type == "dropout":
        X_aug = iaa.Dropout(
            (0.01, max(0.011, magnitude)), per_channel=0.5
        ).augment_images(
            X
        )  # Dropout first argument should be smaller than second one
    elif aug_type == "coarse-dropout":
        X_aug = iaa.CoarseDropout(
            (0.03, 0.15), size_percent=(0.30, np.log10(magnitude * 3)), per_channel=0.2
        ).augment_images(X)
    elif aug_type == "gamma-contrast":
        X_norm = normalize(X)
        X_aug_norm = iaa.GammaContrast(magnitude * 1.75).augment_images(
            X_norm
        )  # needs 0-1 values
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "brighten":
        X_aug = iaa.Add(
            (int(-40 * magnitude), int(40 * magnitude)), per_channel=0.5
        ).augment_images(
            X
        )  # brighten
    elif aug_type == "invert":
        X_aug = iaa.Invert(1.0).augment_images(X)  # magnitude not used
    elif aug_type == "fog":
        X_aug = iaa.Fog().augment_images(X)  # magnitude not used
    elif aug_type == "clouds":
        X_aug = iaa.Clouds().augment_images(X)  # magnitude not used
    elif aug_type == "histogram-equalize":
        X_aug = iaa.AllChannelsHistogramEqualization().augment_images(
            X
        )  # magnitude not used
    elif aug_type == "super-pixels":  # deprecated
        X_norm = normalize(X)
        X_norm2 = (X_norm * 2) - 1
        X_aug_norm2 = iaa.Superpixels(
            p_replace=(0, magnitude), n_segments=(100, 100)
        ).augment_images(X_norm2)
        X_aug_norm = (X_aug_norm2 + 1) / 2
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "perspective-transform":
        X_norm = normalize(X)
        X_aug_norm = iaa.PerspectiveTransform(
            scale=(0.01, max(0.02, magnitude))
        ).augment_images(
            X_norm
        )  # first scale param must be larger
        np.clip(X_aug_norm, 0.0, 1.0, out=X_aug_norm)
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "elastic-transform":  # deprecated
        X_norm = normalize(X)
        X_norm2 = (X_norm * 2) - 1
        X_aug_norm2 = iaa.ElasticTransformation(
            alpha=(0.0, max(0.5, magnitude * 300)), sigma=5.0
        ).augment_images(X_norm2)
        X_aug_norm = (X_aug_norm2 + 1) / 2
        X_aug = denormalize(X_aug_norm)
    elif aug_type == "add-to-hue-and-saturation":
        X_aug = iaa.AddToHueAndSaturation(
            (int(-45 * magnitude), int(45 * magnitude))
        ).augment_images(X)
    elif aug_type == "coarse-salt-pepper":
        X_aug = iaa.CoarseSaltAndPepper(p=0.2, size_percent=magnitude).augment_images(X)
    elif aug_type == "grayscale":
        X_aug = iaa.Grayscale(alpha=(0.0, magnitude)).augment_images(X)
    else:
        raise ValueError
    return X_aug
Esempio n. 20
0
 def __init__(self, param=0.5):
     self.param = 0.5
     self.seq = iaa.Sequential([iaa.Snowflakes(0.4), iaa.Fog()])
Esempio n. 21
0
def get_augmentation(params):
    """
    copy from https://github.com/barisozmen/deepaugment
    :param params:
    :return:
    """
    padding_value = params['PaddingValue']
    output_size = params['OutputSize']

    scale_params = params['Scale']
    if 'disable' in scale_params:
        random_scale_fn = partial(random_scale, output_size=output_size)
    else:
        min_scale = scale_params['min_scale']
        max_scale = scale_params['max_scale']

        random_scale_fn = partial(random_scale,
                                  output_size=output_size,
                                  min_scale=min_scale,
                                  max_scale=max_scale)

    rot_params = params['Rotation']
    if 'disable' in rot_params:
        max_rot_angle = None
    else:
        max_rot_angle = rot_params['max_angle']

    crop_params = params['Crop']
    if 'disable' in crop_params:
        crop_x_ratio = None
        crop_y_ratio = None
    else:
        crop_x_ratio, crop_y_ratio = crop_params['crop_x'], crop_params[
            'crop_y']

    random_rotate_crop_fn = partial(random_rotate_crop,
                                    output_size=output_size,
                                    max_angle=max_rot_angle,
                                    crop_x_ratio=crop_x_ratio,
                                    crop_y_ratio=crop_y_ratio,
                                    padding_value=padding_value)

    augmentation = []
    for aug_type, aug_parameters in params.items():

        if aug_type in [
                'Scale', 'Rotation', 'Crop', 'OutputSize', 'PaddingValue'
        ] or 'disable' in aug_parameters:
            continue
        elif aug_type == "Shear":
            angle = aug_parameters['angle']
            augmentation.append(
                iaa.Affine(shear=(-90 * angle, 90 * angle),
                           cval=padding_value[0]))
        elif aug_type == "HorizontalFlip":
            augmentation.append(iaa.Fliplr(aug_parameters['probability']))
        elif aug_type == "VerticalFlip":
            augmentation.append(iaa.Flipud(aug_parameters['probability']))
        elif aug_type == "gaussian-blur":
            augmentation.append(iaa.GaussianBlur(sigma=(0, magnitude * 25.0)))
        elif aug_type == "Brighten":
            magnitude = aug_parameters['magnitude']
            augmentation.append(iaa.Multiply(
                (1 - magnitude, 1.0 + magnitude)))  # brighten
        elif aug_type == "GammaContrast":
            magnitude = aug_parameters['magnitude']
            augmentation.append(
                iaa.GammaContrast((1 - magnitude, 1.0 + magnitude)))
        elif aug_type == "Sharpen":
            max_alpha = aug_parameters['max_alpha']
            max_lightness = aug_parameters['max_lightness']
            augmentation.append(
                iaa.Sharpen(alpha=(0, max_alpha),
                            lightness=(0.50, max_lightness)))
        elif aug_type == "Emboss":
            max_alpha = aug_parameters['max_alpha']
            max_strength = aug_parameters['max_strength']
            augmentation.append(
                iaa.Emboss(alpha=(0, max_alpha), strength=(0.0, max_strength)))
        elif aug_type == "MotionBlur":
            min_kernel_size = aug_parameters['min_kernel_size']
            interval = aug_parameters['interval']
            max_angle = aug_parameters['max_angle']
            augmentation.append(
                iaa.Emboss(k=(int(min_kernel_size),
                              int(min_kernel_size + interval)),
                           angle=(0.0, max_angle)))

        elif aug_type == "fog" and aug_parameters:
            augmentation.append(iaa.Fog())  # magnitude not used
        elif aug_type == "clouds" and aug_parameters:
            augmentation.append(iaa.Clouds())  # magnitude not used

        elif aug_type == "ElasticTransformation":
            max_alpha = aug_parameters['max_alpha']
            sigma = aug_parameters['sigma']
            augmentation.append(
                iaa.ElasticTransformation(alpha=(0.0, max_alpha), sigma=sigma))
        elif aug_type == "SaltAndPepper":
            probability = aug_parameters['probability']
            per_channel_prob = aug_parameters['per_channel']
            augmentation.append(
                iaa.SaltAndPepper(probability, per_channel=per_channel_prob))
        elif aug_type == "CoarseSaltAndPepper":
            probability = aug_parameters['probability']
            size_percent = aug_parameters['size_percent ']
            augmentation.append(
                iaa.CoarseSaltAndPepper(probability,
                                        size_percent=size_percent))

        elif aug_type == "Dropout":
            max_probability = aug_parameters['max_probability']
            per_channel_prob = aug_parameters['per_channel_prob ']
            augmentation.append(
                iaa.Dropout((0.01, max_probability),
                            per_channel=per_channel_prob))
        elif aug_type == "CoarseDropout":
            max_probability = aug_parameters['max_probability']
            size_percent = aug_parameters['size_percent']
            augmentation.append(
                iaa.CoarseDropout((0.03, max_probability),
                                  size_percent=size_percent))

        elif aug_type == "GrayScale":
            max_alpha = aug_parameters['max_alpha']
            augmentation.append(iaa.Grayscale(alpha=(0., max_alpha)))

        else:
            raise ValueError

    if len(augmentation) > 0:
        iaa_aug = iaa.Sequential(augmentation)
    else:
        iaa_aug = None

    def preprocessing(image, mask):
        image, mask = random_scale_fn(image, mask)
        if iaa_aug is not None:
            image, segmap = iaa_aug(image=image,
                                    segmentation_maps=SegmentationMapOnImage(
                                        mask, shape=mask.shape, nb_classes=19))
            mask = segmap.get_arr_int()
        image, mask = random_rotate_crop_fn(image, mask)
        return image, mask

    return preprocessing
def fog_augmentation1(img):
    seq = iaa.Sequential(iaa.Fog(random_state=9))
    img = seq.augment_image(img)
    seq = iaa.Sequential(iaa.MotionBlur())
    img = seq.augment_image(img)
    return img
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.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_blend = [
        iaa.Alpha((0.01, 0.99), iaa.Noop(), name="Alpha"),
        iaa.AlphaElementwise((0.01, 0.99), iaa.Noop(),
                             name="AlphaElementwise"),
        iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha((-2.0, 2.0),
                                iaa.Noop(),
                                name="FrequencyNoiseAlpha")
    ]
    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.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.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")
    ]
    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"),
        # 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_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_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")
    ]

    augmenters = (augmenters_meta + augmenters_arithmetic + augmenters_blend +
                  augmenters_blur + augmenters_color + augmenters_contrast +
                  augmenters_convolutional + augmenters_edges +
                  augmenters_flip + augmenters_geometric + augmenters_pooling +
                  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
#setting up augmentations
Augmentations = []

augtype = 'rotateandscale'
#rotate & scale
seq = iaa.Sequential([
    iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect keypoints
    iaa.Affine(rotate=23, scale=(
        0.9, 1.1
    ))  # rotate by exactly 23 deg and scale to 90-10%, affects keypoints
])
Augmentations.append([augtype, seq])

augtype = 'fog'
seq = iaa.Sequential([iaa.Fog()])
Augmentations.append([augtype, seq])

augtype = 'snow'
seq = iaa.Sequential([
    iaa.Snowflakes(flake_size=(.2, .5),
                   density=(0.005, 0.07),
                   speed=(0.01, 0.05))
])
Augmentations.append([augtype, seq])

for ind, imname in enumerate(Dataframe.index):
    image = imresize(imread(os.path.join('montblanc_images', imname)),
                     size=scale)
    ny, nx, nc = np.shape(image)
def eval_models_impairments(pooling = 'global',
    backbonetype ='mobilenetv2',
    output_stride = 8,
    residual_shortcut = False, 
    height_image = 448, 
    width_image = 448, 
    channels = 3, 
    crop_enable = False,
    height_crop = 448, 
    width_crop = 448, 
    
    debug_en = False, 
    dataset_name = 'freiburg_forest', 
    class_imbalance_correction = False, 
    data_augmentation = True, 
     
    multigpu_enable = True,
    
    batch_size = 32, 
    epochs = 300, 
    initial_epoch = -1, 
    continue_traning = False, 
    fine_tune_last = False, 
    
    base_learning_rate = 0.007, 
    learning_power = 0.98, 
    decay_steps = 1,
    learning_rate_decay_step = 300, 
    
    decay = 5**(-4), 
    impairment = "noise", 
    intensity = 20
    ):
    
    
    fold_name = (backbonetype+'s'+str(output_stride)+'_pooling_' + pooling
             +('_residual_shortcut' if residual_shortcut else '')+'_ep'+str(epochs)
             +('_crop_'+str(height_crop)+'x'+str(width_crop) if crop_enable else '_')
             +('from' if crop_enable else '')+str(height_image)+'x'+str(width_image)
             +'_'+('wda_' if data_augmentation else 'nda_')
             +('wcic_' if class_imbalance_correction else 'ncic_')
             +('wft_' if fine_tune_last else 'nft_')+dataset_name+'_b'
             +str(batch_size)+('_n' if multigpu_enable else '_1')+'gpu')
    
    
    
    #Override params for eval
    # multigpu_enable = False
    # batch_size      = 2


    ## Check the number of labels
    if dataset_name == 'cityscape':
        classes = cityscape.classes
    elif dataset_name == 'citysmall':
        classes = citysmall.classes
    elif dataset_name == 'off_road_small':
        classes = off_road_small.classes
    elif dataset_name == 'freiburg_forest':
        classes = freiburg_forest.classes
    
    n_classes = len(classes)
    

    if crop_enable:
        assert(height_crop == width_crop, "When crop is enable height_crop should be equals to width_crop")
        assert(height_crop <= height_image, "When crop is enable height_crop should be less than or equals to height_image")
        assert(width_crop <= width_image, "When crop is enable height_crop should be less than or equals to width_image")
    else:
        height_crop = height_image
        width_crop = width_image
    
    
    # Construct a tf.data.Dataset
    info = tfds.builder(dataset_name).info
    print(info)
    [ds_test] = tfds.load(name=dataset_name, split=["test"], as_supervised=True)
    # Add normalize
    def _normalize_img(image, label):
        image = tf.cast(image, tf.float32)/127.5 - 1
        if crop_enable:
            y1 = tf.random.uniform(shape=[], minval = 0., maxval = (height_image-height_crop)/height_image, dtype=tf.float32)
            x1 = tf.random.uniform(shape=[], minval = 0., maxval = (width_image-width_crop)/width_image, dtype=tf.float32)
    
            y2 = y1 + (height_crop/height_image)
            x2 = x1 + (width_crop/width_image)
    
            boxes = [[y1, x1, y2, x2]]
            image = tf.image.crop_and_resize([image], boxes, box_indices = [0], crop_size = (height_crop, width_crop), method=tf.image.ResizeMethod.BILINEAR)[0]
            label = tf.cast(tf.image.crop_and_resize([label], boxes, box_indices = [0], crop_size = (height_crop, width_crop), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)[0],dtype=tf.uint8)
        else:
            image = tf.image.resize(image, (height_image,width_image), method=tf.image.ResizeMethod.BILINEAR)
            label = tf.image.resize(label, (height_image,width_image), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
        return (image, label)
    
    
    def _noise(img, lbl):
        rms = tf.sqrt(tf.math.reduce_mean(tf.pow(img,2.0)))
        std = rms*intensity/100
        noise = tf.random.normal(shape=tf.shape(img), mean=0.0, stddev=std, dtype=tf.float32)
        img = img + noise
        return img, lbl
    
    seq = iaa.Sequential([iaa.Fog()])
    def _aug_img(img):
        img = seq.augment_images([img.numpy()])
        return img[0]
    
    def _fog(img, lbl):
        img = tf.py_function(_aug_img, [img], tf.uint8)
        img.set_shape((1208, 1920, 3))
        return img, lbl
    
    
    if impairment == "noise":
        ds_test = ds_test.map(_normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
        #SNR = mean^2/std^2
        #SNRdb = 10Log10(SNR) -> 10^(SNRdb/10) = SNR -> 10^(SNRdb/10) = mean^2/std^2
        #std^2 = mean^2/10^(SNRdb/10) -> std = sqrt(mean^2/(10^(SNRdb/10)))
        ds_test = ds_test.map(_noise, num_parallel_calls=tf.data.experimental.AUTOTUNE)
    elif impairment == "fog":
        ds_test = ds_test.map(lambda img, lbl: tf.cond(tf.random.uniform([], 0, 1) < intensity/100, 
                                                      lambda: _fog(img, lbl), 
                                                      lambda: (img, lbl)),
                                    num_parallel_calls=tf.data.experimental.AUTOTUNE)
        
        ds_test = ds_test.map(_normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
    if debug_en:
        aug.plot_images(ds_test, n_images=8, samples_per_image=3, classes = classes)
    
            
    ds_test = ds_test.shuffle(124).batch(batch_size).prefetch(10)
    test_steps=int(round(info.splits['test'].num_examples/batch_size))
    
    class MIoU(MeanIoU):
        def __init__(self, num_classes, name=None, dtype=None):
            super(MIoU, self).__init__(num_classes=num_classes, name=name, dtype=dtype)
    
        def update_state(self, y_true, y_pred, sample_weight=None):
            return super(MIoU, self).update_state(
                y_true=y_true, 
                y_pred=tf.math.argmax(input=y_pred, axis=-1, output_type=tf.dtypes.int64), 
                sample_weight=sample_weight)
    

    
    if multigpu_enable:
        strategy = tf.distribute.MirroredStrategy()
        with strategy.scope():
            cmsnet = CMSNet(dl_input_shape=(None, height_crop, width_crop, channels),
                            num_classes=n_classes, output_stride=output_stride,
                            pooling=pooling, residual_shortcut=residual_shortcut,
                            backbonetype=backbonetype)
            cmsnet.summary()
            #cmsnet.mySummary()
    
            optimizer = SGD(momentum=0.9, nesterov=True)
            miou = MIoU(num_classes=n_classes)
    
            cmsnet.compile(optimizer, loss='sparse_categorical_crossentropy', sample_weight_mode="temporal",
                          metrics=['accuracy', miou]
                          )
    else:
        cmsnet = CMSNet(dl_input_shape=(None, height_crop, width_crop, channels),
                        num_classes=n_classes, output_stride=output_stride, pooling=pooling,
                        residual_shortcut=residual_shortcut, backbonetype=backbonetype)
        cmsnet.summary()
        
        optimizer = SGD(momentum=0.9, nesterov=True)
    
        miou = MIoU(num_classes=n_classes)
        cmsnet.compile(optimizer, loss='sparse_categorical_crossentropy', sample_weight_mode="temporal",
                      metrics=['accuracy', miou])
    
    
    class_imbalance_correction
    

    
    #fold_name = 's16_wda_ncic_nft_off_road_small_b8_ngpu_ep300_483x769_pooling_aspp_20190815-151551'
    
    # Define the Keras TensorBoard callback.
    continue_traning = True
    if continue_traning:
        logdir=build_path+"logs/fit/" + fold_name #Continue
        weights_path = glob.glob(logdir+'*/weights.last*')[0]
        logdir = weights_path[:weights_path.find('/weights.')]
    
        print('Continuing train from '+ weights_path)
        if multigpu_enable:
            with strategy.scope():
                cmsnet.load_weights(weights_path)
        else:
            cmsnet.load_weights(weights_path)
    else:
        logdir=build_path+"logs/fit/" + fold_name+'_'+datetime.now().strftime("%Y%m%d-%H%M%S")
    
    
    
    
    
    result = cmsnet.evaluate(ds_test, use_multiprocessing=True, steps=test_steps)
    
    
    
    if multigpu_enable:
        with strategy.scope():
            weights = miou.get_weights()
    else:
        weights = miou.get_weights()
    

    
    result = {'name':fold_name}
    result['classes'] = classes
    result['confusion_matrix'] = weights[0].tolist()
    result['count_params'] = cmsnet.count_params()
    return result
Esempio n. 26
0
 def __init__(self):
     self.aug = iaa.Sequential([
         # iaa.Resize(22),
         iaa.Fog()
     ])
Esempio n. 27
0
    "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
Esempio n. 28
0
    iaa.Add((-80, 80), per_channel=0.5),
    iaa.Multiply((0.5, 1.5), per_channel=0.5),
    iaa.AverageBlur(k=((5), (1, 3))),
    iaa.AveragePooling(2),
    iaa.AddElementwise((-20, -5)),
    iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)),
    iaa.JpegCompression(compression=(50, 99)),
    iaa.MultiplyHueAndSaturation(mul_hue=(0.5, 1.5)),
    iaa.WithBrightnessChannels(iaa.Add((-50, 50))),
    iaa.WithBrightnessChannels(iaa.Add((-50, 50)),
                               to_colorspace=[iaa.CSPACE_Lab, iaa.CSPACE_HSV]),
    iaa.MaxPooling(2),
    iaa.MinPooling((1, 2)),
    # iaa.Superpixels(p_replace=(0.1, 0.2), n_segments=(16, 128)),
    iaa.Clouds(),
    iaa.Fog(),
    iaa.AdditiveGaussianNoise(scale=0.1 * 255, per_channel=True),
    iaa.Dropout(p=(0, 0.2)),

    # iaa.WithChannels(0, iaa.Affine(rotate=(0, 0))),
    iaa.ChannelShuffle(0.35),
    iaa.WithColorspace(to_colorspace="HSV",
                       from_colorspace="RGB",
                       children=iaa.WithChannels(0, iaa.Add((0, 50)))),
    #
    iaa.WithHueAndSaturation([
        iaa.WithChannels(0, iaa.Add((-30, 10))),
        iaa.WithChannels(
            1, [iaa.Multiply((0.5, 1.5)),
                iaa.LinearContrast((0.75, 1.25))])
    ]),
Esempio n. 29
0
    new_image_path = os.path.join(directory_path, new_image_name)

    image = imageio.imread(old_image_path)
    foggyimage = aug.augment_image(image)
    cv2.imwrite(new_image_path, foggyimage)


# In[ ]:

import os
import cv2
import PIL
from PIL import Image
import matplotlib.image as mpimg
import math
from PIL import Image, ImageFilter
from bs4 import BeautifulStoneSoup, Tag, NavigableString
import imageio
import imgaug as ia
from imgaug import augmenters as iaa

main_path = '/home/user/anaconda3/imgaug-master/dataset6661jpeg'
#size of snowflakes
aug = iaa.Fog()
jpeg_files = imageFilesIn(main_path)
for image_file_name in jpeg_files:
    image_path = os.path.join(main_path, image_file_name)
    makefoggyImageFrom(image_path)

# In[ ]: