feat: add segmentation mask quality utilities#2263
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2263 +/- ##
========================================
Coverage 78% 78%
========================================
Files 66 67 +1
Lines 8374 8474 +100
========================================
+ Hits 6501 6597 +96
- Misses 1873 1877 +4 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR adds single-mask segmentation quality primitives (region overlap + boundary-aware metrics) to complement existing batch utilities and CompactMask support, along with tests and documentation to make the new metrics discoverable and reliable.
Changes:
- Added
mask_iou,dice_coefficient,boundary_iou, andboundary_f_scoreutilities supporting 2D masks,(1, H, W)single-mask batches, andCompactMask(single mask). - Added unit tests covering overlap math, empty-mask semantics, tolerance behavior, dtype parity, and
CompactMaskparity. - Added a new docs page and MkDocs navigation entry for the mask metrics.
Assessment (n/5):
- Code quality: 4/5
- Testing: 3/5
- Docs: 4/5
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/supervision/detection/utils/mask_metrics.py |
Implements new single-mask overlap/boundary metrics with validation and CompactMask support. |
tests/detection/utils/test_mask_metrics.py |
Adds unit tests for correctness, empty-mask semantics, tolerance behavior, dtype handling, and CompactMask parity. |
src/supervision/__init__.py |
Re-exports new metrics at the package top level and adds them to __all__. |
docs/detection/utils/mask_metrics.md |
Introduces documentation for the new metrics and their semantics. |
mkdocs.yml |
Adds the new Mask Metrics page to the documentation navigation. |
| """ | ||
| Compute Intersection over Union (IoU) for a single pair of segmentation masks. | ||
|
|
||
| Args: |
| """ | ||
| Compute Dice coefficient for a single pair of segmentation masks. | ||
|
|
||
| Args: |
| Boundary IoU dilates the foreground contours of both masks by `tolerance` | ||
| pixels before measuring IoU. This makes the score more forgiving to small | ||
| contour shifts than standard region IoU. | ||
|
|
||
| Args: |
| Boundary F-score measures contour agreement by matching predicted boundary | ||
| pixels to target boundary pixels within `tolerance` pixels, then combining | ||
| boundary precision and boundary recall into a single score. | ||
|
|
||
| Args: |
| assert math.isclose( | ||
| boundary_f_score(compact_prediction, compact_target, tolerance=1), | ||
| boundary_f_score(prediction, target, tolerance=1), | ||
| ) |
| @@ -0,0 +1,59 @@ | |||
| --- | |||
| comments: true | |||
| status: new | |||
There was a problem hiding this comment.
This needs to have decribtion as other pages
| return None | ||
|
|
||
|
|
||
| def _extract_boundary(mask: npt.NDArray[np.bool_]) -> npt.NDArray[np.bool_]: |
There was a problem hiding this comment.
also private funs shall have at leas summary docstring
Description
Adds small, composable segmentation mask quality utilities for comparing a predicted binary mask with a target mask.
Type of Change
Motivation and Context
Supervision already provides
mask_iou_batchand strong segmentation support throughDetections.maskandCompactMask. This PR adds single-mask quality primitives that are easy to reuse in segmentation model debugging, annotation QA, dataset inspection, and contour-sensitive evaluation workflows.Changes Made
mask_iou,dice_coefficient,boundary_iou, andboundary_f_scoreCompactMaskparityTesting
Commands run:
python -m venv .venv ./.venv/Scripts/python.exe -m pip install -e . pytest ruff mypy ./.venv/Scripts/python.exe -m pip install mypy==1.15.0 ./.venv/Scripts/python.exe -m ruff check src/supervision/detection/utils/mask_metrics.py tests/detection/utils/test_mask_metrics.py src/supervision/__init__.py ./.venv/Scripts/python.exe -m mypy src/supervision/detection/utils/mask_metrics.py ./.venv/Scripts/python.exe -m pytest tests/detection/utils/test_mask_metrics.py -q ./.venv/Scripts/python.exe -m pytest src/supervision/detection/utils/mask_metrics.py -qGoogle Colab
Colab link: N/A
Screenshots/Videos
N/A
Additional Notes
Semantics are explicit and documented:
1.00.0toleranceis an integer pixel distance for boundary matchingThis PR is intentionally focused as PR 1 of a broader segmentation quality toolkit roadmap and does not include reports, error maps, or visualization helpers yet.