Exemplo n.º 1
0
    def test__draw_samples__single_value_hysteresis(self):
        seed = 1
        nb_images = 1000

        aug = iaa.Canny(
            alpha=0.2,
            hysteresis_thresholds=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            sobel_kernel_size=[3, 5, 7],
            random_state=iarandom.RNG(seed))

        example_image = np.zeros((5, 5, 3), dtype=np.uint8)
        samples = aug._draw_samples([example_image] * nb_images,
                                    random_state=iarandom.RNG(seed))
        alpha_samples = samples[0]
        hthresh_samples = samples[1]
        sobel_samples = samples[2]

        rss = iarandom.RNG(seed).duplicate(4)
        alpha_expected = iap.Deterministic(0.2).draw_samples((nb_images, ),
                                                             rss[0])
        hthresh_expected = iap.Choice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                                       10]).draw_samples((nb_images, 2),
                                                         rss[1])
        sobel_expected = iap.Choice([3, 5, 7]).draw_samples((nb_images, ),
                                                            rss[2])

        invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1]
        assert np.any(invalid)
        hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]]
        assert hthresh_expected.shape == (nb_images, 2)
        assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1])

        assert np.allclose(alpha_samples, alpha_expected)
        assert np.allclose(hthresh_samples, hthresh_expected)
        assert np.allclose(sobel_samples, sobel_expected)
Exemplo n.º 2
0
def someAug():
    bg = iap.Uniform(16, 18)
    #Creating a series of augmentations
    shearXY = iaa.Sequential([
        iaa.ShearY(iap.Uniform(-10, 10), cval=bg),
        iaa.ShearX(iap.Uniform(-10, 10), cval=bg)
    ])
    rotate = iaa.Rotate(rotate=iap.Choice([-30, -15, 15, 30],
                                          p=[0.25, 0.25, 0.25, 0.25]),
                        cval=bg)

    pwAff = iaa.PiecewiseAffine(scale=(0.01, 0.06), cval=bg)

    affine = iaa.Affine(scale={
        "x": iap.Uniform(1.1, 1.2),
        "y": iap.Uniform(1.1, 1.2)
    },
                        cval=bg)

    noise = iaa.AdditiveGaussianNoise(loc=0, scale=(0, 0.025 * 255))
    #Using SomeOf to randomly select some augmentations
    someAug = iaa.SomeOf(iap.Choice([2, 3, 4], p=[1 / 3, 1 / 3, 1 / 3]),
                         [affine, shearXY, pwAff, rotate, noise],
                         random_order=True)
    return someAug
Exemplo n.º 3
0
    def test__draw_samples__tuple_as_hysteresis(self):
        seed = 1
        nb_images = 10

        aug = iaa.Canny(
            alpha=0.2,
            hysteresis_thresholds=([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                   iap.DiscreteUniform(5, 100)),
            sobel_kernel_size=[3, 5, 7],
            random_state=iarandom.RNG(seed))
        aug.alpha = remove_prefetching(aug.alpha)
        aug.hysteresis_thresholds = (
            remove_prefetching(aug.hysteresis_thresholds[0]),
            remove_prefetching(aug.hysteresis_thresholds[1])
        )
        aug.sobel_kernel_size = remove_prefetching(aug.sobel_kernel_size)

        example_image = np.zeros((5, 5, 3), dtype=np.uint8)
        samples = aug._draw_samples([example_image] * nb_images,
                                    random_state=iarandom.RNG(seed))
        alpha_samples = samples[0]
        hthresh_samples = samples[1]
        sobel_samples = samples[2]

        rss = iarandom.RNG(seed).duplicate(4)
        alpha_expected = iap.Deterministic(0.2).draw_samples((nb_images,),
                                                             rss[0])
        hthresh_expected = [None, None]
        hthresh_expected[0] = iap.Choice(
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).draw_samples((nb_images,),
                                                             rss[1])
        # TODO simplify this to rss[2].randint(5, 100+1)
        #      would currenlty be a bit more ugly, because DiscrUniform
        #      samples two values for a and b first from rss[2]
        hthresh_expected[1] = iap.DiscreteUniform(5, 100).draw_samples(
            (nb_images,), rss[2])
        hthresh_expected = np.stack(hthresh_expected, axis=-1)

        sobel_expected = iap.Choice([3, 5, 7]).draw_samples((nb_images,),
                                                            rss[3])

        invalid = hthresh_expected[:, 0] > hthresh_expected[:, 1]
        hthresh_expected[invalid, :] = hthresh_expected[invalid, :][:, [1, 0]]
        assert hthresh_expected.shape == (nb_images, 2)
        assert not np.any(hthresh_expected[:, 0] > hthresh_expected[:, 1])

        assert np.allclose(alpha_samples, alpha_expected)
        assert np.allclose(hthresh_samples, hthresh_expected)
        assert np.allclose(sobel_samples, sobel_expected)
Exemplo n.º 4
0
 def test_alpha_1_lightness_is_stochastic_parameter(self):
     aug = iaa.Sharpen(alpha=1.0, lightness=iap.Choice([1.0, 1.5]))
     observed = aug.augment_image(self.base_img)
     expected1 = self._compute_sharpened_base_img(1.0 * 1.0, self.m)
     expected2 = self._compute_sharpened_base_img(1.0 * 1.5, self.m)
     assert (np.allclose(observed, expected1)
             or np.allclose(observed, expected2))
Exemplo n.º 5
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)
Exemplo n.º 6
0
 def test_p_replace_stochastic_parameter_n_segments_2(self):
     aug = iaa.Superpixels(
         p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2)
     observed = aug.augment_image(self.base_img)
     assert (
         np.allclose(observed, self.base_img)
         or self._array_equals_tolerant(
             observed, self.base_img_superpixels, 2)
     )
Exemplo n.º 7
0
 def test_alpha_is_stochastic_parameter(self):
     aug = iaa.Sharpen(alpha=iap.Choice([0.5, 1.0]), lightness=1)
     observed = aug.augment_image(self.base_img)
     expected1 = self._compute_sharpened_base_img(
         0.5 * 1, 0.5 * self.m_noop + 0.5 * self.m)
     expected2 = self._compute_sharpened_base_img(
         1.0 * 1, 0.0 * self.m_noop + 1.0 * self.m)
     assert (np.allclose(observed, expected1)
             or np.allclose(observed, expected2))
Exemplo n.º 8
0
 def test___init___stochastic_parameters(self):
     colorizer = iaa.RandomColorsBinaryImageColorizer(
         color_true=iap.DiscreteUniform(0, 100),
         color_false=iap.Choice([200, 201, 202]))
     assert isinstance(colorizer.color_true, iap.DiscreteUniform)
     assert isinstance(colorizer.color_false, iap.Choice)
     assert colorizer.color_true.a.value == 0
     assert colorizer.color_true.b.value == 100
     assert colorizer.color_false.a[0] == 200
     assert colorizer.color_false.a[1] == 201
     assert colorizer.color_false.a[2] == 202
Exemplo n.º 9
0
 def test_alpha_stochastic_parameter_strength_1(self):
     aug = iaa.Emboss(alpha=iap.Choice([0.5, 1.0]), strength=1)
     observed = aug.augment_image(self.base_img)
     expected1 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=0.5,
                                                 strength=1)
     expected2 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=1.0,
                                                 strength=1)
     assert (self._allclose(observed, expected1)
             or self._allclose(observed, expected2))
Exemplo n.º 10
0
 def test_alpha_1_strength_stochastic_parameter(self):
     aug = iaa.Emboss(alpha=1.0, strength=iap.Choice([1.0, 2.5]))
     observed = aug.augment_image(self.base_img)
     expected1 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=1.0,
                                                 strength=1.0)
     expected2 = self._compute_embossed_base_img(self.base_img,
                                                 alpha=1.0,
                                                 strength=2.5)
     assert (self._allclose(observed, expected1)
             or self._allclose(observed, expected2))
