Example #1
0
def draw():
    image = imageio.imread('./flip/IMG_8038R.JPG')
    json_file = './flip/json/IMG_8038R.json'
    with open(json_file, 'rb') as fp:
        data = json.load(fp)
        all_point = []
        for shapes in data['shapes']:
            aaa = Polygon(shapes['points'])
            all_point.append(aaa)
        psoi = ia.PolygonsOnImage(all_point, shape=image.shape)
        aug = iaa.Sequential([
            iaa.Affine(scale=(1, 1),
                       rotate=(0, 0),
                       translate_percent=(0, 0),
                       mode=["constant"],
                       cval=0),  # 仿射变换

            # iaa.Fliplr(0.5), # 左右翻转
            # iaa.PerspectiveTransform((0.01, 0.1)), # 透视变换
            # iaa.AddToHueAndSaturation((-20, 20)),  #
            # iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
            # iaa.Sometimes(0.75, iaa.Snowflakes())   # 高斯模糊
        ])
        image_aug, psoi_aug = aug(image=image, polygons=psoi)
        ia.imshow(
            psoi_aug.draw_on_image(image_aug, alpha_face=0.2, size_points=7))
Example #2
0
    def test_get_column_names__all_columns(self):
        batch = ia.Batch(
            images=np.zeros((1, 2, 2, 3), dtype=np.uint8),
            heatmaps=[np.zeros((2, 2, 1), dtype=np.float32)],
            segmentation_maps=[np.zeros((2, 2, 1), dtype=np.int32)],
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(x=0, y=0)], shape=(2, 2, 3))
            ],
            bounding_boxes=[
                ia.BoundingBoxesOnImage([ia.BoundingBox(0, 0, 1, 1)],
                                        shape=(2, 2, 3))
            ],
            polygons=[
                ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])],
                                   shape=(2, 2, 3))
            ],
            line_strings=[
                ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])],
                                      shape=(2, 2, 3))
            ])

        names = batch.get_column_names()

        assert names == [
            "images", "heatmaps", "segmentation_maps", "keypoints",
            "bounding_boxes", "polygons", "line_strings"
        ]
Example #3
0
    def test_fill_from_augmented_normalized_batch(self):
        batch = ia.UnnormalizedBatch(
            images=np.zeros((1, 2, 2, 3), dtype=np.uint8),
            heatmaps=[np.zeros((2, 2, 1), dtype=np.float32)],
            segmentation_maps=[np.zeros((2, 2, 1), dtype=np.int32)],
            keypoints=[[(0, 0)]],
            bounding_boxes=[[ia.BoundingBox(0, 0, 1, 1)]],
            polygons=[[ia.Polygon([(0, 0), (1, 0), (1, 1)])]],
            line_strings=[[ia.LineString([(0, 0), (1, 0)])]])
        batch_norm = ia.Batch(
            images=np.zeros((1, 2, 2, 3), dtype=np.uint8),
            heatmaps=[
                ia.HeatmapsOnImage(np.zeros((2, 2, 1), dtype=np.float32),
                                   shape=(2, 2, 3))
            ],
            segmentation_maps=[
                ia.SegmentationMapsOnImage(np.zeros((2, 2, 1), dtype=np.int32),
                                           shape=(2, 2, 3))
            ],
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(2, 2, 3))
            ],
            bounding_boxes=[
                ia.BoundingBoxesOnImage([ia.BoundingBox(0, 0, 1, 1)],
                                        shape=(2, 2, 3))
            ],
            polygons=[
                ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])],
                                   shape=(2, 2, 3))
            ],
            line_strings=[
                ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])],
                                      shape=(2, 2, 3))
            ])
        batch_norm.images_aug = batch_norm.images_unaug
        batch_norm.heatmaps_aug = batch_norm.heatmaps_unaug
        batch_norm.segmentation_maps_aug = batch_norm.segmentation_maps_unaug
        batch_norm.keypoints_aug = batch_norm.keypoints_unaug
        batch_norm.bounding_boxes_aug = batch_norm.bounding_boxes_unaug
        batch_norm.polygons_aug = batch_norm.polygons_unaug
        batch_norm.line_strings_aug = batch_norm.line_strings_unaug

        batch = batch.fill_from_augmented_normalized_batch(batch_norm)

        assert batch.images_aug.shape == (1, 2, 2, 3)
        assert ia.is_np_array(batch.heatmaps_aug[0])
        assert ia.is_np_array(batch.segmentation_maps_aug[0])
        assert batch.keypoints_aug[0][0] == (0, 0)
        assert batch.bounding_boxes_aug[0][0].x1 == 0
        assert batch.polygons_aug[0][0].exterior[0][0] == 0
        assert batch.line_strings_aug[0][0].coords[0][0] == 0
