Ejemplo n.º 1
0
 def test_shape_is_array(self):
     image = np.zeros((10, 20, 3), dtype=np.uint8)
     kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
     with assertWarns(self, ia.DeprecationWarning):
         kpi = ia.KeypointsOnImage(keypoints=kps, shape=image)
     assert kpi.shape == (10, 20, 3)
Ejemplo n.º 2
0
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    #image = ia.imresize_single_image(imageio.imread("quokka.jpg", pilmode="RGB")[0:643, 0:643], (128, 128))
    image = ia.quokka_square(size=(128, 128))

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

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

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

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

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

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

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

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

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


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

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

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

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

    for markup in markups:
        print(markup)
Ejemplo n.º 3
0
def test_determinism():
    reseed()

    images = [
        ia.quokka(size=(128, 128)),
        ia.quokka(size=(64, 64)),
        ia.imresize_single_image(skimage.data.astronaut(), (128, 256))
    ]
    images.extend([ia.quokka(size=(16, 16))] * 20)

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=20, y=10),
            ia.Keypoint(x=5, y=5),
            ia.Keypoint(x=10, y=43)
        ],
                            shape=(50, 60, 3))
    ] * 20

    augs = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)),
        iaa.Grayscale(alpha=(0.1, 1.0)),
        iaa.GaussianBlur((0.1, 3.0)),
        iaa.AverageBlur((3, 11)),
        iaa.MedianBlur((3, 11)),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)),
        iaa.EdgeDetect(alpha=(0.1, 1.0)),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)),
        iaa.Add((-50, 50)),
        iaa.AddElementwise((-50, 50)),
        iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)),
        iaa.Multiply((0.6, 1.4)),
        iaa.MultiplyElementwise((0.6, 1.4)),
        iaa.Dropout((0.3, 0.5)),
        iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)),
        iaa.Invert(0.5),
        iaa.Affine(scale=(0.7, 1.3),
                   translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20),
                   shear=(-20, 20),
                   order=ia.ALL,
                   mode=ia.ALL,
                   cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=0.5)
    ]

    augs_affect_geometry = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(scale=(0.7, 1.3),
                   translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20),
                   shear=(-20, 20),
                   order=ia.ALL,
                   mode=ia.ALL,
                   cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5))
    ]

    for aug in augs:
        aug_det = aug.to_deterministic()
        images_aug1 = aug_det.augment_images(images)
        images_aug2 = aug_det.augment_images(images)

        aug_det = aug.to_deterministic()
        images_aug3 = aug_det.augment_images(images)
        images_aug4 = aug_det.augment_images(images)

        assert array_equal_lists(images_aug1, images_aug2), \
            "Images (1, 2) expected to be identical for %s" % (aug.name,)

        assert array_equal_lists(images_aug3, images_aug4), \
            "Images (3, 4) expected to be identical for %s" % (aug.name,)

        assert not array_equal_lists(images_aug1, images_aug3), \
            "Images (1, 3) expected to be different for %s" % (aug.name,)

    for aug in augs_affect_geometry:
        aug_det = aug.to_deterministic()
        kps_aug1 = aug_det.augment_keypoints(keypoints)
        kps_aug2 = aug_det.augment_keypoints(keypoints)

        aug_det = aug.to_deterministic()
        kps_aug3 = aug_det.augment_keypoints(keypoints)
        kps_aug4 = aug_det.augment_keypoints(keypoints)

        assert keypoints_equal(kps_aug1, kps_aug2), \
            "Keypoints (1, 2) expected to be identical for %s" % (aug.name,)

        assert keypoints_equal(kps_aug3, kps_aug4), \
            "Keypoints (3, 4) expected to be identical for %s" % (aug.name,)

        assert not keypoints_equal(kps_aug1, kps_aug3), \
            "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
Ejemplo n.º 4
0
def test_LinearContrast():
    reseed()

    img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    img = np.uint8(img)
    img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3))

    # check basic functionality with alpha=1 or 2 (deterministic) and per_chanenl on/off (makes
    # no difference due to deterministic alpha)
    for per_channel in [False, 0, 0.0, True, 1, 1.0]:
        for alpha in [1, 2]:
            aug = iaa.LinearContrast(alpha=iap.Deterministic(alpha),
                                     per_channel=per_channel)
            img_aug = aug.augment_image(img)
            img3d_aug = aug.augment_image(img3d)
            assert img_aug.dtype.type == np.uint8
            assert img3d_aug.dtype.type == np.uint8
            assert np.array_equal(
                img_aug, contrast_lib._adjust_linear(img, alpha=alpha))
            assert np.array_equal(
                img3d_aug, contrast_lib._adjust_linear(img3d, alpha=alpha))

    # check that tuple to uniform works
    aug = iaa.LinearContrast((1, 2))
    assert isinstance(aug.params1d[0], iap.Uniform)
    assert isinstance(aug.params1d[0].a, iap.Deterministic)
    assert isinstance(aug.params1d[0].b, iap.Deterministic)
    assert aug.params1d[0].a.value == 1
    assert aug.params1d[0].b.value == 2

    # check that list to choice works
    aug = iaa.LinearContrast([1, 2])
    assert isinstance(aug.params1d[0], iap.Choice)
    assert all([val in aug.params1d[0].a for val in [1, 2]])

    # check that per_channel at 50% prob works
    aug = iaa.LinearContrast((0.5, 2.0), per_channel=0.5)
    seen = [False, False]
    # must not use just value 128 here, otherwise nothing will change as all values would have
    # distance 0 to 128
    img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128 + 20
    for _ in sm.xrange(100):
        img_aug = aug.augment_image(img1000d)
        assert img_aug.dtype.type == np.uint8
        l = len(set(img_aug.flatten().tolist()))
        if l == 1:
            seen[0] = True
        else:
            seen[1] = True
        if all(seen):
            break
    assert all(seen)

    # check that keypoints are not changed
    kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3))
    kpsoi_aug = iaa.LinearContrast(alpha=2).augment_keypoints([kpsoi])
    assert keypoints_equal([kpsoi], kpsoi_aug)

    # check that heatmaps are not changed
    heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5,
                                  shape=(3, 3, 3))
    heatmaps_aug = iaa.LinearContrast(alpha=2).augment_heatmaps([heatmaps])[0]
    assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)
Ejemplo n.º 5
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]
Ejemplo n.º 6
0
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (HEIGHT, WIDTH))

    kps = []
    for y in range(NB_ROWS):
        ycoord = BB_Y1 + int(y * (BB_Y2 - BB_Y1) / (NB_COLS - 1))
        for x in range(NB_COLS):
            xcoord = BB_X1 + int(x * (BB_X2 - BB_X1) / (NB_ROWS - 1))
            kp = (xcoord, ycoord)
            kps.append(kp)
    kps = set(kps)
    kps = [
        ia.Keypoint(x=xcoord, y=ycoord, vis=None, label=None)
        for (xcoord, ycoord) in kps
    ]
    kps = ia.KeypointsOnImage(kps, shape=image.shape)

    bb = ia.BoundingBox(x1=BB_X1, x2=BB_X2, y1=BB_Y1, y2=BB_Y2)
    bbs = ia.BoundingBoxesOnImage([bb], shape=image.shape)

    pairs = []
    params = [{
        "rotate": 45
    }, {
        "translate_px": 20
    }, {
        "translate_percent": 0.1
    }, {
        "scale": 1.2
    }, {
        "scale": 0.8
    }, {
        "shear": 45
    }, {
        "rotate": 45,
        "cval": 255
    }, {
        "translate_px": 20,
        "mode": "constant"
    }, {
        "translate_px": 20,
        "mode": "edge"
    }, {
        "translate_px": 20,
        "mode": "symmetric"
    }, {
        "translate_px": 20,
        "mode": "reflect"
    }, {
        "translate_px": 20,
        "mode": "wrap"
    }, {
        "scale": 0.5,
        "order": 0
    }, {
        "scale": 0.5,
        "order": 1
    }, {
        "scale": 0.5,
        "order": 2
    }, {
        "scale": 0.5,
        "order": 3
    }, {
        "scale": 0.5,
        "order": 4
    }, {
        "scale": 0.5,
        "order": 5
    }, {
        "rotate": 45,
        "translate_px": 20,
        "scale": 1.2
    }, {
        "rotate": 45,
        "translate_px": 20,
        "scale": 0.8
    }, {
        "rotate": (-45, 45),
        "translate_px": (-20, 20),
        "scale": (0.8, 1.2),
        "order": ia.ALL,
        "mode": ia.ALL,
        "cval": ia.ALL
    }, {
        "rotate": (-45, 45),
        "translate_px": (-20, 20),
        "scale": (0.8, 1.2),
        "order": ia.ALL,
        "mode": ia.ALL,
        "cval": ia.ALL
    }, {
        "rotate": (-45, 45),
        "translate_px": (-20, 20),
        "scale": (0.8, 1.2),
        "order": ia.ALL,
        "mode": ia.ALL,
        "cval": ia.ALL
    }, {
        "rotate": (-45, 45),
        "translate_px": (-20, 20),
        "scale": (0.8, 1.2),
        "order": ia.ALL,
        "mode": ia.ALL,
        "cval": ia.ALL
    }]
    seqs_skimage = [iaa.Affine(backend="skimage", **p) for p in params]
    seqs_cv2 = [iaa.Affine(backend="auto", **p) for p in params]

    for seq_skimage, seq_cv2 in zip(seqs_skimage, seqs_cv2):
        seq_skimage_det = seq_skimage.to_deterministic()
        seq_cv2_det = seq_cv2.to_deterministic()

        seq_cv2_det.copy_random_state_(seq_skimage_det)

        image_aug_skimage = seq_skimage_det.augment_image(image)
        image_aug_cv2 = seq_cv2_det.augment_image(image)
        kps_aug_skimage = seq_skimage_det.augment_keypoints([kps])[0]
        kps_aug_cv2 = seq_cv2_det.augment_keypoints([kps])[0]
        bbs_aug_skimage = seq_skimage_det.augment_bounding_boxes([bbs])[0]
        bbs_aug_cv2 = seq_cv2_det.augment_bounding_boxes([bbs])[0]

        image_before_skimage = np.copy(image)
        image_before_cv2 = np.copy(image)
        image_before_skimage = kps.draw_on_image(image_before_skimage)
        image_before_cv2 = kps.draw_on_image(image_before_cv2)
        image_before_skimage = bbs.draw_on_image(image_before_skimage)
        image_before_cv2 = bbs.draw_on_image(image_before_cv2)

        image_after_skimage = np.copy(image_aug_skimage)
        image_after_cv2 = np.copy(image_aug_cv2)
        image_after_skimage = kps_aug_skimage.draw_on_image(
            image_after_skimage)
        image_after_cv2 = kps_aug_cv2.draw_on_image(image_after_cv2)
        image_after_skimage = bbs_aug_skimage.draw_on_image(
            image_after_skimage)
        image_after_cv2 = bbs_aug_cv2.draw_on_image(image_after_cv2)

        pairs.append(
            np.hstack(
                (image_before_skimage, image_after_skimage, image_after_cv2)))

    ia.imshow(np.vstack(pairs))
    imageio.imwrite("affine.jpg", np.vstack(pairs))