Exemplo n.º 11
0
 def test_images_p_is_stochastic_parameter(self):
     aug = self.create_aug(p=iap.Choice([0, 1], p=[0.7, 0.3]))
     seen = [0, 0]
     for _ in sm.xrange(1000):
         observed = aug.augment_image(self.image)
         if np.array_equal(observed, self.image):
             seen[0] += 1
         elif np.array_equal(observed, self.image_flipped):
             seen[1] += 1
         else:
             assert False
     assert np.allclose(seen, [700, 300], rtol=0, atol=75)
def create_augmneter():
    """
    Augmentator declaration.

    Returns
    -------
    iaa.Sequential
        imgaug-augmenter.
    """
    def random_size_crop(src):
        h, w, _ = src.shape
        src_area = h * w
        area = (0.15, 0.25)
        ratio = (3.0 / 4.0, 4.0 / 3.0)
        for _ in range(10):
            target_area = random.uniform(area[0], area[1]) * src_area
            new_ratio = random.uniform(*ratio)
            new_w = int(round(np.sqrt(target_area * new_ratio)))
            new_h = int(round(np.sqrt(target_area / new_ratio)))
            if random.random() < 0.5:
                new_h, new_w = new_w, new_h
            if new_w <= w and new_h <= h:
                x0 = random.randint(0, w - new_w)
                y0 = random.randint(0, h - new_h)
                return src[y0:(y0 + new_h), x0:(x0 + new_w)]
        new_w = int(round(0.3 * w))
        new_h = int(round(0.3 * h))
        x0 = int((w - new_w) / 2)
        y0 = int((h - new_h) / 2)
        return src[y0:(y0 + new_h), x0:(x0 + new_w)]

    def rand_crop(images, random_state, parents, hooks):
        images[0] = random_size_crop(images[0])
        return images

    return iaa.Sequential(children=[
        # iaa.Affine(
        #     rotate=(-25.0, 25.0),
        #     order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
        #     mode="edge"),
        iaa.OneOf(children=[
            # iaa.Rot90((1, 3), keep_size=False),
            # iaa.Grayscale(alpha=1.0),
            # iaa.AddToHueAndSaturation((25, 255)),
            # iaa.Lambda(func_images=rand_crop)
            iaa.Affine(rotate=(85.0, 275.0),
                       order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
                       mode="edge"),
        ])
    ])
Exemplo n.º 13
0
    def cpu_augment(self, imgs, boxes):
        # for bx in boxes:
        #     self.assert_bboxes(bx)
        ia_bb = []
        for n in range(len(imgs)):
            c_boxes = []
            for i in boxes[n]:
                try:
                    c_boxes.append(
                        ia.BoundingBox(x1=i[0], y1=i[1], x2=i[2], y2=i[3]))
                except AssertionError:
                    print('Assertion Error: ', i)
            ia_bb.append(ia.BoundingBoxesOnImage(c_boxes, shape=imgs[n].shape))

        seq = iaa.Sequential([
            iaa.Sometimes(0.5, iaa.AddElementwise((-20, 20), per_channel=1)),
            iaa.Sometimes(0.5,
                          iaa.AdditiveGaussianNoise(scale=(0, 0.10 * 255))),
            iaa.Sometimes(0.5, iaa.Multiply((0.75, 1.25), per_channel=1)),
            iaa.Sometimes(0.5, iaa.MultiplyElementwise((0.75, 1.25))),
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 1.0))),
            iaa.Fliplr(0.5),
            iaa.Sometimes(
                0.95,
                iaa.SomeOf(1, [
                    iaa.CoarseDropout(p=(0.10, 0.25),
                                      size_percent=(0.25, 0.5)),
                    iaa.CoarseDropout(p=(0.0, 0.15), size_percent=(0.1, 0.25)),
                    iaa.Dropout(p=(0, 0.25)),
                    iaa.CoarseSaltAndPepper(p=(0, 0.25),
                                            size_percent=(0.1, 0.2))
                ])),
            iaa.Affine(scale=iap.Choice(
                [iap.Uniform(0.4, 1), iap.Uniform(1, 3)]),
                       rotate=(-180, 180))
        ])
        seq_det = seq.to_deterministic()
        image_b_aug = seq_det.augment_images(imgs)
        bbs_b_aug = seq_det.augment_bounding_boxes(ia_bb)
        bbs_b_aug = [
            b.remove_out_of_image().cut_out_of_image() for b in bbs_b_aug
        ]
        return image_b_aug, [
            np.array([self.bbox_r(j) for j in i.bounding_boxes])
            for i in bbs_b_aug
        ]
Exemplo n.º 14
0
def chapter_parameters_introduction():
    ia.seed(1)
    from imgaug import augmenters as iaa
    from imgaug import parameters as iap

    seq = iaa.Sequential([
        iaa.GaussianBlur(sigma=iap.Uniform(0.0, 1.0)),
        iaa.ContrastNormalization(
            iap.Choice([1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2])),
        iaa.Affine(rotate=iap.Normal(0.0, 30),
                   translate_px=iap.RandomSign(iap.Poisson(3))),
        iaa.AddElementwise(iap.Discretize(
            (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64)),
        iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.1)) + 1.0)
    ])

    images = np.array([ia.quokka_square(size=(128, 128)) for i in range(16)])
    images_aug = [seq.augment_image(images[i]) for i in range(len(images))]
    save("parameters", "introduction.jpg", grid(images_aug, cols=4, rows=4))
Exemplo n.º 15
0
 def __init__(self):
     super(ImgAugTransform, self).__init__()
     from imgaug import augmenters as iaa
     from imgaug import parameters as iap
     self.seq = iaa.Sequential(children=[
         iaa.Sequential(children=[
             iaa.Affine(rotate=(-25, 25),
                        order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
                        mode="edge",
                        name="Affine"),
             iaa.Sequential(children=[
                 iaa.Sometimes(p=0.5,
                               then_list=iaa.AdditiveGaussianNoise(
                                   loc=0,
                                   scale=(0.0, 10.0),
                                   per_channel=0.5,
                                   name="AdditiveGaussianNoise")),
                 iaa.Sometimes(
                     p=0.1,
                     then_list=iaa.SaltAndPepper(
                         p=(
                             0,
                             0.01), per_channel=0.5, name="SaltAndPepper"))
             ],
                            random_order=True,
                            name="Noise"),
             iaa.OneOf(children=[
                 iaa.Sometimes(p=0.05,
                               then_list=iaa.MedianBlur(k=3,
                                                        name="MedianBlur")),
                 iaa.Sometimes(p=0.05,
                               then_list=iaa.AverageBlur(
                                   k=(2, 4), name="AverageBlur")),
                 iaa.Sometimes(p=0.5,
                               then_list=iaa.GaussianBlur(
                                   sigma=(0.0, 2.0), name="GaussianBlur"))
             ],
                       name="Blur"),
         ],
                        random_order=True,
                        name="MainProcess")
     ])
