Skip to content

fix(compact_mask): Match dense nearest-neighbor resizing - #2697

Merged
Borda merged 7 commits into
roboflow:developfrom
aswanth-07:fix/supervision-correctness-20261008
Oct 9, 2026
Merged

Borda merged 7 commits into
roboflow:developfrom
aswanth-07:fix/supervision-correctness-20261008

Conversation

@aswanth-07

Copy link
Copy Markdown
Contributor

Description

I fixed CompactMask.resize losing foreground pixels and disagreeing with dense nearest-neighbor resizing. A singleton in a 10x10 image stays one pixel after resizing to 100x100 instead of becoming a 10x10 block. A full foreground image grows to only 91x91. Downscaling also keeps crops that the output grid never samples.

import numpy as np
import supervision as sv

masks = np.zeros((1, 10, 10), dtype=bool)
masks[0, 2, 3] = True
compact = sv.CompactMask.from_dense(
    masks, sv.mask_to_xyxy(masks), image_shape=(10, 10)
)
print(compact.resize((100, 100)).area[0])  # expected 100, previously 1

Motivation and Context

Scaling inclusive crop endpoints shortens enlarged crops. Resampling each crop independently also uses a different sampling origin and ratio from resizing the full image. I use dense cv2.resize(..., INTER_NEAREST) as the reference.

Changes Made

  • Derive the full-image sampling grid through the active resize backend, then select each crop's output interval and source pixels.
  • Keep sparse run-length processing, dense cropped processing, and threaded batches. Crops with no sampled pixels become all-False.
  • Add 20 exact-parity regression cases and tighten ten existing random cases. Their previous Boolean max(abs(diff)) <= 1 assertion could not detect a mismatch.
  • Update the resize docstring and changelog. Public signatures and encoded mask formats stay unchanged.

Testing

  • Confirmed pristine source: all 30 regression cases fail for pixel mismatches.

  • py -3.13 -m pytest tests/detection/test_compact_mask.py -q: 337 passed.

  • py -3.13 -m pytest -p pytest_cov --cov=supervision -q: 5,110 passed, 6 skipped, 40 warnings; total coverage 93%. Pristine develop has 5,090 passes and the same skips/warnings.

  • Repository pre-commit hooks and whitespace checks pass.

  • Identity, empty, all-False, aligned full-foreground downsampling, repeated calls, and source immutability pass on both base and patch.

  • 900 random masks per backend match dense resizing, using native OpenCV and the NumPy resize implementation at the compatibility facade.

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

Additional Notes

I kept processing cropped masks because decoding the full mask collection would lose its memory advantage. The sampling arrays add memory: for eight dense 128x160 masks resized to 96x120, traced peak allocation grows from about 750 KiB to 919 KiB. This is a correctness fix, with no memory or speedup claim.

Linux/macOS and a fresh environment without OpenCV remain untested locally. The explicit NumPy resize check above does not replace that environment's full suite.

AI assistance disclosure: AI-assisted development tools were used during investigation and implementation. The reported tests were run against the final diff.

@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93%. Comparing base (33e06f8) to head (5bc1a79).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2697   +/-   ##
=======================================
  Coverage       93%     93%           
=======================================
  Files           81      81           
  Lines        12228   12238   +10     
=======================================
+ Hits         11349   11360   +11     
+ Misses         879     878    -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aswanth-07
aswanth-07 marked this pull request as ready for review October 8, 2026 17:33
@aswanth-07
aswanth-07 requested a review from SkalskiP as a code owner October 8, 2026 17:33
@Borda
Borda requested a balanced review from Copilot October 8, 2026 19:50
@Borda Borda added the bug Something isn't working label Oct 8, 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.

🟡 Changes recommended

The native OpenCV backend rejects the new int32 coordinate grids, breaking non-identity resizing.

1 open finding
What changed in this PR

Fixes CompactMask.resize to match full-image nearest-neighbor sampling.

Changes:

  • Uses full-image sampling maps for sparse and dense crops.
  • Adds exact pixel-parity regression tests.
  • Updates documentation and changelog.

Assessment: Code quality 3/5; testing 4/5; documentation 5/5.

File Description
src/​supervision/​detection/​compact_mask.py Implements full-image-grid resizing.
tests/​detection/​test_compact_mask.py Adds exact parity coverage.
docs/​changelog.md Documents the correction.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@aswanth-07

Copy link
Copy Markdown
Contributor Author

The links-check job failed because the existing changelog link to #1502 returned HTTP 500. That link is already present on develop. The runtime test matrix passed, and the PR has 35 successful check contexts.

Could a maintainer rerun the failed link check? GitHub requires repository admin rights for the rerun.

Borda and others added 3 commits October 9, 2026 11:40
Changes:
- Generate missing _rle_resize row and column maps through the active backend.
- Add row and column sampling-boundary regressions and tests preserving a supplied map when the other map is generated.
- Correct sampling docstrings and the exact-parity test description; update the PR roboflow#2697 changelog entry.

Impact:
- Direct RLE resizing matches backend INTER_NEAREST rounding, including the seed-28 case that failed in CI.
- Caller-supplied sampling maps remain intact, and sparse resizing avoids decoding a full crop.

Verification:
- CompactMask unit and integration tests: 356 passed with OpenCV 5.0.0.93 and 356 passed with the NumPy fallback.
- uv run --no-sync pytest -p no:xdist --cov=supervision -q with OpenCV 5.0.0.93: 5262 passed, 1 skipped; 92% coverage.
- uv run --no-sync pre-commit run --all-files: all hooks passed, including ruff and mypy.
- git diff --check: passed.

Residual limits:
- Verification used macOS/Python 3.12.15; the Linux/Python 3.13.16 CI environment has not been rerun.
- The full suite reported 1 skipped test and 39 warnings.

---

Co-authored-by: Codex <[email protected]>
…d-agnostic imports

Updates to use `resize` and `INTER_NEAREST` directly from `supervision._cv2`, improving readability and consistency.
@Borda
Borda merged commit 67c9600 into roboflow:develop Oct 9, 2026
36 checks passed
cfviotti added a commit that referenced this pull request Oct 9, 2026
Brings in #2691 and #2692 (DepthClipRange) as merged, with the
maintainer's edits.

Adaptations:
- changelog: loaders bullet (#2694) sits above the new #2692 and #2697
  bullets.
- visualize_depth.md front matter: description covers loading and
  colouring over images and video.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Borda added a commit to Talhax55z/supervision that referenced this pull request Oct 11, 2026
Changes:
- Integrate target updates for DepthMap loaders and measurement, keypoint mAP, compact-mask resizing, and mAP support with their exports, docs, and tests.
- Resolve the changelog collision by preserving the PR mAR entry and target PR roboflow#2694, roboflow#2693, roboflow#2692, and roboflow#2697 entries; update date_modified.

Impact:
- The PR branch includes the fetched develop changes before report-item work, so follow-up validation runs against integrated code.
- The changelog keeps both sets of release notes without dropping target or PR intent.

Verification:
- git diff --check passed after resolution.
- Changelog assertions confirmed the PR and target entries and no conflict markers.
- git diff --name-only --diff-filter=U returned no paths.

Residual limits:
- The full project gate bundle remains before remediation closure.
- No source-specific regression tests have run after integration.

---

Co-authored-by: Codex <[email protected]>
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