Repository navigation
fix(vlm): crop Gemini 2.5/3.5 masks to the image when box_2d leaves 0-1000 - #2793
Merged
Borda merged 3 commits intoOct 23, 2026
Merged
Conversation
…-1000 A `box_2d` coordinate above 1000 or below 0 made the mask placement in `from_google_gemini_2_5` raise a NumPy broadcast `ValueError`, discarding every detection in the response. Place only the part of the mask inside the image, give a box wholly outside the image an all-false mask, and keep `xyxy` unclipped as the box-only and Gemini 3.6 paths do.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The localized fix has focused regression coverage and no identified blocking defects.
1 open issue
What changed in this PR
🟡 Minor Suggestions
This PR fixes Gemini 2.5/3.5 mask parsing so out-of-range boxes do not discard an entire detection response, addressing #2792.
Changes:
- Resamples only the visible mask region, keeping allocations bounded by image size.
- Preserves unclipped boxes and mask alignment, including all-false masks for wholly outside boxes.
- Adds 20 regression cases and documents the behavior.
The supplied changes cover boundary handling, compatibility, and allocation safety. Tests were not rerun during this review.
Quality: Code 5/5—focused implementation; testing 4/5—targeted regression coverage; documentation 4/5—behavior explained. Overall risk: 1/5.
| File | Description |
|---|---|
tests/detection/test_vlm.py |
Tests cropping, outside boxes, alignment, and unchanged box coordinates. |
src/supervision/detection/vlm.py |
Crops mask placement to image bounds and documents the contract. |
docs/changelog.md |
Records the Gemini mask-parsing fix. |
Review effort: Balanced
Changes: - Integrate the fetched develop target, including grayscale annotator support, overflow-safe box midpoints, precise oriented-box alignment, Pascal VOC coordinate and rounding corrections, and their regression coverage. - Resolve the Unreleased changelog conflict while retaining both the PR entry and every target entry. Impact: - The PR branch now includes current target behavior and tests while preserving the Gemini mask change. - The changelog conflict resolution changes no PR review finding; the placeholder correction remains a separate selected remediation item. Verification: - git merge-tree reported one changelog conflict and clean integration for the other target paths. - git diff --cached --check passed; the unmerged path list is empty. - Code tests were not run before this integration commit; the remediation gate remains pending. Residual limits: - DOC-1 and the full remediation gates remain pending. --- Co-authored-by: Codex <[email protected]>
Changes: - Replace both PR_NUMBER placeholders in the Unreleased Gemini mask entry with PR 2793. - Link the visible changelog label and destination to the canonical pull request. Impact: - Readers can navigate from the changelog entry to the correct pull request. Verification: - Full project suite: 5,740 passed, 133 skipped, 37 warnings. - Pinned MkDocs Markdown format hook passed; exact-link acceptance assertion and git diff --check passed. Residual limits: - The local branch was not published; fresh PR metadata reports pre-commit.ci - pr as ERROR, with run details unavailable. --- Co-authored-by: Codex <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2793 +/- ##
=======================================
Coverage 94% 94%
=======================================
Files 83 83
Lines 13695 13702 +7
=======================================
+ Hits 12904 12911 +7
Misses 791 791 🚀 New features to boost your workflow:
|
Borda
approved these changes
Oct 23, 2026
Borda
added a commit
that referenced
this pull request
Oct 24, 2026
…-1000 (#2793) A `box_2d` coordinate above 1000 or below 0 made the mask placement in `from_google_gemini_2_5` raise a NumPy broadcast `ValueError`, discarding every detection in the response. Place only the part of the mask inside the image, give a box wholly outside the image an all-false mask, and keep `xyxy` unclipped as the box-only and Gemini 3.6 paths do. --------- Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Codex <[email protected]>
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
from_google_gemini_2_5(also used byGOOGLE_GEMINI_3_5) placed each mask withnp_mask[y_min:y_max, x_min:x_max] = ...after resizing the PNG to the whole box. Abox_2dcoordinate above 1000 or below 0 made the resized mask larger than the slice, or made a negative slice start wrap to an empty slice, so NumPy raisedValueError: could not broadcast input arrayand the whole response was lost, including every valid item.Minimal repro (full script in the issue), 1920x1080, one valid item plus one item with box
[500, 500, 1001, 1001]:ValueError: could not broadcast input array from shape (541,961) into shape (540,960);[-1, -1, 500, 500]raises... into shape (0,0).xyxyis[192.0, 108.0, 576.0, 324.0]and[960.0, 540.0, 1921.9, 1081.1].Motivation and Context
Models occasionally overshoot the 0-1000 range, and one such item should not discard a whole response. The function's own contract is exactly one mask per kept item, with every other malformed-mask path degrading to an all-false mask; the Gemini 3.6 parser (
polygon_to_mask) and the box-only path already accept the same box.Closes #2792
Changes Made
src/supervision/detection/vlm.py,from_google_gemini_2_5: compute the part of the box inside the image; if none is inside, append an all-false mask (same as the existing empty-box path); otherwise resample straight from the matching part of the PNG (Image.resize(..., box=...)) into that part and assign it. A box fully inside the image uses the full-PNGbox, so its mask is the same as before: checked on 1500 random in-range responses (random PNGs, image sizes and boxes), old and new masks and boxes identical.[0, 0, 10**9, 10**9]) allocates at most the image size.Unreleased.TestGeminiMasksForBoxesOutsideImageintests/detection/test_vlm.py(20 cases): boxes at exactly 1000, overshooting by 1 and by 10, negative, overshooting on both sides, huge (each for Gemini 2.5 and 3.5); boxes wholly outside the image (four positions); the cropped mask is the matching slice of the PNG and not a squashed copy; a response mixing valid, overshooting and wholly-outside items keeps the valid items' masks and boxes;xyxystays equal to the box-only parse.xyxyis not clipped (the box-only and 3.6 paths return it unclipped; see Additional Notes). Other malformedbox_2dvalues (too few elements,NaN/Infinity) fail in other places and are not part of this change.Testing
Ran on macOS, Python 3.12,
uv pip install -eof this branch:New tests on
develop(source change stashed):18 failed, 2 passed(the 2 are the exactly-1000 regression cases); with this change:20 passed.pytest tests/detection/test_vlm.pypluspytest --doctest-modules src/supervision/detection/vlm.py: 203 passed.pytest tests/detection: 2401 passed, 2 skipped.Whole
testsfolder: 5811 passed, 2 skipped, 1 failed:tests/cv2/test_integration.py::test_ordinary_suite_passes_when_cv2_is_blocked, which also fails on unmodifieddevelopin my environment. Three metrics test files could not be collected here (missing optional reference packages) and were left out:test_keypoint_mean_average_precision.py,test_mean_average_precision_limits.py,test_sklearn_parity.py.ruff check,ruff format --check,docformatter --checkandcodespellon the touched files: clean.mypyonvlm.py: one error,Skipping analyzing "deprecate"(missing stubs in my environment), identical on unmodifieddevelop; nothing from the changed lines.Not run: the full
pre-commit run --all-files(mdformat for the changelog, prettier, etc.).Added/updated tests, and the full suite passes locally
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)(First box left unticked: the full suite is not clean in my environment, see Testing.)
Additional Notes
xyxyas returned. The box-only parse and Gemini 3.6 return the same unclippedxyxy, so a response parses to the same boxes with or without masks. Clippingxyxyto the image would be a separate, wider behaviour change; happy to add it here if you prefer.maskstays aligned withxyxy,class_idandconfidence.from_deepseek_vl_2and Gemini 3.6/3.7 usepolygon_to_maskand do not slice-assign, so the same crash does not occur there (checked by running them).