如何判断对象检测算法运作良好呢?

一、交并比(Intersection over union,IoU)

是产生的候选框(candidate bound)与原标记框(ground truth bound)的交叠率,即它们的交集与并集的比值,理想情况下是完全重叠,即比值为1

一般约定,在计算机检测任务中,如果IoU≥0.5,就说检测正确。当然0.5只是约定阈值,你可以将IoU的阈值定的更高。IoU越高,边界框越精确。

二、非极大值抑制(Non-Maximum Suppression,NMS)

非极大值抑制,顾名思义就是抑制不是极大值的元素,可以理解为局部最大搜索。主要是用于目标检测中提取分数最高的窗口,就是利用非极大值抑制找到最佳的目标边界框,消除冗余的边界框。例如在行人检测中,滑动窗口经提取特征,经分类器识别后,每个窗口都会得到一个分数。但是滑动窗口会导致很多窗口与其他窗口存在包含或者大部分交叉的情况。这是就需要用的NMS来选取哪些领域里分数最高(是行人概率最大),并且抑制哪些分数低的窗口。

非极大值抑制的流程如下:

  • 根据置信度得分进行排序

  • 选择置信度最高的边界框添加到最终的输出列表中,将其从边界框列表中删除
  • 计算所有边界框的面积
  • 计算置信度最高的边界框与其它候选框的IoU
  • 删除IoU大于阈值的边界框
  • 重复上述过程,直至边界框列表为空

解释如下:

如果我们在一幅图片中定位一辆汽车,最后算法找出了一堆的矩形框,我们需要判断哪些矩形框是没用的。非极大值一直的方法是:先假设有6个矩形框,根据分类器的类别分类概率做排序,假设从小到大属于车辆的概率分别是A、B、C、D、E、F。

(1)从最大概率矩形框F开始,分别判断A~E与F的重叠度IoU是否大于某个设定阈值;

(2)假设B、D与F的重叠度超过阈值,那么就扔掉B、D;并标记第一个矩形框F,是我们保留下来的

(3)从剩下的矩形框A、C、E中,选择概率最大的E,然后判断E与A、C的重叠度,重叠度大于一定的阈值,那么就扔掉;并标记E是我们保留下来的第二个矩形框

重复上述过程,找到所有被保留下来的矩形框

三、代码实现

import cv2
import numpy as np
def nms(bounding_boxes, confidence_score, threshold):
# If no bounding boxes, return empty list
if len(bounding_boxes) == 0:
return [], [] # Bounding boxes
boxes = np.array(bounding_boxes) # boxes的位置
start_x = boxes[:, 0]
start_y = boxes[:, 1]
end_x = boxes[:, 2]
end_y = boxes[:, 3] # 每个box的置信度得分
score = np.array(confidence_score) # 记录保留下的box
picked_boxes = []
picked_score = [] # 计算每个box的面积
areas = (end_x - start_x + 1) * (end_y - start_y + 1) # boxes按照score排序
order = np.argsort(score) # Iterate bounding boxes
while order.size > 0:
# score最大的box对应的index
index = order[-1] # 保留score最大的box的index和score
picked_boxes.append(bounding_boxes[index])
picked_score.append(confidence_score[index]) # 计算剩余boxes与当前box的重叠程度IoU
x1 = np.maximum(start_x[index], start_x[order[:-1]])
x2 = np.minimum(end_x[index], end_x[order[:-1]])
y1 = np.maximum(start_y[index], start_y[order[:-1]])
y2 = np.minimum(end_y[index], end_y[order[:-1]]) # 计算IoU
w = np.maximum(0.0, x2 - x1 + 1)
h = np.maximum(0.0, y2 - y1 + 1)
intersection = w * h # 保留IoU小于设定阈值的boxes
ratio = intersection / (areas[index] + areas[order[:-1]] - intersection) left = np.where(ratio < threshold)
order = order[left] return picked_boxes, picked_score
# Image name
image_name = 'nms.jpg' # 三个Bounding box
bounding_boxes = [(187, 82, 337, 317), (150, 67, 305, 282), (246, 121, 368, 304)]
confidence_score = [0.9, 0.75, 0.8] # Read image
image = cv2.imread(image_name) # 复制原图
org = image.copy() # #绘制参数
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
thickness = 2 # IoU阈值
threshold = 0.4 # Draw bounding boxes and confidence score
for (start_x, start_y, end_x, end_y), confidence in zip(bounding_boxes, confidence_score):
(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)
cv2.rectangle(org, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)
cv2.rectangle(org, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)
cv2.putText(org, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness) # Run non-max suppression algorithm
picked_boxes, picked_score = nms(bounding_boxes, confidence_score, threshold) # Draw bounding boxes and confidence score after non-maximum supression
for (start_x, start_y, end_x, end_y), confidence in zip(picked_boxes, picked_score):
(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)
cv2.rectangle(image, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)
cv2.rectangle(image, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)
cv2.putText(image, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness) # Show image
cv2.imshow('Original', org)
cv2.imshow('NMS', image)
cv2.waitKey(0)

实验结果:

1、阈值为0.6

threshold = 0.6

2、阈值为0.5

threshold = 0.5

3、阈值为0.4

threshold = 0.4

参考文章:

https://www.cnblogs.com/makefile/p/nms.html

https://www.jianshu.com/p/d452b5615850

https://blog.csdn.net/mdjxy63/article/details/79343733

最新文章

  1. 微信支付(20140923更新)商户支付密钥key的生成与设置
  2. Web 开发精华文章集锦(jQuery、HTML5、CSS3)【系列二十七】
  3. 【BZOJ-1911】特别行动队 DP + 斜率优化
  4. crawler: 爬虫的基本结构
  5. linux 下 修改mysql账号密码
  6. spring3-hibernate3整合
  7. echarts简单使用案例
  8. 七牛上传Qt版本
  9. Class.forName()数据库驱动
  10. Learning JavaScript Design Patterns The Module Pattern
  11. bzoj3713 [PA2014]Iloczyn|暴力(模拟)
  12. 【福利】十一起,小冰科技所有UWP产品免费半个月
  13. UVA 10622 Perfect P-th Powers
  14. Django入门三之urls.py重构及参数传递
  15. select case when与IF的用法
  16. 阿里启动新项目:Nacos,比 Eureka 更强!
  17. poj 3348
  18. redis进程守护脚本
  19. FASTQ 数据质量统计工具
  20. SQL 2008登录的域账户与数据库服务器不再同一个域的 处理方法

热门文章

  1. mysql数据库卸载和安装
  2. openGL之坐标变换
  3. vue2+axios在不同的环境打包不同的接口地址
  4. ADO中最重要的对象有三个:Connection、Recordset和Command
  5. ASP.NET WEBAPI 使用Swagger生成API文档
  6. SharePoint 2013 错误 0x800700DF 文件大小超出允许的限制,无法保存
  7. POJ3580 SuperMemo splay伸展树,区间操作
  8. pppd[15863]: Terminating on signal 15
  9. nginx多虚拟主机优先级location匹配规则及tryfiles的使用
  10. bootstrap排列顺序