Ejemplo n.º 1
0
    def test_can_convert_polygons_to_mask(self):
        label_categories = LabelCategories()
        for i in range(10):
            label_categories.add(str(i))

        class SrcTestExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(id=1,
                                image=np.zeros((6, 10, 3)),
                                annotations=[
                                    Polygon([0, 0, 4, 0, 4, 4],
                                            label=3,
                                            id=4,
                                            group=4),
                                    Polygon([5, 0, 9, 0, 5, 5],
                                            label=3,
                                            id=4,
                                            group=4),
                                ]),
                ])

            def categories(self):
                return {AnnotationType.label: label_categories}

        class DstTestExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(
                        id=1,
                        image=np.zeros((6, 10, 3)),
                        annotations=[
                            Mask(
                                np.array(
                                    [[0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
                                     [0, 0, 1, 1, 0, 1, 1, 1, 0, 0],
                                     [0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
                                     [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                                    # only internal fragment (without the border),
                                    # but not everywhere...
                                ),
                                attributes={'is_crowd': True},
                                label=3,
                                id=4,
                                group=4),
                        ],
                        attributes={'id': 1}),
                ])

            def categories(self):
                return {AnnotationType.label: label_categories}

        with TestDir() as test_dir:
            self._test_save_and_load(
                SrcTestExtractor(),
                CocoInstancesConverter(segmentation_mode='mask'),
                test_dir,
                target_dataset=DstTestExtractor())
Ejemplo n.º 2
0
    def test_can_crop_covered_segments(self):
        label_categories = LabelCategories()
        for i in range(10):
            label_categories.add(str(i))

        class SrcTestExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(id=1,
                                image=np.zeros((5, 5, 3)),
                                annotations=[
                                    Mask(np.array(
                                        [[0, 0, 1, 1, 1], [0, 0, 1, 1, 1],
                                         [1, 1, 0, 1, 1], [1, 1, 1, 0, 0],
                                         [1, 1, 1, 0, 0]], ),
                                         label=2,
                                         id=1,
                                         z_order=0),
                                    Polygon([1, 1, 4, 1, 4, 4, 1, 4],
                                            label=1,
                                            id=2,
                                            z_order=1),
                                ]),
                ])

            def categories(self):
                return {AnnotationType.label: label_categories}

        class DstTestExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(id=1,
                                image=np.zeros((5, 5, 3)),
                                annotations=[
                                    Mask(np.array(
                                        [[0, 0, 1, 1, 1], [0, 0, 0, 0, 1],
                                         [1, 0, 0, 0, 1], [1, 0, 0, 0, 0],
                                         [1, 1, 1, 0, 0]], ),
                                         attributes={'is_crowd': True},
                                         label=2,
                                         id=1,
                                         group=1),
                                    Polygon([1, 1, 4, 1, 4, 4, 1, 4],
                                            label=1,
                                            id=2,
                                            group=2,
                                            attributes={'is_crowd': False}),
                                ],
                                attributes={'id': 1}),
                ])

            def categories(self):
                return {AnnotationType.label: label_categories}

        with TestDir() as test_dir:
            self._test_save_and_load(SrcTestExtractor(),
                                     CocoInstancesConverter(crop_covered=True),
                                     test_dir,
                                     target_dataset=DstTestExtractor())
Ejemplo n.º 3
0
    def test_can_convert_masks_to_polygons(self):
        label_categories = LabelCategories()
        for i in range(10):
            label_categories.add(str(i))

        class SrcExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(id=1,
                                image=np.zeros((5, 10, 3)),
                                annotations=[
                                    Mask(np.array([
                                        [0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
                                        [0, 0, 1, 1, 0, 1, 1, 1, 0, 0],
                                        [0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                                    ]),
                                         label=3,
                                         id=4,
                                         group=4),
                                ]),
                ])

            def categories(self):
                return {AnnotationType.label: label_categories}

        class DstExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(
                        id=1,
                        image=np.zeros((5, 10, 3)),
                        annotations=[
                            Polygon([3.0, 2.5, 1.0, 0.0, 3.5, 0.0, 3.0, 2.5],
                                    label=3,
                                    id=4,
                                    group=4,
                                    attributes={'is_crowd': False}),
                            Polygon([5.0, 3.5, 4.5, 0.0, 8.0, 0.0, 5.0, 3.5],
                                    label=3,
                                    id=4,
                                    group=4,
                                    attributes={'is_crowd': False}),
                        ],
                        attributes={'id': 1}),
                ])

            def categories(self):
                return {AnnotationType.label: label_categories}

        with TestDir() as test_dir:
            self._test_save_and_load(
                SrcExtractor(),
                CocoInstancesConverter(segmentation_mode='polygons'),
                test_dir,
                target_dataset=DstExtractor())
