精度/召回/AP/mAP

import numpy as np
import matplotlib.pyplot as plt

def voc_ap(rec, prec, use_07_metric=False):
    if use_07_metric:
        # 11 point metric
        ap = 0.
        for t in np.arange(0., 1.1, 0.1):
            if np.sum(rec >= t) == 0:
                p = 0
            else:
                p = np.max(prec[rec >= t])
            ap = ap + p / 11.
    else:
        # correct AP calculation
        # first append sentinel values at the end
        print (rec)
        mrec = np.concatenate(([0.], rec, [1.]))
        mpre = np.concatenate(([0.], prec, [0.]))

        print ('...................')
        # 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

def compute_ap(scores, gt_label, class_id = 0, t = 0.5):
    sorted_ind = np.argsort(scores)[::-1]

    socres = [scores[i] for i in sorted_ind]
    gt_label = [gt_label[i] for i in sorted_ind]

    nd = len(scores)
    npos = np.sum(gt_label)
    print ('npos:', npos)
    tp = np.zeros(nd)
    fp = np.zeros(nd)
    for i in range(nd):
        score = scores[i]
        label = gt_label[i]
        if score > t and label == class_id:
            tp[i] = 1
        else:
            fp[i] = 1

    print ('tp:', tp)
    fp = np.cumsum(fp)
    tp = np.cumsum(tp)

    # recall
    rec = tp / float(npos)
    # precision
    prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
    ap = voc_ap(rec, prec, False)
    # ap = voc_ap(rec, prec, True)
    return rec, prec, ap

scores = [0.23, 0.76, 0.01, 0.91, 0.13, 0.45, 0.12, 0.03, 0.38,
          0.11, 0.03, 0.09, 0.65, 0.07, 0.12, 0.24, 0.1, 0.23, 0.46, 0.08]
gt_label = [0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
# arange_t = np.arange(0, 1.1, 0.1)
# for t in arange_t:
compute_ap(scores, gt_label, 1, 0.1)

# mAP就是所有AP的平均值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值