深度学习目标检测Object detection中非极大值抑制(NMS和soft-NMS)

非极大值抑制(NMS)是对目标检测中区域提取网络和边界回归网络候选区域的筛选过程。由于图像中的目标具有多个候选的边界框(bounding box),我们的任务是要选取置信度(confident socre)最高的候选边界框,同时尽量降低对同时存在的同一类别其他物体的影响。
非极大值抑制的实现方法1:TensorFlow和Keras自带封装接口,在github上点赞最高的Mask RCNN用到

import numpy as np
import tensorflow as tf
from keras import backend as K
#boxes = np.array([[1,2,3,4],[1,3,3,4],[1,3,4,4],[1,1,4,4],[1,1,3,4]],dtype=np.float32)
#scores = np.array([0.4,0.5,0.72,0.9,0.45],dtype=np.float32)
boxes = np.array([[250, 250, 420, 420],
                  [220, 220, 320, 330],
                  [100, 100, 210, 210],
                  [240, 230, 330, 325],
                  [230, 220, 340, 315]], dtype=np.float32)
scores = np.array([0.8,0.92,0.72,0.81,0.9],dtype=np.float32)
with tf.Session() as sess:
    selected_indices = sess.run(tf.image.non_max_suppression(boxes=boxes,scores=scores,iou_threshold=0.5,max_output_size=5))
    print(selected_indices)
    selected_boxes=sess.run(K.gather(boxes,selected_indices))
    print(selected_boxes)

最后的结果为
[1 0 2]
[[220. 220. 320. 330.]
[250. 250. 420. 420.]
[100. 100. 210. 210.]]
非极大值抑制的实现方法2,使用python构建函数:

def py_greedy_nms(dets, iou_thr):
    """Pure python implementation of traditional greedy NMS.
    Args:
        dets (numpy.array): Detection results with shape `(num, 5)`,
            data in second dimension are [x1, y1, x2, y2, score] respectively.
        iou_thr (float): Drop the boxes that overlap with current
            maximum > thresh.
    Returns:
        numpy.array: Retained boxes.
    """
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    sorted_idx = scores.argsort()[::-1]

    keep = []
    while sorted_idx.size > 0:
        i = sorted_idx[0]
        keep.append(i)

        xx1 = np.maximum(x1[i], x1[sorted_idx[1:]])
        yy1 = np.maximum(y1[i], y1[sorted_idx[1:]])
        xx2 = np.minimum(x2[i], x2[sorted_idx[1:]])
        yy2 = np.minimum(y2[i], y2[sorted_idx[1:]])

        w = np.maximum(xx2 - xx1 + 1,
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值