Ejemplo n.º 7
0
def test_SigmoidContrast():
    reseed()

    img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    img = np.uint8(img)
    img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3))

    # check basic functionality with per_chanenl on/off (makes no difference due to deterministic
    # parameters)
    for per_channel in [False, 0, 0.0, True, 1, 1.0]:
        for gain, cutoff in itertools.product([5, 10], [0.25, 0.75]):
            aug = iaa.SigmoidContrast(gain=iap.Deterministic(gain),
                                      cutoff=iap.Deterministic(cutoff),
                                      per_channel=per_channel)
            img_aug = aug.augment_image(img)
            img3d_aug = aug.augment_image(img3d)
            assert img_aug.dtype.type == np.uint8
            assert img3d_aug.dtype.type == np.uint8
            assert np.array_equal(
                img_aug,
                skimage.exposure.adjust_sigmoid(img, gain=gain, cutoff=cutoff))
            assert np.array_equal(
                img3d_aug,
                skimage.exposure.adjust_sigmoid(img3d,
                                                gain=gain,
                                                cutoff=cutoff))

    # check that tuple to uniform works
    # note that gain and cutoff are saved in inverted order in _ContrastFuncWrapper to match
    # the order of skimage's function
    aug = iaa.SigmoidContrast(gain=(1, 2), cutoff=(0.25, 0.75))
    assert isinstance(aug.params1d[0], iap.Uniform)
    assert isinstance(aug.params1d[0].a, iap.Deterministic)
    assert isinstance(aug.params1d[0].b, iap.Deterministic)
    assert np.allclose(aug.params1d[0].a.value, 0.25)
    assert np.allclose(aug.params1d[0].b.value, 0.75)
    assert isinstance(aug.params1d[1], iap.Uniform)
    assert isinstance(aug.params1d[1].a, iap.Deterministic)
    assert isinstance(aug.params1d[1].b, iap.Deterministic)
    assert aug.params1d[1].a.value == 1
    assert aug.params1d[1].b.value == 2

    # check that list to choice works
    # note that gain and cutoff are saved in inverted order in _ContrastFuncWrapper to match
    # the order of skimage's function
    aug = iaa.SigmoidContrast(gain=[1, 2], cutoff=[0.25, 0.75])
    assert isinstance(aug.params1d[0], iap.Choice)
    assert all([
        np.allclose(val, val_choice)
        for val, val_choice in zip([0.25, 0.75], aug.params1d[0].a)
    ])
    assert isinstance(aug.params1d[1], iap.Choice)
    assert all([val in aug.params1d[1].a for val in [1, 2]])

    # check that per_channel at 50% prob works
    aug = iaa.SigmoidContrast(gain=(1, 10),
                              cutoff=(0.25, 0.75),
                              per_channel=0.5)
    seen = [False, False]
    img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128
    for _ in sm.xrange(100):
        img_aug = aug.augment_image(img1000d)
        assert img_aug.dtype.type == np.uint8
        l = len(set(img_aug.flatten().tolist()))
        if l == 1:
            seen[0] = True
        else:
            seen[1] = True
        if all(seen):
            break
    assert all(seen)

    # check that keypoints are not changed
    kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3))
    kpsoi_aug = iaa.SigmoidContrast(gain=10,
                                    cutoff=0.5).augment_keypoints([kpsoi])
    assert keypoints_equal([kpsoi], kpsoi_aug)

    # check that heatmaps are not changed
    heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5,
                                  shape=(3, 3, 3))
    heatmaps_aug = iaa.SigmoidContrast(gain=10,
                                       cutoff=0.5).augment_heatmaps([heatmaps
                                                                     ])[0]
    assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)

    ###################
    # test other dtypes
    ###################
    # uint, int
    for dtype in [
            np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16,
            np.int32, np.int64
    ]:
        min_value, center_value, max_value = meta.get_value_range_of_dtype(
            dtype)

        gains = [5, 20]
        cutoffs = [0.25, 0.75]
        values = [0, 100, int(center_value + 0.1 * max_value)]
        tmax = 1e-8 * max_value if dtype in [np.uint64, np.int64] else 0
        tolerances = [tmax, tmax, tmax]

        for gain, cutoff in itertools.product(gains, cutoffs):
            aug = iaa.SigmoidContrast(gain=gain, cutoff=cutoff)
            for value, tolerance in zip(values, tolerances):
                image = np.full((3, 3), value, dtype=dtype)
                # 1/(1 + exp(gain*(cutoff - I_ij/max)))
                expected = (1 / (1 + np.exp(
                    gain * (cutoff - image.astype(np.float128) / max_value))))
                expected = (expected * max_value).astype(dtype)
                image_aug = aug.augment_image(image)
                assert image_aug.dtype == np.dtype(dtype)
                assert len(np.unique(image_aug)) == 1
                value_aug = int(image_aug[0, 0])
                value_expected = int(expected[0, 0])
                diff = abs(value_aug - value_expected)
                assert diff <= tolerance

    # float
    for dtype in [np.float16, np.float32, np.float64]:

        def _allclose(a, b):
            atol = 1e-3 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        gains = [5, 20]
        cutoffs = [0.25, 0.75]
        isize = np.dtype(dtype).itemsize
        values = [0, 1.0, 50.0, 100**(isize - 1)]

        for gain, cutoff in itertools.product(gains, cutoffs):
            aug = iaa.SigmoidContrast(gain=gain, cutoff=cutoff)
            for value in values:
                image = np.full((3, 3), value, dtype=dtype)
                expected = (1 /
                            (1 + np.exp(gain *
                                        (cutoff - image.astype(np.float128))))
                            ).astype(dtype)
                image_aug = aug.augment_image(image)
                assert image_aug.dtype == np.dtype(dtype)
                assert _allclose(image_aug, expected)
def augment(train_x, train_y_prob, train_y_keys):
    aug = iaa.Sequential()
    x = random.uniform(-10, 10) * .01
    y = random.uniform(-10, 10) * .01
    aug.add(iaa.Affine(translate_percent={"x": x, "y": y},  # (-10, 10) -> (-1, 3)
                       scale=random.uniform(.7, 1.1),  # (.7, 1.1) -> (.99, 1.01)
                       rotate=random.uniform(-10, 10),  # (-10, 10) -> (-5, 5)
                       shear=random.uniform(-15, 15),  # (-15, 15) -> (.5, .5)
                       cval=(0, 255)))  # (0 ~ 255)

    """ random brightness """
    brightness = random.uniform(.5, 1.5)  # (.5, 1.5) -> (.995, 1.005)
    aug.add(iaa.Multiply(brightness))

    """ random pixel dropout """
    aug.add(iaa.CoarseDropout(p=.001, size_percent=0.005))
    aug.add(iaa.Dropout(p=(0, random.uniform(1, 5) * 0.005)))

    """ salt noise """
    aug.add(iaa.Salt(.001))

    """ additive gaussian noise """
    aug.add(iaa.AdditiveGaussianNoise(scale=random.uniform(.01, .1) * 255))

    """ crop """
    pixel = 5
    aug.add(iaa.Crop(px=((0, random.randint(0, pixel)), (0, random.randint(0, pixel)),
                         (0, random.randint(0, pixel)), (0, random.randint(0, pixel)))))

    seq_det = aug.to_deterministic()
    image_aug = []
    keys_aug = []

    for i in range(0, train_x.shape[0]):
        image = train_x[i, :, :, :]
        prob = train_y_prob[i, :]
        keys = train_y_keys[i, :]

        image_aug.append(seq_det.augment_images([image])[0])
        koi = ia.KeypointsOnImage([ia.Keypoint(x=keys[0], y=keys[1]),
                                   ia.Keypoint(x=keys[2], y=keys[3]),
                                   ia.Keypoint(x=keys[4], y=keys[5]),
                                   ia.Keypoint(x=keys[6], y=keys[7]),
                                   ia.Keypoint(x=keys[8], y=keys[9])], shape=image.shape)

        k = seq_det.augment_keypoints([koi])[0]
        k = k.keypoints
        keys = [k[0].x, k[0].y, k[1].x, k[1].y, k[2].x, k[2].y, k[3].x, k[3].y, k[4].x, k[4].y]

        index = 0
        prob = prob.T[0][:-1]  # last index is a direction parameter
        for j in range(0, len(prob)):
            keys[index] = keys[index] * prob[j]
            keys[index + 1] = keys[index + 1] * prob[j]
            index = index + 2
        keys_aug.append(keys)

    image_aug = np.asarray(image_aug)
    keys_aug = np.asarray(keys_aug)
    keys_aug = np.expand_dims(keys_aug, axis=-1)

    return image_aug, keys_aug
