NMS和softnms代码

2023-05-16

NMS的python代码

import numpy as np

def py_cpu_nms(dets, thresh):
    """Pure Python NMS baseline."""
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / (areas[i] + areas[order[1:]] - inter)

        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]

    return keep

soft-nms的python代码

import numpy as np
cimport numpy as np

cdef inline np.float32_t max(np.float32_t a, np.float32_t b):
    return a if a >= b else b

cdef inline np.float32_t min(np.float32_t a, np.float32_t b):
    return a if a <= b else b

def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0):
    cdef unsigned int N = boxes.shape[0]
    cdef float iw, ih, box_area
    cdef float ua
    cdef int pos = 0
    cdef float maxscore = 0
    cdef int maxpos = 0
    cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov

    for i in range(N):
        maxscore = boxes[i, 4]
        maxpos = i

        tx1 = boxes[i,0]
        ty1 = boxes[i,1]
        tx2 = boxes[i,2]
        ty2 = boxes[i,3]
        ts = boxes[i,4]

        pos = i + 1
	# get max box
        while pos < N:
            if maxscore < boxes[pos, 4]:
                maxscore = boxes[pos, 4]
                maxpos = pos
            pos = pos + 1

	# add max box as a detection 
        boxes[i,0] = boxes[maxpos,0]
        boxes[i,1] = boxes[maxpos,1]
        boxes[i,2] = boxes[maxpos,2]
        boxes[i,3] = boxes[maxpos,3]
        boxes[i,4] = boxes[maxpos,4]

	# swap ith box with position of max box
        boxes[maxpos,0] = tx1
        boxes[maxpos,1] = ty1
        boxes[maxpos,2] = tx2
        boxes[maxpos,3] = ty2
        boxes[maxpos,4] = ts

        tx1 = boxes[i,0]
        ty1 = boxes[i,1]
        tx2 = boxes[i,2]
        ty2 = boxes[i,3]
        ts = boxes[i,4]

        pos = i + 1
	# NMS iterations, note that N changes if detection boxes fall below threshold
        while pos < N:
            x1 = boxes[pos, 0]
            y1 = boxes[pos, 1]
            x2 = boxes[pos, 2]
            y2 = boxes[pos, 3]
            s = boxes[pos, 4]

            area = (x2 - x1 + 1) * (y2 - y1 + 1)
            iw = (min(tx2, x2) - max(tx1, x1) + 1)
            if iw > 0:
                ih = (min(ty2, y2) - max(ty1, y1) + 1)
                if ih > 0:
                    ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih)
                    ov = iw * ih / ua #iou between max box and detection box

                    if method == 1: # linear
                        if ov > Nt: 
                            weight = 1 - ov
                        else:
                            weight = 1
                    elif method == 2: # gaussian
                        weight = np.exp(-(ov * ov)/sigma)
                    else: # original NMS
                        if ov > Nt: 
                            weight = 0
                        else:
                            weight = 1

                    boxes[pos, 4] = weight*boxes[pos, 4]
		    
		    # if box score falls below threshold, discard the box by swapping with last box
		    # update N
                    if boxes[pos, 4] < threshold:
                        boxes[pos,0] = boxes[N-1, 0]
                        boxes[pos,1] = boxes[N-1, 1]
                        boxes[pos,2] = boxes[N-1, 2]
                        boxes[pos,3] = boxes[N-1, 3]
                        boxes[pos,4] = boxes[N-1, 4]
                        N = N - 1
                        pos = pos - 1

            pos = pos + 1

    keep = [i for i in range(N)]
    return keep


def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):
    cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]
    cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]
    cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]
    cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]
    cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]

    cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]

    cdef int ndets = dets.shape[0]
    cdef np.ndarray[np.int_t, ndim=1] suppressed = \
            np.zeros((ndets), dtype=np.int)

    # nominal indices
    cdef int _i, _j
    # sorted indices
    cdef int i, j
    # temp variables for box i's (the box currently under consideration)
    cdef np.float32_t ix1, iy1, ix2, iy2, iarea
    # variables for computing overlap with box j (lower scoring box)
    cdef np.float32_t xx1, yy1, xx2, yy2
    cdef np.float32_t w, h
    cdef np.float32_t inter, ovr

    keep = []
    for _i in range(ndets):
        i = order[_i]
        if suppressed[i] == 1:
            continue
        keep.append(i)
        ix1 = x1[i]
        iy1 = y1[i]
        ix2 = x2[i]
        iy2 = y2[i]
        iarea = areas[i]
        for _j in range(_i + 1, ndets):
            j = order[_j]
            if suppressed[j] == 1:
                continue
            xx1 = max(ix1, x1[j])
            yy1 = max(iy1, y1[j])
            xx2 = min(ix2, x2[j])
            yy2 = min(iy2, y2[j])
            w = max(0.0, xx2 - xx1 + 1)
            h = max(0.0, yy2 - yy1 + 1)
            inter = w * h
            ovr = inter / (iarea + areas[j] - inter)
            if ovr >= thresh:
                suppressed[j] = 1

    return keep

参考

