日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/14 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 目标检测模型中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-1bboxes = 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 + 1if 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 calculatexx1 = 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 * hiou = intersection / (areas[i] + areas[pos:] - intersection)# Three methods: 1.linear 2.gaussian 3.original NMSif method == 1: # linearweight = np.ones(iou.shape)weight[iou > Nt] = weight[iou > Nt] - iou[iou > Nt]elif method == 2: # gaussianweight = np.exp(-(iou * iou) / sigma2)else: # original NMSweight = np.ones(iou.shape)weight[iou > Nt] = 0scores[pos:] = weight * scores[pos:]# select the boxes and keep the corresponding indexesinds = 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 Polygondef 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)).areaunion = g.area + p.area - interif union == 0:return 0else:return inter / uniondef 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 gdef standard_nms(S, thres):# 標準NMSorder = 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 = gif p is not None:S.append(p)if len(S) == 0:return np.array([])return standard_nms(np.array(S), thres)

總結

以上是生活随笔為你收集整理的目标检测模型中NMS、soft-NMS、softer-NMS的原理、LNMS文本检测系列(python代码实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。