Skip to content

feat: add segmentation mask quality utilities#2263

Open
Aniketiitk21 wants to merge 2 commits into
roboflow:developfrom
Aniketiitk21:feat/segmentation-mask-quality-utilities
Open

feat: add segmentation mask quality utilities#2263
Aniketiitk21 wants to merge 2 commits into
roboflow:developfrom
Aniketiitk21:feat/segmentation-mask-quality-utilities

Conversation

@Aniketiitk21
Copy link
Copy Markdown

@Aniketiitk21 Aniketiitk21 commented May 21, 2026

Description

Adds small, composable segmentation mask quality utilities for comparing a predicted binary mask with a target mask.

Type of Change

  • New feature (non-breaking change which adds functionality)

Motivation and Context

Supervision already provides mask_iou_batch and strong segmentation support through Detections.mask and CompactMask. 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

  • Added mask_iou, dice_coefficient, boundary_iou, and boundary_f_score
  • Added tests for overlap math, boundary tolerance behavior, empty-mask semantics, dtype handling, validation, and CompactMask parity
  • Added docs/examples and a docs navigation entry for the new utilities

Testing

  • I have tested this code locally
  • I have added unit tests that prove my feature works
  • All new tests pass locally

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 -q

Google Colab

Colab link: N/A

Screenshots/Videos

N/A

Additional Notes

Semantics are explicit and documented:

  • both masks empty -> 1.0
  • one mask empty -> 0.0
  • tolerance is an integer pixel distance for boundary matching

This 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.

@Aniketiitk21 Aniketiitk21 requested a review from SkalskiP as a code owner May 21, 2026 02:34
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 21, 2026

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

❌ Patch coverage is 96.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 78%. Comparing base (e7376d5) to head (4830790).
⚠️ Report is 8 commits behind head on develop.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and boundary_f_score utilities supporting 2D masks, (1, H, W) single-mask batches, and CompactMask (single mask).
  • Added unit tests covering overlap math, empty-mask semantics, tolerance behavior, dtype parity, and CompactMask parity.
  • 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.

Comment on lines +106 to +109
"""
Compute Intersection over Union (IoU) for a single pair of segmentation masks.

Args:
Comment on lines +149 to +152
"""
Compute Dice coefficient for a single pair of segmentation masks.

Args:
Comment on lines +196 to +200
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:
Comment on lines +255 to +259
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to have decribtion as other pages

return None


def _extract_boundary(mask: npt.NDArray[np.bool_]) -> npt.NDArray[np.bool_]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also private funs shall have at leas summary docstring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants