对于怎么计算precision和recall可以参考对于http://blog.csdn.net/freeape/article/details/52090515
1 通过precision和recall计算ap
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author: yicm****@gmail.com
import argparse
import sys
from numpy import *
import numpy as np
#prec = 0.942857
#rec = 0.71895
#ap = 0.67786704015
def parse_args():
"""Parse input arguments."""
parser = argparse.ArgumentParser(description='AP Calculation')
parser.add_argument('--prec', dest='precision', help='The Precision Value',
default=0.0, type=float)
parser.add_argument('--rec', dest='recall', help='The Recall Value',
default=0.0, type=float)
args = parser.parse_args()
return args
def calc_ap(prec, rec):
mrec = np.array([0, rec, 1])
mpre = np.array([0, prec, 0])
for i in range(mrec.size - 2, -1, -1):
mpre[i] = max(mpre[i], mpre[i+1]);
#print mpre[i]
#print mrec[1 : ]
#print mrec[0 : 2]
idx1 = np.where(mrec[1 : ] != mrec[0 : 2])
idx2 = [x + 1 for x in idx1]
#print mrec.take(idx2)
#print mrec.take(idx1)
ap = sum((mrec.take(idx2) - mrec.take(idx1)) * mpre.take(idx2))
print "ap = " + str(ap)
return ap
if __name__ == "__main__":
args = parse_args()
#len(sys.argv)
print "args.precision: {}".format(args.precision)
print "args.recall: {}".format(args.recall)
calc_ap(args.precision, args.recall)
2 通过ap计算mAP
Mean Average Precision(mAP),平均精度计算。
mAP = sum(ap)/N
3 附matlab ap计算代码
另附Matlab代码:
prec = 0.371429
rec = 0.97744
mrec=[0 ; rec ; 1];
mpre=[0 ; prec ; 0];
for i=numel(mpre)-1:-1:1
mpre(i)=max(mpre(i),mpre(i+1));
end
i=find(mrec(2:end)~=mrec(1:end-1))+1;
ap=sum((mrec(i)-mrec(i-1)) .* mpre(i))