Exemplo n.º 1
0
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)
    ]),
    iaa.OneOf([
        iaa.AveragePooling([2, 3]),
        iaa.MaxPooling(([2, 3], [2, 3])),
Exemplo n.º 2
0
def data_aug(images):
    seq = iaa.Sometimes(
        0.5, iaa.Identity(),
        iaa.Sometimes(
            0.5,
            iaa.Sequential([
                iaa.Fliplr(0.5),
                iaa.Sometimes(
                    0.5,
                    iaa.OneOf([
                        iaa.Add((-40, 40)),
                        iaa.AddElementwise((-40, 40)),
                        iaa.AdditiveGaussianNoise(scale=(0, 0.2 * 255)),
                        iaa.AdditiveLaplaceNoise(scale=(0, 0.2 * 255)),
                        iaa.AdditivePoissonNoise((0, 40)),
                        iaa.MultiplyElementwise((0.5, 1.5)),
                        iaa.ReplaceElementwise(0.1, [0, 255]),
                        iaa.SaltAndPepper(0.1)
                    ])),
                iaa.OneOf([
                    iaa.Cutout(nb_iterations=2,
                               size=0.15,
                               cval=0,
                               squared=False),
                    iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25)),
                    iaa.Dropout(p=(0, 0.2)),
                    iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.1)),
                    iaa.Cartoon(),
                    iaa.BlendAlphaVerticalLinearGradient(iaa.TotalDropout(1.0),
                                                         min_value=0.2,
                                                         max_value=0.8),
                    iaa.GaussianBlur(sigma=(0.0, 3.0)),
                    iaa.AverageBlur(k=(2, 11)),
                    iaa.MedianBlur(k=(3, 11)),
                    iaa.BilateralBlur(d=(3, 10),
                                      sigma_color=(10, 250),
                                      sigma_space=(10, 250)),
                    iaa.MotionBlur(k=20),
                    iaa.AllChannelsCLAHE(),
                    iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0)),
                    iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)),
                    iaa.Affine(scale=(0.5, 1.5)),
                    iaa.Affine(translate_px={
                        "x": (-20, 20),
                        "y": (-20, 20)
                    }),
                    iaa.Affine(shear=(-16, 16)),
                    iaa.pillike.EnhanceSharpness()
                ]),
                iaa.OneOf([
                    iaa.GammaContrast((0.5, 2.0)),
                    iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
                    iaa.LogContrast(gain=(0.6, 1.4)),
                    iaa.LinearContrast((0.4, 1.6)),
                    iaa.pillike.EnhanceBrightness()
                ])
            ]),
            iaa.Sometimes(0.5, iaa.RandAugment(n=2, m=9),
                          iaa.RandAugment(n=(0, 3), m=(0, 9)))))
    images = seq(images=images)
    return images
Exemplo n.º 3
0
def test_dtype_preservation():
    reseed()

    size = (4, 16, 16, 3)
    images = [
        np.random.uniform(0, 255, size).astype(np.uint8),
        np.random.uniform(0, 65535, size).astype(np.uint16),
        np.random.uniform(0, 4294967295, size).astype(np.uint32),
        np.random.uniform(-128, 127, size).astype(np.int16),
        np.random.uniform(-32768, 32767, size).astype(np.int32),
        np.random.uniform(0.0, 1.0, size).astype(np.float32),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float16),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float32),
        np.random.uniform(-1000.0, 1000.0, size).astype(np.float64)
    ]

    default_dtypes = set([arr.dtype for arr in images])

    # Some dtypes are here removed per augmenter, because the respective
    # augmenter does not support them. This test currently only checks whether
    # dtypes are preserved from in- to output for all dtypes that are supported
    # per augmenter.
    # dtypes are here removed via list comprehension instead of
    # `default_dtypes - set([dtype])`, because the latter one simply never
    # removed the dtype(s) for some reason

    def _not_dts(dts):
        return [dt for dt in default_dtypes if dt not in dts]

    augs = [
        (iaa.Add((-5, 5),
                 name="Add"), _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AddElementwise((-5, 5), name="AddElementwise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AdditiveGaussianNoise(0.01 * 255, name="AdditiveGaussianNoise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Multiply((0.95, 1.05), name="Multiply"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Dropout(0.01, name="Dropout"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Invert(0.01, per_channel=True, name="Invert"), default_dtypes),
        (iaa.GaussianBlur(sigma=(0.95, 1.05),
                          name="GaussianBlur"), _not_dts([np.float16])),
        (iaa.AverageBlur((3, 5), name="AverageBlur"),
         _not_dts([np.uint32, np.int32, np.float16])),
        (iaa.MedianBlur((3, 5), name="MedianBlur"),
         _not_dts([np.uint32, np.int32, np.float16, np.float64])),
        (iaa.BilateralBlur((3, 5), name="BilateralBlur"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float16, np.float64
         ])),
        (iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.DirectedEdgeDetect(alpha=(0.0, 0.1),
                                direction=0,
                                name="DirectedEdgeDetect"),
         _not_dts([np.uint32, np.int32, np.float16, np.uint32])),
        (iaa.Fliplr(0.5, name="Fliplr"), default_dtypes),
        (iaa.Flipud(0.5, name="Flipud"), default_dtypes),
        (iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(translate_percent=(-0.05, 0.05),
                    name="Affine-translate-percent"),
         _not_dts([np.uint32, np.int32])),
        (iaa.Affine(rotate=(-20, 20),
                    name="Affine-rotate"), _not_dts([np.uint32, np.int32])),
        (iaa.Affine(shear=(-20, 20),
                    name="Affine-shear"), _not_dts([np.uint32, np.int32])),
        (iaa.Affine(scale=(0.9, 1.1),
                    name="Affine-scale"), _not_dts([np.uint32, np.int32])),
        (iaa.PiecewiseAffine(scale=(0.001, 0.005),
                             name="PiecewiseAffine"), default_dtypes),
        (iaa.ElasticTransformation(alpha=(0.1, 0.2),
                                   sigma=(0.1, 0.2),
                                   name="ElasticTransformation"),
         _not_dts([np.float16])),
        (iaa.Sequential([iaa.Identity(), iaa.Identity()],
                        name="SequentialNoop"), default_dtypes),
        (iaa.SomeOf(1, [iaa.Identity(), iaa.Identity()],
                    name="SomeOfNoop"), default_dtypes),
        (iaa.OneOf([iaa.Identity(), iaa.Identity()],
                   name="OneOfNoop"), default_dtypes),
        (iaa.Sometimes(0.5, iaa.Identity(),
                       name="SometimesNoop"), default_dtypes),
        (iaa.Sequential([iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))],
                        name="Sequential"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.SomeOf(1, [iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))],
                    name="SomeOf"), _not_dts([np.uint32, np.int32,
                                              np.float64])),
        (iaa.OneOf([iaa.Add(
            (-5, 5)), iaa.AddElementwise((-5, 5))],
                   name="OneOf"), _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Sometimes(0.5, iaa.Add((-5, 5)), name="Sometimes"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Identity(name="Identity"), default_dtypes),
        (iaa.Alpha((0.0, 0.1), iaa.Identity(),
                   name="AlphaIdentity"), default_dtypes),
        (iaa.AlphaElementwise(
            (0.0, 0.1), iaa.Identity(),
            name="AlphaElementwiseIdentity"), default_dtypes),
        (iaa.SimplexNoiseAlpha(iaa.Identity(),
                               name="SimplexNoiseAlphaIdentity"),
         default_dtypes),
        (iaa.FrequencyNoiseAlpha(exponent=(-2, 2),
                                 first=iaa.Identity(),
                                 name="SimplexNoiseAlphaIdentity"),
         default_dtypes),
        (iaa.Alpha((0.0, 0.1), iaa.Add(10),
                   name="Alpha"), _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10),
                              name="AlphaElementwise"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.FrequencyNoiseAlpha(exponent=(-2, 2),
                                 first=iaa.Add(10),
                                 name="SimplexNoiseAlpha"),
         _not_dts([np.uint32, np.int32, np.float64])),
        (iaa.Superpixels(p_replace=0.01, n_segments=64),
         _not_dts([np.float16, np.float32, np.float64])),
        (iaa.Resize({
            "height": 4,
            "width": 4
        }, name="Resize"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ])),
        (iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ])),
        (iaa.Pad(px=(0, 10), name="Pad"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ])),
        (iaa.Crop(px=(0, 10), name="Crop"),
         _not_dts([
             np.uint16, np.uint32, np.int16, np.int32, np.float32, np.float16,
             np.float64
         ]))
    ]

    for (aug, allowed_dtypes) in augs:
        for images_i in images:
            if images_i.dtype in allowed_dtypes:
                images_aug = aug.augment_images(images_i)
                assert images_aug.dtype == images_i.dtype
Exemplo n.º 4
0
    # 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