Example #4
0
    def __call__(self, img, boxes, masks, labels, filename):
        #print("####")
        #print(type(img), type(boxes), type(masks[0]), len(masks), np.unique(masks[0]))
        #print(img.shape, boxes.shape, masks[0].shape)
        bboxes = []
        for box in boxes[:, :4]:
            x1, y1, x2, y2 = box
            bboxes.append(ia.BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2))

        # mask를 위해서 polygon 만들기
        points = []
        for gt_mask in masks:
            contours, ct = cv2.findContours(gt_mask, cv2.RETR_LIST,
                                            cv2.CHAIN_APPROX_SIMPLE)
            for c in contours:
                # 각도 있는 다각형
                rc = cv2.minAreaRect(contours[0])
                points.append(cv2.boxPoints(rc))
                #cv2.dravwContours(im, [box], 0, (0,255,0),3)
                # 각도 없는 사각형
                #rect = cv2.boundingRect(c)
                #x,y,w,h = rect
                #cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
                #cv2.putText(im,'Moth Detected',(x+w+10,y+h),0,0.3,(0,255,0))

        mulpoly = [Polygon(point) for point in points]
        psoi = ia.PolygonsOnImage(mulpoly, shape=img.shape)

        image_aug, bbs_aug, poly_aug = self.seq(images=[img],
                                                bounding_boxes=[bboxes],
                                                polygons=[psoi])
        image_aug, bbs_aug, poly_aug = image_aug[0], bbs_aug[0], poly_aug[0]

        # ia.BoundingBox 된 boxes 원래 형태로 돌려놓기
        bbs = []
        for bb in bbs_aug:
            bbs.append(bb.coords.reshape(4))
        bbs_aug = np.array(bbs)

        # polygon이 된 mask를 원래 형태로 돌려놓기
        masks_aug = []
        for poly in poly_aug:
            img = Image.new('L', (image_aug.shape[0], image_aug.shape[0]), 0)
            ImageDraw.Draw(img).polygon(poly.coords, outline=1, fill=1)
            masks_aug.append(np.array(img))

        #print(type(image_aug), type(bbs_aug), type(masks_aug[0]), len(masks_aug), np.unique(masks_aug[0]))
        #print(image_aug.shape, bbs_aug.shape, len(masks_aug), masks_aug[0].shape)

        return image_aug, bbs_aug, masks_aug, labels
 def vis_results(self, vis_dir='vis_dir'):
     for i in range(len(self.images_aug)):
         image_with_polygon = self.images_aug[i]
         for j in range(len(self.polygons_aug[i])):
             psoi = ia.PolygonsOnImage([self.polygons_aug[i][j]],
                                       shape=self.images_aug[i].shape)
             image_with_polygon = psoi.draw_on_image(image_with_polygon,
                                                     alpha_points=0,
                                                     alpha_face=0.0,
                                                     color_lines=(255, 0,
                                                                  0))
             #image_with_polygon, alpha_points=0, alpha_face=0.5, color_lines=(255, 0, 0))
         if not os.path.exists(vis_dir):
             os.makedirs(vis_dir)
         save_path = os.path.join(
             vis_dir,
             self.images_name[i] + '_polygon' + '.' + self.images_format[i])
         cv2.imwrite(save_path, image_with_polygon)
Example #6
0
    def test_to_batch_in_augmentation__all_columns(self):
        batch = ia.Batch(
            images=np.zeros((1, 2, 2, 3), dtype=np.uint8),
            heatmaps=[
                ia.HeatmapsOnImage(np.zeros((2, 2, 1), dtype=np.float32),
                                   shape=(2, 2, 3))
            ],
            segmentation_maps=[
                ia.SegmentationMapsOnImage(np.zeros((2, 2, 1), dtype=np.int32),
                                           shape=(2, 2, 3))
            ],
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(x=0, y=0)], shape=(2, 2, 3))
            ],
            bounding_boxes=[
                ia.BoundingBoxesOnImage([ia.BoundingBox(0, 0, 1, 1)],
                                        shape=(2, 2, 3))
            ],
            polygons=[
                ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])],
                                   shape=(2, 2, 3))
            ],
            line_strings=[
                ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])],
                                      shape=(2, 2, 3))
            ])

        batch_inaug = batch.to_batch_in_augmentation()

        assert isinstance(batch_inaug, ia.BatchInAugmentation)
        assert ia.is_np_array(batch_inaug.images)
        assert batch_inaug.images.shape == (1, 2, 2, 3)
        assert isinstance(batch_inaug.heatmaps[0], ia.HeatmapsOnImage)
        assert isinstance(batch_inaug.segmentation_maps[0],
                          ia.SegmentationMapsOnImage)
        assert isinstance(batch_inaug.keypoints[0], ia.KeypointsOnImage)
        assert isinstance(batch_inaug.bounding_boxes[0],
                          ia.BoundingBoxesOnImage)
        assert isinstance(batch_inaug.polygons[0], ia.PolygonsOnImage)
        assert isinstance(batch_inaug.line_strings[0], ia.LineStringsOnImage)
        assert batch_inaug.get_column_names() == [
            "images", "heatmaps", "segmentation_maps", "keypoints",
            "bounding_boxes", "polygons", "line_strings"
        ]
Example #7
0
 def generate_images(self, path_to_pic: str, shapes: List[Dict]) -> (List, List):
     image = imageio.imread(path_to_pic)
     pols = []
     for p in shapes:
         if p['name'] == 'rect':
             x = p['x']
             y = p['y']
             width = p['width']
             height = p['height']
             pol = Polygon([
                 (x, y),
                 (x + width, y),
                 (x + width, y + height),
                 (x, y + height)
             ])
             pols.append(pol)
         else:
             x_all = p['all_points_x']
             y_all = p['all_points_y']
             points = []
             for i in range(len(x_all)):
                 points.append((x_all[i], y_all[i]))
             pol = Polygon(points)
             pols.append(pol)
     psoi = ia.PolygonsOnImage(pols, shape=image.shape)
     aug_images = []
     psoi_augs = []
     transforms = [iaa.Fliplr(1),
                   iaa.GaussianBlur(5),
                   iaa.SaltAndPepper(0.2),
                   iaa.Multiply(0.6),
                   iaa.Multiply(1.2),
                   iaa.ElasticTransformation(sigma=0.2, alpha=3)]
     for trans in transforms:
         aug_func = iaa.Sequential([
             trans
         ])
         image_aug, psoi_aug = aug_func(image=image, polygons=psoi)
         aug_images.append(image_aug)
         psoi_augs.append(psoi_aug)
     # images = [psoi_aug.draw_on_image(image_aug, alpha_face=0.2, size_points=7), image]
     # ia.imshow(np.hstack(images))
     return aug_images, psoi_augs
Example #8
0
    def may_augment_poly(self, aug, img_shape, polys):
        imgaug_polys = []
        for poly in polys:
            poly = poly[0]
            poly = poly.reshape(-1, 2)
            imgaug_polys.append(imgaug.Polygon(poly))
        imgaug_polys = aug.augment_polygons(
            [imgaug.PolygonsOnImage(imgaug_polys,
                                    shape=img_shape)])[0].clip_out_of_image()

        new_polys = []
        for poly in imgaug_polys.polygons:
            new_poly = []
            for point in poly:
                new_poly.append(np.array(point, dtype=np.float32))
            new_poly = np.array(new_poly, dtype=np.float32).flatten()
            new_polys.append([new_poly])

        return new_polys
