Repository navigation
fix(dataset): export a box for detections with an empty mask in YOLO and Pascal VOC - #2631
Merged
Borda merged 8 commits intoSep 28, 2026
Merged
Conversation
…and Pascal VOC `as_yolo` and `as_pascal_voc` wrote only the polygons traced from a detection's mask, so a detection whose mask is empty was silently left out of the label file. `from_coco` gives such masks to box-only annotations in an image that also has polygons, and to every annotation with `force_masks=True`. Fall back to the bounding box, as the LabelMe exporter already does.
MohammadHijjawi97
force-pushed
the
fix/export-box-for-empty-mask
branch
from
September 28, 2026 08:50
d2ad712 to
e61a5f9
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2631 +/- ##
=======================================
Coverage 92% 92%
=======================================
Files 78 78
Lines 11695 11701 +6
=======================================
+ Hits 10705 10716 +11
+ Misses 990 985 -5 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused implementation preserves existing polygon filtering behavior and includes adequate regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes silent annotation loss when exporting detections with empty masks.
Changes:
- Falls back to bounding boxes for empty masks in YOLO and Pascal VOC exports.
- Adds regression tests for empty and mixed masks.
- Documents the fix in the changelog.
| File | Description |
|---|---|
src/supervision/dataset/formats/yolo.py |
Adds empty-mask box fallback. |
src/supervision/dataset/formats/pascal_voc.py |
Adds empty-mask box fallback. |
tests/dataset/formats/test_yolo.py |
Tests YOLO fallback behavior. |
tests/dataset/formats/test_pascal_voc.py |
Tests Pascal VOC fallback behavior. |
docs/changelog.md |
Records the bug fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Changes: - YOLO and Pascal VOC exports fall back to the detection box when a nonempty mask yields no valid contour; valid contours filtered by area remain omitted. - COCO import-to-export regression coverage exercises polygon and box-only annotations through both exporters; changelog documents behavior. Impact: - Prevents silent annotation loss for one-pixel/non-contour masks while preserving configured area-filter behavior. - Protects both public export routes with public-path regression coverage. Verification: - Full suite: pytest -q — 4531 passed, 1 skipped. - Focused regressions — 8 passed; target-baseline reproduction — 2 expected failures. - Lint, Ruff format, mypy pre-commit hooks, and staged diff check — passed. Residual limits: - Coverage-enabled invocation collected no local coverage data; online Codecov snapshot predates these edits. - Independent challenger did not reload exported files through importers. --- Co-authored-by: Codex <[email protected]>
Borda
reviewed
Sep 28, 2026
Borda
reviewed
Sep 28, 2026
Co-authored-by: Jirka Borovec <[email protected]>
Changes: - Name actual and expected coordinate lists in the COCO and Pascal VOC export tests before comparing them. Impact: - Makes each expected coordinate list easier to inspect while preserving the tested values. Verification: - Three affected tests passed; Ruff check, Ruff format check, and git diff --check passed. Residual limits: - Full test suite not run. --- Co-authored-by: Codex <[email protected]>
…al VOC export tests
Borda
approved these changes
Sep 28, 2026
Borda
added a commit
that referenced
this pull request
Sep 29, 2026
…and Pascal VOC (#2631) `as_yolo` and `as_pascal_voc` wrote only the polygons traced from a detection's mask, so a detection whose mask is empty was silently left out of the label file. `from_coco` gives such masks to box-only annotations in an image that also has polygons, and to every annotation with `force_masks=True`. Fall back to the bounding box, as the LabelMe exporter already does. --------- Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Codex <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
DetectionDataset.as_yoloandDetectionDataset.as_pascal_vocsilently leave out every detection whose mask is empty. Both exporters write only the polygons traced from a detection's mask, and an all-zero mask traces to no polygon, so the object disappears from the label file without a warning.DetectionDataset.from_cocoproduces such masks in two ordinary cases: a box-only annotation (nosegmentation) in an image where another annotation has a polygon, and every annotation whenforce_masks=True. So exporting a COCO dataset that mixes polygons and boxes loses its box-only objects, and exporting a box-only COCO dataset loaded withforce_masks=Truewrites empty YOLO label files and Pascal VOC files with no<object>.as_labelmealready falls back to the box in this case (detections_to_labelme_shapes: "so no detection is silently dropped"); this PR does the same for YOLO and Pascal VOC.Repro (COCO file with one polygon annotation and one box-only annotation, 100x100 image):
Before (
labels/img.txt, VOC object count):After:
With a box-only COCO file and
force_masks=True,labels/img.txtwas''before and is'0 0.75000 0.75000 0.30000 0.30000\n'after.Motivation and Context
Converting a dataset from COCO to YOLO or Pascal VOC should not lose annotations, and here the loss is silent, so it only shows up later as missing objects in training labels.
Changes Made
detections_to_yolo_annotationsanddetections_to_pascal_voc: a detection whose mask has no foreground pixel is written as its bounding box, matching the LabelMe exporter.min_image_area_percentage/max_image_area_percentagestill drop polygons outside their range; the fallback applies only to empty masks.tests/dataset/formats/test_yolo.pyandtests/dataset/formats/test_pascal_voc.py; changelog entry underUnreleased.Testing
New tests fail on
develop(3 failed) and pass with this change (3 passed):pytest tests/dataset/formats/test_yolo.py tests/dataset/formats/test_pascal_voc.py -k EmptyMaskpytest tests/dataset src/supervision/dataset/formats/yolo.py src/supervision/dataset/formats/pascal_voc.py: 438 passedruff format/ruff checkon the changed files: cleanAdded/updated tests, and the full suite passes locally (added tests; ran the dataset tests above, not the full suite)
Updated docs (docstrings / mkdocs entry) for new or changed public API
Added a changelog entry in
docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
Making
from_cocorasterise the box as a mask for box-only annotations would change what loading returns (andas_cocoround-trips), so the fix is on the export side, where LabelMe already handles it.