非极大抑制(Non-Maximum Suppression)

来源:互联网 发布:程序员的日常工作内容 编辑:程序博客网 时间:2024/05/19 18:39

转自:http://blog.csdn.net/danieljianfeng/article/details/43084875

最近在看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次。


源代码:
[plain] view plain copy
  1. function pick = nms(boxes, overlap)  
  2. % top = nms(boxes, overlap)  
  3. % Non-maximum suppression. (FAST VERSION)  
  4. % Greedily select high-scoring detections and skip detections  
  5. % that are significantly covered by a previously selected  
  6. % detection.  
  7. %  
  8. % NOTE: This is adapted from Pedro Felzenszwalb's version (nms.m),  
  9. % but an inner loop has been eliminated to significantly speed it  
  10. % up in the case of a large number of boxes  
  11.   
  12.   
  13. % Copyright (C) 2011-12 by Tomasz Malisiewicz  
  14. % All rights reserved.  
  15. %   
  16. % This file is part of the Exemplar-SVM library and is made  
  17. % available under the terms of the MIT license (see COPYING file).  
  18. % Project homepage: https://github.com/quantombone/exemplarsvm  
  19.   
  20.   
  21.   
  22.   
  23. if isempty(boxes)  
  24.   pick = [];  
  25.   return;  
  26. end  
  27.   
  28.   
  29. x1 = boxes(:,1);  
  30. y1 = boxes(:,2);  
  31. x2 = boxes(:,3);  
  32. y2 = boxes(:,4);  
  33. s = boxes(:,end);  
  34.   
  35.   
  36. area = (x2-x1+1) .* (y2-y1+1);    %计算出每一个bounding box的面积  
  37. [vals, I] = sort(s);                %根据score递增排序  
  38.   
  39.   
  40. pick = s*0;  
  41. counter = 1;  
  42. while ~isempty(I)  
  43.   last = length(I);  
  44.   i = I(last);    
  45.   pick(counter) = i;            %选择score最大bounding box加入到候选队列  
  46.   counter = counter + 1;  
  47.     
  48.   xx1 = max(x1(i), x1(I(1:last-1)));  
  49.   yy1 = max(y1(i), y1(I(1:last-1)));  
  50.   xx2 = min(x2(i), x2(I(1:last-1)));  
  51.   yy2 = min(y2(i), y2(I(1:last-1)));  
  52.     
  53.   w = max(0.0, xx2-xx1+1);  
  54.   h = max(0.0, yy2-yy1+1);  
  55.     
  56.   inter = w.*h;     %计算出每一bounding box与当前score最大的box的交集面积  
  57.   o = inter ./ (area(i) + area(I(1:last-1)) - inter);  %IoU(intersection-over-union)  
  58.     
  59.   I = I(find(o<=overlap));  %找出IoU小于overlap阈值的index  
  60. end  
  61.   
  62.   
  63. pick = pick(1:(counter-1));  

阅读全文
0 0