Example #9
0
def rotate_img(soudir, tardir, rate, json_dir, save_dir):
    pathdir = os.listdir(soudir)  # sourdir里不要有目录名
    filenum = len(pathdir)
    picknum = int(filenum * rate)
    sample = random.sample(pathdir, picknum)
    for name in sample:
        json_file = json_dir + name[:-4] + '.json'
        with open(json_file, 'rb') as fp:
            image = imageio.imread(soudir + name)
            data = json.load(fp)
            data['imagePath'] = data['imagePath'][:-4] + 'R.JPG'
            all_point = []
            for shapes in data['shapes']:
                aaa = Polygon(shapes['points'])
                all_point.append(aaa)
            psoi = ia.PolygonsOnImage(all_point, shape=image.shape)
            aug = iaa.Sequential([
                iaa.Affine(scale=(0.8, 1),
                           rotate=(-10, 10),
                           translate_percent=(-0.1, 0.1),
                           mode=["constant"],
                           cval=0),  # 仿射变换

                # iaa.Fliplr(0.5), # 左右翻转
                # iaa.PerspectiveTransform((0.01, 0.1)), # 透视变换
                # iaa.AddToHueAndSaturation((-20, 20)),  #
                # iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
                # iaa.Sometimes(0.75, iaa.Snowflakes())   # 高斯模糊
            ])
            image_aug, psoi_aug = aug(image=image, polygons=psoi)
            psoi_aug_removed = psoi_aug.remove_out_of_image(fully=True,
                                                            partly=False)
            psoi_aug_removed_clipped = psoi_aug_removed.clip_out_of_image()
            assert len(psoi_aug.polygons) == len(
                psoi_aug_removed_clipped.polygons)
            for i, shapes in enumerate(data['shapes']):
                shapes['points'] = psoi_aug_removed_clipped.polygons[
                    i].exterior.astype('int32').tolist()

            imageio.imwrite(tardir + name[:-4] + 'R.JPG', image_aug)
            json.dump(data,
                      open(save_dir + json_file[-13:-5] + 'R.json', 'w'),
                      indent=4)
Example #10
0
def example_visualize_augmented_non_image_data():
    print("Example: Visualize Augmented Non-Image Data")
    import numpy as np
    import imgaug as ia

    image = np.zeros((64, 64, 3), dtype=np.uint8)

    # points
    kps = [ia.Keypoint(x=10.5, y=20.5), ia.Keypoint(x=60.5, y=60.5)]
    kpsoi = ia.KeypointsOnImage(kps, shape=image.shape)
    image_with_kps = kpsoi.draw_on_image(image, size=7, color=(0, 0, 255))
    ia.imshow(image_with_kps)

    # bbs
    bbsoi = ia.BoundingBoxesOnImage(
        [ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=30.5)],
        shape=image.shape)
    image_with_bbs = bbsoi.draw_on_image(image)
    image_with_bbs = ia.BoundingBox(x1=50.5, y1=10.5, x2=100.5,
                                    y2=16.5).draw_on_image(image_with_bbs,
                                                           color=(255, 0, 0),
                                                           size=3)
    ia.imshow(image_with_bbs)

    # polygons
    psoi = ia.PolygonsOnImage(
        [ia.Polygon([(10.5, 20.5), (50.5, 30.5), (10.5, 50.5)])],
        shape=image.shape)
    image_with_polys = psoi.draw_on_image(image,
                                          alpha_points=0,
                                          alpha_face=0.5,
                                          color_lines=(255, 0, 0))
    ia.imshow(image_with_polys)

    # heatmaps
    # pick first result via [0] here, because one image per heatmap channel
    # is generated
    hms = ia.HeatmapsOnImage(np.random.random(size=(32, 32,
                                                    1)).astype(np.float32),
                             shape=image.shape)
    image_with_hms = hms.draw_on_image(image)[0]
    ia.imshow(image_with_hms)
Example #11
0
    def f(polygon, modified_image, seq_det):

        poly = []
        for i in range(0, len(polygon), 2):
            poly.append((polygon[i], polygon[i + 1]))
            """
            poly_x.append(polygon[i])
            poly_y.append(polygon[i + 1])
            """
            # rewrite for imgarg 0.2.8
        poly_on_image = ia.PolygonsOnImage([ia.Polygon(poly)],
                                           shape=(img_h, img_w))
        poly_on_image = seq_det.augment_polygons([poly_on_image])[0]

        moved_poly = poly_on_image.polygons[0]
        for i, (x, y) in enumerate(zip(moved_poly.xx, moved_poly.yy)):
            polygon[2 * i] = x
            polygon[2 * i + 1] = y

        return polygon
Example #12
0
    def test_two_images_and_polygons(self):
        rng = iarandom.RNG(0)
        images = rng.integers(0, 256, size=(2, 32, 32, 3), dtype=np.uint8)
        polys = []
        for x in np.linspace(0, 256, 4):
            for y in np.linspace(0, 256, 4):
                polys.append(
                    ia.Polygon([(x, y), (x + 20, y), (x + 20, y + 20),
                                (x, y + 20)]))
        psoi1 = ia.PolygonsOnImage(polys, shape=images[0].shape)
        psoi2 = psoi1.shift(left=20)
        image1_w_overlay = psoi1.draw_on_image(images[0])
        image2_w_overlay = psoi2.draw_on_image(images[1])

        debug_image = iaa.draw_debug_image(images, polygons=[psoi1, psoi2])

        assert self._image_contains(images[0, ...], debug_image)
        assert self._image_contains(images[1, ...], debug_image)
        assert self._image_contains(image1_w_overlay, debug_image)
        assert self._image_contains(image2_w_overlay, debug_image)
