目标检测模型中NMS、soft-NMS、softer-NMS的原理、LNMS文本检测系列(python代码实现)

非极大值抑制NMS的作用:
  • 是目标检测框架中的后处理模块,主要用于删除高度冗余的bbox。

?一、NMS【参考

非极大值抑制NMS的过程:
  • 根据置信度得分进行排序;
  • 选择置信度最高的边界框添加到最终输出列表中,将其从原始边界框列表中删除;
  • 计算所有边界框的面积;
  • 计算置信度最高的边界框与其它候选框的IoU;
  • 删除IoU大于阈值的边界框;(一般IOU取0.3~0.5)
  • 重复上述过程,直至原始边界框列表为空。
def nms(bounding_boxes, Nt):
   if len(bounding_boxes) == 0:
       return [], []
   bboxes = np.array(bounding_boxes)

   # 计算 n 个候选框的面积大小
   x1 = bboxes[:, 0]
   y1 = bboxes[:, 1]
   x2 = bboxes[:, 2]
   y2 = bboxes[:, 3]
   scores = bboxes[:, 4]
   areas = (x2 - x1 + 1) * (y2 - y1 + 1)

   # 对置信度进行排序, 获取排序后的下标序号, argsort 默认从小到大排序
   order = np.argsort(scores)

   picked_boxes = []  # 返回值
   while order.size > 0:
       # 将当前置信度最大的框加入返回值列表中
       index = order[-1]
       picked_boxes.append(bounding_boxes[index])

       # 获取当前置信度最大的候选框与其他任意候选框的相交面积
       x11 = np.maximum(x1[index], x1[order[:-1]])
       y11 = np.maximum(y1[index], y1[order[:-1]])
       x22 = np.minimum(x2[index], x2[order[:-1]])
       y22 = np.minimum(y2[index], y2[order[:-1]])
       w = np.maximum(0.0, x22 - x11 + 1)
       h = np.maximum(0.0, y22 - y11 + 1)
       intersection = w * h

       # 利用相交的面积和两个框自身的面积计算框的交并比, 将交并比大于阈值的框删除
       ious = intersection / (areas[index] + areas[order[:-1]] - intersection)
       left = np.where(ious < Nt)
       order = order[left]
   return picked_boxes

?二、soft-NMS【论文原文

解决的问题:
  • 物体重合度较大的情况;
soft-NMS的原理:
  • 基于NMS的改进;
  • 将置信度改为IoU的函数:f(IoU),具有较低的值而不至于从排序列表中删去
def soft_nms(bboxes, Nt=0.3, sigma2=0.5, score_thresh=0.3, method=2):
   # 在 bboxes 之后添加对于的下标[0, 1, 2...], 最终 bboxes 的 shape 为 [n, 5], 前四个为坐标, 后一个为下标
   res_bboxes = deepcopy(bboxes)
   N = bboxes.shape[0]  # 总的 box 的数量
   indexes = np.array([np.arange(N)])  # 下标: 0, 1, 2, ..., n-1
   bboxes = np.concatenate((bboxes, indexes.T), axis=1)  # concatenate 之后, bboxes 的操作不会对外部变量产生影响
   # 计算每个 box 的面积
   x1 = bboxes[:, 0]
   y1 = bboxes[:, 1]
   x2 = bboxes[:, 2]
   y2 = bboxes[:, 3]
   scores = bboxes[:, 4]
   areas = (x2 - x1 + 1) * (y2 - y1 + 1)

   for i in range(N):
       # 找出 i 后面的最大 score 及其下标
       pos = i + 1
       if i != N - 1:
           maxscore = np.max(scores[pos:], axis=0)
           maxpos = np.argmax(scores[pos:], axis=0)
       else:
           maxscore = scores[-1]
           maxpos = 0
       # 如果当前 i 的得分小于后面的最大 score, 则与之交换, 确保 i 上的 score 最大
       if scores[i] < maxscore:
           bboxes[[i, maxpos + i + 1]] = bboxes[[maxpos + i + 1, i]]
           scores[[i, maxpos + i + 1]] = scores[[maxpos + i + 1, i]]
           areas[[i, maxpos + i + 1]] = areas[[maxpos + i + 1, i]]
       # IoU calculate
       xx1 = np.maximum(bboxes[i, 0], bboxes[pos:, 0])
       yy1 = np.maximum(bboxes[i, 1], bboxes[pos:, 1])
       xx2 = np.minimum(bboxes[i, 2], bboxes[pos:, 2])
       yy2 = np.minimum(bboxes[i, 3], bboxes[pos:, 3])
       w = np.maximum(0.0, xx2 - xx1 + 1)
       h = np.maximum(0.0, yy2 - yy1 + 1)
       intersection = w * h
       iou = intersection / (areas[i] + areas[pos:] - intersection)
       # Three methods: 1.linear 2.gaussian 3.original NMS
       if method == 1:  # linear
           weight = np.ones(iou.shape)
           weight[iou > Nt] = weight[iou > Nt] - iou[iou > Nt]
       elif method == 2:  # gaussian
           weight = np.exp(-(iou * iou) / sigma2)
       else:  # original NMS
           weight = np.ones(iou.shape)
           weight[iou > Nt] = 0
       scores[pos:] = weight * scores[pos:]
   # select the boxes and keep the corresponding indexes
   inds = bboxes[:, 5][scores > score_thresh]
   keep = inds.astype(int)
   return res_bboxes[keep]