https://www.cnblogs.com/makefile/p/nms.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NMS和softnms代码 的相关文章

  • 深度剖析问题:Could not run ‘torchvision::nms‘ with arguments from the ‘CUDA‘ backend.

    问题 xff1a 使用YOLOv5进行测试的时候 xff0c 报错 xff1a Could not run 39 torchvision nms 39 with arguments from the 39 CUDA 39 backend x
  • ssd.pytorch源码分析(三)— 非极大值抑制NMS

    NMS源码 SSD论文链接 NMS介绍 吴恩达对于NMS xff08 非极大值抑制 xff09 的介绍 xff1a 说白了 xff0c NMS的作用就是去掉目标检测任务重复的检测框 例如 xff0c 一个目标有多个选择框 xff0c 现在要
  • pytorch框架下faster rcnn使用softnms

    pytorch faster rcnn softnms frcnn使用softnms方法一 xff1a pytorch复现版本的cpu版softnms xff08 本方法可以跑通 xff09 0 首先overview一波 xff1a inf
  • NMS和softnms代码

    NMS的python代码 import numpy as np def py cpu nms dets thresh 34 34 34 Pure Python NMS baseline 34 34 34 x1 61 dets 0 y1 61
  • 在faster rcnn中使用soft nms,faster rcnn的改进(一)

    1 背景介绍 我的项目是利用faster rcnn检测kiiti数据集 xff0c 用原始nms xff0c iters 61 10000的情况下 xff0c 得到的mAP 61 0 586 在改用soft nms后 xff0c 其他参数均
  • nms和softnms的代码

    文章目录 前言预测框筛选的方法1 nms2 softnms 总结 前言 nms和softnms的原理及相关简单代码总结 预测框筛选的方法 预测框的筛选 xff0c 是检测模块后处理阶段的一个十分重要的过程 因为我们预测输出的预测框 xff0
  • NMS

    NMS 非极大值抑制 def NMS dects threshold dects x1 y1 x2 y2 score x1 61 dects 0 y1 61 dects 1 x2 61 dects 2 y2 61 dects 3 score
  • soft-nms(softnms)(pytorch实现)& softer nms

    softnms和softer nms是nms的两个改进算法 传统nms存在的问题 传统的NMS方法是基于分类分数的 xff0c 只有最高分数的预测框能留下来 xff0c 但是大多数情况下IoU和分类分数不是强相关 xff0c 很多分类标签置
  • NMS详解及pytorch实现:hard-nms(diou\overlap\merge\batched),soft-nms

    文章目录 NMS详解及pytorch实现 hard nms diou overlap merge batched soft nms1 简介2 原理3 实现3 1 伪代码3 2 pytorch源码3 3 知识点 参考资料 NMS详解及pyto
  • yolo论文中IOU/AP/MAP/NMS概念详解

    之前在只看了一遍吴恩达神经网络下写了一篇Darknet yolov2的综述 xff0c 最近接着往下学时发现很多基础的概念不是很懂 xff0c 所以这篇解决一下寸疑问题 1 卷积滑动窗口 滑动窗口大家都了解的 xff0c 从图片的左上角开始
  • nms-python和C

    代码 import numpy as np def nms bboxes iou threshold x1 bboxes 0 y1 bboxes 1 x2 bboxes 2 y2 bboxes 3 score bboxes 4 area x
  • 非极大值抑制(NMS)及其变种实现

    文章目录 非极大值抑制 NMS 及其变种实现 NMS各大变种 标准NMS 局部感知NMS LNMS 倾斜NMS INMS 多边形NMS PNMS 掩膜NMS MNMS 总结 Soft NMS Motivation Method 非极大值抑制
  • 【使用TensorRT自带的plugin】

    0 背景 在之前的文章TensorRT的plugin实现中介绍了 如何从零实现一个TensorRT的plugin 这篇文章来介绍如何使用TensorRT自带的plugin 将其添加到Network Definition中加速我们的模型 自T
  • 如何监控ActiveMQ Artemis

    我正在 Windows NET 环境中使用 RabbitMQ ActiveMQ Classic 和 ActiveMQ Artemis 进行一些测试 RabbitMQ 和 ActiveMQ 经典 附带一个 Web 界面 您可以在其中查看有关代
  • 如何访问.net中的activemq统计插件

    我正在尝试访问 activemq 统计信息http activemq apache org statisticsplugin html in c 这就是我到目前为止所拥有的 我无法得到消费者的回复 我可以在监控网站上查看队列的计数增加 pu
  • 目标检测中的损失函数:IOU_Loss、GIOU_Loss、DIOU_Loss和CIOU_Loss

    文章目录 前言 1 IOU Loss Intersection over Union Loss 2 GIOU Loss Generalized Intersection over Union Loss 3 DIOU Loss Distanc
  • android 13.0 framework禁用系统所有通知

    1 概述 在13 0的系统rom产品开发中最近公司项目要求 禁用系统所有通知 不需要在下拉状态栏显示通知功能实现 要控制系统通知的开关功能 需要屏蔽系统通知 而系统通知都是由NoticationManagerServices java来管理
  • ActiveMQ NMS C# 对象消息使用什么序列化方法?

    我计划使用 Apache NMS 进行 ActiveMQ 消息传递 并且想知道我发送的对象将使用什么序列化方法 XML 二进制 什么控制序列化以及如何自定义它 有人有使用 C 对象执行此操作的经验吗 您知道有哪些陷阱吗 IObjectMes
  • net-snmp解析代码,如何解析MIB?

    我在学习代码库 解析MIB In parse c and parse h代码保留一个哈希桶 indexed bucket tree list 还有一个树结构 其中包含一个指向的next指针Next node in hashed list o
  • ActiveMQ NMS:当代理关闭时,connection.start() 会因故障转移协议而挂起

    我有使用 nms activemq 1 5 0 的 C 应用程序 当我的应用程序启动时 它尝试使用故障转移协议连接到代理 我有两个主从配置的代理 如果两个经纪人都关闭了 我的应用程序就会因为以下原因而陷入等待状态 connection st

随机推荐