Skip to content

fix(dataset): export a box for detections with an empty mask in YOLO and Pascal VOC - #2631

Merged
Borda merged 8 commits into
roboflow:developfrom
MohammadHijjawi97:fix/export-box-for-empty-mask
Sep 28, 2026
Merged

Borda merged 8 commits into
roboflow:developfrom
MohammadHijjawi97:fix/export-box-for-empty-mask

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

Description

DetectionDataset.as_yolo and DetectionDataset.as_pascal_voc silently 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_coco produces such masks in two ordinary cases: a box-only annotation (no segmentation) in an image where another annotation has a polygon, and every annotation when force_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 with force_masks=True writes empty YOLO label files and Pascal VOC files with no <object>. as_labelme already 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):

ds = sv.DetectionDataset.from_coco(root, "ann.json")   # 2 detections
ds.as_yolo(annotations_directory_path="labels")
ds.as_pascal_voc(annotations_directory_path="voc")

Before (labels/img.txt, VOC object count):

0 0.10000 0.10000 0.10000 0.50000 0.50000 0.50000 0.50000 0.10000
voc objects: 1

After:

0 0.10000 0.10000 0.10000 0.50000 0.50000 0.50000 0.50000 0.10000
1 0.75000 0.75000 0.30000 0.30000
voc objects: 2

With a box-only COCO file and force_masks=True, labels/img.txt was '' 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_annotations and detections_to_pascal_voc: a detection whose mask has no foreground pixel is written as its bounding box, matching the LabelMe exporter.
  • Masks with any foreground pixel are exported as polygons exactly as before. min_image_area_percentage / max_image_area_percentage still drop polygons outside their range; the fallback applies only to empty masks.
  • Regression tests in tests/dataset/formats/test_yolo.py and tests/dataset/formats/test_pascal_voc.py; changelog entry under Unreleased.

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 EmptyMask

  • pytest tests/dataset src/supervision/dataset/formats/yolo.py src/supervision/dataset/formats/pascal_voc.py: 438 passed

  • ruff format / ruff check on the changed files: clean

  • Added/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.md under Unreleased (skip for lint/type/format-only or pure doc changes)

Additional Notes

Making from_coco rasterise the box as a mask for box-only annotations would change what loading returns (and as_coco round-trips), so the fix is on the export side, where LabelMe already handles it.

…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
MohammadHijjawi97 force-pushed the fix/export-box-for-empty-mask branch from d2ad712 to e61a5f9 Compare September 28, 2026 08:50
@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92%. Comparing base (0159909) to head (5f099b7).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a balanced review from Copilot September 28, 2026 18:34
@Borda Borda added the bug Something isn't working label Sep 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 and others added 4 commits September 29, 2026 00:26
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]>
@Borda
Borda merged commit 8c7df23 into roboflow:develop Sep 28, 2026
36 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants