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

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

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

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

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

    # Sample sigma from a discrete uniform distribution of range 1 <= sigma <= 5,
    # i.e. sigma will have any of the following values: 1, 2, 3, 4, 5.
    blurer = iaa.GaussianBlur(iap.DiscreteUniform(1, 5))
    images_aug = blurer.augment_images(images)
def draw_single_sequential_images():
    image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128))

    st = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential([
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5),
            st(iaa.Crop(percent=(0, 0.1))),
            st(iaa.GaussianBlur((0, 3.0))),
            st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5)),
            st(iaa.Dropout((0.0, 0.1), per_channel=0.5)),
            st(iaa.Add((-10, 10), per_channel=0.5)),
            st(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
            st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)),
            st(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, 1.0),
                mode=ia.ALL
            )),
            st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25))
        ],
        random_order=True
    )

    grid = seq.draw_grid(image, cols=8, rows=8)
    misc.imsave("examples_grid.jpg", grid)
Esempio n. 3
0
def example_single_augmenters():
    print("Example: Single Augmenters")
    #from imgaug import augmenters as iaa
    import augmenters as iaa
    images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

    flipper = iaa.Fliplr(1.0)  # always horizontally flip each input image
    images[0] = flipper.augment_image(images[0])  # horizontally flip image 0

    vflipper = iaa.Flipud(
        0.9)  # vertically flip each input image with 90% probability
    images[1] = vflipper.augment_image(
        images[1])  # probably vertically flip image 1

    blurer = iaa.GaussianBlur(3.0)
    images[2] = blurer.augment_image(
        images[2])  # blur image 2 by a sigma of 3.0
    images[3] = blurer.augment_image(
        images[3])  # blur image 3 by a sigma of 3.0 too

    translater = iaa.Affine(
        translate_px={"x": -16})  # move each input image by 16px to the left
    images[4] = translater.augment_image(images[4])  # move image 4 to the left

    scaler = iaa.Affine(
        scale={"y":
               (0.8, 1.2)})  # scale each input image to 80-120% on the y axis
    images[5] = scaler.augment_image(
        images[5])  # scale image 5 by 80-120% on the y axis
Esempio n. 4
0
def example_determinism():
    print("Example: Determinism")
    #from imgaug import augmenters as iaa
    import augmenters as iaa

    # Standard scenario: You have N RGB-images and additionally 21 heatmaps per image.
    # You want to augment each image and its heatmaps identically.
    images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
    heatmaps = np.random.randint(0, 255, (16, 128, 128, 21), dtype=np.uint8)

    seq = iaa.Sequential([
        iaa.GaussianBlur((0, 3.0)),
        iaa.Affine(translate_px={"x": (-40, 40)})
    ])

    # Convert the stochastic sequence of augmenters to a deterministic one.
    # The deterministic sequence will always apply the exactly same effects to the images.
    seq_det = seq.to_deterministic(
    )  # call this for each batch again, NOT only once at the start
    images_aug = seq_det.augment_images(images)
    heatmaps_aug = seq_det.augment_images(heatmaps)

    # -----
    # Make sure that the example really does something
    import imgaug as ia
    assert not np.array_equal(images, images_aug)
    assert not np.array_equal(heatmaps, heatmaps_aug)
    images_show = []
    for img_idx in range(len(images)):
        images_show.extend([
            images[img_idx], images_aug[img_idx], heatmaps[img_idx][..., 0:3],
            heatmaps_aug[img_idx][..., 0:3]
        ])
    ia.show_grid(images_show, cols=4)
Esempio n. 5
0
def main():
    images = [
        misc.imresize(
            ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)),
        misc.imresize(data.astronaut(), (128, 128))
    ]

    augmenters = [
        iaa.Noop(name="Noop"),
        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.Grayscale(0.5, name="Grayscale0.5"),
        iaa.Grayscale(1.0, name="Grayscale1.0"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1),
                                  name="AdditiveGaussianNoise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0)),
        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, 1.0),
                   mode=ia.ALL,
                   name="Affine"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0)
    ]

    #for i, aug in enumerate(augmenters):
    #print(i)
    #aug.deepcopy()
    #import copy
    #copy.deepcopy(aug)
    seq = iaa.Sequential([aug.copy() for aug in augmenters], name="Sequential")
    st = iaa.Sometimes(0.5, seq.copy(), name="Sometimes")
    augmenters.append(seq)
    augmenters.append(st)

    for augmenter in augmenters:
        print("Augmenter: %s" % (augmenter.name, ))
        grid = augmenter.draw_grid(images, rows=1, cols=16)
        misc.imshow(grid)
Esempio n. 6
0
def example_grayscale():
    print("Example: Grayscale")
    #from imgaug import augmenters as iaa
    import augmenters as iaa
    images = np.random.randint(0, 255, (16, 128, 128), dtype=np.uint8)
    seq = iaa.Sequential([iaa.Fliplr(0.5), iaa.GaussianBlur((0, 3.0))])
    # The library expects a list of images (3D inputs) or a single array (4D inputs).
    # So we add an axis to our grayscale array to convert it to shape (16, 128, 128, 1).
    images_aug = seq.augment_images(images[:, :, :, np.newaxis])

    # -----
    # Make sure that the example really does something
    assert not np.array_equal(images, images_aug)
Esempio n. 7
0
def example_show():
    print("Example: Show")
    #from imgaug import augmenters as iaa
    import augmenters as iaa

    images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
    seq = iaa.Sequential([iaa.Fliplr(0.5), iaa.GaussianBlur((0, 3.0))])

    # show an image with 8*8 augmented versions of image 0
    seq.show_grid(images[0], cols=8, rows=8)

    # Show an image with 8*8 augmented versions of image 0 and 8*8 augmented
    # versions of image 1. The identical augmentations will be applied to
    # image 0 and 1.
    seq.show_grid([images[0], images[1]], cols=8, rows=8)
Esempio n. 8
0
def example_keypoints():
    print("Example: Keypoints")
    import imgaug as ia
    #from imgaug import augmenters as iaa
    import augmenters as iaa
    from scipy import misc
    import random
    images = np.random.randint(0, 50, (4, 128, 128, 3), dtype=np.uint8)

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

    seq = iaa.Sequential(
        [iaa.GaussianBlur((0, 3.0)),
         iaa.Affine(scale=(0.5, 0.7))])
    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)
    keypoints_aug = seq_det.augment_keypoints(keypoints_on_images)

    # Example code to show each image and print the new keypoints coordinates
    for img_idx, (image_before, image_after, keypoints_before,
                  keypoints_after) in enumerate(
                      zip(images, images_aug, keypoints_on_images,
                          keypoints_aug)):
        image_before = keypoints_before.draw_on_image(image_before)
        image_after = keypoints_after.draw_on_image(image_after)
        misc.imshow(np.concatenate((image_before, image_after),
                                   axis=1))  # before and after
        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
            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))