Ejemplo n.º 9
0
def test_keypoint_augmentation():
    reseed()

    keypoints = []
    for y in range(40//5):
        for x in range(60//5):
            keypoints.append(ia.Keypoint(x=x * 5, y=y * 5, vis=None, label=None))

    keypoints_oi = ia.KeypointsOnImage(keypoints, shape=(40, 60, 3))
    keypoints_oi_empty = ia.KeypointsOnImage([], shape=(40, 60, 3))

    augs = [
        iaa.Add((-5, 5), name="Add"),
        iaa.AddElementwise((-5, 5), name="AddElementwise"),
        iaa.AdditiveGaussianNoise(0.01*255, name="AdditiveGaussianNoise"),
        iaa.Multiply((0.95, 1.05), name="Multiply"),
        iaa.Dropout(0.01, name="Dropout"),
        iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
        iaa.Invert(0.01, per_channel=True, name="Invert"),
        iaa.ContrastNormalization((0.95, 1.05), name="ContrastNormalization"),
        iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"),
        iaa.AverageBlur((3, 5), name="AverageBlur"),
        iaa.MedianBlur((3, 5), name="MedianBlur"),
        # iaa.BilateralBlur((3, 5), name="BilateralBlur"),
        # WithColorspace ?
        # iaa.AddToHueAndSaturation((-5, 5), name="AddToHueAndSaturation"),
        # ChangeColorspace ?
        # Grayscale cannot be tested, input not RGB
        # Convolve ?
        iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
        iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
        iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
        iaa.DirectedEdgeDetect(alpha=(0.0, 0.1), direction=0, name="DirectedEdgeDetect"),
        iaa.Fliplr(0.5, name="Fliplr"),
        iaa.Flipud(0.5, name="Flipud"),
        iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
        iaa.Affine(translate_percent=(-0.05, 0.05), name="Affine-translate-percent"),
        iaa.Affine(rotate=(-20, 20), name="Affine-rotate"),
        iaa.Affine(shear=(-20, 20), name="Affine-shear"),
        iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"),
        iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"),
        # iaa.PerspectiveTransform(scale=(0.01, 0.10), name="PerspectiveTransform"),
        iaa.ElasticTransformation(alpha=(0.1, 0.2), sigma=(0.1, 0.2), name="ElasticTransformation"),
        # Sequential
        # SomeOf
        # OneOf
        # Sometimes
        # WithChannels
        # Noop
        # Lambda
        # AssertLambda
        # AssertShape
        iaa.Alpha((0.0, 0.1), iaa.Add(10), name="Alpha"),
        iaa.AlphaElementwise((0.0, 0.1), iaa.Add(10), name="AlphaElementwise"),
        iaa.SimplexNoiseAlpha(iaa.Add(10), name="SimplexNoiseAlpha"),
        iaa.FrequencyNoiseAlpha(exponent=(-2, 2), first=iaa.Add(10),
                                name="SimplexNoiseAlpha"),
        iaa.Superpixels(p_replace=0.01, n_segments=64),
        iaa.Resize(0.5, name="Resize"),
        iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
        iaa.Pad(px=(0, 10), name="Pad"),
        iaa.Crop(px=(0, 10), name="Crop")
    ]

    for aug in augs:
        dss = []
        for i in range(10):
            aug_det = aug.to_deterministic()

            kp_fully_empty_aug = aug_det.augment_keypoints([])
            assert kp_fully_empty_aug == []

            kp_first_empty_aug = aug_det.augment_keypoints([keypoints_oi_empty])[0]
            assert len(kp_first_empty_aug.keypoints) == 0

            kp_image = keypoints_oi.to_keypoint_image(size=5)
            kp_image_aug = aug_det.augment_image(kp_image)
            kp_image_aug_rev = ia.KeypointsOnImage.from_keypoint_image(
                kp_image_aug,
                if_not_found_coords={"x": -9999, "y": -9999},
                nb_channels=1
            )
            kp_aug = aug_det.augment_keypoints([keypoints_oi])[0]
            ds = []
            assert len(kp_image_aug_rev.keypoints) == len(kp_aug.keypoints),\
                "Lost keypoints for '%s' (%d vs expected %d)" \
                % (aug.name, len(kp_aug.keypoints), len(kp_image_aug_rev.keypoints))
            for kp_pred, kp_pred_img in zip(kp_aug.keypoints, kp_image_aug_rev.keypoints):
                kp_pred_lost = (kp_pred.x == -9999 and kp_pred.y == -9999)
                kp_pred_img_lost = (kp_pred_img.x == -9999 and kp_pred_img.y == -9999)

                if not kp_pred_lost and not kp_pred_img_lost:
                    d = np.sqrt((kp_pred.x - kp_pred_img.x) ** 2
                                + (kp_pred.y - kp_pred_img.y) ** 2)
                    ds.append(d)
            dss.extend(ds)
            if len(ds) == 0:
                print("[INFO] No valid keypoints found for '%s' "
                      "in test_keypoint_augmentation()" % (str(aug),))
        assert np.average(dss) < 5.0, \
            "Average distance too high (%.2f, with ds: %s)" \
            % (np.average(dss), str(dss))
Ejemplo n.º 10
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
    for xml_ in xmls:
        img_name = xml_[0:-4] + '.jpg'
        image = cv2.imread(os.path.join(img_dir, img_name))

        reader = PascalVocReader(os.path.join(xml_dir, xml_))
        #add by wz
        names = reader.getClasses()
        for name in names:
            if all_classes_num_counter[name] > augthresh:
                continue

        polygons = reader.getPolygons()
        keypoints = []
        for polygon in polygons:
            keypoints.extend(
                [ia.Keypoint(point[0], point[1]) for point in polygon])
        keypoints = [ia.KeypointsOnImage(keypoints, shape=image.shape)]

        # draw on image
        image_keypoints = keypoints[0].draw_on_image(image, size=5)
        seq = iaa.Sequential([
            iaa.Crop(px=(
                0, 16
            )),  # crop images from each side by 0 to 16px (randomly chosen)
            # iaa.Fliplr(0.5),  # horizontally flip 50% of the images
            # iaa.Affine(rotate=(-90,90)),
            iaa.Affine(
                scale={
                    "x": (0.8, 1.2),
                    "y": (0.8, 1.2)
                },
Ejemplo n.º 12
0
    def test_deepcopy_every_argument_provided(self):
        images = np.zeros((1, 1, 1, 3), dtype=np.uint8)
        heatmaps = [
            ia.HeatmapsOnImage(np.zeros((1, 1, 1), dtype=np.float32),
                               shape=(4, 4, 3))
        ]
        segmentation_maps = [
            ia.SegmentationMapsOnImage(np.zeros((1, 1), dtype=np.int32),
                                       shape=(5, 5, 3))
        ]
        keypoints = [
            ia.KeypointsOnImage([ia.Keypoint(x=1, y=2)], shape=(6, 6, 3))
        ]
        bounding_boxes = [
            ia.BoundingBoxesOnImage([ia.BoundingBox(x1=1, y1=2, x2=3, y2=4)],
                                    shape=(7, 7, 3))
        ]
        polygons = [
            ia.PolygonsOnImage([ia.Polygon([(0, 0), (10, 0), (10, 10)])],
                               shape=(100, 100, 3))
        ]
        line_strings = [
            ia.LineStringsOnImage([ia.LineString([(1, 1), (11, 1), (11, 11)])],
                                  shape=(101, 101, 3))
        ]
        data = {"test": 123, "foo": "bar", "test2": [1, 2, 3]}

        batch = ia.Batch(images=images,
                         heatmaps=heatmaps,
                         segmentation_maps=segmentation_maps,
                         keypoints=keypoints,
                         bounding_boxes=bounding_boxes,
                         polygons=polygons,
                         line_strings=line_strings,
                         data=data)
        observed = batch.deepcopy()

        for attr_name in observed.__dict__.keys():
            if "_unaug" not in attr_name and attr_name != "data":
                assert getattr(observed, attr_name) is None

        # must not be identical
        assert observed.images_unaug is not images
        assert observed.heatmaps_unaug is not heatmaps
        assert observed.segmentation_maps_unaug is not segmentation_maps
        assert observed.keypoints_unaug is not keypoints
        assert observed.bounding_boxes_unaug is not bounding_boxes
        assert observed.polygons_unaug is not polygons
        assert observed.line_strings_unaug is not line_strings
        assert observed.data is not data

        # verify that lists were not shallow-copied
        assert observed.heatmaps_unaug[0] is not heatmaps[0]
        assert observed.segmentation_maps_unaug[0] is not segmentation_maps[0]
        assert observed.keypoints_unaug[0] is not keypoints[0]
        assert observed.bounding_boxes_unaug[0] is not bounding_boxes[0]
        assert observed.polygons_unaug[0] is not polygons[0]
        assert observed.line_strings_unaug[0] is not line_strings[0]
        assert observed.data["test2"] is not data["test2"]

        # but must be equal
        assert ia.is_np_array(observed.images_unaug)
        assert observed.images_unaug.shape == (1, 1, 1, 3)
        assert isinstance(observed.heatmaps_unaug[0], ia.HeatmapsOnImage)
        assert isinstance(observed.segmentation_maps_unaug[0],
                          ia.SegmentationMapsOnImage)
        assert isinstance(observed.keypoints_unaug[0], ia.KeypointsOnImage)
        assert isinstance(observed.bounding_boxes_unaug[0],
                          ia.BoundingBoxesOnImage)
        assert isinstance(observed.polygons_unaug[0], ia.PolygonsOnImage)
        assert isinstance(observed.line_strings_unaug[0],
                          ia.LineStringsOnImage)
        assert isinstance(observed.data, dict)

        assert observed.heatmaps_unaug[0].shape == (4, 4, 3)
        assert observed.segmentation_maps_unaug[0].shape == (5, 5, 3)
        assert observed.keypoints_unaug[0].shape == (6, 6, 3)
        assert observed.bounding_boxes_unaug[0].shape == (7, 7, 3)
        assert observed.polygons_unaug[0].shape == (100, 100, 3)
        assert observed.line_strings_unaug[0].shape == (101, 101, 3)

        assert observed.heatmaps_unaug[0].arr_0to1.shape == (1, 1, 1)
        assert observed.segmentation_maps_unaug[0].arr.shape == (1, 1, 1)
        assert observed.keypoints_unaug[0].keypoints[0].x == 1
        assert observed.keypoints_unaug[0].keypoints[0].y == 2
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].x1 == 1
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].y1 == 2
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].x2 == 3
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].y2 == 4
        assert observed.polygons_unaug[0].polygons[0].exterior[0, 0] == 0
        assert observed.polygons_unaug[0].polygons[0].exterior[0, 1] == 0
        assert observed.polygons_unaug[0].polygons[0].exterior[1, 0] == 10
        assert observed.polygons_unaug[0].polygons[0].exterior[1, 1] == 0
        assert observed.polygons_unaug[0].polygons[0].exterior[2, 0] == 10
        assert observed.polygons_unaug[0].polygons[0].exterior[2, 1] == 10
        assert observed.line_strings_unaug[0].line_strings[0].coords[0, 0] == 1
        assert observed.line_strings_unaug[0].line_strings[0].coords[0, 1] == 1
        assert observed.line_strings_unaug[0].line_strings[0].coords[1,
                                                                     0] == 11
        assert observed.line_strings_unaug[0].line_strings[0].coords[1, 1] == 1
        assert observed.line_strings_unaug[0].line_strings[0].coords[2,
                                                                     0] == 11
        assert observed.line_strings_unaug[0].line_strings[0].coords[2,
                                                                     1] == 11

        assert observed.data["test"] == 123
        assert observed.data["foo"] == "bar"
        assert observed.data["test2"] == [1, 2, 3]
Ejemplo n.º 13
0
 def test_shift_on_x_axis_by_negative_amount(self):
     kp = ia.Keypoint(y=1, x=2)
     kp2 = self._func(kp, x=-1)
     assert kp2.y == 1
     assert kp2.x == 1
Ejemplo n.º 14
0
 def test_shift_on_x_axis(self):
     kp = ia.Keypoint(y=1, x=2)
     kp2 = self._func(kp, x=1)
     assert kp2.y == 1
     assert kp2.x == 3
Ejemplo n.º 15
0
    def __init__(self,
                 object_instance: Objects,
                 background_instance: Objects,
                 scene_dim={},
                 object_dim={},
                 overlap_ratio=0):
        """
            Initialization for Scene class

            :parameter
            ----------
            object_instance : Objects
                - Instance of object, used to generate object randomly.
            background_instance : Objects
                - Instance of the background object
            scene_dim : dict[width,height]
                - Dimension of the scene
            object_dim : dict[width,height]
                - Dimension of the object
            overlap_ratio : int
                - Parameter to set the area overlapping value.Need to adjusted accordingly.

        """
        self.object_instance = object_instance
        self.background_instance = background_instance

        self.scene_width = scene_dim['width']
        self.scene_height = scene_dim['height']
        self.object_width = object_dim['width']
        self.object_height = object_dim['height']

        # Get the center of scene based on the object size.
        # We are  centering so that after transforming the object
        # remains in the scene
        self.centerX = int((self.scene_width - self.object_width) / 2)
        self.centerY = int((self.scene_height - self.object_height) / 2)

        self.overlap_ratio = overlap_ratio

        # imgaug keypoints of the bounding box of a whole card
        self.card_key_point = imgaug.KeypointsOnImage(
            [
                imgaug.Keypoint(x=self.centerX, y=self.centerY),
                imgaug.Keypoint(x=self.centerX + self.object_width,
                                y=self.centerY),
                imgaug.Keypoint(x=self.centerX + self.object_width,
                                y=self.centerY + self.object_height),
                imgaug.Keypoint(x=self.centerX,
                                y=self.centerY + self.object_height)
            ],
            shape=(self.scene_height, self.scene_width, 3))

        # imgaug transformation for one card
        self.simple_transformations = imgaug.augmenters.Sequential([
            imgaug.augmenters.Affine(scale=[0.65, 1]),
            imgaug.augmenters.Affine(rotate=(-180, 180)),
            imgaug.augmenters.Affine(translate_percent={
                "x": (-0.25, 0.25),
                "y": (-0.25, 0.25)
            }),
        ])

        # imgaug transformation for the background
        self.scale_bg = imgaug.augmenters.Scale({
            "height": self.scene_height,
            "width": self.scene_width
        })

        # list to store all the final bounding boxes
        self.bounding_boxes = []
Ejemplo n.º 16
0
def test_determinism():
    reseed()

    images = [
        ia.quokka(size=(128, 128)),
        ia.quokka(size=(64, 64)),
        ia.imresize_single_image(skimage.data.astronaut(), (128, 256))
    ]
    images.extend([ia.quokka(size=(16, 16))] * 20)

    keypoints = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=20, y=10, vis=None, label=None), ia.Keypoint(x=5, y=5, vis=None, label=None),
            ia.Keypoint(x=10, y=43, vis=None, label=None)], shape=(50, 60, 3))
    ] * 20

    augs = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.WithColorspace("HSV", children=iaa.Add((-50, 50))),
        # iaa.WithChannels([0], iaa.Add((-50, 50))),
        # iaa.Noop(name="Noop-nochange"),
        # iaa.Lambda(
        #     func_images=lambda images, random_state, parents, hooks: images,
        #     func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: keypoints_on_images,
        #     name="Lambda-nochange"
        # ),
        # iaa.AssertLambda(
        #     func_images=lambda images, random_state, parents, hooks: True,
        #     func_keypoints=lambda keypoints_on_images, random_state, parents, hooks: True,
        #     name="AssertLambda-nochange"
        # ),
        # iaa.AssertShape(
        #     (None, None, None, 3),
        #     check_keypoints=False,
        #     name="AssertShape-nochange"
        # ),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Superpixels(p_replace=(0.25, 1.0), n_segments=(16, 128)),
        # iaa.ChangeColorspace(to_colorspace="GRAY"),
        iaa.Grayscale(alpha=(0.1, 1.0)),
        iaa.GaussianBlur((0.1, 3.0)),
        iaa.AverageBlur((3, 11)),
        iaa.MedianBlur((3, 11)),
        # iaa.Convolve(np.array([[0, 1, 0],
        #                       [1, -4, 1],
        #                       [0, 1, 0]])),
        iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0.8, 1.2)),
        iaa.Emboss(alpha=(0.1, 1.0), strength=(0.8, 1.2)),
        iaa.EdgeDetect(alpha=(0.1, 1.0)),
        iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0.0, 1.0)),
        iaa.Add((-50, 50)),
        iaa.AddElementwise((-50, 50)),
        iaa.AdditiveGaussianNoise(scale=(0.1, 1.0)),
        iaa.Multiply((0.6, 1.4)),
        iaa.MultiplyElementwise((0.6, 1.4)),
        iaa.Dropout((0.3, 0.5)),
        iaa.CoarseDropout((0.3, 0.5), size_percent=(0.05, 0.2)),
        iaa.Invert(0.5),
        iaa.ContrastNormalization((0.6, 1.4)),
        iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20), shear=(-20, 20), order=ia.ALL,
                   mode=ia.ALL, cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=0.5)
    ]

    augs_affect_geometry = [
        iaa.Sequential([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.SomeOf(1, [iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.OneOf([iaa.Fliplr(0.5), iaa.Flipud(0.5)]),
        iaa.Sometimes(0.5, iaa.Fliplr(1.0)),
        iaa.Resize((0.5, 0.9)),
        iaa.CropAndPad(px=(-50, 50)),
        iaa.Pad(px=(1, 50)),
        iaa.Crop(px=(1, 50)),
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(scale=(0.7, 1.3), translate_percent=(-0.1, 0.1),
                   rotate=(-20, 20), shear=(-20, 20), order=ia.ALL,
                   mode=ia.ALL, cval=(0, 255)),
        iaa.PiecewiseAffine(scale=(0.1, 0.3)),
        iaa.ElasticTransformation(alpha=(5, 100), sigma=(3, 5))
    ]

    for aug in augs:
        aug_det = aug.to_deterministic()
        images_aug1 = aug_det.augment_images(images)
        images_aug2 = aug_det.augment_images(images)

        aug_det = aug.to_deterministic()
        images_aug3 = aug_det.augment_images(images)
        images_aug4 = aug_det.augment_images(images)

        assert array_equal_lists(images_aug1, images_aug2), \
            "Images (1, 2) expected to be identical for %s" % (aug.name,)

        assert array_equal_lists(images_aug3, images_aug4), \
            "Images (3, 4) expected to be identical for %s" % (aug.name,)

        assert not array_equal_lists(images_aug1, images_aug3), \
            "Images (1, 3) expected to be different for %s" % (aug.name,)

    for aug in augs_affect_geometry:
        aug_det = aug.to_deterministic()
        kps_aug1 = aug_det.augment_keypoints(keypoints)
        kps_aug2 = aug_det.augment_keypoints(keypoints)

        aug_det = aug.to_deterministic()
        kps_aug3 = aug_det.augment_keypoints(keypoints)
        kps_aug4 = aug_det.augment_keypoints(keypoints)

        assert keypoints_equal(kps_aug1, kps_aug2), \
            "Keypoints (1, 2) expected to be identical for %s" % (aug.name,)

        assert keypoints_equal(kps_aug3, kps_aug4), \
            "Keypoints (3, 4) expected to be identical for %s" % (aug.name,)

        assert not keypoints_equal(kps_aug1, kps_aug3), \
            "Keypoints (1, 3) expected to be different for %s" % (aug.name,)
Ejemplo n.º 17
0
 def may_augment_poly(self, aug, img_shape, poly):
     keypoints = [imgaug.Keypoint(p[0], p[1]) for p in poly]
     keypoints = aug.augment_keypoints(
         [imgaug.KeypointsOnImage(keypoints, shape=img_shape)])[0].keypoints
     poly = [(p.x, p.y) for p in keypoints]
     return poly
Ejemplo n.º 18
0
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (HEIGHT, WIDTH))

    kps = []
    for y in range(NB_ROWS):
        ycoord = BB_Y1 + int(y * (BB_Y2 - BB_Y1) / (NB_COLS - 1))
        for x in range(NB_COLS):
            xcoord = BB_X1 + int(x * (BB_X2 - BB_X1) / (NB_ROWS - 1))
            kp = (xcoord, ycoord)
            kps.append(kp)
    kps = set(kps)
    kps = [ia.Keypoint(x=xcoord, y=ycoord) for (xcoord, ycoord) in kps]
    kps = ia.KeypointsOnImage(kps, shape=image.shape)

    bb = ia.BoundingBox(x1=BB_X1, x2=BB_X2, y1=BB_Y1, y2=BB_Y2)
    bbs = ia.BoundingBoxesOnImage([bb], shape=image.shape)

    pairs = []
    seqs = [
        iaa.AffineCv2(rotate=45),
        iaa.AffineCv2(translate_px=20),
        iaa.AffineCv2(translate_percent=0.1),
        iaa.AffineCv2(scale=1.2),
        iaa.AffineCv2(scale=0.8),
        iaa.AffineCv2(shear=45),
        iaa.AffineCv2(rotate=45, cval=256),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_CONSTANT),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REPLICATE),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REFLECT),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_REFLECT_101),
        iaa.AffineCv2(translate_px=20, mode=cv2.BORDER_WRAP),
        iaa.AffineCv2(translate_px=20, mode="constant"),
        iaa.AffineCv2(translate_px=20, mode="replicate"),
        iaa.AffineCv2(translate_px=20, mode="reflect"),
        iaa.AffineCv2(translate_px=20, mode="reflect_101"),
        iaa.AffineCv2(translate_px=20, mode="wrap"),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_NEAREST),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_LINEAR),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_CUBIC),
        iaa.AffineCv2(scale=0.5, order=cv2.INTER_LANCZOS4),
        iaa.AffineCv2(scale=0.5, order="nearest"),
        iaa.AffineCv2(scale=0.5, order="linear"),
        iaa.AffineCv2(scale=0.5, order="cubic"),
        iaa.AffineCv2(scale=0.5, order="lanczos4"),
        iaa.AffineCv2(rotate=45, translate_px=20, scale=1.2),
        iaa.AffineCv2(rotate=45, translate_px=20, scale=0.8),
        iaa.AffineCv2(rotate=(-45, 45),
                      translate_px=(-20, 20),
                      scale=(0.8, 1.2),
                      order=ia.ALL,
                      mode=ia.ALL,
                      cval=ia.ALL),
        iaa.AffineCv2(rotate=(-45, 45),
                      translate_px=(-20, 20),
                      scale=(0.8, 1.2),
                      order=ia.ALL,
                      mode=ia.ALL,
                      cval=ia.ALL),
        iaa.AffineCv2(rotate=(-45, 45),
                      translate_px=(-20, 20),
                      scale=(0.8, 1.2),
                      order=ia.ALL,
                      mode=ia.ALL,
                      cval=ia.ALL),
        iaa.AffineCv2(rotate=(-45, 45),
                      translate_px=(-20, 20),
                      scale=(0.8, 1.2),
                      order=ia.ALL,
                      mode=ia.ALL,
                      cval=ia.ALL)
    ]

    for seq in seqs:
        seq_det = seq.to_deterministic()
        image_aug = seq_det.augment_image(image)
        #print(image_aug.dtype, np.min(image_aug), np.max(image_aug))
        kps_aug = seq_det.augment_keypoints([kps])[0]
        bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

        image_before = np.copy(image)
        image_before = kps.draw_on_image(image_before)
        image_before = bbs.draw_on_image(image_before)

        image_after = np.copy(image_aug)
        image_after = kps_aug.draw_on_image(image_after)
        image_after = bbs_aug.draw_on_image(image_after)

        pairs.append(np.hstack((image_before, image_after)))

    misc.imshow(np.vstack(pairs))
    misc.imsave("affinecv2.jpg", np.vstack(pairs))
