您的位置:首页 > 其它

非极大值抑制(non-maximum suppression)的理解与实现

2017-05-05 17:40 405 查看
非极大抑制(Non-Maximum Suppression)

Non-Maximum Suppression for Object Detection in Python

RCNN 和微软提出的 SPP_net 等著名的目标检测模型,在算法具体的实施过程中,一般都会用到 non-maximum suppress(非最大值抑制,抑制即忽略, 也即忽略那些值(IoU)高于提供的阈值的) 的机制。

引入 non-maximum suppression 的目的在于:根据事先提供的 score 向量,以及 regions(由不同的 bounding boxes,矩形窗口左上和右下点的坐标构成) 的坐标信息,从中筛选出置信度较高的 bounding boxes。

其基本操作流程如下:

首先,计算每一个 bounding box 的面积:

(x1, y1) ⇒ 左上点的坐标,(x2, y2) ⇒ 右下点的坐标;

(x2-x1+1)x(y2-y1+1)

根据 scores 进行排序(一般从小到大),将 score 最大的bounding box置于队列,接下来计算其余 bounding box 与当前 score 最大的 bounding box 的 IoU,抑制(忽略也即去除)IoU大于设定阈值的 bounding box;

重复以上过程,直至候选 bounding boxes 为空;

function picked = nms(boxes, overlap_thresh)
% boxes[:, 1:4] 存储着 regions 信息,boxes[:, 5] 存储的则是 scores 向量

if isempty(boxes)
picked = [];
return;
end

x1 = boxes[:, 1];
y1 = boxes[:, 2];           % (x1, y1) ⇒ bounding boxes 左上点坐标的集合
x2 = boxes[:, 3];
y2 = boxes[:, 4];           % (x2, y2) ⇒ bounding boxes 右下点坐标的集合
s = boxes[:, 5];            % scores 向量

areas = (x2-x1+1).*(y2-y1+1);   % 各个 bounding boxes 的面积
[vals, idx] = sort(s);      % 默认从小到大排序

while ~isempty(idx)
last = length(idx);
i = idx(last);
picked = [picked, i];

xx1 = max(x1(i), x1(1:last-1));
yy1 = max(y1(i), y1(1:last-1));
xx2 = min(x2(i), x2(1:last-1));
yy2 = min(y2(i), y2(1:last-1));

h = max(0, yy2-yy1+1);
w = max(0, xx2-xx1+1);

inter = w .* h;
iou = inter ./ (areas(i) + areas(1:last-1)-inter);

I = I(iou <= overlap_thresh);
end

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: