【深度学习】mAP (Mean Average Precision)

mAP 概念

P

precision,即 准确率

R

recall,即 召回率

PR曲线

即 以 precisionrecall 作为 纵、横轴坐标 的二维曲线。

一般来说,precisionrecall鱼与熊掌 的关系。下图即是 PR曲线
这里写图片描述

AP值

Average Precision,即 平均精确度

如何衡量一个模型的性能,单纯用 precision 和 recall 都不科学。于是人们想到,哎嘛为何不把 PR曲线下的面积 当做衡量尺度呢?于是就有了 AP值 这一概念。这里的 average,等于是对 precision 进行 取平均

mAP值

Mean Average Precision,即 平均AP值

是对多个验证集个体 求 平均AP值 。如下图:
这里写图片描述

mAP 计算

公式

这里写图片描述

Code

def compute_ap(gt_boxes, gt_class_ids,
               pred_boxes, pred_class_ids, pred_scores,
               iou_threshold=0.5):
    """Compute Average Precision at a set IoU threshold (default 0.5).

    Returns:
    mAP: Mean Average Precision
    precisions: List of precisions at different class score thresholds.
    recalls: List of recall values at different class score thresholds.
    overlaps: [pred_boxes, gt_boxes] IoU overlaps.
    """
    # Trim zero padding and sort predictions by score from high to low
    gt_boxes = trim_zeros(gt_boxes)
    pred_boxes = trim_zeros(pred_boxes)
    pred_scores = pred_scores[:pred_boxes.shape[0]]
    indices = np.argsort(pred_scores)[::-1]
    pred_boxes = pred_boxes[indices]
    pred_class_ids = pred_class_ids[indices]
    pred_scores = pred_scores[indices]

    # Compute IoU overlaps [pred_boxes, gt_boxes]
    overlaps = compute_overlaps(pred_boxes, gt_boxes)

    # Loop through ground truth boxes and find matching predictions
    match_count = 0
    pred_match = np.zeros([pred_boxes.shape[0]])
    gt_match = np.zeros([gt_boxes.shape[0]])
    for i in range(len(pred_boxes)):
        # Find best matching ground truth box
        sorted_ixs = np.argsort(overlaps[i])[::-1]
        for j in sorted_ixs:
            # If ground truth box is already matched, go to next one
            if gt_match[j] == 1:
                continue
            # If we reach IoU smaller than the threshold, end the loop
            iou = overlaps[i, j]
            if iou < iou_threshold:
                break
            # Do we have a match?
            if pred_class_ids[i] == gt_class_ids[j]:
                match_count += 1
                gt_match[j] = 1
                pred_match[i] = 1
                break

    # Compute precision and recall at each prediction box step
    precisions = np.cumsum(pred_match) / (np.arange(len(pred_match)) + 1)
    recalls = np.cumsum(pred_match).astype(np.float32) / len(gt_match)

    # Pad with start and end values to simplify the math
    precisions = np.concatenate([[0], precisions, [0]])
    recalls = np.concatenate([[0], recalls, [1]])

    # Ensure precision values decrease but don't increase. This way, the
    # precision value at each recall threshold is the maximum it can be
    # for all following recall thresholds, as specified by the VOC paper.
    for i in range(len(precisions) - 2, -1, -1):
        precisions[i] = np.maximum(precisions[i], precisions[i + 1])

    # Compute mean AP over recall range
    indices = np.where(recalls[:-1] != recalls[1:])[0] + 1
    mAP = np.sum((recalls[indices] - recalls[indices - 1]) *
                 precisions[indices])

    return mAP, precisions, recalls, overlaps

[1]完整学习目标检测中的 Recalls, Precisions, AP, mAP 算法 Part1
[2]完整学习目标检测中的 Recalls, Precisions, AP, mAP 算法 Part2

### Mean Average Precision (mAP) 的定义 Mean Average Precision (mAP) 是一种广泛用于评估信息检索系统和目标检测模型性能的度量标准。该指标综合考虑了查准率(Precision) 和查全率(Recall),能够更全面地反映系统的整体表现。 在信息检索领域,mAP 表示的是所有查询平均精度的均值[^3]。对于每一个单独的查询请求而言,先计算其对应的 AP 值(Average Precision),再取这些 AP 值的算术平均即得到最终的 mAP 结果。 而在对象检测任务中,mAP 则衡量不同类别上预测框与真实标注框之间的匹配程度。具体来说,针对每一类物体分别统计 TP(True Positive), FP(False Positive) 及 FN(False Negative)[^1]。 ### 计算方法 #### 对于信息检索: 1. 针对单个查询 q,按照文档的相关性得分排序; 2. 根据实际相关情况标记每个返回项是否为正样本; 3. 使用下列公式逐个位置 i 更新当前累计 P@i(Precision at position i),其中 rel(i)=1 若第 i 位是相关项目,否则为0;nrel(q)表示查询q下的总相关数: \[ \text{P}(i|q) = \frac{\sum_{j=1}^{i}\mathrm{rel(j)}}{i},\quad \forall i : \mathrm{rel}(i)=1 \] 4. 将上述过程中获得的所有非零 P@i 平均化作为此查询 Q 下的 AP 值; 5. 最终通过求解所有测试集中各查询下 AP 的简单平均值得到整个数据集上的 mAP。 #### 对象检测中的实现方式略有差异: - 主要区别在于如何判断两个边界框之间是否存在重叠以及设定 IoU(Intersection over Union)阈值来决定真阳性和假阳性。 - 当前普遍采用 COCO 数据集所使用的评价方案,在多个不同的 IoU 阈值水平上来累积 PR 曲线并据此得出每种类别的 AP 后再次做平均形成全局 mAP 指标。 ```python def calculate_map(precisions, recalls): """ Calculate mean average precision given lists of precisions and recalls. Args: precisions (list): List containing precision values. recalls (list): List containing recall values. Returns: float: Calculated MAP value. """ aps = [] unique_recalls = sorted(set(recalls)) for r in unique_recalls: p_interpolated = max([p for p, rec in zip(precisions, recalls) if rec >= r], default=0) aps.append(p_interpolated) return sum(aps)/len(unique_recalls) if len(unique_recalls)>0 else 0 ```
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值