Ejemplo n.º 19
0
 def aug_img_keypoints(self, images, keypoints):
     '''
     images:[img1,img2]
     keypoints:[[[x1,y1],[x2,y2],...],[[x1,y1],[x2,y2],...]]]
     '''
     if not isinstance(images, list):
         images = [images]
     assert len(images) == len(
         keypoints), "images list should equal to keypoints list"
     keypoints_on_images = []
     images_aug_list = []
     keep_indx = []
     idx = 0
     for image, cur_points in zip(images, keypoints):
         height, width = image.shape[0:2]
         keypoints = []
         break_fg = 1
         for i in range(self.landmark_num):
             x = int(cur_points[i][0])
             y = int(cur_points[i][1])
             if x > width - 1 or y > height - 1:
                 break_fg = 0
             keypoints.append(ia.Keypoint(x=x, y=y))
         if not break_fg:
             continue
         images_aug_list.append(image)
         keypoints_on_images.append(
             ia.KeypointsOnImage(keypoints, shape=image.shape))
         keep_indx.append(idx)
         idx += 1
     seq = self.aug_seq()
     hook_activate = ia.HooksImages(activator=self.hook)
     seq_det = seq.to_deterministic(
     )  # call this for each batch again, NOT only once at the start
     # augment keypoints and images
     images_aug = seq_det.augment_images(images_aug_list,
                                         hooks=hook_activate)
     keypoints_aug = seq_det.augment_keypoints(keypoints_on_images,
                                               hooks=hook_activate)
     img_out = []
     keypoint_out = []
     for img_idx, (image_before, image_after, keypoints_before, keypoints_after) in \
                 enumerate(zip(images_aug_list, images_aug, keypoints_on_images, keypoints_aug)):
         if self.DEBUG:
             image_before = keypoints_before.draw_on_image(image_before)
             image_after = keypoints_after.draw_on_image(image_after)
             ia.show_grid([image_before, image_after], rows=1,
                          cols=2)  # before and after
         img_out.append(image_after)
         key_img = []
         for kp_idx, keypoint in enumerate(keypoints_after.keypoints):
             keypoint_old = keypoints_on_images[img_idx].keypoints[kp_idx]
             x_old, y_old = keypoint_old.x, keypoint_old.y
             x_new, y_new = keypoint.x, keypoint.y
             key_img.append([x_new, y_new])
             if self.DEBUG:
                 print("[Keypoints for image #%d] before aug: x=%d y=%d | \
                         after aug: x=%d y=%d" %
                       (img_idx, x_old, y_old, x_new, y_new))
         keypoint_out.append(key_img)
     return img_out, keypoint_out, keep_indx
