cuda nms

2023-11-02

  int YoloLayerPlugin::nms_fun(int batch_size, void **inputs, void *const* outputs, size_t count, int detections_per_im, float nms_thresh, void *workspace, size_t workspace_size, cudaStream_t stream) const {

        if (!workspace || !workspace_size) {
            // Return required scratch space size cub style
            workspace_size  = get_size_aligned<bool>(count);  // flags
            workspace_size += get_size_aligned<int>(count);   // indices
            workspace_size += get_size_aligned<int>(count);   // indices_sorted
            workspace_size += get_size_aligned<float>(count); // scores
            workspace_size += get_size_aligned<float>(count); // scores_sorted
        
            size_t temp_size_flag = 0;
            cub::DeviceSelect::Flagged((void *)nullptr, temp_size_flag,
            cub::CountingInputIterator<int>(count),
            (bool *)nullptr, (int *)nullptr, (int *)nullptr, count);
            size_t temp_size_sort = 0;
            cub::DeviceRadixSort::SortPairsDescending((void *)nullptr, temp_size_sort,
            (float *)nullptr, (float *)nullptr, (int *)nullptr, (int *)nullptr, count);
            workspace_size += std::max(temp_size_flag, temp_size_sort);

            return workspace_size;
        }

        auto on_stream = thrust::cuda::par.on(stream);

        auto flags = get_next_ptr<bool>(count, workspace, workspace_size);
        auto indices = get_next_ptr<int>(count, workspace, workspace_size);
        auto indices_sorted = get_next_ptr<int>(count, workspace, workspace_size);
        auto scores = get_next_ptr<float>(count, workspace, workspace_size);
        auto scores_sorted = get_next_ptr<float>(count, workspace, workspace_size);

        // printf("nms batch %d \n", batch_size);

        for (int batch = 0; batch < batch_size; batch++) {
            auto in_scores = static_cast<const float *>(inputs[0]) + batch * count;
            auto in_boxes = static_cast<const float4 *>(inputs[1]) + batch * count;
            auto in_classes = static_cast<const float *>(inputs[2]) + batch * count;
            auto in_points = static_cast<const float *>(inputs[3]) + batch * count;


            auto out_scores = static_cast<float *>(outputs[0]) + batch * detections_per_im;
            auto out_boxes = static_cast<float4 *>(outputs[1]) + batch * detections_per_im;
            auto out_classes = static_cast<float *>(outputs[2]) + batch * detections_per_im;
            auto out_points = static_cast<float4 *>(outputs[3]) + batch * detections_per_im;
            

           
            // cudaMemcpyAsync(tmp, out_scores, 10 * sizeof(float), cudaMemcpyDeviceToHost, stream);
            // printf("output %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f\n", tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5],tmp[6],tmp[7],tmp[8],tmp[9]);

            // Discard null scores
            thrust::transform(on_stream, in_scores, in_scores + count,flags, thrust::placeholders::_1 > 0.0f);

            int *num_selected = reinterpret_cast<int *>(indices_sorted);
            cub::DeviceSelect::Flagged(workspace, workspace_size, cub::CountingInputIterator<int>(0),flags, indices, num_selected, count, stream);
            cudaStreamSynchronize(stream);
            int num_detections = *thrust::device_pointer_cast(num_selected);

            // Sort scores and corresponding indices
            thrust::gather(on_stream, indices, indices + num_detections, in_scores, scores);
            cub::DeviceRadixSort::SortPairsDescending(workspace, workspace_size,scores, scores_sorted, indices, indices_sorted, num_detections, 0, sizeof(*scores)*8, stream);

            // Launch actual NMS kernel - 1 block with each thread handling n detections
            const int max_threads = 1024;
            int num_per_thread = ceil((float)num_detections / max_threads);
            nms_kernel<<<1, max_threads, 0, stream>>>(num_per_thread, nms_thresh, num_detections,
            indices_sorted, scores_sorted, in_classes, in_boxes);

            // Re-sort with updated scores
            cub::DeviceRadixSort::SortPairsDescending(workspace, workspace_size,
            scores_sorted, scores, indices_sorted, indices, num_detections, 0, sizeof(*scores)*8, stream);

            // Gather filtered scores, boxes, classes
            num_detections = min(detections_per_im, num_detections);
            cudaMemcpyAsync(out_scores, scores, num_detections * sizeof *scores, cudaMemcpyDeviceToDevice, stream);
            if (num_detections < detections_per_im) {
                thrust::fill_n(on_stream, out_scores + num_detections, detections_per_im - num_detections, 0);
            }
            thrust::gather(on_stream, indices, indices + num_detections, in_boxes, out_boxes);
            thrust::gather(on_stream, indices, indices + num_detections, in_classes, out_classes);
            thrust::gather(on_stream, indices, indices + num_detections, in_points, out_points);

			float tmp[10];
			cudaMemcpyAsync(tmp, out_points, 10 * sizeof(float), cudaMemcpyDeviceToHost, stream);
			printf("out_points %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f\n", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8], tmp[9]);
            // printf("num_detections %d \n", num_detections);
            // cudaMemcpyAsync(tmp, out_scores, 10 * sizeof(float), cudaMemcpyDeviceToHost, stream);
            // printf("output %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f\n", tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5],tmp[6],tmp[7],tmp[8],tmp[9]);
        }
        
        return 0;
    }

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

cuda nms 的相关文章

