目標檢測的評價指標(Iou,mAP,Fps)

原文鏈接:https://blog.csdn.net/zl3090/article/details/82740727

一、交併比

   物體檢測需要定位出物體的bounding box,就像下面的圖片一樣,我們不僅要定位出車輛的bounding box 我們還要識別出bounding box 裏面的物體就是車輛。對於bounding box的定位精度,有一個很重要的概念,因爲我們算法不可能百分百跟人工標註的數據完全匹配,因此就存在一個定位精度評價公式:IOU。

                                                 

IOU定義了兩個bounding box的重疊度,如下圖所示:

                                                     

矩形框A、B的一個重合度IOU計算公式爲:

IOU=(A∩B)/(A∪B)

就是矩形框A、B的重疊面積佔A、B並集的面積比例:

IOU=SI/(SA+SB-SI)

二、mAP(均值平均精度)

我們使用loU看檢測是否正確需要設定一個閾值,最常用的閾值是0.5,即如果loU>0.5,則認爲是真實的檢測(true detection),否則認爲是錯誤的檢測(false detection)。我們現在計算模型得到的每個檢測框(置信度閾值後)的loU值。用計算出的loU值與設定的loU閾值(例如0.5)比較,就可以計算出每個圖像中每個類的正確檢測次數(A)。對於每個圖像,我們都有ground truth的數據(即知道每個圖像的真實目標信息),因此也知道了該圖像中給定類別的實際目標(B)的數量。我們也計算了正確預測的數量(A)(True possitive)。因此:

對於一類物體在一張圖像上的精度:

                                                      

即給定一張圖像的類別C的Precision=圖像正確預測(True Positives)的數量 / 在圖像張這一類的總的目標數量。 

對於一類物體在所有圖上的精度:
假如現在有一個給定的類,驗證集中有100個圖像,並且我們知道每個圖像都有其中的所有類(基於ground truth)。所以我們可以得到100個精度值,計算這100個精度值的平均值,得到的就是該類的平均精度。 

                                                  

即一個C類的平均精度=在驗證集上所有的圖像對於類C的精度值的和 / 有類C這個目標的所有圖像的數量。 

對於所有類物體在所有圖上的精度:
        現在假如我們整個集合中有20個類,對於每個類別,我們都先計算loU,接下來計算精度,然後計算平均精度。所有我們現在有20個不同的平均精度值。使用這些平均精度值,我們可以輕鬆的判斷任何給定類別的模型的性能。 
        但是問題是使用20個不同的平均精度使我們難以度量整個模型,所以我們可以選用一個單一的數字來表示一個模型的表現(一個度量來統一它們),我們可以取所有類的平均精度值的平均值,即MAP(均值平均精度)。 

                                                 

mAP=所有類別的平均精度求和除以所有類別,即數據集中所有類的平均精度的平均值。

使用mAP值時我們需要滿足一下條件: 
(1) mAP總是在固定的數據集上計算 
(2)它不是量化模型輸出的絕對度量,但是是一個比較好的相對度量。當我們在流行的公共數據集上計算這個度量時,這個度量可以很容易的用來比較不同目標檢測方法 
(3)根據訓練中類的分佈情況,平均精度值可能會因爲某些類別(具有良好的訓練數據)非常高(對於具有較少或較差數據的類別)而言非常低。所以我們需要mAP可能是適中的,但是模型可能對於某些類非常好,對於某些類非常不好。因此建議在分析模型結果的同時查看個各類的平均精度,這些值也可以作爲我們是不是需要添加更多訓練樣本的一個依據。

三、檢測速率(Fps)

1秒內識別的圖像數(幀數)

四、IOU的python實現

具體實現過程請移步:https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/

# import the necessary packages
from collections import namedtuple
import numpy as np
import cv2
 
# define the `Detection` object
Detection = namedtuple("Detection", ["image_path", "gt", "pred"])

def bb_intersection_over_union(boxA, boxB):
	# determine the (x, y)-coordinates of the intersection rectangle
	xA = max(boxA[0], boxB[0])
	yA = max(boxA[1], boxB[1])
	xB = min(boxA[2], boxB[2])
	yB = min(boxA[3], boxB[3])
 
	# compute the area of intersection rectangle
	interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
 
	# compute the area of both the prediction and ground-truth
	# rectangles
	boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
	boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
 
	# compute the intersection over union by taking the intersection
	# area and dividing it by the sum of prediction + ground-truth
	# areas - the interesection area
	iou = interArea / float(boxAArea + boxBArea - interArea)
 
	# return the intersection over union value
	return iou

# define the list of example detections
examples = [
	Detection("image_0002.jpg", [39, 63, 203, 112], [54, 66, 198, 114]),
	Detection("image_0016.jpg", [49, 75, 203, 125], [42, 78, 186, 126]),
	Detection("image_0075.jpg", [31, 69, 201, 125], [18, 63, 235, 135]),
	Detection("image_0090.jpg", [50, 72, 197, 121], [54, 72, 198, 120]),
	Detection("image_0120.jpg", [35, 51, 196, 110], [36, 60, 180, 108])]

# loop over the example detections
for detection in examples:
	# load the image
	image = cv2.imread(detection.image_path)
 
	# draw the ground-truth bounding box along with the predicted
	# bounding box
	cv2.rectangle(image, tuple(detection.gt[:2]), 
		tuple(detection.gt[2:]), (0, 255, 0), 2)
	cv2.rectangle(image, tuple(detection.pred[:2]), 
		tuple(detection.pred[2:]), (0, 0, 255), 2)
 
	# compute the intersection over union and display it
	iou = bb_intersection_over_union(detection.gt, detection.pred)
	cv2.putText(image, "IoU: {:.4f}".format(iou), (10, 30),
		cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
	print("{}: {:.4f}".format(detection.image_path, iou))
 
	# show the output image
	cv2.imshow("Image", image)
	cv2.waitKey(0)

結果:

                                          

參考:

【目標識別學習筆記系列】附錄一、評價指標(Iou,mAP,Fps)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章