结合faster-rcnn给出的py_cpu_nms.py的源码来介绍一下nms算法在物体检测方面的应用。faster-rcnn中经过rpn层之后会得到一些boundingbox和boundingbox对应的属于某一类的分数(置信度)。所以可以根据NMS来去除那些overlap值比较大的box。
import numpy as np
def py_cpu_nms(dets, thresh):
"""Pure Python NMS baseline."""
#dets:N*M,N是bbox的个数,M的前4位是对应的(x1,y1,x2,y2),第5位是对应的分数
#thresh:0.3,0.5....
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)#求每个bbox的面积
order = scores.argsort()[::-1]#对分数进行倒排序
keep = []#用来保存最后留下来的bbox
while order.size > 0:
i = order[0]#无条件保留每次迭代中置信度最高的bbox
keep.append(i)
#计算置信度最高的bbox和其他剩下bbox之间的交叉区域
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:]])
#计算置信度高的bbox和其他剩下bbox之间交叉区域的面积
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
#求交叉区域的面积占两者(置信度高的bbox和其他bbox)面积和的必烈
ovr = inter / (areas[i] + areas[order[1:]] - inter)
#保留ovr小于thresh的bbox,进入下一次迭代。
inds = np.where(ovr <= thresh)[0]
#因为ovr中的索引不包括order[0]所以要向后移动一位
order = order[inds + 1]
return keep
非极大抑制,顾名思义就是把非极大值过滤掉(抑制)。下面我就R-CNN或者SPP_net中的matlab源码来进行解释。
function picks = nms_multiclass(boxes, overlap)
%%boxes为一个m*n的矩阵,其中m为boundingbox的个数,n的前4列为每个boundingbox的坐标,格式为
%%(x1,y1,x2,y2);第5:n列为每一类的置信度。overlap为设定值,0.3,0.5 .....
x1 = boxes(:,1);%所有boundingbox的x1坐标
y1 = boxes(:,2);%所有boundingbox的y1坐标
x2 = boxes(:,3);%所有boundingbox的x2坐标
y2 = boxes(:,4);%所有boundingbox的y2坐标
area = (x2-x1+1) .* (y2-y1+1); %每个%所有boundingbox的面积
picks = cell(size(boxes, 2)-4, 1);%为每一类预定义一个将要保留的cell
for iS = 5:size(boxes, 2)%每一类单独进行
s = boxes(:,iS);
[~, I] = sort(s);%置信度从低到高排序
pick = s*0;
counter = 1;
while ~isempty(I)
last = length(I);
i = I(last);
pick(counter) = i;%无条件保留每类得分最高的boundingbox
counter = counter + 1;
xx1 = max(x1(i), x1(I(1:last-1)));
yy1 = max(y1(i), y1(I(1:last-1)));
xx2 = min(x2(i), x2(I(1:last-1)));
yy2 = min(y2(i), y2(I(1:last-1)));
w = max(0.0, xx2-xx1+1);
h = max(0.0, yy2-yy1+1);
inter = w.*h;
o = inter ./ (area(i) + area(I(1:last-1)) - inter);%计算得分最高的那个boundingbox和其余的boundingbox的交集面积
I = I(o<=overlap);%保留交集小于一定阈值的boundingbox
end
pick = pick(1:(counter-1));
picks{iS-4} = pick;%保留每一类的boundingbox
end
np.max:(a, axis=None, out=None, keepdims=False)
求序列的最值
最少接收一个参数
axis:默认为列向(也即 axis=0),axis = 1 时为行方向的最值;
np.maximum:(X, Y, out=None)
X 与 Y 逐位比较取其大者;
最少接收两个参数