《YOLOv5全面解析教程》四,目標(biāo)檢測模型精確度評估
??代碼倉庫地址:https://github.com/Oneflow-Inc/one-yolov5歡迎star one-yolov5項目 獲取最新的動態(tài)。如果您有問題,歡迎在倉庫給我們提出寶貴的意見。??????如果對您有幫助,歡迎來給我Star呀??~
指標(biāo)評估(重要的一些定義)
?? IOU
(Intersection Over Union) 基于Jaccard索引,用于評估兩個邊界框之間的重疊程度。它需要一個真實回歸框 (a ground truth bounding box) 和一個預(yù)測回歸框(a predicted bounding box) 計算得到。通過應(yīng)用 IOU 我們能夠判斷出預(yù)測結(jié)果是有效(True Positive) 或者 無效(False Positive)
也稱重疊度 表示計算預(yù)測回歸框和真實回歸框的交并比,計算公式如下:
其中: ,
下圖可視化了真實回歸框(綠色)和 預(yù)測回歸框(紅色)之間的IOU。
圖1.1 ; 的計算。綠色: , 紅色:
TP&FP&FN&TN
| Positive | Negative | |
|---|---|---|
| True | TP | TN |
| False | FP | FN |
指標(biāo)的一些基本概念:
TP(True Postives):分類器把正例正確的分類-預(yù)測為正例。(IOU >= 閾值) FN(False Negatives):分類器把正例錯誤的分類-預(yù)測為負(fù)例。(IOU < 閾值) FP(False Postives):分類器把負(fù)例錯誤的分類-預(yù)測為正例 TN(True Negatives):分類器把負(fù)例正確的分類-預(yù)測為負(fù)例(yolov5中沒有應(yīng)用到)
yolov5中沒有應(yīng)用TN的原因: TN代表的是所有可能的未正確檢測到的邊界框。然而在yolo在目標(biāo)檢測任務(wù)中,每個網(wǎng)格會生成很多的預(yù)測邊界框,有許多的預(yù)測邊界框是沒有相應(yīng)的真實標(biāo)簽框,導(dǎo)致未正確檢測到的邊界框數(shù)量遠(yuǎn)遠(yuǎn)大于正確檢測到的邊界框,這就是為什么不使用TN的原因。
threshold: depending on the metric, it is usually set to 50%, 75% or 95%.
Precision
Precision 定義:模型識別相關(guān)目標(biāo)的能力。分類正確的樣本在所有樣本中的數(shù)量比例,公式如下:
Recall
Recall 定義:是模型找到真實回歸框(即標(biāo)簽標(biāo)注的框)的能力。計算公式如下:
mAP
多標(biāo)簽圖像分類任務(wù)中圖片的標(biāo)簽不止一個,因此評價不能用普通單標(biāo)簽圖像分類的標(biāo)準(zhǔn),即mean accuracy,該任務(wù)采用的是和信息檢索中類似的方法—mAP,雖然其字面意思和mean average precision看起來差不多,但是計算方法要繁瑣得多,mAP 會統(tǒng)計所有 Confidence 值下的 PR值,而實際使用時,會設(shè)定一個 Confidence 閾值,低于該閾值的目標(biāo)會被丟棄,這部分目標(biāo)在統(tǒng)計 mAP 時也會有一定的貢獻(xiàn)。
Confidence(置信度):在統(tǒng)計學(xué)中,一個概率樣本的置信區(qū)間(Confidence interval)是對這個樣本的某個總體參數(shù)的區(qū)間估計。置信區(qū)間展現(xiàn)的是這個參數(shù)的真實值有一定概率落在測量結(jié)果的周圍的程度。置信區(qū)間給出的是被測量參數(shù)測量值的可信程度范圍,即前面所要求的“一定概率”。這個概率也被稱為置信水平。
(紅色曲線代表,人為的方式將PR曲線變成單調(diào)遞減,使得計算面積更容易。)AP(Average Percision):AP為平均精度,指的是所有圖片內(nèi)的具體某一類的PR曲線下的面積(橫軸為Recall,縱軸為Precision)。
AP衡量的是對一個類檢測好壞,mAP就是對多個類的檢測好壞。在多類多目標(biāo)檢測中,計算出每個類別的AP后,再除于類別總數(shù),即所有類別AP的平均值,比如有兩類,類A的AP值是0.5,類B的AP值是0.2,那么 =(0.5+0.2)/2=0.35。
MAP: 是指所有圖片內(nèi)的所有類別的AP的平均值,map越高代表模型預(yù)測精度值越高。
:是用和作為兩軸作圖后圍成的面積,表示平均,@后面的數(shù)表示判定正負(fù)樣本的 閾值,其中 @0.5表示IOU閾值取0.5。
:只以 的閥值的時候不一定就是好的模型,可能僅僅在0.5閥值表現(xiàn)的很好,在0.6,0.7...閥值表現(xiàn)的很差,為了更好地評估整體模型的準(zhǔn)確度,因此計算一個模型在各個IOU值的AP(mAP)取平均值。
方法是:計算每個分類的AP,求和再平均,得到的就是mAP,它是直接把mAP當(dāng)成AP,然后再把IOU值大于0.5的 ,以0.05的增量,到0.95,也就是以 的 的平均值當(dāng)成 ,通過 的方式得到 結(jié)果。
目標(biāo)檢測中的mAP計算
yolov5計算IOU源碼解析
源代碼地址:
https://github.com/Oneflow-Inc/one-yolov5/blob/main/utils/metrics.py#L224-L261
# 計算兩框的特定iou (DIou, DIou, CIou)
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
# Get the coordinates of bounding boxes 下面條件語句作用是:進(jìn)行坐標(biāo)轉(zhuǎn)換從而獲取yolo格式邊界框的坐標(biāo)
if xywh: # transform from xywh to xyxy
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, 1), box2.chunk(4, 1)
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
else: # x1, y1, x2, y2 = box1
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, 1)
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, 1)
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
# Intersection area 獲取兩個框相交的面積。
"""
left_line = max(b1_x1, b2_x1)
reft_line = min(b1_x2, b2_x2)
top_line = max(b1_y1, b2_y1)
bottom_line = min(b1_y2, b2_y2)
intersect = (reight_line - left_line) * (bottom_line - top_line)
"""
inter = (flow.min(b1_x2, b2_x2) - flow.max(b1_x1, b2_x1)).clamp(0) * \
(flow.min(b1_y2, b2_y2) - flow.max(b1_y1, b2_y1)).clamp(0)
# Union Area 兩個框并到面積
union = w1 * h1 + w2 * h2 - inter + eps
# IoU
iou = inter / union
if CIoU or DIoU or GIoU:
cw = flow.max(b1_x2, b2_x2) - flow.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = flow.max(b1_y2, b2_y2) - flow.min(b1_y1, b2_y1) # convex height
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2
if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pyflow.blob/master/utils/box/box_utils.py#L47
v = (4 / math.pi ** 2) * flow.pow(flow.atan(w2 / (h2 + eps)) - flow.atan(w1 / (h1 + eps)), 2)
with flow.no_grad():
alpha = v / (v - iou + (1 + eps))
return iou - (rho2 / c2 + v * alpha) # CIoU
return iou - rho2 / c2 # DIoU
c_area = cw * ch + eps # convex area
return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf
return iou # IoU
yolov5計算AP源碼逐行解析
源代碼地址:
https://github.com/Oneflow-Inc/one-yolov5/blob/main/utils/metrics.py#L96-L121
# 根據(jù)PR曲線計算AP
def compute_ap(recall, precision):
""" Compute the average precision, given the recall and precision curves
# Arguments
recall: The recall curve (list)
precision: The precision curve (list)
# Returns
Average precision, precision curve, recall curve
"""
# Append sentinel values to beginning and end 將開區(qū)間給補(bǔ)上,補(bǔ)成閉合的區(qū)間。
mrec = np.concatenate(([0.0], recall, [1.0]))
mpre = np.concatenate(([1.0], precision, [0.0]))
# Compute the precision envelope
"""
人為的把PR曲線變成單調(diào)遞減的,例如:
np.maximum(accumulate(np.array([21, 23, 18, 19, 20, 13, 12, 11]) ) => np.array([23, 23, 20, 20, 20, 13, 12, 11])
"""
mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
# Integrate area under curve
method = 'interp' # methods: 'continuous', 'interp'
if method == 'interp': # 默認(rèn)采用 interpolated-precision 曲線,
x = np.linspace(0, 1, 101) # 101-point interp (COCO)
ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
else: # 'continuous'
i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
return ap, mpre, mrec
參考文章
https://github.com/rafaelpadilla/Object-Detection-Metrics
