目标检测中的AP计算 |
您所在的位置:网站首页 › ar计算公式是 › 目标检测中的AP计算 |
目标检测中的AP计算
最近在学习目标检测,对模型评价指标AP的计算过程有点疑问,经过查找资料、问师兄,最终算是有了一个相对明确的了解,特此记录一下,方便以后查看,不足之处还请大家批评指正! AP(average precision)是目标检测论文中广泛使用的模型评价指标,VOC的AP计算方法在2010年的时候发生过一次更改,现在常用的是2010年之后更改的AP计算方法,该计算方法相比于之前也更为合理,本文的AP计算也是2010年之后的计算方法。官方的定义如下: AP计算涉及到一些其他的知识,如TP,FP,TN,FN,precision,recall,IOU等等,下面进行简单介绍: TP,FP,TN,FN,precision,recallTP:真正例 true positive FP:假正例 false positive TN:真负例 true negative FN:假负例 false negative precision:准确率 recall:召回率 这几个名词的介绍我觉得这篇博客(https://blog.csdn.net/qq_41994006/article/details/81051150)里的一张图说的很清晰,这里直接摘下来了(略懒。。。) IOU(交并比)就是两个bounding box的交集与并集之比,这里再摘一张图。。。(源自博客https://blog.csdn.net/hsqyc/article/details/81702437)
![]() ![]() 对每个类别需要单独计算AP,最后所有类别取平均。 下面以类别1为例,假设该测试集图片中共有3个类别1的标注框(ground truth boxes),显然上述预测结果并没有将全部真值召回。 绘制P-R曲线,结果如下表: 下面贴上AP计算的python代码(源自https://github.com/eriklindernoren/PyTorch-YOLOv3) 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: ground truth label (np array),true positive为1,false positive为0 conf: Objectness value from 0-1 (np array). pred_cls: Predicted object classes (np array). target_cls: True object classes (np array). # Returns The average precision as computed in py-faster-rcnn. """ # Sort by objectness i = np.argsort(-conf) tp, conf, pred_cls = tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes = np.unique(target_cls) # Create Precision-Recall curve and compute AP for each class ap, p, r = [], [], [] for c in tqdm.tqdm(unique_classes, desc="Computing AP"): i = pred_cls == c n_gt = (target_cls == c).sum() # Number of ground truth objects n_p = i.sum() # Number of predicted objects 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() #累加和列表 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") 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 (np.array). precision: The precision curve (np.array). # 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] #错位比较,前一个元素与其后一个元素比较,np.where()返回下标索引数组组成的元组 # and sum (\Delta recall) * prec ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) return apOK,终于写完了。 以前没有写博客记笔记的习惯,以后得多记笔记! 原创不易,花了挺多时间,转载请注明出处! |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |