非極大值抑制NMS的作用:
- 是目標檢測框架中的后處理模塊,主要用于刪除高度冗余的bbox。
?一、NMS【參考】
非極大值抑制NMS的過程:
- 根據(jù)置信度得分進行排序;
- 選擇置信度最高的邊界框添加到最終輸出列表中,將其從原始邊界框列表中刪除;
- 計算所有邊界框的面積;
- 計算置信度最高的邊界框與其它候選框的IoU;
- 刪除IoU大于閾值的邊界框;(一般IOU取0.3~0.5)
- 重復上述過程,直至原始邊界框列表為空。
def nms(bounding_boxes
, Nt
):if len(bounding_boxes
) == 0:return [], []bboxes
= np
.array
(bounding_boxes
)x1
= bboxes
[:, 0]y1
= bboxes
[:, 1]x2
= bboxes
[:, 2]y2
= bboxes
[:, 3]scores
= bboxes
[:, 4]areas
= (x2
- x1
+ 1) * (y2
- y1
+ 1)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
* hious
= intersection
/ (areas
[index
] + areas
[order
[:-1]] - intersection
)left
= np
.where
(ious
< Nt
)order
= order
[left
]return picked_boxes
?二、soft-NMS【論文原文】
解決的問題:
soft-NMS的原理:
- 基于NMS的改進;
- 將置信度改為IoU的函數(shù):f(IoU),具有較低的值而不至于從排序列表中刪去;
def soft_nms(bboxes
, Nt
=0.3, sigma2
=0.5, score_thresh
=0.3, method
=2):res_bboxes
= deepcopy
(bboxes
)N
= bboxes
.shape
[0] indexes
= np
.array
([np
.arange
(N
)]) bboxes
= np
.concatenate
((bboxes
, indexes
.T
), axis
=1) 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
):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
= 0if 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
]]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
* hiou
= intersection
/ (areas
[i
] + areas
[pos
:] - intersection
)if method
== 1: weight
= np
.ones
(iou
.shape
)weight
[iou
> Nt
] = weight
[iou
> Nt
] - iou
[iou
> Nt
]elif method
== 2: weight
= np
.exp
(-(iou
* iou
) / sigma2
)else: weight
= np
.ones
(iou
.shape
)weight
[iou
> Nt
] = 0scores
[pos
:] = weight
* scores
[pos
:]inds
= bboxes
[:, 5][scores
> score_thresh
]keep
= inds
.astype
(int)return res_bboxes
[keep
]
?三、Softer-NMS【參考】【代碼】
解決的問題:
- 包圍框精度不夠的問題;
- 高分類得分但是定位精度不統(tǒng)一。
softer-NMS的原理:
- 基于soft-NMS的改進;
- 將大于一定重疊度閾值Nt的候選包圍框根據(jù)置信度加權平均;
- 提出了一種新的包圍框回歸的損失函數(shù)(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
= Polygon
(g
[:8].reshape
((4, 2)))p
= Polygon
(p
[:8].reshape
((4, 2)))if not g
.is_valid
or not p
.is_valid
:return 0inter
= Polygon
(g
).intersection
(Polygon
(p
)).areaunion
= g
.area
+ p
.area
- inter
if union
== 0:return 0else:return inter
/ union
def weighted_merge(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
):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
)
總結
以上是生活随笔為你收集整理的目标检测模型中NMS、soft-NMS、softer-NMS的原理、LNMS文本检测系列(python代码实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。