Exemplo n.º 1
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
Exemplo n.º 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"
        ]
Exemplo n.º 3
0
    def test_to_normalized_batch__all_columns(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 = batch.to_normalized_batch()

        assert isinstance(batch_norm, ia.Batch)
        assert ia.is_np_array(batch_norm.images_unaug)
        assert batch_norm.images_unaug.shape == (1, 2, 2, 3)
        assert isinstance(batch_norm.heatmaps_unaug[0], ia.HeatmapsOnImage)
        assert isinstance(batch_norm.segmentation_maps_unaug[0],
                          ia.SegmentationMapsOnImage)
        assert isinstance(batch_norm.keypoints_unaug[0], ia.KeypointsOnImage)
        assert isinstance(batch_norm.bounding_boxes_unaug[0],
                          ia.BoundingBoxesOnImage)
        assert isinstance(batch_norm.polygons_unaug[0], ia.PolygonsOnImage)
        assert isinstance(batch_norm.line_strings_unaug[0],
                          ia.LineStringsOnImage)
        assert batch_norm.get_column_names() == [
            "images", "heatmaps", "segmentation_maps", "keypoints",
            "bounding_boxes", "polygons", "line_strings"
        ]
Exemplo n.º 4
0
    def __call__(self, results):

        Affine = True if np.random.rand() < self.Affine_ratio else False
        if Affine:
            polys = []
            for bx in results['gt_bboxes']:
                bx = ((bx[0], bx[1]), (bx[2], bx[3]), bx[4])
                # rbox를 polygon으로 변환
                poly = cv2.boxPoints(bx)
                polys.append(ia.Polygon(poly))

            image_aug, poly_aug = self.seq(images=[results['img']],
                                           polygons=[polys])
            image_aug, poly_aug = image_aug[0], poly_aug[0]

            rboxs = []
            for poly in poly_aug:
                # polygon을 rbox로 변환
                rc = cv2.minAreaRect(poly.coords)
                rboxs.append([rc[0][0], rc[0][1], rc[1][0], rc[1][1], rc[2]])
            rboxs = np.array(rboxs)

            results['img'] = image_aug
            results['gt_bboxes'] = rboxs
            results['rotate'] = False
        return results
Exemplo n.º 5
0
def example_augment_images_and_polygons():
    print("Example: Augment Images and Polygons")
    import numpy as np
    import imgaug as ia
    import imgaug.augmenters as iaa

    images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
    images[:, 64, 64, :] = 255
    polygons = [[ia.Polygon([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
                [
                    ia.Polygon([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0),
                                (64.5, 128.0)])
                ]]

    seq = iaa.Sequential([
        iaa.AdditiveGaussianNoise(scale=0.05 * 255),
        iaa.Affine(translate_px={"x": (1, 5)})
    ])

    images_aug, polygons_aug = seq(images=images, polygons=polygons)
Exemplo n.º 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"
        ]
Exemplo n.º 7
0
    def __call__(self, results):

        if 'flip' not in results:
            flip = True if np.random.rand() < self.flip_ratio else False
            results['flip'] = flip
        if results['flip']:
            """
            # flip image
            results['img'] = mmcv.imflip(results['img'])
            # flip bboxes
            for key in results.get('bbox_fields', []):
                results[key] = rbox_flip(results[key],
                                         results['img_shape'])
            # flip cluster if exist
            if 'gt_clusters' in results and results['gt_clusters'] is not None:
                results['gt_clusters'] = self.bbox_flip(results['gt_clusters'],
                                                        results['img_shape'])
            # flip masks
            for key in results.get('mask_fields', []):
                results[key] = [mask[:, ::-1] for mask in results[key]]
            """
            polys = []
            for bx in results['gt_bboxes']:
                bx = ((bx[0], bx[1]), (bx[2], bx[3]), bx[4])
                # rbox를 polygon으로 변환
                poly = cv2.boxPoints(bx)
                polys.append(ia.Polygon(poly))

            image_aug, poly_aug = self.seq(images=[results['img']],
                                           polygons=[polys])
            image_aug, poly_aug = image_aug[0], poly_aug[0]

            rboxs = []
            for poly in poly_aug:
                # polygon을 rbox로 변환
                rc = cv2.minAreaRect(poly.coords)
                rboxs.append([rc[0][0], rc[0][1], rc[1][0], rc[1][1], rc[2]])
            rboxs = np.array(rboxs)

            results['img'] = image_aug
            results['gt_bboxes'] = rboxs

        return results
Exemplo n.º 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
Exemplo n.º 9
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)
Exemplo n.º 10
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
Exemplo n.º 11
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)
Exemplo n.º 12
0
    def read_images_annos(self, images_path, annos_path):
        self.images = []
        self.images_name = []
        self.images_format = []
        self.polygons = []
        self.json_dicts = []
        if self.anno_type == 'labelme':
            assert (len(images_path) == len(annos_path)
                    ), '{} not the same {}'.format(len(images_path),
                                                   len(annos_path))
            ## read polygons points from labelme json
            for i in range(len(images_path)):
                image_path = images_path[i]
                image_name = os.path.basename(image_path).split('.')[0]
                image_format = os.path.basename(image_path).split('.')[1]
                try:
                    image = cv2.imread(image_path)
                except:
                    print('read {} failed'.format(image_path))
                    continue

                self.images.append(image)
                self.images_name.append(image_name)
                self.images_format.append(image_format)

                anno_path = annos_path[i]
                with open(anno_path, "r") as f:
                    json_dict = json.load(f)
                self.json_dicts.append(json_dict)
                polygon = []  ## polygon with one picture
                for j in range(len(json_dict["shapes"])):
                    keypoints = []
                    for kp_list in json_dict["shapes"][j]["points"]:
                        keypoints.append((kp_list[0], kp_list[1]))
                    polygon.append(ia.Polygon(keypoints))
                    #print('keypoints', keypoints)
                self.polygons.append(polygon)
        else:
            print('{} is not supported'.format(self.anno_type))
            sys.exit(-1)
