Skip to content

Commit ad000b4

Browse files
authored
fix: abstract away category indexing differences (#254)
* fix: abstract away category indexing differences Semseg and non-semseg datasets behave differently (position-based vs. explicit indexing). Now computing index field in case position-based indexing is used. Signed-off-by: Max SCHMELLER <max.schmeller@tier4.jp> * Cherry-pick of b91ece3 "refactor(compatibility): move to schema package " Signed-off-by: Max SCHMELLER <max.schmeller@tier4.jp> --------- Signed-off-by: Max SCHMELLER <max.schmeller@tier4.jp>
1 parent 0782473 commit ad000b4

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

t4_devkit/helper/rendering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, t4: Tier4) -> None:
5454
"""
5555
self._t4 = t4
5656
self._label2id: dict[str, int] = {
57-
category.name: idx for idx, category in enumerate(self._t4.category)
57+
category.name: category.index for category in self._t4.category
5858
}
5959

6060
self._executor = concurrent.futures.ThreadPoolExecutor()

t4_devkit/schema/compatibility.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Compatibility helpers that abstract away differences between T4Dataset revisions."""
2+
3+
from __future__ import annotations
4+
5+
from t4_devkit.schema import Category
6+
7+
8+
def fix_category_table(categories: list[Category]) -> list[Category]:
9+
"""Fix unpopulated index fields in some T4Dataset revisions.
10+
11+
This function behaves differently in the below three cases:
12+
13+
- All `index` fields are set: the list is returned unmodified.
14+
- All `index` fields are `None`: the position of each category in the list is used to compute
15+
`index`. The resulting indices start at `0`.
16+
- Some `index` fields are set and some are `None`: raise a `ValueError`.
17+
18+
Args:
19+
categories (list[Category]): List of categories to fix.
20+
21+
Returns:
22+
list[Category]: Fixed list of categories.
23+
24+
Raises:
25+
ValueError: If the `index` field is set for some categories and `None` for others.
26+
"""
27+
if any(category.index is None for category in categories):
28+
if not all(category.index is None for category in categories):
29+
raise ValueError("Category index is not set for some categories.")
30+
31+
for idx, category in enumerate(categories):
32+
category.index = idx
33+
return categories

t4_devkit/tier4.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from t4_devkit.dataclass import Box2D, Box3D, SemanticLabel, Shape, ShapeType
1616
from t4_devkit.helper import RenderingHelper, TimeseriesHelper
1717
from t4_devkit.schema import SchemaName, SensorModality, VisibilityLevel, build_schema
18+
from t4_devkit.schema.compatibility import fix_category_table
1819

1920
if TYPE_CHECKING:
2021
from t4_devkit.typing import CameraIntrinsicLike, Vector3
@@ -181,6 +182,8 @@ def __init__(
181182
self.annotation_dir, SchemaName.CALIBRATED_SENSOR
182183
)
183184
self.category: list[Category] = load_table(self.annotation_dir, SchemaName.CATEGORY)
185+
self.category = fix_category_table(self.category)
186+
184187
self.ego_pose: list[EgoPose] = load_table(self.annotation_dir, SchemaName.EGO_POSE)
185188
self.instance: list[Instance] = load_table(self.annotation_dir, SchemaName.INSTANCE)
186189
self.keypoint: list[Keypoint] = load_table(self.annotation_dir, SchemaName.KEYPOINT)
@@ -265,7 +268,7 @@ def __make_reverse_index__(self, verbose: bool) -> None:
265268
}
266269

267270
self._label2id: dict[str, int] = {
268-
category.name: idx for idx, category in enumerate(self.category)
271+
category.name: category.index for category in self.category
269272
}
270273

271274
# add shortcuts

0 commit comments

Comments
 (0)