Exemplo n.º 5
0
def black_and_white_aug():
    alpha_seconds = iaa.OneOf([
        iaa.Affine(rotate=(-3, 3)),
        iaa.Affine(translate_percent={
            "x": (0.95, 1.05),
            "y": (0.95, 1.05)
        }),
        iaa.Affine(scale={
            "x": (0.95, 1.05),
            "y": (0.95, 1.05)
        }),
        iaa.Affine(shear=(-2, 2)),
        iaa.CoarseDropout(p=0.1, size_percent=(0.08, 0.02)),
    ])

    first_set = iaa.OneOf([
        iaa.Multiply(iap.Choice([0.5, 1.5]), per_channel=True),
        iaa.EdgeDetect((0.1, 1)),
    ])

    second_set = iaa.OneOf([
        iaa.AddToHueAndSaturation((-40, 40)),
        iaa.ContrastNormalization((0.5, 2.0), per_channel=True)
    ])

    color_aug = iaa.Sequential(
        [
            # Original Image Domain ==================================================

            # Geometric Rigid
            iaa.Fliplr(0.5),
            iaa.OneOf([
                iaa.Noop(),
                iaa.Affine(rotate=90),
                iaa.Affine(rotate=180),
                iaa.Affine(rotate=270),
            ]),
            iaa.OneOf([
                iaa.Noop(),
                iaa.Crop(percent=(0, 0.1)),  # Random Crops
                iaa.PerspectiveTransform(scale=(0.05, 0.15)),
            ]),

            # Affine
            sometimes(
                iaa.PiecewiseAffine(
                    scale=(0.01, 0.07), nb_rows=(3, 6), nb_cols=(3, 6))),
            fewtimes(
                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=(-45, 45),
                           shear=(-16, 16),
                           order=[0, 1],
                           cval=0)),

            # Transformations outside Image domain ==============================================

            # COLOR, CONTRAST, HUE
            iaa.Invert(0.5, name='Invert'),
            fewtimes(iaa.Add((-10, 10), per_channel=0.5, name='Add')),
            fewtimes(
                iaa.AddToHueAndSaturation(
                    (-40, 40), per_channel=0.5, name='AddToHueAndSaturation')),

            # Intensity / contrast
            fewtimes(
                iaa.ContrastNormalization(
                    (0.8, 1.1), name='ContrastNormalization')),

            # Add to hue and saturation
            fewtimes(
                iaa.Multiply(
                    (0.5, 1.5), per_channel=0.5, name='HueAndSaturation')),

            # Noise ===========================================================================
            fewtimes(
                iaa.AdditiveGaussianNoise(loc=0,
                                          scale=(0.0, 0.15 * 255),
                                          per_channel=0.5,
                                          name='AdditiveGaussianNoise')),
            fewtimes(
                iaa.Alpha(factor=(0.5, 1),
                          first=iaa.ContrastNormalization(
                              (0.5, 2.0), per_channel=True),
                          second=alpha_seconds,
                          per_channel=0.5,
                          name='AlphaNoise'), ),
            fewtimes(
                iaa.SimplexNoiseAlpha(first=first_set,
                                      second=second_set,
                                      per_channel=0.5,
                                      aggregation_method="max",
                                      sigmoid=False,
                                      upscale_method='cubic',
                                      size_px_max=(2, 12),
                                      name='SimplexNoiseAlpha'), ),
            fewtimes(
                iaa.FrequencyNoiseAlpha(first=first_set,
                                        second=second_set,
                                        per_channel=0.5,
                                        aggregation_method="max",
                                        sigmoid=False,
                                        upscale_method='cubic',
                                        size_px_max=(2, 12),
                                        name='FrequencyNoiseAlpha'), ),

            # Blur
            fewtimes(
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.AverageBlur(k=(2, 7)),
                    iaa.MedianBlur(k=(3, 11)),
                    iaa.BilateralBlur(d=(3, 10),
                                      sigma_color=(10, 250),
                                      sigma_space=(10, 250))
                ],
                          name='Blur')),

            # Regularization ======================================================================
            unlikely(
                iaa.OneOf([
                    iaa.Dropout((0.01, 0.1), per_channel=0.5, name='Dropout'),
                    iaa.CoarseDropout((0.03, 0.15),
                                      size_percent=(0.02, 0.05),
                                      per_channel=0.5,
                                      name='CoarseDropout'),
                ], )),
        ],
        random_order=True)

    seq = iaa.Sequential(
        [
            iaa.Sequential(
                [
                    # Texture
                    rarely(
                        iaa.Superpixels(p_replace=(0.3, 1.0),
                                        n_segments=(500, 1000),
                                        name='Superpixels')),
                    rarely(
                        iaa.Sharpen(alpha=(0, 0.5),
                                    lightness=(0.75, 1.0),
                                    name='Sharpen')),
                    rarely(
                        iaa.Emboss(
                            alpha=(0, 1.0), strength=(0, 1.0), name='Emboss')),
                    rarely(
                        iaa.OneOf([
                            iaa.EdgeDetect(alpha=(0, 0.5)),
                            iaa.DirectedEdgeDetect(alpha=(0, 0.5),
                                                   direction=(0.0, 1.0)),
                        ],
                                  name='EdgeDetect')),
                    rarely(
                        iaa.ElasticTransformation(
                            alpha=(0.5, 3.5),
                            sigma=0.25,
                            name='ElasticTransformation')),
                ],
                random_order=True),
            color_aug,
            iaa.Grayscale(alpha=1.0, name='Grayscale')
        ],
        random_order=False)

    def activator_masks(images, augmenter, parents, default):
        if 'Unnamed' not in augmenter.name:
            return False
        else:
            return default

    hooks_masks = ia.HooksImages(activator=activator_masks)
    return seq, hooks_masks
    def __init__(self):
        self.seq = iaa.Sequential(
            [
                iaa.Fliplr(0.5),
                iaa.Sometimes(0.5, iaa.Crop(percent=(0, 0.1))),

                iaa.Sometimes(0.5, iaa.Affine(
                    rotate=(-20, 20),  # 旋转±20度
                    # shear=(-16, 16),   # 剪切变换±16度,矩形变平行四边形
                    # order=[0, 1],  # 使用最近邻插值 或 双线性插值
                    cval=0,  # 填充值
                    mode=ia.ALL  # 定义填充图像外区域的方法
                )),

                # 使用0~3个方法进行图像增强
                iaa.SomeOf((0, 3),
                           [
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.GaussianBlur((0, 2.0)),  # 高斯模糊
                                   iaa.AverageBlur(k=(1, 5)),  # 平均模糊,磨砂
                               ])),

                               # 要么运动,要么美颜
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.MotionBlur(k=(3, 11)),  # 运动模糊
                                   iaa.BilateralBlur(d=(1, 5),
                                                     sigma_color=(10, 250),
                                                     sigma_space=(10, 250)),  # 双边滤波,美颜
                               ])),

                               # 模仿雪花
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.SaltAndPepper(p=(0., 0.03)),
                                   iaa.AdditiveGaussianNoise(loc=0, scale=(0., 0.05 * 255), per_channel=False)
                               ])),

                               # 对比度
                               iaa.Sometimes(0.8, iaa.LinearContrast((0.6, 1.4), per_channel=0.5)),

                               # 锐化
                               iaa.Sometimes(0.8, iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))),

                               # 整体亮度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   # 加性调整
                                   iaa.AddToBrightness((-30, 30)),
                                   # 线性调整
                                   iaa.MultiplyBrightness((0.5, 1.5)),
                                   # 加性 & 线性
                                   iaa.MultiplyAndAddToBrightness(mul=(0.5, 1.5), add=(-30, 30)),
                                ])),

                               # 饱和度
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToSaturation((-75, 75)),
                                   iaa.MultiplySaturation((0., 3.)),
                               ])),

                               # 色相
                               iaa.Sometimes(0.8, iaa.OneOf([
                                   iaa.AddToHue((-255, 255)),
                                   iaa.MultiplyHue((-3.0, 3.0)),
                               ])),

                               # 云雾
                               # iaa.Sometimes(0.3, iaa.Clouds()),

                               # 卡通化
                               # iaa.Sometimes(0.01, iaa.Cartoon()),
                           ],
                           random_order=True
                           )
            ],
            random_order=True
        )
Exemplo n.º 7
0
    def __init__(self,list_file,train,transform, device, little_train=False, with_file_path=False, with_mask=False, S=7, B = 2, C = 20, test_mode=False):
        print('data init')
        
        self.train = train
        self.transform=transform
        self.fnames = []
        self.boxes = []
        self.labels = []
        self.resize = 448
        self.with_mask = with_mask
        self.S = S
        self.B = B
        self.C = C
        self.device = device
        self._test = test_mode
        self.with_file_path = with_file_path
        self.img_augsometimes = lambda aug: iaa.Sometimes(0.25, aug)
        self.bbox_augsometimes = lambda aug: iaa.Sometimes(0.5, aug)

        self.augmentation = iaa.Sequential(
            [
                # augment without change bboxes 
                self.img_augsometimes(
                    iaa.SomeOf((1, 3), [
                        iaa.Dropout([0.05, 0.2]),      # drop 5% or 20% of all pixels
                        iaa.Sharpen((0.1, .8)),       # sharpen the image
                        # iaa.GaussianBlur(sigma=(2., 3.5)),
                        iaa.OneOf([
                            iaa.GaussianBlur(sigma=(2., 3.5)),
                            iaa.AverageBlur(k=(2, 5)),
                            iaa.BilateralBlur(d=(7, 12), sigma_color=(10, 250), sigma_space=(10, 250)),
                            iaa.MedianBlur(k=(3, 7)),
                        ]),
                        

                        iaa.AddElementwise((-50, 50)),
                        iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                        iaa.JpegCompression(compression=(80, 95)),

                        iaa.Multiply((0.5, 1.5)),
                        iaa.MultiplyElementwise((0.5, 1.5)),
                        iaa.ReplaceElementwise(0.05, [0, 255]),
                        # iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                        #                 children=iaa.WithChannels(2, iaa.Add((-10, 50)))),
                        iaa.OneOf([
                            iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                                            children=iaa.WithChannels(1, iaa.Add((-10, 50)))),
                            iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                                            children=iaa.WithChannels(2, iaa.Add((-10, 50)))),
                        ]),

                    ], random_order=True)
                ),

                # iaa.Fliplr(.5),
                # iaa.Flipud(.125),
                # # augment changing bboxes 
                # self.bbox_augsometimes(
                #     iaa.Affine(
                #         # translate_px={"x": 40, "y": 60},
                #         scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                #         translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
                #         rotate=(-5, 5),
                #     )
                # )
            ],
            random_order=True
        )

        # torch.manual_seed(23)
        with open(list_file) as f:
            lines  = f.readlines()
        
        if little_train:
            lines = lines[:64*8]

        for line in lines:
            splited = line.strip().split()
            self.fnames.append(splited[0])
            
        self.num_samples = len(self.fnames)
Exemplo n.º 8
0
        pos_risk = pos_risk / len(input)

        return {'loss': loss, 'neg_risk': neg_risk, 'pos_risk': pos_risk}


if __name__ == "__main__":
    from ksptrack.pu.set_explorer import SetExplorer
    from torch.utils.data import DataLoader
    from imgaug import augmenters as iaa
    from ksptrack.pu.utils import df_to_tgt
    import matplotlib.pyplot as plt

    transf = iaa.Sequential([
        iaa.OneOf([
            iaa.BilateralBlur(d=8,
                              sigma_color=(100, 150),
                              sigma_space=(100, 150)),
            iaa.AdditiveGaussianNoise(scale=(0, 0.06 * 255)),
            iaa.GammaContrast((1., 2.))
        ])
        # iaa.Flipud(p=0.5),
        # iaa.Fliplr(p=.5),
        # iaa.Rot90((1, 3))
    ])

    dl = SetExplorer('/home/ubelix/lejeune/data/medical-labeling/Dataset00',
                     augmentations=transf,
                     normalization='rescale',
                     resize_shape=512)
    criterion = PULoss(pxls=True)
    dl = DataLoader(dl, collate_fn=dl.collate_fn)
Exemplo n.º 9
0
        transformed_image = transform(image=image)['image']

    elif augmentation == 'median_blur':
        transform = MedianBlur(always_apply=True, blur_limit=(18, 25))
        transformed_image = transform(image=image)['image']

    elif augmentation == 'motion_blur':
        transform = iaa.MotionBlur(k=15)
        transformed_image = transform(image=image)

    elif augmentation == 'average_blur':
        transform = iaa.AverageBlur(k=(2, 11))
        transformed_image = transform(image=image)

    elif augmentation == 'bilateral_blur':
        transform = iaa.BilateralBlur(d=(3, 10), sigma_color=(250), 
                                                 sigma_space=(250))
        transformed_image = transform(image=image)

    elif augmentation == 'mean_shift_blur':
        transform = iaa.MeanShiftBlur()
        transformed_image = transform(image=image)

    elif augmentation == 'glass_blur':
        transform = GlassBlur(always_apply=True)
        transformed_image = transform(image=image)['image']

    elif augmentation == 'defocus_blur':
        transform = iaa.imgcorruptlike.DefocusBlur(severity=2)
        transformed_image = transform(image=image)

    elif augmentation == 'zoom_blur':
