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

 

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