예제 #1
0
def example_determinism():
    print("Example: Determinism")
    from imgaug 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)
예제 #2
0
def example_determinism():
    print("Example: Determinism")
    from imgaug 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)
예제 #3
0
    def test(self):
        ia.seed(1)
        # Load an example image (uint8, 128x128x3).
        image = ia.quokka(size=(128, 128), extract="square")

        # Create an example segmentation map (int32, 128x128).
        # Here, we just randomly place some squares on the image.
        # Class 0 is the background class.
        segmap = np.zeros((128, 128), dtype=np.int32)
        segmap[28:71, 35:85] = 1
        segmap[10:25, 30:45] = 2
        segmap[10:25, 70:85] = 3
        segmap[10:110, 5:10] = 4
        segmap[118:123, 10:110] = 5
        segmap = ia.SegmentationMapOnImage(segmap,
                                           shape=image.shape,
                                           nb_classes=1 + 5)

        # Define our augmentation pipeline.
        seq = iaa.Sequential(
            [
                iaa.Dropout([0.05, 0.2]),  # drop 5% or 20% of all pixels
                iaa.Sharpen((0.0, 1.0)),  # sharpen the image
                iaa.Affine(rotate=(
                    -45,
                    45)),  # rotate by -45 to 45 degrees (affects heatmaps)
                iaa.ElasticTransformation(
                    alpha=50, sigma=5)  # apply water effect (affects heatmaps)
            ],
            random_order=True)

        # Augment images and heatmaps.
        images_aug = []
        segmaps_aug = []
        for _ in range(5):
            seq_det = seq.to_deterministic()
            images_aug.append(seq_det.augment_image(image))
            segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0])

        # We want to generate an image of original input images and heatmaps before/after augmentation.
        # It is supposed to have five columns: (1) original image, (2) augmented image,
        # (3) augmented heatmap on top of augmented image, (4) augmented heatmap on its own in jet
        # color map, (5) augmented heatmap on its own in intensity colormap,
        # We now generate the cells of these columns.
        #
        # Note that we add a [0] after each heatmap draw command. That's because the heatmaps object
        # can contain many sub-heatmaps and hence we draw command returns a list of drawn sub-heatmaps.
        # We only used one sub-heatmap, so our lists always have one entry.
        cells = []
        for image_aug, segmap_aug in zip(images_aug, segmaps_aug):
            cells.append(image)  # column 1
            cells.append(segmap.draw_on_image(image))  # column 2
            cells.append(image_aug)  # column 3
            cells.append(segmap_aug.draw_on_image(image_aug))  # column 4
            cells.append(segmap_aug.draw(size=image_aug.shape[:2]))  # column 5

        # Convert cells to grid image and save.
        grid_image = ia.draw_grid(cells, cols=5)
        ia.show_grid(cells, cols=5)
예제 #4
0
def visualize_imgaug(img, segmap, img_aug, segmap_aug, segment_idx=None):
    img_with_seg = segmap.draw_on_image(img)
    if not segment_idx:
        segment_idx = random.randint(0, len(img_with_seg))

    cells = [
        img, img_with_seg[segment_idx], img_aug,
        segmap_aug.draw_on_image(img_aug)[segment_idx]
    ]
    imgaug.show_grid(cells, cols=2)
예제 #5
0
def example_hooks():
    print("Example: Hooks")
    import numpy as np
    import imgaug as ia
    import imgaug.augmenters as iaa

    # Images and heatmaps, just arrays filled with value 30.
    # We define the heatmaps here as uint8 arrays as we are going to feed them
    # through the pipeline similar to normal images. In that way, every
    # augmenter is applied to them.
    images = np.full((16, 128, 128, 3), 30, dtype=np.uint8)
    heatmaps = np.full((16, 128, 128, 21), 30, dtype=np.uint8)

    # 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,
    # we only want to execute horizontal flip, affine transformation and one of
    # the gaussian noises
    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)

    # call to_deterministic() once per batch, NOT only once at the start
    seq_det = seq.to_deterministic()
    images_aug = seq_det(images=images)
    heatmaps_aug = seq_det(images=heatmaps, hooks=hooks_heatmaps)

    # -----------
    ia.show_grid(images_aug)
    ia.show_grid(heatmaps_aug[..., 0:3])
예제 #6
0
 def aug_img_masks(self, images, masks):
     '''
     images:[img1,img2]
     masks:[m1,m2]
     '''
     if not isinstance(images, list):
         images = [images]
     if not isinstance(masks, list):
         masks = [masks]
     masks_on_images = []
     for cur_img, cur_mask in zip(images, masks):
         segmap = ia.SegmentationMapOnImage(cur_mask,
                                            shape=cur_img.shape,
                                            nb_classes=2)
         masks_on_images.append(segmap)
     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 masks and images
     images_aug = seq_det.augment_images(images, hooks=hook_activate)
     segmaps_aug = seq_det.augment_segmentation_maps(masks_on_images,
                                                     hooks=hook_activate)
     img_out = []
     masks_out = []
     cells = []
     for img_idx, (image_aug,
                   segmap_aug) in enumerate(zip(images_aug, segmaps_aug)):
         img_out.append(image_aug)  # column 3
         masks_out.append(segmap_aug)
         if self.DEBUG:
             cells.append(images[img_idx])  # column 1
             cells.append(masks_on_images[img_idx].draw_on_image(
                 images[img_idx]))  # column 2
             cells.append(image_aug)  # column 3
             cells.append(segmap_aug.draw_on_image(image_aug))  # column 4
             cells.append(masks_on_images[img_idx].draw(
                 size=image_aug.shape[:2]))  # column 5
     # Convert cells to grid image and save
     if self.DEBUG:
         grid_image = ia.draw_grid(cells, cols=5)
         ia.show_grid(cells, cols=5)
         #imageio.imwrite("example_segmaps.jpg", grid_image)
     return img_out, masks_out
예제 #7
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])
예제 #8
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])
예제 #9
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