Exemplo n.º 10
0
def det_aug(image, polys_np=None):
    """
    随机对图像做以下的增强操作
    :param image: cv2 read
    :param polys_np:[N, 4, 2]
    :return:
    """
    aug_sample = random.sample(cfg.TRAIN.AUG_TOOL, 1)[0]  #从数组中随机取出一个增强的功能

    rotate_sample = random.choice([0, 1])

    ######################################################################################################
    # blur-模糊
    aug = None
    # 高斯滤波 sigma 为1-10的保留小数点后一位的float的随机值,可根据情况调整
    if aug_sample == 'GaussianBlur':
        sigma = random.uniform(1, 2)
        sigma = round(sigma, 10)
        aug = iaa.GaussianBlur(sigma)

    # 平均模糊 k 为1-10的随机 奇 数,范围根据情况调整
    if aug_sample == 'AverageBlur':
        k = random.randint(8, 10) * 2 + 1
        aug = iaa.AverageBlur(k)

    # 中值滤波 k 为1-10的随机 奇 数,范围根据情况调整
    if aug_sample == 'MedianBlur':
        k = random.randint(8, 10) * 2 + 1
        aug = iaa.MedianBlur(k)

    # 双边滤波 d=1 为 奇 数, sigma_color=(10, 250), sigma_space=(10, 250)
    if aug_sample == 'BilateralBlur':
        d = random.randint(0, 2) * 2 + 1
        sigma_color = random.randint(10, 250)
        sigma_space = random.randint(10, 250)
        aug = iaa.BilateralBlur(d, sigma_color, sigma_space)

    # 运动模糊 k=5 一定大于3 的 奇 数, angle=(0, 360), direction=(-1.0, 1.0)
    if aug_sample == 'MotionBlur':
        k = random.randint(15, 20) * 2 + 1
        angle = random.randint(0, 360)
        direction = random.uniform(-1, 1)
        direction = round(direction, 1)
        aug = iaa.MotionBlur(k, angle, direction)

    ######################################################################################################
    # geometric  几何学

    # 弹性变换
    if aug_sample == 'ElasticTransformation':
        alpha = random.uniform(10, 20)
        alpha = round(alpha, 1)
        sigma = random.uniform(5, 10)
        sigma = round(sigma, 1)
        # print(alpha, sigma)
        aug = iaa.ElasticTransformation(alpha, sigma)

    # 透视
    if aug_sample == 'PerspectiveTransform':
        scale = random.uniform(0, 0.15)
        scale = round(scale, 3)
        aug = iaa.PerspectiveTransform(scale)

    # 旋转角度
    if aug_sample == 'Affine_rot':
        rotate = random.randint(-180, 180)
        while rotate == 0:
            rotate = random.randint(-180, 180)
        if rotate_sample == 0:
            aug = iaa.Affine(rotate=rotate, fit_output=True)
        else:
            aug = iaa.Affine(rotate=rotate)

    if aug_sample == 'Affine_scale':
        scale = random.uniform(0, 2)
        scale = round(scale, 1)
        while scale == 0 or scale <= 0.3:
            scale = random.uniform(0, 2)
            scale = round(scale, 1)

        if rotate_sample == 0:
            aug = iaa.Affine(scale=scale, fit_output=True)
        else:
            aug = iaa.Affine(scale=scale)

    ######################################################################################################
    # flip 镜像

    # 水平镜像
    if aug_sample == 'Fliplr':
        aug = iaa.Fliplr(1)
    #
    # 垂直镜像
    if aug_sample == 'Flipud':
        aug = iaa.Flipud(1)

    ######################################################################################################
    # size 尺寸

    # if aug_sample == 'CropAndPad':
    #     top = random.randint(0, 10)
    #     right = random.randint(0, 10)
    #     bottom = random.randint(0, 10)
    #     left = random.randint(0, 10)
    #     aug = iaa.CropAndPad(px=(top, right, bottom, left))  # 上 右 下 左 各crop多少像素,然后进行padding

    if aug_sample == 'Crop':
        top = random.randint(0, 10)
        right = random.randint(0, 10)
        bottom = random.randint(0, 10)
        left = random.randint(0, 10)
        aug = iaa.Crop(px=(top, right, bottom, left))  # 上 右 下 左

    if aug_sample == 'Pad':
        top = random.randint(0, 10)
        right = random.randint(0, 10)
        bottom = random.randint(0, 10)
        left = random.randint(0, 10)
        aug = iaa.Pad(px=(top, right, bottom, left))  # 上 右 下 左

    # if aug_sample == 'PadToFixedSize':
    #     height = image.shape[0] + 32
    #     width = image.shape[1] + 100
    #     aug = iaa.PadToFixedSize(width=width, height=height)z

    # if aug_sample == 'CropToFixedSize':
    #     height = image.shape[0] - 32
    #     width = image.shape[1] - 100
    #     aug = iaa.CropToFixedSize(width=width, height=height)

    if polys_np is not None:
        if aug is not None:
            # print(aug_sample)
            h, w, _ = image.shape
            boxes_info_list = []
            for box in polys_np:
                boxes_info_list.append(Polygon(box))

            psoi = ia.PolygonsOnImage(boxes_info_list,
                                      shape=image.shape)  # 生成单个图像上所有多边形的对象
            image, psoi_aug = aug(image=image, polygons=psoi)

            pts_list = []
            for each_poly in psoi_aug.polygons:
                pts_list.append(np.array(each_poly.exterior).reshape((4, 2)))
            return image, np.array(pts_list, np.float32).reshape((-1, 4, 2))
        else:

            return image, polys_np
    else:
        if aug is not None:
            image = aug(image=image)
        else:
            image = image
        return image
Exemplo n.º 11
0
    'addPN': iaa.AdditivePoissonNoise(lam=16.00),
    'addPNp': iaa.AdditivePoissonNoise(lam=16.00, per_channel=True),
    'mul-': iaa.Multiply(mul=0.50),
    'mul+': iaa.Multiply(mul=1.50),
    'mulp-': iaa.Multiply(mul=0.50, per_channel=True),
    'mulp+': iaa.Multiply(mul=1.50, per_channel=True),
    'jpeg': iaa.JpegCompression(compression=62),
    'jpeg+': iaa.JpegCompression(compression=75),
    'jpeg++': iaa.JpegCompression(compression=87)
}

blur = {
    'GBlur': iaa.GaussianBlur(sigma=1.00),
    'ABlur': iaa.AverageBlur(k=3),
    'MBlur': iaa.MedianBlur(k=3),
    'BBlur': iaa.BilateralBlur(sigma_color=250, sigma_space=250, d=5),
    'MoBlur': iaa.MotionBlur(angle=0, k=7),
    'MoBlurAng': iaa.MotionBlur(angle=144, k=5)
}

color = {
    'ATHAS-': iaa.AddToHueAndSaturation(value=-45),
    'ATHAS+': iaa.AddToHueAndSaturation(value=45),
    'Gray': iaa.Grayscale(alpha=0.2)
}

contrast = {
    'GContrast-': iaa.GammaContrast(gamma=0.81),
    'GContrast+': iaa.GammaContrast(gamma=1.44),
    'SContrast': iaa.SigmoidContrast(cutoff=0.5, gain=10),
    'LContrast': iaa.LogContrast(gain=0.88),
Exemplo n.º 12
0
 def __init__(self, image):
     self.img = image
     # 随机通道处理,加减100以内
     # self.aug_WithChannels = iaa.WithChannels((0,2), iaa.Add((-100, 100)))
     # 随机裁剪和填充,percent为裁剪与填充比例,负数为放大后裁剪,正数为缩小和填充,pad_mode为填充方式,pad_cval为当空白填充时,填充像素值
     self.aug_CropAndPad = iaa.CropAndPad(percent=(-0.05, 0.1),
                                          pad_mode=ia.ALL,
                                          pad_cval=(0, 255))
     # 随机水平翻转,参数为概率
     self.aug_Fliplr = iaa.Fliplr(0.5)
     # 随机垂直翻转,参数为概率
     self.aug_Flipud = iaa.Flipud(0.5)
     # 超像素表示,p_replace被超像素代替的百分比,n_segments分割块数
     self.aug_Superpixels = iaa.Superpixels(p_replace=(0, 1.0),
                                            n_segments=(20, 200))
     # 灰度化 (0.0,1.0),前者为偏彩色部分,后者为偏灰度部分,随机灰度化
     self.aug_GrayScale = iaa.Grayscale(alpha=(0.0, 0.6))
     # 高斯模糊
     self.aug_GaussianBlur = iaa.GaussianBlur(sigma=(0, 3.0))
     # 均值模糊,k为kernel size
     self.aug_AverageBlur = iaa.AverageBlur(k=(2, 7))
     # 中值模糊, k为kernel size
     self.aug_MedianBlur = iaa.MedianBlur(k=(3, 11))
     # 双边滤波,d为kernel size,sigma_color为颜色域标准差,sigma_space为空间域标准差
     self.aug_BilateralBlur = iaa.BilateralBlur(sigma_color=(0, 250),
                                                sigma_space=(0, 250),
                                                d=(3, 7))
     # 锐化
     self.aug_Sharpen = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))
     # 浮雕效果
     self.aug_Emboss = iaa.Emboss(alpha=(0.0, 1.0), strength=(0.0, 1.5))
     # 边缘检测
     self.aug_EdgeDetect = iaa.EdgeDetect(alpha=(0.0, 1.0))
     # 方向性边缘检测
     self.aug_DirectedEdgeDetece = iaa.DirectedEdgeDetect(alpha=(0.0, 1.0),
                                                          direction=(0.0,
                                                                     1.0))
     # 暴力叠加像素值,每个像素统一加一个值
     self.aug_Add = iaa.Add((-40, 40))
     # 暴力叠加像素值,每个像素加不同的值
     self.aug_AddElementwise = iaa.AddElementwise((-40, 40))
     # 随机高斯加性噪声
     self.aug_AdditiveGaussianNoise = iaa.AdditiveGaussianNoise(scale=(0.0,
                                                                       0.1 *
                                                                       255))
     # 暴力乘法,每个像素统一乘以一个值
     self.aug_Multiply = iaa.Multiply((0.8, 1.2))
     # 暴力乘法,每个像素乘以不同值
     self.aug_MultiplyElementwise = iaa.MultiplyElementwise((0.8, 1.2))
     # 随机dropout像素值
     self.aug_Dropout = iaa.Dropout(p=(0, 0.2))
     # 随机粗dropout,2*2方块像素被dropout
     self.aug_CoarseDropout = iaa.CoarseDropout(0.02, size_percent=0.5)
     # 50%的图片,p概率反转颜色
     self.aug_Invert = iaa.Invert(0.25, per_channel=0.5)
     # 对比度归一化
     self.aug_ContrastNormalization = iaa.ContrastNormalization((0.5, 1.5))
     # 仿射变换
     self.aug_Affine = iaa.Affine(rotate=(0, 20),
                                  scale={
                                      "x": (0.8, 1.2),
                                      "y": (0.8, 1.2)
                                  })
     # 仿射变换, 局部像素仿射扭曲
     self.aug_PiecewiseAffine = iaa.PiecewiseAffine(scale=(0.01, 0.05))
     # 单应性变换
     self.aug_PerspectiveTransform = iaa.PerspectiveTransform(scale=(0.01,
                                                                     0.1))
     # 弹性变换
     self.aug_ElasticTransformation = iaa.ElasticTransformation(alpha=(0,
                                                                       5.0),
                                                                sigma=0.25)
     # 简单的加噪,小黑块
     self.aug_SimplexNoiseAlpha = iaa.SimplexNoiseAlpha(
         iaa.OneOf([
             iaa.EdgeDetect(alpha=(0.0, 0.5)),
             iaa.DirectedEdgeDetect(alpha=(0.0, 0.5), direction=(0.0, 1.0)),
         ]))
     # 频域加噪,表现为色彩的块状变换
     self.aug_FrequencyNoiseAlpha = iaa.FrequencyNoiseAlpha(
         exponent=(-4, 0),
         first=iaa.Multiply((0.5, 1.5), per_channel=True),
         second=iaa.ContrastNormalization((0.5, 2.0)))
