Repository navigation
fix(dataset): keep YOLO box extents non-negative on both ends - #2663
Conversation
_parse_box built xyxy as x_center +/- width/2 without checking the sign, so a label such as '0 0.5 0.5 -0.4 0.4' produced [70, 30, 30, 70]: x_min past x_max. Nothing downstream expects that ordering and nothing reported it. Detections.area came back -1600, box_iou_batch scored two boxes covering the identical region at 0.0 instead of 1.0, and with_nms kept both of a pair it should have suppressed, so a corrupt label silently skewed evaluation. Reject a negative extent on load, matching how this parser already rejects a class id that is not a whole number and a non-numeric trailing token. Detections does not enforce x_min <= x_max, so object_to_yolo could write the negative extent the loader now refuses, leaving supervision unable to read a dataset it had just written. Order the corners before measuring the width and height so both ends agree; the exported box describes the same rectangle and normal boxes are byte-identical. A zero extent is degenerate but still ordered, so it keeps loading. The polygon path is unaffected: polygon_to_xyxy takes min and max. NaN and inf extents were already rejected by Detections' finite-value check.
…box-extent # Conflicts: # docs/changelog.md
|
While checking that this change did not need to reach further, I found the same defect in three of the other loaders, and one that already handles it.
>>> from supervision.dataset.formats.coco import coco_annotations_to_detections
>>> ann = [{"id": 1, "image_id": 1, "category_id": 0, "bbox": [50, 50, -40, 40],
... "area": 1600, "iscrowd": 0, "segmentation": []}]
>>> coco_annotations_to_detections(
... image_annotations=ann, resolution_wh=(100, 100), with_masks=False
... ).area
array([-1600.], dtype=float32)This PR only covers Happy to follow either route, whichever you prefer:
Worth deciding which, since leaving three loaders able to produce the box this one now rejects is the inconsistent outcome. |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused fix has regression coverage and no identified blockers; only a minor documentation metadata correction remains.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Fixes #2662 by keeping Supervision’s YOLO import and export behavior consistent for reversed boxes.
Changes:
- Reject negative widths and heights on import.
- Order box corners on export without changing normal boxes.
- Add eight regression cases and a changelog entry.
Scores: Code 5/5 · Testing 5/5 · Documentation 4/5.
| File | Description |
|---|---|
| tests/dataset/formats/test_yolo.py | Covers negative and zero extents, plus reversed-box round trips. |
| src/supervision/dataset/formats/yolo.py | Validates imported extents and orders exported corners. |
| docs/changelog.md | Documents the import and export fixes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
docs/theme/main.html publishes this value as the page's JSON-LD dateModified, so an entry added without bumping it leaves the metadata pointing at the previous edit.
…box-extent # Conflicts: # docs/changelog.md
…box-extent # Conflicts: # docs/changelog.md
…box-extent # Conflicts: # docs/changelog.md
|
Addressed the Copilot note: bumped Also merged |
…box-extent # Conflicts: # docs/changelog.md
…box-extent --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- object_to_yolo orders corners through _sort_box_corners instead of builtin min/max, so a NaN corner stays visible as nan - load_yolo_annotations re-raises ValueError naming the annotation file - box extent error quotes values with repr - Raises sections added to yolo_annotations_to_detections and DetectionDataset.from_yolo - object_to_yolo docstring and doctest cover a reversed box [resolve group] PR roboflow#2663 — items 3 4 12 14 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- negative extent cases for -inf and -1e-9, with the error match tightened - negative extent on confidence, masked and OBB-mode box lines - zero extent including -0.0 asserts exact xyxy - reversed box export asserts the written line directly with atol - NaN corner test and as_yolo to from_yolo round trip - _EMPTY_MASK moved next to TestDetectionsToYoloAnnotationsEmptyMask [resolve group] PR roboflow#2663 — items 7 8 9 10 11 15 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- area is negative only when one extent is negative - export half names DetectionDataset.as_yolo instead of object_to_yolo - PR link wrapped in parentheses and error file naming noted [resolve group] PR roboflow#2663 — items 13 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…nmiral/supervision into fix/yolo-negative-box-extent --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- removed the older copy of the PR entry that the fork merge re-added --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2663 +/- ##
=======================================
Coverage 92% 92%
=======================================
Files 78 78
Lines 11893 11900 +7
=======================================
+ Hits 10997 11004 +7
Misses 896 896 🚀 New features to boost your workflow:
|
- roboflow#4: docs(changelog): drop duplicate roboflow#2663 entry left by the merge - roboflow#7: reject nan and infinite YOLO box values with one finite-value error before the extent check - roboflow#8: make YOLO line dispatch exhaustive so every parsed line appends exactly one box [resolve group] PR roboflow#2665 — items 4 7 8 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Codex <[email protected]>
_parse_box built xyxy as x_center +/- width/2 without checking the sign, so a label such as '0 0.5 0.5 -0.4 0.4' produced [70, 30, 30, 70]: x_min past x_max. Nothing downstream expects that ordering and nothing reported it. Detections.area came back -1600, box_iou_batch scored two boxes covering the identical region at 0.0 instead of 1.0, and with_nms kept both of a pair it should have suppressed, so a corrupt label silently skewed evaluation. Reject a negative extent on load, matching how this parser already rejects a class id that is not a whole number and a non-numeric trailing token. Detections does not enforce x_min <= x_max, so object_to_yolo could write the negative extent the loader now refuses, leaving supervision unable to read a dataset it had just written. Order the corners before measuring the width and height so both ends agree; the exported box describes the same rectangle and normal boxes are byte-identical. A zero extent is degenerate but still ordered, so it keeps loading. The polygon path is unaffected: polygon_to_xyxy takes min and max. NaN and inf extents were already rejected by Detections' finite-value check. --------- Co-authored-by: jirka <[email protected]> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…#2683) #2663 stopped the YOLO loader building a box with x_min past x_max, but coco, createml and pascal_voc still did. Such a box makes Detections.area negative, scores two boxes covering the identical region at 0.0 under box_iou_batch, and stops with_nms suppressing, so a corrupt label skews evaluation silently. The format decides the remedy, following what #2663 established: - coco and createml name an extent, so a negative width or height is not representable and is now rejected, as the YOLO loader does. - pascal_voc names two corners explicitly, so a reversed pair describes the same rectangle and is now ordered, as labelme already does. All three exporters measured the extent from raw corner values, so a reversed Detections exported as a file the loader refuses to read. Each now orders the corners first, and the COCO exporter writes the ordered origin rather than the raw one. The exported box describes the same rectangle and normal boxes are unchanged. --------- Co-authored-by: jirka <[email protected]> Co-authored-by: Codex <[email protected]>
…#2683) #2663 stopped the YOLO loader building a box with x_min past x_max, but coco, createml and pascal_voc still did. Such a box makes Detections.area negative, scores two boxes covering the identical region at 0.0 under box_iou_batch, and stops with_nms suppressing, so a corrupt label skews evaluation silently. The format decides the remedy, following what #2663 established: - coco and createml name an extent, so a negative width or height is not representable and is now rejected, as the YOLO loader does. - pascal_voc names two corners explicitly, so a reversed pair describes the same rectangle and is now ordered, as labelme already does. All three exporters measured the extent from raw corner values, so a reversed Detections exported as a file the loader refuses to read. Each now orders the corners first, and the COCO exporter writes the ordered origin rather than the raw one. The exported box describes the same rectangle and normal boxes are unchanged. --------- Co-authored-by: jirka <[email protected]> Co-authored-by: Codex <[email protected]>
…#2683) #2663 stopped the YOLO loader building a box with x_min past x_max, but coco, createml and pascal_voc still did. Such a box makes Detections.area negative, scores two boxes covering the identical region at 0.0 under box_iou_batch, and stops with_nms suppressing, so a corrupt label skews evaluation silently. The format decides the remedy, following what #2663 established: - coco and createml name an extent, so a negative width or height is not representable and is now rejected, as the YOLO loader does. - pascal_voc names two corners explicitly, so a reversed pair describes the same rectangle and is now ordered, as labelme already does. All three exporters measured the extent from raw corner values, so a reversed Detections exported as a file the loader refuses to read. Each now orders the corners first, and the COCO exporter writes the ordered origin rather than the raw one. The exported box describes the same rectangle and normal boxes are unchanged. --------- Co-authored-by: jirka <[email protected]> Co-authored-by: Codex <[email protected]>