Ejemplo n.º 20
0
def main():
    parser = argparse.ArgumentParser(description="Check augmenters visually.")
    parser.add_argument(
        "--only",
        default=None,
        help=
        "If this is set, then only the results of an augmenter with this name will be shown. "
        "Optionally, comma-separated list.",
        required=False)
    args = parser.parse_args()

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

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

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

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

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

    for augmenter in augmenters:
        if args.only is None or augmenter.name in [
                v.strip() for v in args.only.split(",")
        ]:
            print("Augmenter: %s" % (augmenter.name, ))
            grid = []
            for image, kps, bbs in zip(images, keypoints, bounding_boxes):
                aug_det = augmenter.to_deterministic()
                imgs_aug = aug_det.augment_images(
                    np.tile(image[np.newaxis, ...], (16, 1, 1, 1)))
                kps_aug = aug_det.augment_keypoints([kps] * 16)
                bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16)
                imgs_aug_drawn = [
                    kps_aug_one.draw_on_image(img_aug)
                    for img_aug, kps_aug_one in zip(imgs_aug, kps_aug)
                ]
                imgs_aug_drawn = [
                    bbs_aug_one.draw_on_image(img_aug)
                    for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug)
                ]
                grid.append(np.hstack(imgs_aug_drawn))
            ia.imshow(np.vstack(grid))