Esempio n. 9
0
def example_hooks():
    print("Example: Hooks")
    import imgaug as ia
    #from imgaug import augmenters as iaa
    import augmenters as iaa
    import numpy as np

    # images and heatmaps, just arrays filled with value 30
    images = np.ones((16, 128, 128, 3), dtype=np.uint8) * 30
    heatmaps = np.ones((16, 128, 128, 21), dtype=np.uint8) * 30

    # add vertical lines to see the effect of flip
    images[:, 16:128 - 16, 120:124, :] = 120
    heatmaps[:, 16:128 - 16, 120:124, :] = 120

    seq = iaa.Sequential([
        iaa.Fliplr(0.5, name="Flipper"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.Dropout(0.02, name="Dropout"),
        iaa.AdditiveGaussianNoise(scale=0.01 * 255, name="MyLittleNoise"),
        iaa.AdditiveGaussianNoise(loc=32,
                                  scale=0.0001 * 255,
                                  name="SomeOtherNoise"),
        iaa.Affine(translate_px={"x": (-40, 40)}, name="Affine")
    ])

    # change the activated augmenters for heatmaps
    def activator_heatmaps(images, augmenter, parents, default):
        if augmenter.name in ["GaussianBlur", "Dropout", "MyLittleNoise"]:
            return False
        else:
            # default value for all other augmenters
            return default

    hooks_heatmaps = ia.HooksImages(activator=activator_heatmaps)

    seq_det = seq.to_deterministic(
    )  # call this for each batch again, NOT only once at the start
    images_aug = seq_det.augment_images(images)
    heatmaps_aug = seq_det.augment_images(heatmaps, hooks=hooks_heatmaps)

    # -----------
    ia.show_grid(images_aug)
    ia.show_grid(heatmaps_aug[..., 0:3])
Esempio n. 10
0
def example_standard_situation():
    print("Example: Standard Situation")

    # -------
    # dummy functions to make the example runnable here
    def load_batch(batch_idx):
        return np.random.randint(0, 255, (1, 16, 16, 3), dtype=np.uint8)

    def train_on_images(images):
        pass

    # -------

    #from imgaug import augmenters as iaa
    import augmenters as iaa

    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.GaussianBlur(sigma=(0,
                                3.0))  # blur images with a sigma of 0 to 3.0
    ])

    for batch_idx in range(1000):
        # 'images' should be either a 4D numpy array of shape (N, height, width, channels)
        # or a list of 3D numpy arrays, each having shape (height, width, channels).
        # Grayscale images must have shape (height, width, 1) each.
        # All images must have numpy's dtype uint8. Values are expected to be in
        # range 0-255.
        images = load_batch(batch_idx)
        images_aug = seq.augment_images(images)
        train_on_images(images_aug)

        # -----
        # Make sure that the example really does something
        if batch_idx == 0:
            assert not np.array_equal(images, images_aug)
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 range(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]]),
        ("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]]),
        ("AdditiveGaussianNoise", [("scale=%.2f" % (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" % (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)]]),
        ("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 range(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 range(grid_rows):
        axes.append([plt.subplot(gs[i, col_idx]) for col_idx in range(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 range(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")
Esempio n. 12
0
def augment_data():
    aug_data = np.zeros((4000, 227, 227, 3))

    st = lambda aug: iaa.Sometimes(0.5, aug)

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.5),  # vertically flip 50% of all images
            st(iaa.Crop(
                percent=(0,
                         0.1))),  # crop images by 0-10% of their height/width
            st(iaa.GaussianBlur(
                (0, 3.0))),  # blur images with a sigma between 0 and 3.0
            st(
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.2),
                    per_channel=0.5)),  # add gaussian noise to images
            st(iaa.Dropout(
                (0.0, 0.1),
                per_channel=0.5)),  # randomly remove up to 10% of the pixels
            st(iaa.Add(
                (-10, 10), per_channel=0.5
            )),  # change brightness of images (by -10 to 10 of original value)
            st(iaa.Multiply((0.5, 1.5), per_channel=0.5)
               ),  # change brightness of images (50-150% of original value)
            st(iaa.ContrastNormalization(
                (0.5, 2.0),
                per_channel=0.5)),  # improve or worsen the contrast
            st(
                iaa.Affine(
                    scale={
                        "x": (0.8, 1.2),
                        "y": (0.8, 1.2)
                    },  # scale images to 80-120% of their size, individually per axis
                    translate_px={
                        "x": (-16, 16),
                        "y": (-16, 16)
                    },  # translate by -16 to +16 pixels (per axis)
                    rotate=(-45, 45),  # rotate by -45 to +45 degrees
                    shear=(-16, 16),  # shear by -16 to +16 degrees
                    #order=ia.ALL, # use any of scikit-image's interpolation methods
                    cval=(
                        0, 1.0
                    ),  # if mode is constant, use a cval between 0 and 1.0
                    #mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
            st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
               )  # apply elastic transformations with random strengths
        ],
        random_order=True  # do all of the above in random order
    )

    for i in range(4000):
        img = plt.imread(trainfileName(i + 1))
        image_aug = seq.augment_image(img)

        #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        #gray = rgb2gray(img)
        aug_data[i, ...] = image_aug.astype(float) / 255

    np.save("aug_data_1.npy", aug_data)


#augment_data()
Esempio n. 13
0
def example_heavy_augmentations():
    print("Example: Heavy Augmentations")
    import imgaug as ia
    #from imgaug import augmenters as iaa
    import augmenters as iaa

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

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

    # Define our sequence of augmentation steps that will be applied to every image
    # All augmenters with per_channel=0.5 will sample one value _per image_
    # in 50% of all cases. In all other cases they will sample new values
    # _per channel_.
    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontally flip 50% of all images
            iaa.Flipud(0.5),  # vertically flip 50% of all images
            st(iaa.Crop(
                percent=(0,
                         0.1))),  # crop images by 0-10% of their height/width
            st(iaa.GaussianBlur(
                (0, 3.0))),  # blur images with a sigma between 0 and 3.0
            st(
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05 * 255),
                    per_channel=0.5)),  # add gaussian noise to images
            st(iaa.Dropout(
                (0.0, 0.1),
                per_channel=0.5)),  # randomly remove up to 10% of the pixels
            st(iaa.Add(
                (-10, 10), per_channel=0.5
            )),  # change brightness of images (by -10 to 10 of original value)
            st(iaa.Multiply((0.5, 1.5), per_channel=0.5)
               ),  # change brightness of images (50-150% of original value)
            st(iaa.ContrastNormalization(
                (0.5, 2.0),
                per_channel=0.5)),  # improve or worsen the contrast
            st(iaa.Grayscale((0.0, 1.0))),  # blend with grayscale image
            st(
                iaa.Affine(
                    scale={
                        "x": (0.8, 1.2),
                        "y": (0.8, 1.2)
                    },  # scale images to 80-120% of their size, individually per axis
                    translate_px={
                        "x": (-16, 16),
                        "y": (-16, 16)
                    },  # translate by -16 to +16 pixels (per axis)
                    rotate=(-45, 45),  # rotate by -45 to +45 degrees
                    shear=(-16, 16),  # shear by -16 to +16 degrees
                    order=[
                        0,
                        1
                    ],  # use scikit-image's interpolation orders 0 (nearest neighbour) and 1 (bilinear)
                    cval=(
                        0, 1.0
                    ),  # if mode is constant, use a cval between 0 and 1.0
                    mode=ia.
                    ALL  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                )),
            st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)
               )  # apply elastic transformations with random strengths
        ],
        random_order=True  # do all of the above in random order
    )

    images_aug = seq.augment_images(images)

    # -----
    # Make sure that the example really does something
    assert not np.array_equal(images, images_aug)