Example #13
0
    def test_get_rowwise_shapes__nonimages(self):
        heatmaps = [
            ia.HeatmapsOnImage(np.zeros((1, 2, 1), dtype=np.float32),
                               shape=(1, 2, 3))
        ]
        segmaps = [
            ia.SegmentationMapsOnImage(np.zeros((1, 2, 1), dtype=np.int32),
                                       shape=(1, 2, 3))
        ]
        keypoints = [ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(1, 2, 3))]
        bounding_boxes = [
            ia.BoundingBoxesOnImage([ia.BoundingBox(0, 1, 2, 3)],
                                    shape=(1, 2, 3))
        ]
        polygons = [
            ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])],
                               shape=(1, 2, 3))
        ]
        line_strings = [
            ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])],
                                  shape=(1, 2, 3))
        ]

        kwargs = [{
            "heatmaps": heatmaps
        }, {
            "segmentation_maps": segmaps
        }, {
            "keypoints": keypoints
        }, {
            "bounding_boxes": bounding_boxes
        }, {
            "polygons": polygons
        }, {
            "line_strings": line_strings
        }]
        for kwargs_i in kwargs:
            batch = ia.BatchInAugmentation(**kwargs_i)
            shapes = batch.get_rowwise_shapes()
            assert shapes == [(1, 2, 3)]
Example #14
0
def do_aug(image, aug):
    seq = iaa.Sequential([aug])

    # 测试bbox变形,不过放弃了,因为bbox始终是水平的,即使是做了仿射,还是水平的,改用下面的多边形测试了
    # seq_det = seq.to_deterministic()  # 保持坐标和图像同步改变,而不是随机
    # bbox的测试
    # bbs = ia.BoundingBoxesOnImage(
    # [
    #     ia.BoundingBox(x1=20, y1=70, x2=110, y2=130)
    # ], shape=image.shape)
    # image_aug = seq_det.augment_images([image])[0]
    # bbs_aug = seq_det.augment_polygons([bbs])[0]
    # image_aug = seq_det.augment_images([image])[0]
    # bbs_aug = seq_det.augment_polygons(polygons)[0]

    # 多边形测试
    polygons = ia.PolygonsOnImage(
        [ia.Polygon([(20, 70), (110, 70), (110, 130), (20, 130)])],
        shape=image.shape)
    images_aug, polygons_aug = seq(images=[image], polygons=polygons)
    image_after = polygons_aug[0].draw_on_image(images_aug[0], size=2)

    cv2.imwrite(f"debug/{aug.name}.jpg", image_after)
    return image_after
Example #15
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]
def augment_image(image_name, frame_name):
    image = imageio.imread(
        f"C:/Users/Matthew/Desktop/Mobile Robotics - Personal Project/photos/original - labelme/{image_name}"
    )

    with open(
            f"C:/Users/Matthew/Desktop/Mobile Robotics - Personal Project/photos/original - labelme/{frame_name}"
    ) as f:
        labelme_json = json.load(f)

    shapes_list = labelme_json['shapes']
    polygon_list = []
    for shape in shapes_list:
        polygon_list.append(Polygon(shape['points'], label=shape['label']))

    psoi = ia.PolygonsOnImage(polygon_list, shape=image.shape)
    # ia.imshow(psoi.draw_on_image(image, alpha_face=0.2, size_points=7))
    psoi.draw_on_image(image, alpha_face=0.2, size_points=7)

    ### Construct a sequential transformation pipeline ###
    # Randomly select some subset of the available transformations
    # Apply them in a random order
    # Seed a random value and rotate the image
    ia.seed(4)

    aug = iaa.Sequential([
        iaa.SomeOf(
            (0, 7),
            [
                iaa.Affine(rotate=(-25, 25)),
                iaa.AdditiveGaussianNoise(scale=(30, 90)),
                iaa.Crop(percent=(0, 0.4)),
                iaa.CropAndPad(percent=(-0.2, 0.2),
                               pad_mode="edge"),  # crop and pad images
                iaa.AddToHueAndSaturation((-60, 60)),  # change their color
                iaa.ElasticTransformation(alpha=90,
                                          sigma=9),  # water-like effect
                iaa.Cutout(
                )  # replace one squared area within the image by a constant intensity value
            ],
            random_order=True)
    ])

    # Conduct that sequential transofrmation in a random order, 32 times
    # augmented = [seq(image=image, polygons=psoi) for _ in range(32)]

    # Display the 32 synthetic variations of the original image
    # Print the frame name as a progress indicator
    print(f"Augmenting: {frame_name}")

    images_polys_aug = []
    for idx in range(32):
        image_aug, psoi_aug = aug(image=image, polygons=psoi)
        image_polys_aug = psoi_aug.draw_on_image(image_aug,
                                                 alpha_face=0.2,
                                                 size_points=11)
        images_polys_aug.append(image_polys_aug)

        shape_aug_list = []
        for poly_aug in psoi_aug:
            shape_aug_list.append(
                dict({
                    'label': poly_aug.label,
                    'points': list(poly_aug.coords.tolist()),
                    "group_id": None,
                    "shape_type": "polygon",
                    "flags": {}
                }))

        labelme_json_augmented = {
            "version": "4.5.7",
            "flags": {},
            "shapes": shape_aug_list,
            "imagePath": f"{image_name.replace('.jpg','')}_v{idx}.jpg",
            "imageData": None,
            "imageHeight": 480,
            "imageWidth": 640
        }

        # pprint.pprint(labelme_json_augmented)

        with open(
                f"C:/Users/Matthew/Desktop/Mobile Robotics - Personal Project/photos/original - labelme/{frame_name.replace('.json','')}_v{idx}.json",
                'w') as f:
            json.dump(labelme_json_augmented, f)

        imageio.imwrite(
            f"C:/Users/Matthew/Desktop/Mobile Robotics - Personal Project/photos/original - labelme/{image_name.replace('.jpg','')}_v{idx}.jpg",
            image_aug)