Exemplo n.º 13
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
Exemplo n.º 14
0
def draw_per_augmenter_images(img_path, idx):
    print("[draw_per_augmenter_images] Loading image...")
    image = ndimage.imread(img_path)
    print(img_path)
    textPath = img_path.replace('.jpg', '.txt')
    f = open(textPath, 'r')
    rows_keypoints = []
    classId = []
    while True:
        line = f.readline()
        if not line: break
        values = line.split(' ')
        classId.append(values[0])
        cx = image.shape[1] * float(values[1])
        cy = image.shape[0] * float(values[2])
        w = image.shape[1] * float(values[3])
        h = image.shape[0] * float(values[4].replace('\n', ''))
        keypoints = [
            ia.Keypoint(x=(cx - w / 2), y=(cy - h / 2)),
            ia.Keypoint(x=(cx - w / 2), y=(cy + h / 2)),
            ia.Keypoint(x=(cx + w / 2), y=(cy + h / 2)),
            ia.Keypoint(x=(cx + w / 2), y=(cy - h / 2))
        ]
        rows_keypoints.append(keypoints[0])
        rows_keypoints.append(keypoints[1])
        rows_keypoints.append(keypoints[2])
        rows_keypoints.append(keypoints[3])
    f.close()
    keypoints = [ia.KeypointsOnImage(rows_keypoints, shape=image.shape)]
    print("[draw_per_augmenter_images] Initializing...")

    rows_augmenters = [
        (0, "Crop\n(top, right,\nbottom, left)",
         [(str(vals), iaa.Crop(px=vals))
          for vals in [(64, 128, 64,
                        0), (0, 64, 64, 128), (64, 0, 64, 64), (64, 64, 0,
                                                                128)]]),
        (0, "Fliplr", [(str(p), iaa.Fliplr(p)) for p in [1]]),
        (0, "Flipud", [(str(p), iaa.Flipud(p)) for p in [1]]),
        (0, "Add", [("value=%d" % (val, ), iaa.Add(val))
                    for val in [-45, -25, 25, 45]]),
        (0, "Multiply", [("value=%.2f" % (val, ), iaa.Multiply(val))
                         for val in [0.25, 0.5, 1.25, 1.5]]),
        (0, "GaussianBlur", [("sigma=%.2f" % (sigma, ),
                              iaa.GaussianBlur(sigma=sigma))
                             for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]),
        (0, "BilateralBlur\nsigma_color=250,\nsigma_space=250",
         [("d=%d" % (d, ),
           iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250))
          for d in [1, 3, 5, 7, 9]]),
        (0, "Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness, ),
                                    iaa.Sharpen(alpha=1, lightness=lightness))
                                   for lightness in [0.5, 1.0, 1.5, 2.0]]),
        (0, "Emboss\n(alpha=1)", [("strength=%.2f" % (strength, ),
                                   iaa.Emboss(alpha=1, strength=strength))
                                  for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "AdditiveGaussianNoise",
         [("scale=%.2f*255" % (scale, ),
           iaa.AdditiveGaussianNoise(scale=scale * 255))
          for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "Dropout", [("p=%.2f" % (p, ), iaa.Dropout(p=p))
                        for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "SaltAndPepper", [("p=%.2f" % (p, ), iaa.SaltAndPepper(p=p))
                              for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "CoarseSaltAndPepper\n(p=0.2)",
         [("size_percent=%.2f" % (size_percent, ),
           iaa.CoarseSaltAndPepper(p=0.2,
                                   size_percent=size_percent,
                                   min_size=2))
          for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "ContrastNormalization",
         [("alpha=%.1f" % (alpha, ), iaa.ContrastNormalization(alpha=alpha))
          for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]),
        (6, "PerspectiveTransform",
         [("scale=%.3f" % (scale, ), iaa.PerspectiveTransform(scale=scale))
          for scale in [0.075, 0.075, 0.10, 0.125, 0.125]]),
        (0, "Affine: Scale", [("%.1fx" % (scale, ), iaa.Affine(scale=scale))
                              for scale in [0.25, 0.5, 1.5, 2.0]]),
        (0, "Affine: Translate",
         [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={
             "x": x,
             "y": y
         })) for x, y in [(
             int(-image.shape[1] *
                 0.1), int(-image.shape[1] * 0.1)
         ), (int(-image.shape[1] * 0.2),
             int(-image.shape[1] *
                 0.1)), (
                     int(-image.shape[1] * 0.1), int(-image.shape[1] * 0.2)
                 ), (int(image.shape[1] * 0.1), int(image.shape[1] * 0.1)
                     ), (int(image.shape[1] * 0.2), int(image.shape[1] * 0.2))]
          ]),
        (0, "Affine: Rotate",
         [("%d deg" % (rotate, ), iaa.Affine(rotate=rotate))
          for rotate in [-90, -75, -45, -30, -15, 15, 30, 45, 75, 90]]),
        (0, "Affine: Shear", [("%d deg" % (shear, ), iaa.Affine(shear=shear))
                              for shear in [-45, -25, 25, 45]]),
        (0, "Affine: Modes", [(mode, iaa.Affine(translate_px=-32, mode=mode))
                              for mode in ["edge"]]),
        (
            2,
            "Affine: all",
            [
                (
                    "",
                    iaa.Affine(
                        scale={
                            "x": (0.25, 0.75),
                            "y": (0.25, 0.75)
                        },
                        # scale images to 80-120% of their size, individually per axis
                        translate_percent={
                            "x": (-0.25, 0.25),
                            "y": (-0.25, 0.25)
                        },
                        # translate by -20 to +20 percent (per axis)
                        rotate=(-45, 45),  # rotate by -45 to +45 degrees
                        shear=(-25, 25),  # shear by -16 to +16 degrees
                    )) for _ in sm.xrange(5)
            ])
    ]
    print("[draw_per_augmenter_images] Augmenting...")
    rows = []
    for (row_seed, row_name, augmenters) in rows_augmenters:
        ia.seed(row_seed)
        row_images = []
        row_keypoints = []
        row_titles = []
        for img_title, augmenter in augmenters:
            aug_det = augmenter.to_deterministic()
            row_images.append(aug_det.augment_image(image))
            row_keypoints.append(aug_det.augment_keypoints(keypoints)[0])
            row_titles.append(img_title)
        rows.append((row_name, row_images, row_keypoints, row_titles))
    # routine to draw many single files
    seen = defaultdict(lambda: 0)
    markups = []
    m = 0
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        #output_image = ExamplesImage(128, 128, 128+64, 32)
        for image, keypoints in zip(row_images, row_keypoints):

            m += 1
            #print("[draw_per_augmenter_images] Saving augmented images...")

            keypoints.draw_on_image(image, size=40)
            misc.imsave("DB/image_%05d_%05d.jpg" % (m, idx), image)
            #misc.imsave("DB/kpt_image_%05d_%05d.jpg" % (m, idx), image)
            ff = open("DB/image_%05d_%05d.txt" % (m, idx), 'a')
            x = 0
            while True:
                if 4 * x == len(keypoints.keypoints): break
                cx_ = (keypoints.keypoints[4 * x].x +
                       keypoints.keypoints[2 + 4 * x].x) / (2 * image.shape[1])
                cy_ = (keypoints.keypoints[4 * x].y +
                       keypoints.keypoints[2 + 4 * x].y) / (2 * image.shape[0])
                w_ = (keypoints.keypoints[2 + 4 * x].x -
                      keypoints.keypoints[4 * x].x) / image.shape[1]
                h_ = (keypoints.keypoints[2 + 4 * x].y -
                      keypoints.keypoints[4 * x].y) / image.shape[0]
                if (w_ < 1 / (13.0) or h_ < 1 / (13.0)):
                    data = str('32') + ' ' + str(cx_) + ' ' + str(
                        cy_) + ' ' + str(w_) + ' ' + str(h_) + '\n'
                elif ((w_ < 3.0 / (13.0) and w_ > 1 / (13.0))
                      or (h_ < 3.0 / (13.0) and h_ > 3.0 / (13.0))):
                    data = str('33') + ' ' + str(cx_) + ' ' + str(
                        cy_) + ' ' + str(w_) + ' ' + str(h_) + '\n'
                else:
                    data = str('15') + ' ' + str(cx_) + ' ' + str(
                        cy_) + ' ' + str(w_) + ' ' + str(h_) + '\n'
                ff.write(data)
                x += 1
            ff.close()
Exemplo n.º 15
0
    def augument(self, image, bbox_list):
        seq = iaa.Sequential([
            # 变形
            iaa.Sometimes(
                0.6,
                [
                    iaa.OneOf([
                        iaa.Affine(shear={
                            'x': (-1.5, 1.5),
                            'y': (-1.5, 1.5)
                        },
                                   mode="edge"),  # 仿射变化程度,单位像素
                        iaa.Rotate(rotate=(-1, 1), mode="edge"),  # 旋转,单位度
                    ])
                ]),
            # 扭曲
            iaa.Sometimes(
                0.5,
                [
                    iaa.OneOf([
                        iaa.PiecewiseAffine(
                            scale=(0, 0.02), nb_rows=2, nb_cols=2),  # 局部仿射
                        iaa.ElasticTransformation(  # distort扭曲变形
                            alpha=(0, 3),  # 扭曲程度
                            sigma=(0.8, 1),  # 扭曲后的平滑程度
                            mode="nearest"),
                    ]),
                ]),
            # 模糊
            iaa.Sometimes(
                0.5,
                [
                    iaa.OneOf([
                        iaa.GaussianBlur(sigma=(0, 0.7)),
                        iaa.AverageBlur(k=(1, 3)),
                        iaa.MedianBlur(k=(1, 3)),
                        iaa.BilateralBlur(
                            d=(1, 5),
                            sigma_color=(10, 200),
                            sigma_space=(10, 200)),  # 既噪音又模糊,叫双边,
                        iaa.MotionBlur(k=(3, 5)),
                        iaa.Snowflakes(flake_size=(0.1, 0.2),
                                       density=(0.005, 0.025)),
                        iaa.Rain(nb_iterations=1,
                                 drop_size=(0.05, 0.1),
                                 speed=(0.04, 0.08)),
                    ])
                ]),
            # 锐化
            iaa.Sometimes(0.3, [
                iaa.OneOf([
                    iaa.Sharpen(),
                    iaa.GammaContrast(),
                    iaa.SigmoidContrast()
                ])
            ]),
            # 噪音
            iaa.Sometimes(0.3, [
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(scale=(1, 5)),
                    iaa.AdditiveLaplaceNoise(scale=(1, 5)),
                    iaa.AdditivePoissonNoise(lam=(1, 5)),
                    iaa.Salt((0, 0.02)),
                    iaa.Pepper((0, 0.02))
                ])
            ]),
            # 剪切
            iaa.Sometimes(
                0.8,
                [
                    iaa.OneOf([
                        iaa.Crop((0, 2)),  # 切边, (0到10个像素采样)
                    ])
                ]),
        ])

        assert bbox_list is None or type(bbox_list) == list

        if bbox_list is None or len(bbox_list) == 0:
            polys = None
        else:
            polys = [ia.Polygon(pos) for pos in bbox_list]
            polys = ia.PolygonsOnImage(polys, shape=image.shape)

        # 处理部分或者整体出了图像的范围的多边形,参考:https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html
        polys = polys.remove_out_of_image().clip_out_of_image()
        images_aug, polygons_aug = seq(images=[image], polygons=polys)

        image = images_aug[0]

        if polygons_aug is None:
            polys = None
        else:
            polys = []
            for p in polygons_aug.polygons:
                polys.append(p.coords)
            polys = np.array(polys, np.int32).tolist()  # (N,2)

        return image, polys
Exemplo n.º 16
0
    def __init__(self,
                 list_file,
                 train,
                 transform,
                 device,
                 little_train=False,
                 S=7):
        print('data init')

        self.train = train
        self.transform = transform
        self.fnames = []
        self.boxes = []
        self.labels = []
        self.S = S
        self.B = 2
        self.C = 20
        self.device = device

        self.augmentation = iaa.Sometimes(
            0.5,
            iaa.SomeOf(
                (1, 6),
                [
                    iaa.Dropout([0.05, 0.2]),  # drop 5% or 20% of all pixels
                    iaa.Sharpen((0.1, 1.0)),  # sharpen the image
                    iaa.GaussianBlur(sigma=(2., 3.5)),
                    iaa.OneOf([
                        iaa.GaussianBlur(sigma=(2., 3.5)),
                        iaa.AverageBlur(k=(2, 5)),
                        iaa.BilateralBlur(d=(7, 12),
                                          sigma_color=(10, 250),
                                          sigma_space=(10, 250)),
                        iaa.MedianBlur(k=(3, 7)),
                    ]),
                    # iaa.Fliplr(1.0),
                    # iaa.Flipud(1.0),
                    iaa.AddElementwise((-50, 50)),
                    iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                    iaa.JpegCompression(compression=(80, 95)),
                    iaa.Multiply((0.5, 1.5)),
                    iaa.MultiplyElementwise((0.5, 1.5)),
                    iaa.ReplaceElementwise(0.05, [0, 255]),
                    iaa.WithColorspace(to_colorspace="HSV",
                                       from_colorspace="RGB",
                                       children=iaa.WithChannels(
                                           2, iaa.Add((-10, 50)))),
                    iaa.OneOf([
                        iaa.WithColorspace(to_colorspace="HSV",
                                           from_colorspace="RGB",
                                           children=iaa.WithChannels(
                                               1, iaa.Add((-10, 50)))),
                        iaa.WithColorspace(to_colorspace="HSV",
                                           from_colorspace="RGB",
                                           children=iaa.WithChannels(
                                               2, iaa.Add((-10, 50)))),
                    ]),
                ],
                random_order=True))

        torch.manual_seed(23)
        with open(list_file) as f:
            lines = f.readlines()

        if little_train:
            lines = lines[:64]

        for line in lines:
            splited = line.strip().split()
            self.fnames.append(splited[0])

        self.num_samples = len(self.fnames)