Ejemplo n.º 4
0
    def test_can_merge_polygons_on_loading(self):
        label_categories = LabelCategories()
        for i in range(10):
            label_categories.add(str(i))
        categories = {AnnotationType.label: label_categories}

        class TestExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(id=1,
                                image=np.zeros((6, 10, 3)),
                                annotations=[
                                    Polygon([0, 0, 4, 0, 4, 4],
                                            label=3,
                                            id=4,
                                            group=4),
                                    Polygon([5, 0, 9, 0, 5, 5],
                                            label=3,
                                            id=4,
                                            group=4),
                                ]),
                ])

            def categories(self):
                return categories

        class TargetExtractor(TestExtractor):
            def __iter__(self):
                items = list(super().__iter__())
                items[0]._annotations = [
                    Mask(
                        np.array(
                            [[0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
                             [0, 0, 1, 1, 0, 1, 1, 1, 0, 0],
                             [0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
                             [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                            # only internal fragment (without the border),
                            # but not everywhere...
                        ),
                        label=3,
                        id=4,
                        group=4,
                        attributes={'is_crowd': False}),
                ]
                return iter(items)

        with TestDir() as test_dir:
            self._test_save_and_load(
                TestExtractor(),
                CocoInstancesConverter(),
                test_dir,
                importer_args={'merge_instance_polygons': True},
                target_dataset=TargetExtractor())
Ejemplo n.º 5
0
    def test_can_save_and_load_instances(self):
        label_categories = LabelCategories()
        for i in range(10):
            label_categories.add(str(i))
        categories = {AnnotationType.label: label_categories}

        class TestExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(
                        id=1,
                        subset='train',
                        image=np.ones((4, 4, 3)),
                        annotations=[
                            # Bbox + single polygon
                            Bbox(0,
                                 1,
                                 2,
                                 2,
                                 label=2,
                                 group=1,
                                 id=1,
                                 attributes={'is_crowd': False}),
                            Polygon([0, 1, 2, 1, 2, 3, 0, 3],
                                    attributes={'is_crowd': False},
                                    label=2,
                                    group=1,
                                    id=1),
                        ]),
                    DatasetItem(
                        id=2,
                        subset='train',
                        image=np.ones((4, 4, 3)),
                        annotations=[
                            # Mask + bbox
                            Mask(np.array([[0, 1, 0, 0], [0, 1, 0, 0],
                                           [0, 1, 1, 1], [0, 0, 0, 0]], ),
                                 attributes={'is_crowd': True},
                                 label=4,
                                 group=3,
                                 id=3),
                            Bbox(1,
                                 0,
                                 2,
                                 2,
                                 label=4,
                                 group=3,
                                 id=3,
                                 attributes={'is_crowd': True}),
                        ]),
                    DatasetItem(
                        id=3,
                        subset='val',
                        image=np.ones((4, 4, 3)),
                        annotations=[
                            # Bbox + mask
                            Bbox(0,
                                 1,
                                 2,
                                 2,
                                 label=4,
                                 group=3,
                                 id=3,
                                 attributes={'is_crowd': True}),
                            Mask(np.array([[0, 0, 0, 0], [1, 1, 1, 0],
                                           [1, 1, 0, 0], [0, 0, 0, 0]], ),
                                 attributes={'is_crowd': True},
                                 label=4,
                                 group=3,
                                 id=3),
                        ]),
                ])

            def categories(self):
                return categories

        class DstExtractor(Extractor):
            def __iter__(self):
                return iter([
                    DatasetItem(id=1,
                                subset='train',
                                image=np.ones((4, 4, 3)),
                                annotations=[
                                    Polygon([0, 1, 2, 1, 2, 3, 0, 3],
                                            attributes={'is_crowd': False},
                                            label=2,
                                            group=1,
                                            id=1),
                                ]),
                    DatasetItem(id=2,
                                subset='train',
                                image=np.ones((4, 4, 3)),
                                annotations=[
                                    Mask(np.array(
                                        [[0, 1, 0, 0], [0, 1, 0, 0],
                                         [0, 1, 1, 1], [0, 0, 0, 0]], ),
                                         attributes={'is_crowd': True},
                                         label=4,
                                         group=3,
                                         id=3),
                                ]),
                    DatasetItem(id=3,
                                subset='val',
                                image=np.ones((4, 4, 3)),
                                annotations=[
                                    Mask(np.array(
                                        [[0, 0, 0, 0], [1, 1, 1, 0],
                                         [1, 1, 0, 0], [0, 0, 0, 0]], ),
                                         attributes={'is_crowd': True},
                                         label=4,
                                         group=3,
                                         id=3),
                                ]),
                ])

            def categories(self):
                return categories

        with TestDir() as test_dir:
            self._test_save_and_load(TestExtractor(),
                                     CocoInstancesConverter(),
                                     test_dir,
                                     target_dataset=DstExtractor())