perf: avoid compact mask materialization in polygon annotator - #2369
Merged
Conversation
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 Report❌ Patch coverage is 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:
|
Contributor
There was a problem hiding this comment.
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 useCompactMask.crop()and addCompactMask.offsets[detection_idx]to polygon coordinates before drawing. - Added a regression test ensuring compact-mask annotation avoids integer indexing into
CompactMaskand 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. |
…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>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
This pull request enhances the handling and testing of compact mask annotations in the
PolygonAnnotator. The main change is ensuring that when aCompactMaskis 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:
annotateincore.pyto detect when aCompactMaskis 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:
test_compact_mask_uses_crops_and_matches_dense_maskto verify that the annotator usesCompactMask.crop(not dense mask indexing) and that the output matches the dense mask annotation. The test also ensures that using integer indexing onCompactMaskraises an error, enforcing the correct usage pattern.