Skip to content

Commit

Permalink
hierarchical labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
bonhunko committed Oct 19, 2022
1 parent 5ff089d commit 516e0c7
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Add jupyter sample introducing how to merge datasets
(<https://github.com/openvinotoolkit/datumaro/pull/738>)
- Support for exclusive of labels with LabelGroup
(<https://github.com/openvinotoolkit/datumaro/pull/742>)

## 06/09/2022 - Release v0.3.1
### Added
Expand Down
24 changes: 23 additions & 1 deletion datumaro/components/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ class Category:
parent: str = field(default="", validator=default_if_none(str))
attributes: Set[str] = field(factory=set, validator=default_if_none(set))

@attrs(slots=True, order=False)
class LabelGroup:
name: str = field(converter=str, validator=not_empty)
labels: List[str] = field(default=[], validator=default_if_none(list))
group_type: str = field(default="exclusive", validator=default_if_none(str))

items: List[str] = field(factory=list, validator=default_if_none(list))
label_groups: List[str] = field(factory=list, validator=default_if_none(list))
_indices: Dict[str, int] = field(factory=dict, init=False, eq=False)

@classmethod
Expand Down Expand Up @@ -146,7 +153,10 @@ def _reindex(self):
self._indices = indices

def add(
self, name: str, parent: Optional[str] = None, attributes: Optional[Set[str]] = None
self,
name: str,
parent: Optional[str] = None,
attributes: Optional[Set[str]] = None,
) -> int:
assert name
assert name not in self._indices, name
Expand All @@ -156,6 +166,18 @@ def add(
self._indices[name] = index
return index

def add_label_group(
self,
name: str,
labels: List[str],
group_type: str,
) -> int:
assert name

index = len(self.label_groups)
self.label_groups.append(self.LabelGroup(name, labels, group_type))
return index

def find(self, name: str) -> Tuple[Optional[int], Optional[Category]]:
index = self._indices.get(name)
if index is not None:
Expand Down
14 changes: 14 additions & 0 deletions datumaro/plugins/datumaro_format/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import os
import os.path as osp
import shutil
from collections import defaultdict

import numpy as np
import pycocotools.mask as mask_utils

from datumaro.components.annotation import (
Annotation,
AnnotationType,
Bbox,
Caption,
Cuboid3d,
Expand Down Expand Up @@ -268,9 +270,13 @@ def _convert_cuboid_3d_object(self, obj):
def _convert_attribute_categories(self, attributes):
return sorted(attributes)

def _convert_labels_label_groups(self, labels):
return sorted(labels)

def _convert_label_categories(self, obj):
converted = {
"labels": [],
"label_groups": [],
"attributes": self._convert_attribute_categories(obj.attributes),
}
for label in obj.items:
Expand All @@ -281,6 +287,14 @@ def _convert_label_categories(self, obj):
"attributes": self._convert_attribute_categories(label.attributes),
}
)
for label_group in obj.label_groups:
converted["label_groups"].append(
{
"name": cast(label_group.name, str),
"group_type": cast(label_group.group_type, str),
"labels": self._convert_labels_label_groups(label_group.labels),
}
)
return converted

def _convert_mask_categories(self, obj):
Expand Down
9 changes: 8 additions & 1 deletion datumaro/plugins/datumaro_format/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ def _load_categories(parsed):
label_categories = LabelCategories(attributes=parsed_label_cat.get("attributes", []))
for item in parsed_label_cat["labels"]:
label_categories.add(
item["name"], parent=item["parent"], attributes=item.get("attributes", [])
item["name"],
parent=item["parent"],
attributes=item.get("attributes", []),
)

for item in parsed_label_cat["label_groups"]:
label_categories.add_label_group(
item["name"], labels=item["labels"], group_type=item["group_type"]
)

categories[AnnotationType.label] = label_categories
Expand Down
77 changes: 77 additions & 0 deletions tests/assets/datumaro_dataset/annotations/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"info": {},
"categories": {
"label": {
"label_groups": [
{
"name": "manmade",
"group_type": "exclusive",
"labels": ["car", "bicycle"]
},
{
"name": "empty_group",
"group_type": "empty",
"labels": ["tom", "mary"]
}
],
"labels": [
{
"name": "car",
"parent": "",
"attributes": []
},
{
"name": "bicycle",
"parent": "",
"attributes": []
},
{
"name": "tom",
"parent": "",
"attributes": []
},
{
"name": "mary",
"parent": "",
"attributes": []
}
],
"attributes": []
}
},
"items": [
{
"id": "c",
"annotations": [
{
"id": 0,
"type": "label",
"attributes": {
"score": 1.0
},
"group": 0,
"label_id": 1
},
{
"id": 0,
"type": "label",
"attributes": {
"score": 1.0
},
"group": 0,
"label_id": 3
}
],
"image": {
"path": "../tests/assets/datumaro_dataset/images/test/c.jpg",
"size": [
10,
5
]
},
"media": {
"path": "../tests/assets/datumaro_dataset/images/test/c.jpg"
}
}
]
}
103 changes: 103 additions & 0 deletions tests/assets/datumaro_dataset/annotations/train.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"info": {},
"categories": {
"label": {
"label_groups": [
{
"name": "manmade",
"group_type": "exclusive",
"labels": ["car", "bicycle"]
},
{
"name": "empty_group",
"group_type": "empty",
"labels": ["tom", "mary"]
}
],
"labels": [
{
"name": "car",
"parent": "",
"attributes": []
},
{
"name": "bicycle",
"parent": "",
"attributes": []
},
{
"name": "tom",
"parent": "",
"attributes": []
},
{
"name": "mary",
"parent": "",
"attributes": []
}
],
"attributes": []
}
},
"items": [
{
"id": "a",
"annotations": [
{
"id": 0,
"type": "label",
"attributes": {
"score": 1.0
},
"group": 0,
"label_id": 0
}
],
"image": {
"path": "../tests/assets/datumaro_dataset/images/train/a.jpg"
},
"media": {
"path": "../tests/assets/datumaro_dataset/images/train/a.jpg"
}
},
{
"id": "b",
"annotations": [
{
"id": 0,
"type": "label",
"group": 0,
"label_id": 0
},
{
"id": 1,
"type": "label",
"group": 0,
"label_id": 1
},
{
"id": 2,
"type": "label",
"group": 0,
"label_id": 2
},
{
"id": 3,
"type": "label",
"group": 0,
"label_id": 5
}
],
"image": {
"path": "../tests/assets/datumaro_dataset/images/train/b.jpg",
"size": [
2,
8
]
},
"media": {
"path": "../tests/assets/datumaro_dataset/images/train/b.jpg"
}
}
]
}
54 changes: 54 additions & 0 deletions tests/assets/datumaro_dataset/annotations/validation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"info": {},
"categories": {
"label": {
"label_groups": [
{
"name": "manmade",
"group_type": "exclusive",
"labels": ["car", "bicycle"]
},
{
"name": "empty_group",
"group_type": "empty",
"labels": ["tom", "mary"]
}
],
"labels": [
{
"name": "car",
"parent": "",
"attributes": []
},
{
"name": "bicycle",
"parent": "",
"attributes": []
},
{
"name": "tom",
"parent": "",
"attributes": []
},
{
"name": "mary",
"parent": "",
"attributes": []
}
],
"attributes": []
}
},
"items": [
{
"id": "d",
"annotations": [],
"image": {
"path": "../tests/assets/datumaro_dataset/images/validation/d.png"
},
"media": {
"path": "../tests/assets/datumaro_dataset/images/validation/d.png"
}
}
]
}
Binary file added tests/assets/datumaro_dataset/images/test/c.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/datumaro_dataset/images/train/a.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/datumaro_dataset/images/train/b.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 516e0c7

Please sign in to comment.