def test_remap_labels_ignore_missing_labels_in_secondary_categories(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, annotations=[ Label(0), ]) ], categories={ AnnotationType.label: LabelCategories.from_iterable(['a', 'b', 'c']), AnnotationType.points: PointsCategories.from_iterable([]), # all missing AnnotationType.mask: MaskCategories.generate(2) # no c color }) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, annotations=[ Label(0), ]), ], categories={ AnnotationType.label: LabelCategories.from_iterable(['d', 'e', 'f']), AnnotationType.points: PointsCategories.from_iterable([]), AnnotationType.mask: MaskCategories.generate(2) }) actual = transforms.RemapLabels(source_dataset, mapping={ 'a': 'd', 'b': 'e', 'c': 'f' }, default='delete') compare_datasets(self, target_dataset, actual)
def test_project_labels_maps_secondary_categories(self): source = Dataset.from_iterable([], categories={ AnnotationType.label: LabelCategories.from_iterable([ 'a', 'b', # no parents ('c', 'a'), ('d', 'b') # have parents ]), AnnotationType.points: PointsCategories.from_iterable([ (0, ['a']), (1, ['b']), (2, ['c']) ]), AnnotationType.mask: MaskCategories.generate(4) }) expected = Dataset.from_iterable([], categories={ AnnotationType.label: LabelCategories.from_iterable([ ('c', 'a'), # must keep parent 'a', 'd' # must drop parent because it was removed ]), AnnotationType.points: PointsCategories.from_iterable([ (0, ['c']), (1, ['a']) ]), AnnotationType.mask: MaskCategories(colormap={ i: v for i, v in { { 2: 0, 0: 1, 3: 2 }.get(k): v for k, v in mask_tools.generate_colormap(4).items() }.items() if i is not None }), }) actual = transforms.ProjectLabels(source, dst_labels=['c', 'a', 'd']) compare_datasets(self, expected, actual)
def test_project_labels_generates_colors_for_added_labels(self): source = Dataset.from_iterable([], categories={ AnnotationType.label: LabelCategories.from_iterable(['a', 'b', 'c']), AnnotationType.mask: MaskCategories.generate(2) }) actual = transforms.ProjectLabels(source, dst_labels=['a', 'c', 'd']) self.assertEqual((0, 0, 0), actual.categories()[AnnotationType.mask][0]) self.assertNotIn(1, actual.categories()[AnnotationType.mask]) self.assertIn(2, actual.categories()[AnnotationType.mask])
def test_inplace_save_writes_only_updated_data(self): src_mask_cat = MaskCategories.generate(3, include_background=False) expected = Dataset.from_iterable( [ DatasetItem(1, subset='a', image=np.ones((2, 1, 3)), annotations=[Mask(np.ones((2, 1)), label=2)]), DatasetItem(2, subset='a', image=np.ones((3, 2, 3))), DatasetItem(2, subset='b'), ], categories=Camvid.make_camvid_categories( OrderedDict([ ('background', (0, 0, 0)), ('a', src_mask_cat.colormap[0]), ('b', src_mask_cat.colormap[1]), ]))) with TestDir() as path: dataset = Dataset.from_iterable( [ DatasetItem(1, subset='a', image=np.ones((2, 1, 3)), annotations=[Mask(np.ones((2, 1)), label=1)]), DatasetItem(2, subset='b'), DatasetItem(3, subset='c', image=np.ones((2, 2, 3)), annotations=[Mask(np.ones((2, 2)), label=0)]), ], categories={ AnnotationType.label: LabelCategories.from_iterable(['a', 'b']), AnnotationType.mask: src_mask_cat }) dataset.export(path, 'camvid', save_images=True) dataset.put(DatasetItem(2, subset='a', image=np.ones((3, 2, 3)))) dataset.remove(3, 'c') dataset.save(save_images=True) self.assertEqual( {'a', 'aannot', 'a.txt', 'b.txt', 'label_colors.txt'}, set(os.listdir(path))) self.assertEqual({'1.jpg', '2.jpg'}, set(os.listdir(osp.join(path, 'a')))) compare_datasets(self, expected, Dataset.import_from(path, 'camvid'), require_images=True)
def test_inplace_save_writes_only_updated_data(self): src_mask_cat = MaskCategories.generate(2, include_background=False) expected = Dataset.from_iterable([ DatasetItem(1, subset='a', image=np.ones((2, 1, 3)), annotations=[ Mask(np.ones((2, 1)), label=2, id=1) ]), DatasetItem(2, subset='a', image=np.ones((3, 2, 3))), DatasetItem(2, subset='b', image=np.ones((2, 2, 3)), annotations=[ Mask(np.ones((2, 2)), label=1, id=1) ]), ], categories=Cityscapes.make_cityscapes_categories(OrderedDict([ ('a', src_mask_cat.colormap[0]), ('b', src_mask_cat.colormap[1]), ]))) with TestDir() as path: dataset = Dataset.from_iterable([ DatasetItem(1, subset='a', image=np.ones((2, 1, 3)), annotations=[ Mask(np.ones((2, 1)), label=1) ]), DatasetItem(2, subset='b', image=np.ones((2, 2, 3)), annotations=[ Mask(np.ones((2, 2)), label=0) ]), DatasetItem(3, subset='c', image=np.ones((2, 3, 3)), annotations=[ Mask(np.ones((2, 2)), label=0) ]), ], categories={ AnnotationType.label: LabelCategories.from_iterable(['a', 'b']), AnnotationType.mask: src_mask_cat }) dataset.export(path, 'cityscapes', save_images=True) dataset.put(DatasetItem(2, subset='a', image=np.ones((3, 2, 3)))) dataset.remove(3, 'c') dataset.save(save_images=True) self.assertEqual({'a', 'b'}, set(os.listdir(osp.join(path, 'gtFine')))) self.assertEqual({ '1_gtFine_color.png', '1_gtFine_instanceIds.png', '1_gtFine_labelIds.png' }, set(os.listdir(osp.join(path, 'gtFine', 'a')))) self.assertEqual({ '2_gtFine_color.png', '2_gtFine_instanceIds.png', '2_gtFine_labelIds.png' }, set(os.listdir(osp.join(path, 'gtFine', 'b')))) self.assertEqual({'a', 'b'}, set(os.listdir(osp.join(path, 'imgsFine', 'leftImg8bit')))) self.assertEqual({'1_leftImg8bit.png', '2_leftImg8bit.png'}, set(os.listdir(osp.join(path, 'imgsFine', 'leftImg8bit', 'a')))) self.assertEqual({'2_leftImg8bit.png'}, set(os.listdir(osp.join(path, 'imgsFine', 'leftImg8bit', 'b')))) compare_datasets(self, expected, Dataset.import_from(path, 'cityscapes'), require_images=True, ignored_attrs=IGNORE_ALL)
def test_can_compare_projects(self): # just a smoke test label_categories1 = LabelCategories.from_iterable(['x', 'a', 'b', 'y']) mask_categories1 = MaskCategories.generate(len(label_categories1)) point_categories1 = PointsCategories() for index, _ in enumerate(label_categories1.items): point_categories1.add(index, ['cat1', 'cat2'], joints=[[0, 1]]) dataset1 = Dataset.from_iterable([ DatasetItem(id=100, subset='train', image=np.ones((10, 6, 3)), annotations=[ Caption('hello', id=1), Caption('world', id=2, group=5), Label(2, id=3, attributes={ 'x': 1, 'y': '2', }), Bbox(1, 2, 3, 4, label=0, id=4, z_order=1, attributes={ 'score': 1.0, }), Bbox(5, 6, 7, 8, id=5, group=5), Points([1, 2, 2, 0, 1, 1], label=0, id=5, z_order=4), Mask(label=3, id=5, z_order=2, image=np.ones((2, 3))), ]), DatasetItem(id=21, subset='train', annotations=[ Caption('test'), Label(2), Bbox(1, 2, 3, 4, label=2, id=42, group=42) ]), DatasetItem(id=2, subset='val', annotations=[ PolyLine([1, 2, 3, 4, 5, 6, 7, 8], id=11, z_order=1), Polygon([1, 2, 3, 4, 5, 6, 7, 8], id=12, z_order=4), ]), DatasetItem(id=42, subset='test', attributes={'a1': 5, 'a2': '42'}), DatasetItem(id=42), DatasetItem(id=43, image=Image(path='1/b/c.qq', size=(2, 4))), ], categories={ AnnotationType.label: label_categories1, AnnotationType.mask: mask_categories1, AnnotationType.points: point_categories1, }) label_categories2 = LabelCategories.from_iterable(['a', 'b', 'x', 'y']) mask_categories2 = MaskCategories.generate(len(label_categories2)) point_categories2 = PointsCategories() for index, _ in enumerate(label_categories2.items): point_categories2.add(index, ['cat1', 'cat2'], joints=[[0, 1]]) dataset2 = Dataset.from_iterable([ DatasetItem(id=100, subset='train', image=np.ones((10, 6, 3)), annotations=[ Caption('hello', id=1), Caption('world', id=2, group=5), Label(2, id=3, attributes={ 'x': 1, 'y': '2', }), Bbox(1, 2, 3, 4, label=1, id=4, z_order=1, attributes={ 'score': 1.0, }), Bbox(5, 6, 7, 8, id=5, group=5), Points([1, 2, 2, 0, 1, 1], label=0, id=5, z_order=4), Mask(label=3, id=5, z_order=2, image=np.ones((2, 3))), ]), DatasetItem(id=21, subset='train', annotations=[ Caption('test'), Label(2), Bbox(1, 2, 3, 4, label=3, id=42, group=42) ]), DatasetItem(id=2, subset='val', annotations=[ PolyLine([1, 2, 3, 4, 5, 6, 7, 8], id=11, z_order=1), Polygon([1, 2, 3, 4, 5, 6, 7, 8], id=12, z_order=4), ]), DatasetItem(id=42, subset='test', attributes={'a1': 5, 'a2': '42'}), DatasetItem(id=42), DatasetItem(id=43, image=Image(path='1/b/c.qq', size=(2, 4))), ], categories={ AnnotationType.label: label_categories2, AnnotationType.mask: mask_categories2, AnnotationType.points: point_categories2, }) with TestDir() as test_dir: with DiffVisualizer(save_dir=test_dir, comparator=DistanceComparator(iou_threshold=0.8), ) as visualizer: visualizer.save(dataset1, dataset2) self.assertNotEqual(0, os.listdir(osp.join(test_dir)))