Skip to content

Commit b9debee

Browse files
authored
Add h-cls label info normalization (#4173)
* Normalize h_cls label info on export * Add unit test * Upda copyright * Update changelog
1 parent c3f5e02 commit b9debee

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ All notable changes to this project will be documented in this file.
6060
(<https://github.com/openvinotoolkit/training_extensions/pull/4028>)
6161
- Fix SupCon flag
6262
(https://github.com/openvinotoolkit/training_extensions/pull/4076)
63+
- Add h-cls label info normalization
64+
(<https://github.com/openvinotoolkit/training_extensions/pull/4173>)
65+
- Fix arrow support for semantic segmentation task
66+
(<https://github.com/openvinotoolkit/training_extensions/pull/4172>)
6367

6468
## \[2.2.2\]
6569

src/otx/core/types/export.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2024 Intel Corporation
1+
# Copyright (C) 2024-2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33
#
44
"""OTX export-related types definition."""
@@ -123,10 +123,11 @@ def to_metadata(self) -> dict[tuple[str, str], str]:
123123
}
124124

125125
if isinstance(self.label_info, HLabelInfo):
126+
dict_info = self.label_info.as_dict(normalize_label_names=True)
126127
metadata[("model_info", "hierarchical_config")] = json.dumps(
127128
{
128-
"cls_heads_info": self.label_info.as_dict(),
129-
"label_tree_edges": self.label_info.label_tree_edges,
129+
"cls_heads_info": dict_info,
130+
"label_tree_edges": dict_info["label_tree_edges"],
130131
},
131132
)
132133

src/otx/core/types/label.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2023 Intel Corporation
1+
# Copyright (C) 2023-2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33
#
44
"""Dataclasses for label information."""
@@ -119,9 +119,28 @@ def from_dm_label_groups_arrow(cls, dm_label_categories: LabelCategories) -> Lab
119119
label_ids=label_ids,
120120
)
121121

122-
def as_dict(self) -> dict[str, Any]:
122+
def as_dict(self, normalize_label_names: bool = False) -> dict[str, Any]:
123123
"""Return a dictionary including all params."""
124-
return asdict(self)
124+
result = asdict(self)
125+
126+
if normalize_label_names:
127+
128+
def normalize_fn(node: str | list | tuple | dict | int) -> str | list | tuple | dict | int:
129+
"""Normalizes the label names stored in various nested structures."""
130+
if isinstance(node, str):
131+
return node.replace(" ", "_")
132+
if isinstance(node, list):
133+
return [normalize_fn(item) for item in node]
134+
if isinstance(node, tuple):
135+
return tuple(normalize_fn(item) for item in node)
136+
if isinstance(node, dict):
137+
return {normalize_fn(key): normalize_fn(value) for key, value in node.items()}
138+
return node
139+
140+
for k in result:
141+
result[k] = normalize_fn(result[k])
142+
143+
return result
125144

126145
def to_json(self) -> str:
127146
"""Return JSON serialized string."""

tests/unit/core/types/test_label.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2024 Intel Corporation
1+
# Copyright (C) 2024-2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33
from __future__ import annotations
44

@@ -74,6 +74,11 @@ def test_hlabel_info():
7474

7575
hlabel_info = HLabelInfo.from_dm_label_groups(dm_label_categories)
7676

77+
# check if label info can be normalized on export
78+
dict_label_info = hlabel_info.as_dict(normalize_label_names=True)
79+
for lbl in dict_label_info["label_names"]:
80+
assert " " not in lbl
81+
7782
# Check if class_to_group_idx and label_to_idx have the same keys
7883
assert list(hlabel_info.class_to_group_idx.keys()) == list(
7984
hlabel_info.label_to_idx.keys(),

0 commit comments

Comments
 (0)