Skip to content

feat(compact_mask): add resize() method and benchmark stage#2227

Draft
Borda wants to merge 4 commits intodevelopfrom
rle/resize
Draft

feat(compact_mask): add resize() method and benchmark stage#2227
Borda wants to merge 4 commits intodevelopfrom
rle/resize

Conversation

@Borda
Copy link
Copy Markdown
Member

@Borda Borda commented Apr 21, 2026

This pull request adds a new resize method to the CompactMask class, enabling efficient resizing of masks to arbitrary resolutions with nearest-neighbor interpolation. The benchmark and summary reporting code is updated to include timing and correctness checks for the new operation, and a comprehensive suite of tests is provided to ensure correctness and robustness of the resize logic.

CompactMask resize implementation and tests:

  • Added a resize method to CompactMask, which rescales masks, offsets, and crop shapes to a new image size using nearest-neighbor interpolation and updates the internal RLE encoding. The method handles identity, empty, and invalid-resize cases efficiently and raises an error for non-positive dimensions.
  • Added a new test class TestCompactMaskResize with extensive parameterized and property-based tests covering proportional scaling, identity, empty masks, invalid shapes, non-square scaling, extreme downscaling, and pixel-level parity with OpenCV's resizing.

Benchmark and reporting updates:

  • Implemented a new stage_resize timing and correctness check in the benchmark, using the new resize method and a dense mask baseline, with 1-pixel tolerance for correctness. Integrated these metrics into scenario results and reporting. [1] [2] [3] [4] [5]
  • Updated the summary table, CSV export, and reporting logic to include the new "resize" operation, its timing, speedup, and correctness status. [1] [2] [3] [4] [5] [6]

Internal utilities:

  • Added _resize_dense_to_shape as a utility for resizing dense masks using numpy fancy-indexing, to serve as a baseline for correctness and timing comparisons.

These changes ensure that CompactMask supports robust and efficient resizing, with full test coverage and integration into benchmarking and reporting workflows.

- Add CompactMask.resize(new_image_shape) — scales RLE crops, offsets, and
  image_shape to an arbitrary target resolution via per-crop decode →
  cv2.INTER_NEAREST → re-encode; identity fast path avoids all decoding
- Add 16 parametrized tests covering identity, halve/double, empty N=0,
  invalid dimensions, non-square scale, zero-extent edge case, dense-parity
  roundtrip (1-px tolerance)
- Add stage_resize to benchmark: times resize-to-half vs numpy linspace
  dense path; adds Resize column to summary table and CSV output

---
Co-authored-by: Claude Code <noreply@anthropic.com>
@Borda Borda added the enhancement New feature or request label Apr 21, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 93.70079% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 78%. Comparing base (87e8153) to head (0fbc48c).

❌ Your project check has failed because the head coverage (78%) is below the target coverage (95%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #2227    +/-   ##
========================================
  Coverage       77%     78%            
========================================
  Files           66      66            
  Lines         8211    8338   +127     
========================================
+ Hits          6350    6469   +119     
- Misses        1861    1869     +8     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Borda and others added 3 commits April 21, 2026 22:38
- Vectorise all coordinate arithmetic (no Python loop over N masks); offsets
  and crop shapes computed with np.clip/round in one shot
- All-False crops skip decode/resize entirely (single False-run output)
- Parallel decode/resize/encode via ThreadPoolExecutor for N ≥ 8
- Add _rle_resize(): direct RLE manipulation without 2D array allocation;
  uses floor(dst*src/dst_size) — bit-exact with cv2.INTER_NEAREST; caches
  per-source-row scaled runs (O(1) for repeated rows in upscale)
- Dispatch in resize(): _rle_resize for sparse masks (run density < 0.25);
  cv2 path retained for dense masks
- 35 new parametrized tests; cv2 parity verified across 20 random seeds

---
Co-authored-by: Claude Code <noreply@anthropic.com>
- Split _rle_resize into three focused helpers: _rle_split_rows (flat RLE
  → per-row run lists), _rle_scale_row (horizontal nearest-neighbour scale
  via precomputed column map), _rle_join_rows (concatenate rows, merging
  same-parity runs at junctions)
- Extract _resize_one closure from resize() into module-level _resize_crop
  (all-False → L3 → cv2 dispatch); remove local cv2 import from resize()
- Move _L3_DENSITY_THRESHOLD to module level with explanatory comment

---
Co-authored-by: Claude Code <noreply@anthropic.com>
- Add tests for independent axis scaling, extreme ratios, and large crops.
- Parameterize for prime-sized, asymmetric, and single-axis crops.
- Increase random seeds in dense parity and cv2 rounding tolerance checks.
- Adjust `_rle_resize` parity checks to allow 1-pixel differences.
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.

1 participant