Exemplo n.º 17
0
    def __init__(self,
                 imgdirs_list,
                 annfiles_list,
                 train,
                 transform,
                 device,
                 little_train=False,
                 with_file_path=False,
                 S=7,
                 B=2,
                 C=20,
                 test_mode=False):
        print('data init')

        self.imgdirs_list = imgdirs_list
        self.anns_list = annfiles_list
        self.train = train
        self.transform = transform
        self.fnames = []
        self.boxes = []
        self.labels = []
        self.dataset_list = []
        self.resize = 448
        self.S = S
        self.B = B
        self.C = C
        self.device = device
        self._test = test_mode
        self.with_file_path = with_file_path
        self.img_augsometimes = lambda aug: iaa.Sometimes(0.25, aug)
        self.bbox_augsometimes = lambda aug: iaa.Sometimes(0.5, aug)

        self.augmentation = iaa.Sequential(
            [
                # augment without change bboxes
                self.img_augsometimes(
                    iaa.SomeOf(
                        (1, 3),
                        [
                            iaa.Dropout([0.05, 0.2
                                         ]),  # drop 5% or 20% of all pixels
                            iaa.Sharpen((0.1, .8)),  # sharpen the image
                            # iaa.GaussianBlur(sigma=(2., 3.5)),
                            iaa.OneOf([
                                iaa.GaussianBlur(sigma=(2., 3.5)),
                                iaa.AverageBlur(k=(2, 5)),
                                iaa.BilateralBlur(d=(7, 12),
                                                  sigma_color=(10, 250),
                                                  sigma_space=(10, 250)),
                                iaa.MedianBlur(k=(3, 7)),
                            ]),
                            iaa.AddElementwise((-50, 50)),
                            iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                            # iaa.JpegCompression(compression=(80, 95)),
                            iaa.Multiply((0.5, 1.5)),
                            iaa.MultiplyElementwise((0.5, 1.5)),
                            iaa.ReplaceElementwise(0.05, [0, 255]),
                            # iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                            #                 children=iaa.WithChannels(2, iaa.Add((-10, 50)))),
                            iaa.OneOf([
                                iaa.WithColorspace(to_colorspace="HSV",
                                                   from_colorspace="RGB",
                                                   children=iaa.WithChannels(
                                                       1, iaa.Add((-10, 50)))),
                                iaa.WithColorspace(to_colorspace="HSV",
                                                   from_colorspace="RGB",
                                                   children=iaa.WithChannels(
                                                       2, iaa.Add((-10, 50)))),
                            ]),
                        ],
                        random_order=True)),
                iaa.Fliplr(.5),
                iaa.Flipud(.125),
                # augment changing bboxes
                self.bbox_augsometimes(
                    iaa.Affine(
                        # translate_px={"x": 40, "y": 60},
                        scale={
                            "x": (0.8, 1.2),
                            "y": (0.8, 1.2)
                        },
                        translate_percent={
                            "x": (-0.1, 0.1),
                            "y": (-0.1, 0.1)
                        },
                        rotate=(-5, 5),
                    ))
            ],
            random_order=True)
        for imgdir, annfile in zip(self.imgdirs_list, self.anns_list):
            print('handle dataset:\n\t' + imgdir + '\n\t' + annfile)
            annfile_json = json.load(open(annfile, 'r'))
            images = annfile_json['images']
            annotations = annfile_json['annotations']
            ann_dicts = {}
            for ann in annotations:
                if ann['image_id'] not in ann_dicts.keys():
                    ann_dicts[ann['image_id']] = []
                ann_dicts[ann['image_id']].append(ann)
            for img in images:
                img['file_name'] = os.path.join(imgdir, img['file_name'])
                if img['id'] in ann_dicts.keys():
                    anns = ann_dicts[img['id']]
                else:
                    continue
                image_ann = {'image_info': img, 'ann': anns}
                self.dataset_list.append(image_ann)
        self.num_samples = len(self.dataset_list)
        print('There are %d pics in datasets.' % (self.num_samples))
Exemplo n.º 18
0
def get_optimistic_img_aug():
    texture = iaa.OneOf([
        iaa.Superpixels(p_replace=(0.1, 0.3),
                        n_segments=(500, 1000),
                        interpolation="cubic",
                        name='Superpixels'),
        iaa.Sharpen(alpha=(0, 1.0), lightness=(0.5, 1.0), name='Sharpen'),
        iaa.Emboss(alpha=(0, 1.0), strength=(0.1, 0.3), name='Emboss'),
        iaa.OneOf([
            iaa.EdgeDetect(alpha=(0, 0.4)),
            iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
        ],
                  name='EdgeDetect'),
        iaa.ElasticTransformation(alpha=(0.5, 1.0),
                                  sigma=0.2,
                                  name='ElasticTransformation'),
    ])

    blur = iaa.OneOf([
        iaa.GaussianBlur((1, 5.0), name='GaussianBlur'),
        iaa.AverageBlur(k=(2, 15), name='AverageBlur'),
        iaa.MedianBlur(k=(3, 15), name='MedianBlur'),
        iaa.BilateralBlur(d=(3, 15),
                          sigma_color=(10, 250),
                          sigma_space=(10, 250),
                          name='BilaBlur'),
    ])

    affine = iaa.OneOf([
        iaa.Affine(rotate=(-3, 3)),
        iaa.Affine(translate_percent={
            "x": (0.95, 1.05),
            "y": (0.95, 1.05)
        }),
        iaa.Affine(scale={
            "x": (0.95, 1.05),
            "y": (0.95, 1.05)
        }),
        iaa.Affine(shear=(-2, 2)),
    ])

    factors = iaa.OneOf([
        iaa.Multiply(iap.Choice([0.75, 1.25]), per_channel=False),
        iaa.EdgeDetect(1.0),
    ])

    seq = iaa.Sequential(
        [

            # Size and shape ==================================================
            iaa.Sequential([
                iaa.Fliplr(0.5),
                iaa.OneOf([
                    iaa.Noop(),
                    iaa.Affine(rotate=90),
                    iaa.Affine(rotate=180),
                    iaa.Affine(rotate=270),
                ]),
                half_times(
                    iaa.SomeOf(
                        (1, 2),
                        [
                            iaa.Crop(percent=(0.1, 0.4)),  # Random Crops
                            iaa.PerspectiveTransform(scale=(0.10, 0.175)),
                            iaa.PiecewiseAffine(scale=(0.01, 0.06),
                                                nb_rows=(3, 6),
                                                nb_cols=(3, 6)),
                        ])),
            ]),

            # Texture ==================================================
            sometimes(
                iaa.SomeOf((1, 2), [
                    texture,
                    iaa.Alpha((0.0, 1.0), first=texture, per_channel=False)
                ],
                           random_order=True,
                           name='Texture')),
            half_times(
                iaa.SomeOf((1, 2), [
                    blur,
                    iaa.Alpha((0.0, 1.0), first=blur, per_channel=False),
                    iaa.Alpha(factor=(0.2, 0.8),
                              first=iaa.Sequential([
                                  affine,
                                  blur,
                              ]),
                              per_channel=False),
                ],
                           random_order=True,
                           name='Blur')),
            # Noise ==================================================
            sometimes(
                iaa.SomeOf(
                    (1, 2),
                    [
                        # Just noise
                        iaa.AdditiveGaussianNoise(
                            loc=0,
                            scale=(0.0, 0.15 * 255),
                            per_channel=False,
                            name='AdditiveGaussianNoise'),
                        iaa.SaltAndPepper(
                            0.05, per_channel=False, name='SaltAndPepper'),

                        # Regularization
                        iaa.Dropout(
                            (0.01, 0.1), per_channel=False, name='Dropout'),
                        iaa.CoarseDropout((0.03, 0.15),
                                          size_percent=(0.02, 0.05),
                                          per_channel=False,
                                          name='CoarseDropout'),
                        iaa.Alpha(
                            factor=(0.2, 0.8),
                            first=texture,
                            second=iaa.CoarseDropout(
                                p=0.1, size_percent=(0.02, 0.05)),
                            per_channel=False,
                        ),

                        # Perlin style noise
                        iaa.SimplexNoiseAlpha(first=factors,
                                              per_channel=False,
                                              aggregation_method="max",
                                              sigmoid=False,
                                              upscale_method='cubic',
                                              size_px_max=(2, 12),
                                              name='SimplexNoiseAlpha'),
                        iaa.FrequencyNoiseAlpha(first=factors,
                                                per_channel=False,
                                                aggregation_method="max",
                                                sigmoid=False,
                                                upscale_method='cubic',
                                                size_px_max=(2, 12),
                                                name='FrequencyNoiseAlpha'),
                    ],
                    random_order=True,
                    name='Noise')),
        ],
        random_order=False)

    def activator_masks(images, augmenter, parents, default):
        if 'Unnamed' not in augmenter.name:
            return False
        else:
            return default

    hooks_masks = ia.HooksImages(activator=activator_masks)

    return seq, hooks_masks
Exemplo n.º 19
0
    def __init__(self,
                 data_dir,
                 image_size=128,
                 augmentations=50,
                 crop_percent=(-0.07, 0.1),
                 affine_scale=(0.8, 1.2),
                 hue_range=(0, 20),
                 translate_percent=(-0.1, 0.1),
                 rotation=(-10, 10)):
        """

        Args:
            data_dir:
            image_size:
            jpeg_pics:
            different_crops:
            crop_diff_w:
            crop_diff_h:
            keep_close_aspect_ratio: If None will randomly keep aspect ratio
        """
        self.data_dir = data_dir
        self._data = []
        self.image_size = image_size
        self.num_augmentations = augmentations

        self.seq = iaa.Sequential(
            [
                iaa.OneOf([
                    # Crop images to -7% to 10% of their width/height
                    sometimes(iaa.CropAndPad(percent=crop_percent, ),
                              chance=0.2),

                    # Scale image between 80% to 120% of original size
                    # Translate the picture -10% to 10% on both axes
                    sometimes(iaa.Affine(
                        scale=affine_scale,
                        translate_percent=translate_percent,
                    ),
                              chance=0.4)
                ]),

                # Rotate and shear image
                sometimes(iaa.Affine(
                    rotate=rotation, shear=(-3, 3), mode=ia.ALL),
                          chance=0.2),

                # Changes gamma contrast
                sometimes(iaa.GammaContrast(gamma=(0.8, 1.3)), chance=0.3),

                # Change to HSV and add hue then transfer back to RGB
                sometimes([
                    iaa.ChangeColorspace(from_colorspace="RGB",
                                         to_colorspace="HSV"),
                    iaa.WithChannels(0, iaa.Add(hue_range)),
                    iaa.ChangeColorspace(from_colorspace="HSV",
                                         to_colorspace="RGB")
                ],
                          chance=0.2),

                # Add one type of blur
                sometimes(iaa.OneOf([
                    iaa.GaussianBlur(sigma=(0.1, 2)),
                    iaa.AverageBlur(k=(1, 6)),
                    iaa.MedianBlur(k=(1, 7)),
                    iaa.BilateralBlur(
                        d=(1, 7), sigma_color=250, sigma_space=250)
                ]),
                          chance=0.4),
                sometimes(iaa.Sharpen(alpha=(0, 0.4)), chance=0.4)
            ],
            random_order=True)
Exemplo n.º 20
0
def apply_transform(matrix, image, params):

    # rgb
    # seq describes an object for rgb image augmentation using aleju/imgaug
    seq = iaa.Sequential(
        [
            # blur
            iaa.SomeOf((0, 2), [
                iaa.GaussianBlur((0.0, 2.0)),
                iaa.AverageBlur(k=(3, 7)),
                iaa.MedianBlur(k=(3, 7)),
                iaa.BilateralBlur(d=(1, 7)),
                iaa.MotionBlur(k=(3, 7))
            ]),
            # color
            iaa.SomeOf(
                (0, 2),
                [
                    # iaa.WithColorspace(),
                    iaa.AddToHueAndSaturation((-15, 15)),
                    # iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                    iaa.Grayscale(alpha=(0.0, 0.2))
                ]),
            # brightness
            iaa.OneOf([
                iaa.Sequential([
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.75, 1.25), per_channel=0.5)
                ]),
                iaa.Add((-10, 10), per_channel=0.5),
                iaa.Multiply((0.75, 1.25), per_channel=0.5),
                iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                        first=iaa.Multiply(
                                            (0.75, 1.25), per_channel=0.5),
                                        second=iaa.LinearContrast(
                                            (0.7, 1.3), per_channel=0.5))
            ]),
            # contrast
            iaa.SomeOf((0, 2), [
                iaa.GammaContrast((0.75, 1.25), per_channel=0.5),
                iaa.SigmoidContrast(
                    gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
            ]),
        ],
        random_order=True)
    image = seq.augment_image(image)
    '''
    seq = iaa.Sequential([
                        iaa.Sometimes(0.5, iaa.CoarseDropout(p=0.2, size_percent=(0.1, 0.25))),
                        iaa.Sometimes(0.5, iaa.GaussianBlur(1.2*np.random.rand())),
                        iaa.Sometimes(0.5, iaa.Add((-25, 25), per_channel=0.3)),
                        iaa.Sometimes(0.5, iaa.Invert(0.2, per_channel=True)),
                        iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4), per_channel=0.5)),
                        iaa.Sometimes(0.5, iaa.Multiply((0.6, 1.4))),
                        iaa.Sometimes(0.5, iaa.ContrastNormalization((0.5, 2.2), per_channel=0.3))
                ], random_order=False)
    image = seq.augment_image(image)
    '''
    image = cv2.warpAffine(
        image,
        matrix[:2, :],
        dsize=(image.shape[1], image.shape[0]),
        flags=params.cvInterpolation(),
        borderMode=params.cvBorderMode(),
        borderValue=params.cval,
    )

    return image
