Skip to content
Back to skills

Map Iou Precision Sweep

ASecurity

Compute mean Average Precision by sweeping IoU thresholds from 0.5 to 0.95 on RLE-encoded instance masks using pycocotools

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
toolspython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill map-iou-precision-sweep --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Map Iou Precision Sweep?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Map Iou Precision Sweep
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-map-iou-precision-sweep/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-map-iou-precision-sweep)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-map-iou-precision-sweep
description: Compute mean Average Precision by sweeping IoU thresholds from 0.5 to 0.95 on RLE-encoded instance masks using pycocotools
---

# mAP IoU Precision Sweep

## Overview

The standard COCO-style mAP for instance segmentation averages precision across IoU thresholds [0.5, 0.55, ..., 0.95]. This implementation uses pycocotools `mask_util.iou` for fast RLE-based IoU computation, then sweeps thresholds to compute TP/FP/FN counts and average precision. Essential for offline evaluation of Mask R-CNN, Detectron2, or MMDetection outputs.

## Quick Start

```python
import numpy as np
from pycocotools import mask as mask_util

def precision_at(threshold, iou_matrix):
    matches = iou_matrix > threshold
    tp = np.sum(np.sum(matches, axis=1) == 1)
    fp = np.sum(np.sum(matches, axis=0) == 0)
    fn = np.sum(np.sum(matches, axis=1) == 0)
    return tp, fp, fn

def compute_map(pred_masks, gt_masks):
    enc_preds = [mask_util.encode(np.asfortranarray(m)) for m in pred_masks]
    enc_gts = [mask_util.encode(np.asfortranarray(m)) for m in gt_masks]
    ious = mask_util.iou(enc_preds, enc_gts, [0] * len(enc_gts))
    precisions = []
    for t in np.arange(0.5, 1.0, 0.05):
        tp, fp, fn = precision_at(t, ious)
        precisions.append(tp / (tp + fp + fn) if (tp + fp + fn) > 0 else 0)
    return np.mean(precisions)
```

## Workflow

1. Encode predicted and ground-truth binary masks as RLE using `mask_util.encode`
2. Compute the full IoU matrix with `mask_util.iou` (fast C implementation)
3. For each threshold in [0.5, 0.55, ..., 0.95], count TP/FP/FN
4. Compute precision per threshold, then average across all 10 thresholds

## Key Decisions

- **RLE encoding**: `np.asfortranarray` is required — pycocotools expects Fortran-order arrays
- **One-to-one matching**: each pred matches at most one GT (sum axis check == 1)
- **Empty predictions**: return 0 if no predictions; missing this causes division errors
- **Integration**: wrap in a Detectron2 `DatasetEvaluator` for periodic validation during training

## References

- [Positive score with Detectron 2/3 - Training](https://www.kaggle.com/code/slawekbiel/positive-score-with-detectron-2-3-training)
- [Sartorius Segmentation - Detectron2 [Training]](https://www.kaggle.com/code/ammarnassanalhajali/sartorius-segmentation-detectron2-training)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…