Skip to content

fix: populate class_name in DetectionDataset annotations#2156

Merged
Borda merged 3 commits intoroboflow:developfrom
happyhj:fix/dataset-class-name-population
Feb 25, 2026
Merged

fix: populate class_name in DetectionDataset annotations#2156
Borda merged 3 commits intoroboflow:developfrom
happyhj:fix/dataset-class-name-population

Conversation

@happyhj
Copy link
Copy Markdown
Contributor

@happyhj happyhj commented Feb 23, 2026

DetectionDataset.init now maps class_id to class names using CLASS_NAME_DATA_FIELD so that LabelAnnotator displays human-readable labels instead of raw integer IDs.

Closes #1772

Before submitting
  • Self-reviewed the code
  • Updated documentation, follow Google-style
  • Added docs entry for autogeneration (if new functions/classes)
  • Added/updated tests
  • All tests pass locally

Description

DetectionDataset.__init__ now populates CLASS_NAME_DATA_FIELD in each annotation's data dict by mapping class_id to the dataset's classes list. This is done centrally in the constructor so all format loaders (from_yolo, from_coco, from_pascal_voc) benefit without individual changes.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

Motivation and Context

When loading a dataset via DetectionDataset.from_yolo(...) and annotating with LabelAnnotator, labels showed raw integer class IDs (e.g. 0, 1) instead of human-readable class names (e.g. dog, person). This is because the class_name data field was never populated on dataset-loaded Detections, unlike Detections.from_inference() which does set it.

Closes #1772

Changes Made

  • Added CLASS_NAME_DATA_FIELD import to src/supervision/dataset/core.py
  • Added 7 lines in DetectionDataset.__init__ to populate class_name from classes and class_id
  • Added 5 test cases in tests/dataset/test_core.py covering init, empty detections, empty classes, merge, and YOLO integration

Testing

  • I have tested this code locally
  • I have added unit tests that prove my fix is effective or that my feature works
  • All new and existing tests pass

Google Colab

Before fix: https://colab.research.google.com/drive/1ycOTk-zxUnjD_ZHcD8LBDlUVc9jij2Iu?usp=sharing
After fix: https://colab.research.google.com/drive/1kkz0YbwJv7X-9o9AVWu6v601sR4Bc5q9?usp=sharing

Screenshots/Videos

Screenshot 2026-02-23 at 10 11 00 AM

Additional Notes

No changes to individual format loaders or annotator logic were needed.

@happyhj happyhj requested a review from SkalskiP as a code owner February 23, 2026 17:52
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 23, 2026

CLA assistant check
All committers have signed the CLA.

DetectionDataset.__init__ now maps class_id to class names using
CLASS_NAME_DATA_FIELD so that LabelAnnotator displays human-readable
labels instead of raw integer IDs.

Closes roboflow#1772
@happyhj happyhj force-pushed the fix/dataset-class-name-population branch from 0e8ac54 to 9314487 Compare February 23, 2026 18:13
@Borda Borda requested a review from Copilot February 25, 2026 18:14
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74%. Comparing base (d70bc4f) to head (e2b9f34).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2156   +/-   ##
=======================================
  Coverage       74%     74%           
=======================================
  Files           62      62           
  Lines         7486    7492    +6     
=======================================
+ Hits          5547    5554    +7     
+ Misses        1939    1938    -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where DetectionDataset displays raw class IDs (e.g., 0, 1) instead of human-readable class names (e.g., "dog", "cat") when using LabelAnnotator. The fix populates the class_name data field centrally in DetectionDataset.__init__ by mapping class_id values to class names from the dataset's classes list.

Changes:

  • Added logic to populate CLASS_NAME_DATA_FIELD in DetectionDataset.__init__ for all annotations
  • Added comprehensive test coverage including edge cases (empty annotations, merge operations, YOLO integration)

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/supervision/dataset/core.py Added import of CLASS_NAME_DATA_FIELD and logic in __init__ to populate class_name in annotation data by indexing the classes list with class_id values
tests/dataset/test_core.py Added TestClassNamePopulation test class with 5 test cases covering initialization, empty annotations, empty classes, merge operations, and YOLO format integration

Comment thread tests/dataset/test_core.py Outdated
@Borda Borda merged commit 9eba61d into roboflow:develop Feb 25, 2026
24 checks passed
@Borda Borda mentioned this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detections loaded with sv.DetectionDataset.from_yolo annotate class IDs instead of class names with sv.LabelAnnotator

4 participants