目标检测的评估函数(precision,recall,mAP)

对于二分类的情况,我们可以使用以下公式计算其精确度(precision)和召回率(recall)。

精确率和召回率的计算公式

而在目标检测中,存在多种类别,例如COCO有80种类别,若要计算模型的mAP,先要求出单个类别的ap,而单个类别的ap由该类别的precision和recall计算得出。
其中的关于目标检测中的TP,FP,TN,FN定义。
TP:预测框Bounding box与真实框GT的IOU值大于0.5。
FP:预测框Bounding box与真实框GT的IOU值小于0.5,或者虽然BB与GT的IOU大于0.5,但是已经有BB与该GT匹配了。
TN:不存在该指标,因为每张图片至少包含一个目标。
FN:方法不能在图片上产生Bounding box。即真实情况下有目标,但是模型在该位置不能产生预测框。
下面通过一个例子来计算单个类别的ap。

例子

对于一幅图片,若person类别在图片中有7个,即7个ground truth box。而我们的模型一共检测出了10个bounding box,并且按照pred_conf进行排序,若bounding box与greound truth box的iou值大于IOU_thresh阈值(一般设为0.5))的话,GT为1。

检测情况

计算precision和recall:可以看出一共有7个真实框,而模型只检测出了5个框,下图为按照pred_conf的顺序计算各处的precision和recall,公式参考图1。
以rank_5为例,计算precision,到rank_5为止,一共有5个bounding box,而TP的数量为2,所以TP=TP/TP+FN=2/5=0.4。而recall,由于ground truth的数量为7,所以recall=2/7=0.29。
各阶段的精确率和召回率的计算

仔细观察,随着rank的增加,我们预测框的数目不断增加,那么我们预测框更有可能与真实框匹配,漏检的情况会减少,在分母不变(因为该类别的真实框数目已经确定),分子不断变大,那么recall会增大。同时,随着预测框增多,我们可能更会出现错检的情况,当发生错检时,分子不变(bounding box与ground truth匹配的数量),分母变大那么precision会减小。当然,由于precision在预测框增多时,分子和分母可能都会变化,导致precision增大的情况,如rank_8到rank_9的情况。但是总体是下降的趋势。所以presicion和recall去一对矛盾的测量指标。
计算单类别ap:现采用VOC2010及以后的方法,对于Recall >= 0, 0.14, 0.29, 0.43, 0.57, 0.71, 1,我们选取此时Percision的最大值:1, 1, 1, 0.5, 0.5, 0.5, 0。此时,该类别的 AP = (0.14-0)x1 + (0.29-0.14)x1 + (0.43-0.29)x0.5 + (0.57-0.43)x0.5 + (0.71-0.57)x0.5 + (1-0.71)x0 = 0.5
不同recall下,最大的precision值

以上便是计算单个类别的ap,mAP就是对每一个类别都计算出AP然后再计算AP平均值就好了。

代码实现

以下代码截取于YOLOv3的模型评估函数
计算单个类别的ap

# 计算每个类的ap 具体算法:
def ap_per_class(tp, conf, pred_cls, target_cls):
    """ Compute the average precision, given the recall and precision curves.
    Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
    # Arguments
        tp:    True positives (list).
        conf:  Objectness value from 0-1 (list).
        pred_cls: Predicted object classes (list).
        target_cls: True object classes (list).
    # Returns
        The average precision as computed in py-faster-rcnn.
    """

    # 1、按照pred_conf进行排序
    i = np.argsort(-conf)
    tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
    # 2、目标框中的不同类别(Person,Car...)
    unique_classes = np.unique(target_cls)
    # Create Precision-Recall curve and compute AP for each class
    ap, p, r = [], [], []
    # 3、遍历类别,计算每个类别的precison和recall
    for c in tqdm.tqdm(unique_classes, desc="Computing AP"):
        i = pred_cls == c
        n_gt = (target_cls == c).sum()  # 真实框中该类别数目
        n_p = i.sum()  # 预测框中该类别数目

        if n_p == 0 and n_gt == 0:
            continue
        elif n_p == 0 or n_gt == 0:
            ap.append(0)
            r.append(0)
            p.append(0)
        else:
            # Accumulate FPs and TPs
            fpc = (1 - tp[i]).cumsum()  # cumsum一维数组返回累计和
            tpc = (tp[i]).cumsum()

            # Recall
            recall_curve = tpc / (n_gt + 1e-16)
            r.append(recall_curve[-1])

            # Precision
            precision_curve = tpc / (tpc + fpc)
            p.append(precision_curve[-1])

            # AP from recall-precision curve
            ap.append(compute_ap(recall_curve, precision_curve))

    # Compute F1 score (harmonic mean of precision and recall)
    p, r, ap = np.array(p), np.array(r), np.array(ap)
    f1 = 2 * p * r / (p + r + 1e-16)

    return p, r, ap, f1, unique_classes.astype("int32")

对于上述例子,person类别的tp[i]为[1,1,0,0,0,1,0,0,1,1],tpc即将tp进行累加得到每个rank的分子,为[1,2,2,2,2,3,3,3,4,5]。tpc+fpc即为当前rank的预测框数目,为[1,2,3,4,5,6,7,8,9,10]。
根据precision和recall计算ap

def compute_ap(recall, precision):
    """ Compute the average precision, given the recall and precision curves.
    Code originally from https://github.com/rbgirshick/py-faster-rcnn.

    # Arguments
        recall:    The recall curve (list).
        precision: The precision curve (list).
    # Returns
        The average precision as computed in py-faster-rcnn.
    """
    # correct AP calculation
    # first append sentinel values at the end
    mrec = np.concatenate(([0.0], recall, [1.0]))
    mpre = np.concatenate(([0.0], precision, [0.0]))

    # compute the precision envelope
    for i in range(mpre.size - 1, 0, -1):
        mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])

    # to calculate area under PR curve, look for points
    # where X axis (recall) changes value
    i = np.where(mrec[1:] != mrec[:-1])[0]

    # and sum (\Delta recall) * prec
    ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
    return ap

参考

https://www.zhihu.com/question/53405779

你可能感兴趣的:(目标检测的评估函数(precision,recall,mAP))