feat: add CompactMask for memory-efficient crop-RLE mask storage (#2159)

Dense (N, H, W) bool masks cause OOM for aerial imagery (1000 objects x
4K image ~ 8.3 GB). CompactMask encodes each mask as a run-length
sequence of its bounding-box crop, reducing typical usage to ~2 MB.

* fix: correct bounding box coordinates in CompactMask doctests
* feat: implement memory-efficient IoU and NMS with CompactMask integration
* test: add extensive tests for CompactMask IoU, NMS, and InferenceSlicer integration
* feat(examples): add CompactMask demo and benchmark
* feat(examples): expand CompactMask benchmark with new stages and metrics
* feat(tests): add detailed CompactMask tests for NMM, centroids, holes, and segments
* feat(compact_mask): add repack(), fix merge perf, and add parity tests
* fix(masks): handle empty crops by defaulting centroid to (0, 0)
* feat(compact_mask): add `bbox_xyxy` property and improve type annotations
* feat(compact_mask): enhance `with_offset` for clipping and add tests
* docs(compact_mask): unwrap prose and add per-operation speedup analysis
* perf(compact_mask): fast path in with_offset avoids decode/re-encode
* fix(benchmark): count NMS mismatches and explain exact-vs-resize difference
* test(compact_mask): add 121 parametrised random-scenario parity tests
* refactor: rename single-char variables to descriptive names
* fix(nms): remove resize-to-640 approximation from mask_non_max_suppression
* docs(compact_mask): update README with fresh benchmark results
* refactor(benchmark): improve summary table logic, add CSV export
* docs(compact_mask): update README with revised benchmark speedups and cleanup

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Jirka Borovec
2026-04-13 11:10:12 +02:00
committed by GitHub
co-authored by Claude Sonnet 4.6 Codex pre-commit-ci[bot] Copilot
parent ee8a61a3e7
commit 9b7099d3fc
16 changed files with 4762 additions and 46 deletions
+591
View File
@@ -0,0 +1,591 @@
# CompactMask — Memory-Efficient Mask Storage
This example benchmarks `CompactMask`, a new mask representation introduced in `supervision` that replaces dense `(N, H, W)` boolean arrays with a crop-scoped Run-Length Encoding (RLE). The benchmark demonstrates full API compatibility, massive memory savings, and order-of-magnitude annotation speedups — with no change to your existing `Detections` code.
---
## The Problem
Instance segmentation models return one boolean mask per detected object. `supervision` stores these as a stacked `(N, H, W)` numpy array.
For a 4K image with 1 000 detected objects:
```
1 000 x 3840 x 2160 x 1 byte = 8.3 GB
```
At this scale, typical pipelines crash with `MemoryError` before a single frame is annotated. Aerial imagery, satellite tiles, and high-density crowd scenes all hit this wall.
---
## The Solution — Crop-RLE Storage
`CompactMask` stores each mask as a run-length encoding of its **bounding-box crop** rather than the full image canvas.
```
dense (N,H,W) mask → N x crop_RLE + N x (x1,y1) offset
8.3 GB → ~280 KB
```
The bounding boxes are already present in `Detections.xyxy`, so no extra metadata is required from the caller.
### Theoretical analysis (4K scene, 80x80 px objects, ~65% fill per bbox)
Assumptions used throughout the PR design analysis:
| Parameter | Value |
| ---------------------- | ------------------------ |
| Image size | 4K — 3840x2160 = 8.29 MP |
| Avg bounding box | 80x80 px = 6 400 px² |
| Fill ratio within bbox | ~65% |
| Avg contour vertices | ~400 pts |
| Avg RLE runs / mask | ~240 (3 runs x 80 rows) |
#### Space comparison
| Format | Per object | N=100 | N=1 000 | vs Dense |
| ------------------- | -------------- | ------ | ---------- | --------- |
| **Dense** (current) | 8.29 MB | 829 MB | **8.3 GB** | 1x |
| Local Crop + Offset | 6.4 KB | 640 KB | 6.4 MB | 1 300x |
| **Crop-RLE** ✓ | ~2 KB | 200 KB | **2 MB** | 4 000x |
| Polygon ⚠ lossy | ~3.2 KB | 320 KB | 3.2 MB | 2 600x |
| memmap | 8.29 MB (disk) | 829 MB | 8.3 GB | 1x (disk) |
Crop-RLE beats Local Crop because it only encodes actual pixel runs, skipping the ~35% background pixels within each bounding box.
#### Encode time: dense array → format
| Format | Complexity | N=10 | N=100 | N=1 000 |
| ------------------- | --------------------------------- | ------- | ------- | --------- |
| Local Crop + Offset | O(A) — strided slice from xyxy | ~0.1 ms | ~1 ms | ~10 ms |
| **Crop RLE** | O(A) — scan crop rows for runs | ~0.2 ms | ~2 ms | ~20 ms |
| Polygon | O(P) — `cv2.findContours` on crop | ~2 ms | ~20 ms | ~200 ms |
| memmap | O(I) — write 8.29 MB to disk | ~80 ms | ~800 ms | ~8 000 ms |
#### Decode time: format → full (H, W) mask
Required by `MaskAnnotator`, `mask_iou_batch`, `merge()`, etc. Dominant cost at 4K is **allocating and zeroing a 8.29 MB array**, which is identical across all in-memory formats once full materialisation is needed.
| Format | N=10 | N=100 | N=1 000 |
| --------------------- | ------ | ------- | --------- |
| Local Crop / Crop RLE | ~3 ms | ~30 ms | ~300 ms |
| Polygon | ~5 ms | ~50 ms | ~500 ms |
| memmap | ~80 ms | ~800 ms | ~8 000 ms |
#### Decode time: crop-only path (optimised)
When callers need only the bounding-box region — `MaskAnnotator` crop-paint path, `.area`, `contains_holes`, `filter_segments_by_distance`:
| Format | Complexity | N=10 | N=100 | N=1 000 |
| ------------------- | -------------------------------- | -------- | ------- | --------- |
| Local Crop + Offset | O(1) — already stored | ~0 ms | ~0 ms | ~0 ms |
| **Crop RLE** ✓ | O(A) — expand ~240 runs | ~0.02 ms | ~0.2 ms | ~2 ms |
| Polygon | O(A) — `fillPoly` on crop canvas | ~2 ms | ~20 ms | ~200 ms |
| memmap | N/A — always full-size | ~80 ms | ~800 ms | ~8 000 ms |
Crop RLE's `.crop()` method powers the `MaskAnnotator` optimisation — it never allocates the full image canvas, which is the entire source of the annotation speedup.
#### IoU / NMS at 1 % bbox overlap rate (sparse aerial scene)
| Format | Strategy | N=1 000 |
| ------------------- | ------------------------------------- | ---------- |
| Dense (current) | All pairs, 640² pixel AND | ~10 000 ms |
| Local Crop + Offset | Bbox pre-filter → pixel IoU | **~5 ms** |
| Crop RLE | Bbox pre-filter → expand intersection | **~15 ms** |
At N=1 000 with 1 % overlap, bbox pre-filter reduces 499 500 candidate pairs to ~5 000 overlapping pairs — a ~2 000x reduction in pixel-level work.
---
## Why Crop-RLE Was Chosen over Local Crop
Both formats compress extremely well; the deciding factors for Crop-RLE are:
1. **~3x smaller** for masks that are themselves sparse within their bounding box.
2. **COCO RLE interop path** — row-major crop RLE can be re-encoded to column-major full-image RLE for `pycocotools` if needed.
3. `.area` computed directly from run lengths — no materialisation, no allocation.
The main trade-off: crop-only decode is O(A) rather than O(1). For the common solid-fill segmentation mask this is negligible (\<0.1 ms per mask).
---
## Operation-by-Operation Speedup Analysis
This section walks through every `Detections` operation that touches masks and shows exactly why `CompactMask` is faster. All code snippets are taken from the actual implementation. Numbers use the **FHD-200-50%-v600** scenario unless noted (1920 x 1080 image, 200 detections, each mask filling ~50% of the frame, 600-vertex polygons — a realistic hard case with dense fill and complex object boundaries).
At 50% fill on an FHD image each mask's bounding box covers a large portion of the frame, producing many RLE runs per row.
---
### Memory
Dense stores one full-resolution bool array per mask:
```
N x H x W x 1 byte
200 x 1080 x 1920 x 1 = 414 MB
```
Compact stores three lightweight structures:
```python
self._rles: list[npt.NDArray[np.int32]] # N Python references to small int32 arrays
self._crop_shapes: npt.NDArray[np.int32] # (N, 2) — crop (h, w) per mask
self._offsets: npt.NDArray[np.int32] # (N, 2) — (x1, y1) origin per mask
```
Per-mask RLE size at 50% fill with 600-vertex polygons: ~4.7 KB (933 KB / 200). Per-mask dense size: 1920 x 1080 x 1 = 2.1 MB. Per-mask ratio: 2.1 MB / 4.7 KB = **~445x**.
Scaled to N=200: 200 x 4.7 KB = ~933 KB of RLE data, plus `_crop_shapes` (1.6 KB) and `_offsets` (1.6 KB). Python list + array object overhead roughly doubles the footprint for small N.
| Component | Dense | Compact | Ratio |
| --------------- | ---------- | ----------- | --------- |
| Mask data | 414 MB | ~933 KB | ~445x |
| Python overhead | negligible | ~933 KB | -- |
| **Total** | **414 MB** | **~1.9 MB** | **~392x** |
At 5% fill with 8-vertex polygons, the ratio reaches 10 000x–20 000x because crops are tiny and RLEs are extremely short. The benchmark's 4K-200-5%-v8 scenario measures 21 786x (theory) / ~6 000x (malloc). The SAT-200-5%-v8 scenario reaches 62 968x theoretical.
---
### `.area`
Dense `Detections.area` reads every pixel of every mask:
```python
# detection/core.py — dense path
return np.array([np.sum(mask) for mask in self.mask])
# N masks x H x W boolean sums = 200 x 2.1 M = 420 million reads
```
Compact delegates to `_rle_area`, which sums only the odd-indexed run lengths (the True-pixel runs) in each RLE:
```python
# detection/compact_mask.py — _rle_area
return int(np.sum(rle[1::2]))
```
```python
# detection/compact_mask.py — CompactMask.area
return np.array([_rle_area(r) for r in self._rles], dtype=np.int64)
```
At FHD-200-50%-v600, dense `.area` takes 84.66 ms; compact takes 0.48 ms — a **71x speedup**. At SAT-200-20%-v128 the measured speedup reaches **1 204x** because the dense array is 13.4 GB and each sum must scan the entire canvas.
| Factor | Reduction |
| ---------------------------------- | ----------- |
| RLE sums vs full-frame pixel reads | ~4 600x |
| int32 arithmetic vs bool reduction | ~2x |
| No (H, W) allocation per mask | latency |
| **Combined** | **~1 000x** |
---
### `filter` / `__getitem__` (boolean index)
Dense: `masks[bool_array]` triggers NumPy fancy indexing, which allocates a new `(K, H, W)` bool array and copies K full frames:
```python
# detection/core.py — Detections.__getitem__
mask = (self.mask[index] if self.mask is not None else None,)
# For dense ndarray, numpy allocates (K, 2160, 3840) and memcpy's K frames
```
Compact `CompactMask.__getitem__` converts the boolean index to integer positions and builds a new `CompactMask` from Python list indexing and NumPy fancy indexing on small `(N, 2)` arrays:
```python
# detection/compact_mask.py — CompactMask.__getitem__
if isinstance(index, np.ndarray) and index.dtype == bool:
idx_arr = np.where(index)[0]
# ...
new_rles = [self._rles[int(i)] for i in idx_arr]
new_crop_shapes: npt.NDArray[np.int32] = self._crop_shapes[idx_arr]
new_offsets: npt.NDArray[np.int32] = self._offsets[idx_arr]
return CompactMask(new_rles, new_crop_shapes, new_offsets, self._image_shape)
```
At FHD-200-50%-v600, dense `filter` takes 14.56 ms; compact takes 0.03 ms — a **500x speedup**. At SAT-200-20%-v128 the speedup reaches **14 757x**.
| | Dense | Compact |
| ----------- | ----------------------- | ----------------------------------- |
| Data copied | K x H x W (full frames) | K Python references + K x 8 bytes |
| Allocation | new `(K, H, W)` array | new `CompactMask` shell (~trivial) |
| **Speedup** | | **hundreds to tens of thousands x** |
---
### `annotate` (`MaskAnnotator`)
Dense: for each mask, `MaskAnnotator` indexes the full `(H, W)` array and applies a boolean mask across the entire scene:
```python
# annotators/core.py — dense path
mask = np.asarray(detections.mask[detection_idx], dtype=bool)
colored_mask[mask] = color.as_bgr()
```
Each `detections.mask[detection_idx]` for a dense array yields a full `(H, W)` view, and the boolean indexing scans all pixels.
Compact: the annotator detects `CompactMask` and paints only the crop region:
```python
# annotators/core.py — compact path
x1 = int(compact_mask.offsets[detection_idx, 0])
y1 = int(compact_mask.offsets[detection_idx, 1])
crop_m = compact_mask.crop(detection_idx)
crop_h, crop_w = crop_m.shape
colored_mask[y1 : y1 + crop_h, x1 : x1 + crop_w][crop_m] = color.as_bgr()
```
`compact_mask.crop()` decodes the RLE into a `(crop_h, crop_w)` array. At FHD-200-50%-v600, dense `annotate` takes 848.95 ms; compact takes 32.67 ms — a **22x speedup**. At SAT-200-20%-v128 the speedup reaches **89x**.
| Factor | Reduction |
| -------------------------------------------------- | ------------------- |
| Crop decode vs full-frame boolean index (per mask) | crop-size dependent |
| No full `(H, W)` allocation per integer index | latency |
| x N masks | compounds |
| **Combined** | **~26 – 400x** |
---
### IoU (`mask_iou_batch` / `compact_mask_iou_batch`)
Dense `mask_iou_batch` on N=200, FHD:
```python
# detection/utils/iou_and_nms.py — _mask_iou_batch_split
intersection_area = np.logical_and(masks_true[:, None], masks_detection).sum(
axis=(2, 3)
)
# shape (200, 200, 1080, 1920) — ~80 billion boolean ops
# .sum(axis=(2,3)) for intersection counts
# memory_limit splits this into chunks capped at 5 GB scratch
```
Compact `compact_mask_iou_batch` — three layered optimisations:
**1. Vectorised bbox pre-filter — O(N²) array ops, zero decoding**
```python
ix1: npt.NDArray[np.int32] = np.maximum(x1a[:, None], x1b[None, :])
iy1: npt.NDArray[np.int32] = np.maximum(y1a[:, None], y1b[None, :])
ix2: npt.NDArray[np.int32] = np.minimum(x2a[:, None], x2b[None, :])
iy2: npt.NDArray[np.int32] = np.minimum(y2a[:, None], y2b[None, :])
bbox_overlap: npt.NDArray[np.bool_] = (ix1 <= ix2) & (iy1 <= iy2)
```
At 5% fill, two random masks overlap with probability ~4%. ~96% of the N² pairs get IoU = 0 for free — no pixel work at all.
**2. Sub-crop decode — compare only the intersection region**
```python
ox_a, oy_a = int(x1a[i]), int(y1a[i])
sub_a = crops_a[i][ly1 - oy_a : ly2 - oy_a + 1, lx1 - ox_a : lx2 - ox_a + 1]
ox_b, oy_b = int(x1b[j]), int(y1b[j])
sub_b = crops_b[j][ly1 - oy_b : ly2 - oy_b + 1, lx1 - ox_b : lx2 - ox_b + 1]
inter = int(np.logical_and(sub_a, sub_b).sum())
```
The intersection sub-region of two overlapping crops is typically far smaller than the full frame.
**3. Crop caching — each mask decoded at most once**
```python
if i not in crops_a:
crops_a[i] = masks_true.crop(i)
```
Area is obtained from `_rle_area` (sum odd-indexed runs), never touching the pixel grid:
```python
areas_a: npt.NDArray[np.int64] = masks_true.area
```
At FHD-200-50%-v600, dense IoU takes 23 915 ms; compact takes 51.58 ms — a **446x speedup**. At 5% fill / sparse scenarios the speedup is even larger because fewer bbox pairs overlap.
| Factor | Reduction |
| ------------------------------------ | --------------- |
| Bbox pre-filter at sparse fill | 25x |
| Sub-crop vs full frame per pair | ~200x |
| Area from RLE, not `sum(axis=(1,2))` | ~10x |
| No 5 GB scratch allocation | latency |
| **Combined** | **~100 – 500x** |
At 20% fill the gaps close — more pairs overlap, larger crops — speedup drops toward the lower end of the range.
---
### NMS (`mask_non_max_suppression`)
Both dense and compact paths now call `mask_iou_batch(masks, masks)` directly, computing exact mask IoU on the original (unresized) masks. There is no intermediate resize step.
```python
# detection/utils/iou_and_nms.py — NMS (both paths)
ious = mask_iou_batch(masks, masks, overlap_metric)
```
`mask_iou_batch` dispatches internally: when passed a `CompactMask` it calls `compact_mask_iou_batch`, applying all three IoU optimisations (bbox pre-filter, sub-crop decode, crop caching). When passed a dense ndarray it runs the chunked pixel-AND path.
All three IoU optimisations apply to the compact path:
| Factor | Reduction |
| ------------------------------------- | ---------------------------- |
| Bbox pre-filter eliminates most pairs | 25x at sparse fill |
| Sub-crop decode for remaining pairs | ~200x |
| Area from RLE, not pixel sum | ~10x |
| **Combined** | **same as IoU: ~100 – 500x** |
At FHD-200-50%-v600, dense NMS takes 5 231 ms; compact takes 48.15 ms — a **481x speedup**. Dense IoU/NMS is skipped for scenarios above 1 GB (4K-200 and SAT-200 tiers); compact NMS still runs on those.
---
### `merge` (`Detections.merge`)
Dense: `np.vstack` allocates a new `(N1+N2, H, W)` array and copies both halves:
```python
# detection/core.py — dense merge path
return np.vstack([np.asarray(m) for m in masks])
# Merging two 100-mask sets at FHD: 2 x 100 x 2.1 MB = 414 MB copied
```
Compact: `CompactMask.merge` extends a Python list and concatenates two small int32 arrays:
```python
# detection/compact_mask.py — CompactMask.merge
new_rles: list[npt.NDArray[np.int32]] = []
for m in masks_list:
new_rles.extend(m._rles)
new_crop_shapes: npt.NDArray[np.int32] = np.concatenate(
[m._crop_shapes for m in masks_list], axis=0
)
new_offsets: npt.NDArray[np.int32] = np.concatenate(
[m._offsets for m in masks_list], axis=0
)
```
`list.extend` copies N reference pointers. `np.concatenate` on `(N, 2)` int32 arrays copies N x 8 bytes per array.
At FHD-200-50%-v600, dense merge takes 29.71 ms; compact takes 0.03 ms — a **929x speedup**. At SAT-200-20%-v128 the speedup reaches **89 046x**.
| | Dense | Compact |
| ----------- | ----------------------- | -------------------------- |
| Data moved | N x H x W (full frames) | N references + N x 8 bytes |
| Allocation | new `(N, H, W)` array | new `CompactMask` shell |
| **Speedup** | | **effectively free** |
**Note:** `Detections.merge` calls `is_empty()` on each input. Before the `len(xyxy) > 0` short-circuit was added, `is_empty()` invoked `__eq__` which called `np.array_equal(self.to_dense(), ...)` — materialising the entire `(N, H, W)` CompactMask to dense just to check emptiness. The fix:
```python
# detection/core.py — Detections.is_empty (fixed)
if len(self.xyxy) > 0:
return False
```
This O(1) check avoids the O(N x H x W) dense materialisation that previously dominated compact merge time.
---
### `offset` / `with_offset` (`InferenceSlicer` tile stitching)
Dense `move_masks`: allocates a new `(N, new_H, new_W)` array and copies each mask with shifted slice coordinates — O(N x H x W):
```python
# detection/utils/masks.py — move_masks
mask_array = np.full((masks.shape[0], resolution_wh[1], resolution_wh[0]), False)
# ... source/destination slicing logic ...
mask_array[:, dst_y1:dst_y2, dst_x1:dst_x2] = masks[:, src_y1:src_y2, src_x1:src_x2]
```
Compact `with_offset(dx, dy)`: vectorised bounds check first. All new bounding-box positions are computed in a single numpy op. When none overflow the new canvas — the common case in `InferenceSlicer` — the RLE data is not touched at all:
```python
# detection/compact_mask.py — CompactMask.with_offset (fast path)
new_offsets = self._offsets + np.array([dx, dy], dtype=np.int32) # O(N) numpy
needs_clip = (x1s < 0) | (y1s < 0) | (x2s >= new_w) | (y2s >= new_h)
if not needs_clip.any():
return CompactMask(
list(self._rles), self._crop_shapes.copy(), new_offsets, new_image_shape
)
```
When a crop does overflow (e.g. object at a tile edge), only that crop is decoded, sliced, and re-encoded. Masks fully outside bounds get a 1x1 all-False stub without any decoding.
At FHD-200-50%-v600, dense offset takes 42.30 ms; compact takes 0.02 ms — a **2 016x speedup**. At SAT-200-20%-v128 the speedup reaches **290 779x**.
| | Dense | Compact (no-clip fast path) |
| ----------------- | -------------------------------------- | ------------------------------------ |
| Work per mask | allocate `(new_H, new_W)` + copy H x W | add scalar to offset row — O(1) |
| N=200 at FHD | 200 x 2.1 MB = **414 MB** alloc + copy | two numpy ops on `(N, 2)` int32 |
| Output allocation | new `(N, new_H, new_W)` | shared RLE list + new `(N, 2)` array |
| **Speedup** | | **effectively free (>1 000x)** |
In the `InferenceSlicer` pipeline the canvas is always expanded by the tile offset, so no crop ever overflows — the fast path is always taken. Clipping only activates for objects that genuinely straddle the image boundary.
---
### `centroids` (`calculate_masks_centroids`)
Dense: `np.tensordot` reads every pixel of every mask to compute weighted coordinate sums:
```python
# detection/utils/masks.py — dense centroid path
vertical_indices, horizontal_indices = np.indices((height, width)) + 0.5
# np.tensordot(masks, indices, axes=([1, 2], [0, 1]))
# reads all N x H x W values
```
Compact: per-crop loop decodes only the bounding-box region and computes centroids within that crop:
```python
# detection/utils/masks.py — compact centroid path
crop = masks.crop(i)
crop_h, crop_w = crop.shape
x1 = int(masks.offsets[i, 0])
y1 = int(masks.offsets[i, 1])
# ...
crop_rows, crop_cols = np.indices((crop_h, crop_w))
cx = float(np.sum((crop_cols + 0.5)[crop])) / total + x1
cy = float(np.sum((crop_rows + 0.5)[crop])) / total + y1
```
At FHD-200-50%-v600, dense centroids takes 1 133.68 ms; compact takes 60.39 ms — a **13x speedup**. At SAT-200-20%-v128 the speedup reaches **857x** because the dense path must allocate and scan a 13.4 GB array.
| Factor | Reduction |
| ----------------------------------------- | ------------------- |
| Crop area vs full frame (per mask) | fill-dependent |
| No global `np.indices((H, W))` allocation | saves large float64 |
| **Combined (N=200)** | **~19 – 1 000x** |
---
### Summary
Measured speedups at the **FHD-200-50%-v600** operating point (dense fill, complex polygons — a realistic hard case). Dense baseline = 1x.
| Operation | Dense cost | Compact cost | Speedup |
| ---------------- | ----------- | ------------ | ------- |
| Memory | 414 MB | ~1.9 MB | ~392x |
| `.area` | 84.66 ms | 0.48 ms | 71x |
| `filter` | 14.56 ms | 0.03 ms | 500x |
| `annotate` | 848.95 ms | 32.67 ms | 22x |
| `mask_iou_batch` | 23 915 ms | 51.58 ms | 446x |
| NMS | 5 231 ms | 48.15 ms | 481x |
| `merge` | 29.71 ms | 0.03 ms | 929x |
| `with_offset` | 42.30 ms | 0.02 ms | 2 016x |
| `centroids` | 1 133.68 ms | 60.39 ms | 13x |
All speedups are larger at sparser fill fractions and larger resolutions. At SAT-200-20%-v128, `.area` reaches 1 204x and `merge` reaches 89 046x. At the sparsest scenarios (5% fill, 8-vertex polygons), memory ratios exceed 60 000x.
---
## Drop-In Compatibility
`CompactMask` implements the same duck-typed interface as `np.ndarray`:
```python
import supervision as sv
from supervision.detection.compact_mask import CompactMask
# Build from an existing dense (N, H, W) bool array:
compact = CompactMask.from_dense(masks_dense, xyxy, image_shape=(H, W))
# Use exactly like a dense mask — no other code changes needed:
detections = sv.Detections(xyxy=xyxy, mask=compact, class_id=class_ids)
# Filtering, merging, area — all work transparently:
filtered = detections[confidence > 0.5]
areas = detections.area # RLE sum, no materialisation
merged = sv.Detections.merge([det_a, det_b])
# MaskAnnotator works without any change:
annotated = sv.MaskAnnotator().annotate(frame, detections)
# Materialise back to dense when you need raw numpy:
dense_again = compact.to_dense() # (N, H, W) bool
```
Supported indexing patterns:
| Expression | Returns |
| ------------------ | ---------------------------- |
| `mask[i]` (int) | Dense `(H, W)` bool array |
| `mask[bool_array]` | New `CompactMask` (filtered) |
| `mask[slice]` | New `CompactMask` |
| `np.asarray(mask)` | Dense `(N, H, W)` bool array |
---
## Benchmark
Run on any machine — no GPU or real model required:
```bash
uv run python examples/compact_mask/benchmark.py
```
Six image tiers x three fill fractions (5 / 20 / 50 %) x three vertex counts (8 / 128 / 600):
| Tier | Resolution | Objects | Dense array | Notes |
| ------- | ---------- | ------- | ----------- | ------------------------------------ |
| FHD-100 | 1920x1080 | 100 | 0.21 GB | Full operations including IoU+NMS |
| FHD-200 | 1920x1080 | 200 | 0.41 GB | Full operations including IoU+NMS |
| FHD-400 | 1920x1080 | 400 | 0.83 GB | Full operations including IoU+NMS |
| 4K-100 | 3840x2160 | 100 | 0.83 GB | Full operations including IoU+NMS |
| 4K-200 | 3840x2160 | 200 | 1.66 GB | Dense IoU+NMS skipped (array > 1 GB) |
| SAT-200 | 8192x8192 | 200 | 13.4 GB | Dense IoU+NMS skipped (array > 1 GB) |
Dense timing is skipped automatically when the dense IoU/NMS array would exceed 1 GB (`IOU_DENSE_SKIP_GB`), preventing swap thrashing. All dense ops are skipped above 16 GB (`DENSE_SKIP_GB`); no scenario in the current matrix reaches that threshold. Memory is always reported as theoretical `NxHxW` bytes.
### Sample results (macOS, Apple M4 Max, REPS=4)
| Scenario | Dense mem | Compact theor. | Mem x | Area x | Filter x | Annot x | IoU x | NMS x | Merge x | Offset x | Centroids x |
| ---------------- | --------- | -------------- | ------- | ------ | -------- | ------- | ----- | ----- | -------- | -------- | ----------- |
| FHD-100-5%-v8 | 207 MB | 28 KB | 7 418x | — | — | — | — | — | — | — | — |
| FHD-100-50%-v600 | 207 MB | 913 KB | 227x | — | — | — | — | — | — | — | — |
| FHD-200-50%-v600 | 415 MB | 933 KB | 445x | 71x | 500x | 22x | 446x | 481x | 929x | 2 016x | 13x |
| FHD-400-5%-v8 | 829 MB | 60 KB | 13 937x | — | — | — | — | — | — | — | — |
| 4K-100-5%-v8 | 829 MB | 53 KB | 15 554x | — | — | — | — | — | — | — | — |
| 4K-100-20%-v128 | 829 MB | 586 KB | 1 415x | — | — | — | — | — | — | — | — |
| 4K-200-5%-v8 | 1 659 MB | 76 KB | 21 786x | — | — | — | — | — | — | — | — |
| SAT-200-5%-v8 | 13 422 MB | 213 KB | 62 968x | 6 942x | 30 255x | 204x | † | † | 105 545x | 251 629x | 2 173x |
| SAT-200-20%-v128 | 13 422 MB | 2 596 KB | 5 171x | 1 204x | 14 757x | 89x | † | † | 89 046x | 290 779x | 857x |
| SAT-200-50%-v600 | 13 422 MB | 14 222 KB | 944x | — | — | — | † | † | — | — | — |
- **Compact theor.** — sum of internal numpy buffer `nbytes`
- **Mem x** — dense / compact theoretical ratio
- **Area x / Filter x / Annot x / IoU x / NMS x / Merge x / Offset x / Centroids x** — compact speedup over dense for each operation
- **†** — dense IoU+NMS skipped (dense array > 1 GB); compact still runs and is timed
- **—** — not shown; full per-scenario tables are printed by the benchmark script
All non-skipped scenarios pass: pixel-perfect annotation, exact area, lossless `to_dense()` roundtrip.
---
## Use-Cases
- **Aerial / satellite imagery** — thousands of small objects on large tiles; dense masks exhaust RAM before inference completes.
- **High-density crowd / cell segmentation** — N > 500 on FHD already requires several GB of mask storage per batch.
- **Real-time annotation pipelines** — crop-paint cuts annotation from seconds to milliseconds at 4K resolution.
- **Long-running tracking** — accumulated `Detections` across many frames stay in kilobytes rather than gigabytes.
- **`InferenceSlicer`** — `with_offset()` adjusts crop origins directly when stitching tile results; no dense materialisation needed.
---
## Limitations
- `CompactMask` is **not** a full `np.ndarray`. Call `.to_dense()` before passing to code that requires arbitrary ndarray methods (`astype`, `reshape`, `ravel`, `any`, `all`, …).
- RLE format is **row-major (C-order), crop-scoped** — incompatible with pycocotools / COCO API RLEs (column-major, full-image-scoped). Use `.to_dense()` first if you need pycocotools interop.
- `from_dense()` requires the input `(N, H, W)` array to fit in memory. For truly OOM-scale data, build `CompactMask` per-detection directly from model output crops rather than from a pre-allocated dense stack.
---
## Files
| File | Description |
| -------------- | ------------------------------------------------ |
| `benchmark.py` | Full benchmark across FHD / 4K / satellite tiers |
| `README.md` | This file |
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -222,7 +222,7 @@ Script to run object detection on an RTSP stream using the RF-DETR model.
- `--model_size`: RF-DETR backbone size to load — choose from 'nano', 'small', 'medium', 'base', or 'large' (default 'medium').
- `--device`: Compute device to run the model on ('cpu', 'mps', or 'cuda'; default 'cpu').
- `--classes`: Space-separated list of class IDs to track. Leave empty to track all classes.
- `--confidence_threshold`: Minimum confidence score for a detection to be kept, range 0–1 (default 0.3).
- `--confidence_threshold`: Minimum confidence score for a detection to be kept, range 0-1 (default 0.3).
- `--iou_threshold`: IOU threshold applied during non-max suppression (default 0.7).
- `--resolution`: Shortest-side input resolution supplied to the model. The script will round it to the nearest valid multiple (default 640).
+2
View File
@@ -45,6 +45,7 @@ from supervision.dataset.core import (
)
from supervision.dataset.formats.coco import get_coco_class_index_mapping
from supervision.dataset.utils import mask_to_rle, rle_to_mask
from supervision.detection.compact_mask import CompactMask
from supervision.detection.core import Detections
from supervision.detection.line_zone import (
LineZone,
@@ -161,6 +162,7 @@ __all__ = [
"ColorAnnotator",
"ColorLookup",
"ColorPalette",
"CompactMask",
"ComparisonAnnotator",
"ConfusionMatrix",
"CropAnnotator",
+21 -4
View File
@@ -26,6 +26,7 @@ from supervision.annotators.utils import (
wrap_text,
)
from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.detection.compact_mask import CompactMask
from supervision.detection.core import Detections
from supervision.detection.utils.boxes import clip_boxes, spread_out_boxes
from supervision.detection.utils.converters import (
@@ -436,6 +437,9 @@ class MaskAnnotator(BaseAnnotator):
colored_mask = np.array(scene, copy=True, dtype=np.uint8)
compact_mask = (
detections.mask if isinstance(detections.mask, CompactMask) else None
)
for detection_idx in np.flip(np.argsort(detections.area)):
color = resolve_color(
color=self.color,
@@ -445,8 +449,21 @@ class MaskAnnotator(BaseAnnotator):
if custom_color_lookup is None
else custom_color_lookup,
)
mask = np.asarray(detections.mask[detection_idx], dtype=bool)
colored_mask[mask] = color.as_bgr()
if compact_mask is not None:
# Paint only the bounding-box crop — avoids a full (H, W) alloc.
x1 = int(compact_mask.offsets[detection_idx, 0])
y1 = int(compact_mask.offsets[detection_idx, 1])
crop_m = compact_mask.crop(detection_idx)
crop_h, crop_w = crop_m.shape
colored_mask[y1 : y1 + crop_h, x1 : x1 + crop_w][crop_m] = (
color.as_bgr()
)
else:
mask = np.asarray(
detections.mask[detection_idx],
dtype=bool,
)
colored_mask[mask] = color.as_bgr()
cv2.addWeighted(
colored_mask, self.opacity, scene, 1 - self.opacity, 0, dst=scene
@@ -2935,8 +2952,8 @@ class BackgroundOverlayAnnotator(BaseAnnotator):
colored_mask[y1:y2, x1:x2] = scene[y1:y2, x1:x2]
else:
for mask in detections.mask:
mask = np.asarray(mask, dtype=bool)
colored_mask[mask] = scene[mask]
mask_bool = np.asarray(mask, dtype=bool)
colored_mask[mask_bool] = scene[mask_bool]
np.copyto(scene, colored_mask)
return scene
+905
View File
@@ -0,0 +1,905 @@
"""Crop-RLE compact mask storage for memory-efficient instance segmentation.
Dense ``(N, H, W)`` boolean masks use O(N·H·W) memory, which becomes
prohibitive for aerial imagery (e.g. 1000 objects x 4K image ~ 8.3 GB).
:class:`CompactMask` stores each mask as a run-length encoding of its
bounding-box crop, reducing typical usage to tens of MB.
The bounding boxes (``xyxy``) already present in ``Detections`` serve as the
crop boundaries, so no extra metadata is required from the caller.
"""
from __future__ import annotations
from collections.abc import Iterator
from typing import Any, cast
import numpy as np
import numpy.typing as npt
def _rle_encode(mask_2d: npt.NDArray[Any]) -> npt.NDArray[np.int32]:
"""Run-length encode a 2D boolean mask in row-major order.
The encoding starts with the count of leading ``False`` values (may be 0
if the mask begins with ``True``). Subsequent values alternate between
``True`` and ``False`` run counts.
Args:
mask_2d: 2D boolean array of shape ``(H, W)``.
Returns:
int32 array of run lengths, starting with the False count.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import _rle_encode
>>> mask = np.array([[False, True, True], [True, False, False]])
>>> _rle_encode(mask).tolist()
[1, 3, 2]
```
"""
flat = mask_2d.ravel() # C-order (row-major)
if len(flat) == 0:
return np.array([0], dtype=np.int32)
# Locate positions where the boolean value changes.
changes = np.diff(flat.view(np.uint8))
boundaries = np.where(changes != 0)[0] + 1
positions = np.concatenate(([0], boundaries, [len(flat)]))
run_lengths = np.diff(positions).astype(np.int32)
# Guarantee the encoding always starts with a False count.
if flat[0]:
run_lengths = np.concatenate(([np.int32(0)], run_lengths))
return run_lengths
def _rle_decode(
rle: npt.NDArray[np.int32], height: int, width: int
) -> npt.NDArray[np.bool_]:
"""Decode a run-length encoded mask back to a 2D boolean array.
Args:
rle: int32 array of run lengths as produced by :func:`_rle_encode`.
height: Height of the output array.
width: Width of the output array.
Returns:
2D boolean array of shape ``(height, width)``.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import _rle_decode
>>> rle = np.array([1, 3, 2], dtype=np.int32)
>>> _rle_decode(rle, 2, 3)
array([[False, True, True],
[ True, False, False]])
```
"""
# Even-indexed entries → False runs; odd-indexed entries → True runs.
is_true = np.arange(len(rle)) % 2 == 1
flat: npt.NDArray[np.bool_] = np.repeat(is_true, rle)
num_pixels = height * width
if len(flat) < num_pixels:
# Pad with False if the RLE is shorter than expected (e.g. all-False
# tails are often omitted during encoding).
flat = np.pad(flat, (0, num_pixels - len(flat)))
return cast(npt.NDArray[np.bool_], flat[:num_pixels].reshape(height, width))
def _rle_area(rle: npt.NDArray[np.int32]) -> int:
"""Return the number of ``True`` pixels in a run-length encoded mask.
Args:
rle: int32 array of run lengths as produced by :func:`_rle_encode`.
Returns:
Total number of ``True`` pixels.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import _rle_area
>>> rle = np.array([1, 3, 2], dtype=np.int32) # 1 F, 3 T, 2 F
>>> _rle_area(rle)
3
```
"""
return int(np.sum(rle[1::2]))
class CompactMask:
"""Memory-efficient crop-RLE mask storage for instance segmentation.
Instead of storing N full ``(H, W)`` boolean arrays, :class:`CompactMask`
encodes each mask as a run-length sequence of its bounding-box crop. This
reduces memory from O(N·H·W) to roughly O(N·bbox_area), which is orders of
magnitude smaller for sparse masks on high-resolution images.
The class exposes a duck-typed interface compatible with ``np.ndarray``
masks used elsewhere in ``supervision``:
* ``mask[int]`` → dense ``(H, W)`` bool array (annotators, converters).
* ``mask[slice | list | ndarray]`` → new :class:`CompactMask` (filtering).
* ``np.asarray(mask)`` → dense ``(N, H, W)`` bool array (numpy interop).
* ``mask.shape``, ``mask.dtype``, ``mask.area`` — match the dense API.
:class:`CompactMask` is **not** a drop-in ``np.ndarray`` replacement.
When you need to call arbitrary ndarray methods (``astype``, ``reshape``,
``ravel``, ``any``, ``all``, …) call :meth:`to_dense` first:
``cm.to_dense().astype(np.uint8)``. :meth:`to_dense` is the single
explicit materialisation boundary.
.. note:: **RLE encoding incompatibility with pycocotools / COCO API**
:class:`CompactMask` uses **row-major (C-order)** run-lengths scoped
to each mask's bounding-box crop. The COCO API (pycocotools) uses
**column-major (Fortran-order)** run-lengths scoped to the **full
image**. The two formats are not interchangeable: you cannot pass a
:class:`CompactMask` RLE directly to ``maskUtils.iou()`` or
``maskUtils.decode()``, and you cannot load a COCO RLE dict into a
:class:`CompactMask` without re-encoding. Use
:meth:`to_dense` to obtain a standard boolean array, then pass it to
pycocotools if needed.
Args:
rles: List of N int32 run-length arrays.
crop_shapes: Array of shape ``(N, 2)`` — ``(crop_h, crop_w)`` per mask.
offsets: Array of shape ``(N, 2)`` — ``(x1, y1)`` bounding-box origins.
image_shape: ``(H, W)`` of the full image.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((2, 100, 100), dtype=bool)
>>> masks[0, 10:20, 10:20] = True
>>> masks[1, 50:70, 50:80] = True
>>> xyxy = np.array([[10, 10, 19, 19], [50, 50, 79, 69]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> len(cm)
2
>>> cm.shape
(2, 100, 100)
```
"""
__slots__ = ("_crop_shapes", "_image_shape", "_offsets", "_rles")
def __init__(
self,
rles: list[npt.NDArray[np.int32]],
crop_shapes: npt.NDArray[np.int32],
offsets: npt.NDArray[np.int32],
image_shape: tuple[int, int],
) -> None:
self._rles: list[npt.NDArray[np.int32]] = rles
self._crop_shapes: npt.NDArray[np.int32] = crop_shapes # (N,2): (h,w)
self._offsets: npt.NDArray[np.int32] = offsets # (N,2): (x1,y1)
self._image_shape: tuple[int, int] = image_shape # (H, W)
# ------------------------------------------------------------------
# Construction
# ------------------------------------------------------------------
@classmethod
def from_dense(
cls,
masks: npt.NDArray[np.bool_],
xyxy: npt.NDArray[Any],
image_shape: tuple[int, int],
) -> CompactMask:
"""Create a :class:`CompactMask` from a dense ``(N, H, W)`` bool array.
Bounding boxes are clipped to image bounds and interpreted in the
supervision ``xyxy`` convention (inclusive max coordinates). A
box with invalid ordering (``x2 < x1`` or ``y2 < y1``) is replaced by
a ``1x1`` all-False crop to avoid degenerate RLE.
Args:
masks: Dense boolean mask array of shape ``(N, H, W)``.
xyxy: Bounding boxes of shape ``(N, 4)`` in ``[x1, y1, x2, y2]``
format.
image_shape: ``(H, W)`` of the full image.
Returns:
A new :class:`CompactMask` instance.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 100, 100), dtype=bool)
>>> masks[0, 10:20, 10:20] = True
>>> xyxy = np.array([[10, 10, 19, 19]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> cm.shape
(1, 100, 100)
```
"""
img_h, img_w = image_shape
num_masks = len(masks)
if num_masks == 0:
return cls(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
image_shape,
)
rles: list[npt.NDArray[np.int32]] = []
crop_shapes_list: list[tuple[int, int]] = []
offsets_list: list[tuple[int, int]] = []
for mask_idx in range(num_masks):
x1, y1, x2, y2 = xyxy[mask_idx]
x1c = int(max(0, min(int(x1), img_w - 1)))
y1c = int(max(0, min(int(y1), img_h - 1)))
x2c = int(max(0, min(int(x2), img_w - 1)))
y2c = int(max(0, min(int(y2), img_h - 1)))
crop: npt.NDArray[np.bool_]
# supervision xyxy uses inclusive max coords, so slicing must add +1.
if x2c < x1c or y2c < y1c:
crop = np.zeros((1, 1), dtype=bool)
x2c, y2c = x1c, y1c
else:
crop = masks[mask_idx, y1c : y2c + 1, x1c : x2c + 1]
crop_h = y2c - y1c + 1
crop_w = x2c - x1c + 1
rles.append(_rle_encode(crop))
crop_shapes_list.append((crop_h, crop_w))
offsets_list.append((x1c, y1c))
crop_shapes = np.array(crop_shapes_list, dtype=np.int32)
offsets = np.array(offsets_list, dtype=np.int32)
return cls(rles, crop_shapes, offsets, image_shape)
# ------------------------------------------------------------------
# Materialisation
# ------------------------------------------------------------------
def to_dense(self) -> npt.NDArray[np.bool_]:
"""Materialise all masks as a dense ``(N, H, W)`` boolean array.
Returns:
Boolean array of shape ``(N, H, W)``.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 50, 50), dtype=bool)
>>> masks[0, 10:20, 10:30] = True
>>> xyxy = np.array([[10, 10, 29, 19]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(50, 50))
>>> cm.to_dense().shape
(1, 50, 50)
```
"""
num_masks = len(self._rles)
img_h, img_w = self._image_shape
result: npt.NDArray[np.bool_] = np.zeros((num_masks, img_h, img_w), dtype=bool)
for mask_idx in range(num_masks):
crop_h, crop_w = (
int(self._crop_shapes[mask_idx, 0]),
int(self._crop_shapes[mask_idx, 1]),
)
x1, y1 = int(self._offsets[mask_idx, 0]), int(self._offsets[mask_idx, 1])
crop = _rle_decode(self._rles[mask_idx], crop_h, crop_w)
result[mask_idx, y1 : y1 + crop_h, x1 : x1 + crop_w] = crop
return result
def crop(self, index: int) -> npt.NDArray[np.bool_]:
"""Decode a single mask crop without allocating the full image array.
This is an O(crop_area) operation — ideal for annotators that only
need the cropped region.
Args:
index: Index of the mask to decode.
Returns:
Boolean array of shape ``(crop_h, crop_w)``.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 100, 100), dtype=bool)
>>> masks[0, 20:30, 10:40] = True
>>> xyxy = np.array([[10, 20, 39, 29]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> cm.crop(0).shape
(10, 30)
```
"""
crop_h = int(self._crop_shapes[index, 0])
crop_w = int(self._crop_shapes[index, 1])
return _rle_decode(self._rles[index], crop_h, crop_w)
# ------------------------------------------------------------------
# Sequence / array protocol
# ------------------------------------------------------------------
def __len__(self) -> int:
"""Return the number of masks.
Returns:
Number of masks N.
Examples:
```pycon
>>> from supervision.detection.compact_mask import CompactMask
>>> import numpy as np
>>> cm = CompactMask(
... [], np.empty((0, 2), dtype=np.int32),
... np.empty((0, 2), dtype=np.int32), (100, 100))
>>> len(cm)
0
```
"""
return len(self._rles)
def __iter__(self) -> Iterator[npt.NDArray[np.bool_]]:
"""Iterate over masks as dense ``(H, W)`` boolean arrays."""
for mask_idx in range(len(self)):
yield self[mask_idx]
@property
def shape(self) -> tuple[int, int, int]:
"""Return ``(N, H, W)`` matching the dense mask convention.
Returns:
Tuple ``(N, H, W)``.
Examples:
```pycon
>>> from supervision.detection.compact_mask import CompactMask
>>> import numpy as np
>>> cm = CompactMask(
... [], np.empty((0, 2), dtype=np.int32),
... np.empty((0, 2), dtype=np.int32), (480, 640))
>>> cm.shape
(0, 480, 640)
```
"""
img_h, img_w = self._image_shape
return (len(self), img_h, img_w)
@property
def offsets(self) -> npt.NDArray[np.int32]:
"""Return per-mask crop origins as ``(x1, y1)`` integer offsets.
Returns:
Array of shape ``(N, 2)`` with ``int32`` offsets.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 2:4, 3:5] = True
>>> xyxy = np.array([[3, 2, 4, 3]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm.offsets.tolist()
[[3, 2]]
```
"""
return self._offsets
@property
def bbox_xyxy(self) -> npt.NDArray[np.int32]:
"""Return per-mask inclusive bounding boxes in ``xyxy`` format.
Boxes are derived from crop metadata:
``x2 = x1 + crop_w - 1``, ``y2 = y1 + crop_h - 1``.
Returns:
Array of shape ``(N, 4)`` with ``int32`` boxes
``[x1, y1, x2, y2]``.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 2:5, 3:7] = True
>>> xyxy = np.array([[3, 2, 6, 4]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm.bbox_xyxy.tolist()
[[3, 2, 6, 4]]
```
"""
if len(self) == 0:
return np.empty((0, 4), dtype=np.int32)
x1: npt.NDArray[np.int32] = self._offsets[:, 0]
y1: npt.NDArray[np.int32] = self._offsets[:, 1]
x2: npt.NDArray[np.int32] = x1 + self._crop_shapes[:, 1] - 1
y2: npt.NDArray[np.int32] = y1 + self._crop_shapes[:, 0] - 1
return np.column_stack((x1, y1, x2, y2)).astype(np.int32, copy=False)
@property
def dtype(self) -> np.dtype[Any]:
"""Return ``np.dtype(bool)`` — always.
Returns:
``np.dtype(bool)``.
Examples:
```pycon
>>> from supervision.detection.compact_mask import CompactMask
>>> import numpy as np
>>> cm = CompactMask(
... [], np.empty((0, 2), dtype=np.int32),
... np.empty((0, 2), dtype=np.int32), (100, 100))
>>> cm.dtype
dtype('bool')
```
"""
return np.dtype(bool)
@property
def area(self) -> npt.NDArray[np.int64]:
"""Compute the area (``True`` pixel count) of each mask.
Returns:
int64 array of shape ``(N,)`` with per-mask pixel counts.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((2, 100, 100), dtype=bool)
>>> masks[0, 0:10, 0:10] = True # 100 pixels
>>> masks[1, 0:5, 0:5] = True # 25 pixels
>>> xyxy = np.array([[0, 0, 9, 9], [0, 0, 4, 4]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(100, 100))
>>> cm.area.tolist()
[100, 25]
```
"""
return np.array([_rle_area(rle) for rle in self._rles], dtype=np.int64)
def sum(self, axis: int | tuple[int, ...] | None = None) -> npt.NDArray[Any] | int:
"""NumPy-compatible sum with a fast path for per-mask area.
When ``axis=(1, 2)``, returns the per-mask True-pixel count via
:attr:`area` without materialising the full dense array.
Args:
axis: Axis or axes to sum over.
Returns:
Sum result matching NumPy semantics.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 0:3, 0:3] = True
>>> xyxy = np.array([[0, 0, 2, 2]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm.sum(axis=(1, 2)).tolist()
[9]
```
"""
if axis == (1, 2):
return self.area
return self.to_dense().sum(axis=axis)
def __getitem__(
self,
index: int | slice | list[Any] | npt.NDArray[Any],
) -> npt.NDArray[np.bool_] | CompactMask:
"""Index into the mask collection.
* ``int`` → dense ``(H, W)`` bool array (for annotators, iterators).
* ``slice | list | ndarray`` → new :class:`CompactMask` (for filtering).
Args:
index: An integer returns a dense ``(H, W)`` mask. Any other
supported index type returns a new :class:`CompactMask`.
Returns:
Dense ``(H, W)`` ``np.ndarray`` for integer index, or a new
:class:`CompactMask` for all other index types.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((3, 20, 20), dtype=bool)
>>> xyxy = np.array(
... [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
>>> cm[0].shape # int → dense (H, W)
(20, 20)
>>> len(cm[[0, 2]]) # list → CompactMask
2
```
"""
if isinstance(index, (int, np.integer)):
idx = int(index)
img_h, img_w = self._image_shape
result: npt.NDArray[np.bool_] = np.zeros((img_h, img_w), dtype=bool)
crop_h = int(self._crop_shapes[idx, 0])
crop_w = int(self._crop_shapes[idx, 1])
x1 = int(self._offsets[idx, 0])
y1 = int(self._offsets[idx, 1])
crop = _rle_decode(self._rles[idx], crop_h, crop_w)
result[y1 : y1 + crop_h, x1 : x1 + crop_w] = crop
return result
# Slice: use direct Python list slice and numpy view — O(k), no arange.
if isinstance(index, slice):
return CompactMask(
self._rles[index],
self._crop_shapes[index],
self._offsets[index],
self._image_shape,
)
# Boolean selectors and fancy index → convert to integer positions first.
if isinstance(index, np.ndarray) and index.dtype == bool:
idx_arr = np.where(index)[0]
elif isinstance(index, list) and all(
isinstance(item, (bool, np.bool_)) for item in index
):
idx_arr = np.flatnonzero(np.asarray(index, dtype=bool))
else:
idx_arr = np.asarray(list(index), dtype=np.intp)
new_rles = [self._rles[int(mask_idx)] for mask_idx in idx_arr]
new_crop_shapes: npt.NDArray[np.int32] = self._crop_shapes[idx_arr]
new_offsets: npt.NDArray[np.int32] = self._offsets[idx_arr]
return CompactMask(new_rles, new_crop_shapes, new_offsets, self._image_shape)
def __array__(self, dtype: np.dtype[Any] | None = None) -> npt.NDArray[Any]:
"""NumPy interop: materialise as a dense ``(N, H, W)`` array.
Called by ``np.asarray(compact_mask)`` and similar NumPy functions.
Args:
dtype: Optional dtype to cast the result to.
Returns:
Dense boolean array of shape ``(N, H, W)``.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> np.asarray(cm).shape
(1, 10, 10)
```
"""
result = self.to_dense()
if dtype is not None:
return result.astype(dtype)
return result
def __eq__(self, other: object) -> bool:
"""Element-wise equality with another :class:`CompactMask` or ndarray.
Args:
other: Another :class:`CompactMask` or ``np.ndarray``.
Returns:
``True`` if all masks are pixel-identical.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> xyxy = np.array([[0, 0, 5, 5]], dtype=np.float32)
>>> cm1 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm2 = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> cm1 == cm2
True
```
"""
if isinstance(other, CompactMask):
return bool(np.array_equal(self.to_dense(), other.to_dense()))
if isinstance(other, np.ndarray):
return bool(np.array_equal(self.to_dense(), other))
return NotImplemented
# ------------------------------------------------------------------
# Collection utilities
# ------------------------------------------------------------------
@staticmethod
def merge(masks_list: list[CompactMask]) -> CompactMask:
"""Concatenate multiple :class:`CompactMask` objects into one.
All inputs must have the same ``image_shape``.
Args:
masks_list: Non-empty list of :class:`CompactMask` objects.
Returns:
A new :class:`CompactMask` containing every mask from the inputs,
in order.
Raises:
ValueError: If ``masks_list`` is empty or image shapes differ.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks1 = np.zeros((2, 50, 50), dtype=bool)
>>> masks2 = np.zeros((3, 50, 50), dtype=bool)
>>> xyxy1 = np.array([[0,0,10,10],[10,10,20,20]], dtype=np.float32)
>>> xyxy2 = np.array(
... [[0,0,5,5],[5,5,10,10],[10,10,15,15]], dtype=np.float32)
>>> cm1 = CompactMask.from_dense(masks1, xyxy1, image_shape=(50, 50))
>>> cm2 = CompactMask.from_dense(masks2, xyxy2, image_shape=(50, 50))
>>> len(CompactMask.merge([cm1, cm2]))
5
```
"""
if not masks_list:
raise ValueError("Cannot merge an empty list of CompactMask objects.")
image_shape = masks_list[0]._image_shape
for cm in masks_list[1:]:
if cm._image_shape != image_shape:
raise ValueError(
f"Cannot merge CompactMask objects with different image shapes: "
f"{image_shape} vs {cm._image_shape}"
)
# list.extend is a C-level call and avoids the per-element Python
# bytecode overhead of a flat list comprehension. This matters under
# GIL contention when multiple threads call merge concurrently.
new_rles: list[npt.NDArray[np.int32]] = []
for cm in masks_list:
new_rles.extend(cm._rles)
# np.concatenate handles (0, 2) arrays correctly.
# No .astype() needed — _crop_shapes and _offsets are already int32.
new_crop_shapes: npt.NDArray[np.int32] = np.concatenate(
[cm._crop_shapes for cm in masks_list], axis=0
)
new_offsets: npt.NDArray[np.int32] = np.concatenate(
[cm._offsets for cm in masks_list], axis=0
)
return CompactMask(new_rles, new_crop_shapes, new_offsets, image_shape)
def repack(self) -> CompactMask:
"""Re-encode all masks using tight bounding boxes.
When the original ``xyxy`` boxes are padded or loose — common with
object-detector outputs and full-image boxes used in tests — each RLE
crop encodes more background (``False``) pixels than necessary. This
method decodes every crop, trims it to the minimal rectangle that
contains all ``True`` pixels, and re-encodes. All-``False`` masks are
normalised to a ``1x1`` all-``False`` crop.
The call is O(sum of crop areas) — suitable as a one-time cleanup
after accumulating many merges (e.g. after
:class:`~supervision.detection.tools.inference_slicer.InferenceSlicer`
tiles are merged).
Returns:
A new :class:`CompactMask` with minimal-area crops and updated
offsets.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 10, 10), dtype=bool)
>>> masks[0, 3:7, 3:7] = True
>>> # Deliberately loose bbox: covers the full image.
>>> xyxy = np.array([[0, 0, 9, 9]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
>>> repacked = cm.repack()
>>> repacked.offsets.tolist() # tight origin: x1=3, y1=3
[[3, 3]]
```
"""
num_masks = len(self._rles)
if num_masks == 0:
return CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
self._image_shape,
)
new_rles: list[npt.NDArray[np.int32]] = []
new_crop_shapes_list: list[tuple[int, int]] = []
new_offsets_list: list[tuple[int, int]] = []
for mask_idx in range(num_masks):
crop = self.crop(mask_idx)
x1_off = int(self._offsets[mask_idx, 0])
y1_off = int(self._offsets[mask_idx, 1])
rows_any = np.any(crop, axis=1)
cols_any = np.any(crop, axis=0)
if not rows_any.any():
# All-False: normalise to 1x1 to avoid zero-sized arrays.
new_rles.append(_rle_encode(np.zeros((1, 1), dtype=bool)))
new_crop_shapes_list.append((1, 1))
new_offsets_list.append((x1_off, y1_off))
continue
y_indices = np.where(rows_any)[0]
x_indices = np.where(cols_any)[0]
y_min, y_max = int(y_indices[0]), int(y_indices[-1])
x_min, x_max = int(x_indices[0]), int(x_indices[-1])
tight = crop[y_min : y_max + 1, x_min : x_max + 1]
new_rles.append(_rle_encode(tight))
new_crop_shapes_list.append((y_max - y_min + 1, x_max - x_min + 1))
new_offsets_list.append((x1_off + x_min, y1_off + y_min))
return CompactMask(
new_rles,
np.array(new_crop_shapes_list, dtype=np.int32),
np.array(new_offsets_list, dtype=np.int32),
self._image_shape,
)
# ------------------------------------------------------------------
# Slicer support
# ------------------------------------------------------------------
def with_offset(
self,
dx: int,
dy: int,
new_image_shape: tuple[int, int],
) -> CompactMask:
"""Return a new :class:`CompactMask` with adjusted offsets and image shape.
Used by :class:`~supervision.detection.tools.inference_slicer.InferenceSlicer`
to relocate tile-local masks into full-image coordinates without
materialising the dense ``(N, H, W)`` array.
Args:
dx: Pixels to add to every mask's ``x1`` offset.
dy: Pixels to add to every mask's ``y1`` offset.
new_image_shape: ``(H, W)`` of the full (destination) image.
Returns:
New :class:`CompactMask` with updated offsets and image shape.
Crops are clipped to stay inside ``new_image_shape``; masks fully
outside are represented as ``1x1`` all-False crops.
Examples:
```pycon
>>> import numpy as np
>>> from supervision.detection.compact_mask import CompactMask
>>> masks = np.zeros((1, 20, 20), dtype=bool)
>>> xyxy = np.array([[5, 5, 15, 15]], dtype=np.float32)
>>> cm = CompactMask.from_dense(masks, xyxy, image_shape=(20, 20))
>>> cm2 = cm.with_offset(100, 200, new_image_shape=(400, 400))
>>> cm2.offsets[0].tolist()
[105, 205]
```
"""
new_h, new_w = new_image_shape
if new_h <= 0 or new_w <= 0:
raise ValueError("new_image_shape must contain positive dimensions")
num_masks = len(self)
if num_masks == 0:
return CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
new_image_shape,
)
# Vectorised bounds check: compute every new [x1,y1,x2,y2] at once.
# For the common case (InferenceSlicer tiles that fit fully inside the
# new canvas) this catches the "no clipping needed" path in O(N) numpy
# without touching any RLE data.
new_offsets: npt.NDArray[np.int32] = self._offsets + np.array(
[dx, dy], dtype=np.int32
)
x1s = new_offsets[:, 0]
y1s = new_offsets[:, 1]
x2s = x1s + self._crop_shapes[:, 1] - 1
y2s = y1s + self._crop_shapes[:, 0] - 1
needs_clip: npt.NDArray[np.bool_] = (
(x1s < 0) | (y1s < 0) | (x2s >= new_w) | (y2s >= new_h)
)
if not needs_clip.any():
# Fast path: pure offset arithmetic, no decode/re-encode needed.
return CompactMask(
list(self._rles),
self._crop_shapes.copy(),
new_offsets,
new_image_shape,
)
# Slow path: only decode+clip+re-encode the masks that actually overflow.
out_rles: list[npt.NDArray[np.int32]] = []
out_crop_shapes: list[tuple[int, int]] = []
out_offsets_list: list[tuple[int, int]] = []
for mask_idx in range(num_masks):
x1 = int(x1s[mask_idx])
y1 = int(y1s[mask_idx])
x2 = int(x2s[mask_idx])
y2 = int(y2s[mask_idx])
if not needs_clip[mask_idx]:
out_rles.append(self._rles[mask_idx])
out_crop_shapes.append(
(
int(self._crop_shapes[mask_idx, 0]),
int(self._crop_shapes[mask_idx, 1]),
)
)
out_offsets_list.append((x1, y1))
continue
ix1 = max(0, x1)
iy1 = max(0, y1)
ix2 = min(new_w - 1, x2)
iy2 = min(new_h - 1, y2)
if ix1 > ix2 or iy1 > iy2:
anchor_x = min(max(x1, 0), new_w - 1)
anchor_y = min(max(y1, 0), new_h - 1)
out_rles.append(_rle_encode(np.zeros((1, 1), dtype=bool)))
out_crop_shapes.append((1, 1))
out_offsets_list.append((anchor_x, anchor_y))
continue
crop = self.crop(mask_idx)
clipped = crop[iy1 - y1 : iy2 - y1 + 1, ix1 - x1 : ix2 - x1 + 1]
out_rles.append(_rle_encode(clipped))
out_crop_shapes.append((iy2 - iy1 + 1, ix2 - ix1 + 1))
out_offsets_list.append((ix1, iy1))
return CompactMask(
out_rles,
np.array(out_crop_shapes, dtype=np.int32),
np.array(out_offsets_list, dtype=np.int32),
new_image_shape,
)
+22 -9
View File
@@ -13,6 +13,7 @@ from supervision.config import (
CLASS_NAME_DATA_FIELD,
ORIENTED_BOX_COORDINATES,
)
from supervision.detection.compact_mask import CompactMask
from supervision.detection.tools.transformers import (
process_transformers_detection_result,
process_transformers_v4_segmentation_result,
@@ -133,7 +134,8 @@ class Detections:
xyxy: An array of shape `(n, 4)` containing
the bounding boxes coordinates in format `[x1, y1, x2, y2]`
mask: An array of shape `(n, H, W)` containing the segmentation masks
(`bool` data type), or `None` when masks are not available.
(`bool` data type), or `None` when masks are not available, or as
:class:`~supervision.detection.compact_mask.CompactMask`.
confidence: An array of shape `(n,)` containing the confidence scores
of the detections, or `None` when confidence values are not available.
class_id: An array of shape `(n,)` containing the class ids of the
@@ -149,7 +151,7 @@ class Detections:
""" # noqa: E501 // docs
xyxy: npt.NDArray[np.generic]
mask: npt.NDArray[np.generic] | None = None
mask: npt.NDArray[np.generic] | CompactMask | None = None
confidence: npt.NDArray[np.generic] | None = None
class_id: npt.NDArray[np.generic] | None = None
tracker_id: npt.NDArray[np.generic] | None = None
@@ -2073,6 +2075,11 @@ class Detections:
"""
Returns `True` if the `Detections` object is considered empty.
"""
# Fast path: avoids __eq__ which calls np.array_equal(to_dense(), ...)
# and would materialise the entire (N, H, W) CompactMask to a dense
# array just to check emptiness — O(N·H·W) for an O(1) check.
if len(self.xyxy) > 0:
return False
empty_detections = Detections.empty()
empty_detections.data = self.data
empty_detections.metadata = self.metadata
@@ -2150,16 +2157,20 @@ class Detections:
xyxy = np.vstack([d.xyxy for d in detections_list])
def stack_or_none(name: str) -> npt.NDArray[np.generic] | None:
def stack_or_none(
name: str,
) -> npt.NDArray[np.generic] | CompactMask | None:
if all(d.__getattribute__(name) is None for d in detections_list):
return None
if any(d.__getattribute__(name) is None for d in detections_list):
raise ValueError(f"All or none of the '{name}' fields must be None")
return (
np.vstack([d.__getattribute__(name) for d in detections_list])
if name == "mask"
else np.hstack([d.__getattribute__(name) for d in detections_list])
)
if name == "mask":
masks = [d.__getattribute__(name) for d in detections_list]
if all(isinstance(m, CompactMask) for m in masks):
return CompactMask.merge(masks)
# Mixed or all-ndarray: __array__ auto-converts any CompactMask.
return np.vstack([np.asarray(m) for m in masks])
return np.hstack([d.__getattribute__(name) for d in detections_list])
mask = stack_or_none("mask")
confidence = stack_or_none("confidence")
@@ -2281,7 +2292,7 @@ class Detections:
"""
if isinstance(index, str):
return self.data.get(index)
if self.is_empty():
if len(self) == 0:
return self
if isinstance(index, int):
index = [index]
@@ -2343,6 +2354,8 @@ class Detections:
where n is the number of detections.
"""
if self.mask is not None:
if isinstance(self.mask, CompactMask):
return self.mask.area
return np.array([np.sum(mask) for mask in self.mask])
else:
return self.box_area
@@ -10,6 +10,7 @@ import numpy as np
import numpy.typing as npt
from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.detection.compact_mask import CompactMask
from supervision.detection.core import Detections
from supervision.detection.utils.boxes import move_boxes, move_oriented_boxes
from supervision.detection.utils.iou_and_nms import OverlapFilter, OverlapMetric
@@ -46,9 +47,17 @@ def move_detections(
"Resolution width and height are required for moving segmentation "
"detections. This should be the same as (width, height) of image shape."
)
detections.mask = move_masks(
masks=detections.mask, offset=offset, resolution_wh=resolution_wh
)
if isinstance(detections.mask, CompactMask):
# Preserve move_masks clipping semantics without dense materialisation.
detections.mask = detections.mask.with_offset(
dx=int(offset[0]),
dy=int(offset[1]),
new_image_shape=(resolution_wh[1], resolution_wh[0]),
)
else:
detections.mask = move_masks(
masks=detections.mask, offset=offset, resolution_wh=resolution_wh
)
return detections
@@ -75,6 +84,15 @@ class InferenceSlicer:
iou_threshold: IOU threshold used in merging overlap filtering.
overlap_metric: Metric to compute overlap (`IOU` or `IOS`).
thread_workers: Number of threads for concurrent slice inference.
compact_masks: If ``True``, dense ``(N, H, W)`` boolean mask
arrays returned by the callback are immediately converted to a
:class:`~supervision.detection.compact_mask.CompactMask`. This
keeps masks in run-length-encoded form for the entire pipeline —
merge, NMS, and annotation — avoiding the large ``(N, H, W)``
allocations that cause OOM on high-resolution images with many
objects. IoU and NMS are computed directly on the RLE crops
without ever materialising a full ``(N, H, W)`` array.
Defaults to ``False`` for backward compatibility.
Raises:
ValueError: If `slice_wh` or `overlap_wh` are invalid or inconsistent.
@@ -123,6 +141,7 @@ class InferenceSlicer:
iou_threshold: float = 0.5,
overlap_metric: OverlapMetric | str = OverlapMetric.IOU,
thread_workers: int = 1,
compact_masks: bool = False,
):
slice_wh_norm = self._normalize_slice_wh(slice_wh)
overlap_wh_norm = self._normalize_overlap_wh(overlap_wh)
@@ -136,6 +155,7 @@ class InferenceSlicer:
self.overlap_filter = OverlapFilter.from_value(overlap_filter)
self.callback: Callable[[ImageType], Detections] = callback
self.thread_workers = thread_workers
self.compact_masks = compact_masks
self._out_of_slice_bounds_warned: bool = False
self._out_of_slice_bounds_lock = threading.Lock()
@@ -199,8 +219,20 @@ class InferenceSlicer:
"""
image_slice = crop_image(image=image, xyxy=offset)
detections = self.callback(image_slice)
resolution_wh = get_image_resolution_wh(image)
if (
self.compact_masks
and detections.mask is not None
and isinstance(detections.mask, np.ndarray)
):
slice_w, slice_h = get_image_resolution_wh(image_slice)
detections.mask = CompactMask.from_dense(
detections.mask,
detections.xyxy,
image_shape=(slice_h, slice_w),
)
resolution_wh = get_image_resolution_wh(image)
# Fast-path: skip locking and bounds checking when the warning has already
# been emitted or when there are no detections to inspect.
needs_warning_check = (
+166 -20
View File
@@ -6,6 +6,7 @@ from typing import Any, cast
import numpy as np
import numpy.typing as npt
from supervision.detection.compact_mask import CompactMask
from supervision.detection.utils.converters import polygon_to_mask
from supervision.detection.utils.masks import resize_masks
@@ -30,7 +31,7 @@ class OverlapFilter(Enum):
@classmethod
def list(cls) -> list[str]:
return list(map(lambda c: c.value, cls))
return list(map(lambda member: member.value, cls))
@classmethod
def from_value(cls, value: OverlapFilter | str) -> OverlapFilter:
@@ -66,7 +67,7 @@ class OverlapMetric(Enum):
@classmethod
def list(cls) -> list[str]:
return list(map(lambda c: c.value, cls))
return list(map(lambda member: member.value, cls))
@classmethod
def from_value(cls, value: OverlapMetric | str) -> OverlapMetric:
@@ -351,9 +352,9 @@ def box_iou_batch_with_jaccard(
ious: npt.NDArray[np.float64] = np.zeros(
(len(boxes_detection), len(boxes_true)), dtype=np.float64
)
for g_idx, g in enumerate(boxes_true):
for d_idx, d in enumerate(boxes_detection):
ious[d_idx, g_idx] = _jaccard(d, g, is_crowd[g_idx])
for gt_idx, gt_box in enumerate(boxes_true):
for det_idx, det_box in enumerate(boxes_detection):
ious[det_idx, gt_idx] = _jaccard(det_box, gt_box, is_crowd[gt_idx])
return ious
@@ -385,19 +386,124 @@ def oriented_box_iou_batch(
max_width = int(max(boxes_true[:, :, 1].max(), boxes_detection[:, :, 1].max()) + 1)
mask_true = np.zeros((boxes_true.shape[0], max_height, max_width), dtype=np.uint8)
for i, box_true in enumerate(boxes_true):
mask_true[i] = polygon_to_mask(box_true, (max_width, max_height))
for box_idx, box_true in enumerate(boxes_true):
mask_true[box_idx] = polygon_to_mask(box_true, (max_width, max_height))
mask_detection = np.zeros(
(boxes_detection.shape[0], max_height, max_width), dtype=np.uint8
)
for i, box_detection in enumerate(boxes_detection):
mask_detection[i] = polygon_to_mask(box_detection, (max_width, max_height))
for box_idx, box_detection in enumerate(boxes_detection):
mask_detection[box_idx] = polygon_to_mask(
box_detection, (max_width, max_height)
)
ious = mask_iou_batch(mask_true, mask_detection)
return ious
def compact_mask_iou_batch(
masks_true: Any,
masks_detection: Any,
overlap_metric: OverlapMetric = OverlapMetric.IOU,
) -> npt.NDArray[np.floating]:
"""Compute pairwise overlap between two :class:`CompactMask` collections.
Avoids materialising full ``(N, H, W)`` arrays by:
1. Vectorised bounding-box pre-filter — pairs whose boxes do not overlap
get IoU = 0 without any mask decoding.
2. Sub-crop decoding — for overlapping pairs, only the intersection region
of each crop is decoded and compared.
3. Crop caching — each individual crop is decoded at most once even when it
participates in many pairs.
The result is numerically identical to running the dense
:func:`mask_iou_batch` on ``np.asarray(masks_true)`` /
``np.asarray(masks_detection)``.
Args:
masks_true: :class:`~supervision.detection.compact_mask.CompactMask`
holding the ground-truth masks.
masks_detection: :class:`~supervision.detection.compact_mask.CompactMask`
holding the detection masks.
overlap_metric: :class:`OverlapMetric` — ``IOU`` or ``IOS``.
Returns:
Float array of shape ``(N1, N2)`` with pairwise overlap values.
"""
n1: int = len(masks_true)
n2: int = len(masks_detection)
result: npt.NDArray[np.floating] = np.zeros((n1, n2), dtype=float)
if n1 == 0 or n2 == 0:
return result
areas_a: npt.NDArray[np.int64] = masks_true.area
areas_b: npt.NDArray[np.int64] = masks_detection.area
# Inclusive per-mask bounding boxes obtained from public accessors.
# bbox_xyxy: (N, 4) → (x1, y1, x2, y2)
bboxes_a: npt.NDArray[np.int32] = masks_true.bbox_xyxy.astype(np.int32)
x1a: npt.NDArray[np.int32] = bboxes_a[:, 0]
y1a: npt.NDArray[np.int32] = bboxes_a[:, 1]
x2a: npt.NDArray[np.int32] = bboxes_a[:, 2]
y2a: npt.NDArray[np.int32] = bboxes_a[:, 3]
bboxes_b: npt.NDArray[np.int32] = masks_detection.bbox_xyxy.astype(np.int32)
x1b: npt.NDArray[np.int32] = bboxes_b[:, 0]
y1b: npt.NDArray[np.int32] = bboxes_b[:, 1]
x2b: npt.NDArray[np.int32] = bboxes_b[:, 2]
y2b: npt.NDArray[np.int32] = bboxes_b[:, 3]
# Pairwise intersection bounding box — shape (N1, N2).
ix1: npt.NDArray[np.int32] = np.maximum(x1a[:, None], x1b[None, :])
iy1: npt.NDArray[np.int32] = np.maximum(y1a[:, None], y1b[None, :])
ix2: npt.NDArray[np.int32] = np.minimum(x2a[:, None], x2b[None, :])
iy2: npt.NDArray[np.int32] = np.minimum(y2a[:, None], y2b[None, :])
bbox_overlap: npt.NDArray[np.bool_] = (ix1 <= ix2) & (iy1 <= iy2)
# Decode each crop at most once, even if it participates in many pairs.
crops_a: dict[int, npt.NDArray[np.bool_]] = {}
crops_b: dict[int, npt.NDArray[np.bool_]] = {}
for idx_pair in np.argwhere(bbox_overlap):
idx_a, idx_b = int(idx_pair[0]), int(idx_pair[1])
if idx_a not in crops_a:
crops_a[idx_a] = masks_true.crop(idx_a)
if idx_b not in crops_b:
crops_b[idx_b] = masks_detection.crop(idx_b)
lx1 = int(ix1[idx_a, idx_b])
ly1 = int(iy1[idx_a, idx_b])
lx2 = int(ix2[idx_a, idx_b])
ly2 = int(iy2[idx_a, idx_b])
ox_a, oy_a = int(x1a[idx_a]), int(y1a[idx_a])
sub_a = crops_a[idx_a][ly1 - oy_a : ly2 - oy_a + 1, lx1 - ox_a : lx2 - ox_a + 1]
ox_b, oy_b = int(x1b[idx_b]), int(y1b[idx_b])
sub_b = crops_b[idx_b][ly1 - oy_b : ly2 - oy_b + 1, lx1 - ox_b : lx2 - ox_b + 1]
inter = int(np.logical_and(sub_a, sub_b).sum())
area_a_i = int(areas_a[idx_a])
area_b_j = int(areas_b[idx_b])
if overlap_metric == OverlapMetric.IOU:
union = area_a_i + area_b_j - inter
result[idx_a, idx_b] = inter / union if union > 0 else 0.0
elif overlap_metric == OverlapMetric.IOS:
small = min(area_a_i, area_b_j)
result[idx_a, idx_b] = inter / small if small > 0 else 0.0
else:
raise ValueError(
f"overlap_metric {overlap_metric} is not supported, "
"only 'IOU' and 'IOS' are supported"
)
return result
def _mask_iou_batch_split(
masks_true: npt.NDArray[Any],
masks_detection: npt.NDArray[Any],
@@ -461,16 +567,33 @@ def mask_iou_batch(
Compute Intersection over Union (IoU) of two sets of masks -
`masks_true` and `masks_detection`.
Accepts both dense ``(N, H, W)`` boolean arrays and
:class:`~supervision.detection.compact_mask.CompactMask` objects.
When both inputs are :class:`~supervision.detection.compact_mask.CompactMask`,
the computation uses :func:`compact_mask_iou_batch` to avoid materialising
full ``(N, H, W)`` arrays.
Args:
masks_true: 3D `np.ndarray` representing ground-truth masks.
masks_detection: 3D `np.ndarray` representing detection masks.
overlap_metric: Metric used to compute the degree of overlap
between pairs of masks (e.g., IoU, IoS).
memory_limit: Memory limit in MB, default is 1024 * 5 MB (5GB).
Ignored when both inputs are CompactMask.
Returns:
Pairwise IoU of masks from `masks_true` and `masks_detection`.
"""
if isinstance(masks_true, CompactMask) and isinstance(masks_detection, CompactMask):
return compact_mask_iou_batch(masks_true, masks_detection, overlap_metric)
# Materialise any CompactMask that was passed alongside a dense array.
if isinstance(masks_true, CompactMask):
masks_true = np.asarray(masks_true)
if isinstance(masks_detection, CompactMask):
masks_detection = np.asarray(masks_detection)
memory = (
masks_true.shape[0]
* masks_true.shape[1]
@@ -494,10 +617,12 @@ def mask_iou_batch(
),
1,
)
for i in range(0, masks_true.shape[0], step):
for chunk_start in range(0, masks_true.shape[0], step):
ious.append(
_mask_iou_batch_split(
masks_true[i : i + step], masks_detection, overlap_metric
masks_true[chunk_start : chunk_start + step],
masks_detection,
overlap_metric,
)
)
@@ -514,6 +639,11 @@ def mask_non_max_suppression(
"""
Perform Non-Maximum Suppression (NMS) on segmentation predictions.
IoU is computed exactly on the full-resolution masks for both dense and
:class:`~supervision.detection.compact_mask.CompactMask` inputs. The
``mask_dimension`` parameter is kept for backward compatibility but is no
longer used — dense masks are **not** resized before IoU computation.
Args:
predictions: A 2D array of object detection predictions in
the format of `(x_min, y_min, x_max, y_max, score)`
@@ -526,8 +656,8 @@ def mask_non_max_suppression(
to use for non-maximum suppression.
overlap_metric: Metric used to compute the degree of overlap
between pairs of masks (e.g., IoU, IoS).
mask_dimension: The dimension to which the masks should be
resized before computing IOU values. Defaults to 640.
mask_dimension: Deprecated, no longer used. Kept for backward
compatibility.
Returns:
A boolean array indicating which predictions to keep after
@@ -549,15 +679,19 @@ def mask_non_max_suppression(
sort_index = predictions[:, 4].argsort()[::-1]
predictions = predictions[sort_index]
masks = masks[sort_index]
masks_resized = resize_masks(masks, mask_dimension)
ious = mask_iou_batch(masks_resized, masks_resized, overlap_metric)
ious = mask_iou_batch(masks, masks, overlap_metric)
categories = predictions[:, 5]
keep = np.ones(rows, dtype=bool)
for i in range(rows):
if keep[i]:
condition = (ious[i] > iou_threshold) & (categories[i] == categories)
keep[i + 1 :] = np.where(condition[i + 1 :], False, keep[i + 1 :])
for row_idx in range(rows):
if keep[row_idx]:
condition = (ious[row_idx] > iou_threshold) & (
categories[row_idx] == categories
)
keep[row_idx + 1 :] = np.where(
condition[row_idx + 1 :], False, keep[row_idx + 1 :]
)
return cast(npt.NDArray[np.bool_], keep[sort_index.argsort()])
@@ -712,7 +846,19 @@ def mask_non_max_merge(
AssertionError: If `iou_threshold` is not within the closed
range from `0` to `1`.
"""
masks_resized = resize_masks(masks, mask_dimension)
if isinstance(masks, CompactMask):
# _group_overlapping_masks needs dense arrays for logical_or union merging.
# Note: np.asarray(masks) first materialises a full-resolution (N, H, W)
# dense array before downscaling with resize_masks. This reduces the size
# of the array used for overlap computation but does not avoid the initial
# full-frame materialisation, which may still be memory-intensive for very
# large images or object counts.
masks = resize_masks(np.asarray(masks), mask_dimension)
else:
masks = resize_masks(masks, mask_dimension)
masks_resized = masks
if predictions.shape[1] == 5:
return _group_overlapping_masks(
predictions, masks_resized, iou_threshold, overlap_metric
+29 -2
View File
@@ -6,6 +6,8 @@ import cv2
import numpy as np
import numpy.typing as npt
from supervision.detection.compact_mask import CompactMask
def move_masks(
masks: npt.NDArray[np.bool_],
@@ -86,7 +88,7 @@ def move_masks(
def calculate_masks_centroids(
masks: npt.NDArray[Any],
masks: npt.NDArray[Any] | CompactMask,
) -> npt.NDArray[np.int_]:
"""
Calculate the centroids of binary masks in a tensor.
@@ -94,11 +96,36 @@ def calculate_masks_centroids(
Args:
masks: A 3D NumPy array of shape (num_masks, height, width).
Each 2D array in the tensor represents a binary mask.
Also accepts a :class:`~supervision.detection.compact_mask.CompactMask`.
Returns:
A 2D NumPy array of shape (num_masks, 2), where each row contains the x and y
coordinates (in that order) of the centroid of the corresponding mask.
"""
if isinstance(masks, CompactMask):
# Compute centroids per-crop to avoid materialising the full (N, H, W) array.
n = len(masks)
if n == 0:
return cast(npt.NDArray[np.int_], np.empty((0, 2), dtype=int))
centroids: npt.NDArray[np.float64] = np.zeros((n, 2), dtype=np.float64)
for i in range(n):
crop = masks.crop(i)
crop_h, crop_w = crop.shape
x1 = int(masks.offsets[i, 0])
y1 = int(masks.offsets[i, 1])
total = int(crop.sum())
if total == 0:
centroids[i] = [0.0, 0.0]
continue
# Match the +0.5 offset used by the dense implementation.
crop_rows, crop_cols = np.indices((crop_h, crop_w))
cx = float(np.sum((crop_cols + 0.5)[crop])) / total + x1
cy = float(np.sum((crop_rows + 0.5)[crop])) / total + y1
centroids[i] = [cx, cy]
return cast(npt.NDArray[np.int_], centroids.astype(int))
_num_masks, height, width = masks.shape
total_pixels = masks.sum(axis=(1, 2))
@@ -339,7 +366,7 @@ def filter_segments_by_distance(
```
The nearby 2×2 block at columns 6–7 is kept because its edge distance
The nearby 2x2 block at columns 6-7 is kept because its edge distance
is within 3 pixels. The distant block at columns 9-10 is removed.
""" # noqa E501 // docs
if mask.dtype != bool:
+12 -6
View File
@@ -7,6 +7,7 @@ import numpy as np
import numpy.typing as npt
from supervision.config import ORIENTED_BOX_COORDINATES
from supervision.detection.compact_mask import CompactMask
from supervision.metrics.core import MetricTarget
if TYPE_CHECKING:
@@ -122,12 +123,15 @@ def get_bbox_size_category(xyxy: npt.NDArray[np.float32]) -> npt.NDArray[np.int_
return result
def get_mask_size_category(mask: npt.NDArray[np.bool_]) -> npt.NDArray[np.int_]:
def get_mask_size_category(
mask: npt.NDArray[np.bool_] | CompactMask,
) -> npt.NDArray[np.int_]:
"""
Get the size category of detection masks.
Args:
mask: The mask array shaped (N, H, W).
mask: The mask array shaped (N, H, W), or a
:class:`~supervision.detection.compact_mask.CompactMask`.
Returns:
The size category of each mask, matching
@@ -146,10 +150,12 @@ def get_mask_size_category(mask: npt.NDArray[np.bool_]) -> npt.NDArray[np.int_]:
```
"""
if len(mask.shape) != 3:
raise ValueError("Masks must be shaped (N, H, W)")
areas = np.sum(mask, axis=(1, 2))
if isinstance(mask, CompactMask):
areas = mask.area
else:
if len(mask.shape) != 3:
raise ValueError("Masks must be shaped (N, H, W)")
areas = np.sum(mask, axis=(1, 2))
result = np.full(areas.shape, ObjectSizeCategory.ANY.value)
SM, LG = SIZE_THRESHOLDS
+8
View File
@@ -2,6 +2,7 @@ from typing import Any
import numpy as np
from supervision.detection.compact_mask import CompactMask
from supervision.utils.internal import warn_deprecated
@@ -27,6 +28,13 @@ def validate_mask(mask: Any, n: int) -> None:
if mask is None:
return
# Fast path: CompactMask only needs a length check.
if isinstance(mask, CompactMask):
if len(mask) != n:
raise ValueError(f"mask must contain {n} masks, but got {len(mask)}")
return
expected_shape = f"({n}, H, W)"
actual_shape = str(getattr(mask, "shape", None))
actual_dtype = getattr(mask, "dtype", None)
+936
View File
@@ -0,0 +1,936 @@
"""Unit tests for CompactMask and its private RLE helpers."""
from __future__ import annotations
from contextlib import ExitStack as DoesNotRaise
import numpy as np
import pytest
from supervision.detection.compact_mask import (
CompactMask,
_rle_area,
_rle_decode,
_rle_encode,
)
from supervision.detection.utils.converters import mask_to_xyxy
from supervision.detection.utils.masks import (
calculate_masks_centroids,
contains_holes,
contains_multiple_segments,
move_masks,
)
def _make_cm(masks: np.ndarray, image_shape: tuple[int, int]) -> CompactMask:
"""Build a CompactMask whose crops equal the full bounding-box extents."""
num_masks = len(masks)
img_h, img_w = image_shape
xyxy = np.tile(np.array([0, 0, img_w, img_h], dtype=np.float32), (num_masks, 1))
return CompactMask.from_dense(masks, xyxy, image_shape=image_shape)
class TestRleHelpers:
"""Tests for _rle_encode, _rle_decode, and _rle_area.
Verifies that the private RLE encoding round-trips correctly for a range
of mask shapes (all-False, all-True, diagonal, L-shape, checkerboard,
single-pixel, and empty), and that _rle_area matches np.sum on the
original boolean array.
"""
@pytest.mark.parametrize(
("mask_2d", "description"),
[
(np.zeros((5, 5), dtype=bool), "all-False"),
(np.ones((5, 5), dtype=bool), "all-True"),
(np.eye(4, dtype=bool), "diagonal"),
(
np.array([[True, True, False], [True, False, False]], dtype=bool),
"L-shape",
),
(
np.indices((4, 4)).sum(axis=0) % 2 == 0,
"checkerboard",
),
(np.zeros((1, 1), dtype=bool), "single-pixel-False"),
(np.ones((1, 1), dtype=bool), "single-pixel-True"),
(np.zeros((0, 0), dtype=bool), "empty"),
],
)
def test_encode_decode_round_trip(
self, mask_2d: np.ndarray, description: str
) -> None:
if mask_2d.size == 0:
rle = _rle_encode(mask_2d)
assert _rle_area(rle) == 0
return
rle = _rle_encode(mask_2d)
assert rle.dtype == np.int32, "RLE must be int32"
reconstructed = _rle_decode(rle, mask_2d.shape[0], mask_2d.shape[1])
np.testing.assert_array_equal(
reconstructed, mask_2d, err_msg=f"Round-trip failed for: {description}"
)
@pytest.mark.parametrize(
"mask_2d",
[
np.zeros((6, 6), dtype=bool),
np.ones((6, 6), dtype=bool),
np.eye(6, dtype=bool),
np.array([[True, False, True], [False, True, False]], dtype=bool),
],
)
def test_area_matches_numpy_sum(self, mask_2d: np.ndarray) -> None:
rle = _rle_encode(mask_2d)
assert _rle_area(rle) == int(np.sum(mask_2d))
class TestFromDenseToDense:
"""Tests for CompactMask.from_dense and to_dense.
Verifies that the from_dense → to_dense round-trip is lossless when the
bounding boxes span the full image (no True pixels fall outside the crop).
Covers N=0 (empty), N=1 (single mask), and N=5 (several random masks).
"""
@pytest.mark.parametrize(
("num_masks", "image_shape"),
[
(0, (50, 50)),
(1, (50, 50)),
(5, (50, 50)),
],
)
def test_round_trip(self, num_masks: int, image_shape: tuple[int, int]) -> None:
rng = np.random.default_rng(42)
img_h, img_w = image_shape
masks = rng.integers(0, 2, size=(num_masks, img_h, img_w)).astype(bool)
cm = _make_cm(masks, image_shape)
np.testing.assert_array_equal(cm.to_dense(), masks)
def test_round_trip_with_mask_to_xyxy(self) -> None:
"""Round-trip must be lossless with inclusive xyxy from mask_to_xyxy."""
img_h, img_w = 12, 14
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 3:7, 4:9] = True # non-full-image object
xyxy = mask_to_xyxy(masks).astype(np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
np.testing.assert_array_equal(cm.to_dense(), masks)
class TestGetItem:
"""Tests for CompactMask.__getitem__.
Covers four indexing modes:
- Integer index → dense (H, W) np.ndarray with correct shape and dtype.
- List of indices → new CompactMask with the selected detections.
- Slice → new CompactMask with the sliced detections.
- Boolean ndarray → new CompactMask filtered by the boolean selector.
"""
def test_int_returns_2d_dense(self) -> None:
img_h, img_w = 30, 40
rng = np.random.default_rng(0)
masks = rng.integers(0, 2, size=(3, img_h, img_w)).astype(bool)
cm = _make_cm(masks, (img_h, img_w))
result = cm[1]
assert isinstance(result, np.ndarray)
assert result.shape == (img_h, img_w)
assert result.dtype == bool
np.testing.assert_array_equal(result, masks[1])
def test_list_returns_compact_mask(self) -> None:
img_h, img_w = 20, 20
masks = np.zeros((4, img_h, img_w), dtype=bool)
for mask_idx in range(4):
masks[
mask_idx,
mask_idx * 2 : mask_idx * 2 + 2,
mask_idx * 2 : mask_idx * 2 + 2,
] = True
cm = _make_cm(masks, (img_h, img_w))
subset = cm[[0, 2]]
assert isinstance(subset, CompactMask)
assert len(subset) == 2
np.testing.assert_array_equal(subset[0], masks[0])
np.testing.assert_array_equal(subset[1], masks[2])
def test_slice_returns_compact_mask(self) -> None:
img_h, img_w = 20, 20
masks = np.zeros((5, img_h, img_w), dtype=bool)
cm = _make_cm(masks, (img_h, img_w))
subset = cm[1:4]
assert isinstance(subset, CompactMask)
assert len(subset) == 3
def test_bool_ndarray(self) -> None:
img_h, img_w = 15, 15
rng = np.random.default_rng(7)
masks = rng.integers(0, 2, size=(4, img_h, img_w)).astype(bool)
cm = _make_cm(masks, (img_h, img_w))
selector = np.array([True, False, True, False])
subset = cm[selector]
assert isinstance(subset, CompactMask)
assert len(subset) == 2
np.testing.assert_array_equal(subset[0], masks[0])
np.testing.assert_array_equal(subset[1], masks[2])
def test_bool_list(self) -> None:
"""Python list[bool] should behave like boolean masking."""
img_h, img_w = 15, 15
rng = np.random.default_rng(8)
masks = rng.integers(0, 2, size=(4, img_h, img_w)).astype(bool)
cm = _make_cm(masks, (img_h, img_w))
subset = cm[[True, False, True, False]]
assert isinstance(subset, CompactMask)
assert len(subset) == 2
np.testing.assert_array_equal(subset[0], masks[0])
np.testing.assert_array_equal(subset[1], masks[2])
class TestProperties:
"""Tests for len, shape, dtype, and area properties.
Verifies that the shape tuple follows the (N, H, W) dense convention,
dtype is always bool, and area returns per-mask True-pixel counts that
match np.sum on the corresponding dense masks.
"""
def test_len(self) -> None:
masks = np.zeros((3, 10, 10), dtype=bool)
cm = _make_cm(masks, (10, 10))
assert len(cm) == 3
def test_shape(self) -> None:
masks = np.zeros((3, 10, 10), dtype=bool)
cm = _make_cm(masks, (10, 10))
assert cm.shape == (3, 10, 10)
def test_shape_empty(self) -> None:
cm = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(480, 640),
)
assert cm.shape == (0, 480, 640)
def test_dtype(self) -> None:
cm = _make_cm(np.zeros((1, 5, 5), dtype=bool), (5, 5))
assert cm.dtype == np.dtype(bool)
def test_area_matches_dense(self) -> None:
img_h, img_w = 20, 20
rng = np.random.default_rng(3)
masks = rng.integers(0, 2, size=(4, img_h, img_w)).astype(bool)
cm = _make_cm(masks, (img_h, img_w))
expected = np.array([mask.sum() for mask in masks])
np.testing.assert_array_equal(cm.area, expected)
def test_area_empty(self) -> None:
cm = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(10, 10),
)
assert cm.area.shape == (0,)
class TestCrop:
"""Tests for CompactMask.crop.
Verifies that crop(index) returns an array shaped (crop_h, crop_w)
containing only the pixels within the bounding box, without allocating
the full (H, W) image.
"""
def test_returns_crop_shape(self) -> None:
img_h, img_w = 50, 60
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 10:30, 5:25] = True # 20 x 20 region
xyxy = np.array([[5, 10, 24, 29]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
crop = cm.crop(0)
assert crop.shape == (20, 20)
assert crop.all() # the entire crop should be True
class TestArrayProtocol:
"""Tests for the __array__ protocol.
Verifies that np.asarray(cm) materialises the full (N, H, W) dense array
and that optional dtype casting (e.g. to uint8) is correctly applied.
"""
def test_array_protocol(self) -> None:
img_h, img_w = 10, 10
rng = np.random.default_rng(9)
masks = rng.integers(0, 2, size=(2, img_h, img_w)).astype(bool)
cm = _make_cm(masks, (img_h, img_w))
arr = np.asarray(cm)
assert arr.shape == (2, img_h, img_w)
np.testing.assert_array_equal(arr, masks)
def test_dtype_cast(self) -> None:
masks = np.ones((1, 5, 5), dtype=bool)
cm = _make_cm(masks, (5, 5))
arr = np.asarray(cm, dtype=np.uint8)
assert arr.dtype == np.uint8
assert arr.sum() == 25
class TestMerge:
"""Tests for CompactMask.merge.
Verifies that multiple CompactMask instances with the same image_shape
can be concatenated into a single CompactMask, that merging with an empty
instance works, that an empty input list raises ValueError, and that
mismatched image shapes raise ValueError.
"""
def test_merge(self) -> None:
img_h, img_w = 20, 20
masks1 = np.zeros((2, img_h, img_w), dtype=bool)
masks2 = np.zeros((3, img_h, img_w), dtype=bool)
cm1 = _make_cm(masks1, (img_h, img_w))
cm2 = _make_cm(masks2, (img_h, img_w))
merged = CompactMask.merge([cm1, cm2])
assert len(merged) == 5
assert merged.shape == (5, img_h, img_w)
np.testing.assert_array_equal(
merged.to_dense(), np.concatenate([masks1, masks2], axis=0)
)
def test_merge_with_empty(self) -> None:
img_h, img_w = 10, 10
empty_cm = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(img_h, img_w),
)
masks = np.zeros((2, img_h, img_w), dtype=bool)
cm = _make_cm(masks, (img_h, img_w))
merged = CompactMask.merge([empty_cm, cm])
assert len(merged) == 2
def test_merge_empty_list_raises(self) -> None:
with pytest.raises(ValueError, match="empty list"):
CompactMask.merge([])
def test_merge_mismatched_image_shape_raises(self) -> None:
cm1 = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(10, 10),
)
cm2 = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(20, 20),
)
with pytest.raises(ValueError, match="image shapes"):
CompactMask.merge([cm1, cm2])
class TestEquality:
"""Tests for CompactMask.__eq__.
Verifies element-wise equality between two CompactMask instances and
between a CompactMask and an equivalent dense (N, H, W) boolean array.
"""
def test_eq_identical(self) -> None:
masks = np.zeros((2, 10, 10), dtype=bool)
masks[0, 2:5, 2:5] = True
cm1 = _make_cm(masks, (10, 10))
cm2 = _make_cm(masks, (10, 10))
assert cm1 == cm2
def test_eq_different(self) -> None:
masks_a = np.zeros((2, 10, 10), dtype=bool)
masks_a[0, 2:5, 2:5] = True
masks_b = np.zeros((2, 10, 10), dtype=bool)
masks_b[1, 6:9, 6:9] = True
cm1 = _make_cm(masks_a, (10, 10))
cm2 = _make_cm(masks_b, (10, 10))
assert not (cm1 == cm2)
def test_eq_with_dense_array(self) -> None:
masks = np.zeros((1, 8, 8), dtype=bool)
masks[0, 1:4, 1:4] = True
cm = _make_cm(masks, (8, 8))
assert cm == masks
class TestEdgeCases:
"""Tests for boundary conditions and unusual inputs.
Covers: zero-area bounding box (x1 == x2), masks that reach the image
edge, xyxy values beyond image dimensions (clamped silently), empty
CompactMask (N=0), sum axis compatibility with area, and with_offset for
use by InferenceSlicer.
"""
def test_zero_area_mask_clipped_to_1x1(self) -> None:
"""An invalid bounding box should not crash from_dense."""
masks = np.zeros((1, 10, 10), dtype=bool)
xyxy = np.array([[6, 5, 5, 8]], dtype=np.float32)
with DoesNotRaise():
cm = CompactMask.from_dense(masks, xyxy, image_shape=(10, 10))
assert len(cm) == 1
def test_mask_at_image_boundary(self) -> None:
img_h, img_w = 20, 20
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 15:20, 15:20] = True
xyxy = np.array([[15, 15, 19, 19]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
np.testing.assert_array_equal(cm.to_dense(), masks)
def test_xyxy_beyond_image_clipped(self) -> None:
"""xyxy values beyond the image boundary should be clipped silently."""
img_h, img_w = 10, 10
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 5:10, 5:10] = True
xyxy = np.array([[5, 5, 999, 999]], dtype=np.float32)
with DoesNotRaise():
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
np.testing.assert_array_equal(cm.to_dense(), masks)
def test_empty_compact_mask_to_dense(self) -> None:
cm = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(50, 60),
)
dense = cm.to_dense()
assert dense.shape == (0, 50, 60)
assert dense.dtype == bool
def test_sum_axis_1_2_equals_area(self) -> None:
rng = np.random.default_rng(11)
masks = rng.integers(0, 2, size=(4, 15, 15)).astype(bool)
cm = _make_cm(masks, (15, 15))
np.testing.assert_array_equal(cm.sum(axis=(1, 2)), cm.area)
def test_with_offset(self) -> None:
img_h, img_w = 20, 20
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 5:10, 5:10] = True
xyxy = np.array([[5, 5, 9, 9]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
cm2 = cm.with_offset(100, 200, new_image_shape=(400, 400))
assert cm2.offsets[0].tolist() == [105, 205]
assert cm2._image_shape == (400, 400)
np.testing.assert_array_equal(cm2.crop(0), cm.crop(0))
def test_with_offset_clips_partial_overlap_like_move_masks(self) -> None:
"""with_offset must clip partial out-of-frame translations like move_masks."""
img_h, img_w = 10, 10
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 2:6, 3:8] = True
xyxy = np.array([[3, 2, 7, 5]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
dx, dy = -4, 3
cm_shifted = cm.with_offset(dx=dx, dy=dy, new_image_shape=(img_h, img_w))
expected = move_masks(
masks=masks,
offset=np.array([dx, dy], dtype=np.int32),
resolution_wh=(img_w, img_h),
)
np.testing.assert_array_equal(cm_shifted.to_dense(), expected)
def test_with_offset_clips_full_outside_like_move_masks(self) -> None:
"""Masks shifted fully outside should remain valid and decode to all-False."""
img_h, img_w = 10, 10
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 2:6, 2:6] = True
xyxy = np.array([[2, 2, 5, 5]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
dx, dy = 100, 100
cm_shifted = cm.with_offset(dx=dx, dy=dy, new_image_shape=(img_h, img_w))
expected = move_masks(
masks=masks,
offset=np.array([dx, dy], dtype=np.int32),
resolution_wh=(img_w, img_h),
)
np.testing.assert_array_equal(cm_shifted.to_dense(), expected)
def test_repack_tightens_loose_bbox(self) -> None:
"""repack() shrinks the crop to the minimal True-pixel rectangle."""
img_h, img_w = 20, 20
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 5:10, 6:12] = True # True block at (5,6)-(9,11)
# Deliberately loose bbox covers full image.
xyxy = np.array([[0, 0, img_w - 1, img_h - 1]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
# Before repack: crop is the full 20x20 image.
assert cm._crop_shapes[0].tolist() == [20, 20]
repacked = cm.repack()
# After repack: crop is exactly the True block.
assert repacked.offsets[0].tolist() == [6, 5] # (x1, y1)
assert repacked._crop_shapes[0].tolist() == [5, 6] # (h, w)
# Pixel content must be identical to the original.
np.testing.assert_array_equal(repacked.to_dense(), masks)
def test_repack_preserves_all_false_mask(self) -> None:
"""repack() normalises an all-False mask to a 1x1 crop."""
img_h, img_w = 10, 10
masks = np.zeros((2, img_h, img_w), dtype=bool)
masks[1, 3:6, 3:6] = True # only mask 1 is non-empty
xyxy = np.array([[0, 0, 9, 9], [0, 0, 9, 9]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
repacked = cm.repack()
assert repacked._crop_shapes[0].tolist() == [1, 1] # normalised
assert repacked._crop_shapes[1].tolist() == [3, 3] # tight True block
np.testing.assert_array_equal(repacked.to_dense(), masks)
def test_repack_empty_collection(self) -> None:
"""repack() on an empty CompactMask returns another empty CompactMask."""
cm = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(10, 10),
)
repacked = cm.repack()
assert len(repacked) == 0
assert repacked._image_shape == (10, 10)
def test_repack_already_tight(self) -> None:
"""repack() is a no-op when bboxes are already tight."""
img_h, img_w = 15, 15
masks = np.zeros((1, img_h, img_w), dtype=bool)
masks[0, 4:9, 3:8] = True
# Tight bbox.
xyxy = np.array([[3, 4, 7, 8]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
repacked = cm.repack()
np.testing.assert_array_equal(repacked.offsets, cm.offsets)
np.testing.assert_array_equal(repacked._crop_shapes, cm._crop_shapes)
np.testing.assert_array_equal(repacked.to_dense(), masks)
class TestCalculateMasksCentroidsCompact:
"""Verify calculate_masks_centroids gives identical results for CompactMask.
The function has a dedicated CompactMask branch that computes centroids
per-crop. Results must match the dense path to within integer rounding.
"""
def test_centroids_compact_matches_dense(self) -> None:
"""Centroid coordinates must be numerically identical for dense and compact."""
rng = np.random.default_rng(42)
img_h, img_w = 30, 30
masks = rng.integers(0, 2, size=(5, img_h, img_w)).astype(bool)
# Ensure each mask has at least one True pixel.
for mask_idx in range(5):
masks[mask_idx, mask_idx * 5, mask_idx * 5] = True
cm = _make_cm(masks, (img_h, img_w))
centroids_dense = calculate_masks_centroids(masks)
centroids_compact = calculate_masks_centroids(cm)
np.testing.assert_array_equal(centroids_compact, centroids_dense)
def test_centroids_empty_mask(self) -> None:
"""All-zero masks should return centroid (0, 0) — same as dense."""
img_h, img_w = 10, 10
masks = np.zeros((3, img_h, img_w), dtype=bool)
cm = _make_cm(masks, (img_h, img_w))
centroids_dense = calculate_masks_centroids(masks)
centroids_compact = calculate_masks_centroids(cm)
np.testing.assert_array_equal(centroids_compact, centroids_dense)
def test_centroids_empty_mask_with_tight_bbox(self) -> None:
"""All-zero tight crops must still return centroid (0, 0)."""
img_h, img_w = 10, 10
masks = np.zeros((1, img_h, img_w), dtype=bool)
xyxy = np.array([[3, 4, 7, 8]], dtype=np.float32)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
centroids_dense = calculate_masks_centroids(masks)
centroids_compact = calculate_masks_centroids(cm)
np.testing.assert_array_equal(centroids_compact, centroids_dense)
def test_centroids_zero_masks_returns_empty(self) -> None:
"""Empty CompactMask (0 objects) must return shape (0, 2)."""
empty_cm = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(10, 10),
)
result = calculate_masks_centroids(empty_cm)
assert result.shape == (0, 2)
class TestContainsHolesCompact:
"""Verify contains_holes result is unchanged after CompactMask roundtrip.
contains_holes works on a 2D boolean mask. Encoding then decoding via
CompactMask must preserve pixel topology so that the function returns
the same result as on the original array.
"""
@pytest.mark.parametrize(
("mask_2d", "expected"),
[
# simple foreground blob — no holes
(
np.array(
[[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 0]],
dtype=bool,
),
False,
),
# ring shape — has one hole
(
np.array(
[[1, 1, 1, 0], [1, 0, 1, 0], [1, 1, 1, 0], [0, 0, 0, 0]],
dtype=bool,
),
True,
),
# all-False — no holes
(np.zeros((6, 6), dtype=bool), False),
# all-True — no holes
(np.ones((6, 6), dtype=bool), False),
],
)
def test_contains_holes_compact_roundtrip(
self, mask_2d: np.ndarray, expected: bool
) -> None:
"""contains_holes must agree after CompactMask encode→decode."""
img_h, img_w = mask_2d.shape
masks = mask_2d[np.newaxis] # (1, H, W)
cm = _make_cm(masks, (img_h, img_w))
decoded = cm.to_dense()[0]
assert contains_holes(decoded) == expected
assert contains_holes(decoded) == contains_holes(mask_2d)
class TestContainsMultipleSegmentsCompact:
"""Verify contains_multiple_segments result survives CompactMask roundtrip.
Encoding and decoding must preserve connected-component topology so
that the multi-segment predicate returns the same value.
"""
@pytest.mark.parametrize(
("mask_2d", "connectivity", "expected"),
[
# single contiguous blob — not multi-segment
(
np.array(
[[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 0]],
dtype=bool,
),
4,
False,
),
# two separate blobs — multi-segment
(
np.array(
[[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]],
dtype=bool,
),
4,
True,
),
# diagonal touch — single segment under 8-connectivity
(
np.array(
[[1, 1, 0, 0], [1, 1, 0, 1], [1, 0, 1, 1], [0, 0, 1, 1]],
dtype=bool,
),
8,
False,
),
# all-False — not multi-segment
(np.zeros((6, 6), dtype=bool), 4, False),
],
)
def test_contains_multiple_segments_compact_roundtrip(
self, mask_2d: np.ndarray, connectivity: int, expected: bool
) -> None:
"""contains_multiple_segments must agree after CompactMask encode→decode."""
img_h, img_w = mask_2d.shape
masks = mask_2d[np.newaxis] # (1, H, W)
cm = _make_cm(masks, (img_h, img_w))
decoded = cm.to_dense()[0]
result = contains_multiple_segments(decoded, connectivity=connectivity)
assert result == expected
assert result == contains_multiple_segments(mask_2d, connectivity=connectivity)
# ---------------------------------------------------------------------------
# Random scenario helpers
# ---------------------------------------------------------------------------
# Varying (N, image_h, image_w) combinations for random tests.
_RANDOM_CONFIGS = [
(1, 50, 50),
(5, 50, 50),
(5, 200, 300),
(20, 100, 150),
(20, 200, 300),
(50, 50, 50),
(5, 1080, 1920),
(1, 1080, 1920),
(20, 480, 640),
(50, 100, 100),
]
def _random_masks_and_xyxy(
rng: np.random.Generator,
num_masks: int,
img_h: int,
img_w: int,
fill_prob: float = 0.3,
) -> tuple[np.ndarray, np.ndarray]:
"""Generate *num_masks* random boolean masks with matching tight xyxy boxes.
Each mask is built by filling a random sub-rectangle with Bernoulli noise at
``fill_prob``, then computing tight bounding boxes via ``mask_to_xyxy``.
This guarantees every mask has at least one True pixel (for non-degenerate
bounding boxes).
"""
masks = np.zeros((num_masks, img_h, img_w), dtype=bool)
for mask_idx in range(num_masks):
y1 = rng.integers(0, img_h)
y2 = rng.integers(y1, img_h)
x1 = rng.integers(0, img_w)
x2 = rng.integers(x1, img_w)
region = rng.random((y2 - y1 + 1, x2 - x1 + 1)) < fill_prob
# Ensure at least one True pixel.
if not region.any():
region[0, 0] = True
masks[mask_idx, y1 : y2 + 1, x1 : x2 + 1] = region
xyxy = mask_to_xyxy(masks).astype(np.float32)
return masks, xyxy
class TestCompactMaskRoundtripRandom:
"""from_dense -> to_dense pixel equality across 10 random seeds.
Uses tight bounding boxes so the round-trip must be lossless (all True
pixels lie strictly within the crop).
"""
@pytest.mark.parametrize("seed", list(range(10)))
def test_parity_seed(self, seed: int) -> None:
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
np.testing.assert_array_equal(
cm.to_dense(),
masks,
err_msg=(
f"Round-trip failed for seed={seed}, "
f"N={num_masks}, shape=({img_h},{img_w})"
),
)
@pytest.mark.parametrize("seed", list(range(10)))
def test_shape_and_len(self, seed: int) -> None:
"""len() and .shape must agree with the dense array."""
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
assert len(cm) == num_masks
assert cm.shape == (num_masks, img_h, img_w)
@pytest.mark.parametrize("seed", list(range(10)))
def test_individual_mask_access(self, seed: int) -> None:
"""cm[i] must equal masks[i] for every index."""
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
for mask_idx in range(num_masks):
np.testing.assert_array_equal(
cm[mask_idx],
masks[mask_idx],
err_msg=f"cm[{mask_idx}] mismatch for seed={seed}",
)
class TestCompactMaskAreaRandom:
"""area from CompactMask equals dense .sum(axis=(1,2)) across 10 seeds."""
@pytest.mark.parametrize("seed", list(range(10)))
def test_parity_seed(self, seed: int) -> None:
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
expected_area = masks.sum(axis=(1, 2))
np.testing.assert_array_equal(
cm.area,
expected_area,
err_msg=(
f"Area mismatch for seed={seed}, N={num_masks}, shape=({img_h},{img_w})"
),
)
@pytest.mark.parametrize("seed", list(range(10)))
def test_sum_axis_matches_area(self, seed: int) -> None:
"""cm.sum(axis=(1,2)) must equal cm.area (the fast path)."""
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
np.testing.assert_array_equal(cm.sum(axis=(1, 2)), cm.area)
class TestCompactMaskFilterRandom:
"""Boolean filter on CompactMask matches dense fancy indexing across 10 seeds."""
@pytest.mark.parametrize("seed", list(range(10)))
def test_parity_seed(self, seed: int) -> None:
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
selector = rng.random(num_masks) > 0.5
# Guarantee at least one True in the selector so we test non-empty subsets.
if not selector.any():
selector[0] = True
subset_cm = cm[selector]
subset_dense = masks[selector]
assert isinstance(subset_cm, CompactMask)
assert len(subset_cm) == int(selector.sum())
np.testing.assert_array_equal(
subset_cm.to_dense(),
subset_dense,
err_msg=f"Boolean filter mismatch for seed={seed}",
)
@pytest.mark.parametrize("seed", list(range(10)))
def test_list_index(self, seed: int) -> None:
"""Integer list indexing must match dense fancy indexing."""
rng = np.random.default_rng(seed)
num_masks, img_h, img_w = _RANDOM_CONFIGS[seed]
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
num_selected = min(num_masks, max(1, rng.integers(1, num_masks + 1)))
indices = sorted(
rng.choice(num_masks, size=num_selected, replace=False).tolist()
)
subset_cm = cm[indices]
subset_dense = masks[indices]
np.testing.assert_array_equal(
subset_cm.to_dense(),
subset_dense,
err_msg=f"List index mismatch for seed={seed}, indices={indices}",
)
class TestCompactMaskWithOffsetRandom:
"""with_offset roundtrip matches move_masks across 10 random seeds."""
@pytest.mark.parametrize("seed", list(range(10)))
def test_parity_seed(self, seed: int) -> None:
rng = np.random.default_rng(seed)
# Use smaller images to keep move_masks fast.
num_masks = rng.integers(1, 10)
img_h, img_w = int(rng.integers(30, 80)), int(rng.integers(30, 80))
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
# Random offset that may push some masks partially or fully off-frame.
dx = int(rng.integers(-img_w, img_w))
dy = int(rng.integers(-img_h, img_h))
cm_shifted = cm.with_offset(dx=dx, dy=dy, new_image_shape=(img_h, img_w))
expected = move_masks(
masks=masks,
offset=np.array([dx, dy], dtype=np.int32),
resolution_wh=(img_w, img_h),
)
np.testing.assert_array_equal(
cm_shifted.to_dense(),
expected,
err_msg=(
f"with_offset mismatch for seed={seed}, "
f"dx={dx}, dy={dy}, shape=({img_h},{img_w})"
),
)
@pytest.mark.parametrize("seed", list(range(10)))
def test_offset_into_larger_canvas(self, seed: int) -> None:
"""Offset into a larger destination image must preserve pixels."""
rng = np.random.default_rng(seed + 100)
num_masks = rng.integers(1, 8)
img_h, img_w = int(rng.integers(20, 50)), int(rng.integers(20, 50))
masks, xyxy = _random_masks_and_xyxy(rng, num_masks, img_h, img_w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(img_h, img_w))
new_h, new_w = img_h * 2, img_w * 2
dx = int(rng.integers(0, img_w))
dy = int(rng.integers(0, img_h))
cm_shifted = cm.with_offset(dx=dx, dy=dy, new_image_shape=(new_h, new_w))
dense_shifted = cm_shifted.to_dense()
assert dense_shifted.shape == (num_masks, new_h, new_w)
# Manually place each original mask into the larger canvas.
expected = np.zeros((num_masks, new_h, new_w), dtype=bool)
for mask_idx in range(num_masks):
expected[mask_idx, dy : dy + img_h, dx : dx + img_w] |= masks[mask_idx]
np.testing.assert_array_equal(
dense_shifted,
expected,
err_msg=f"Larger canvas offset mismatch for seed={seed}",
)
@@ -0,0 +1,274 @@
"""Integration tests: CompactMask <-> Detections, annotators, merge."""
from __future__ import annotations
from contextlib import ExitStack as DoesNotRaise
import numpy as np
import pytest
import supervision as sv
from supervision.detection.compact_mask import CompactMask
from supervision.detection.core import Detections
def _full_xyxy(n: int, h: int, w: int) -> np.ndarray:
"""N boxes covering the whole image (ensures crop == full mask)."""
return np.tile(np.array([0, 0, w, h], dtype=np.float32), (n, 1))
def _make_compact_detections(
n: int, h: int = 40, w: int = 40
) -> tuple[Detections, np.ndarray]:
"""Detections with a CompactMask backed by full-image bounding boxes.
Using full-image xyxy means all True pixels are within the crop region,
so from_dense -> to_dense is lossless.
"""
rng = np.random.default_rng(42)
masks = rng.integers(0, 2, size=(n, h, w)).astype(bool)
xyxy = _full_xyxy(n, h, w)
cm = CompactMask.from_dense(masks, xyxy, image_shape=(h, w))
det = Detections(
xyxy=xyxy,
mask=cm,
confidence=np.ones(n, dtype=np.float32) * 0.9,
class_id=np.arange(n),
)
return det, masks
class TestConstruction:
"""Tests for building Detections with a CompactMask.
Verifies that a CompactMask is accepted as a valid mask argument and that
the validator raises ValueError when the mask length does not match the
number of bounding boxes.
"""
def test_detections_construction_with_compact_mask(self) -> None:
with DoesNotRaise():
det, _ = _make_compact_detections(3)
assert isinstance(det.mask, CompactMask)
assert len(det) == 3
def test_detections_compact_mask_validation_mismatch(self) -> None:
n, h, w = 3, 20, 20
xyxy = _full_xyxy(n, h, w)
masks_wrong_n = np.zeros((n + 1, h, w), dtype=bool)
cm = CompactMask.from_dense(masks_wrong_n, _full_xyxy(n + 1, h, w), (h, w))
with pytest.raises(ValueError, match="mask must contain"):
Detections(xyxy=xyxy, mask=cm)
class TestFiltering:
"""Tests for Detections.__getitem__ with a CompactMask.
Verifies that integer, slice, and boolean-array indexing all preserve the
CompactMask type and return the correct subset of masks.
"""
def test_int_wraps_to_compact_mask(self) -> None:
det, _ = _make_compact_detections(3)
# Detections converts int to [int] internally -> subset has 1 element
subset = det[1]
assert isinstance(subset.mask, CompactMask)
assert len(subset) == 1
def test_slice_preserves_compact_mask(self) -> None:
det, masks = _make_compact_detections(4)
subset = det[1:3]
assert isinstance(subset.mask, CompactMask)
assert len(subset) == 2
np.testing.assert_array_equal(subset.mask.to_dense(), masks[1:3])
def test_bool_array_preserves_compact_mask(self) -> None:
det, masks = _make_compact_detections(4)
selector = np.array([True, False, True, False])
subset = det[selector]
assert isinstance(subset.mask, CompactMask)
assert len(subset) == 2
np.testing.assert_array_equal(subset.mask.to_dense(), masks[[0, 2]])
class TestIteration:
"""Tests for iterating over Detections with a CompactMask.
Verifies that each iteration step yields a 2-D boolean (H, W) array
identical to the corresponding dense mask, so downstream code that
iterates over detections needs no changes.
"""
def test_iter_yields_2d_dense(self) -> None:
h, w = 20, 20
det, masks = _make_compact_detections(3, h, w)
for i, (_, mask_2d, *_) in enumerate(det):
assert mask_2d is not None
assert isinstance(mask_2d, np.ndarray)
assert mask_2d.shape == (h, w)
assert mask_2d.dtype == bool
np.testing.assert_array_equal(mask_2d, masks[i])
class TestEquality:
"""Tests for Detections.__eq__ mixing CompactMask and dense arrays.
Verifies that a Detections object backed by a CompactMask compares equal
to an otherwise identical Detections object backed by a dense ndarray.
"""
def test_compact_vs_dense(self) -> None:
h, w = 20, 20
det_compact, masks = _make_compact_detections(2, h, w)
xyxy = det_compact.xyxy.copy()
det_dense = Detections(
xyxy=xyxy,
mask=masks,
confidence=np.ones(2, dtype=np.float32) * 0.9,
class_id=np.arange(2),
)
assert det_compact == det_dense
class TestArea:
"""Tests for the Detections.area property with a CompactMask.
Verifies that the fast CompactMask path in Detections.area returns the
same per-detection pixel counts as summing the equivalent dense array.
"""
def test_compact_matches_dense(self) -> None:
det_compact, masks = _make_compact_detections(3)
expected_area = np.array([m.sum() for m in masks])
np.testing.assert_array_equal(det_compact.area, expected_area)
class TestMerge:
"""Tests for merging Detections objects that contain CompactMask instances.
Covers three scenarios:
- All-compact merge: result is a CompactMask.
- Mixed compact + dense: result falls back to a dense ndarray.
- Inner pair merge (merge_inner_detection_object_pair): used during NMS-like
operations, each input must contain exactly one detection.
"""
def test_all_compact(self) -> None:
h, w = 30, 30
det1, masks1 = _make_compact_detections(2, h, w)
rng = np.random.default_rng(7)
masks2 = rng.integers(0, 2, size=(3, h, w)).astype(bool)
xyxy2 = _full_xyxy(3, h, w)
cm2 = CompactMask.from_dense(masks2, xyxy2, (h, w))
det2 = Detections(
xyxy=xyxy2,
mask=cm2,
confidence=np.ones(3, dtype=np.float32) * 0.8,
class_id=np.arange(3),
)
merged = Detections.merge([det1, det2])
assert isinstance(merged.mask, CompactMask)
assert len(merged) == 5
expected = np.concatenate([masks1, masks2], axis=0)
np.testing.assert_array_equal(merged.mask.to_dense(), expected)
def test_mixed_compact_and_dense(self) -> None:
"""Merging a CompactMask with a dense ndarray falls back to dense."""
h, w = 20, 20
det_compact, _ = _make_compact_detections(2, h, w)
masks_dense = np.zeros((1, h, w), dtype=bool)
xyxy_dense = _full_xyxy(1, h, w)
det_dense = Detections(
xyxy=xyxy_dense,
mask=masks_dense,
confidence=np.array([0.5], dtype=np.float32),
class_id=np.array([0]),
)
merged = Detections.merge([det_compact, det_dense])
assert isinstance(merged.mask, np.ndarray)
assert merged.mask.shape == (3, h, w)
def test_inner_pair_with_compact(self) -> None:
from supervision.detection.core import merge_inner_detection_object_pair
h, w = 20, 20
masks_a = np.zeros((1, h, w), dtype=bool)
masks_a[0, 0:5, 0:5] = True
xyxy_a = _full_xyxy(1, h, w)
cm_a = CompactMask.from_dense(masks_a, xyxy_a, (h, w))
det_a = Detections(
xyxy=xyxy_a,
mask=cm_a,
confidence=np.array([0.9], dtype=np.float32),
class_id=np.array([1]),
)
masks_b = np.zeros((1, h, w), dtype=bool)
masks_b[0, 5:10, 5:10] = True
xyxy_b = _full_xyxy(1, h, w)
cm_b = CompactMask.from_dense(masks_b, xyxy_b, (h, w))
det_b = Detections(
xyxy=xyxy_b,
mask=cm_b,
confidence=np.array([0.7], dtype=np.float32),
class_id=np.array([1]),
)
with DoesNotRaise():
result = merge_inner_detection_object_pair(det_a, det_b)
assert len(result) == 1
class TestAnnotators:
"""Tests for annotators that consume CompactMask via Detections.
Verifies that MaskAnnotator and PolygonAnnotator produce pixel-identical
output when given Detections backed by a CompactMask versus the equivalent
dense ndarray, confirming that the annotators are transparent to the mask
representation.
"""
def test_mask_annotator(self) -> None:
h, w = 40, 40
det_compact, masks = _make_compact_detections(2, h, w)
det_dense = Detections(
xyxy=det_compact.xyxy.copy(),
mask=masks,
confidence=det_compact.confidence.copy(),
class_id=det_compact.class_id.copy(),
)
image = np.zeros((h, w, 3), dtype=np.uint8)
annotator = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
annotated_compact = annotator.annotate(image.copy(), det_compact)
annotated_dense = annotator.annotate(image.copy(), det_dense)
np.testing.assert_array_equal(
annotated_compact,
annotated_dense,
err_msg="MaskAnnotator output differs between CompactMask and dense mask",
)
def test_polygon_annotator(self) -> None:
h, w = 40, 40
# Use solid rectangular masks for stable polygon results.
masks = np.zeros((2, h, w), dtype=bool)
masks[0, 5:15, 5:15] = True
masks[1, 20:30, 20:30] = True
xyxy = _full_xyxy(2, h, w)
cm = CompactMask.from_dense(masks, xyxy, (h, w))
det_compact = Detections(xyxy=xyxy, mask=cm, class_id=np.array([0, 1]))
det_dense = Detections(xyxy=xyxy, mask=masks, class_id=np.array([0, 1]))
image = np.zeros((h, w, 3), dtype=np.uint8)
annotator = sv.PolygonAnnotator(color_lookup=sv.ColorLookup.INDEX)
annotated_compact = annotator.annotate(image.copy(), det_compact)
annotated_dense = annotator.annotate(image.copy(), det_dense)
np.testing.assert_array_equal(annotated_compact, annotated_dense)
+500
View File
@@ -0,0 +1,500 @@
"""Correctness and integration tests for CompactMask IoU and NMS.
These tests verify that:
- compact_mask_iou_batch gives numerically identical results to the
dense mask_iou_batch (raster IoU) for all overlap patterns.
- mask_iou_batch dispatches correctly when given CompactMask inputs.
- mask_non_max_suppression and mask_non_max_merge work with CompactMask
and produce the same keep-set as when given equivalent dense arrays.
"""
from __future__ import annotations
import numpy as np
import pytest
from supervision.detection.compact_mask import CompactMask
from supervision.detection.utils.iou_and_nms import (
OverlapMetric,
compact_mask_iou_batch,
mask_iou_batch,
mask_non_max_merge,
mask_non_max_suppression,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _cm_from_masks(masks: np.ndarray, image_shape: tuple[int, int]) -> CompactMask:
"""Build a CompactMask using full-image bounding boxes (lossless)."""
num_masks = len(masks)
img_h, img_w = image_shape
xyxy = np.tile(
np.array([0, 0, img_w - 1, img_h - 1], dtype=np.float32), (num_masks, 1)
)
return CompactMask.from_dense(masks, xyxy, image_shape=image_shape)
def _cm_tight(masks: np.ndarray, image_shape: tuple[int, int]) -> CompactMask:
"""Build a CompactMask using tight per-mask bounding boxes."""
from supervision.detection.utils.converters import mask_to_xyxy
xyxy = mask_to_xyxy(masks).astype(np.float32)
return CompactMask.from_dense(masks, xyxy, image_shape=image_shape)
def _dense_iou(
masks_a: np.ndarray,
masks_b: np.ndarray,
metric: OverlapMetric = OverlapMetric.IOU,
) -> np.ndarray:
"""Reference pairwise IoU using the existing dense implementation."""
return mask_iou_batch(masks_a, masks_b, overlap_metric=metric)
class TestCompactMaskIouBatch:
"""Verify that compact_mask_iou_batch matches dense raster IoU exactly.
Every test builds a pair of CompactMask collections from known boolean
arrays, runs compact_mask_iou_batch, and compares the result to the dense
reference computed by mask_iou_batch on the raw numpy arrays.
"""
def test_no_overlap_gives_zero(self) -> None:
"""Non-overlapping masks should always produce IoU = 0."""
img_h, img_w = 20, 20
masks_a = np.zeros((1, img_h, img_w), dtype=bool)
masks_a[0, 0:5, 0:5] = True # top-left
masks_b = np.zeros((1, img_h, img_w), dtype=bool)
masks_b[0, 10:15, 10:15] = True # bottom-right
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
result = compact_mask_iou_batch(cm_a, cm_b)
assert result.shape == (1, 1)
assert result[0, 0] == pytest.approx(0.0)
def test_identical_masks_give_one(self) -> None:
"""IoU of a mask with itself must be 1.0."""
img_h, img_w = 20, 20
masks = np.zeros((2, img_h, img_w), dtype=bool)
masks[0, 2:8, 2:8] = True
masks[1, 10:18, 10:18] = True
cm = _cm_from_masks(masks, (img_h, img_w))
result = compact_mask_iou_batch(cm, cm)
assert result.shape == (2, 2)
np.testing.assert_allclose(np.diag(result), [1.0, 1.0], atol=1e-9)
def test_matches_dense_random(self) -> None:
"""compact_mask_iou_batch must be numerically identical to dense IoU."""
rng = np.random.default_rng(0)
img_h, img_w = 30, 30
masks_a = rng.integers(0, 2, size=(5, img_h, img_w)).astype(bool)
masks_b = rng.integers(0, 2, size=(4, img_h, img_w)).astype(bool)
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
compact_result = compact_mask_iou_batch(cm_a, cm_b)
dense_result = _dense_iou(masks_a, masks_b)
assert compact_result.shape == (5, 4)
np.testing.assert_allclose(compact_result, dense_result, atol=1e-9)
def test_matches_dense_with_tight_bboxes(self) -> None:
"""Using tight bounding boxes (mask_to_xyxy) must still be accurate."""
rng = np.random.default_rng(1)
img_h, img_w = 40, 40
masks_a = rng.integers(0, 2, size=(4, img_h, img_w)).astype(bool)
masks_b = rng.integers(0, 2, size=(3, img_h, img_w)).astype(bool)
cm_a = _cm_tight(masks_a, (img_h, img_w))
cm_b = _cm_tight(masks_b, (img_h, img_w))
compact_result = compact_mask_iou_batch(cm_a, cm_b)
dense_result = _dense_iou(masks_a, masks_b)
np.testing.assert_allclose(compact_result, dense_result, atol=1e-9)
def test_partial_overlap(self) -> None:
"""Partially overlapping masks: IoU should match the analytic value."""
img_h, img_w = 10, 10
# Mask A: columns 0-4 (5 wide), Mask B: columns 3-7 (5 wide).
# Overlap: columns 3-4 (2 wide) x full height (10 rows) = 20 px.
masks_a = np.zeros((1, img_h, img_w), dtype=bool)
masks_a[0, :, 0:5] = True # area = 50
masks_b = np.zeros((1, img_h, img_w), dtype=bool)
masks_b[0, :, 3:8] = True # area = 50
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
result = compact_mask_iou_batch(cm_a, cm_b)
# inter=20, union=50+50-20=80 → IoU=0.25
assert result[0, 0] == pytest.approx(0.25, abs=1e-9)
np.testing.assert_allclose(result, _dense_iou(masks_a, masks_b), atol=1e-9)
def test_ios_metric(self) -> None:
"""IOS = intersection / min(area_a, area_b) must match dense reference."""
rng = np.random.default_rng(2)
img_h, img_w = 25, 25
masks_a = rng.integers(0, 2, size=(3, img_h, img_w)).astype(bool)
masks_b = rng.integers(0, 2, size=(3, img_h, img_w)).astype(bool)
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
compact_result = compact_mask_iou_batch(cm_a, cm_b, OverlapMetric.IOS)
dense_result = _dense_iou(masks_a, masks_b, OverlapMetric.IOS)
np.testing.assert_allclose(compact_result, dense_result, atol=1e-9)
def test_all_false_masks(self) -> None:
"""Zero-area masks should produce IoU = 0, not NaN."""
img_h, img_w = 10, 10
masks_a = np.zeros((2, img_h, img_w), dtype=bool)
masks_b = np.zeros((2, img_h, img_w), dtype=bool)
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
result = compact_mask_iou_batch(cm_a, cm_b)
assert not np.any(np.isnan(result))
np.testing.assert_array_equal(result, 0.0)
def test_empty_inputs(self) -> None:
"""Empty CompactMask collections should return a zero-shaped matrix."""
img_h, img_w = 10, 10
empty = CompactMask(
[],
np.empty((0, 2), dtype=np.int32),
np.empty((0, 2), dtype=np.int32),
(img_h, img_w),
)
masks = np.zeros((3, img_h, img_w), dtype=bool)
cm = _cm_from_masks(masks, (img_h, img_w))
result_a = compact_mask_iou_batch(empty, cm)
assert result_a.shape == (0, 3)
result_b = compact_mask_iou_batch(cm, empty)
assert result_b.shape == (3, 0)
def test_n_by_n_pairwise(self) -> None:
"""N x N pairwise IoU: diagonal must be 1.0 for non-zero-area masks."""
img_h, img_w = 50, 50
rng = np.random.default_rng(3)
masks = rng.integers(0, 2, size=(8, img_h, img_w)).astype(bool)
# Ensure no all-false mask (diagonal would be undefined).
for mask_idx in range(8):
masks[mask_idx, mask_idx * 5, mask_idx * 5] = True
cm = _cm_from_masks(masks, (img_h, img_w))
result = compact_mask_iou_batch(cm, cm)
assert result.shape == (8, 8)
np.testing.assert_allclose(np.diag(result), 1.0, atol=1e-9)
np.testing.assert_allclose(result, _dense_iou(masks, masks), atol=1e-9)
class TestMaskIouBatchDispatch:
"""Verify mask_iou_batch dispatches correctly for CompactMask inputs.
When both arguments are CompactMask, the function must route to the
efficient RLE implementation and produce identical results to the dense
path. When one argument is dense and the other is CompactMask, the
CompactMask must be materialised transparently before computation.
"""
def test_both_compact_dispatches_to_rle(self) -> None:
img_h, img_w = 20, 20
rng = np.random.default_rng(10)
masks_a = rng.integers(0, 2, size=(3, img_h, img_w)).astype(bool)
masks_b = rng.integers(0, 2, size=(2, img_h, img_w)).astype(bool)
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
result_compact = mask_iou_batch(cm_a, cm_b)
result_dense = mask_iou_batch(masks_a, masks_b)
np.testing.assert_allclose(result_compact, result_dense, atol=1e-9)
def test_mixed_compact_and_dense(self) -> None:
"""One CompactMask + one dense array must still work correctly."""
img_h, img_w = 20, 20
rng = np.random.default_rng(11)
masks_a = rng.integers(0, 2, size=(3, img_h, img_w)).astype(bool)
masks_b = rng.integers(0, 2, size=(2, img_h, img_w)).astype(bool)
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
result = mask_iou_batch(cm_a, masks_b)
expected = mask_iou_batch(masks_a, masks_b)
np.testing.assert_allclose(result, expected, atol=1e-9)
class TestNmsWithCompactMask:
"""Verify mask NMS produces identical keep-sets for CompactMask and dense inputs.
Both paths now use exact full-resolution IoU — no resize approximation.
Tests use images larger than 640 px to ensure the old resize-to-640 path
would have introduced lossy approximation (catching the regression).
"""
def test_nms_compact_matches_dense(self) -> None:
"""NMS keep-set is identical for CompactMask and the equivalent dense array."""
# Use > 640 px so the old resize-to-640 path would have been lossy.
img_h, img_w = 720, 720
masks = np.zeros((3, img_h, img_w), dtype=bool)
masks[0, 0:360, 0:360] = True # top-left
masks[1, 0:324, 0:324] = True # heavily overlaps mask 0
masks[2, 360:720, 360:720] = True # bottom-right, no overlap
scores = np.array([0.9, 0.8, 0.7])
predictions = np.column_stack(
[np.zeros((3, 4)), scores] # dummy xyxy, real scores
)
cm = _cm_from_masks(masks, (img_h, img_w))
keep_dense = mask_non_max_suppression(predictions, masks, iou_threshold=0.3)
keep_compact = mask_non_max_suppression(predictions, cm, iou_threshold=0.3)
np.testing.assert_array_equal(keep_compact, keep_dense)
def test_nms_compact_matches_dense_borderline(self) -> None:
"""Borderline IoU pair (≈ threshold) must agree — catches the resize bug.
With resize-to-640, sub-pixel rounding on a pair whose true IoU is very
close to the threshold flips the keep/suppress decision. Both paths now
compute exact pixel-level IoU so results are identical.
"""
img_h, img_w = 1080, 1920
masks = np.zeros((2, img_h, img_w), dtype=bool)
# Mask 0: 200x200 square; mask 1: shifted 141 px → true IoU ≈ 0.50.
masks[0, 100:300, 100:300] = True
masks[1, 241:441, 241:441] = True
scores = np.array([0.9, 0.8])
predictions = np.column_stack([np.zeros((2, 4)), scores])
cm = _cm_from_masks(masks, (img_h, img_w))
keep_dense = mask_non_max_suppression(predictions, masks, iou_threshold=0.5)
keep_compact = mask_non_max_suppression(predictions, cm, iou_threshold=0.5)
np.testing.assert_array_equal(keep_compact, keep_dense)
def test_nms_compact_no_suppression(self) -> None:
"""Non-overlapping masks: all should be kept."""
img_h, img_w = 20, 20
masks = np.zeros((3, img_h, img_w), dtype=bool)
masks[0, 0:5, 0:5] = True
masks[1, 7:12, 7:12] = True
masks[2, 14:19, 14:19] = True
scores = np.array([0.9, 0.8, 0.7])
predictions = np.column_stack([np.zeros((3, 4)), scores])
cm = _cm_from_masks(masks, (img_h, img_w))
keep = mask_non_max_suppression(predictions, cm, iou_threshold=0.5)
assert keep.all(), "All non-overlapping masks should be kept"
def test_nms_compact_full_suppression(self) -> None:
"""Identical masks: only the highest-confidence one should survive."""
img_h, img_w = 20, 20
mask = np.zeros((1, img_h, img_w), dtype=bool)
mask[0, 5:15, 5:15] = True
masks = np.repeat(mask, 3, axis=0)
scores = np.array([0.9, 0.8, 0.7])
predictions = np.column_stack([np.zeros((3, 4)), scores])
cm = _cm_from_masks(masks, (img_h, img_w))
keep = mask_non_max_suppression(predictions, cm, iou_threshold=0.5)
assert keep.sum() == 1
assert keep[0], "Highest-confidence mask should survive"
class TestNmmWithCompactMask:
"""Verify mask_non_max_merge produces the same groups for CompactMask and dense.
NMM materialises CompactMask to a downscaled dense array internally, so
results must be numerically identical to the dense path.
"""
def test_nmm_compact_matches_dense(self) -> None:
"""Merge groups must match between CompactMask and dense inputs."""
img_h, img_w = 40, 40
masks = np.zeros((3, img_h, img_w), dtype=bool)
masks[0, 0:20, 0:20] = True # top-left
masks[1, 0:18, 0:18] = True # heavily overlaps mask 0
masks[2, 20:40, 20:40] = True # bottom-right, no overlap
scores = np.array([0.9, 0.8, 0.7])
predictions = np.column_stack([np.zeros((3, 4)), scores])
cm = _cm_from_masks(masks, (img_h, img_w))
groups_dense = mask_non_max_merge(predictions, masks, iou_threshold=0.3)
groups_compact = mask_non_max_merge(predictions, cm, iou_threshold=0.3)
def normalise(groups: list[list[int]]) -> list[list[int]]:
return sorted(sorted(group) for group in groups)
assert normalise(groups_compact) == normalise(groups_dense)
def test_nmm_no_merge(self) -> None:
"""Non-overlapping masks: every mask should be its own group."""
img_h, img_w = 20, 20
masks = np.zeros((3, img_h, img_w), dtype=bool)
masks[0, 0:5, 0:5] = True
masks[1, 7:12, 7:12] = True
masks[2, 14:19, 14:19] = True
scores = np.array([0.9, 0.8, 0.7])
predictions = np.column_stack([np.zeros((3, 4)), scores])
cm = _cm_from_masks(masks, (img_h, img_w))
groups = mask_non_max_merge(predictions, cm, iou_threshold=0.5)
assert len(groups) == 3, "Each non-overlapping mask gets its own group"
assert all(len(group) == 1 for group in groups)
def test_nmm_full_merge(self) -> None:
"""Identical masks: all predictions should merge into one group."""
img_h, img_w = 20, 20
single = np.zeros((1, img_h, img_w), dtype=bool)
single[0, 5:15, 5:15] = True
masks = np.repeat(single, 3, axis=0)
scores = np.array([0.9, 0.8, 0.7])
predictions = np.column_stack([np.zeros((3, 4)), scores])
cm = _cm_from_masks(masks, (img_h, img_w))
groups = mask_non_max_merge(predictions, cm, iou_threshold=0.5)
assert len(groups) == 1, "Identical masks must collapse to one group"
assert len(groups[0]) == 3
# ---------------------------------------------------------------------------
# Random scenario helpers
# ---------------------------------------------------------------------------
# Small (N, h, w) configs to keep IoU tests fast.
_IOU_RANDOM_CONFIGS = [
(5, 30, 30),
(8, 40, 40),
(10, 25, 25),
(6, 50, 50),
(12, 30, 40),
(5, 60, 60),
(15, 20, 20),
(7, 35, 35),
(10, 40, 50),
(8, 45, 45),
]
def _random_masks(
rng: np.random.Generator,
num_masks: int,
img_h: int,
img_w: int,
fill_prob: float = 0.25,
) -> np.ndarray:
"""Generate *num_masks* random boolean masks with at least one True pixel each."""
masks = np.zeros((num_masks, img_h, img_w), dtype=bool)
for mask_idx in range(num_masks):
y1 = rng.integers(0, img_h)
y2 = rng.integers(y1, img_h)
x1 = rng.integers(0, img_w)
x2 = rng.integers(x1, img_w)
region = rng.random((y2 - y1 + 1, x2 - x1 + 1)) < fill_prob
if not region.any():
region[0, 0] = True
masks[mask_idx, y1 : y2 + 1, x1 : x2 + 1] = region
return masks
class TestCompactMaskIouRandom:
"""compact_mask_iou_batch matches dense mask_iou_batch across 10 random seeds.
Uses small mask counts (5-15) and image sizes (20x20 to 60x60) to keep
individual test runs under 1 second.
"""
@pytest.mark.parametrize("seed", list(range(10)))
def test_parity_seed(self, seed: int) -> None:
rng = np.random.default_rng(seed)
num_masks_a, img_h, img_w = _IOU_RANDOM_CONFIGS[seed]
num_masks_b = max(3, num_masks_a - 2)
masks_a = _random_masks(rng, num_masks_a, img_h, img_w)
masks_b = _random_masks(rng, num_masks_b, img_h, img_w)
cm_a = _cm_from_masks(masks_a, (img_h, img_w))
cm_b = _cm_from_masks(masks_b, (img_h, img_w))
compact_result = compact_mask_iou_batch(cm_a, cm_b)
dense_result = _dense_iou(masks_a, masks_b)
assert compact_result.shape == (num_masks_a, num_masks_b), (
f"Shape mismatch: {compact_result.shape} vs ({num_masks_a}, {num_masks_b})"
)
np.testing.assert_allclose(
compact_result,
dense_result,
atol=1e-9,
err_msg=f"IoU mismatch: seed={seed}, N_a={num_masks_a}, N_b={num_masks_b}",
)
@pytest.mark.parametrize("seed", list(range(10)))
def test_self_iou_diagonal(self, seed: int) -> None:
"""Self-IoU diagonal must be 1.0 for masks with at least one True pixel."""
rng = np.random.default_rng(seed + 50)
num_masks, img_h, img_w = _IOU_RANDOM_CONFIGS[seed]
masks = _random_masks(rng, num_masks, img_h, img_w)
cm = _cm_from_masks(masks, (img_h, img_w))
result = compact_mask_iou_batch(cm, cm)
np.testing.assert_allclose(
np.diag(result),
1.0,
atol=1e-9,
err_msg=f"Diagonal not 1.0 for seed={seed}",
)
@pytest.mark.parametrize("seed", list(range(10)))
def test_tight_bbox_parity(self, seed: int) -> None:
"""Tight bounding boxes (mask_to_xyxy) must still produce identical IoU."""
from supervision.detection.utils.converters import mask_to_xyxy
rng = np.random.default_rng(seed + 200)
num_masks, img_h, img_w = _IOU_RANDOM_CONFIGS[seed]
num_masks_b = max(3, num_masks - 2)
masks_a = _random_masks(rng, num_masks, img_h, img_w)
masks_b = _random_masks(rng, num_masks_b, img_h, img_w)
xyxy_a = mask_to_xyxy(masks_a).astype(np.float32)
xyxy_b = mask_to_xyxy(masks_b).astype(np.float32)
cm_a = CompactMask.from_dense(masks_a, xyxy_a, image_shape=(img_h, img_w))
cm_b = CompactMask.from_dense(masks_b, xyxy_b, image_shape=(img_h, img_w))
compact_result = compact_mask_iou_batch(cm_a, cm_b)
dense_result = _dense_iou(masks_a, masks_b)
np.testing.assert_allclose(
compact_result,
dense_result,
atol=1e-9,
err_msg=f"Tight bbox IoU mismatch for seed={seed}",
)
@@ -0,0 +1,162 @@
"""Integration tests for InferenceSlicer with compact_masks=True.
Verifies that with compact_masks=True:
- Masks stay as CompactMask throughout the pipeline (no dense materialisation).
- NMS is computed via RLE IoU (no resize, no dense (N,H,W) alloc).
- Final detections are pixel-identical to the compact_masks=False path.
"""
from __future__ import annotations
import numpy as np
import supervision as sv
from supervision.detection.compact_mask import CompactMask
from supervision.detection.core import Detections
def _fake_seg_callback(tile: np.ndarray) -> Detections:
"""Return two non-overlapping segmentation detections for any tile."""
h, w = tile.shape[:2]
masks = np.zeros((2, h, w), dtype=bool)
masks[0, : h // 3, : w // 3] = True
masks[1, h // 2 :, w // 2 :] = True
xyxy = np.array([[0, 0, w // 3, h // 3], [w // 2, h // 2, w, h]], dtype=np.float32)
return Detections(
xyxy=xyxy,
mask=masks,
confidence=np.array([0.9, 0.8], dtype=np.float32),
class_id=np.array([0, 1]),
)
class TestInferenceSlicerCompactMasks:
"""Tests that compact_masks=True keeps masks in RLE form end-to-end.
The pipeline inside InferenceSlicer goes:
callback → CompactMask.from_dense (tile coords)
→ with_offset (full-image coords)
→ CompactMask.merge (all tiles)
→ mask_non_max_suppression → compact_mask_iou_batch (RLE IoU)
None of those steps materialise a full (N, H, W) dense array.
"""
def test_compact_masks_flag_converts_dense_to_compact(self) -> None:
"""Masks returned from callback are CompactMask after _run_callback."""
image = np.zeros((200, 200, 3), dtype=np.uint8)
slicer = sv.InferenceSlicer(
callback=_fake_seg_callback,
slice_wh=200,
overlap_wh=0,
overlap_filter=sv.OverlapFilter.NONE,
compact_masks=True,
)
result = slicer(image)
assert isinstance(result.mask, CompactMask), (
f"compact_masks=True must produce a CompactMask, got {type(result.mask)}"
)
def test_compact_masks_false_keeps_dense(self) -> None:
"""Default (compact_masks=False) keeps dense ndarray masks."""
image = np.zeros((200, 200, 3), dtype=np.uint8)
slicer = sv.InferenceSlicer(
callback=_fake_seg_callback,
slice_wh=200,
overlap_wh=0,
overlap_filter=sv.OverlapFilter.NONE,
compact_masks=False,
)
result = slicer(image)
assert isinstance(result.mask, np.ndarray)
assert not isinstance(result.mask, CompactMask)
def test_compact_and_dense_pipelines_give_same_masks(self) -> None:
"""compact_masks=True and False must produce pixel-identical final masks."""
image = np.zeros((300, 300, 3), dtype=np.uint8)
slicer_dense = sv.InferenceSlicer(
callback=_fake_seg_callback,
slice_wh=150,
overlap_wh=0,
overlap_filter=sv.OverlapFilter.NON_MAX_SUPPRESSION,
iou_threshold=0.3,
compact_masks=False,
)
slicer_compact = sv.InferenceSlicer(
callback=_fake_seg_callback,
slice_wh=150,
overlap_wh=0,
overlap_filter=sv.OverlapFilter.NON_MAX_SUPPRESSION,
iou_threshold=0.3,
compact_masks=True,
)
det_dense = slicer_dense(image)
det_compact = slicer_compact(image)
assert len(det_dense) == len(det_compact)
dense_masks = det_dense.mask
compact_masks_arr = np.asarray(det_compact.mask)
# Sort both by xyxy to align order (NMS order may differ).
def _sort_key(d: Detections) -> np.ndarray:
return d.xyxy[:, 0] * 10000 + d.xyxy[:, 1]
order_d = np.argsort(_sort_key(det_dense))
order_c = np.argsort(_sort_key(det_compact))
np.testing.assert_array_equal(
dense_masks[order_d],
compact_masks_arr[order_c],
err_msg="compact_masks pipeline produced different mask pixels than dense",
)
def test_nms_with_overlapping_tiles_uses_rle_iou(self) -> None:
"""With overlapping tiles, NMS must suppress duplicates using RLE IoU."""
image = np.zeros((300, 300, 3), dtype=np.uint8)
call_count = 0
def counting_callback(tile: np.ndarray) -> Detections:
nonlocal call_count
call_count += 1
return _fake_seg_callback(tile)
slicer = sv.InferenceSlicer(
callback=counting_callback,
slice_wh=200,
overlap_wh=100, # heavy overlap → many duplicate detections
overlap_filter=sv.OverlapFilter.NON_MAX_SUPPRESSION,
iou_threshold=0.3,
compact_masks=True,
)
result = slicer(image)
assert call_count > 1, "Should have run on multiple tiles"
assert isinstance(result.mask, CompactMask), (
"Result mask must remain CompactMask after cross-tile NMS"
)
def test_no_mask_callback_unaffected(self) -> None:
"""compact_masks=True must not crash when callback returns no masks."""
def box_only_callback(tile: np.ndarray) -> Detections:
h, w = tile.shape[:2]
return Detections(
xyxy=np.array([[0, 0, w // 2, h // 2]], dtype=np.float32),
confidence=np.array([0.9]),
class_id=np.array([0]),
)
image = np.zeros((200, 200, 3), dtype=np.uint8)
slicer = sv.InferenceSlicer(
callback=box_only_callback,
slice_wh=200,
overlap_wh=0,
overlap_filter=sv.OverlapFilter.NONE,
compact_masks=True,
)
result = slicer(image)
assert result.mask is None