Skip to content

perf: avoid compact mask materialization in polygon annotator - #2369

Merged
Borda merged 7 commits into
developfrom
perf-CMask/3
Jul 1, 2026
Merged

perf: avoid compact mask materialization in polygon annotator#2369
Borda merged 7 commits into
developfrom
perf-CMask/3

Conversation

@Borda

@Borda Borda commented Jun 29, 2026

Copy link
Copy Markdown
Member

This pull request enhances the handling and testing of compact mask annotations in the PolygonAnnotator. The main change is ensuring that when a CompactMask is used, the annotation logic utilizes cropped masks and applies the correct offset, instead of relying on dense mask indexing. Additionally, a new test verifies this behavior and ensures compatibility between dense and compact masks.

Improvements to mask annotation logic:

  • Updated annotate in core.py to detect when a CompactMask is used, apply the appropriate crop for each detection, and add the offset to polygons before drawing. This ensures annotations are correctly positioned for compact masks. [1] [2]

Testing and validation:

  • Added a test test_compact_mask_uses_crops_and_matches_dense_mask to verify that the annotator uses CompactMask.crop (not dense mask indexing) and that the output matches the dense mask annotation. The test also ensures that using integer indexing on CompactMask raises an error, enforcing the correct usage pattern.

Use CompactMask crops for polygon extraction and offset crop-local contours back into image coordinates.

Add a regression test covering tight crops, disconnected contours, empty masks, dense parity, and no integer CompactMask indexing.

Co-authored-by: Codex <codex@openai.com>
@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84%. Comparing base (3ecd5d0) to head (12601cf).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2369   +/-   ##
=======================================
  Coverage       84%     84%           
=======================================
  Files           70      70           
  Lines         9887    9898   +11     
=======================================
+ Hits          8270    8280   +10     
- Misses        1617    1618    +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Pull request overview

This PR improves PolygonAnnotator performance and correctness for CompactMask by avoiding full-frame mask materialization: it now decodes per-detection crops and applies the stored crop offset before drawing polygons, keeping output consistent with dense-mask behavior.

Changes:

  • Updated PolygonAnnotator.annotate() to use CompactMask.crop() and add CompactMask.offsets[detection_idx] to polygon coordinates before drawing.
  • Added a regression test ensuring compact-mask annotation avoids integer indexing into CompactMask and matches dense-mask output exactly.

Assessment (n/5):

  • Code quality: 5/5
  • Testing: 5/5
  • Documentation: 5/5

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/supervision/annotators/core.py Uses CompactMask.crop() + offsetting to draw polygons without dense mask indexing/materialization.
tests/annotators/test_core.py Adds coverage asserting compact-mask path uses crops (not __getitem__ int indexing) and matches dense output.

@Borda Borda added the enhancement New feature or request label Jun 29, 2026
@Borda Borda self-assigned this Jul 1, 2026
Borda and others added 3 commits July 1, 2026 15:55
…spatch

- Add `_iter_mask_crops()` helper under shared-utilities seam: yields
  (detection_idx, mask_or_crop, offset_or_None) encapsulating the
  CompactMask vs dense isinstance dispatch in one place (eliminates 4th
  inline copy of the same pattern; see _paint_masks_by_area)
- Refactor PolygonAnnotator.annotate() to consume _iter_mask_crops;
  removes the 9-line inline dispatch block
- Add TODO comment at isinstance site flagging MaskLike Protocol as
  follow-up (separate PR; review item #3 self-resolved)
- Extend PolygonAnnotator.annotate() docstring with Note section
  covering CompactMask fast path and offset semantics

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- Add N=0 empty CompactMask test (scene unchanged, no error)
- Add all-False mask test (no polygons drawn, documents boundary behavior)
- Add N=1 single-detection parity test (CompactMask == dense)
- Add float xyxy truncation test (sub-pixel xyxy → same output as int xyxy)
- Add disjoint-contour coordinate assertion: both blobs painted at correct
  image-space coords after crop→image offset translation
- Add PolygonAnnotator to TestCompactMaskParity.test_annotator_compact_mask_matches_dense_mask parametrize

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread tests/annotators/test_core.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Borda
Borda merged commit 3f816c0 into develop Jul 1, 2026
26 checks passed
@Borda
Borda deleted the perf-CMask/3 branch July 1, 2026 16:05
@Borda Borda mentioned this pull request Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants