From 2c9b745d440ef95c028dc9c9353074a08b42b88a Mon Sep 17 00:00:00 2001 From: Songki Choi Date: Fri, 10 Nov 2023 16:28:48 +0900 Subject: [PATCH] Fix h-label bug of missing parent labels in output (#2626) * Fix h-label bug of missing parent labels in output * Fix h-label test data label schema * Update CHANGELOG.md --------- Signed-off-by: Songki Choi --- CHANGELOG.md | 1 + .../adapters/mmcls/datasets/otx_datasets.py | 8 ++++++++ src/otx/algorithms/classification/adapters/mmcls/task.py | 1 + tests/assets/datumaro_h-label/annotations/train.json | 5 +++++ tests/assets/datumaro_h-label/annotations/valid.json | 5 +++++ .../annotations/train.json | 5 +++++ .../annotations/valid.json | 5 +++++ 7 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f009610b0e7..9116e8b6adb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file. - Fix h-label loss normalization issue w/ exclusive label group of singe label () - Fix division by zero in class incremental learning for classification () - Fix saliency maps calculation issue for detection models () +- Fix h-label bug of missing parent labels in output () ## \[v1.4.3\] diff --git a/src/otx/algorithms/classification/adapters/mmcls/datasets/otx_datasets.py b/src/otx/algorithms/classification/adapters/mmcls/datasets/otx_datasets.py index 9d62cb48cee..ed4267d366e 100644 --- a/src/otx/algorithms/classification/adapters/mmcls/datasets/otx_datasets.py +++ b/src/otx/algorithms/classification/adapters/mmcls/datasets/otx_datasets.py @@ -300,6 +300,7 @@ class OTXHierarchicalClsDataset(OTXMultilabelClsDataset): def __init__(self, **kwargs): self.hierarchical_info = kwargs.pop("hierarchical_info", None) + self.label_schema = kwargs.pop("label_schema", None) super().__init__(**kwargs) def load_annotations(self): @@ -308,6 +309,13 @@ def load_annotations(self): for i, _ in enumerate(self.otx_dataset): class_indices = [] item_labels = self.otx_dataset[i].get_roi_labels(self.labels, include_empty=include_empty) + if self.label_schema: + # NOTE: Parent labels might be missing in annotations. + # This code fills the gap just in case. + full_item_labels = set() + for label in item_labels: + full_item_labels.update(self.label_schema.get_ancestors(label)) + item_labels = full_item_labels ignored_labels = self.otx_dataset[i].ignored_labels if item_labels: num_cls_heads = self.hierarchical_info["num_multiclass_heads"] diff --git a/src/otx/algorithms/classification/adapters/mmcls/task.py b/src/otx/algorithms/classification/adapters/mmcls/task.py index 312c968956c..9d29087e039 100644 --- a/src/otx/algorithms/classification/adapters/mmcls/task.py +++ b/src/otx/algorithms/classification/adapters/mmcls/task.py @@ -190,6 +190,7 @@ def configure( elif self._hierarchical: options_for_patch_datasets["type"] = "OTXHierarchicalClsDataset" options_for_patch_datasets["hierarchical_info"] = self._hierarchical_info + options_for_patch_datasets["label_schema"] = self._task_environment.label_schema options_for_patch_evaluation["task"] = "hierarchical" elif self._selfsl: options_for_patch_datasets["type"] = "SelfSLDataset" diff --git a/tests/assets/datumaro_h-label/annotations/train.json b/tests/assets/datumaro_h-label/annotations/train.json index dc7994026dc..f641fbb2352 100644 --- a/tests/assets/datumaro_h-label/annotations/train.json +++ b/tests/assets/datumaro_h-label/annotations/train.json @@ -3,6 +3,11 @@ "categories": { "label": { "label_groups": [ + { + "name": "shape", + "group_type": "exclusive", + "labels": ["blue", "green"] + }, { "name": "blue", "group_type": "exclusive", diff --git a/tests/assets/datumaro_h-label/annotations/valid.json b/tests/assets/datumaro_h-label/annotations/valid.json index dc7994026dc..f641fbb2352 100644 --- a/tests/assets/datumaro_h-label/annotations/valid.json +++ b/tests/assets/datumaro_h-label/annotations/valid.json @@ -3,6 +3,11 @@ "categories": { "label": { "label_groups": [ + { + "name": "shape", + "group_type": "exclusive", + "labels": ["blue", "green"] + }, { "name": "blue", "group_type": "exclusive", diff --git a/tests/assets/datumaro_h-label_class_decremental/annotations/train.json b/tests/assets/datumaro_h-label_class_decremental/annotations/train.json index be96929c774..dbd6dfa0702 100644 --- a/tests/assets/datumaro_h-label_class_decremental/annotations/train.json +++ b/tests/assets/datumaro_h-label_class_decremental/annotations/train.json @@ -3,6 +3,11 @@ "categories": { "label": { "label_groups": [ + { + "name": "shape", + "group_type": "exclusive", + "labels": ["blue", "green"] + }, { "name": "blue", "group_type": "exclusive", diff --git a/tests/assets/datumaro_h-label_class_decremental/annotations/valid.json b/tests/assets/datumaro_h-label_class_decremental/annotations/valid.json index be96929c774..dbd6dfa0702 100644 --- a/tests/assets/datumaro_h-label_class_decremental/annotations/valid.json +++ b/tests/assets/datumaro_h-label_class_decremental/annotations/valid.json @@ -3,6 +3,11 @@ "categories": { "label": { "label_groups": [ + { + "name": "shape", + "group_type": "exclusive", + "labels": ["blue", "green"] + }, { "name": "blue", "group_type": "exclusive",