随机推荐

  • 在Ubuntu中安装eclipse

    1 下载JDK和eclipse jdk下载网址 http www oracle com technetwork java javase downloads jdk8 downloads 2133151 html eclipse下载网址 ht
  • AD20铺铜操作及设置

    AD20铺铜方法 首先 建议铺铜前 先滴泪 提高信号完整性 1 铺铜 放置 gt 铺铜 2 铺铜时 先设置属性 如果以前设置过 不用重设 点键盘上的 Tab 键 调出 属性 界面 3 操作 沿着四个点 到第4点时 点鼠标左键 完成划区域 如
  • stm32同芯片但不同flash工程更换Device出现报错

    目录 1 问题描述 2 解决方案 1 问题描述 stm32同芯片但不同flash工程更换Device出现报错 2 解决方案 更换Device 我是从ZE换为C8 把这个从HD更换为MD 解决
  • Three.js - 透视相机(PerspectiveCamera)(三)

    简介 在three js中 摄像机的作用就是不断的拍摄我们创建好的场景 然后通过渲染器渲染到屏幕中 想通过不同的角度观看场景 就需要修改摄像机的位置来拍摄场景 本文详细介绍的是透视相机 PerspectiveCamera 它是用来模拟人眼所
  • OpenWrt自定义luci页面来修改配置文件

    在使用OpenWrt路由器的过程中 经常需要根据需要改改配置文件然后重新启动服务什么的 一般的做法是SSH登录路由器后台 使用vi编辑器修改文件 然后使用 etc init d xxxx restart 来重启服务 次数多了就会觉得很繁琐
  • 一步步写嵌入式操作系统 中断处理

    简单的中断处理程序 简单的中断处理程序 1 获取被中断模式的将要执行的指令的地址到LR 2 将LR压入中断模式栈 3 将pc置为公共的中断服务函数入口地址 并记录下一条指令地址到LR 4 从公共的中断服务函数返回 5 从spsr恢复被中断模
  • Ribbon负载均衡器

    两种 1 1 集中式负载均衡 服务端负载均衡 硬件 nginx 轮询 负载 哈希 随机 权重 为什么要做负载均衡 1 2 客户端负载均衡器 用客户端 负载均衡器 很多机制可以自定义 小知识 不想让别人调自己 只想用别人的 怎么做 只需要不注
  • 用simulink 模型自动生成代码之 SPWM

    正弦PWM的信号波为正弦波 就是正弦波等效成一系列等幅不等宽的矩形脉冲波形 其脉冲宽度是由正弦波和三角波自然相交生成的 正弦波波形产生的方法有很多种 但较典型的主要有 对称规则采样法 不对称规则采样法和平均对称规则采样法三种 第一种方法由于
  • qt工具栏和菜单栏

    以前用过qt 但是老是忘 得现查 现记录如下 1 一个menubar可以有多个menu 2 一个menu可以有多个action 菜单栏里的各项叫做action 而不是Menu action可以当作实体 3 每个action对应事件 比如 1
  • Git 介绍

    一 理解 Git 1 分布式版本控制 Git 版本控制系统的设计思想是 去中心化 传统的 CVS SVN 等工具采用的是 C S 架构 只有一个中心代码仓库 位于服务器端 而一旦由于服务器系统宕机 网络不通等各种原因造成中心仓库不可用 整个
  • 使用taro框架注意避免的一些问题

    1 参数名的问题 Taro request url path data prams header 这里注意header是没有s的 不然消息体里的数据就会是 object Object Content Type application jso
  • com.google.zxing.NotFoundException 问题分析

    这仅仅是一篇问题分析哈 提供理解这个问题的思路 并不是解决这个问题的方法 背景 先说背景 项目中需要一个扫描二维码的功能 网上找了一个比较火的 BGAQRCode Android 用了一圈感觉还不错 但是在扫描页面 logcat 总是报 W
  • redhat中文文件名、文件夹乱码问题解决

    redhat在没有安装中文rpm包之前 中文会显示为乱码的小方块字样 利用ssh客户端在上传中文文件名的文件或文件夹时 均不能识别中文 给开发应用造成很大的困扰 首先安装fonts chinese 3 02 9 6 el5 noarch r
  • 在服务器上安装vasp如何得到输出文件,科学网—VASP各输出文件解读-更新中 - 叶小球的博文...

    PROCAR file For static calculations the file PROCAR contains the spd and site projected wave function character of each
  • 单片机MPU9250/6050陀螺仪芯片驱动

    单片机MPU9250 6050陀螺仪芯片驱动 CubeMX配置 驱动代码 项目需要陀螺仪检测设备位置角度信息 所以就有了本文章 代码借鉴了github上大佬写的应用了卡尔曼滤波 关于寄存器的说明参考 当然驱动代码中也附带了说明 https
  • 用jquery实现仿淘宝焦点图的动画

  • 百万前端之js生成用户登录图形验证码

    用户登录的图形验证码 jquey生成引入图形验证码 和前端判断是否正确 参考代码如下 css login title width 20 height 3rem margin 0 auto margin top 2rem text align
  • 如何设计一个数据库

    前言 我们知道 软件工程是为了解决软件危机的 它是采用工程的概念 原理 技术和方法来开发与维护软件 把经过时间考验而证明正确的管理技术和当前能够得到的最好的技术方法结合起来 在软件开发的过程中 数据库设计是非常重要的 它需要根据需求分析设抽
  • Android合并音频文件

    java view plain copy 需求 将两个amr格式音频文件合并为1个 注意 amr格式的头文件为6个字节的长度 param partsPaths 各部分路径 param unitedFilePath 合并后路径 public
  • cuda nms

    int YoloLayerPlugin nms fun int batch size void inputs void const outputs size t count int detections per im float nms t