feat: add compact RLE mask ingestion#2367
Open
Borda wants to merge 18 commits into
Open
Conversation
Co-authored-by: Codex <codex@openai.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2367 +/- ##
========================================
Coverage 83% 83%
========================================
Files 69 69
Lines 9616 9771 +155
========================================
+ Hits 7941 8095 +154
- Misses 1675 1676 +1 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an opt-in pathway to keep segmentation masks in a compact RLE-backed representation (CompactMask) throughout Roboflow/Inference ingestion, reducing the need to materialize dense (N, H, W) boolean mask stacks.
Changes:
- Added
compact_maskskeyword toDetections.from_inference(...)and plumbed it into Roboflow result parsing. - Extended
process_roboflow_result(...)to optionally returnCompactMaskinstead of dense masks. - Implemented
CompactMask.from_coco_rle(...)plus supporting RLE utilities, and added tests covering the new behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/supervision/detection/core.py |
Adds compact_masks option to Detections.from_inference and forwards it into Roboflow result parsing. |
src/supervision/detection/utils/internal.py |
Adds compact_masks option to process_roboflow_result, returning masks as CompactMask when enabled. |
src/supervision/detection/compact_mask.py |
Adds COCO RLE transcoding utilities and CompactMask.from_coco_rle(...) to avoid dense mask materialization. |
tests/detection/utils/test_internal.py |
Adds tests verifying process_roboflow_result(..., compact_masks=True) returns CompactMask and preserves behavior. |
tests/detection/test_core.py |
Adds tests for Detections.from_inference(..., compact_masks=True) representation change without pixel changes. |
tests/detection/test_compact_mask.py |
Adds unit tests validating CompactMask.from_coco_rle(...) correctness and behavior. |
…oflow_result - Batch COCO-RLE decode: defer per-prediction from_coco_rle calls to single post-loop call, eliminating O(N×W) per-prediction column-split overhead - Mixed-batch alignment: drop all masks when only subset of predictions carry masks, matching tracker_id handling and preventing xyxy misalignment - DoS guard: add _MAX_IMAGE_DIMENSION=32768 constant; raise ValueError when either image dimension exceeds limit in from_coco_rle - Size-mismatch warning: emit logger.debug when compact_masks=True RLE size does not match image size, then fall back to dense decode - int64 dtype fix: np.sum(counts, dtype=np.int64) prevents overflow on large masks - Redundant outer int() removed from bbox clamp in from_coco_rle - Sum invariant comment added to _rle_trim_col_runs - Document that compact path drops True pixels outside the detection bbox; fix misleading pixel-invariance test docstring; add test asserting drop behavior --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…oflow_result - 7 parametrized ValueError cases for from_coco_rle (zero-height, zero-width, xyxy-shape-mismatch, non-mapping-rle-item, missing-counts-key, counts-sum-mismatch, max-image-dimension-exceeded) - bytes input path test for _coco_rle_counts_to_array via TestCcoRleCountsToArray - polygon + compact_masks=True path asserts CompactMask returned - box-only + compact_masks=True asserts None mask - RLE size mismatch resize-fallback branch in dense path (rle_mask key) --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- _rle_split_cols: accept x_start/x_stop to skip columns outside crop (O(W)→O(crop_w)) - _rle_split_cols: .tolist() before loop to avoid numpy-scalar boxing overhead - Detections.to_compact_masks(): explicit opt-in post-conversion method - Extract _resolve_rle_mask helper; reduce process_roboflow_result branch count - metrics: convert CompactMask to dense before cast in _detections_content - Document _base48/_delta private symbol coupling in compact_mask.py imports - Restrict CompactMask "more efficient" claim to memory footprint in module docstring - Add pixel-layout annotation to from_coco_rle doctest example --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
The __class__ constructor call in to_compact_masks() was missing metadata=self.metadata; collection-level metadata would be silently dropped on every conversion. --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Preserve CompactMask instances through mask metrics so mask_iou_batch can use the compact fast path. Fix batched COCO RLE CompactMask slicing and add direct regression coverage for to_compact_masks and metric mask content. Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
…tches in docstring
…into perf-CMask/1
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.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 introduces support for returning segmentation masks as
CompactMaskobjects in the detection pipeline, providing a more efficient and flexible mask representation. The changes allow users to opt-in to this new format via thecompact_masksargument, affecting both mask decoding and theDetections.from_inferenceAPI. Several internal utilities and the main mask handling logic have been updated to support this feature.New CompactMask support for segmentation masks:
src/supervision/detection/core.py: Added acompact_maskskeyword argument toDetections.from_inference, allowing users to request segmentation masks asCompactMaskobjects instead of dense NumPy arrays. The argument is documented, and the call toprocess_roboflow_resultis updated accordingly. [1] [2] [3]src/supervision/detection/utils/internal.py: Updatedprocess_roboflow_resultto support thecompact_masksargument. When enabled, segmentation masks are decoded and returned asCompactMaskinstances, either directly from COCO RLE or by converting dense masks. The function's return type and documentation are updated, and the mask construction logic is refactored to handle both dense and compact formats. [1] [2] [3] [4] [5] [6] [7]Core CompactMask functionality and utilities:
src/supervision/detection/compact_mask.py:_coco_rle_counts_to_array) and trimming RLE columns (_rle_trim_col_runs).CompactMask.from_coco_rle, enabling efficient construction of compact masks from COCO RLE payloads and bounding boxes without materializing dense arrays.castfor type safety in iteration and summation. [1] [2] [3] [4]Type and import updates:
CompactMasktype and ensure type correctness.These changes improve mask handling performance and flexibility, especially when working with large images or batches, while maintaining backward compatibility with existing dense mask workflows.