Example #1
0
def example_unusual_distributions():
    print("Example: Unusual Distributions")
    from imgaug import augmenters as iaa
    from imgaug import parameters as iap
    images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

    # Blur by a value sigma which is sampled from a uniform distribution
    # of range 0.1 <= x < 3.0.
    # The convenience shortcut for this is: iaa.GaussianBlur((0.1, 3.0))
    blurer = iaa.GaussianBlur(iap.Uniform(0.1, 3.0))
    images_aug = blurer.augment_images(images)

    # Blur by a value sigma which is sampled from a normal distribution N(1.0, 0.1),
    # i.e. sample a value that is usually around 1.0.
    # Clip the resulting value so that it never gets below 0.1 or above 3.0.
    blurer = iaa.GaussianBlur(iap.Clip(iap.Normal(1.0, 0.1), 0.1, 3.0))
    images_aug = blurer.augment_images(images)

    # Same again, but this time the mean of the normal distribution is not constant,
    # but comes itself from a uniform distribution between 0.5 and 1.5.
    blurer = iaa.GaussianBlur(
        iap.Clip(iap.Normal(iap.Uniform(0.5, 1.5), 0.1), 0.1, 3.0))
    images_aug = blurer.augment_images(images)

    # Use for sigma one of exactly three allowed values: 0.5, 1.0 or 1.5.
    blurer = iaa.GaussianBlur(iap.Choice([0.5, 1.0, 1.5]))
    images_aug = blurer.augment_images(images)

    # Sample sigma from a discrete uniform distribution of range 1 <= sigma <= 5,
    # i.e. sigma will have any of the following values: 1, 2, 3, 4, 5.
    blurer = iaa.GaussianBlur(iap.DiscreteUniform(1, 5))
    images_aug = blurer.augment_images(images)
def get_noise_augmenters(augmenter_level):
    """
    Gets an augmenter object of a given level
    """
    # 0 heavy , 1 medium,2 simple
    if augmenter_level == 0:
        ####################################################### heavy augmentation #########################################################################
        return [
            iaa.Sometimes(
                0.9,
                iaa.Crop(percent=((iap.Clip(iap.Normal(0, .5), 0, .6), ) *
                                  4)  # random crops top,right,bottom,left
                         )),
            # some noise
            iaa.Sometimes(0.9, [
                iaa.GaussianBlur(sigma=(0, 0.3)),
                iaa.Sharpen(alpha=(0.0, .15), lightness=(0.5, 1.5)),
                iaa.Emboss(alpha=(0.0, 1.0), strength=(0.1, 0.2))
            ]),
            iaa.Sometimes(0.9, iaa.Add((-12, 12), per_channel=1))
        ]  # rgb jittering
    elif augmenter_level == 1:
        ####################################################### medium  augmentation #######################################################################
        return [
            iaa.Sometimes(
                0.9,
                iaa.Crop(
                    percent=((0.0, 0.15), (0.0, 0.15), (0.0, 0.15),
                             (0.0,
                              0.15)))),  # random crops top,right,bottom,left
            # some noise
            iaa.Sometimes(0.5, [
                iaa.GaussianBlur(sigma=(0, 0.25)),
                iaa.Sharpen(alpha=(0.0, .1), lightness=(0.5, 1.25)),
                iaa.Emboss(alpha=(0.0, 1.0), strength=(0.05, 0.1))
            ]),
            iaa.Sometimes(.7, iaa.Add((-10, 10), per_channel=1))
        ]  # rgb jittering
    elif augmenter_level == 2:
        ######################################################## simple augmentation #######################################################################
        return [
            iaa.Sometimes(
                0.6,
                iaa.Crop(
                    percent=((0.0, 0.1), (0.0, 0.1), (0.0, 0.1),
                             (0.0,
                              0.1)))),  # random crops top,right,bottom,left
            # some noise
            iaa.Sometimes(0.35, [
                iaa.GaussianBlur(sigma=(0, 0.17)),
                iaa.Sharpen(alpha=(0.0, .07), lightness=(0.35, 1)),
                iaa.Emboss(alpha=(0.0, .7), strength=(0.1, 0.7))
            ]),
            iaa.Sometimes(.45, iaa.Add((-7, 7), per_channel=1))
        ]  # rgb jittering
