yolov11 Soft-NMS
时间: 2025-02-21 14:21:41 浏览: 131
### YOLOv11 中 Soft-NMS 实现与应用
在目标检测领域,NMS (Non-Maximum Suppression) 是一种用于消除冗余边界框的技术。传统 NMS 方法通过设定阈值来过滤重叠度较高的候选框。然而,在某些情况下,这种方法可能会丢失一些重要的信息。
Soft-NMS 提供了一种更平滑的方式处理这些情况。相比于传统的硬截断方式,Soft-NMS 使用衰减因子逐渐降低低置信度得分的权重[^3]。具体来说:
对于每一个预测框 \(B_i\) 和其对应的分类分数 \(S_i\) ,当遇到另一个与其有较大交并比(IoU) 的更高分预测框时,不是简单地丢弃该框,而是按照一定规则调整其分数:
\[ S'_i = \frac{S_i}{(1 + e^{w * IoU(B_i, B_j)})} \]
其中 \( w \) 控制着惩罚力度大小。这种机制允许保留更多潜在的有效检测结果,尤其适用于密集场景下的物体识别任务。
以下是 Python 版本的 Soft-NMS 实现代码片段:
```python
def soft_nms(dets, sigma=0.5, Nt=0.3, threshold=0.001, method=1):
"""
Build a soft NMS algorithm.
Args:
dets: detections with shape (num_detections, 5), where each row is [x1, y1, x2, y2, score].
sigma: variance of Gaussian decay function.
Nt: IOU threshold value for matching.
threshold: minimum score threshold to keep the box.
method: either linear or gaussian weight applied on scores.
Returns:
The indices list after applying nms.
"""
# Initialize output variables
indexes = np.argsort(dets[:, 4])
suppressed = np.zeros(len(indexes), dtype=np.int)
while len(indexes) > 0:
idx = indexes[-1]
suppressed[idx] = 1
pos = len(indexes)-1
iou_scores = compute_iou(dets[idx], dets[indexes[:pos]])
if method == 1: # Linear
wt = np.ones_like(iou_scores)
ind = iou_scores > Nt
wt[ind] = 1 - iou_scores[ind]
elif method == 2: # Gaussian
wt = np.exp(-iou_scores ** 2 / sigma)
dets[indexes[:pos], 4] *= wt
inds = np.where((suppressed[indexes[:pos]] == 0))[0]
indexes = indexes[inds]
keep = np.where(suppressed)[0]
return keep[dets[keep, 4].argsort()[::-1]]
```
此算法实现了两种不同形式的软化策略——线性和高斯型,并提供了灵活配置参数的能力以适应不同的应用场景需求。
阅读全文
相关推荐


















