
컴퓨터 비전
Object Detection Metrics (mAP, IoU)객체 감지 메트릭
객체 감지(Object Detection) 모델을 평가하기 위한 핵심 메트릭인 IoU(교차 영역 비율)와 mAP(평균 정밀도 평균)를 다룬다.
IoU (Intersection over Union)
IoU = 예측 박스 ∩ 정답 박스 / 예측 박스 ∪ 정답 박스
IoU ≥ 0.5 → TP (맞게 감지)
IoU < 0.5 → FP (잘못 감지)
감지 없음 → FN (미탐지)python
def compute_iou(box1, box2):
"""box: [x1, y1, x2, y2]"""
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
intersection = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (box1[2]-box1[0]) * (box1[3]-box1[1])
area2 = (box2[2]-box2[0]) * (box2[3]-box2[1])
union = area1 + area2 - intersection
return intersection / (union + 1e-6)mAP 계산 과정
python
import numpy as np
def compute_ap(recalls, precisions):
"""AP = 정밀도-재현율 곡선의 아래 면적 (11점 보간)"""
ap = 0.0
for threshold in np.arange(0, 1.1, 0.1):
prec_at_threshold = precisions[recalls >= threshold]
ap += max(prec_at_threshold) if len(prec_at_threshold) else 0
return ap / 11
def compute_map(predictions, ground_truths, iou_threshold=0.5):
"""
predictions: [{'class': int, 'score': float, 'box': [x1,y1,x2,y2]}]
ground_truths: [{'class': int, 'box': [x1,y1,x2,y2]}]
"""
classes = set(gt['class'] for gt in ground_truths)
ap_per_class = {}
for cls in classes:
cls_preds = sorted(
[p for p in predictions if p['class'] == cls],
key=lambda x: x['score'], reverse=True
)
cls_gts = [g for g in ground_truths if g['class'] == cls]
tps, fps = [], []
matched = set()
for pred in cls_preds:
best_iou, best_gt_idx = 0, -1
for i, gt in enumerate(cls_gts):
if i in matched:
continue
iou = compute_iou(pred['box'], gt['box'])
if iou > best_iou:
best_iou, best_gt_idx = iou, i
if best_iou >= iou_threshold and best_gt_idx not in matched:
tps.append(1); fps.append(0)
matched.add(best_gt_idx)
else:
tps.append(0); fps.append(1)
tp_cumsum = np.cumsum(tps)
fp_cumsum = np.cumsum(fps)
recalls = tp_cumsum / (len(cls_gts) + 1e-6)
precisions = tp_cumsum / (tp_cumsum + fp_cumsum + 1e-6)
ap_per_class[cls] = compute_ap(recalls, precisions)
return np.mean(list(ap_per_class.values()))COCO mAP 기준
| 메트릭 | IoU 범위 | 설명 |
|---|---|---|
| mAP@0.5 | 0.50 | PASCAL VOC 기준 |
| mAP@0.75 | 0.75 | 엄격한 기준 |
| mAP@0.5:0.95 | 0.50~0.95 (0.05 단위) | COCO 메인 메트릭 |
| mAP_S/M/L | 크기별 | 소/중/대 객체별 성능 |