前言
之前簡單介紹過目标檢測算法的一些評價标準,位址為目标檢測算法之評價标準和常見資料集盤點。然而這篇文章僅僅隻是從概念性的角度來闡述了常見的評價标準如Acc,Precision,Recall,AP等。并沒有從源碼的角度來分析具體的計算過程,這一篇推文的目的就是結合代碼再次詳細的解釋目标檢測算法中的常見評價标準如Precision,Recall,AP,mAP的具體計算過程。
評價名額
由于在上面那篇推文中已經詳細解釋過了,是以這裡就隻是簡單的再回顧一下,詳細的解釋請移步那篇推文看看。為了友善了解,還是先說一下TP,TN,FP,FN的含義。
一個經典例子是存在一個測試集合,測試集合隻有大雁和飛機兩種圖檔組成,假設你的分類系統最終的目的是:能取出測試集中所有飛機的圖檔,而不是大雁的圖檔。然後就可以定義:
- True positives: 簡稱為TP,即正樣本被正确識别為正樣本,飛機的圖檔被正确的識别成了飛機。
- True negatives: 簡稱為TN,即負樣本被正确識别為負樣本,大雁的圖檔沒有被識别出來,系統正确地認為它們是大雁。
- False Positives: 簡稱為FP,即負樣本被錯誤識别為正樣本,大雁的圖檔被錯誤地識别成了飛機。
- False negatives: 簡稱為FN,即正樣本被錯誤識别為負樣本,飛機的圖檔沒有被識别出來,系統錯誤地認為它們是大雁。
接下來我們就開始定義一些評價标準:
- 準确率(Acc):準确率(Acc)的計算公式為,即預測正确的樣本比例,代表測試的樣本數。在檢測任務中沒有預測正确的負樣本的概念,是以Acc自然用不到了。
- 查準率(Precision):查準率是針對某一個具體類别而言的,公式為:,其中N代表所有檢測到的某個具體類的目标框個數。
- 召回率(Recall):召回率仍然是針對某一個具體類别而言的,公式為:,即預測正确的目标框和所有Ground Truth框的比值。
- F1 Score:定位Wie查準率和召回率的調和平均,公式如下:。
- IOU:先為計算mAP值做一個鋪墊,即IOU門檻值是如何影響Precision和Recall值的?比如在PASCAL VOC競賽中采用的IoU門檻值為0.5,而COCO競賽中在計算mAP較複雜,其計算了一系列IoU門檻值(0.05至0.95)下的mAP當成最後的mAP值。
- mAP:全稱為Average Precision,AP值是Precision-Recall曲線下方的面積。那麼問題來了,目标檢測中PR曲線怎麼來的?可以在這篇論文找到答案,截圖如下:
我來解釋一下,要得到Precision-Recall曲線(以下簡稱PR)曲線,首先要對檢測模型的預測結果按照目标置信度降序排列。然後給定一個
rank
值,Recall和Precision僅在置信度高于該
rank
值的預測結果中計算,改變
rank
值會相應的改變Recall值和Precision值。這裡選擇了11個不同的
rank
值,也就得到了11組Precision和Recall值,然後AP值即定義為在這11個Recall下Precision值的平均值,其可以表征整個PR曲線下方的面積。即:
在這裡插入圖檔描述
還有另外一種插值的計算方法,即對于某個Recall值
r
,Precision取所有Recall值大于
r
中的最大值,這樣保證了PR曲線是單調遞減的,避免曲線出現搖擺。另外需要注意的一點是在2010年後計算AP值時是取了所有的資料點,而不僅僅隻是11個Recall值。我們在計算出AP之後,對所有類别求平均之後就是mAP值了,也是目前目标檢測用的最多的評判标準。
- AP50,AP60,AP70等等代表什麼意思?代表IOU門檻值分别取0.5,0.6,0.7等對應的AP值。
代碼解析
下面解析一下Faster-RCNN中對VOC資料集計算每個類别AP值的代碼,mAP就是所有類的AP值平均值。代碼來自py-faster-rcnn項目,連結見附錄。代碼解析如下:
# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Bharath Hariharan
# --------------------------------------------------------
import xml.etree.ElementTree as ET #讀取xml檔案
import os
import cPickle #序列化存儲子產品
import numpy as np
def parse_rec(filename):
""" Parse a PASCAL VOC xml file """
tree = ET.parse(filename)
objects = []
# 解析xml檔案,将GT框資訊放入一個清單
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)
return objects
# 單個計算AP的函數,輸入參數為精确率和召回率,原理見上面
def voc_ap(rec, prec, use_07_metric=False):
""" ap = voc_ap(rec, prec, [use_07_metric])
Compute VOC AP given precision and recall.
If use_07_metric is true, uses the
VOC 07 11 point method (default:False).
"""
# 如果使用2017年的計算AP的方式(插值的方式)
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:
# 使用2010年後的計算AP值的方式
# 這裡是新增一個(0,0),友善計算
mrec = np.concatenate(([0.], rec, [1.]))
mpre = np.concatenate(([0.], prec, [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
# 主函數
def voc_eval(detpath,
annopath,
imagesetfile,
classname,
cachedir,
ovthresh=0.5,
use_07_metric=False):
"""rec, prec, ap = voc_eval(detpath,
annopath,
imagesetfile,
classname,
[ovthresh],
[use_07_metric])
Top level function that does the PASCAL VOC evaluation.
detpath: 産生的txt檔案,裡面是一張圖檔的各個檢測框結果。
annopath: xml 檔案與對應的圖像相呼應。
imagesetfile: 一個txt檔案,裡面是每個圖檔的位址,每行一個位址。
classname: 種類的名字,即類别。
cachedir: 緩存标注的目錄。
[ovthresh]: IOU門檻值,預設為0.5,即mAP50。
[use_07_metric]: 是否使用2007的計算AP的方法,預設為Fasle
"""
# assumes detections are in detpath.format(classname)
# assumes annotations are in annopath.format(imagename)
# assumes imagesetfile is a text file with each line an image name
# cachedir caches the annotations in a pickle file
# 首先加載Ground Truth标注資訊。
if not os.path.isdir(cachedir):
os.mkdir(cachedir)
# 即将建立檔案的路徑
cachefile = os.path.join(cachedir, 'annots.pkl')
# 讀取文本裡的所有圖檔路徑
with open(imagesetfile, 'r') as f:
lines = f.readlines()
# 擷取檔案名,strip用來去除頭尾字元、空白符(包括\n、\r、\t、' ',即:換行、回車、制表符、空格)
imagenames = [x.strip() for x in lines]
#如果cachefile檔案不存在,則寫入
if not os.path.isfile(cachefile):
# load annots
recs = {}
for i, imagename in enumerate(imagenames):
#annopath.format(imagename): label的xml檔案所在的路徑
recs[imagename] = parse_rec(annopath.format(imagename))
if i % 100 == 0:
print 'Reading annotation for {:d}/{:d}'.format(
i + 1, len(imagenames))
# save
print 'Saving cached annotations to {:s}'.format(cachefile)
with open(cachefile, 'w') as f:
#寫入cPickle檔案裡面。寫入的是一個字典,左側為xml檔案名,右側為檔案裡面個各個參數。
cPickle.dump(recs, f)
else:
# load
with open(cachefile, 'r') as f:
recs = cPickle.load(f)
# 對每張圖檔的xml擷取函數指定類的bbox等
class_recs = {}# 儲存的是 Ground Truth的資料
npos = 0
for imagename in imagenames:
# 擷取Ground Truth每個檔案中某種類别的物體
R = [obj for obj in recs[imagename] if obj['name'] == classname]
bbox = np.array([x['bbox'] for x in R])
# different基本都為0/False
difficult = np.array([x['difficult'] for x in R]).astype(np.bool)
det = [False] * len(R)
npos = npos + sum(~difficult) #自增,~difficult取反,統計樣本個數
# # 記錄Ground Truth的内容
class_recs[imagename] = {'bbox': bbox,
'difficult': difficult,
'det': det}
# read dets 讀取某類别預測輸出
detfile = detpath.format(classname)
with open(detfile, 'r') as f:
lines = f.readlines()
splitlines = [x.strip().split(' ') for x in lines]
image_ids = [x[0] for x in splitlines] # 圖檔ID
confidence = np.array([float(x[1]) for x in splitlines]) # IOU值
BB = np.array([[float(z) for z in x[2:]] for x in splitlines]) # bounding box數值
# 對confidence的index根據值大小進行降序排列。
sorted_ind = np.argsort(-confidence)
sorted_scores = np.sort(-confidence)
#重排bbox,由大機率到小機率。
BB = BB[sorted_ind, :]
# 圖檔重排,由大機率到小機率。
image_ids = [image_ids[x] for x in sorted_ind]
# go down dets and mark TPs and FPs
nd = len(image_ids)
tp = np.zeros(nd)
fp = np.zeros(nd)
for d in range(nd):
R = class_recs[image_ids[d]]
bb = BB[d, :].astype(float)
ovmax = -np.inf
BBGT = R['bbox'].astype(float)
if BBGT.size > 0:
# compute overlaps
# intersection
ixmin = np.maximum(BBGT[:, 0], bb[0])
iymin = np.maximum(BBGT[:, 1], bb[1])
ixmax = np.minimum(BBGT[:, 2], bb[2])
iymax = np.minimum(BBGT[:, 3], bb[3])
iw = np.maximum(ixmax - ixmin + 1., 0.)
ih = np.maximum(iymax - iymin + 1., 0.)
inters = iw * ih
# union
uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +
(BBGT[:, 2] - BBGT[:, 0] + 1.) *
(BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)
overlaps = inters / uni
ovmax = np.max(overlaps)
jmax = np.argmax(overlaps)
if ovmax > ovthresh:
if not R['difficult'][jmax]:
if not R['det'][jmax]:
tp[d] = 1.
R['det'][jmax] = 1
else:
fp[d] = 1.
else:
fp[d] = 1.
# compute precision recall
fp = np.cumsum(fp)
tp = np.cumsum(tp)
rec = tp / float(npos)
# avoid divide by zero in case the first detection matches a difficult
# ground truth
prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
ap = voc_ap(rec, prec, use_07_metric)
return rec, prec, ap
複制
這個腳本可以直接調用來計算mAP值,可以看一下附錄中的最後一個連結。
附錄
- http://host.robots.ox.ac.uk/pascal/VOC/pubs/everingham15.pdf
- http://homepages.inf.ed.ac.uk/ckiw/postscript/ijcv_voc09.pdf
- 代碼連結:https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/datasets/voc_eval.py
- 在Darknet中調用上面的腳本來計算mAP值:https://blog.csdn.net/amusi1994/article/details/81564504