YOLOv5——NMS

这篇博客详细解析了YOLOv5中NMS(非极大值抑制)的过程。首先,介绍了输入张量的结构,包括预测框的坐标、置信度和分类概率。然后,解释了如何通过置信度阈值筛选出有效分类,并探讨了NMS在处理多个预测框时的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False,
                        labels=()):
    """Runs Non-Maximum Suppression (NMS) on inference results

    Returns:
         list of detections, on (n,6) tensor per image [xyxy, conf, cls]
    """

    nc = prediction.shape[2] - 5  # 分类数
    # 第四个值框置信度大于conf_thres的为True,否则为False
    xc = prediction[..., 4] > conf_thres  # candidates 

    # Settings
    min_wh, max_wh = 2, 4096  # (pixels) minimum and maximum box width and height
    max_det = 300  # maximum number of detections per image
    max_nms = 30000  # maximum number of boxes into torchvision.ops.nms()
    time_limit = 10.0  # seconds to quit after
    redundant = True  # require redundant detections
    # 是否属于多分类问题
    multi_label &= nc > 1  # multiple labels per box (adds 0.5ms/img)
    # 默认是关闭的,使用的话需要修改为True
    merge = False  # use merge-NMS

    t = time.time()
    # 创建一个存储容器
    output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0]
    # xi表示某张图片的索引
    # x表示某张图片的tensor张量数据,x中包含数条预测框数据
    for xi, x in enumerate(prediction):  # image index, image inference
        # xc[xi]:表示筛选某张图片框置信度大于阈值的所有数据,这里的表示形式是True或False,而不是数据
        # x表示所有的candidates 为True的数据,这里表示筛选出所有置信度大于conf_thres的框的数据
        x = x[xc[xi]]  # confidence

        # Cat apriori labels if autolabelling
        # 好像没用到,暂时不管了
        if labels and len(labels[xi]):
            l = labels[xi]
            v = torch.zeros((len(l), nc + 5), device=x.device)
            v[:, :4] = l[:, 1:5]  # box
            v[:, 4] = 1.0  # conf
            v[range(len(l)), l[:, 0].long() + 5] = 1.0  # cls
            x = torch.cat((x, v), 0)

        # If none remain process next image
        if not x.shape[0]:
            continue

        # Compute conf
        # 见2讲解
        # 计算最后六个分类置信度信息,将他们分别乘上第四个数据(框置信度)
        x<
YOLOv5是一种先进的深度学习目标检测算法,但是还有改进的空间。针对YOLOv5NMS(非极大值抑制)方法,可以进行一些改进来提高检测效果。其中一种改进方法是使用不同的NMS变体,如DIoU-NMS、CIoU-NMS、EIoU-NMS、GIoU-NMS和SIoU-NMS。这些变体在计算目标框之间的IoU(交并比)时引入了不同的因素,如目标尺度、距离等,以更准确地衡量目标之间的重叠程度。这些改进方法可以在YOLOv5中替代传统的NMS方法,从而提高目标检测的准确性和稳定性。\[1\]\[3\]另外,还有一种改进方法是使用Soft-NMS,它通过自适应地调整阈值来平滑地抑制重叠的目标框,从而减少误检率。Soft-NMS可以在目标稀疏或稠密的情况下使用不同的阈值,以适应不同的目标密度。\[2\]这些改进方法可以根据具体的应用场景和需求来选择和应用,以达到更好的检测效果。 #### 引用[.reference_title] - *1* [[YOLOv7/YOLOv5系列算法改进NO.8]非极大值抑制NMS算法改进Soft-nms](https://blog.youkuaiyun.com/m0_70388905/article/details/125448230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [YOLOv5改进系列(14)——更换NMS(非极大抑制)之 DIoU-NMS、CIoU-NMS、EIoU-NMS、GIoU-NMS 、SIoU-NMS、...](https://blog.youkuaiyun.com/weixin_43334693/article/details/131552028)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值