Exemplo n.º 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)]
Exemplo n.º 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
Exemplo n.º 15
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
Exemplo n.º 16
0
def augment_half(seq,
                 images_dir,
                 lables_dir,
                 output_dir,
                 start_num=1,
                 bbox_type="obb"):
    assert Path(images_dir).is_dir(), "images_dir is not exist"
    assert Path(lables_dir).is_dir(), "lables_dir is not exist"
    assert Path(output_dir).is_dir(), "output_dir is not exist"

    #seq=get_seq(0, 0, 0, 0, 0, 0, 0, 1)

    end = os.listdir(images_dir)[0].split(".")[-1]
    txts_list = os.listdir(lables_dir)
    nums = int(len(txts_list) * 0.5)
    random.shuffle(txts_list)

    if bbox_type == "obb":
        for txt in txts_list[:nums]:
            print("process: ", txt)
            polygons_list = []
            with open(os.path.join(lables_dir, txt), 'r') as f:
                lines = f.readlines()

            # 读label
            for line in lines:
                label = line.split()[0]
                coors = np.array(list(map(lambda x: int(x),
                                          line.split()[1:]))).reshape((4, 2))
                coor_list = [tuple(x) for x in coors]
                # polygons = ia.Polygon(coor_list, label=label)
                polygons_list.append(ia.Polygon(coor_list, label=label))

            # 读image
            img_path = os.path.join(images_dir, txt.split(".")[0] + "." + end)
            img = cv2.imread(img_path)
            image_aug, polygons_aug = seq(image=img, polygons=polygons_list)
            cv2.imwrite(
                os.path.join(output_dir, "images",
                             str(start_num) + "." + end), image_aug)

            # save label
            with open(
                    os.path.join(output_dir, "label",
                                 str(start_num) + ".txt"), "w") as f:
                for x in polygons_aug:
                    wr_str = str(x.label) + " " + \
                        str(int(round(x[0][0]))) + " " + str(int(round(x[0][1]))) + " " + \
                        str(int(round(x[1][0]))) + " " + str(int(round(x[1][1]))) + " " + \
                        str(int(round(x[2][0]))) + " " + str(int(round(x[2][1]))) + " " + \
                        str(int(round(x[3][0]))) + " " + \
                        str(int(round(x[3][1]))) + "\n"
                    f.write(wr_str)

            start_num += 1

        return start_num

    elif bbox_type == "hbb":
        for txt in txts_list[:nums]:
            print("process: ", txt)
            bbox_list = []
            with open(os.path.join(lables_dir, txt), 'r') as f:
                lines = f.readlines()

            # 读label
            for line in lines:
                print(line)
                label = line.split()[0]
                coor = list(map(lambda x: int(x), line.split()[1:]))
                #coor_list = [tuple([coor[0], coor[1]]), tuple([coor[0]+coor[2], coor[1]+coor[3]])]
                bbox_list.append(
                    BoundingBox(coor[0], coor[1], coor[0] + coor[2],
                                coor[1] + coor[3], label))
            #print(bbox_list)

            # 读image
            img = cv2.imread(
                os.path.join(images_dir,
                             txt.split(".")[0] + "." + end))
            image_aug, bbox_aug = seq(image=img, bounding_boxes=bbox_list)

            if not os.path.exists(os.path.join(output_dir, "images")):
                os.makedirs(os.path.join(output_dir, "images"))
            if not os.path.exists(os.path.join(output_dir, "labels")):
                os.makedirs(os.path.join(output_dir, "labels"))

            #  save image
            cv2.imwrite(
                os.path.join(output_dir, "images",
                             str(start_num) + "." + end), image_aug)

            # save label
            with open(
                    os.path.join(output_dir, "labels",
                                 str(start_num) + ".txt"), "w") as f:
                print(bbox_aug[0])
                print(bbox_aug[0][0])
                print(bbox_aug[0][1])
                for x in bbox_aug:
                    wr_str = str(x.label) + " " + \
                             str(int(round(x[0][0]))) + " " + \
                             str(int(round(x[0][1]))) + " " + \
                             str(int(round(x[1][0] - x[0][0]))) + " " + \
                             str(int(round(x[1][1] - x[0][1]))) + "\n"
                    f.write(wr_str)

            start_num += 1
        return start_num
