Python实现NMS、DIOU_NMS

Python实现NMS,DIOU_NMS

NMS

import numpy as np

def NMS(dects, threshhold):
    x1 = dects[:, 0]
    y1 = dects[:, 1]
    x2 = dects[:, 2]
    y2 = dects[:, 3]
    scores = dects[:, 4]

    areas = (x2 - x1) * (y2 - y1) # 获取面积
    index = scores.argsort()[::-1]  # scores 降序排列
    keep = []

    while index.size > 0:
        i = index[0]
        keep.append(i)

        xx1 = np.maximum(x1[i], x1[index[1:]])
        yy1 = np.maximum(y1[i], y1[index[1:]])
        xx2 = np.minimum(x2[i], x2[index[1:]])
        yy2 = np.minimum(y2[i], y2[index[1:]])

        w = np.maximum(0.0, xx2 - xx1)
        h = np.maximum(0.0, yy2 - yy1)

        inter = w * h
        ious = inter/(areas[i] + areas[index[1:]] - inter)
        ins = np.where(ious <= threshhold)[0]   # 过滤iou大于阈值的候选框
        index = index[ins + 1]  # 更新index

    print(boxes[keep])

if __name__ == "__main__":
    threshhold = 0.7
    boxes = np.array([[100, 100, 210, 210, 0.71],
                      [250, 250, 420, 420, 0.8],
                      [220, 220, 320, 330, 0.92],
                      [100, 100, 210, 210, 0.72],
                      [230, 240, 325, 330, 0.81],
                      [220, 230, 315, 340, 0.9]])
    NMS(boxes, threshhold)

DIOU_NMS

import numpy as np

def DIoU_NMS(dects, threshhold):
    x1 = dects[:, 0]
    y1 = dects[:, 1]
    x2 = dects[:, 2]
    y2 = dects[:, 3]
    scores = dects[:, 4]

    areas = (x2 - x1) * (y2 - y1) # 获取面积
    index = scores.argsort()[::-1]  # scores 降序排列
    keep = []

    while index.size > 0:
        i = index[0]
        keep.append(i)

        # IoU
        xx1 = np.maximum(x1[i], x1[index[1:]])
        yy1 = np.maximum(y1[i], y1[index[1:]])
        xx2 = np.minimum(x2[i], x2[index[1:]])
        yy2 = np.minimum(y2[i], y2[index[1:]])
        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h

        # 中心点距离
        center_x1 = (x1[i]+x2[i])/2
        center_y1 = (y1[i]+y2[i])/2
        center_x2 = (x1[index[1:]]+x2[index[1:]])/2
        center_y2 = (y1[index[1:]]+y2[index[1:]])/2
        center_distance = (center_x1 - center_x2)**2 + (center_y1 - center_y2)**2

        # 对角线距离
        c_xx1 = np.minimum(x1[i], x1[index[1:]])
        c_yy1 = np.minimum(y1[i], y1[index[1:]])
        c_xx2 = np.maximum(x2[i], x2[index[1:]])
        c_yy2 = np.maximum(y2[i], y2[index[1:]])
        c_w = np.maximum(0.0, c_xx2 - c_xx1)
        c_h = np.maximum(0.0, c_yy2 - c_yy1)
        c_distance = c_w**2 + c_h**2

        ious = inter/(areas[i] + areas[index[1:]] - inter) - center_distance/c_distance
        ins = np.where(ious <= threshhold)[0]   # 过滤iou大于阈值的候选框
        index = index[ins + 1]  # 更新index

    print(boxes[keep])

if __name__ == "__main__":
    threshhold = 0.7
    boxes = np.array([[100, 100, 210, 210, 0.71],
                      [250, 250, 420, 420, 0.8],
                      [220, 220, 320, 330, 0.92],
                      [100, 100, 210, 210, 0.72],
                      [230, 240, 325, 330, 0.81],
                      [220, 230, 315, 340, 0.9]])
    DIoU_NMS(boxes, threshhold)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
需要学习Windows系统YOLOv4的同学请前往《Windows版YOLOv4目标检测实战:原理与源码解析》,课程链接 https://edu.csdn.net/course/detail/29865【为什么要学习这门课】 Linux创始人Linus Torvalds有一句名言:Talk is cheap. Show me the code. 冗谈不够,放码过来!  代码阅读是从基础到提高的必由之路。尤其对深度学习,许多框架隐藏了神经网络底层的实现,只能在上层调包使用,对其内部原理很难认识清晰,不利于进一步优化和创新。YOLOv4是最近推出的基于深度学习的端到端实时目标检测方法。YOLOv4的实现darknet是使用C语言开发的轻型开源深度学习框架,依赖少,可移植性好,可以作为很好的代码阅读案例,让我们深入探究其实现原理。【课程内容与收获】 本课程将解析YOLOv4的实现原理和源码,具体内容包括:- YOLOv4目标检测原理- 神经网络及darknet的C语言实现,尤其是反向传播的梯度求解和误差计算- 代码阅读工具及方法- 深度学习计算的利器:BLAS和GEMM- GPU的CUDA编程方法及在darknet的应用- YOLOv4的程序流程- YOLOv4各层及关键技术的源码解析本课程将提供注释后的darknet的源码程序文件。【相关课程】 除本课程《YOLOv4目标检测:原理与源码解析》外,本人推出了有关YOLOv4目标检测的系列课程,包括:《YOLOv4目标检测实战:训练自己的数据集》《YOLOv4-tiny目标检测实战:训练自己的数据集》《YOLOv4目标检测实战:人脸口罩佩戴检测》《YOLOv4目标检测实战:中国交通标志识别》建议先学习一门YOLOv4实战课程,对YOLOv4的使用方法了解以后再学习本课程。【YOLOv4网络模型架构图】 下图由白勇老师绘制  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值