NMS的各種實(shí)現(xiàn)和速度對(duì)比

非極大值抑制(NMS)的幾種實(shí)現(xiàn)

非極大值抑制(Non-Maximum Suppression,NMS) : python 代碼實(shí)現(xiàn)和詳細(xì)講解

用matplotlib.pyplot 畫 bounding box:
http://m.itdecent.cn/p/8d14238d402a

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May  7 21:45:37 2018

@author: lps
"""
import numpy as np
import os
os.environ["DISPLAY"] = 'localhost:11.0'

boxes=np.array([[100,100,210,210,0.72],
        [250,250,420,420,0.8],
        [220,220,320,330,0.92],
        [100,100,210,210,0.72],
        [230,240,325,330,0.81],
        [220,230,315,340,0.9]]) 


def py_cpu_nms(dets, thresh):
    # dets:(m,5)  thresh:scaler
    
    x1 = dets[:,0]
    y1 = dets[:,1]
    x2 = dets[:,2]
    y2 = dets[:,3]
    
    areas = (y2-y1+1) * (x2-x1+1)
    scores = dets[:,4]
    keep = []
    
    index = scores.argsort()[::-1]  # 這里面存儲(chǔ)著 分?jǐn)?shù)按照降序排列的 所有框的 索引
    
    while index.size >0:

        i = index[0]       # every time the first is the biggst, and add it directly
        keep.append(i)
        
        x11 = np.maximum(x1[i], x1[index[1:]]) # 左上找盡量右下    # calculate the points of overlap 
        y11 = np.maximum(y1[i], y1[index[1:]])
        x22 = np.minimum(x2[i], x2[index[1:]]) # 右下找盡量左上 
        y22 = np.minimum(y2[i], y2[index[1:]])
        # 以[x11, y11, x22,y22] 構(gòu)成的矩形是與 最高分?jǐn)?shù)相交的矩形

        w = np.maximum(0, x22-x11+1) # 這里可能是負(fù)的(兩個(gè)矩形不相交),負(fù)的這里取寬度為0    # the weights of overlap
        h = np.maximum(0, y22-y11+1)                                                     # the height of overlap
       
        overlaps = w*h
        
        ious = overlaps / (areas[i]+areas[index[1:]] - overlaps)
        
        idx = np.where(ious<=thresh)[0]  # 如果他們的iou小于閾值,說(shuō)明他們重合的很少,重合的很少的這些框是框的其他目標(biāo)。
        # 選出重合較少的框(也就是去掉了框中同一個(gè)目標(biāo),但是分?jǐn)?shù)比較低的框)。
        # 從剩下的里面選擇第0位置,也就是剩下的里面選擇評(píng)分最高的框,進(jìn)行下次 去iou大的框操作。


        index = index[idx+1]   # because index start from 1
        
    return keep
        

import matplotlib.pyplot as plt
def plot_bbox(dets, c='k'):
    
    x1 = dets[:,0]
    y1 = dets[:,1]
    x2 = dets[:,2]
    y2 = dets[:,3]
    
    
    plt.plot([x1,x2], [y1,y1], c) # plot([起點(diǎn)橫坐標(biāo),終點(diǎn)橫坐標(biāo)],[起點(diǎn)縱左邊, 終點(diǎn)縱坐標(biāo)])
    plt.plot([x1,x1], [y1,y2], c)
    plt.plot([x1,x2], [y2,y2], c)
    plt.plot([x2,x2], [y1,y2], c)
    plt.title("after nms")
    
plt.subplot(121)
plot_bbox(boxes,'k')   # before nms

plt.subplot(122)
keep = py_cpu_nms(boxes, thresh=0.7)
plot_bbox(boxes[keep], 'r')# after nms
plt.show()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容