?三、Softer-NMS【参考】【代码

解决的问题:
  • 包围框精度不够的问题;
  • 高分类得分但是定位精度不统一。
softer-NMS的原理:
  • 基于soft-NMS的改进;
  • 将大于一定重叠度阈值Nt的候选包围框根据置信度加权平均
  • 提出了一种新的包围框回归的损失函数(KL Loss),用来同时学习包围框变换和定位置信度。

?四、Locality-Aware NMS文本系列【参考

基本步骤:
  • 1.先对所有的output box集合结合相应的阈值(大于阈值则进行合并,小于阈值则不和并),依次遍历进行加权合并,得到合并后的bbox集合;
  • 2.对合并后的bbox集合进行标准的NMS操作。
import numpy as np
from shapely.geometry import Polygon

def intersection(g, p):
   # 取g,p中的几何体信息组成多边形
   g = Polygon(g[:8].reshape((4, 2)))
   p = Polygon(p[:8].reshape((4, 2)))
   # 判断g,p是否为有效的多边形几何体
   if not g.is_valid or not p.is_valid:
       return 0
   # 取两个几何体的交集和并集
   inter = Polygon(g).intersection(Polygon(p)).area
   union = g.area + p.area - inter
   if union == 0:
       return 0
   else:
       return inter / union

def weighted_merge(g, p):
   # 取g,p两个几何体的加权(权重根据对应的检测得分计算得到)
   g[:8] = (g[8] * g[:8] + p[8] * p[:8]) / (g[8] + p[8])
   # 合并后的几何体的得分为两个几何体得分的总和
   g[8] = (g[8] + p[8])
   return g

def standard_nms(S, thres):
   # 标准NMS
   order = np.argsort(S[:, 8])[::-1]
   keep = []
   while order.size > 0:
       i = order[0]
       keep.append(i)
       ovr = np.array([intersection(S[i], S[t]) for t in order[1:]])
       inds = np.where(ovr <= thres)[0]
       order = order[inds + 1]
   return S[keep]


def nms_locality(polys, thres=0.3):
   '''
  locality aware nms of EAST
  :param polys: a N*9 numpy array. first 8 coordinates, then prob
  :return: boxes after nms
  '''
   S = []  # 合并后的几何体集合
   p = None  # 合并后的几何体
   for g in polys:
       if p is not None and intersection(g, p) > thres:  # 若两个几何体的相交面积大于指定的阈值,则进行合并
           p = weighted_merge(g, p)
       else:  # 反之,则保留当前的几何体
           if p is not None:
               S.append(p)
           p = g
   if p is not None:
       S.append(p)
   if len(S) == 0:
       return np.array([])
   return standard_nms(np.array(S), thres)
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值