Exemplo n.º 16
0
def make_synthetic_prev_mask_complex_mask_augmenter(crop_size):
    h, w = crop_size

    return iaa.Sequential([
        iaa.Sometimes(0.5, iaa.Lambda(func_segmentation_maps=choose_random_objects_mask_augmenter)),
        iaa.Lambda(func_segmentation_maps=morph_close_mask_augmenter),
        iaa.Sometimes(0.3,
            # failed mask
            iaa.OneOf([
                iaa.TotalDropout(1.0),  # fill image
                iaa.Sequential([  # fail mask
                    iaa.OneOf([
                        iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.erode, min_coef=0.2, max_coef=0.5)),
                        iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.dilate, min_coef=0.2, max_coef=0.5)),
                    ]),
                    iaa.Affine(translate_percent=iap.Choice([iap.Uniform(-0.5, -0.2), iap.Uniform(0.2, 0.5)]))
                ])
            ]),

            # normal mask
            iaa.Sequential([
                iaa.Sometimes(0.1, iaa.OneOf([
                    iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.erode)),  # smaller mask
                    iaa.Lambda(func_segmentation_maps=make_morph_operation_mask_augmenter(cv2.dilate)),  # larger mask
                ])),
                iaa.Sometimes(1.0, iaa.Affine(
                    scale=iap.Normal(loc=1, scale=0.02),
                    translate_percent=iap.Normal(loc=0, scale=0.03),
                    shear=iap.Normal(loc=0, scale=1),
                    backend='cv2'
                )),
                iaa.Sometimes(0.1,
                    iaa.ElasticTransformation(alpha=2000, sigma=50)
                ),
                iaa.Sometimes(0.1,
                    iaa.PiecewiseAffine()
                )
            ])
        )
    ], random_order=False)