Example #17
0
def det_aug(image, polys_np=None):
    """
    随机对图像做以下的增强操作
    :param image: cv2 read
    :param polys_np:[N, 4, 2]
    :return:
    """
    aug_sample = random.sample(cfg.TRAIN.AUG_TOOL, 1)[0]  #从数组中随机取出一个增强的功能

    rotate_sample = random.choice([0, 1])

    ######################################################################################################
    # blur-模糊
    aug = None
    # 高斯滤波 sigma 为1-10的保留小数点后一位的float的随机值,可根据情况调整
    if aug_sample == 'GaussianBlur':
        sigma = random.uniform(1, 2)
        sigma = round(sigma, 10)
        aug = iaa.GaussianBlur(sigma)

    # 平均模糊 k 为1-10的随机 奇 数,范围根据情况调整
    if aug_sample == 'AverageBlur':
        k = random.randint(8, 10) * 2 + 1
        aug = iaa.AverageBlur(k)

    # 中值滤波 k 为1-10的随机 奇 数,范围根据情况调整
    if aug_sample == 'MedianBlur':
        k = random.randint(8, 10) * 2 + 1
        aug = iaa.MedianBlur(k)

    # 双边滤波 d=1 为 奇 数, sigma_color=(10, 250), sigma_space=(10, 250)
    if aug_sample == 'BilateralBlur':
        d = random.randint(0, 2) * 2 + 1
        sigma_color = random.randint(10, 250)
        sigma_space = random.randint(10, 250)
        aug = iaa.BilateralBlur(d, sigma_color, sigma_space)

    # 运动模糊 k=5 一定大于3 的 奇 数, angle=(0, 360), direction=(-1.0, 1.0)
    if aug_sample == 'MotionBlur':
        k = random.randint(15, 20) * 2 + 1
        angle = random.randint(0, 360)
        direction = random.uniform(-1, 1)
        direction = round(direction, 1)
        aug = iaa.MotionBlur(k, angle, direction)

    ######################################################################################################
    # geometric  几何学

    # 弹性变换
    if aug_sample == 'ElasticTransformation':
        alpha = random.uniform(10, 20)
        alpha = round(alpha, 1)
        sigma = random.uniform(5, 10)
        sigma = round(sigma, 1)
        # print(alpha, sigma)
        aug = iaa.ElasticTransformation(alpha, sigma)

    # 透视
    if aug_sample == 'PerspectiveTransform':
        scale = random.uniform(0, 0.15)
        scale = round(scale, 3)
        aug = iaa.PerspectiveTransform(scale)

    # 旋转角度
    if aug_sample == 'Affine_rot':
        rotate = random.randint(-180, 180)
        while rotate == 0:
            rotate = random.randint(-180, 180)
        if rotate_sample == 0:
            aug = iaa.Affine(rotate=rotate, fit_output=True)
        else:
            aug = iaa.Affine(rotate=rotate)

    if aug_sample == 'Affine_scale':
        scale = random.uniform(0, 2)
        scale = round(scale, 1)
        while scale == 0 or scale <= 0.3:
            scale = random.uniform(0, 2)
            scale = round(scale, 1)

        if rotate_sample == 0:
            aug = iaa.Affine(scale=scale, fit_output=True)
        else:
            aug = iaa.Affine(scale=scale)

    ######################################################################################################
    # flip 镜像

    # 水平镜像
    if aug_sample == 'Fliplr':
        aug = iaa.Fliplr(1)
    #
    # 垂直镜像
    if aug_sample == 'Flipud':
        aug = iaa.Flipud(1)

    ######################################################################################################
    # size 尺寸

    # if aug_sample == 'CropAndPad':
    #     top = random.randint(0, 10)
    #     right = random.randint(0, 10)
    #     bottom = random.randint(0, 10)
    #     left = random.randint(0, 10)
    #     aug = iaa.CropAndPad(px=(top, right, bottom, left))  # 上 右 下 左 各crop多少像素,然后进行padding

    if aug_sample == 'Crop':
        top = random.randint(0, 10)
        right = random.randint(0, 10)
        bottom = random.randint(0, 10)
        left = random.randint(0, 10)
        aug = iaa.Crop(px=(top, right, bottom, left))  # 上 右 下 左

    if aug_sample == 'Pad':
        top = random.randint(0, 10)
        right = random.randint(0, 10)
        bottom = random.randint(0, 10)
        left = random.randint(0, 10)
        aug = iaa.Pad(px=(top, right, bottom, left))  # 上 右 下 左

    # if aug_sample == 'PadToFixedSize':
    #     height = image.shape[0] + 32
    #     width = image.shape[1] + 100
    #     aug = iaa.PadToFixedSize(width=width, height=height)z

    # if aug_sample == 'CropToFixedSize':
    #     height = image.shape[0] - 32
    #     width = image.shape[1] - 100
    #     aug = iaa.CropToFixedSize(width=width, height=height)

    if polys_np is not None:
        if aug is not None:
            # print(aug_sample)
            h, w, _ = image.shape
            boxes_info_list = []
            for box in polys_np:
                boxes_info_list.append(Polygon(box))

            psoi = ia.PolygonsOnImage(boxes_info_list,
                                      shape=image.shape)  # 生成单个图像上所有多边形的对象
            image, psoi_aug = aug(image=image, polygons=psoi)

            pts_list = []
            for each_poly in psoi_aug.polygons:
                pts_list.append(np.array(each_poly.exterior).reshape((4, 2)))
            return image, np.array(pts_list, np.float32).reshape((-1, 4, 2))
        else:

            return image, polys_np
    else:
        if aug is not None:
            image = aug(image=image)
        else:
            image = image
        return image
