Skip to content

perf: avoid compact mask materialization in polygon annotator#2369

Open
Borda wants to merge 3 commits into
developfrom
perf-CMask/3
Open

perf: avoid compact mask materialization in polygon annotator#2369
Borda wants to merge 3 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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83%. Comparing base (934da12) to head (549730d).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2369   +/-   ##
=======================================
  Coverage       83%     83%           
=======================================
  Files           69      69           
  Lines         9616    9624    +8     
=======================================
+ Hits          7941    7949    +8     
  Misses        1675    1675           
🚀 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
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