data={}
data["images"]=[]
data["annotations"]=[]

with open('output.json','w') as output_json:
    num_images=len(data_from_labstudio["images"])
    count=0

    for i in range(num_images):
        segment=data_from_labstudio["annotations"][i]["segmentation"][0]
        pts=data_from_labstudio["annotations"][i]["bbox"]
        #x,y, x+w, y+h
        bb=[pts[0], pts[1], pts[0] + pts[2], pts[1]+ pts[3]]
        poly = [ia.Polygon([
            (segment[0],segment[1]), 
            (segment[2],segment[3]), 
            (segment[4],segment[5]), 
            (segment[6],segment[7])]
            )]
        bbox=[ia.BoundingBox(x1=bb[0], y1=bb[1], x2=bb[2], y2=bb[3])]
        
        img=cv2.imread(data_from_labstudio["images"][i]["file_name"])

        if RESIZE:
            seq=iaa.Sequential([final_resize_step])
            img_aug, polygon_aug, bbox_aug = seq(image=img, polygons=poly, bounding_boxes=bbox)
        else:
            img_aug=img
            polygon_aug=poly
            bbox_aug=bbox

Exemplo n.º 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
Exemplo n.º 19
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]
Exemplo n.º 20
0
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
images[:, 64, 64, :] = 255
polygons = [
    [ia.Polygon([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
    [ia.Polygon([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0)])]
]

seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=0.05*255),
    iaa.Affine(translate_px={"x": (1, 5)})
])

images_aug, polygons_aug = seq(images=images, polygons=polygons)
print(polygons_aug)
Exemplo n.º 21
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])
Exemplo n.º 22
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]
Exemplo n.º 23
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)]
Exemplo n.º 24
0
 def psoi(self):
     polygons = [ia.Polygon([(0, 0), (2, 0), (2, 2)])]
     return [ia.PolygonsOnImage(polygons, shape=self.image.shape)]
Exemplo n.º 25
0
 def to_imgaug(self, shape):
     import imgaug
     ia_exterior = imgaug.Polygon(self.data['exterior'])
     ia_interiors = [imgaug.Polygon(p) for p in self.data.get('interiors', [])]
     iamp = imgaug.MultiPolygon([ia_exterior] + ia_interiors)
     return iamp
Exemplo n.º 26
0
        data = [label for label in data if label["Label"] != "Skip"]

        images = []
        polygons = []
        for label in tqdm(data):
            # CONSTANTS
            IMG_NAME = label["External ID"]
            IMPORTANT_VERTICES = label["Label"]["important"][0]["geometry"]
            # BILL_VERTICES = label["Label"]["bill"][0]["geometry"]

            raw_img = cv2.imread(os.path.join(FULL_PATH, IMG_NAME),
                                 cv2.IMREAD_GRAYSCALE)

            # load every picture as grayscale and create dictionary
            images.append(raw_img)
            poly = ia.Polygon(vertice_chunks(IMPORTANT_VERTICES))
            polygons.append([poly])

        # images, polygons = seq(images=images, polygons=polygons)
        for i, img in enumerate(images):
            img_vertices = []
            for idx in range(len(polygons[i][0].xx)):
                img_vertices.append(polygons[i][0].xx[idx])
                img_vertices.append(polygons[i][0].yy[idx])

            # resize image to fixed resolution
            resized_img = cv2.resize(img, (RES_X, RES_Y))

            # calculate ratios to transform labeled coordinates to resized image
            RATIO_X = RES_X / raw_img.shape[1]
            RATIO_Y = RES_Y / raw_img.shape[0]
Exemplo n.º 27
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)
Exemplo n.º 28
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]