Ejemplo n.º 21
0
def test_GammaContrast():
    reseed()

    img = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    img = np.uint8(img)
    img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3))

    # check basic functionality with gamma=1 or 2 (deterministic) and per_chanenl on/off (makes
    # no difference due to deterministic gamma)
    for per_channel in [False, 0, 0.0, True, 1, 1.0]:
        for gamma in [1, 2]:
            aug = iaa.GammaContrast(gamma=iap.Deterministic(gamma),
                                    per_channel=per_channel)
            img_aug = aug.augment_image(img)
            img3d_aug = aug.augment_image(img3d)
            assert img_aug.dtype.type == np.uint8
            assert img3d_aug.dtype.type == np.uint8
            assert np.array_equal(
                img_aug, skimage.exposure.adjust_gamma(img, gamma=gamma))
            assert np.array_equal(
                img3d_aug, skimage.exposure.adjust_gamma(img3d, gamma=gamma))

    # check that tuple to uniform works
    aug = iaa.GammaContrast((1, 2))
    assert isinstance(aug.params1d[0], iap.Uniform)
    assert isinstance(aug.params1d[0].a, iap.Deterministic)
    assert isinstance(aug.params1d[0].b, iap.Deterministic)
    assert aug.params1d[0].a.value == 1
    assert aug.params1d[0].b.value == 2

    # check that list to choice works
    aug = iaa.GammaContrast([1, 2])
    assert isinstance(aug.params1d[0], iap.Choice)
    assert all([val in aug.params1d[0].a for val in [1, 2]])

    # check that per_channel at 50% prob works
    aug = iaa.GammaContrast((0.5, 2.0), per_channel=0.5)
    seen = [False, False]
    img1000d = np.zeros((1, 1, 1000), dtype=np.uint8) + 128
    for _ in sm.xrange(100):
        img_aug = aug.augment_image(img1000d)
        assert img_aug.dtype.type == np.uint8
        l = len(set(img_aug.flatten().tolist()))
        if l == 1:
            seen[0] = True
        else:
            seen[1] = True
        if all(seen):
            break
    assert all(seen)

    # check that keypoints are not changed
    kpsoi = ia.KeypointsOnImage([ia.Keypoint(1, 1)], shape=(3, 3, 3))
    kpsoi_aug = iaa.GammaContrast(gamma=2).augment_keypoints([kpsoi])
    assert keypoints_equal([kpsoi], kpsoi_aug)

    # check that heatmaps are not changed
    heatmaps = ia.HeatmapsOnImage(np.zeros((3, 3, 1), dtype=np.float32) + 0.5,
                                  shape=(3, 3, 3))
    heatmaps_aug = iaa.GammaContrast(gamma=2).augment_heatmaps([heatmaps])[0]
    assert np.allclose(heatmaps.arr_0to1, heatmaps_aug.arr_0to1)

    ###################
    # test other dtypes
    ###################
    # uint, int
    for dtype in [
            np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16,
            np.int32, np.int64
    ]:
        min_value, center_value, max_value = meta.get_value_range_of_dtype(
            dtype)

        exps = [1, 2, 3]
        values = [0, 100, int(center_value + 0.1 * max_value)]
        tolerances = [
            0, 0, 1e-8 * max_value if dtype in [np.uint64, np.int64] else 0
        ]

        for exp in exps:
            aug = iaa.GammaContrast(exp)
            for value, tolerance in zip(values, tolerances):
                image = np.full((3, 3), value, dtype=dtype)
                expected = (((image.astype(np.float128) / max_value)**exp) *
                            max_value).astype(dtype)
                image_aug = aug.augment_image(image)
                assert image_aug.dtype == np.dtype(dtype)
                assert len(np.unique(image_aug)) == 1
                value_aug = int(image_aug[0, 0])
                value_expected = int(expected[0, 0])
                diff = abs(value_aug - value_expected)
                assert diff <= tolerance

    # float
    for dtype in [np.float16, np.float32, np.float64]:

        def _allclose(a, b):
            atol = 1e-3 if dtype == np.float16 else 1e-8
            return np.allclose(a, b, atol=atol, rtol=0)

        exps = [1, 2]
        isize = np.dtype(dtype).itemsize
        values = [0, 1.0, 50.0, 100**(isize - 1)]

        for exp in exps:
            aug = iaa.GammaContrast(exp)
            for value in values:
                image = np.full((3, 3), value, dtype=dtype)
                expected = (image.astype(np.float128)**exp).astype(dtype)
                image_aug = aug.augment_image(image)
                assert image_aug.dtype == np.dtype(dtype)
                assert _allclose(image_aug, expected)
def main():
    # test 2d image
    misc.imshow(iaa.Scale(64).augment_image(data.camera()))

    # test many images
    images = [ia.quokka(size=0.5), ia.quokka(size=0.5)]
    images_aug = iaa.Scale(64).augment_images(images)
    misc.imshow(np.hstack(images_aug))

    image = ia.quokka(size=0.5)
    kps = [ia.KeypointsOnImage(
        [ia.Keypoint(x=245, y=203), ia.Keypoint(x=365, y=195), ia.Keypoint(x=313, y=269)],
        shape=(image.shape[0]*2, image.shape[1]*2)
    )]
    kps[0] = kps[0].on(image.shape)
    print("image shape:", image.shape)

    augs = [
        iaa.Scale("keep", name="keep"),
        iaa.Scale(32, name="i32"),
        iaa.Scale(0.5, name="f05"),

        iaa.Scale({"height": 32}, name="height32"),
        iaa.Scale({"width": 32}, name="width32"),
        iaa.Scale({"height": "keep", "width": 32}, name="keep-width32"),
        iaa.Scale({"height": 32, "width": "keep"}, name="height32-keep"),
        iaa.Scale({"height": "keep", "width": "keep"}, name="keep-keep"),
        iaa.Scale({"height": 32, "width": 64}, name="height32width64"),
        iaa.Scale({"height": 64, "width": "keep-aspect-ratio"}, name="height64width-kar"),
        iaa.Scale({"height": "keep-aspect-ratio", "width": 64}, name="height-kar_width64")
    ]

    augs_many = [
        iaa.Scale((32, 128), name="tuple-32-128"),
        iaa.Scale([32, 64, 128], name="list-32-64-128"),
        iaa.Scale({"height": (32, 128), "width": "keep"}, name="height-32-64_width-keep"),
        iaa.Scale({"height": (32, 128), "width": "keep-aspect-ratio"}, name="height-32-128_width-kar"),
        iaa.Scale({"height": (32, 128), "width": (32, 128)}, name="height-32-128_width-32-128")
    ]

    print("original", image.shape)
    misc.imshow(kps[0].draw_on_image(image))

    print("-----------------")
    print("Same size per image")
    print("-----------------")
    for aug in augs:
        img_aug = aug.augment_image(image)
        kps_aug = aug.augment_keypoints(kps)[0]
        img_aug_kps = kps_aug.draw_on_image(img_aug)
        print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0])
        misc.imshow(img_aug_kps)

    print("-----------------")
    print("Random per image")
    print("-----------------")
    for aug in augs_many:
        images_aug = []
        for _ in range(64):
            aug_det = aug.to_deterministic()
            img_aug = aug_det.augment_image(image)
            kps_aug = aug_det.augment_keypoints(kps)[0]
            img_aug_kps = kps_aug.draw_on_image(img_aug)
            #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0])
            images_aug.append(img_aug_kps)
        print(aug.name)
        misc.imshow(ia.draw_grid(images_aug))

    print("nearest/cv2.INTER_NEAREST/cubic")
    misc.imshow(np.hstack([
        iaa.Scale(64, interpolation="nearest").augment_image(image),
        iaa.Scale(64, interpolation=cv2.INTER_NEAREST).augment_image(image),
        iaa.Scale(64, interpolation="cubic").augment_image(image)
    ]))

    print("random nearest/cubic")
    iaa.Scale(64, interpolation=["nearest", "cubic"]).show_grid([image], 8, 8)
