非极大值抑制(NMS)是对目标检测中区域提取网络和边界回归网络候选区域的筛选过程。由于图像中的目标具有多个候选的边界框(bounding box),我们的任务是要选取置信度(confident socre)最高的候选边界框,同时尽量降低对同时存在的同一类别其他物体的影响。
非极大值抑制的实现方法1:TensorFlow和Keras自带封装接口,在github上点赞最高的Mask RCNN用到
import numpy as np
import tensorflow as tf
from keras import backend as K
#boxes = np.array([[1,2,3,4],[1,3,3,4],[1,3,4,4],[1,1,4,4],[1,1,3,4]],dtype=np.float32)
#scores = np.array([0.4,0.5,0.72,0.9,0.45],dtype=np.float32)
boxes = np.array([[250, 250, 420, 420],
[220, 220, 320, 330],
[100, 100, 210, 210],
[240, 230, 330, 325],
[230, 220, 340, 315]], dtype=np.float32)
scores = np.array([0.8,0.92,0.72,0.81,0.9],dtype=np.float32)
with tf.Session() as sess:
selected_indices = sess.run(tf.image.non_max_suppression(boxes=boxes,scores=scores,iou_threshold=0.5,max_output_size=5))
print(selected_indices)
selected_boxes=sess.run(K.gather(boxes,selected_indices))
print(selected_boxes)
最后的结果为
[1 0 2]
[[220. 220. 320. 330.]
[250. 250. 420. 420.]
[100. 100. 210. 210.]]
非极大值抑制的实现方法2,使用python构建函数:
def py_greedy_nms(dets, iou_thr):
"""Pure python implementation of traditional greedy NMS.
Args:
dets (numpy.array): Detection results with shape `(num, 5)`,
data in second dimension are [x1, y1, x2, y2, score] respectively.
iou_thr (float): Drop the boxes that overlap with current
maximum > thresh.
Returns:
numpy.array: Retained boxes.
"""
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
sorted_idx = scores.argsort()[::-1]
keep = []
while sorted_idx.size > 0:
i = sorted_idx[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[sorted_idx[1:]])
yy1 = np.maximum(y1[i], y1[sorted_idx[1:]])
xx2 = np.minimum(x2[i], x2[sorted_idx[1:]])
yy2 = np.minimum(y2[i], y2[sorted_idx[1:]])
w = np.maximum(xx2 - xx1 + 1,