最近在看RCNN和微軟的SPP-net,其中涉及到Non-Maximum Suppression,論文中沒具體展開,我就研究下了代碼,這裏做一個簡單的總結,聽這個名字感覺是一個很高深的算法,其實很簡單,就是把找出score比較region,其中需要考慮不同region的一個重疊問題。
假設從一個圖像中得到了2000region proposals,通過在RCNN和SPP-net之後我們會得到2000*4096的一個特徵矩陣,然後通過N的SVM來判斷每一個region屬於N的類的scores。其中,SVM的權重矩陣大小爲4096*N,最後得到2000*N的一個score矩陣(其中,N爲類別的數量)。
Non-Maximum Suppression就是需要根據score矩陣和region的座標信息,從中找到置信度比較高的bounding box。首先,NMS計算出每一個bounding box的面積,然後根據score進行排序,把score最大的bounding box作爲隊列中。接下來,計算其餘bounding box與當前最大score與box的IoU,去除IoU大於設定的閾值的bounding box。然後重複上面的過程,直至候選bounding box爲空。最終,檢測了bounding box的過程中有兩個閾值,一個就是IoU,另一個是在過程之後,從候選的bounding box中剔除score小於閾值的bounding box。需要注意的是:Non-Maximum Suppression一次處理一個類別,如果有N個類別,Non-Maximum Suppression就需要執行N次。
源代碼:
function pick = nms(boxes, overlap)
% top = nms(boxes, overlap)
% Non-maximum suppression. (FAST VERSION)
% Greedily select high-scoring detections and skip detections
% that are significantly covered by a previously selected
% detection.
%
% NOTE: This is adapted from Pedro Felzenszwalb's version (nms.m),
% but an inner loop has been eliminated to significantly speed it
% up in the case of a large number of boxes
% Copyright (C) 2011-12 by Tomasz Malisiewicz
% All rights reserved.
%
% This file is part of the Exemplar-SVM library and is made
% available under the terms of the MIT license (see COPYING file).
% Project homepage: https://github.com/quantombone/exemplarsvm
if isempty(boxes)
pick = [];
return;
end
x1 = boxes(:,1);
y1 = boxes(:,2);
x2 = boxes(:,3);
y2 = boxes(:,4);
s = boxes(:,end);
area = (x2-x1+1) .* (y2-y1+1); %計算出每一個bounding box的面積
[vals, I] = sort(s); %根據score遞增排序
pick = s*0;
counter = 1;
while ~isempty(I)
last = length(I);
i = I(last);
pick(counter) = i; %選擇score最大bounding box加入到候選隊列
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; %計算出每一bounding box與當前score最大的box的交集面積
o = inter ./ (area(i) + area(I(1:last-1)) - inter); %IoU(intersection-over-union)
I = I(find(o<=overlap)); %找出IoU小於overlap閾值的index
end
pick = pick(1:(counter-1));