Faster R-CNN/ R-FCN在github上的python源码用mAP来度量模型的性能。mAP是各类别AP的平均,而各类别AP值是该类别precision(prec)对该类别recall(rec)的积分得到的,即PR曲线下面积,关于PR曲线和AP计算相关博客很多不在这赘述,这里主要从代码角度看一下pascal_voc.py和voc_eval.py里关于AP,rec, prec的实现。
源码里有AP和mAP的计算部分,但没有画PR曲线,上一篇博客讲了通过在lib/datasets/pascal_voc.py里加几行代码画PR曲线。严格来说,其实就是加了一句话:
pl.plot(rec, prec, lw=2,
label='Precision-recall curve of class {} (area = {:.4f})'
''.format(cls, ap))
参数里的rec和prec是由函数voc_eval得到:
rec, prec, ap = voc_eval(
filename, annopath, imagesetfile, cls, cachedir, ovthresh=0.5,
use_07_metric=use_07_metric)
该函数在lib/datasets/voc_eval.py中,详细分析如下:
# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Bharath Hariharan
# --------------------------------------------------------
import xml.etree.ElementTree as ET
import os
import cPickle
import numpy as np
def parse_rec(filename): #读取标注的xml文件
""" Parse a PASCAL VOC xml file """
tree = ET.parse(filename)
objects = []
for obj in tree.findall('object'):
obj_struct = {}
obj_struct['name'] = obj.find('name').text
obj_struct['pose'] = obj.find('pose').text
obj_struct['truncated'] = int(obj.find('truncated').text)
obj_struct['difficult'] = int(obj.find('difficult').text)
bbox = obj.find('bndbox')
obj_struct['bbox'] = [int(bbox.find('xmin').text),
int(bbox.find('ymin').text),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
objects.append(obj_struct)