Exemplo n.º 21
0
    def next(self):
        if not self.is_init:
            self.reset()
            self.is_init = True
        """Returns the next batch of data."""
        #print('in next', self.cur, self.labelcur)
        self.nbatch += 1
        batch_size = self.batch_size
        c, h, w = self.data_shape
        batch_data = nd.empty((batch_size, c, h, w))
        if self.provide_label is not None:
            batch_label = nd.empty(self.provide_label[0][1])
        i = 0
        try:
            while i < batch_size:
                label, s, bbox, landmark = self.next_sample()
                _data = self.imdecode(s)
                if _data.shape[0] != self.data_shape[1]:
                    _data = mx.image.resize_short(_data, self.data_shape[1])
                if self.rand_mirror:
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        _data = mx.ndarray.flip(data=_data, axis=1)
                if self.blur:
                    aug_blur = iaa.Sequential([
                        iaa.OneOf([
                            iaa.GaussianBlur(sigma=(0.5, 2.5)),
                            iaa.AverageBlur(k=(2, 5)),
                            iaa.MotionBlur(k=(5, 7)),
                            iaa.BilateralBlur(d=(3, 4),
                                              sigma_color=(10, 250),
                                              sigma_space=(10, 250)),
                            iaa.imgcorruptlike.DefocusBlur(severity=1),
                            iaa.imgcorruptlike.GlassBlur(severity=1),
                            iaa.imgcorruptlike.Pixelate(severity=(1, 3)),
                            iaa.Pepper(0.01),
                            iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255),
                                                      per_channel=True),
                            iaa.imgcorruptlike.SpeckleNoise(severity=1),
                            iaa.imgcorruptlike.JpegCompression(severity=(1,
                                                                         4)),
                        ])
                    ])
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        _data = aug_blur(images=_data)

                if self.maxpooling:
                    maxpool_aug = iaa.MaxPooling(2)
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        _data = maxpool_aug(images=_data)

                if self.color_jittering > 0:
                    if self.color_jittering > 1:
                        _rd = random.randint(0, 1)
                        if _rd == 1:
                            _data = self.compress_aug(_data)
                    #print('do color aug')
                    _data = _data.astype('float32', copy=False)
                    #print(_data.__class__)
                    _data = self.color_aug(_data, 0.125)
                if self.nd_mean is not None:
                    _data = _data.astype('float32', copy=False)
                    _data -= self.nd_mean
                    _data *= 0.0078125
                if self.cutoff > 0:
                    _rd = random.randint(0, 1)
                    if _rd == 1:
                        #print('do cutoff aug', self.cutoff)
                        centerh = random.randint(0, _data.shape[0] - 1)
                        centerw = random.randint(0, _data.shape[1] - 1)
                        half = self.cutoff // 2
                        starth = max(0, centerh - half)
                        endh = min(_data.shape[0], centerh + half)
                        startw = max(0, centerw - half)
                        endw = min(_data.shape[1], centerw + half)
                        #print(starth, endh, startw, endw, _data.shape)
                        _data[starth:endh, startw:endw, :] = 128
                data = [_data]
                try:
                    self.check_valid_image(data)
                except RuntimeError as e:
                    logging.debug('Invalid image, skipping:  %s', str(e))
                    continue
                #print('aa',data[0].shape)
                #data = self.augmentation_transform(data)
                #print('bb',data[0].shape)
                for datum in data:
                    assert i < batch_size, 'Batch size must be multiples of augmenter output length'
                    #print(datum.shape)
                    batch_data[i][:] = self.postprocess_data(datum)
                    batch_label[i][:] = label
                    i += 1
        except StopIteration:
            if i < batch_size:
                raise StopIteration

        return io.DataBatch([batch_data], [batch_label], batch_size - i)
Exemplo n.º 22
0
def main():
    parser = argparse.ArgumentParser(description="Check augmenters visually.")
    parser.add_argument(
        "--only",
        default=None,
        help=
        "If this is set, then only the results of an augmenter with this name will be shown. "
        "Optionally, comma-separated list.",
        required=False)
    args = parser.parse_args()

    images = [
        ia.quokka_square(size=(128, 128)),
        ia.imresize_single_image(data.astronaut(), (128, 128))
    ]

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=50, y=40),
            ia.Keypoint(x=70, y=38),
            ia.Keypoint(x=62, y=52)
        ],
                            shape=images[0].shape),
        ia.KeypointsOnImage([
            ia.Keypoint(x=55, y=32),
            ia.Keypoint(x=42, y=95),
            ia.Keypoint(x=75, y=89)
        ],
                            shape=images[1].shape)
    ]

    bounding_boxes = [
        ia.BoundingBoxesOnImage([
            ia.BoundingBox(x1=10, y1=10, x2=20, y2=20),
            ia.BoundingBox(x1=40, y1=50, x2=70, y2=60)
        ],
                                shape=images[0].shape),
        ia.BoundingBoxesOnImage([
            ia.BoundingBox(x1=10, y1=10, x2=20, y2=20),
            ia.BoundingBox(x1=40, y1=50, x2=70, y2=60)
        ],
                                shape=images[1].shape)
    ]

    augmenters = [
        iaa.Sequential([
            iaa.CoarseDropout(p=0.5, size_percent=0.05),
            iaa.AdditiveGaussianNoise(scale=0.1 * 255),
            iaa.Crop(percent=0.1)
        ],
                       name="Sequential"),
        iaa.SomeOf(2,
                   children=[
                       iaa.CoarseDropout(p=0.5, size_percent=0.05),
                       iaa.AdditiveGaussianNoise(scale=0.1 * 255),
                       iaa.Crop(percent=0.1)
                   ],
                   name="SomeOf"),
        iaa.OneOf(children=[
            iaa.CoarseDropout(p=0.5, size_percent=0.05),
            iaa.AdditiveGaussianNoise(scale=0.1 * 255),
            iaa.Crop(percent=0.1)
        ],
                  name="OneOf"),
        iaa.Sometimes(0.5,
                      iaa.AdditiveGaussianNoise(scale=0.1 * 255),
                      name="Sometimes"),
        iaa.WithColorspace("HSV",
                           children=[iaa.Add(20)],
                           name="WithColorspace"),
        iaa.WithChannels([0], children=[iaa.Add(20)], name="WithChannels"),
        iaa.AddToHueAndSaturation((-20, 20),
                                  per_channel=True,
                                  name="AddToHueAndSaturation"),
        iaa.Noop(name="Noop"),
        iaa.Resize({
            "width": 64,
            "height": 64
        }, name="Resize"),
        iaa.CropAndPad(px=(-8, 8), name="CropAndPad-px"),
        iaa.Pad(px=(0, 8), name="Pad-px"),
        iaa.Crop(px=(0, 8), name="Crop-px"),
        iaa.Crop(percent=(0, 0.1), name="Crop-percent"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"),
        iaa.Grayscale(0.5, name="Grayscale0.5"),
        iaa.Grayscale(1.0, name="Grayscale1.0"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AverageBlur(k=(3, 11), name="AverageBlur"),
        iaa.MedianBlur(k=(3, 11), name="MedianBlur"),
        iaa.BilateralBlur(d=10, name="BilateralBlur"),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0),
                               direction=(0, 1.0),
                               name="DirectedEdgeDetect"),
        iaa.Add((-50, 50), name="Add"),
        iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"),
        iaa.AddElementwise((-50, 50), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1 * 255),
                                  name="AdditiveGaussianNoise"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"),
        iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.CoarseDropout(p=0.05,
                          size_percent=(0.05, 0.5),
                          name="CoarseDropout"),
        iaa.Invert(p=0.5, name="Invert"),
        iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0),
                                  name="ContrastNormalization"),
        iaa.SaltAndPepper(p=0.05, name="SaltAndPepper"),
        iaa.Salt(p=0.05, name="Salt"),
        iaa.Pepper(p=0.05, name="Pepper"),
        iaa.CoarseSaltAndPepper(p=0.05,
                                size_percent=(0.01, 0.1),
                                name="CoarseSaltAndPepper"),
        iaa.CoarseSalt(p=0.05, size_percent=(0.01, 0.1), name="CoarseSalt"),
        iaa.CoarsePepper(p=0.05, size_percent=(0.01, 0.1),
                         name="CoarsePepper"),
        iaa.Affine(scale={
            "x": (0.8, 1.2),
            "y": (0.8, 1.2)
        },
                   translate_px={
                       "x": (-16, 16),
                       "y": (-16, 16)
                   },
                   rotate=(-45, 45),
                   shear=(-16, 16),
                   order=ia.ALL,
                   cval=(0, 255),
                   mode=ia.ALL,
                   name="Affine"),
        iaa.PiecewiseAffine(scale=0.03,
                            nb_rows=(2, 6),
                            nb_cols=(2, 6),
                            name="PiecewiseAffine"),
        iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0),
                                  sigma=1.0,
                                  name="ElasticTransformation"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Add(100),
                  second=iaa.Dropout(0.5),
                  per_channel=False,
                  name="Alpha"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Add(100),
                  second=iaa.Dropout(0.5),
                  per_channel=True,
                  name="AlphaPerChannel"),
        iaa.Alpha(factor=(0.0, 1.0),
                  first=iaa.Affine(rotate=(-45, 45)),
                  per_channel=True,
                  name="AlphaAffine"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Add(50),
                             second=iaa.ContrastNormalization(2.0),
                             per_channel=False,
                             name="AlphaElementwise"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Add(50),
                             second=iaa.ContrastNormalization(2.0),
                             per_channel=True,
                             name="AlphaElementwisePerChannel"),
        iaa.AlphaElementwise(factor=(0.0, 1.0),
                             first=iaa.Affine(rotate=(-45, 45)),
                             per_channel=True,
                             name="AlphaElementwiseAffine"),
        iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0),
                              per_channel=False,
                              name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0),
                                per_channel=False,
                                name="FrequencyNoiseAlpha")
    ]

    augmenters.append(
        iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters],
                       name="Sequential"))
    augmenters.append(
        iaa.Sometimes(0.5, [aug.copy() for aug in augmenters],
                      name="Sometimes"))

    for augmenter in augmenters:
        if args.only is None or augmenter.name in [
                v.strip() for v in args.only.split(",")
        ]:
            print("Augmenter: %s" % (augmenter.name, ))
            grid = []
            for image, kps, bbs in zip(images, keypoints, bounding_boxes):
                aug_det = augmenter.to_deterministic()
                imgs_aug = aug_det.augment_images(
                    np.tile(image[np.newaxis, ...], (16, 1, 1, 1)))
                kps_aug = aug_det.augment_keypoints([kps] * 16)
                bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16)
                imgs_aug_drawn = [
                    kps_aug_one.draw_on_image(img_aug)
                    for img_aug, kps_aug_one in zip(imgs_aug, kps_aug)
                ]
                imgs_aug_drawn = [
                    bbs_aug_one.draw_on_image(img_aug)
                    for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug)
                ]
                grid.append(np.hstack(imgs_aug_drawn))
            ia.imshow(np.vstack(grid))
