Repository navigation
Instance Segmentation Confusion Matrix - #240
mayankagarwals wants to merge 1 commit into
Conversation
| return area_inter / (area_true[:, None] + area_detection - area_inter) | ||
|
|
||
|
|
||
| def mask_iou_batch(masks_true: np.ndarray, masks_detection: np.ndarray) -> np.ndarray: |
There was a problem hiding this comment.
Hey! Yep, waiting for some discussion to happen with respect to API here #238 before completing the feature
|
Hi @mayankagarwals 👋🏻! Is that code ready for review? It is still marked as |
|
@mayankagarwals okey I'm rolling that PR over to |
|
Hi @kirilllzaitsev @hardikdava @SkalskiP While the code here is functional, I wanted to ask your opinion on Right now I'm assuming if force_masks is passed we will enforce that every detection in the |
|
@mayankagarwals hey, this PR should be relevant. |
|
hi @kirilllzaitsev! Cool! I guess then it makes sense to provide a segmentation matrix only if @kirilllzaitsev @hardikdava |
|
@SkalskiP @hardikdava |
There was a problem hiding this comment.
Pull request overview
This PR extends the existing ConfusionMatrix metric to optionally compute an additional confusion matrix based on instance-segmentation mask IoU, addressing the request in issue #238 while keeping the existing box-based confusion matrix behavior.
Changes:
- Added
mask_iou_batchutility for computing pairwise IoU between sets of boolean masks. - Extended
ConfusionMatrixto optionally compute and return a segmentation-based confusion matrix alongside the box-based one. - Added/updated tests to cover mask IoU and the new segmentation confusion matrix behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
supervision/detection/utils.py |
Adds mask_iou_batch for mask IoU computation (used by metrics). |
supervision/metrics/detection.py |
Extends ConfusionMatrix to compute segmentation confusion matrix and adds force_masks plumbing. |
test/metrics/test_detection.py |
Adds test coverage for mask IoU and segmentation confusion matrix cases. |
| max_x = int(np.max(detection_tensors[0][:, [0, 2]])) | ||
| max_x = max(max_x, int(np.max(target_tensors[0][:, [0, 2]]))) | ||
| max_y = int(np.max(detection_tensors[0][:, [1, 3]])) | ||
| max_y = max(max_x, int(np.max(target_tensors[0][:, [1, 3]]))) |
| classes: List[str], | ||
| conf_threshold: float = 0.3, | ||
| iou_threshold: float = 0.5, | ||
| force_masks=True, |
| force_masks (bool, default=True): | ||
| If True, the ConfusionMatrix.segmentationMatrix is computed. | ||
| Otherwise, it is None | ||
| If this value is set to True and masks | ||
| are not present for some images, error will be thrown. |
| if force_masks: | ||
| if prediction.mask is None or target.mask is None: | ||
| raise Exception( | ||
| "Please make sure all images have corresponding mask" | ||
| " annotations" | ||
| ) |
| iou_threshold: float, | ||
| prediction_mask: Optional[np.ndarray] = None, | ||
| target_mask: Optional[np.ndarray] = None, | ||
| ) -> np.ndarray: |
| iou_mask = ( | ||
| target_mask | ||
| if target_mask is None | ||
| else mask_iou_batch( | ||
| masks_true=target_mask, masks_detection=detection_mask_batch_filtered | ||
| ) | ||
| matches = ConfusionMatrix._drop_extra_matches(matches=matches) | ||
| else: | ||
| matches = np.zeros((0, 3)) | ||
|
|
||
| matched_true_idx, matched_detection_idx, _ = matches.transpose().astype( | ||
| np.int16 | ||
| ) |
| callback: Callable[[np.ndarray], Detections], | ||
| conf_threshold: float = 0.3, | ||
| iou_threshold: float = 0.5, | ||
| force_masks=True, | ||
| ) -> ConfusionMatrix: |
| def mask_iou_batch(masks_true: np.ndarray, masks_detection: np.ndarray) -> np.ndarray: | ||
| """ | ||
| Compute Intersection over Union (IoU) of two sets of masks - `masks_true` | ||
| and `masks_detection`. Both sets of masks are expected to be of type np.ndarray | ||
|
|
||
| Args: | ||
| masks_true (np.ndarray): 3D `np.ndarray` representing ground-truth | ||
| segmentation mask. `shape = (N, W, H)` where `N` is number of true objects. | ||
| masks_detection (np.ndarray): 3D `np.ndarray` representing detection | ||
| segmentation mask. `shape = (M, W, H)` where `M` is number of detected objects. | ||
|
|
||
| Returns: | ||
| np.ndarray: Pairwise IoU of masks from `masks_true` and | ||
| `masks_detection`. `shape = (N, M)` | ||
| where `N` is number of true objects and `M` is number of detected objects. | ||
| """ | ||
|
|
||
| intersection = np.logical_and(masks_true[:, None], masks_detection).sum((2, 3)) | ||
| union = np.logical_or(masks_true[:, None], masks_detection).sum((2, 3)) | ||
|
|
||
| return intersection / union |
| classes: List[str] | ||
| conf_threshold: float | ||
| iou_threshold: float | ||
| segmentationMatrix: Optional[np.ndarray] = None | ||
|
|
| segmentation_matrix = ( | ||
| prediction_masks | ||
| if prediction_masks is None | ||
| else (np.zeros((num_classes + 1, num_classes + 1))) | ||
| ) |
Adds segmentation confusion-matrix support by mirroring the existing ORIENTED_BOUNDING_BOXES dispatch pattern: masks are threaded as a parallel optional argument (they can't flatten into the tensor row like OBB coordinates), and evaluate_detection_batch/_split_detections_by_outcome dispatch to mask_iou_batch. Reuses the existing vectorized mask_iou_batch (matmul-based, chunked, zero-guarded) rather than introducing a new one. Removes _assert_supported_target, which previously rejected MASKS unconditionally at every entry point. Converts the 3 rejection tests to assert ValueError only when masks are actually missing, and adds positive coverage for perfect overlap, mask-IoU-vs-box-IoU divergence, and IoU-threshold sensitivity. Supersedes PR roboflow#240's force_masks/segmentationMatrix approach, which targeted the pre-src/ layout and predates this metric_target axis.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #240 +/- ##
=======================================
- Coverage 87% 87% -0%
=======================================
Files 84 84
Lines 11797 11816 +19
=======================================
+ Hits 10209 10224 +15
- Misses 1588 1592 +4 🚀 New features to boost your workflow:
|
AI Code Review (PR Review Agent)
|
|
Hi @mayankagarwals — thanks for the work here, and sorry this sat for so long without a real answer. Instance-segmentation support for Given that, I'm closing this PR — the functionality it targeted is now covered by #2612. I know that's a frustrating outcome for work that was otherwise sound; the review discussion here (the |
Description
Solves for #238
Type of change
Please delete options that are not relevant.
How has this change been tested, please provide a testcase or example of how you tested the change?
Please use this colab for testing: https://colab.research.google.com/drive/1OKjyH8JmFMROJ97447DUn13p-GDP6dXl
Test cases have been added