Example #3
0
def example_probability_distributions_as_parameters():
    print("Example: Probability Distributions as Parameters")
    import numpy as np
    from imgaug import augmenters as iaa
    from imgaug import parameters as iap

    images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

    # Blur by a value sigma which is sampled from a uniform distribution
    # of range 10.1 <= x < 13.0.
    # The convenience shortcut for this is: GaussianBlur((10.1, 13.0))
    blurer = iaa.GaussianBlur(10 + iap.Uniform(0.1, 3.0))
    images_aug = blurer(images=images)

    # Blur by a value sigma which is sampled from a gaussian distribution
    # N(1.0, 0.1), i.e. sample a value that is usually around 1.0.
    # Clip the resulting value so that it never gets below 0.1 or above 3.0.
    blurer = iaa.GaussianBlur(iap.Clip(iap.Normal(1.0, 0.1), 0.1, 3.0))
    images_aug = blurer(images=images)
Example #4
0
def chapter_parameters_special():
    ia.seed(1)

    # -----------------------
    # Choice
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Choice([0, 1, 2]),
        iap.Choice([0, 1, 2], p=[0.15, 0.5, 0.35]),
        iap.Choice([iap.Normal(-3, 1), iap.Normal(3, 1)]),
        iap.Choice([iap.Normal(-3, 1), iap.Poisson(3)])
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "special_choice.jpg", gridarr)

    # -----------------------
    # Clip
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Clip(iap.Normal(0, 1), -2, 2),
        iap.Clip(iap.Normal(0, 1), -2, None)
    ]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "special_clip.jpg", gridarr)

    # -----------------------
    # Discretize
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Discretize(iap.Normal(0, 1)),
        iap.Discretize(iap.ChiSquare(3))
    ]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "special_discretize.jpg", gridarr)

    # -----------------------
    # Absolute
    # -----------------------
    from imgaug import parameters as iap
    params = [iap.Absolute(iap.Normal(0, 1)), iap.Absolute(iap.Laplace(0, 1))]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "special_absolute.jpg", gridarr)

    # -----------------------
    # RandomSign
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.ChiSquare(3),
        iap.RandomSign(iap.ChiSquare(3)),
        iap.RandomSign(iap.ChiSquare(3), p_positive=0.75),
        iap.RandomSign(iap.ChiSquare(3), p_positive=0.9)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "special_randomsign.jpg", gridarr)

    # -----------------------
    # ForceSign
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.ForceSign(iap.Normal(0, 1), positive=True),
        iap.ChiSquare(3) - 3.0,
        iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="invert"),
        iap.ForceSign(iap.ChiSquare(3) - 3.0, positive=True, mode="reroll")
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "special_forcesign.jpg", gridarr)
Example #5
0
def chapter_parameters_arithmetic():
    ia.seed(1)

    # -----------------------
    # Add
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1) + 1,  # identical to: Add(Uniform(0, 1), 1)
        iap.Add(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])),
        iap.Normal(0, 1) + iap.Uniform(-5.5, -5) + iap.Uniform(5, 5.5),
        iap.Normal(0, 1) + iap.Uniform(-7, 5) + iap.Poisson(3),
        iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Add(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True)
    ]
    gridarr = draw_distributions_grid(
        params,
        rows=2,
        sample_sizes=[  # (iterations, samples per iteration)
            (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000),
            (1, 100000), (1, 100000)
        ])
    save("parameters", "arithmetic_add.jpg", gridarr)

    # -----------------------
    # Multiply
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1) * 2,  # identical to: Multiply(Uniform(0, 1), 2)
        iap.Multiply(iap.Uniform(0, 1), iap.Choice([0, 1], p=[0.7, 0.3])),
        (iap.Normal(0, 1) * iap.Uniform(-5.5, -5)) * iap.Uniform(5, 5.5),
        (iap.Normal(0, 1) * iap.Uniform(-7, 5)) * iap.Poisson(3),
        iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Multiply(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True)
    ]
    gridarr = draw_distributions_grid(
        params,
        rows=2,
        sample_sizes=[  # (iterations, samples per iteration)
            (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000),
            (1, 100000), (1, 100000)
        ])
    save("parameters", "arithmetic_multiply.jpg", gridarr)

    # -----------------------
    # Divide
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1) / 2,  # identical to: Divide(Uniform(0, 1), 2)
        iap.Divide(iap.Uniform(0, 1), iap.Choice([0, 2], p=[0.7, 0.3])),
        (iap.Normal(0, 1) / iap.Uniform(-5.5, -5)) / iap.Uniform(5, 5.5),
        (iap.Normal(0, 1) * iap.Uniform(-7, 5)) / iap.Poisson(3),
        iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1)),
        iap.Divide(iap.Normal(-3, 1), iap.Normal(3, 1), elementwise=True)
    ]
    gridarr = draw_distributions_grid(
        params,
        rows=2,
        sample_sizes=[  # (iterations, samples per iteration)
            (1000, 1000), (1000, 1000), (1000, 1000), (1000, 1000),
            (1, 100000), (1, 100000)
        ])
    save("parameters", "arithmetic_divide.jpg", gridarr)

    # -----------------------
    # Power
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Uniform(0, 1)**2,  # identical to: Power(Uniform(0, 1), 2)
        iap.Clip(iap.Uniform(-1, 1)**iap.Normal(0, 1), -4, 4)
    ]
    gridarr = draw_distributions_grid(params, rows=1)
    save("parameters", "arithmetic_power.jpg", gridarr)
