[Bug]: `from_yolo` silently remaps labels when `data.yaml` `names` dict has gaps in its indices
Search before asking
- [x] I have searched the issues and discussions and found no similar bug report.
Bug
When names in data.yaml is a dict whose numeric keys aren't exactly 0..n-1 (e.g. 1-indexed, or a class
was removed), _extract_class_names_from_data sorts the keys and packs the values into a list. The class ids
in the label files still use the original keys, so they now point at a different class. No error or warning.
With names: {1: cat, 2: dog} and a label line 1 ..., the detection comes back as dog.
Expected: either the ids map to the names declared in the YAML, or loading fails with a clear error.
Ultralytics rejects the same YAML in check_class_names (2-class dataset requires class indices 0-1, but you have invalid class indices 1-2 ...), so such a dataset wouldn't train there either.
The {0: background, 2: car, 10: traffic_light} case in test_extract_class_names_sorts_numeric_string_keys
currently locks in this packing (it came from #2296, which was about numeric vs lexicographic ordering).
With that YAML, a label with class 2 loads as traffic_light.
Suggested fix: raise a ValueError in _extract_class_names_from_data when numeric keys aren't 0..n-1,
matching Ultralytics. This covers both the YOLO and YOLO pose loaders. Filling the gaps with placeholder names
would also work if you'd rather keep loading these files. Happy to go either way.
Environment
- supervision
0.32.0.dev0(develop @f33ce15f) - Python 3.12.13, Windows 11
Minimal Reproducible Example
import os
import tempfile
from PIL import Image
import supervision as sv
root = tempfile.mkdtemp()
os.makedirs(f"{root}/images")
os.makedirs(f"{root}/labels")
Image.new("RGB", (100, 100)).save(f"{root}/images/a.jpg")
with open(f"{root}/data.yaml", "w") as f:
f.write("names:\n 1: cat\n 2: dog\n")
with open(f"{root}/labels/a.txt", "w") as f:
f.write("1 0.5 0.5 0.2 0.2\n")
ds = sv.DetectionDataset.from_yolo(
f"{root}/images", f"{root}/labels", f"{root}/data.yaml"
)
_, _, detections = ds[0]
print(ds.classes) # ['cat', 'dog']
print([ds.classes[i] for i in detections.class_id]) # ['dog'], expected 'cat' or an error
Are you willing to submit a PR?
- [x] Yes I'd like to help by submitting a PR!
I reproduced this on current develop: with names: {1: cat, 2: dog}, a label with class ID 1 is loaded as dog.
Are you already working on a fix? If not, I'd be happy to help with the validation and regression tests for the detection and pose loaders.
Thanks for confirming the repro! Yes, I'm working on it: I already have regression tests for the detection loader covering list names, contiguous and 1-indexed dicts, dicts with gaps, and unknown/negative class ids. I'll open a PR shortly. Happy to have your eyes on it for the pose loader once it's up.
Thanks @EnesSoydan! I checked the pose loader in #2985. All 844 dataset tests and 30 additional local checks passed, covering sparse IDs, numeric-string keys, empty labels, and export/reload round trips. Keypoint coordinates, visibility, and box metadata stayed consistent. I didn’t find any issues in these cases.