c++版 nms

 

目前这个代码运行会报一些错误,还要再修改修改才行

#include<iostream>
#include<vector>
#include<algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


typedef struct Bbox{
    int x;
    int y;
    int w;
    int h;
    float score;
}Bbox;

class Solution {
public:

    static bool sort_score(Bbox box1,Bbox box2){
        return box1.score > box2.score ? true : false;
    }


    float iou(Bbox box1,Bbox box2){
        int x1 = max(box1.x,box2.x);
        int y1 = max(box1.y,box2.y);
        int x2 = min(box1.x+box1.w,box2.x+box2.w);
        int y2 = min(box1.y+box1.h,box2.y+box2.h);
        float over_area = (y2 - y1)*(x2 - x1);
        return over_area/(box1.w * box1.h + box2.w * box2.h - over_area);
    }

    vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold)
    {
        vector<Bbox>results;
        std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
        while(vec_boxs.size() > 0)
        {
            results.push_back(vec_boxs[0]);
            int index = 1;
            while(index < vec_boxs.size()){
                float iou_value = iou(vec_boxs[0],vec_boxs[index]);
                if(iou_value > threshold)
                    vec_boxs.erase(vec_boxs.begin() + index);
                else
                    index++;
            }
            vec_boxs.erase(vec_boxs.begin());
        }
    }
};

int main(){
    Solution a;
    vector<Bbox> input;

    Bbox box1 = {1,2,3,4,0.3};
    Bbox box2 = {2,3,4,5,0.4};
    input.push_back(box1);
    input.push_back(box2);
    vector<Bbox> res;
    res = a.nms(input,0.2);
    for(int i = 0;i < res.size();i++)
        printf("%d %d %d %d %f",res[i].x,res[i].y,res[i].w,res[i].h,res[i].score);
    return 0;
}

 https://blog.youkuaiyun.com/qq_32582681/article/details/81352758

### TensorRT C++ 实现非极大值抑制(NMS)的指南与代码示例 TensorRT 是 NVIDIA 提供的一个高性能深度学习推理库,广泛用于加速模型推理。非极大值抑制(NMS)是目标检测任务中的关键步骤之一,用于去除冗余的边界框,保留最有可能的检测结果。以下是关于如何在 TensorRT 的 C++ API 中实现 NMS 的详细说明和代码示例。 #### TensorRT C++ 实现 NMS 的方法 TensorRT 提供了内置的 `nms` 层来简化 NMS 的实现过程。开发者可以通过 TensorRT 的网络定义接口添加 NMS 层,并配置相关参数以满足具体需求[^4]。 以下是一个使用 TensorRT C++ API 实现 NMS 的代码示例: ```cpp #include "NvInfer.h" #include <iostream> #include <vector> // 假设我们已经创建了一个 TensorRT 网络定义对象 network // 并且输入张量为 boxes 和 scores // 添加 NMS 插件层 nvinfer1::IPluginV2Layer* addNMSToNetwork(nvinfer1::INetworkDefinition* network, nvinfer1::ITensor* boxes, nvinfer1::ITensor* scores) { // 定义 NMS 参数 std::vector<float> nmsScores; // 输入得分 std::vector<int> nmsBoxes; // 输入边界框 int keepTopK = 100; // 保留的最高得分框数量 float nmsThreshold = 0.5f; // NMS 阈值 // 创建 NMS 插件 auto creator = getPluginRegistry()->getPluginCreator("NonMaxSuppression_TRT", "1"); if (!creator) { std::cerr << "Failed to find NMS plugin creator" << std::endl; return nullptr; } // 设置插件参数 nvinfer1::PluginFieldCollection pfc; std::vector<nvinfer1::PluginField> fields; fields.emplace_back("shareLocation", (void*)&(bool{true}), nvinfer1::PluginFieldType::kINT32, 1); fields.emplace_back("backgroundLabelId", (void*)&(int{-1}), nvinfer1::PluginFieldType::kINT32, 1); fields.emplace_back("numClasses", (void*)&(int{80}), nvinfer1::PluginFieldType::kINT32, 1); fields.emplace_back("topK", (void*)&(int{keepTopK}), nvinfer1::PluginFieldType::kINT32, 1); fields.emplace_back("keepTopK", (void*)&(int{keepTopK}), nvinfer1::PluginFieldType::kINT32, 1); fields.emplace_back("scoreThreshold", (void*)&(float{0.3f}), nvinfer1::PluginFieldType::kFLOAT32, 1); fields.emplace_back("iouThreshold", (void*)&(nmsThreshold), nvinfer1::PluginFieldType::kFLOAT32, 1); pfc.nbFields = fields.size(); pfc.fields = fields.data(); // 创建插件实例 auto plugin = creator->createPlugin("nms", &pfc); if (!plugin) { std::cerr << "Failed to create NMS plugin" << std::endl; return nullptr; } // 将插件添加到网络中 return network->addPluginV2(&boxes, 2, *plugin); } ``` #### 关键点解析 1. **NMS 参数**: - `shareLocation`: 是否共享位置信息。 - `backgroundLabelId`: 背景类别的 ID。 - `numClasses`: 类别总数。 - `topK`: 每个类别中选择的候选框数量。 - `keepTopK`: 最终保留的边界框数量。 - `scoreThreshold`: 得分阈值,低于该值的框将被忽略。 - `iouThreshold`: IOU 阈值,用于决定是否抑制重叠框[^5]。 2. **插件注册**: 在 TensorRT 中,NMS 功能通常通过自定义插件实现。上述代码中展示了如何从插件注册表中获取 `NonMaxSuppression_TRT` 插件并设置其参数[^6]。 3. **性能优化**: 使用 TensorRT 的 GPU 加速能力可以显著提升 NMS 的运行速度。确保 TensorRT 本支持最新插件,并根据硬件环境调整批处理大小和线程数以获得最佳性能[^7]。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值