一、交并比IOU(Intersection-over-Union,IoU)
- 交并比
交并比越大,两个包围盒(bounding boxes)的重叠程度越高 - python实现
def iou(box1, box2):
"""Implement the intersection over union (IoU) between box1 and box2
Arguments:
box1 -- first box, list object with coordinates (x1, y1, x2, y2)
box2 -- second box, list object with coordinates (x1, y1, x2, y2)
"""
# Calculate the (y1, x1, y2, x2) coordinates of the intersection of box1 and box2. Calculate its Area
xi1 = max(box1[0], box2[0])
yi1 = max(box1[1], box2[1])
xi2 = min(box1[2], box2[2])
yi2 = min(box1[3], box2[3])
inter_area = max(0,(yi2-yi1))*max(0,(xi2-xi1))
# Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)
box1_area = (box1[3]-box1[1])*(box1[2]-box1[0])
box2_area = (box2[3]-box2[1])*(box2[2]-box2[0])
union_area = box1_area + box2_area - inter_area
# compute the IoU
iou = inter_area/union_area
return iou
二、 非最大值抑制(Non-Maximum Suppression,NMS)
-
NMS在边缘检测、人脸检测、目标检测(YOLO,SSD,Faster R-CNN)等计算机视觉视觉任务中得到了广泛应用
-
为什么要非最大值抑制?目标检测的过程中在同一目标的位置上会产生大量的候选框,这些候选框相互之间可能会有重叠,故而存在模型可能对同一个对象做出多次检测的状况。此时我们需要利用非极大值抑制找到最佳的目标边界框,消除冗余的边界框。
-
实现方法
预处理:score threshold (抛弃所有预测概率Pc小于一定阈值的输出边界框)
NMS: 对于Bounding Box的列表B及其对应的置信度S,采用下面的计算方式:选择具有最大score的检测框M,将其从B集合中移除并加入到最终的检测结果D中.通常将B中剩余检测框中与M的IoU大于阈值iou_threshold的框从B中移除.重复这个过程,直到B为空。 -
tensorflow中可以使用tf.image.non_max_suppression(boxes, scores, max_boxes_tensor, iou_threshold)函数
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
import numpy as np
def nms(dets, thresh):
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1) # 各 box 的面积
order = scores.argsort()[::-1] # 将score降序排序,并返回排序后的下标
keep = []
while order.size > 0:
i = order[0] # score 最大的 box 对应的 index
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h # 交面积
ovr = inter / (areas[i] + areas[order[1:]] - inter) # 交并比
inds = np.where(ovr <= thresh)[0] # 保留 IoU 小于设定阈值的 boxes
order = order[inds + 1]
return keep
python 中的[:-1]和[::-1] b=a[:-1] #从位置0到位置-1之前的数 b=a[::-1] 为倒序