外贸接单网站,中国小康建设网 是个什么网站,wordpress跳转页面插件,郑州男科医院十大排名YOLOv2 中 NMS 的详解
一、什么是 NMS#xff1f;
#x1f9e0; 定义#xff1a;
NMS#xff08;非极大值抑制#xff09;是一种目标检测中的后处理技术#xff0c;用于去除重复预测的边界框#xff0c;保留置信度最高且不重叠的边界框。
#x1f3af; 目标#x…YOLOv2 中 NMS 的详解
一、什么是 NMS 定义
NMS非极大值抑制是一种目标检测中的后处理技术用于去除重复预测的边界框保留置信度最高且不重叠的边界框。 目标
提高检测结果的准确性避免同一物体被多次检测减少误检和冗余框 二、YOLOv1 中的 NMS 实现 来源依据
来自 You Only Look Once: Unified, Real-Time Object Detection (CVPR 2016) 输出结构回顾
YOLOv1 输出为一个 7×7×30 的张量
每个 grid cell 输出 2 个 bounding box每个 bounding box 包含 (x, y, w, h, confidence)后接 20 个类别的概率PASCAL VOC
⚙️ NMS 执行流程根据论文描述 Darknet 实现 按类别执行 NMS 对每个类别分别执行 NMS即只在同一类别的预测框之间比较 IoU 筛选该类别的所有预测框
class_boxes [box for box in all_boxes if box.class_id class_id]按 confidence 排序
class_boxes.sort(keylambda x: x.confidence_score, reverseTrue)依次选择最大 confidence 的框并删除与其高度重叠的其他框
keep_boxes []
while len(class_boxes) 0:highest_conf_box class_boxes.pop(0)keep_boxes.append(highest_conf_box)class_boxes [box for box in class_boxesif iou(box, highest_conf_box) iou_threshold]常用阈值iou_threshold 0.5 ✅ YOLOv1 NMS 的特点总结
特点说明按类别执行不同类别的框不会相互干扰使用 confidence 排序只使用 bounding box 的 confidence 分数每个网格预测两个框框的数量有限召回率较低简单高效在 CPU 上也能运行良好效果一般对密集目标或小目标效果较差 三、YOLOv2 中的 NMS 实现 来源依据
来自 YOLO9000: Better, Faster, Stronger (CVPR 2017) 输出结构回顾
YOLOv2 输出为一个 13×13×(B × 5 C) 张量
每个 grid cell 预测 B5 个 anchor boxes每个 bounding box 包含 (tx, ty, tw, th, confidence)类别概率为 C 个COCO 为 80每个 anchor box 还包含对应的宽高偏移基于聚类 anchors ⚙️ YOLOv2 NMS 的执行流程基于 Darknet 实现
Step 1: 计算每个 bounding box 的综合得分score
不同于 YOLOv1YOLOv2 在排序时使用的是 score confidence × max ( class probabilities ) \text{score} \text{confidence} \times \max(\text{class probabilities}) scoreconfidence×max(class probabilities)
即
表示“这个框有多大概率是一个物体” × “它属于某个类别的概率”
for box in all_boxes:box.score box.confidence * max(box.class_probs)Step 2: 按 score 排序
all_boxes.sort(keylambda x: x.score, reverseTrue)Step 3: 执行 NMS
keep_boxes []while len(all_boxes) 0:highest_score_box all_boxes.pop(0)keep_boxes.append(highest_score_box)all_boxes [box for box in all_boxesif iou(box, highest_score_box) iou_threshold]✅ YOLOv2 NMS 的特点总结
特点说明综合 score 排序使用 confidence × class probability提升排序合理性更多 anchor boxes每个 grid cell 预测 5 个框提高召回率支持 Soft-NMSDarknet 后续版本支持软抑制非官方多尺度训练适配对不同大小目标的鲁棒性更强Anchor Boxes 支持anchor 是通过 K-Means 聚类获得的预设框 四、YOLOv1 与 YOLOv2 NMS 的核心差异对比基于论文与 Darknet 实现
对比维度YOLOv1YOLOv2是否使用 Anchor Boxes❌ 不使用✅ 使用 K-Means 聚类 anchor排序依据confidenceconfidence × max(class_probs)是否按类别执行✅ 是✅ 是默认每个 grid cell 预测框数量25是否支持 Soft-NMS❌ 不支持✅ 支持Darknet 后续实现是否支持 DIoU-NMS❌ 不支持✅ 支持可通过修改实现小目标检测能力较差明显提升多尺度训练支持❌ 不支持✅ 支持输入尺寸随机变化NMS 应用位置检测头输出后检测头输出后同 YOLOv1 五、YOLOv1 / YOLOv2 中 NMS 的 PyTorch 示例代码简化版
✅ YOLOv1 NMS 示例伪代码
def nms_yolov1(boxes, confidences, iou_threshold0.5):# 按置信度排序indices np.argsort(confidences)[::-1]keep []while len(indices) 0:best_idx indices[0]keep.append(best_idx)# 计算 IoUious [iou(boxes[best_idx], boxes[i]) for i in indices[1:]]indices indices[1:][np.array(ious) iou_threshold]return keep✅ YOLOv2 NMS 示例伪代码
def nms_yolov2(boxes, scores, iou_threshold0.5):# 按综合分数排序confidence × class probindices np.argsort(scores)[::-1]keep []while len(indices) 0:best_idx indices[0]keep.append(best_idx)# 计算 IoUious [iou(boxes[best_idx], boxes[i]) for i in indices[1:]]indices indices[1:][np.array(ious) iou_threshold]return keep六、YOLOv1 / YOLOv2 中 NMS 的实际表现对比来源YOLO 官方文档 Darknet 实现
模型mAPCOCOFPSNMS 类型NMS 输入方式YOLOv1~63.445 fpsIoU-NMSconfidence 排序YOLOv2~76.867 fpsIoU-NMS可扩展为 Soft/DIoUconfidence × class_prob 排序 七、YOLOv2 中 NMS 的改进意义
虽然 YOLOv2 并未在论文中明确提出新的 NMS 方法但在实践中
引入了 Anchor Boxes使得预测框更合理提高召回率综合 score 排序提升了排序的合理性支持更多框预测从每格 2 个框增加到 5 个框提高了覆盖范围配合多尺度训练增强了模型对不同大小目标的适应性
这些变化间接使 NMS 的效果显著优于 YOLOv1。 八、结语
模块内容YOLOv1 的 NMS按类别执行仅使用 confidence 排序YOLOv2 的 NMS使用 confidence × class prob 排序anchor 框提升召回区别总结YOLOv2 的 NMS 更加合理结合 anchor boxes 提升整体性能现实意义NMS 是目标检测不可或缺的一环YOLOv2 的改进是后续版本的基础 欢迎点赞 收藏 关注我我会持续更新更多关于计算机视觉、目标检测、深度学习、YOLO系列等内容