Ejemplo n.º 23
0
def main():
    image = ia.quokka(size=0.5)
    kps = [ia.KeypointsOnImage(
        [ia.Keypoint(x=245, y=203, vis=None, label=None), ia.Keypoint(x=365, y=195, vis=None, label=None),
         ia.Keypoint(x=313, y=269, vis=None, label=None)],
        shape=(image.shape[0]*2, image.shape[1]*2)
    )]
    kps[0] = kps[0].on(image.shape)
    print("image shape:", image.shape)

    augs = [
        iaa.CropAndPad(px=50, name="pad-by-50px"),
        iaa.CropAndPad(px=(10, 20, 30, 40), name="pad-by-10-20-30-40px"),
        iaa.CropAndPad(percent=0.1, name="pad-by-01percent"),
        iaa.CropAndPad(percent=(0.01, 0.02, 0.03, 0.04), name="pad-by-001-002-003-004percent"),
        iaa.CropAndPad(px=-20, name="crop-by-20px"),
        iaa.CropAndPad(px=(-10, -20, -30, -40), name="crop-by-10-20-30-40px"),
        iaa.CropAndPad(percent=-0.1, name="crop-by-01percent"),
        iaa.CropAndPad(percent=(-0.01, -0.02, -0.03, -0.04), name="crop-by-001-002-003-004percent")
    ]

    augs_many = [
        iaa.Crop(px=(0, 50), name="native-crop-0-to-50px"),
        iaa.Crop(px=iap.DiscreteUniform(0, 50), name="native-crop-0-to-50px-iap"),
        iaa.Pad(px=(0, 50), pad_mode="linear_ramp", pad_cval=(0, 255), name="native-pad-0-to-50px-pad-modes"),
        iaa.CropAndPad(px=(0, 50), sample_independently=False, name="pad-by-0-to-50px-same"),
        iaa.CropAndPad(px=(0, 50), name="pad-by-0-to-50px"),
        iaa.CropAndPad(px=(0, 50), pad_mode=ia.ALL, pad_cval=(0, 255), name="pad-by-0-to-50px-random-pad-modes-cvals"),
        iaa.CropAndPad(px=((0, 50), (0, 50), (0, 50), (0, 50)), name="pad-by-0-to-50px-each"),
        iaa.CropAndPad(percent=(0, 0.1), sample_independently=False, name="pad-by-0-to-01percent-same"),
        iaa.CropAndPad(percent=(0, 0.1), name="pad-by-0-to-01percent"),
        iaa.CropAndPad(percent=(0, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255),
                       name="pad-by-0-to-01percent-random-pad-modes-cvals"),
        iaa.CropAndPad(percent=((0, 0.1), (0, 0.1), (0, 0.1), (0, 0.1)), name="pad-by-0-to-01percent-each"),
        iaa.CropAndPad(px=(-50, 0), name="crop-by-50-to-0px"),
        iaa.CropAndPad(px=((-50, 0), (-50, 0), (-50, 0), (-50, 0)), name="crop-by-50-to-0px-each"),
        iaa.CropAndPad(percent=(-0.1, 0), name="crop-by-01-to-0percent"),
        iaa.CropAndPad(percent=((-0.1, 0), (-0.1, 0), (-0.1, 0), (-0.1, 0)), name="crop-by-01-to-0percent-each"),
        iaa.CropAndPad(px=(-50, 50), name="pad-and-crop-by-50px")
    ]

    print("original", image.shape)
    ia.imshow(kps[0].draw_on_image(image))

    print("-----------------")
    print("Same aug per image")
    print("-----------------")
    for aug in augs:
        img_aug = aug.augment_image(image)
        kps_aug = aug.augment_keypoints(kps)[0]
        img_aug_kps = kps_aug.draw_on_image(img_aug)
        print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0])
        ia.imshow(img_aug_kps)

    print("-----------------")
    print("Random aug per image")
    print("-----------------")
    for aug in augs_many:
        images_aug = []
        for _ in range(64):
            aug_det = aug.to_deterministic()
            img_aug = aug_det.augment_image(image)
            kps_aug = aug_det.augment_keypoints(kps)[0]
            img_aug_kps = kps_aug.draw_on_image(img_aug)
            img_aug_kps = np.pad(img_aug_kps, ((1, 1), (1, 1), (0, 0)), mode="constant", constant_values=255)
            images_aug.append(img_aug_kps)
        print(aug.name)
        ia.imshow(ia.draw_grid(images_aug))
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (128, 128))

    # check if scipy and cv2 remap similarly
    rs = ia.new_random_state(1)
    aug_scipy = ElasticTransformationScipy(alpha=30,
                                           sigma=3,
                                           random_state=rs,
                                           deterministic=True)
    aug_cv2 = ElasticTransformationCv2(alpha=30,
                                       sigma=3,
                                       random_state=rs,
                                       deterministic=True)
    augs_scipy = aug_scipy.augment_images([image] * 8)
    augs_cv2 = aug_cv2.augment_images([image] * 8)
    ia.imshow(ia.draw_grid(augs_scipy + augs_cv2, rows=2))

    print("alpha=vary, sigma=0.25")
    augs = [
        iaa.ElasticTransformation(alpha=alpha, sigma=0.25)
        for alpha in np.arange(0.0, 50.0, 0.1)
    ]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=vary, sigma=1.0")
    augs = [
        iaa.ElasticTransformation(alpha=alpha, sigma=1.0)
        for alpha in np.arange(0.0, 50.0, 0.1)
    ]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=vary, sigma=3.0")
    augs = [
        iaa.ElasticTransformation(alpha=alpha, sigma=3.0)
        for alpha in np.arange(0.0, 50.0, 0.1)
    ]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=vary, sigma=5.0")
    augs = [
        iaa.ElasticTransformation(alpha=alpha, sigma=5.0)
        for alpha in np.arange(0.0, 50.0, 0.1)
    ]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=1.0, sigma=vary")
    augs = [
        iaa.ElasticTransformation(alpha=1.0, sigma=sigma)
        for sigma in np.arange(0.0, 50.0, 0.1)
    ]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    print("alpha=10.0, sigma=vary")
    augs = [
        iaa.ElasticTransformation(alpha=10.0, sigma=sigma)
        for sigma in np.arange(0.0, 50.0, 0.1)
    ]
    images_aug = [aug.augment_image(image) for aug in augs]
    ia.imshow(ia.draw_grid(images_aug, cols=10))

    kps = ia.KeypointsOnImage([
        ia.Keypoint(x=1, y=1),
        ia.Keypoint(x=50, y=24),
        ia.Keypoint(x=42, y=96),
        ia.Keypoint(x=88, y=106),
        ia.Keypoint(x=88, y=53),
        ia.Keypoint(x=0, y=0),
        ia.Keypoint(x=128, y=128),
        ia.Keypoint(x=-20, y=30),
        ia.Keypoint(x=20, y=-30),
        ia.Keypoint(x=-20, y=-30)
    ],
                              shape=image.shape)

    images = []
    params = [(0.0, 0.0), (0.2, 0.2), (2.0, 0.25), (0.25, 3.0), (2.0, 3.0),
              (6.0, 3.0), (12.0, 3.0), (50.0, 5.0), (100.0, 5.0),
              (100.0, 10.0)]

    for (alpha, sigma) in params:
        images_row = []
        seqs_row = [
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=0,
                                      order=0),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=128,
                                      order=0),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=255,
                                      order=0),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=0,
                                      order=1),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=128,
                                      order=1),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=255,
                                      order=1),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=0,
                                      order=3),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=128,
                                      order=3),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="constant",
                                      cval=255,
                                      order=3),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="nearest",
                                      order=0),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="nearest",
                                      order=1),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="nearest",
                                      order=2),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="nearest",
                                      order=3),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="reflect",
                                      order=0),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="reflect",
                                      order=1),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="reflect",
                                      order=2),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="reflect",
                                      order=3),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="wrap",
                                      order=0),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="wrap",
                                      order=1),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="wrap",
                                      order=2),
            iaa.ElasticTransformation(alpha=alpha,
                                      sigma=sigma,
                                      mode="wrap",
                                      order=3)
        ]

        for seq in seqs_row:
            seq_det = seq.to_deterministic()
            image_aug = seq_det.augment_image(image)
            kps_aug = seq_det.augment_keypoints([kps])[0]
            image_aug_kp = np.copy(image_aug)
            image_aug_kp = kps_aug.draw_on_image(image_aug_kp, size=3)
            images_row.append(image_aug_kp)

        images.append(np.hstack(images_row))

    ia.imshow(np.vstack(images))
    imageio.imwrite("elastic_transformations.jpg", np.vstack(images))
Ejemplo n.º 25
0
def test_AlphaElementwise():
    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.AlphaElementwise(1, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = base_img + 10
    assert np.allclose(observed, expected)

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

    aug = iaa.AlphaElementwise(0, iaa.Add(10), iaa.Add(20))
    observed = aug.augment_image(base_img)
    expected = base_img + 20
    assert np.allclose(observed, expected)

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

    aug = iaa.AlphaElementwise(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.AlphaElementwise(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.AlphaElementwise(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((100, 100), dtype=np.uint8)
    aug = iaa.AlphaElementwise((0.0, 1.0), iaa.Add(10), iaa.Add(110))
    observed = (aug.augment_image(base_img) - 10) / 100
    nb_bins = 10
    hist, _ = np.histogram(observed.flatten(),
                           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 / observed.size
        assert density_expected - density_tolerance < density < density_expected + density_tolerance

    base_img = np.zeros((1, 1, 100), dtype=np.uint8)
    aug = iaa.AlphaElementwise((0.0, 1.0),
                               iaa.Add(10),
                               iaa.Add(110),
                               per_channel=True)
    observed = aug.augment_image(base_img)
    assert len(set(observed.flatten())) > 1

    # propagating
    aug = iaa.AlphaElementwise(0.5,
                               iaa.Add(100),
                               iaa.Add(50),
                               name="AlphaElementwiseTest")

    def propagator(images, augmenter, parents, default):
        if "AlphaElementwise" 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)

    # -----
    # heatmaps and per_channel
    # -----
    class _DummyMaskParameter(iap.StochasticParameter):
        def __init__(self, inverted=False):
            super(_DummyMaskParameter, self).__init__()
            self.nb_calls = 0
            self.inverted = inverted

        def _draw_samples(self, size, random_state):
            self.nb_calls += 1
            h, w = size
            ones = np.ones((h, w), dtype=np.float32)
            zeros = np.zeros((h, w), dtype=np.float32)
            if self.nb_calls == 1:
                return zeros if not self.inverted else ones
            elif self.nb_calls in [2, 3]:
                return ones if not self.inverted else zeros
            else:
                assert False

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

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

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

    aug = iaa.AlphaElementwise(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.AlphaElementwise(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.AlphaElementwise(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.AlphaElementwise(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.AlphaElementwise(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.AlphaElementwise(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])
    """
    TODO this test currently doesn't work as AlphaElementwise augments keypoints without sampling
    overlay factors per (x, y) location. (i.e. similar behaviour to Alpha)

    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_both_shifted = kpsoi.shift(x=1)
    expected_first_shifted = KeypointsOnImage([kps[0].shift(x=1), kps[1]], shape=kpsoi.shape)
    expected_second_shifted = KeypointsOnImage([kps[0], kps[1].shift(x=1)], shape=kpsoi.shape)
    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_both_shifted]):
            seen[1] += 1
        elif keypoints_equal([observed], [expected_first_shifted]):
            seen[2] += 1
        elif keypoints_equal([observed], [expected_second_shifted]):
            seen[3] += 1
        else:
            assert False
    assert 100 - 50 < seen[0] < 100 + 50
    assert 100 - 50 < seen[1] < 100 + 50
    """

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

    def propagator(kpsoi_to_aug, augmenter, parents, default):
        if "AlphaElementwise" 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])
Ejemplo n.º 26
0
    def test_deepcopy(self):
        batch = ia.Batch()
        observed = batch.deepcopy()
        keys = list(observed.__dict__.keys())
        assert len(keys) >= 14
        for attr_name in keys:
            assert getattr(observed, attr_name) is None

        batch = ia.Batch(images=np.zeros((1, 1, 3), dtype=np.uint8))
        observed = batch.deepcopy()
        for attr_name in observed.__dict__.keys():
            if attr_name != "images_unaug":
                assert getattr(observed, attr_name) is None
        assert ia.is_np_array(observed.images_unaug)

        batch = ia.Batch(
            images=np.zeros((1, 1, 3), dtype=np.uint8),
            heatmaps=[
                ia.HeatmapsOnImage(np.zeros((1, 1, 1), dtype=np.float32),
                                   shape=(4, 4, 3))
            ],
            segmentation_maps=[
                ia.SegmentationMapOnImage(np.zeros((1, 1), dtype=np.int32),
                                          shape=(5, 5, 3),
                                          nb_classes=20)
            ],
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(x=1, y=2)], shape=(6, 6, 3))
            ],
            bounding_boxes=[
                ia.BoundingBoxesOnImage(
                    [ia.BoundingBox(x1=1, y1=2, x2=3, y2=4)], shape=(7, 7, 3))
            ],
            polygons=[
                ia.PolygonsOnImage([ia.Polygon([(0, 0), (10, 0), (10, 10)])],
                                   shape=(100, 100, 3))
            ],
            line_strings=[
                ia.LineStringsOnImage(
                    [ia.LineString([(1, 1), (11, 1), (11, 11)])],
                    shape=(101, 101, 3))
            ],
            data={
                "test": 123,
                "foo": "bar",
                "test2": [1, 2, 3]
            })
        observed = batch.deepcopy()
        for attr_name in observed.__dict__.keys():
            if "_unaug" not in attr_name and attr_name != "data":
                assert getattr(observed, attr_name) is None

        assert ia.is_np_array(observed.images_unaug)
        assert observed.images_unaug.shape == (1, 1, 3)
        assert isinstance(observed.heatmaps_unaug[0], ia.HeatmapsOnImage)
        assert isinstance(observed.segmentation_maps_unaug[0],
                          ia.SegmentationMapOnImage)
        assert isinstance(observed.keypoints_unaug[0], ia.KeypointsOnImage)
        assert isinstance(observed.bounding_boxes_unaug[0],
                          ia.BoundingBoxesOnImage)
        assert isinstance(observed.polygons_unaug[0], ia.PolygonsOnImage)
        assert isinstance(observed.line_strings_unaug[0],
                          ia.LineStringsOnImage)
        assert isinstance(observed.data, dict)

        assert observed.heatmaps_unaug[0].shape == (4, 4, 3)
        assert observed.segmentation_maps_unaug[0].shape == (5, 5, 3)
        assert observed.keypoints_unaug[0].shape == (6, 6, 3)
        assert observed.bounding_boxes_unaug[0].shape == (7, 7, 3)
        assert observed.polygons_unaug[0].shape == (100, 100, 3)
        assert observed.line_strings_unaug[0].shape == (101, 101, 3)

        assert observed.heatmaps_unaug[0].arr_0to1.shape == (1, 1, 1)
        assert observed.segmentation_maps_unaug[0].arr.shape == (1, 1, 20)
        assert observed.keypoints_unaug[0].keypoints[0].x == 1
        assert observed.keypoints_unaug[0].keypoints[0].y == 2
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].x1 == 1
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].y1 == 2
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].x2 == 3
        assert observed.bounding_boxes_unaug[0].bounding_boxes[0].y2 == 4
        assert observed.polygons_unaug[0].polygons[0].exterior[0, 0] == 0
        assert observed.polygons_unaug[0].polygons[0].exterior[0, 1] == 0
        assert observed.polygons_unaug[0].polygons[0].exterior[1, 0] == 10
        assert observed.polygons_unaug[0].polygons[0].exterior[1, 1] == 0
        assert observed.polygons_unaug[0].polygons[0].exterior[2, 0] == 10
        assert observed.polygons_unaug[0].polygons[0].exterior[2, 1] == 10
        assert observed.line_strings_unaug[0].line_strings[0].coords[0, 0] == 1
        assert observed.line_strings_unaug[0].line_strings[0].coords[0, 1] == 1
        assert observed.line_strings_unaug[0].line_strings[0].coords[1,
                                                                     0] == 11
        assert observed.line_strings_unaug[0].line_strings[0].coords[1, 1] == 1
        assert observed.line_strings_unaug[0].line_strings[0].coords[2,
                                                                     0] == 11
        assert observed.line_strings_unaug[0].line_strings[0].coords[2,
                                                                     1] == 11

        assert observed.data["test"] == 123
        assert observed.data["foo"] == "bar"
        assert observed.data["test2"] == [1, 2, 3]
