NMS (Non-Maximum Suppression) 非極大值抑制

參考博文:https://blog.csdn.net/shuzfan/article/details/52711706

                  https://blog.csdn.net/leviopku/article/details/80886386

Non-Maximum Suppression

翻譯:非極大值抑制

說人話:不是極大值的不要

作用:在目標檢測中去掉多餘的候選框(candidate boxes)

           = 候選框三千,只取一個(分最高的)

過程:迭代-遍歷-消除

1、所有boxes按照foreground score排序,選中得分最高的框(圖中紅色,foreground score:0.98)

è¿éåå¾çæè¿°

2、遍歷其餘的框,如果和當前最高分框的重疊面積(IOU)大於一定閾值,我們就將框刪除

è¿éåå¾çæè¿°

 3、從未處理的框中繼續選一個得分最高的,重複上述過程。

è¿éåå¾çæè¿°

 

代碼



# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

import numpy as np


def py_cpu_nms(dets, thresh):
    '''
    :param dets: [x1 y1 x2 y2 score]
    :param thresh: IOU閾值
    :return:
    '''
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)  # 面積,這裏的+1是因爲計算像素點個數,如3-0=3,但0到3是4個數
    order = scores.argsort()[::-1]  # foreground score大小順序

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i) # 保留得分最大的box
        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:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / (areas[i] + areas[order[1:]] - inter)  # IoU

        inds = np.where(ovr <= thresh)[0]  # 消除掉IOU大於閾值的box,只留小於閾值的box
        order = order[inds + 1]  # 繼續下一輪

    return keep

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章