def categories(self): label_cat = LabelCategories() for label in range(10): label_cat.add('label_' + str(label)) return { AnnotationType.label: label_cat, }
def _generate_classification_dataset(self, config, num_duplicate): subsets = ["train", "val", "test"] dummy_images = [ np.random.randint(0, 255, size=(224, 224, 3)) for _ in range(num_duplicate) ] iterable = [] label_cat = LabelCategories() idx = 0 for label_id, label in enumerate(config.keys()): label_cat.add(label, attributes=None) num_item = config[label] for subset in subsets: for _ in range(num_item): idx += 1 iterable.append( DatasetItem( idx, subset=subset, annotations=[Label(label_id)], image=Image(data=dummy_images[idx % num_duplicate]), )) categories = {AnnotationType.label: label_cat} dataset = Dataset.from_iterable(iterable, categories) return dataset
def categories(self): label_cat = LabelCategories() for label in VOC.VocLabel: label_cat.add(label.name) return { AnnotationType.label: label_cat, }
def categories(self): label_categories = LabelCategories() for i in range(10): label_categories.add(str(i)) return { AnnotationType.label: label_categories, }
def categories(self): label_categories = LabelCategories() label_categories.add('label1', attributes={'a1', 'a2'}) label_categories.add('label2') return { AnnotationType.label: label_categories, }
def categories(self): label_cat = LabelCategories() label_cat.add('Label_1') # should become lowercase label_cat.add('label_2') return { AnnotationType.label: label_cat, }
def _make_label_id_map(self, src_label_cat, label_mapping, default_action): dst_label_cat = LabelCategories(attributes=src_label_cat.attributes) id_mapping = {} for src_index, src_label in enumerate(src_label_cat.items): dst_label = label_mapping.get(src_label.name) if not dst_label and default_action == self.DefaultAction.keep: dst_label = src_label.name # keep unspecified as is if not dst_label: continue dst_index = dst_label_cat.find(dst_label)[0] if dst_index is None: dst_label_cat.add(dst_label, src_label.parent, src_label.attributes) dst_index = dst_label_cat.find(dst_label)[0] id_mapping[src_index] = dst_index if log.getLogger().isEnabledFor(log.DEBUG): log.debug("Label mapping:") for src_id, src_label in enumerate(src_label_cat.items): if id_mapping.get(src_id): log.debug("#%s '%s' -> #%s '%s'", src_id, src_label.name, id_mapping[src_id], dst_label_cat.items[id_mapping[src_id]].name ) else: log.debug("#%s '%s' -> <deleted>", src_id, src_label.name) self._map_id = lambda src_id: id_mapping.get(src_id, None) self._categories[AnnotationType.label] = dst_label_cat
def make_camvid_categories(label_map=None): if label_map is None: label_map = CamvidLabelMap # There must always be a label with color (0, 0, 0) at index 0 bg_label = find(label_map.items(), lambda x: x[1] == (0, 0, 0)) if bg_label is not None: bg_label = bg_label[0] else: bg_label = 'background' if bg_label not in label_map: has_colors = any(v is not None for v in label_map.values()) color = (0, 0, 0) if has_colors else None label_map[bg_label] = color label_map.move_to_end(bg_label, last=False) categories = {} label_categories = LabelCategories() for label, desc in label_map.items(): label_categories.add(label) categories[AnnotationType.label] = label_categories has_colors = any(v is not None for v in label_map.values()) if not has_colors: # generate new colors colormap = generate_colormap(len(label_map)) else: # only copy defined colors label_id = lambda label: label_categories.find(label)[0] colormap = { label_id(name): (desc[0], desc[1], desc[2]) for name, desc in label_map.items() } mask_categories = MaskCategories(colormap) mask_categories.inverse_colormap # pylint: disable=pointless-statement categories[AnnotationType.mask] = mask_categories return categories
def _load_categories(parsed): categories = {} parsed_label_cat = parsed['categories'].get(AnnotationType.label.name) if parsed_label_cat: label_categories = LabelCategories() for item in parsed_label_cat['labels']: label_categories.add(item['name'], parent=item['parent']) categories[AnnotationType.label] = label_categories parsed_mask_cat = parsed['categories'].get(AnnotationType.mask.name) if parsed_mask_cat: colormap = {} for item in parsed_mask_cat['colormap']: colormap[int(item['label_id'])] = \ (item['r'], item['g'], item['b']) mask_categories = MaskCategories(colormap=colormap) categories[AnnotationType.mask] = mask_categories parsed_points_cat = parsed['categories'].get( AnnotationType.points.name) if parsed_points_cat: point_categories = PointsCategories() for item in parsed_points_cat['items']: point_categories.add(int(item['label_id']), item['labels'], joints=item['joints']) categories[AnnotationType.points] = point_categories return categories
def categories(self): label_cat = LabelCategories() label_cat.add(VOC.VocLabel.cat.name) label_cat.add('non_voc_label') return { AnnotationType.label: label_cat, }
def make_voc_categories(label_map=None): if label_map is None: label_map = make_voc_label_map() categories = {} label_categories = LabelCategories() label_categories.attributes.update(['difficult', 'truncated', 'occluded']) for label, desc in label_map.items(): label_categories.add(label, attributes=desc[2]) for part in OrderedDict((k, None) for k in chain( *(desc[1] for desc in label_map.values()))): label_categories.add(part) categories[AnnotationType.label] = label_categories has_colors = any(v[0] is not None for v in label_map.values()) if not has_colors: # generate new colors colormap = generate_colormap(len(label_map)) else: # only copy defined colors label_id = lambda label: label_categories.find(label)[0] colormap = { label_id(name): desc[0] for name, desc in label_map.items() if desc[0] is not None } mask_categories = MaskCategories(colormap) mask_categories.inverse_colormap # pylint: disable=pointless-statement categories[AnnotationType.mask] = mask_categories return categories
def categories(self): label_cat = LabelCategories() label_cat.add('Label_1') label_cat.add('label_2') return { AnnotationType.label: label_cat, }
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())
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())
def _load_categories(names_path): label_categories = LabelCategories() with open(names_path, 'r') as f: for label in f: label_categories.add(label.strip()) return label_categories
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())
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())
def categories(self): label_categories = LabelCategories() points_categories = PointsCategories() for i in range(10): label_categories.add(str(i)) points_categories.add(i, []) return { AnnotationType.label: label_categories, AnnotationType.points: points_categories, }
def get_categories(): # output categories - label map etc. label_categories = LabelCategories() with open("samples/imagenet.class", "r") as file: for line in file.readlines(): label = line.strip() label_categories.add(label) return {AnnotationType.label: label_categories}
def _init_categories(self): if self._adapter is None or self._adapter.label_map is None: return None label_map = sorted(self._adapter.label_map.items(), key=lambda e: e[0]) label_cat = LabelCategories() for _, label in label_map: label_cat.add(label) return { AnnotationType.label: label_cat }
def _load_categories(self, labels): attributes = ['track_id'] if self._is_gt: attributes += ['occluded', 'visibility', 'ignored'] else: attributes += ['score'] label_cat = LabelCategories(attributes=attributes) for label in labels: label_cat.add(label) return { AnnotationType.label: label_cat }
def _load_label_categories(self, loader): catIds = loader.getCatIds() cats = loader.loadCats(catIds) categories = LabelCategories() label_map = {} for idx, cat in enumerate(cats): label_map[cat['id']] = idx categories.add(name=cat['name'], parent=cat['supercategory']) return categories, label_map
def categories(self): label_cat = LabelCategories() label_cat.add('window') label_cat.add('license plate') label_cat.add('o1') label_cat.add('q1') label_cat.add('b1') label_cat.add('m1') label_cat.add('hg') return { AnnotationType.label: label_cat, }
def test_dataset(self): label_categories = LabelCategories() for i in range(5): label_categories.add('cat' + str(i)) mask_categories = MaskCategories( generate_colormap(len(label_categories.items))) points_categories = PointsCategories() for index, _ in enumerate(label_categories.items): points_categories.add(index, ['cat1', 'cat2'], joints=[[0, 1]]) return 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=4, 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=5, 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_categories, AnnotationType.mask: mask_categories, AnnotationType.points: points_categories, })
def categories(self): label_cat = LabelCategories() label_cat.add('label0') label_cat.add('label9') label_cat.add('label4') mask_cat = MaskCategories(colormap={ k: v for k, v in mask_tools.generate_colormap(5).items() if k in { 0, 1, 3, 4 } }) return { AnnotationType.label: label_cat, AnnotationType.mask: mask_cat, }
def test_can_allow_undeclared_attrs(self): source_dataset = Dataset.from_iterable([ DatasetItem( id=0, annotations=[ Label(0, attributes={ 'x': 4, 'y': 2 }), Bbox(1, 2, 3, 4, label=0, attributes={ 'x': 1, 'y': 1 }), ]), ], categories=[('a', '', {'x'})]) target_label_cat = LabelCategories(attributes={'occluded'}) target_label_cat.add('a', attributes={'x'}) target_dataset = Dataset.from_iterable( [ DatasetItem(id=0, annotations=[ Label(0, attributes={ 'x': 4, 'y': 2 }), Bbox(1, 2, 3, 4, label=0, attributes={ 'x': 1, 'y': 1, 'occluded': False }), ], attributes={'frame': 0}), ], categories={AnnotationType.label: target_label_cat}) with TestDir() as test_dir: self._test_save_and_load(source_dataset, partial(CvatConverter.convert, allow_undeclared_attrs=True), test_dir, target_dataset=target_dataset)
def categories(self): label_categories = LabelCategories() for i in range(5): label_categories.add('cat' + str(i)) mask_categories = MaskCategories( generate_colormap(len(label_categories.items))) points_categories = PointsCategories() for index, _ in enumerate(label_categories.items): points_categories.add(index, ['cat1', 'cat2'], adjacent=[0, 1]) return { AnnotationType.label: label_categories, AnnotationType.mask: mask_categories, AnnotationType.points: points_categories, }
def _make_voc_categories(): categories = {} label_categories = LabelCategories() for label in chain(VocLabel, VocAction, VocBodyPart): label_categories.add(label.name) categories[AnnotationType.label] = label_categories def label_id(class_index): class_label = VocLabel(class_index).name label_id, _ = label_categories.find(class_label) return label_id colormap = { label_id(idx): tuple(color) \ for idx, color in VocColormap.items() } mask_categories = MaskCategories(colormap) mask_categories.inverse_colormap # init inverse colormap categories[AnnotationType.mask] = mask_categories return categories
def _generate_classification_dataset(self, config, subset=None, empty_scores=False, out_range=False, no_attr=False, no_img=False): probs = self._get_probs(out_range) if subset is None: self.subset = ["train", "val", "test"] else: self.subset = subset iterable = [] label_cat = LabelCategories() idx = 0 for label_id, label in enumerate(config.keys()): num_item = config[label] label_cat.add(label, attributes=None) for _ in range(num_item): scores = probs[idx] idx += 1 if empty_scores: scores = [] attr = {"scores": scores} if no_attr: attr = {} img = Image(path=f"test/dataset/{idx}.jpg", size=(90, 90)) if no_img: img = None iterable.append( DatasetItem( idx, subset=self.subset[idx % len(self.subset)], annotations=[Label( label_id, attributes=attr, )], image=img, )) categories = {AnnotationType.label: label_cat} dataset = Dataset.from_iterable(iterable, categories) return dataset
def __init__(self, extractor, label): super().__init__(extractor) assert isinstance(label, str) self._categories = {} label_cat = self._extractor.categories().get(AnnotationType.label) if not label_cat: label_cat = LabelCategories() self._label = label_cat.add(label) self._categories[AnnotationType.label] = label_cat