Exemplo n.º 23
0
    def __init__(self,
                 dataset_type,
                 dataset_path,
                 real_path,
                 mesh_path,
                 mesh_info,
                 object_id,
                 batch_size,
                 img_res=(224, 224),
                 is_testing=False):
        self.data_type = dataset_type
        self.img_res = img_res
        self.dataset_path = dataset_path
        self.real_path = [
            os.path.join(real_path, x) for x in os.listdir(real_path)
        ]
        self.batch_size = batch_size
        self.is_testing = is_testing
        self.ply_path = mesh_path
        self.obj_id = int(object_id)

        # annotate
        self.train_info = os.path.join(self.dataset_path, 'annotations',
                                       'instances_' + 'train' + '.json')
        self.val_info = os.path.join(self.dataset_path, 'annotations',
                                     'instances_' + 'val' + '.json')
        # self.mesh_info = os.path.join(self.dataset_path, 'annotations', 'models_info' + '.yml')
        self.mesh_info = mesh_info
        with open(self.train_info, 'r') as js:
            data = json.load(js)
        image_ann = data["images"]
        anno_ann = data["annotations"]
        self.image_ids = []
        self.Anns = []

        # init renderer
        # < 11 ms;
        self.ren = bop_renderer.Renderer()
        self.ren.init(640, 480)
        self.ren.add_object(self.obj_id, self.ply_path)

        stream = open(self.mesh_info, 'r')
        for key, value in yaml.load(stream).items():
            # for key, value in yaml.load(open(self.mesh_info)).items():
            if int(key) == self.obj_id + 1:
                self.model_dia = value['diameter']

        for ann in anno_ann:
            y_mean = (ann['bbox'][0] + ann['bbox'][2] * 0.5)
            x_mean = (ann['bbox'][1] + ann['bbox'][3] * 0.5)
            max_side = np.max(ann['bbox'][2:])
            x_min = int(x_mean - max_side * 0.75)
            x_max = int(x_mean + max_side * 0.75)
            y_min = int(y_mean - max_side * 0.75)
            y_max = int(y_mean + max_side * 0.75)
            if ann['category_id'] != 2 or ann[
                    'feature_visibility'] < 0.5 or x_min < 0 or x_max > 639 or y_min < 0 or y_max > 479:
                continue
            else:
                self.Anns.append(ann)
                # for img_info in image_ann:
                # print(img_info)
                #    if img_info['id'] == ann['id']:
                #        self.image_ids.append(img_info['file_name'])
                #        print(img_info['file_name'])
                template_name = '00000000000'
                id = str(ann['image_id'])
                # print(ann['id'])
                name = template_name[:-len(id)] + id + '.png'
                # print(name)
                self.image_ids.append(name)

        self.fx = image_ann[0]["fx"]
        self.fy = image_ann[0]["fy"]
        self.cx = image_ann[0]["cx"]
        self.cy = image_ann[0]["cy"]

        #self.image_idxs = range(len(self.image_ids))
        c = list(zip(self.Anns, self.image_ids))  #, self.image_idxs))
        np.random.shuffle(c)
        self.Anns, self.image_ids = zip(*c)

        self.img_seq = iaa.Sequential(
            [
                # blur
                iaa.SomeOf((0, 2), [
                    iaa.GaussianBlur((0.0, 2.0)),
                    iaa.AverageBlur(k=(3, 7)),
                    iaa.MedianBlur(k=(3, 7)),
                    iaa.BilateralBlur(d=(1, 7)),
                    iaa.MotionBlur(k=(3, 7))
                ]),
                # color
                iaa.SomeOf(
                    (0, 2),
                    [
                        # iaa.WithColorspace(),
                        iaa.AddToHueAndSaturation((-15, 15)),
                        # iaa.ChangeColorspace(to_colorspace[], alpha=0.5),
                        iaa.Grayscale(alpha=(0.0, 0.2))
                    ]),
                # brightness
                iaa.OneOf([
                    iaa.Sequential([
                        iaa.Add((-10, 10), per_channel=0.5),
                        iaa.Multiply((0.75, 1.25), per_channel=0.5)
                    ]),
                    iaa.Add((-10, 10), per_channel=0.5),
                    iaa.Multiply((0.75, 1.25), per_channel=0.5),
                    iaa.FrequencyNoiseAlpha(exponent=(-4, 0),
                                            first=iaa.Multiply(
                                                (0.75, 1.25), per_channel=0.5),
                                            second=iaa.LinearContrast(
                                                (0.7, 1.3), per_channel=0.5))
                ]),
                # contrast
                iaa.SomeOf((0, 2), [
                    iaa.GammaContrast((0.75, 1.25), per_channel=0.5),
                    iaa.SigmoidContrast(
                        gain=(0, 10), cutoff=(0.25, 0.75), per_channel=0.5),
                    iaa.LogContrast(gain=(0.75, 1), per_channel=0.5),
                    iaa.LinearContrast(alpha=(0.7, 1.3), per_channel=0.5)
                ]),
            ],
            random_order=True)

        self.n_batches = int(np.floor(len(self.image_ids) / self.batch_size))
        self.on_epoch_end()
