目标检测的评价指标(Iou,mAP,Fps)

一、交并比

   物体检测需要定位出物体的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)

发布了150 篇原创文章 · 获赞 200 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/qq_37764129/article/details/101309206