Example #6
0
 def _enhance_parameter(level):
     fparam = _float_parameter(level, 0.9)
     return iap.Clip(
         iap.Add(1.0, iap.RandomSign(fparam), elementwise=True), 0.1,
         1.9)
Example #7
0
    def _create_main_augmenters_list(cls, m, cval):
        # pylint: disable=invalid-name
        m_max = cls._M_MAX

        def _float_parameter(level, maxval):
            maxval_norm = maxval / m_max
            return iap.Multiply(level, maxval_norm, elementwise=True)

        def _int_parameter(level, maxval):
            # paper applies just int(), so we don't round here
            return iap.Discretize(_float_parameter(level, maxval), round=False)

        # In the paper's code they use the definition from AutoAugment,
        # which is 0.1 + M*1.8/10. But that results in 0.1 for M=0, i.e. for
        # Brightness an almost black image, while M=5 would result in an
        # unaltered image. For AutoAugment that may be fine, as M is optimized
        # for each operation individually, but here we have only one fixed M
        # for all operations. Hence, we rather set this to 1.0 +/- M*0.9/10,
        # so that M=10 would result in 0.1 or 1.9.
        def _enhance_parameter(level):
            fparam = _float_parameter(level, 0.9)
            return iap.Clip(
                iap.Add(1.0, iap.RandomSign(fparam), elementwise=True), 0.1,
                1.9)

        def _subtract(a, b):
            return iap.Subtract(a, b, elementwise=True)

        def _affine(*args, **kwargs):
            kwargs["fillcolor"] = cval
            if "center" not in kwargs:
                kwargs["center"] = (0.0, 0.0)
            return pillike.Affine(*args, **kwargs)

        # we don't add vertical flips here, paper is not really clear about
        # whether they used them or not
        return [
            meta.Identity(),
            pillike.Autocontrast(cutoff=0),
            pillike.Equalize(),
            arithmetic.Invert(p=1.0),
            # paper uses 4 - int_parameter(M, 4)
            pillike.Posterize(
                nb_bits=_subtract(8, iap.Clip(_int_parameter(m, 6), 0, 6))),
            # paper uses 256 - int_parameter(M, 256)
            pillike.Solarize(p=1.0,
                             threshold=iap.Clip(
                                 _subtract(256, _int_parameter(m, 256)), 0,
                                 256)),
            pillike.EnhanceColor(_enhance_parameter(m)),
            pillike.EnhanceContrast(_enhance_parameter(m)),
            pillike.EnhanceBrightness(_enhance_parameter(m)),
            pillike.EnhanceSharpness(_enhance_parameter(m)),
            # paper code uses 20px on CIFAR (i.e. size 20/32), no information
            # on ImageNet values so we just use the same values
            arithmetic.Cutout(1,
                              size=iap.Clip(_float_parameter(m, 20 / 32), 0,
                                            20 / 32),
                              squared=True,
                              fill_mode="constant",
                              cval=cval),
            pillike.FilterBlur(),
            pillike.FilterSmooth(),
            iaa.AdditiveGaussianNoise(scale=(m / 100.) * 255, per_channel=True)
        ]