Exemplo n.º 24
0
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

    keypoints = [
        ia.Keypoint(x=34, y=15),
        ia.Keypoint(x=85, y=13),
        ia.Keypoint(x=63, y=73)
    ]  # left ear, right ear, mouth
    keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)]

    print("[draw_per_augmenter_images] Initializing...")
    rows_augmenters = [
        (0, "Noop", [("", iaa.Noop()) for _ in sm.xrange(5)]),
        (0, "Crop\n(top, right,\nbottom, left)", [
            (str(vals), iaa.Crop(px=vals))
            for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16,
                                                      4), (8, 0, 0,
                                                           32), (32, 64, 0, 0)]
        ]),
        (0, "Pad\n(top, right,\nbottom, left)", [
            (str(vals), iaa.Pad(px=vals))
            for vals in [(2, 0, 0, 0), (0, 8, 8, 0), (4, 0, 16,
                                                      4), (8, 0, 0,
                                                           32), (32, 64, 0, 0)]
        ]), (0, "Fliplr", [(str(p), iaa.Fliplr(p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Flipud", [(str(p), iaa.Flipud(p)) for p in [0, 0, 1, 1, 1]]),
        (0, "Superpixels\np_replace=1",
         [("n_segments=%d" % (n_segments, ),
           iaa.Superpixels(p_replace=1.0, n_segments=n_segments))
          for n_segments in [25, 50, 75, 100, 125]]),
        (0, "Superpixels\nn_segments=100",
         [("p_replace=%.2f" % (p_replace, ),
           iaa.Superpixels(p_replace=p_replace, n_segments=100))
          for p_replace in [0, 0.25, 0.5, 0.75, 1.0]]),
        (0, "Invert", [("p=%d" % (p, ), iaa.Invert(p=p))
                       for p in [0, 0, 1, 1, 1]]),
        (0, "Invert\n(per_channel)", [("p=%.2f" % (p, ),
                                       iaa.Invert(p=p, per_channel=True))
                                      for p in [0.5, 0.5, 0.5, 0.5, 0.5]]),
        (0, "Add", [("value=%d" % (val, ), iaa.Add(val))
                    for val in [-45, -25, 0, 25, 45]]),
        (0, "Add\n(per channel)", [
            ("value=(%d, %d)" % (
                vals[0],
                vals[1],
            ), iaa.Add(vals, per_channel=True))
            for vals in [(-55, -35), (-35, -15), (-10, 10), (15, 35), (35, 55)]
        ]),
        (0, "AddToHueAndSaturation", [("value=%d" % (val, ),
                                       iaa.AddToHueAndSaturation(val))
                                      for val in [-45, -25, 0, 25, 45]]),
        (0, "Multiply", [("value=%.2f" % (val, ), iaa.Multiply(val))
                         for val in [0.25, 0.5, 1.0, 1.25, 1.5]]),
        (1, "Multiply\n(per channel)",
         [("value=(%.2f, %.2f)" % (
             vals[0],
             vals[1],
         ), iaa.Multiply(vals, per_channel=True))
          for vals in [(0.15, 0.35), (0.4, 0.6), (0.9, 1.1), (1.15,
                                                              1.35), (1.4,
                                                                      1.6)]]),
        (0, "GaussianBlur", [("sigma=%.2f" % (sigma, ),
                              iaa.GaussianBlur(sigma=sigma))
                             for sigma in [0.25, 0.50, 1.0, 2.0, 4.0]]),
        (0, "AverageBlur", [("k=%d" % (k, ), iaa.AverageBlur(k=k))
                            for k in [1, 3, 5, 7, 9]]),
        (0, "MedianBlur", [("k=%d" % (k, ), iaa.MedianBlur(k=k))
                           for k in [1, 3, 5, 7, 9]]),
        (0, "BilateralBlur\nsigma_color=250,\nsigma_space=250",
         [("d=%d" % (d, ),
           iaa.BilateralBlur(d=d, sigma_color=250, sigma_space=250))
          for d in [1, 3, 5, 7, 9]]),
        (0, "Sharpen\n(alpha=1)", [("lightness=%.2f" % (lightness, ),
                                    iaa.Sharpen(alpha=1, lightness=lightness))
                                   for lightness in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "Emboss\n(alpha=1)", [("strength=%.2f" % (strength, ),
                                   iaa.Emboss(alpha=1, strength=strength))
                                  for strength in [0, 0.5, 1.0, 1.5, 2.0]]),
        (0, "EdgeDetect", [("alpha=%.2f" % (alpha, ),
                            iaa.EdgeDetect(alpha=alpha))
                           for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (0, "DirectedEdgeDetect\n(alpha=1)",
         [("direction=%.2f" % (direction, ),
           iaa.DirectedEdgeDetect(alpha=1, direction=direction))
          for direction in [
              0.0, 1 * (360 / 5) / 360, 2 * (360 / 5) / 360, 3 * (360 / 5) /
              360, 4 * (360 / 5) / 360
          ]]),
        (0, "AdditiveGaussianNoise",
         [("scale=%.2f*255" % (scale, ),
           iaa.AdditiveGaussianNoise(scale=scale * 255))
          for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "AdditiveGaussianNoise\n(per channel)",
         [("scale=%.2f*255" % (scale, ),
           iaa.AdditiveGaussianNoise(scale=scale * 255, per_channel=True))
          for scale in [0.025, 0.05, 0.1, 0.2, 0.3]]),
        (0, "Dropout", [("p=%.2f" % (p, ), iaa.Dropout(p=p))
                        for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "Dropout\n(per channel)", [("p=%.2f" % (p, ),
                                        iaa.Dropout(p=p, per_channel=True))
                                       for p in [0.025, 0.05, 0.1, 0.2, 0.4]]),
        (0, "CoarseDropout\n(p=0.2)",
         [("size_percent=%.2f" % (size_percent, ),
           iaa.CoarseDropout(p=0.2, size_percent=size_percent, min_size=2))
          for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "CoarseDropout\n(p=0.2, per channel)",
         [("size_percent=%.2f" % (size_percent, ),
           iaa.CoarseDropout(p=0.2,
                             size_percent=size_percent,
                             per_channel=True,
                             min_size=2))
          for size_percent in [0.3, 0.2, 0.1, 0.05, 0.02]]),
        (0, "ContrastNormalization",
         [("alpha=%.1f" % (alpha, ), iaa.ContrastNormalization(alpha=alpha))
          for alpha in [0.5, 0.75, 1.0, 1.25, 1.50]]),
        (0, "ContrastNormalization\n(per channel)",
         [("alpha=(%.2f, %.2f)" % (
             alphas[0],
             alphas[1],
         ), iaa.ContrastNormalization(alpha=alphas, per_channel=True))
          for alphas in [(0.4, 0.6), (0.65,
                                      0.85), (0.9, 1.1), (1.15,
                                                          1.35), (1.4, 1.6)]]),
        (0, "Grayscale", [("alpha=%.1f" % (alpha, ),
                           iaa.Grayscale(alpha=alpha))
                          for alpha in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (6, "PerspectiveTransform",
         [("scale=%.3f" % (scale, ), iaa.PerspectiveTransform(scale=scale))
          for scale in [0.025, 0.05, 0.075, 0.10, 0.125]]),
        (0, "PiecewiseAffine",
         [("scale=%.3f" % (scale, ), iaa.PiecewiseAffine(scale=scale))
          for scale in [0.015, 0.03, 0.045, 0.06, 0.075]]),
        (0, "Affine: Scale", [("%.1fx" % (scale, ), iaa.Affine(scale=scale))
                              for scale in [0.1, 0.5, 1.0, 1.5, 1.9]]),
        (0, "Affine: Translate",
         [("x=%d y=%d" % (x, y), iaa.Affine(translate_px={
             "x": x,
             "y": y
         }))
          for x, y in [(-32, -16), (-16, -32), (-16, -8), (16, 8), (16, 32)]]),
        (0, "Affine: Rotate", [("%d deg" % (rotate, ),
                                iaa.Affine(rotate=rotate))
                               for rotate in [-90, -45, 0, 45, 90]]),
        (0, "Affine: Shear", [("%d deg" % (shear, ), iaa.Affine(shear=shear))
                              for shear in [-45, -25, 0, 25, 45]]),
        (0, "Affine: Modes",
         [(mode, iaa.Affine(translate_px=-32, mode=mode))
          for mode in ["constant", "edge", "symmetric", "reflect", "wrap"]]),
        (0, "Affine: cval", [("%d" % (int(cval * 255), ),
                              iaa.Affine(translate_px=-32,
                                         cval=int(cval * 255),
                                         mode="constant"))
                             for cval in [0.0, 0.25, 0.5, 0.75, 1.0]]),
        (2, "Affine: all", [("",
                             iaa.Affine(scale={
                                 "x": (0.5, 1.5),
                                 "y": (0.5, 1.5)
                             },
                                        translate_px={
                                            "x": (-32, 32),
                                            "y": (-32, 32)
                                        },
                                        rotate=(-45, 45),
                                        shear=(-32, 32),
                                        mode=ia.ALL,
                                        cval=(0.0, 1.0)))
                            for _ in sm.xrange(5)]),
        (1, "ElasticTransformation\n(sigma=0.2)",
         [("alpha=%.1f" % (alpha, ),
           iaa.ElasticTransformation(alpha=alpha, sigma=0.2))
          for alpha in [0.1, 0.5, 1.0, 3.0, 9.0]])
    ]

    print("[draw_per_augmenter_images] Augmenting...")
    rows = []
    for (row_seed, row_name, augmenters) in rows_augmenters:
        ia.seed(row_seed)
        #for img_title, augmenter in augmenters:
        #    #aug.reseed(1000)
        #    pass

        row_images = []
        row_keypoints = []
        row_titles = []
        for img_title, augmenter in augmenters:
            aug_det = augmenter.to_deterministic()
            row_images.append(aug_det.augment_image(image))
            row_keypoints.append(aug_det.augment_keypoints(keypoints)[0])
            row_titles.append(img_title)
        rows.append((row_name, row_images, row_keypoints, row_titles))

    # matplotlib drawin routine
    """
    print("[draw_per_augmenter_images] Plotting...")
    width = 8
    height = int(1.5 * len(rows_augmenters))
    fig = plt.figure(figsize=(width, height))
    grid_rows = len(rows)
    grid_cols = 1 + 5
    gs = gridspec.GridSpec(grid_rows, grid_cols, width_ratios=[2, 1, 1, 1, 1, 1])
    axes = []
    for i in sm.xrange(grid_rows):
        axes.append([plt.subplot(gs[i, col_idx]) for col_idx in sm.xrange(grid_cols)])
    fig.tight_layout()
    #fig.subplots_adjust(bottom=0.2 / grid_rows, hspace=0.22)
    #fig.subplots_adjust(wspace=0.005, hspace=0.425, bottom=0.02)
    fig.subplots_adjust(wspace=0.005, hspace=0.005, bottom=0.02)

    for row_idx, (row_name, row_images, row_keypoints, row_titles) in enumerate(rows):
        axes_row = axes[row_idx]

        for col_idx in sm.xrange(grid_cols):
            ax = axes_row[col_idx]

            ax.cla()
            ax.axis("off")
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)

            if col_idx == 0:
                ax.text(0, 0.5, row_name, color="black")
            else:
                cell_image = row_images[col_idx-1]
                cell_keypoints = row_keypoints[col_idx-1]
                cell_image_kp = cell_keypoints.draw_on_image(cell_image, size=5)
                ax.imshow(cell_image_kp)
                x = 0
                y = 145
                #ax.text(x, y, row_titles[col_idx-1], color="black", backgroundcolor="white", fontsize=6)
                ax.text(x, y, row_titles[col_idx-1], color="black", fontsize=7)


    fig.savefig("examples.jpg", bbox_inches="tight")
    #plt.show()
    """

    # simpler and faster drawing routine
    """
    output_image = ExamplesImage(128, 128, 128+64, 32)
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
    misc.imsave("examples.jpg", output_image.draw())
    """

    # routine to draw many single files
    seen = defaultdict(lambda: 0)
    markups = []
    for (row_name, row_images, row_keypoints, row_titles) in rows:
        output_image = ExamplesImage(128, 128, 128 + 64, 32)
        row_images_kps = []
        for image, keypoints in zip(row_images, row_keypoints):
            row_images_kps.append(keypoints.draw_on_image(image, size=5))
        output_image.add_row(row_name, row_images_kps, row_titles)
        if "\n" in row_name:
            row_name_clean = row_name[0:row_name.find("\n") + 1]
        else:
            row_name_clean = row_name
        row_name_clean = re.sub(r"[^a-z0-9]+", "_", row_name_clean.lower())
        row_name_clean = row_name_clean.strip("_")
        if seen[row_name_clean] > 0:
            row_name_clean = "%s_%d" % (row_name_clean,
                                        seen[row_name_clean] + 1)
        fp = os.path.join(IMAGES_DIR, "examples_%s.jpg" % (row_name_clean, ))
        #misc.imsave(fp, output_image.draw())
        save(fp, output_image.draw())
        seen[row_name_clean] += 1

        markup_descr = row_name.replace('"', '') \
                               .replace("\n", " ") \
                               .replace("(", "") \
                               .replace(")", "")
        markup = '![%s](%s?raw=true "%s")' % (markup_descr, fp, markup_descr)
        markups.append(markup)

    for markup in markups:
        print(markup)
Exemplo n.º 25
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.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
import os
import json
import random
import cv2 as cv
import numpy as np
from imgaug import augmenters as iaa

aug = [
    iaa.LinearContrast(alpha=2),
    iaa.SigmoidContrast(gain=10),
    iaa.GammaContrast(gamma=2),
    iaa.CLAHE(clip_limit=(1, 5)),
    iaa.Grayscale(alpha=1.0),
    iaa.AddToHueAndSaturation((-20, 20), per_channel=True),
    iaa.BilateralBlur(d=6),
    iaa.MotionBlur(k=7),
    iaa.MedianBlur(k=3),
    iaa.AverageBlur(k=3),
    iaa.AdditiveGaussianNoise(loc=0.8, scale=(0.01, 0.08 * 255)),
    iaa.ContrastNormalization((0.3, 1.5)),
    iaa.Sharpen(alpha=0, lightness=1)
]


class Dataset(object):
    def __init__(self, args):
        self.args = args
        self.char = json.load(open(self.args.char, mode='r'))

    def get_file(self, data_path):
    def __init__(self,  rgb_mean, randomImg, insize):
        sometimes = lambda aug: iaa.Sometimes(0.7, aug)
        self.rand_img_dir = randomImg
        self.rgb_mean = rgb_mean
        self.inp_dim = insize
        #
        self.randomImgList = glob.glob( randomImg + '*.jpg')

        self.aug = iaa.Sequential([
        sometimes(iaa.Affine(
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
            translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)}, # translate by -20 to +20 percent (per axis)
            rotate=(-25, 25), # rotate by -45 to +45 degrees
            shear=(-6, 6), # shear by -16 to +16 degrees
            order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
            cval=(0, 255), # if mode is constant, use a cval between 0 and 255
            mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
        )),

        iaa.OneOf([
            iaa.Fliplr(0.5),

            iaa.GaussianBlur(
                sigma=iap.Uniform(0.0, 1.0)
            ),

            iaa.BlendAlphaSimplexNoise(
                foreground=iaa.BlendAlphaSimplexNoise(
                    foreground=iaa.EdgeDetect(1.0),
                    background=iaa.LinearContrast((0.1, .8)),
                    per_channel=True
                ),
                background=iaa.BlendAlphaFrequencyNoise(
                    exponent=(-.5, -.1),
                    foreground=iaa.Affine(
                        rotate=(-10, 10),
                        translate_px={"x": (-1, 1), "y": (-1, 1)}
                    ),
                    # background=iaa.AddToHueAndSaturation((-4, 4)),
                    # per_channel=True
                ),
                per_channel=True,
                aggregation_method="max",
                sigmoid=False
            ),

        iaa.BlendAlpha(
            factor=(0.2, 0.8),
            foreground=iaa.Sharpen(1.0, lightness=2),
            background=iaa.CoarseDropout(p=0.1, size_px=8)
        ),

        iaa.BlendAlpha(
            factor=(0.2, 0.8),
            foreground=iaa.Affine(rotate=(-5, 5)),
            per_channel=True
        ),
        iaa.MotionBlur(k=15, angle=[-5, 5]),
        iaa.BlendAlphaCheckerboard(nb_rows=2, nb_cols=(1, 4),
                                       foreground=iaa.AddToHue((-10, 10))),
        iaa.BlendAlphaElementwise((0, 1.0), iaa.AddToHue(10)),
        iaa.BilateralBlur(
                d=(3, 10), sigma_color=(1, 5), sigma_space=(1, 5)),
        iaa.AdditiveGaussianNoise(scale=0.02 * 255),
        iaa.AddElementwise((-5, 5), per_channel=0.5),
        iaa.AdditiveLaplaceNoise(scale=0.01 * 255),
        iaa.AdditivePoissonNoise(20),
        iaa.Cutout(fill_mode="gaussian", fill_per_channel=True),
        iaa.CoarseDropout(0.02, size_percent=0.1),
        iaa.SaltAndPepper(0.1, per_channel=True),
        iaa.JpegCompression(compression=(70, 99)),
        iaa.ImpulseNoise(0.02),
        iaa.Dropout(p=(0, 0.04)),
        iaa.Sharpen(alpha=0.1),
        ]) # oneof

        ])
    def __init__(self,
                 base_data_path,
                 train,
                 transform,
                 id_name_path,
                 device,
                 little_train=False,
                 read_mode='jpeg4py',
                 input_size=224,
                 C=2048,
                 test_mode=False):
        print('data init')

        self.train = train
        self.base_data_path = base_data_path
        self.transform = transform
        self.fnames = []
        self.resize = input_size
        self.little_train = little_train
        self.id_name_path = id_name_path
        self.C = C
        self.read_mode = read_mode
        self.device = device
        self._test = test_mode

        self.fnames = self.get_data_list(base_data_path)
        self.num_samples = len(self.fnames)
        self.get_id_map()
        self.cls_path_map = self.get_cls_pathlist_map()
        self.img_augsometimes = lambda aug: iaa.Sometimes(0.5, aug)
        self.augmentation = iaa.Sequential(
            [
                # augment without change bboxes
                self.img_augsometimes(
                    iaa.SomeOf(
                        (1, 4),
                        [
                            iaa.Dropout([0.05, 0.2
                                         ]),  # drop 5% or 20% of all pixels
                            iaa.Sharpen((0.1, .8)),  # sharpen the image
                            # iaa.GaussianBlur(sigma=(2., 3.5)),
                            iaa.OneOf([
                                iaa.GaussianBlur(sigma=(2., 3.5)),
                                iaa.AverageBlur(k=(2, 5)),
                                iaa.BilateralBlur(d=(7, 12),
                                                  sigma_color=(10, 250),
                                                  sigma_space=(10, 250)),
                                iaa.MedianBlur(k=(3, 7)),
                            ]),
                            iaa.AddElementwise((-50, 50)),
                            iaa.AdditiveGaussianNoise(scale=(0, 0.1 * 255)),
                            iaa.JpegCompression(compression=(80, 95)),
                            iaa.Multiply((0.5, 1.5)),
                            iaa.MultiplyElementwise((0.5, 1.5)),
                            iaa.ReplaceElementwise(0.05, [0, 255]),
                            # iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                            #                 children=iaa.WithChannels(2, iaa.Add((-10, 50)))),
                            iaa.OneOf([
                                iaa.WithColorspace(to_colorspace="HSV",
                                                   from_colorspace="RGB",
                                                   children=iaa.WithChannels(
                                                       1, iaa.Add((-10, 50)))),
                                iaa.WithColorspace(to_colorspace="HSV",
                                                   from_colorspace="RGB",
                                                   children=iaa.WithChannels(
                                                       2, iaa.Add((-10, 50)))),
                            ]),
                            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))
                        ],
                        random_order=True)),
                iaa.Fliplr(.5),
                iaa.Flipud(.25),
            ],
            random_order=True)
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