Exemplo n.º 17
0
def stochastic():
    return iaa.Sequential([
        iaa.GaussianBlur(
            sigma=iap.Uniform(0.0, 1.0)
        ),
        iaa.ContrastNormalization(
            iap.Choice(
                [1.0, 1.5, 3.0],
                p=[0.5, 0.3, 0.2]
            )
        ),
        iaa.Affine(
            rotate=iap.Normal(0.0, 30),
            translate_px=iap.RandomSign(iap.Poisson(3))
        ),
        iaa.AddElementwise(
            iap.Discretize(
                (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64
            )
        ),
        iaa.Multiply(
            iap.Positive(iap.Normal(0.0, 0.1)) + 1.0
        )
    ])
Exemplo n.º 18
0
def chapter_parameters_continuous():
    ia.seed(1)

    # -----------------------
    # Normal
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Normal(0, 1),
        iap.Normal(5, 3),
        iap.Normal(iap.Choice([-3, 3]), 1),
        iap.Normal(iap.Uniform(-3, 3), 1)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_normal.jpg", gridarr)

    # -----------------------
    # Laplace
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Laplace(0, 1),
        iap.Laplace(5, 3),
        iap.Laplace(iap.Choice([-3, 3]), 1),
        iap.Laplace(iap.Uniform(-3, 3), 1)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_laplace.jpg", gridarr)

    # -----------------------
    # ChiSquare
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.ChiSquare(1),
        iap.ChiSquare(3),
        iap.ChiSquare(iap.Choice([1, 5])),
        iap.RandomSign(iap.ChiSquare(3))
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_chisquare.jpg", gridarr)

    # -----------------------
    # Weibull
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Weibull(0.5),
        iap.Weibull(1),
        iap.Weibull(1.5),
        iap.Weibull((0.5, 1.5))
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_weibull.jpg", gridarr)

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

    # -----------------------
    # Beta
    # -----------------------
    from imgaug import parameters as iap
    params = [
        iap.Beta(0.5, 0.5),
        iap.Beta(2.0, 2.0),
        iap.Beta(1.0, 0.5),
        iap.Beta(0.5, 1.0)
    ]
    gridarr = draw_distributions_grid(params)
    save("parameters", "continuous_beta.jpg", gridarr)
Exemplo n.º 19
0
def test_Sharpen():
    reseed()

    def _compute_sharpened_base_img(lightness, m):
        base_img_sharpened = np.zeros((3, 3), dtype=np.float32)
        k = 1
        # note that cv2 uses reflection padding by default
        base_img_sharpened[0, 0] = (m[1, 1] + lightness) / k * 10 + 4 * (
            m[0, 0] / k) * 10 + 4 * (m[2, 2] / k) * 20
        base_img_sharpened[0, 2] = base_img_sharpened[0, 0]
        base_img_sharpened[2, 0] = base_img_sharpened[0, 0]
        base_img_sharpened[2, 2] = base_img_sharpened[0, 0]
        base_img_sharpened[0, 1] = (m[1, 1] + lightness) / k * 10 + 6 * (
            m[0, 1] / k) * 10 + 2 * (m[2, 2] / k) * 20
        base_img_sharpened[1, 0] = base_img_sharpened[0, 1]
        base_img_sharpened[1, 2] = base_img_sharpened[0, 1]
        base_img_sharpened[2, 1] = base_img_sharpened[0, 1]
        base_img_sharpened[
            1, 1] = (m[1, 1] + lightness) / k * 20 + 8 * (m[0, 1] / k) * 10

        base_img_sharpened = np.clip(base_img_sharpened, 0,
                                     255).astype(np.uint8)

        return base_img_sharpened

    base_img = [[10, 10, 10], [10, 20, 10], [10, 10, 10]]
    base_img = np.uint8(base_img)
    m = np.float32([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
    m_noop = np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]])
    base_img_sharpened = _compute_sharpened_base_img(1, m)

    aug = iaa.Sharpen(alpha=0, lightness=1)
    observed = aug.augment_image(base_img)
    expected = base_img
    assert np.allclose(observed, expected)

    aug = iaa.Sharpen(alpha=1.0, lightness=1)
    observed = aug.augment_image(base_img)
    expected = base_img_sharpened
    assert np.allclose(observed, expected)

    aug = iaa.Sharpen(alpha=0.5, lightness=1)
    observed = aug.augment_image(base_img)
    expected = _compute_sharpened_base_img(0.5 * 1, 0.5 * m_noop + 0.5 * m)
    assert np.allclose(observed, expected.astype(np.uint8))

    aug = iaa.Sharpen(alpha=0.75, lightness=1)
    observed = aug.augment_image(base_img)
    expected = _compute_sharpened_base_img(0.75 * 1, 0.25 * m_noop + 0.75 * m)
    assert np.allclose(observed, expected)

    aug = iaa.Sharpen(alpha=iap.Choice([0.5, 1.0]), lightness=1)
    observed = aug.augment_image(base_img)
    expected1 = _compute_sharpened_base_img(0.5 * 1, m)
    expected2 = _compute_sharpened_base_img(1.0 * 1, m)
    assert np.allclose(observed, expected1) or np.allclose(observed, expected2)

    got_exception = False
    try:
        _ = iaa.Sharpen(alpha="test", lightness=1)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    aug = iaa.Sharpen(alpha=1.0, lightness=2)
    observed = aug.augment_image(base_img)
    expected = _compute_sharpened_base_img(1.0 * 2, m)
    assert np.allclose(observed, expected)

    aug = iaa.Sharpen(alpha=1.0, lightness=3)
    observed = aug.augment_image(base_img)
    expected = _compute_sharpened_base_img(1.0 * 3, m)
    assert np.allclose(observed, expected)

    aug = iaa.Sharpen(alpha=1.0, lightness=iap.Choice([1.0, 1.5]))
    observed = aug.augment_image(base_img)
    expected1 = _compute_sharpened_base_img(1.0 * 1.0, m)
    expected2 = _compute_sharpened_base_img(1.0 * 1.5, m)
    assert np.allclose(observed, expected1) or np.allclose(observed, expected2)

    got_exception = False
    try:
        _ = iaa.Sharpen(alpha=1.0, lightness="test")
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # this part doesnt really work so far due to nonlinearities resulting from clipping to uint8
    """
Exemplo n.º 20
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)
Exemplo n.º 21
0
 def __init__(self):
     super(ImgAugTransform, self).__init__()
     from imgaug import augmenters as iaa
     from imgaug import parameters as iap
     self.seq = iaa.Sequential(children=[
         iaa.Sequential(children=[
             iaa.Sequential(children=[
                 iaa.OneOf(children=[
                     iaa.Sometimes(
                         p=0.95,
                         then_list=iaa.Affine(scale={
                             "x": (0.9, 1.1),
                             "y": (0.9, 1.1)
                         },
                                              translate_percent={
                                                  "x": (-0.05, 0.05),
                                                  "y": (-0.05, 0.05)
                                              },
                                              rotate=(-30, 30),
                                              shear=(-15, 15),
                                              order=iap.Choice(
                                                  [0, 1, 3],
                                                  p=[0.15, 0.80, 0.05]),
                                              mode="reflect",
                                              name="Affine")),
                     iaa.Sometimes(p=0.05,
                                   then_list=iaa.PerspectiveTransform(
                                       scale=(0.01, 0.1)))
                 ],
                           name="Blur"),
                 iaa.Sometimes(p=0.01,
                               then_list=iaa.PiecewiseAffine(
                                   scale=(0.0, 0.01),
                                   nb_rows=(4, 20),
                                   nb_cols=(4, 20),
                                   order=iap.Choice([0, 1, 3],
                                                    p=[0.15, 0.80, 0.05]),
                                   mode="reflect",
                                   name="PiecewiseAffine"))
             ],
                            random_order=True,
                            name="GeomTransform"),
             iaa.Sequential(children=[
                 iaa.Sometimes(p=0.75,
                               then_list=iaa.Add(value=(-10, 10),
                                                 per_channel=0.5,
                                                 name="Brightness")),
                 iaa.Sometimes(p=0.05,
                               then_list=iaa.Emboss(alpha=(0.0, 0.5),
                                                    strength=(0.5, 1.2),
                                                    name="Emboss")),
                 iaa.Sometimes(p=0.1,
                               then_list=iaa.Sharpen(alpha=(0.0, 0.5),
                                                     lightness=(0.5, 1.2),
                                                     name="Sharpen")),
                 iaa.Sometimes(p=0.25,
                               then_list=iaa.ContrastNormalization(
                                   alpha=(0.5, 1.5),
                                   per_channel=0.5,
                                   name="ContrastNormalization"))
             ],
                            random_order=True,
                            name="ColorTransform"),
             iaa.Sequential(children=[
                 iaa.Sometimes(p=0.5,
                               then_list=iaa.AdditiveGaussianNoise(
                                   loc=0,
                                   scale=(0.0, 10.0),
                                   per_channel=0.5,
                                   name="AdditiveGaussianNoise")),
                 iaa.Sometimes(p=0.1,
                               then_list=iaa.SaltAndPepper(
                                   p=(0, 0.001),
                                   per_channel=0.5,
                                   name="SaltAndPepper"))
             ],
                            random_order=True,
                            name="Noise"),
             iaa.OneOf(children=[
                 iaa.Sometimes(p=0.05,
                               then_list=iaa.MedianBlur(k=3,
                                                        name="MedianBlur")),
                 iaa.Sometimes(p=0.05,
                               then_list=iaa.AverageBlur(
                                   k=(2, 4), name="AverageBlur")),
                 iaa.Sometimes(p=0.5,
                               then_list=iaa.GaussianBlur(
                                   sigma=(0.0, 2.0), name="GaussianBlur"))
             ],
                       name="Blur"),
         ],
                        random_order=True,
                        name="MainProcess")
     ])
Exemplo n.º 22
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)
Exemplo n.º 23
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
Exemplo n.º 24
0
def test_Alpha():
    reseed()

    base_img = np.zeros((3, 3, 1), dtype=np.uint8)
    heatmaps_arr = np.float32([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0],
                               [0.0, 1.0, 1.0]])
    heatmaps_arr_r1 = np.float32([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],
                                  [0.0, 0.0, 1.0]])
    heatmaps_arr_l1 = np.float32([[0.0, 1.0, 0.0], [0.0, 1.0, 0.0],
                                  [1.0, 1.0, 0.0]])
    heatmaps = ia.HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3))

    aug = iaa.Alpha(1, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 10).astype(np.uint8)
    assert np.allclose(observed, expected)

    for per_channel in [False, True]:
        aug = iaa.Alpha(1,
                        iaa.Affine(translate_px={"x": 1}),
                        iaa.Affine(translate_px={"x": -1}),
                        per_channel=per_channel)
        observed = aug.augment_heatmaps([heatmaps])[0]
        assert observed.shape == heatmaps.shape
        assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6
        assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6
        assert np.allclose(observed.get_arr(), heatmaps_arr_r1)

    aug = iaa.Alpha(0, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 20).astype(np.uint8)
    assert np.allclose(observed, expected)

    for per_channel in [False, True]:
        aug = iaa.Alpha(0,
                        iaa.Affine(translate_px={"x": 1}),
                        iaa.Affine(translate_px={"x": -1}),
                        per_channel=per_channel)
        observed = aug.augment_heatmaps([heatmaps])[0]
        assert observed.shape == heatmaps.shape
        assert 0 - 1e-6 < heatmaps.min_value < 0 + 1e-6
        assert 1 - 1e-6 < heatmaps.max_value < 1 + 1e-6
        assert np.allclose(observed.get_arr(), heatmaps_arr_l1)

    aug = iaa.Alpha(0.75, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = np.round(base_img + 0.75 * 10 + 0.25 * 20).astype(np.uint8)
    assert np.allclose(observed, expected)

    aug = iaa.Alpha(0.75, None, iaa.Add(20))
    observed = aug.augment_image(base_img + 10)
    expected = np.round(base_img + 0.75 * 10 + 0.25 * (10 + 20)).astype(
        np.uint8)
    assert np.allclose(observed, expected)

    aug = iaa.Alpha(0.75, iaa.Add(10), None)
    observed = aug.augment_image(base_img + 10)
    expected = np.round(base_img + 0.75 * (10 + 10) + 0.25 * 10).astype(
        np.uint8)
    assert np.allclose(observed, expected)

    base_img = np.zeros((1, 2, 1), dtype=np.uint8)
    nb_iterations = 1000
    aug = iaa.Alpha((0.0, 1.0), iaa.Add(10), iaa.Add(110))
    values = []
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_image(base_img)
        observed_val = np.round(np.average(observed)) - 10
        values.append(observed_val / 100)

    nb_bins = 5
    hist, _ = np.histogram(values,
                           bins=nb_bins,
                           range=(0.0, 1.0),
                           density=False)
    density_expected = 1.0 / nb_bins
    density_tolerance = 0.05
    for nb_samples in hist:
        density = nb_samples / nb_iterations
        assert density_expected - density_tolerance < density < density_expected + density_tolerance

    # bad datatype for factor
    got_exception = False
    try:
        _ = iaa.Alpha(False, iaa.Add(10), None)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # per_channel
    aug = iaa.Alpha(1.0,
                    iaa.Add((0, 100), per_channel=True),
                    None,
                    per_channel=True)
    observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8))
    uq = np.unique(observed)
    assert len(uq) > 1
    assert np.max(observed) > 80
    assert np.min(observed) < 20

    aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), None, per_channel=True)
    observed = aug.augment_image(np.zeros((1, 1, 1000), dtype=np.uint8))
    uq = np.unique(observed)
    assert len(uq) > 1
    assert np.max(observed) > 80
    assert np.min(observed) < 20

    aug = iaa.Alpha((0.0, 1.0), iaa.Add(100), iaa.Add(0), per_channel=0.5)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_image(np.zeros((1, 1, 100), dtype=np.uint8))
        uq = np.unique(observed)
        if len(uq) == 1:
            seen[0] += 1
        elif len(uq) > 1:
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # bad datatype for per_channel
    got_exception = False
    try:
        _ = iaa.Alpha(0.5, iaa.Add(10), None, per_channel="test")
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    # propagating
    aug = iaa.Alpha(0.5, iaa.Add(100), iaa.Add(50), name="AlphaTest")

    def propagator(images, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksImages(propagator=propagator)
    image = np.zeros((10, 10, 3), dtype=np.uint8) + 1
    observed = aug.augment_image(image, hooks=hooks)
    assert np.array_equal(observed, image)

    # -----
    # keypoints
    # -----
    kps = [ia.Keypoint(x=5, y=10), ia.Keypoint(x=6, y=11)]
    kpsoi = ia.KeypointsOnImage(kps, shape=(20, 20, 3))

    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    # per_channel
    aug = iaa.Alpha(1.0,
                    iaa.Noop(),
                    iaa.Affine(translate_px={"x": 1}),
                    per_channel=True)
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.deepcopy()
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(0.0,
                    iaa.Noop(),
                    iaa.Affine(translate_px={"x": 1}),
                    per_channel=True)
    observed = aug.augment_keypoints([kpsoi])[0]
    expected = kpsoi.shift(x=1)
    assert keypoints_equal([observed], [expected])

    aug = iaa.Alpha(iap.Choice([0.49, 0.51]),
                    iaa.Noop(),
                    iaa.Affine(translate_px={"x": 1}),
                    per_channel=True)
    expected_same = kpsoi.deepcopy()
    expected_shifted = kpsoi.shift(x=1)
    seen = [0, 0]
    for _ in sm.xrange(200):
        observed = aug.augment_keypoints([kpsoi])[0]
        if keypoints_equal([observed], [expected_same]):
            seen[0] += 1
        elif keypoints_equal([observed], [expected_shifted]):
            seen[1] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50

    # propagating
    aug = iaa.Alpha(0.0,
                    iaa.Affine(translate_px={"x": 1}),
                    iaa.Affine(translate_px={"y": 1}),
                    name="AlphaTest")

    def propagator(kpsoi_to_aug, augmenter, parents, default):
        if "Alpha" in augmenter.name:
            return False
        else:
            return default

    hooks = ia.HooksKeypoints(propagator=propagator)
    observed = aug.augment_keypoints([kpsoi], hooks=hooks)[0]
    assert keypoints_equal([observed], [kpsoi])

    # -----
    # get_parameters()
    # -----
    first = iaa.Noop()
    second = iaa.Sequential([iaa.Add(1)])
    aug = iaa.Alpha(0.65, first, second, per_channel=1)
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Deterministic)
    assert isinstance(params[1], iap.Deterministic)
    assert 0.65 - 1e-6 < params[0].value < 0.65 + 1e-6
    assert params[1].value == 1

    # -----
    # get_children_lists()
    # -----
    first = iaa.Noop()
    second = iaa.Sequential([iaa.Add(1)])
    aug = iaa.Alpha(0.65, first, second, per_channel=1)
    children_lsts = aug.get_children_lists()
    assert len(children_lsts) == 2
    assert ia.is_iterable([lst for lst in children_lsts])
    assert first in children_lsts[0]
    assert second == children_lsts[1]
Exemplo n.º 25
0
def data_aug(img, bboxs=None, keypoints=None):
    '''
    :param img: 需要进行数据增强的图像
    :param bboxs: list, [ [x1, y1, x2, y2], ..., [xn1, yn1, xn2, yn2] ]
    :param keypoints: 关键点, COCO format or Ai-challenger format, list of list, [ [num_joints x 3], [num_joints x 3], ..., ]
    :return:
    '''
    is_flip = [random.randint(0, 1), random.randint(0, 1)]
    seq = iaa.Sequential([
        iaa.Multiply((0.7, 1.5)),
        iaa.Grayscale(iap.Choice(a=[0, 1], p=[0.8, 0.2]),
                      from_colorspace='BGR'),
        iaa.Fliplr(is_flip[0]),
        iaa.Flipud(is_flip[1]),
        iaa.Affine(rotate=(-15, 15), scale=(0.8, 1.2), mode='constant'),
    ])

    seq_det = seq.to_deterministic()
    bbs = None
    kps = None

    if bboxs is not None:
        assert type(bboxs) == type([])
        bbs = ia.BoundingBoxesOnImage([], shape=img.shape)
        for box in bboxs:
            bbs.bounding_boxes.append(
                ia.BoundingBox(x1=box[0], y1=box[1], x2=box[2], y2=box[3]))

    if keypoints is not None:
        kps = ia.KeypointsOnImage([], shape=img.shape)
        assert type(keypoints) == type([])
        for single_person_keypoints in keypoints:
            for i in range(14):
                joint = single_person_keypoints[i * 3:i * 3 + 3]
                kps.keypoints.append(ia.Keypoint(x=joint[0], y=joint[1]))

    img_aug = seq_det.augment_image(img)
    if bbs is not None:
        bbs_aug = seq_det.augment_bounding_boxes(bbs)
        bboxs = []
        for i in range(len(bbs_aug.bounding_boxes)):
            box_aug = bbs_aug.bounding_boxes[i]
            box = [box_aug.x1, box_aug.y1, box_aug.x2, box_aug.y2]
            bboxs.append(box)

    if kps is not None:
        kps_aug = seq_det.augment_keypoints(kps)
        kps_ori = copy.copy(keypoints)
        kps_ori = np.reshape(np.asarray(kps_ori), newshape=(-1, 14, 3))
        joint_nums = 14
        keypoints = []
        for i in range(len(kps_aug.keypoints)):
            point = kps_aug.keypoints[i]
            keypoints.append([point.x, point.y, 1])
            # single_keypoints.append([point.x, point.y, 1])
            # if len(single_keypoints) == joint_nums:
            #     keypoints.append(single_keypoints)
            #     single_keypoints = []
        keypoints = np.reshape(np.asarray(keypoints), newshape=(-1, 14, 3))
        # keep ori keypoint visiable attribute
        for i in range(kps_ori.shape[0]):
            for joint in range(kps_ori.shape[1]):
                keypoints[i][joint][2] = kps_ori[i][joint][2]

        # if flip, change keypoint order (left <-> right)
        # ai-format: [ 0-right_shoulder, 1-right_elbow, 2-right_wrist,
        #              3-left_shoulder, 4-left_elbow, 5-left_wrist,
        #              6-right_hip, 7-right_knee, 8-right_ankle,
        #              9-left_hip, 10-left_knee, 11-left_ankle,
        #              12-head, 13-neck ]
        # coco-format: TODO add coco-foramt change index
        change_index = [[0, 3], [1, 4], [2, 5], [6, 9], [7, 10], [8, 11]]
        for flip in is_flip:
            if flip:
                for i in range(kps_ori.shape[0]):
                    for index in change_index:
                        right_point = copy.copy(keypoints[i][index[0]])
                        keypoints[i][index[0]] = keypoints[i][index[1]]
                        keypoints[i][index[1]] = right_point
        keypoints = [
            list(np.reshape(single_person_keypoints, (-1, )))
            for single_person_keypoints in keypoints
        ]

    # test
    # if bbs is not None:
    #     img_before = bbs.draw_on_image(img, color=(0, 255, 0), thickness=2)
    #     img_after = bbs_aug.draw_on_image(img_aug, color=(0,0,255), thickness=2)
    #     cv2.imshow('box ori', img_before)
    #     cv2.imshow('box after', img_after)
    #     cv2.waitKey(0)
    # if kps is not None:
    #     img_before = kps.draw_on_image(img, color=(0, 255, 0), size=5)
    #     img_after = kps_aug.draw_on_image(img_aug, color=(0, 0, 255), size=5)
    #     for i in range(kps_ori.shape[0]):
    #         for joint in range(kps_ori.shape[1]):
    #             point = kps_ori[i][joint]
    #             cv2.putText(img_before, str(point[2]), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 250), 1)
    #             point = keypoints[i][joint]
    #             cv2.putText(img_after, str(point[2]), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 250), 1)
    #     cv2.imshow('kps ori', img_before)
    #     cv2.imshow('kps after', img_after)
    #     cv2.waitKey(0)

    return img_aug, bboxs, keypoints
def apply_transform_on_an_image(image, bbox=None):

    # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
    # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second
    # image.
    sometimes = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            # iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBs
            # Small gaussian blur with random sigma between 0 and 0.5.
            # But we only blur about 50% of all images.
            # iaa.Sometimes(0.5,
            #               iaa.GaussianBlur(sigma=iap.Uniform(0.0, 1.0))
            #               ),
            iaa.ContrastNormalization(
                iap.Choice([1.0, 1.5, 3.0], p=[0.5, 0.3, 0.2])),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            # iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply(iap.Positive(iap.Normal(0.0, 0.1)) + 1.0,
                         per_channel=0.2),
            # Apply affine transformations to each image.
            # - scale to 80-120% of image height/width (each axis independently)
            # - translate by -20 to +20 relative to height/width (per axis)
            # - rotate by -45 to +45 degrees
            # - shear by -16 to +16 degrees
            # - order: use nearest neighbour or bilinear interpolation (fast)
            # - mode: use any available mode to fill newly created pixels
            #         see API or scikit-image for which modes are available
            # - cval: if the mode is constant, then use a random brightness
            #         for the newly created pixels (e.g. sometimes black,
            #         sometimes white)
            # 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)},
            #     # translate_percent=0.1,
            #     rotate=iap.Normal(-45, 45),
            #     shear=(-8, 8),
            #     order=[0, 1],
            #     cval=(0, 255),
            #     # translate_px=iap.RandomSign(iap.Poisson(3)),
            #     # translate_px=3,
            #     # mode=["constant", "edge"]
            #     mode=ia.ALL
            # ),

            # iaa.AddElementwise(
            #     iap.Discretize(
            #         (iap.Beta(0.5, 0.5) * 2 - 1.0) * 64
            #     )
            # ),
            #
            # Execute 0 to 5 of the following (less important) augmenters per
            # image. Don't execute all of them, as that would often be way too
            # strong.
            #
            iaa.SomeOf(
                (0, 5),
                [
                    # Convert some images into their superpixel representation,
                    # sample between 20 and 200 superpixels per image, but do
                    # not replace all superpixels with their average, only
                    # some of them (p_replace).
                    # sometimes(
                    #     iaa.Superpixels(
                    #         p_replace=(0, 0.5),
                    #         n_segments=(1, 4)
                    #     )
                    # ),

                    # Blur each image with varying strength using
                    # gaussian blur (sigma between 0 and 3.0),
                    # average/uniform blur (kernel size between 2x2 and 7x7)
                    # median blur (kernel size between 3x3 and 11x11).
                    iaa.OneOf([
                        iaa.GaussianBlur((0, 0.3)),
                        iaa.AverageBlur(k=(1, 2)),
                        iaa.MedianBlur(k=(1, 3)),
                    ]),

                    # iaa.OneOf([
                    #     iaa.GaussianBlur((0, 3.0)),
                    #     iaa.AverageBlur(k=(2, 7)),
                    #     iaa.MedianBlur(k=(3, 11)),
                    # ]),

                    # Sharpen each image, overlay the result with the original
                    # image using an alpha between 0 (no sharpening) and 1
                    # (full sharpening effect).
                    iaa.Sharpen(alpha=(0, 0.01), lightness=(0, 0.01)),

                    # Same as sharpen, but for an embossing effect.
                    iaa.Emboss(alpha=(0, 0.01), strength=(0, 0.01)),

                    # Search in some images either for all edges or for
                    # directed edges. These edges are then marked in a black
                    # and white image and overlayed with the original image
                    # using an alpha of 0 to 0.7.
                    sometimes(
                        iaa.OneOf([
                            iaa.EdgeDetect(alpha=(0, 0.005)),
                            iaa.DirectedEdgeDetect(alpha=(0, 0.001),
                                                   direction=(0.0, 0.001)),
                        ])),

                    # Add gaussian noise to some images.
                    # In 50% of these cases, the noise is randomly sampled per
                    # channel and pixel.
                    # In the other 50% of all cases it is sampled once per
                    # pixel (i.e. brightness change).
                    # iaa.AdditiveGaussianNoise(
                    #     loc=0, scale=(0.0, 0.001 * 255), per_channel=0.5
                    # ),

                    # Either drop randomly 1 to 10% of all pixels (i.e. set
                    # them to black) or drop them on an image with 2-5% percent
                    # of the original size, leading to large dropped
                    # rectangles.
                    # iaa.OneOf([
                    #     iaa.Dropout((0, 0.05), per_channel=0.5),
                    #     iaa.CoarseDropout(
                    #         (0, 0.01), size_percent=(0.1, 0.2),
                    #         per_channel=0.2
                    #     ),
                    # ]),

                    # Invert each image's channel with 5% probability.
                    # This sets each pixel value v to 255-v.
                    iaa.Invert(0.1, per_channel=True),  # invert color channels

                    # Add a value of -10 to 10 to each pixel.
                    #iaa.Add((-40, 40), per_channel=0.5),

                    # Change brightness of images (50-150% of original value).
                    # iaa.Multiply((0.5, 1.5), per_channel=0.5),

                    # Improve or worsen the contrast of images.
                    # iaa.contrast.LinearContrast((0.5, 2.0), per_channel=0.5),

                    # Convert each image to grayscale and then overlay the
                    # result with the original with random alpha. I.e. remove
                    # colors with varying strengths.
                    # iaa.Grayscale(alpha=(0.0, 1.0)),

                    # In some images move pixels locally around (with random
                    # strengths).
                    # sometimes(
                    #     iaa.ElasticTransformation(alpha=(0.5, 1.5), sigma=0.25)
                    # ),

                    # In some images distort local areas with varying strength.
                    # sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05)))
                ],
                # do all of the above augmentations in random order
                random_order=True)
        ],
        random_order=True)

    # Augment BBs and images.
    aug_image = seq(image=image)
    return aug_image
Exemplo n.º 27
0
def test_Emboss():
    reseed()

    base_img = [[10, 10, 10], [10, 20, 10], [10, 10, 15]]
    base_img = np.uint8(base_img)

    def _compute_embossed_base_img(img, alpha, strength):
        img = np.copy(img)
        base_img_embossed = np.zeros((3, 3), dtype=np.float32)

        m = np.float32([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
        strength_matrix = strength * np.float32([[-1, -1, 0], [-1, 0, 1],
                                                 [0, 1, 1]])
        ms = m + strength_matrix

        for i in range(base_img_embossed.shape[0]):
            for j in range(base_img_embossed.shape[1]):
                for u in range(ms.shape[0]):
                    for v in range(ms.shape[1]):
                        weight = ms[u, v]
                        inputs_i = abs(i + (u - (ms.shape[0] - 1) // 2))
                        inputs_j = abs(j + (v - (ms.shape[1] - 1) // 2))
                        if inputs_i >= img.shape[0]:
                            diff = inputs_i - (img.shape[0] - 1)
                            inputs_i = img.shape[0] - 1 - diff
                        if inputs_j >= img.shape[1]:
                            diff = inputs_j - (img.shape[1] - 1)
                            inputs_j = img.shape[1] - 1 - diff
                        inputs = img[inputs_i, inputs_j]
                        base_img_embossed[i, j] += inputs * weight

        return np.clip((1 - alpha) * img + alpha * base_img_embossed, 0,
                       255).astype(np.uint8)

    def _allclose(a, b):
        return np.max(a.astype(np.float32) - b.astype(np.float32)) <= 2.1

    aug = iaa.Emboss(alpha=0, strength=1)
    observed = aug.augment_image(base_img)
    expected = base_img
    assert _allclose(observed, expected)

    aug = iaa.Emboss(alpha=1.0, strength=1)
    observed = aug.augment_image(base_img)
    expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=1)
    assert _allclose(observed, expected)

    aug = iaa.Emboss(alpha=0.5, strength=1)
    observed = aug.augment_image(base_img)
    expected = _compute_embossed_base_img(base_img, alpha=0.5, strength=1)
    assert _allclose(observed, expected.astype(np.uint8))

    aug = iaa.Emboss(alpha=0.75, strength=1)
    observed = aug.augment_image(base_img)
    expected = _compute_embossed_base_img(base_img, alpha=0.75, strength=1)
    assert _allclose(observed, expected)

    aug = iaa.Emboss(alpha=iap.Choice([0.5, 1.0]), strength=1)
    observed = aug.augment_image(base_img)
    expected1 = _compute_embossed_base_img(base_img, alpha=0.5, strength=1)
    expected2 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1)
    assert _allclose(observed, expected1) or np.allclose(observed, expected2)

    got_exception = False
    try:
        _ = iaa.Emboss(alpha="test", strength=1)
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception

    aug = iaa.Emboss(alpha=1.0, strength=2)
    observed = aug.augment_image(base_img)
    expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=2)
    assert _allclose(observed, expected)

    aug = iaa.Emboss(alpha=1.0, strength=3)
    observed = aug.augment_image(base_img)
    expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=3)
    assert _allclose(observed, expected)

    aug = iaa.Emboss(alpha=1.0, strength=6)
    observed = aug.augment_image(base_img)
    expected = _compute_embossed_base_img(base_img, alpha=1.0, strength=6)
    assert _allclose(observed, expected)

    aug = iaa.Emboss(alpha=1.0, strength=iap.Choice([1.0, 1.5]))
    observed = aug.augment_image(base_img)
    expected1 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1.0)
    expected2 = _compute_embossed_base_img(base_img, alpha=1.0, strength=1.5)
    assert _allclose(observed, expected1) or np.allclose(observed, expected2)

    got_exception = False
    try:
        _ = iaa.Emboss(alpha=1.0, strength="test")
    except Exception as exc:
        assert "Expected " in str(exc)
        got_exception = True
    assert got_exception
Exemplo n.º 28
0
    def transform(self, image: np.ndarray, target: str,
                  condition: int) -> Tuple[torch.Tensor, torch.Tensor, int]:
        """Transforms and normalizes the data. If in training mode the data is augmentated.

        Args:
            image (np.ndarray): Image to transform
            target (str): Training target
            condition (int): Condition

        Returns:
            Tuple[torch.Tensor, torch.Tensor, int]: Augmented image, target and condition
        """
        # Resize
        resize = iaa.Resize({"height": 224, "width": 224})
        image = resize.augment_image(image)

        # Random horizontal flipping and erase
        if self.train:

            if random.random() > 0.5:
                # flip image
                flip = iaa.HorizontalFlip(1.0)
                image = flip.augment_image(image)

                # flip class
                if target == "a":
                    target = "d"
                elif target == "d":
                    target = "a"

                # flip condition
                if condition == 2:
                    condition = 4
                elif condition == 4:
                    condition = 2

            #imgaug
            seq = iaa.Sequential([
                iaa.Sometimes(0.5, iaa.Affine(rotate=(-15, 15))),
                iaa.Sometimes(0.3, iaa.EdgeDetect(alpha=(0.3, 0.8))),
                iaa.Sometimes(0.5, iaa.MotionBlur(k=iap.Choice([3, 5, 7]))),
                iaa.OneOf([
                    iaa.Dropout(p=(0, 0.3), per_channel=0.5),
                    iaa.CoarseSaltAndPepper(0.05, size_percent=(0.01, 0.09))
                ]),
                iaa.Sometimes(0.5, iaa.AllChannelsCLAHE(clip_limit=(1, 10)))
            ])
            image = seq.augment_image(image)

        # Transform to tensor
        image = TF.to_tensor(image)

        # Transform to one hot encoding
        target = torch.tensor(self.target_dict[target])

        #normalize image to fit pretrained vgg model
        normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])
        image = normalize(image)

        return image, target, condition
Exemplo n.º 29
0
def test_Fliplr():
    reseed()

    base_img = np.array([[0, 0, 1], [0, 0, 1], [0, 1, 1]], dtype=np.uint8)
    base_img = base_img[:, :, np.newaxis]

    base_img_flipped = np.array([[1, 0, 0], [1, 0, 0], [1, 1, 0]],
                                dtype=np.uint8)
    base_img_flipped = base_img_flipped[:, :, np.newaxis]

    images = np.array([base_img])
    images_flipped = np.array([base_img_flipped])

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=0, y=0),
            ia.Keypoint(x=1, y=1),
            ia.Keypoint(x=2, y=2)
        ],
                            shape=base_img.shape)
    ]
    keypoints_flipped = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=2, y=0),
            ia.Keypoint(x=1, y=1),
            ia.Keypoint(x=0, y=2)
        ],
                            shape=base_img.shape)
    ]

    # 0% chance of flip
    aug = iaa.Fliplr(0)
    aug_det = aug.to_deterministic()

    for _ in sm.xrange(10):
        observed = aug.augment_images(images)
        expected = images
        assert np.array_equal(observed, expected)

        observed = aug_det.augment_images(images)
        expected = images
        assert np.array_equal(observed, expected)

        observed = aug.augment_keypoints(keypoints)
        expected = keypoints
        assert keypoints_equal(observed, expected)

        observed = aug_det.augment_keypoints(keypoints)
        expected = keypoints
        assert keypoints_equal(observed, expected)

    # 0% chance of flip, heatmaps
    aug = iaa.Fliplr(0)
    heatmaps = ia.HeatmapsOnImage(np.float32([
        [0, 0.5, 0.75],
        [0, 0.5, 0.75],
        [0.75, 0.75, 0.75],
    ]),
                                  shape=(3, 3, 3))
    observed = aug.augment_heatmaps([heatmaps])[0]
    expected = heatmaps.get_arr()
    assert observed.shape == heatmaps.shape
    assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6
    assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6
    assert np.array_equal(observed.get_arr(), expected)

    # 100% chance of flip
    aug = iaa.Fliplr(1.0)
    aug_det = aug.to_deterministic()

    for _ in sm.xrange(10):
        observed = aug.augment_images(images)
        expected = images_flipped
        assert np.array_equal(observed, expected)

        observed = aug_det.augment_images(images)
        expected = images_flipped
        assert np.array_equal(observed, expected)

        observed = aug.augment_keypoints(keypoints)
        expected = keypoints_flipped
        assert keypoints_equal(observed, expected)

        observed = aug_det.augment_keypoints(keypoints)
        expected = keypoints_flipped
        assert keypoints_equal(observed, expected)

    # 100% chance of flip, heatmaps
    aug = iaa.Fliplr(1.0)
    heatmaps = ia.HeatmapsOnImage(np.float32([
        [0, 0.5, 0.75],
        [0, 0.5, 0.75],
        [0.75, 0.75, 0.75],
    ]),
                                  shape=(3, 3, 3))
    observed = aug.augment_heatmaps([heatmaps])[0]
    expected = np.fliplr(heatmaps.get_arr())
    assert observed.shape == heatmaps.shape
    assert heatmaps.min_value - 1e-6 < observed.min_value < heatmaps.min_value + 1e-6
    assert heatmaps.max_value - 1e-6 < observed.max_value < heatmaps.max_value + 1e-6
    assert np.array_equal(observed.get_arr(), expected)

    # 50% chance of flip
    aug = iaa.Fliplr(0.5)
    aug_det = aug.to_deterministic()

    nb_iterations = 1000
    nb_images_flipped = 0
    nb_images_flipped_det = 0
    nb_keypoints_flipped = 0
    nb_keypoints_flipped_det = 0
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_images(images)
        if np.array_equal(observed, images_flipped):
            nb_images_flipped += 1

        observed = aug_det.augment_images(images)
        if np.array_equal(observed, images_flipped):
            nb_images_flipped_det += 1

        observed = aug.augment_keypoints(keypoints)
        if keypoints_equal(observed, keypoints_flipped):
            nb_keypoints_flipped += 1

        observed = aug_det.augment_keypoints(keypoints)
        if keypoints_equal(observed, keypoints_flipped):
            nb_keypoints_flipped_det += 1

    assert int(nb_iterations * 0.3) <= nb_images_flipped <= int(
        nb_iterations * 0.7)
    assert int(nb_iterations * 0.3) <= nb_keypoints_flipped <= int(
        nb_iterations * 0.7)
    assert nb_images_flipped_det in [0, nb_iterations]
    assert nb_keypoints_flipped_det in [0, nb_iterations]

    # 50% chance of flipped, multiple images, list as input
    images_multi = [base_img, base_img]
    aug = iaa.Fliplr(0.5)
    aug_det = aug.to_deterministic()
    nb_iterations = 1000
    nb_flipped_by_pos = [0] * len(images_multi)
    nb_flipped_by_pos_det = [0] * len(images_multi)
    for _ in sm.xrange(nb_iterations):
        observed = aug.augment_images(images_multi)
        for i in sm.xrange(len(images_multi)):
            if np.array_equal(observed[i], base_img_flipped):
                nb_flipped_by_pos[i] += 1

        observed = aug_det.augment_images(images_multi)
        for i in sm.xrange(len(images_multi)):
            if np.array_equal(observed[i], base_img_flipped):
                nb_flipped_by_pos_det[i] += 1

    for val in nb_flipped_by_pos:
        assert int(nb_iterations * 0.3) <= val <= int(nb_iterations * 0.7)

    for val in nb_flipped_by_pos_det:
        assert val in [0, nb_iterations]

    # test StochasticParameter as p
    aug = iaa.Fliplr(p=iap.Choice([0, 1], p=[0.7, 0.3]))
    seen = [0, 0]
    for _ in sm.xrange(1000):
        observed = aug.augment_image(base_img)
        if np.array_equal(observed, base_img):
            seen[0] += 1
        elif np.array_equal(observed, base_img_flipped):
            seen[1] += 1
        else:
            assert False
    assert 700 - 75 < seen[0] < 700 + 75
    assert 300 - 75 < seen[1] < 300 + 75

    # test exceptions for wrong parameter types
    got_exception = False
    try:
        _ = iaa.Fliplr(p="test")
    except Exception:
        got_exception = True
    assert got_exception

    # test get_parameters()
    aug = iaa.Fliplr(p=0.5)
    params = aug.get_parameters()
    assert isinstance(params[0], iap.Binomial)
    assert isinstance(params[0].p, iap.Deterministic)
    assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4

    ###################
    # test other dtypes
    ###################
    aug = iaa.Fliplr(1.0)

    image = np.zeros((3, 3), dtype=bool)
    image[0, 0] = True
    expected = np.zeros((3, 3), dtype=bool)
    expected[0, 2] = True
    image_aug = aug.augment_image(image)
    assert image_aug.dtype.type == image.dtype.type
    assert np.all(image_aug == expected)

    for dtype in [
            np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int32,
            np.int64
    ]:
        min_value, center_value, max_value = iadt.get_value_range_of_dtype(
            dtype)
        value = max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.array_equal(image_aug, expected)

    for dtype, value in zip([np.float16, np.float32, np.float64, np.float128],
                            [5000, 1000**2, 1000**3, 1000**4]):
        atol = 1e-9 * max_value if dtype != np.float16 else 1e-3 * max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[0, 2] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.allclose(image_aug, expected, atol=atol)
Exemplo n.º 30
0
    def __init__(self,
                 mean=(0.0, 0.0, 0.0, 0.0),
                 std=(1.0, 1.0, 1.0, 1.0),
                 crop_image_size=(224, 224)):
        if isinstance(crop_image_size, int):
            crop_image_size = (crop_image_size, crop_image_size)
        self._mean = mean
        self._std = std
        self.crop_image_size = crop_image_size

        self.seq = iaa.Sequential(
            children=[
                iaa.Sequential(
                    children=[
                        iaa.Fliplr(
                            p=0.5,
                            name="Fliplr"),
                        iaa.Flipud(
                            p=0.5,
                            name="Flipud"),
                        iaa.Sequential(
                            children=[
                                iaa.Affine(
                                    scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
                                    translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)},
                                    rotate=(-45, 45),
                                    shear=(-16, 16),
                                    order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
                                    mode="reflect",
                                    name="Affine"),
                                iaa.Sometimes(
                                    p=0.01,
                                    then_list=iaa.PiecewiseAffine(
                                        scale=(0.0, 0.01),
                                        nb_rows=(4, 20),
                                        nb_cols=(4, 20),
                                        order=iap.Choice([0, 1, 3], p=[0.15, 0.80, 0.05]),
                                        mode="reflect",
                                        name="PiecewiseAffine"))],
                            random_order=True,
                            name="GeomTransform"),
                        iaa.Sequential(
                            children=[
                                iaa.Sometimes(
                                    p=0.75,
                                    then_list=iaa.Add(
                                        value=(-10, 10),
                                        per_channel=0.5,
                                        name="Brightness")),
                                iaa.Sometimes(
                                    p=0.05,
                                    then_list=iaa.Emboss(
                                        alpha=(0.0, 0.5),
                                        strength=(0.5, 1.2),
                                        name="Emboss")),
                                iaa.Sometimes(
                                    p=0.1,
                                    then_list=iaa.Sharpen(
                                        alpha=(0.0, 0.5),
                                        lightness=(0.5, 1.2),
                                        name="Sharpen")),
                                iaa.Sometimes(
                                    p=0.25,
                                    then_list=iaa.ContrastNormalization(
                                        alpha=(0.5, 1.5),
                                        per_channel=0.5,
                                        name="ContrastNormalization"))
                            ],
                            random_order=True,
                            name="ColorTransform"),
                        iaa.Sequential(
                            children=[
                                iaa.Sometimes(
                                    p=0.5,
                                    then_list=iaa.AdditiveGaussianNoise(
                                        loc=0,
                                        scale=(0.0, 10.0),
                                        per_channel=0.5,
                                        name="AdditiveGaussianNoise")),
                                iaa.Sometimes(
                                    p=0.1,
                                    then_list=iaa.SaltAndPepper(
                                        p=(0, 0.001),
                                        per_channel=0.5,
                                        name="SaltAndPepper"))],
                            random_order=True,
                            name="Noise"),
                        iaa.OneOf(
                            children=[
                                iaa.Sometimes(
                                    p=0.05,
                                    then_list=iaa.MedianBlur(
                                        k=3,
                                        name="MedianBlur")),
                                iaa.Sometimes(
                                    p=0.05,
                                    then_list=iaa.AverageBlur(
                                        k=(2, 4),
                                        name="AverageBlur")),
                                iaa.Sometimes(
                                    p=0.5,
                                    then_list=iaa.GaussianBlur(
                                        sigma=(0.0, 2.0),
                                        name="GaussianBlur"))],
                            name="Blur"),
                    ],
                    random_order=True,
                    name="MainProcess")])