示例#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
示例#2
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"
        ]
示例#3
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"
        ]
示例#4
0
def example_augment_images_and_linestrings():
    print("Example: Augment Images and LineStrings")
    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
    ls = [[ia.LineString([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
          [
              ia.LineString([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0),
                             (64.5, 128.0), (128.0, 0.0)])
          ]]

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

    images_aug, ls_aug = seq(images=images, line_strings=ls)
示例#5
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"
        ]
示例#6
0
    def test_two_images_and_line_strings(self):
        rng = iarandom.RNG(0)
        images = rng.integers(0, 256, size=(2, 32, 32, 3), dtype=np.uint8)
        ls = []
        for x in np.linspace(0, 256, 4):
            for y in np.linspace(0, 256, 4):
                ls.append(
                    ia.LineString([(x, y), (x + 20, y), (x + 20, y + 20),
                                   (x, y + 20)]))
        lsoi1 = ia.LineStringsOnImage(ls, shape=images[0].shape)
        lsoi2 = lsoi1.deepcopy()
        image1_w_overlay = lsoi1.draw_on_image(images[0])
        image2_w_overlay = lsoi2.draw_on_image(images[1])

        debug_image = iaa.draw_debug_image(images, line_strings=[lsoi1, lsoi2])

        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)
示例#7
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)]
示例#8
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]
示例#9
0
 def lsoi_flipped(self):
     ls = [ia.LineString([(0, 3 - 0), (2, 3 - 0), (2, 3 - 2)])]
     return [ia.LineStringsOnImage(ls, shape=self.image.shape)]
示例#10
0
 def lsoi(self):
     ls = [ia.LineString([(0, 0), (2, 0), (2, 2)])]
     return [ia.LineStringsOnImage(ls, shape=self.image.shape)]
示例#11
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]