Description
Fixes #2662.
_parse_boxbuiltxyxyasx_center ± width / 2without checking the sign, so a label such as0 0.5 0.5 -0.4 0.4produced[70, 30, 30, 70]—x_minpastx_max. Nothing downstream expects that ordering and nothing reported it:Detections.area-1600.01600.0box_iou_batch, two boxes over the identical region, one reversed0.01.0with_nms(threshold=0.5), two heavily overlapping boxes, one reversed21A corrupt label therefore skewed evaluation silently rather than failing.
On load, a negative extent now raises
ValueError, matching how this parser already rejects a class id that is not a whole number (test_rejects_class_id_that_is_not_a_whole_number) and a non-numeric trailing token (test_rejects_a_nonnumeric_trailing_token).On export,
object_to_yolonow orders the corners before measuring.Detectionsdoes not enforcex_min <= x_max, so without this it would write the very extent the loader rejects, leaving supervision unable to read back a dataset it had just written:Normal boxes are byte-identical; the
object_to_yolodocstring example still produces'0 0.37500 0.37500 0.50000 0.50000'.Scope
polygon_to_xyxytakes min and max.NaNandinfextents were already rejected byDetections' finite-value check;-infis caught by the new guard.Type of change
Strictly, a label file with a negative extent that previously loaded will now raise. That is the intent — it was loading as a box no consumer could interpret.
How has this change been tested?
Eight new tests in
tests/dataset/formats/test_yolo.py, covering both directions: negative width, height and both on load; zero width and height still loading; and reversed x, y and both surviving an export/import round trip.Following AGENTS.md §8, with a baseline captured before the change:
The failing set is identical in both runs — all eight are pre-existing
ImportError: metrics extra is requiredin this environment.uv run pre-commit runpasses on both changed files, mypy included.Any specific deployment considerations?
One thing I left out deliberately: the error names the offending values but not the annotation file, so a single bad row in a large dataset aborts the load without saying which file.
_parse_class_idhas the same gap, so threading the path through looks like a separate change across all of this parser's errors rather than something to bolt onto this one. Happy to do it in a follow-up if you would like it.Docs
docs/changelog.mdentry added, linking this PR.