Example #18
0
    def augument(self, image, bbox_list):
        seq = iaa.Sequential([
            # 变形
            iaa.Sometimes(
                0.6,
                [
                    iaa.OneOf([
                        iaa.Affine(shear={
                            'x': (-1.5, 1.5),
                            'y': (-1.5, 1.5)
                        },
                                   mode="edge"),  # 仿射变化程度,单位像素
                        iaa.Rotate(rotate=(-1, 1), mode="edge"),  # 旋转,单位度
                    ])
                ]),
            # 扭曲
            iaa.Sometimes(
                0.5,
                [
                    iaa.OneOf([
                        iaa.PiecewiseAffine(
                            scale=(0, 0.02), nb_rows=2, nb_cols=2),  # 局部仿射
                        iaa.ElasticTransformation(  # distort扭曲变形
                            alpha=(0, 3),  # 扭曲程度
                            sigma=(0.8, 1),  # 扭曲后的平滑程度
                            mode="nearest"),
                    ]),
                ]),
            # 模糊
            iaa.Sometimes(
                0.5,
                [
                    iaa.OneOf([
                        iaa.GaussianBlur(sigma=(0, 0.7)),
                        iaa.AverageBlur(k=(1, 3)),
                        iaa.MedianBlur(k=(1, 3)),
                        iaa.BilateralBlur(
                            d=(1, 5),
                            sigma_color=(10, 200),
                            sigma_space=(10, 200)),  # 既噪音又模糊,叫双边,
                        iaa.MotionBlur(k=(3, 5)),
                        iaa.Snowflakes(flake_size=(0.1, 0.2),
                                       density=(0.005, 0.025)),
                        iaa.Rain(nb_iterations=1,
                                 drop_size=(0.05, 0.1),
                                 speed=(0.04, 0.08)),
                    ])
                ]),
            # 锐化
            iaa.Sometimes(0.3, [
                iaa.OneOf([
                    iaa.Sharpen(),
                    iaa.GammaContrast(),
                    iaa.SigmoidContrast()
                ])
            ]),
            # 噪音
            iaa.Sometimes(0.3, [
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(scale=(1, 5)),
                    iaa.AdditiveLaplaceNoise(scale=(1, 5)),
                    iaa.AdditivePoissonNoise(lam=(1, 5)),
                    iaa.Salt((0, 0.02)),
                    iaa.Pepper((0, 0.02))
                ])
            ]),
            # 剪切
            iaa.Sometimes(
                0.8,
                [
                    iaa.OneOf([
                        iaa.Crop((0, 2)),  # 切边, (0到10个像素采样)
                    ])
                ]),
        ])

        assert bbox_list is None or type(bbox_list) == list

        if bbox_list is None or len(bbox_list) == 0:
            polys = None
        else:
            polys = [ia.Polygon(pos) for pos in bbox_list]
            polys = ia.PolygonsOnImage(polys, shape=image.shape)

        # 处理部分或者整体出了图像的范围的多边形,参考:https://imgaug.readthedocs.io/en/latest/source/examples_bounding_boxes.html
        polys = polys.remove_out_of_image().clip_out_of_image()
        images_aug, polygons_aug = seq(images=[image], polygons=polys)

        image = images_aug[0]

        if polygons_aug is None:
            polys = None
        else:
            polys = []
            for p in polygons_aug.polygons:
                polys.append(p.coords)
            polys = np.array(polys, np.int32).tolist()  # (N,2)

        return image, polys
aug = [aug1]

new_json = {}

for aug_num in range(len(aug)):
    transform_type = str(aug_num)
    for fname in tqdm(sorted(os.listdir(src_img))):
        inner_dict = {}
        if fname[-3:] == 'png':  # json files also in the folder
            # read image
            img = imageio.imread(src_img + fname)

            if fname in polycoords:
                psoi = polycoords.get(fname)  # get corresponding polygons
                # for augment image + polygons
                psoi = ia.PolygonsOnImage(psoi, shape=img.shape)

                image_aug, psoi_aug = aug[aug_num](image=img, polygons=psoi)
                #ia.imshow(psoi_aug.draw_on_image(image_aug, alpha_face=0.2, size_points=7))

                # save image
                imageio.imwrite(
                    dst_augimg + fname[:-4] + "__" + transform_type + ".png",
                    image_aug)

                # save json
                fsize = os.path.getsize(src_img + fname)
                inner_dict.update(
                    {"filename": fname[:-4] + "__" + transform_type + ".png"})
                inner_dict.update({"size": fsize})
Example #20
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]
Example #21
0
 def psoi(self):
     polygons = [ia.Polygon([(0, 0), (2, 0), (2, 2)])]
     return [ia.PolygonsOnImage(polygons, shape=self.image.shape)]
Example #22
0
 def psoi_flipped(self):
     polygons = [ia.Polygon([(0, 3 - 0), (2, 3 - 0), (2, 3 - 2)])]
     return [ia.PolygonsOnImage(polygons, shape=self.image.shape)]
Example #23
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 = HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3))

    segmaps_arr = np.int32([[0, 0, 1],
                            [0, 0, 1],
                            [0, 1, 1]])
    segmaps_arr_r1 = np.int32([[0, 0, 0],
                               [0, 0, 0],
                               [0, 0, 1]])
    segmaps_arr_l1 = np.int32([[0, 1, 0],
                               [0, 1, 0],
                               [1, 1, 0]])
    segmaps = SegmentationMapsOnImage(segmaps_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)

    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_segmentation_maps([segmaps])[0]
        assert observed.shape == segmaps.shape
        assert np.array_equal(observed.get_arr(), segmaps_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)

    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_segmentation_maps([segmaps])[0]
        assert observed.shape == segmaps.shape
        assert np.array_equal(observed.get_arr(), segmaps_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

    # empty keypoints
    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_keypoints(ia.KeypointsOnImage([], shape=(1, 2, 3)))
    assert len(observed.keypoints) == 0
    assert observed.shape == (1, 2, 3)

    # 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])

    # -----
    # polygons
    # -----
    ps = [ia.Polygon([(5, 5), (10, 5), (10, 10)])]
    psoi = ia.PolygonsOnImage(ps, shape=(20, 20, 3))

    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    # per_channel
    aug = iaa.Alpha(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.Alpha(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

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

    # empty polygons
    aug = iaa.Alpha(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons(ia.PolygonsOnImage([], shape=(1, 2, 3)))
    assert len(observed.polygons) == 0
    assert observed.shape == (1, 2, 3)

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

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

    hooks = ia.HooksKeypoints(propagator=propagator)  # no hooks for polygons yet, so we use HooksKeypoints
    observed = aug.augment_polygons([psoi], hooks=hooks)[0]
    assert observed.polygons[0].exterior_almost_equals(psoi.polygons[0])

    # -----
    # 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]
Example #24
0
    def augment(self):
        few_instances = self.df[self.df['counts'] < self.df['counts'].median()]
        aug_list = list(few_instances['instance_name'])
        label_files = Path(self.anno_dir).glob("*.json")

        aug = iaa.Sequential([
            iaa.AdditiveGaussianNoise(scale=10),
            # The following transformations will change the polygon
            # iaa.Affine(rotate=(-0.05, 0.05), translate_percent=(-0.05, 0.05), scale=(0.8, 1.2),
            #            mode=["constant", "edge"], cval=0),
            # iaa.CoarseDropout(0.1,size_px=8),
            # iaa.Fliplr(0.5),
            #iaa.PerspectiveTransform((0.01, 0.01)),
            #iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
            iaa.Sometimes(0.05, iaa.Snowflakes()),
            iaa.AddToHueAndSaturation((-50, 50)),
        ])

        for lf in label_files:
            label_file = json.loads(lf.read_bytes())
            img_path = lf.with_suffix('.jpg')
            img = imageio.imread(img_path)
            image_polys = np.copy(img)
            polys = []
            is_aug = False

            aug_dir = img_path.parent.parent / (img_path.parent.stem + '_aug')
            aug_dir.mkdir(exist_ok=True)

            for i, shape in enumerate(label_file['shapes']):
                label = shape['label']
                if label in aug_list:
                    is_aug = True
                    points = shape['points']

                    polygon = Polygon(points, [label])
                    psoi = ia.PolygonsOnImage([polygon],
                                              shape=image_polys.shape)
                    instance_counts_median = self.df['counts'].median()
                    instance_counts = (self.df[self.df['instance_name'] ==
                                               label]['counts'].values[0])
                    for j in range(
                            int(instance_counts_median - instance_counts)):
                        aug_img, psoi_aug = aug(image=image_polys,
                                                polygons=psoi)
                        aug_img_path = aug_dir / \
                            (img_path.stem + f'_{j}_aug.jpg')
                        aug_json_path = aug_img_path.with_suffix('.json')
                        aug_points = psoi_aug.polygons[0].exterior
                        imageio.imsave(aug_img_path, aug_img, '.jpg')
                        label_file["imageData"] = None
                        label_file['imagePath'] = aug_img_path.name
                        with open(aug_json_path, "w") as f:
                            json.dump(label_file,
                                      f,
                                      ensure_ascii=False,
                                      indent=2)

                        label_file['shapes'][i]['points'] = aug_points.tolist()

                    self.augment_list.append(lf)
        return set(self.augment_list)
Example #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 = HeatmapsOnImage(heatmaps_arr, shape=(3, 3, 3))

    segmaps_arr = np.int32([[0, 0, 1],
                            [0, 0, 1],
                            [0, 1, 1]])
    segmaps_arr_r1 = np.int32([[0, 0, 0],
                               [0, 0, 0],
                               [0, 0, 1]])
    segmaps_arr_l1 = np.int32([[0, 1, 0],
                               [0, 1, 0],
                               [1, 1, 0]])
    segmaps = SegmentationMapsOnImage(segmaps_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(1,
                               iaa.Affine(translate_px={"x": 1}),
                               iaa.Affine(translate_px={"x": -1}))
    observed = aug.augment_segmentation_maps([segmaps])[0]
    assert observed.shape == (3, 3, 3)
    assert np.array_equal(observed.get_arr(), segmaps_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,
                               iaa.Affine(translate_px={"x": 1}),
                               iaa.Affine(translate_px={"x": -1}))
    observed = aug.augment_segmentation_maps([segmaps])[0]
    assert observed.shape == (3, 3, 3)
    assert np.array_equal(observed.get_arr(), segmaps_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)

    # -----
    # segmaps and per_channel
    # -----
    aug = iaa.AlphaElementwise(
        _DummyMaskParameter(inverted=False),
        iaa.Affine(translate_px={"x": 1}),
        iaa.Affine(translate_px={"x": -1}),
        per_channel=True
    )
    observed = aug.augment_segmentation_maps([segmaps])[0]
    assert observed.shape == (3, 3, 3)
    assert np.array_equal(observed.get_arr(), segmaps_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_segmentation_maps([segmaps])[0]
    assert observed.shape == (3, 3, 3)
    assert np.array_equal(observed.get_arr(), segmaps_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])

    # -----
    # polygons
    # -----
    ps = [ia.Polygon([(5, 5), (10, 5), (10, 10)])]
    psoi = ia.PolygonsOnImage(ps, shape=(20, 20, 3))

    aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.AlphaElementwise(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.AlphaElementwise(0.499, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

    # per_channel
    aug = iaa.AlphaElementwise(1.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_polygons([psoi])
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(psoi.polygons[0])
    assert observed[0].polygons[0].is_valid

    aug = iaa.AlphaElementwise(0.0, iaa.Noop(), iaa.Affine(translate_px={"x": 1}), per_channel=True)
    observed = aug.augment_polygons([psoi])
    expected = psoi.shift(left=1)
    assert len(observed) == 1
    assert len(observed[0].polygons) == 1
    assert observed[0].shape == psoi.shape
    assert observed[0].polygons[0].exterior_almost_equals(expected.polygons[0])
    assert observed[0].polygons[0].is_valid

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

    # empty polygons
    aug = iaa.AlphaElementwise(0.501, iaa.Noop(), iaa.Affine(translate_px={"x": 1}))
    observed = aug.augment_polygons(ia.PolygonsOnImage([], shape=(1, 2, 3)))
    assert len(observed.polygons) == 0
    assert observed.shape == (1, 2, 3)

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

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

    hooks = ia.HooksKeypoints(propagator=propagator)  # no hooks for polygons yet, so we use HooksKeypoints
    observed = aug.augment_polygons([psoi], hooks=hooks)[0]
    assert observed.polygons[0].exterior_almost_equals(psoi.polygons[0])
Example #26
0
def test_Flipud():
    reseed()

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

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

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

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

    polygons = [
        ia.PolygonsOnImage([ia.Polygon([(0, 0), (2, 0), (2, 2)])],
                           shape=base_img.shape)
    ]
    polygons_flipped = [
        ia.PolygonsOnImage([ia.Polygon([(0, 3 - 0), (2, 3 - 0), (2, 3 - 2)])],
                           shape=base_img.shape)
    ]

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

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

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

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

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

        for aug_ in [aug, aug_det]:
            observed = aug_.augment_polygons(polygons)
            assert len(observed) == 1
            assert len(observed[0].polygons) == 1
            assert observed[0].shape == polygons[0].shape
            assert observed[0].polygons[0].exterior_almost_equals(
                polygons[0].polygons[0])
            assert observed[0].polygons[0].is_valid

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

    # 0% chance of flip, segmaps
    aug = iaa.Flipud(0)
    segmaps = SegmentationMapsOnImage(np.int32([
        [0, 1, 2],
        [0, 1, 2],
        [2, 2, 2],
    ]),
                                      shape=(3, 3, 3))
    observed = aug.augment_segmentation_maps([segmaps])[0]
    expected = segmaps.get_arr()
    assert observed.shape == segmaps.shape
    assert np.array_equal(observed.get_arr(), expected)

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

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

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

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

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

        for aug_ in [aug, aug_det]:
            observed = aug_.augment_polygons(polygons)
            assert len(observed) == 1
            assert len(observed[0].polygons) == 1
            assert observed[0].shape == polygons[0].shape
            assert observed[0].polygons[0].exterior_almost_equals(
                polygons_flipped[0].polygons[0])
            assert observed[0].polygons[0].is_valid

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

    # 100% chance of flip, segmaps
    aug = iaa.Flipud(1.0)
    segmaps = SegmentationMapsOnImage(np.int32([
        [0, 1, 2],
        [0, 1, 2],
        [2, 2, 2],
    ]),
                                      shape=(3, 3, 3))
    observed = aug.augment_segmentation_maps([segmaps])[0]
    expected = np.flipud(segmaps.get_arr())
    assert observed.shape == segmaps.shape
    assert np.array_equal(observed.get_arr(), expected)

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

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

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

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

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

        observed = aug.augment_polygons(polygons)
        if observed[0].polygons[0].exterior_almost_equals(
                polygons_flipped[0].polygons[0]):
            nb_polygons_flipped += 1

        observed = aug_det.augment_polygons(polygons)
        if observed[0].polygons[0].exterior_almost_equals(
                polygons_flipped[0].polygons[0]):
            nb_polygons_flipped_det += 1

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

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

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

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

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

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

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

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

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

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

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

    for dtype, value in zip([np.float16, np.float32, np.float64, np.float128],
                            [5000, 1000**2, 1000**3, 1000**4]):
        atol = 1e-9 * max_value if dtype != np.float16 else 1e-3 * max_value
        image = np.zeros((3, 3), dtype=dtype)
        image[0, 0] = value
        expected = np.zeros((3, 3), dtype=dtype)
        expected[2, 0] = value
        image_aug = aug.augment_image(image)
        assert image_aug.dtype.type == dtype
        assert np.allclose(image_aug, expected, atol=atol)
Example #27
0
def do_random(image, pos_list):
    # 1.先任选5种影响位置的效果之一做位置变换
    seq = iaa.Sequential([
        iaa.Sometimes(
            0.5,
            [
                iaa.Crop((0, 10)),  # 切边, (0到10个像素采样)
            ]),
        iaa.Sometimes(
            0.5,
            [
                iaa.Affine(shear={
                    'x': (-10, 10),
                    'y': (-10, 10)
                }, mode="edge"),
                iaa.Rotate(rotate=(-10, 10), mode="edge"),  # 旋转
            ]),
        iaa.Sometimes(
            0.5,
            [
                iaa.PiecewiseAffine(),  # 局部仿射
                iaa.ElasticTransformation(  # distort扭曲变形
                    alpha=(0.0, 20.0),
                    sigma=(3.0, 5.0),
                    mode="nearest"),
            ]),
        # 18种位置不变的效果
        iaa.SomeOf(
            (1, 3),
            [
                iaa.GaussianBlur(),
                iaa.AverageBlur(),
                iaa.MedianBlur(),
                iaa.Sharpen(),
                iaa.BilateralBlur(),  # 既噪音又模糊,叫双边,
                iaa.MotionBlur(),
                iaa.MeanShiftBlur(),
                iaa.GammaContrast(),
                iaa.SigmoidContrast(),
                iaa.Fog(),
                iaa.Clouds(),
                iaa.Snowflakes(flake_size=(0.1, 0.2), density=(0.005, 0.025)),
                iaa.Rain(nb_iterations=1,
                         drop_size=(0.05, 0.1),
                         speed=(0.04, 0.08)),
                iaa.AdditiveGaussianNoise(scale=(0, 10)),
                iaa.AdditiveLaplaceNoise(scale=(0, 10)),
                iaa.AdditivePoissonNoise(lam=(0, 10)),
                iaa.Salt((0, 0.02)),
                iaa.Pepper((0, 0.02))
            ])
    ])

    polys = [ia.Polygon(pos) for pos in pos_list]
    polygons = ia.PolygonsOnImage(polys, shape=image.shape)
    images_aug, polygons_aug = seq(images=[image], polygons=polygons)
    image = images_aug[0]
    image = polygons_aug.draw_on_image(image, size=2)

    new_polys = []
    for p in polygons_aug.polygons:
        new_polys.append(p.coords)
    polys = np.array(new_polys, np.int32).tolist()

    return image, polys