Ejemplo n.º 27
0
    def test_many_augmenters(self):
        keypoints = []
        for y in sm.xrange(40 // 5):
            for x in sm.xrange(60 // 5):
                keypoints.append(ia.Keypoint(y=y * 5, x=x * 5))

        keypoints_oi = ia.KeypointsOnImage(keypoints, shape=(40, 60, 3))
        keypoints_oi_empty = ia.KeypointsOnImage([], shape=(40, 60, 3))

        augs = [
            iaa.Add((-5, 5), name="Add"),
            iaa.AddElementwise((-5, 5), name="AddElementwise"),
            iaa.AdditiveGaussianNoise(0.01 * 255,
                                      name="AdditiveGaussianNoise"),
            iaa.Multiply((0.95, 1.05), name="Multiply"),
            iaa.Dropout(0.01, name="Dropout"),
            iaa.CoarseDropout(0.01, size_px=6, name="CoarseDropout"),
            iaa.Invert(0.01, per_channel=True, name="Invert"),
            iaa.GaussianBlur(sigma=(0.95, 1.05), name="GaussianBlur"),
            iaa.AverageBlur((3, 5), name="AverageBlur"),
            iaa.MedianBlur((3, 5), name="MedianBlur"),
            iaa.Sharpen((0.0, 0.1), lightness=(1.0, 1.2), name="Sharpen"),
            iaa.Emboss(alpha=(0.0, 0.1), strength=(0.5, 1.5), name="Emboss"),
            iaa.EdgeDetect(alpha=(0.0, 0.1), name="EdgeDetect"),
            iaa.DirectedEdgeDetect(alpha=(0.0, 0.1),
                                   direction=0,
                                   name="DirectedEdgeDetect"),
            iaa.Fliplr(0.5, name="Fliplr"),
            iaa.Flipud(0.5, name="Flipud"),
            iaa.Affine(translate_px=(-5, 5), name="Affine-translate-px"),
            iaa.Affine(translate_percent=(-0.05, 0.05),
                       name="Affine-translate-percent"),
            iaa.Affine(rotate=(-20, 20), name="Affine-rotate"),
            iaa.Affine(shear=(-20, 20), name="Affine-shear"),
            iaa.Affine(scale=(0.9, 1.1), name="Affine-scale"),
            iaa.PiecewiseAffine(scale=(0.001, 0.005), name="PiecewiseAffine"),
            iaa.ElasticTransformation(alpha=(0.1, 0.2),
                                      sigma=(0.1, 0.2),
                                      name="ElasticTransformation"),
            iaa.BlendAlpha((0.0, 0.1), iaa.Add(10), name="BlendAlpha"),
            iaa.BlendAlphaElementwise((0.0, 0.1),
                                      iaa.Add(10),
                                      name="BlendAlphaElementwise"),
            iaa.BlendAlphaSimplexNoise(iaa.Add(10),
                                       name="BlendAlphaSimplexNoise"),
            iaa.BlendAlphaFrequencyNoise(exponent=(-2, 2),
                                         foreground=iaa.Add(10),
                                         name="BlendAlphaSimplexNoise"),
            iaa.Superpixels(p_replace=0.01, n_segments=64),
            iaa.Resize(0.5, name="Resize"),
            iaa.CropAndPad(px=(-10, 10), name="CropAndPad"),
            iaa.Pad(px=(0, 10), name="Pad"),
            iaa.Crop(px=(0, 10), name="Crop")
        ]

        for aug in augs:
            dss = []
            for i in sm.xrange(10):
                aug_det = aug.to_deterministic()

                kp_fully_empty_aug = aug_det.augment_keypoints([])
                assert kp_fully_empty_aug == []

                kp_first_empty_aug = aug_det.augment_keypoints(
                    keypoints_oi_empty)
                assert len(kp_first_empty_aug.keypoints) == 0

                kp_image = keypoints_oi.to_keypoint_image(size=5)
                with assertWarns(self, iaa.SuspiciousSingleImageShapeWarning):
                    kp_image_aug = aug_det.augment_image(kp_image)
                kp_image_aug_rev = ia.KeypointsOnImage.from_keypoint_image(
                    kp_image_aug,
                    if_not_found_coords={
                        "x": -9999,
                        "y": -9999
                    },
                    nb_channels=1)
                kp_aug = aug_det.augment_keypoints([keypoints_oi])[0]
                ds = []
                assert len(kp_image_aug_rev.keypoints) == len(
                    kp_aug.keypoints), (
                        "Lost keypoints for '%s' (%d vs expected %d)" %
                        (aug.name, len(kp_aug.keypoints),
                         len(kp_image_aug_rev.keypoints)))

                gen = zip(kp_aug.keypoints, kp_image_aug_rev.keypoints)
                for kp_pred, kp_pred_img in gen:
                    kp_pred_lost = (kp_pred.x == -9999 and kp_pred.y == -9999)
                    kp_pred_img_lost = (kp_pred_img.x == -9999
                                        and kp_pred_img.y == -9999)

                    if not kp_pred_lost and not kp_pred_img_lost:
                        d = np.sqrt((kp_pred.x - kp_pred_img.x)**2 +
                                    (kp_pred.y - kp_pred_img.y)**2)
                        ds.append(d)
                dss.extend(ds)
                if len(ds) == 0:
                    print("[INFO] No valid keypoints found for '%s' "
                          "in test_keypoint_augmentation()" % (str(aug), ))
            assert np.average(dss) < 5.0, \
                "Average distance too high (%.2f, with ds: %s)" \
                % (np.average(dss), str(dss))
Ejemplo n.º 28
0
images = images[0:4] / 255

heatmaps = np.load("heatmap8_s3_128_R.npy")
heatmaps = heatmaps[0:4, :, :, 0:3]

# Generate random keypoints.
# The augmenters expect a list of imgaug.KeypointsOnImage.
i = 0
keypoints_on_images = []
for image in images:
    height, width = image.shape[0:2]
    keypoints = []
    for k in range(8):
        x = points[i][k][0]
        y = points[i][k][1]
        keypoints.append(ia.Keypoint(x=x, y=y))
    keypoints_on_images.append(
        ia.KeypointsOnImage(keypoints, shape=image.shape))
    i += 1

seq = iaa.Sequential([
    # iaa.GaussianBlur((0, 0.1)),
    iaa.Crop(px=(0, 10)
             ),  # crop images from each side by 0 to 16px (randomly chosen)
    iaa.Fliplr(0.5),  # horizontally flip 50% of the images
    iaa.Affine(scale=(0.75, 1.25),
               translate_percent={
                   "x": (-0.25, 0.25),
                   "y": (-0.25, 0.25)
               },
               rotate=(-30, 30)),
def draw_per_augmenter_images():
    print("[draw_per_augmenter_images] Loading image...")
    image = misc.imresize(
        ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))
    #image = misc.imresize(data.chelsea()[0:300, 50:350, :], (128, 128))
    #image = misc.imresize(data.astronaut(), (128, 128))

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

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

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

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

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

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

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

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

    fig.savefig("examples.jpg", bbox_inches="tight")
Ejemplo n.º 30
0
 def test_width(self):
     kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
     kpi = ia.KeypointsOnImage(keypoints=kps, shape=(10, 20, 3))
     assert kpi.width == 20