Esempio n. 14
0
def main():
    augmenters = [
        iaa.Noop(name="Noop"),
        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.Grayscale((0.0, 1.0), name="Grayscale"),
        iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
        iaa.AdditiveGaussianNoise(loc=0,
                                  scale=(0.0, 0.1),
                                  name="AdditiveGaussianNoise"),
        iaa.Dropout((0.0, 0.1), name="Dropout"),
        iaa.Multiply((0.5, 1.5), name="Multiply"),
        iaa.ContrastNormalization(alpha=(0.5, 2.0),
                                  name="ContrastNormalization"),
        iaa.Grayscale(alpha=(0.0, 1.0), name="Grayscale"),
        iaa.ElasticTransformation(alpha=(0.5, 8.0),
                                  sigma=1.0,
                                  name="ElasticTransformation"),
        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=0,
                   cval=(0, 1.0),
                   mode="constant",
                   name="AffineOrder0ModeConstant")
    ]

    for order in [0, 1]:
        augmenters.append(
            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=order,
                       cval=(0, 1.0),
                       mode=ia.ALL,
                       name="AffineOrder%d" % (order, )))

    augmenters.append(
        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, 1.0),
                   mode=ia.ALL,
                   name="AffineAll"))

    kps = []
    for _ in range(20):
        x = random.randint(0, 31)
        y = random.randint(0, 31)
        kps.append(ia.Keypoint(x=x, y=y))
    kps = ia.KeypointsOnImage(kps, shape=(32, 32, 3))
    #small_keypoints_one = ia.KeypointsOnImage([ia.Keypoint(x=0, y=0), ia.Keypoint(x=1, y=1), ia.Keypoint(x=3, y=3)], shape=(4, 4, 3))
    #medium_keypoints_one = ia.KeypointsOnImage([ia.Keypoint(x=0, y=0), ia.Keypoint(x=16, y=16), ia.Keypoint(x=31, y=31)], shape=(32, 32, 3))
    #large_keypoints_one = ia.KeypointsOnImage([ia.Keypoint(x=0, y=0), ia.Keypoint(x=128, y=128), ia.Keypoint(x=255, y=255)], shape=(256, 256, 3))
    small_keypoints_one = kps.on((4, 4, 3))
    medium_keypoints_one = kps.on((32, 32, 3))
    large_keypoints_one = kps.on((256, 256, 3))
    small_keypoints = [small_keypoints_one.deepcopy() for _ in range(16)]
    medium_keypoints = [medium_keypoints_one.deepcopy() for _ in range(16)]
    large_keypoints = [large_keypoints_one.deepcopy() for _ in range(16)]

    small_images = np.random.randint(0, 255, (16, 4, 4, 3)).astype(np.uint8)
    medium_images = np.random.randint(0, 255, (16, 32, 32, 3)).astype(np.uint8)
    large_images = np.random.randint(0, 255,
                                     (16, 256, 256, 3)).astype(np.uint8)

    print("---------------------------")
    print("Keypoints")
    print("---------------------------")
    for augmenter in augmenters:
        print("[Augmenter: %s]" % (augmenter.name, ))
        for keypoints in [small_keypoints, medium_keypoints, large_keypoints]:
            times = []
            for i in range(100):
                time_start = time.time()
                img_aug = augmenter.augment_keypoints(keypoints)
                time_end = time.time()
                times.append(time_end - time_start)
            times = np.array(times)
            img_str = "{:20s}".format(keypoints[0].shape)
            print("%s | SUM %.5fs | PER ITER avg %.5fs, min %.5fs, max %.5fs" %
                  (img_str, np.sum(times), np.average(times), np.min(times),
                   np.max(times)))

    print("---------------------------")
    print("Images")
    print("---------------------------")
    for augmenter in augmenters:
        print("[Augmenter: %s]" % (augmenter.name, ))
        for images in [small_images, medium_images, large_images]:
            times = []
            for i in range(100):
                time_start = time.time()
                img_aug = augmenter.augment_images(images)
                time_end = time.time()
                times.append(time_end - time_start)
            times = np.array(times)
            img_str = "{:20s}".format(images.shape)
            print("%s | SUM %.5fs | PER ITER avg %.5fs, min %.5fs, max %.5fs" %
                  (img_str, np.sum(times), np.average(times), np.min(times),
                   np.max(times)))