IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> tensorrt yolov3部署 使用parse -> 正文阅读

[C++知识库]tensorrt yolov3部署 使用parse

#include "NvInfer.h"
#include <iostream>
#include "NvOnnxParser.h"
#include <fstream>
#include "cuda_runtime_api.h"
#include <vector>
#include <opencv2/opencv.hpp>
#include <algorithm>

using namespace nvonnxparser;
using namespace nvinfer1;
using namespace std;
using namespace cv;

class Logger : public ILogger
{
    void log(Severity severity, const char* msg) noexcept override
    {
        // suppress info-level messages
        if (severity <= Severity::kWARNING)
            std::cout << msg << std::endl;
    }
} logger;


Mat preprocess_img(cv::Mat& img, int input_w, int input_h) {
    int w, h, x, y;
    float r_w = input_w / (img.cols*1.0);
    float r_h = input_h / (img.rows*1.0);
    if (r_h > r_w) {
        w = input_w;
        h = r_w * img.rows;
        x = 0;
        y = (input_h - h) / 2;
    } else {
        w = r_h * img.cols;
        h = input_h;
        x = (input_w - w) / 2;
        y = 0;
    }
    cv::Mat re(h, w, CV_8UC3);
    cv::resize(img, re, re.size(), 0, 0, cv::INTER_LINEAR);
    cv::Mat out(input_h, input_w, CV_8UC3, cv::Scalar(128, 128, 128));
    re.copyTo(out(cv::Rect(x, y, re.cols, re.rows)));
    return out;
}




struct  Object{
  Rect_<float> rect;
  int label;
  float prob;

};


void qsort_descent_inplace(vector<Object>&faceobjects,int left, int right){
    int i = left;
    int j = right;
    float p = faceobjects[(left+right)/2].prob;
    while (i<=j){
        while (faceobjects[i].prob>p ){
            i++;
        }
        while (faceobjects[j].prob<p){
            j--;
        }
        if(i<=j){
            swap(faceobjects[i],faceobjects[j]);
            i++;
            j--;

        }

    }
#pragma omp parallel sections
    {
#pragma omp section
        {
            if (left < j) qsort_descent_inplace(faceobjects, left, j);
        }
#pragma omp section
        {
            if (i < right) qsort_descent_inplace(faceobjects, i, right);
        }

    }
}



void  qsort_descent_inplace(vector<Object>&faceobjects){
    if(faceobjects.empty()){
        return ;
    }
    qsort_descent_inplace(faceobjects,0,faceobjects.size()-1);

}

float intersection_area(Object & a,Object&b) {
    Rect2f inter = a.rect&b.rect;
    return inter.area();

}


void nms_sorted_bboxes(std::vector<Object>& faceobjects, std::vector<int>& picked, float nms_threshold)
{
    picked.clear();

    const int n = faceobjects.size();

    std::vector<float> areas(n);
    for (int i = 0; i < n; i++)
    {
        areas[i] = faceobjects[i].rect.area();
    }

    for (int i = 0; i < n; i++)
    {
         Object& a = faceobjects[i];

        int keep = 1;
        for (int j = 0; j < (int)picked.size(); j++)
        {
          Object& b = faceobjects[picked[j]];

            // intersection over union
            float inter_area = intersection_area(a, b);
            float union_area = areas[i] + areas[picked[j]] - inter_area;
            // float IoU = inter_area / union_area
            if (inter_area / union_area > nms_threshold)
                keep = 0;
        }

        if (keep)
            picked.push_back(i);
    }
}



float data[3*640*640];
float prob[25200*85];
float out422[3*80*80*85];
float out481[3*40*40*85];
float out540[3*20*20*85];


static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
{
    static const char* class_names[] = {
            "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
            "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
            "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
            "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
            "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
            "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
            "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
            "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
            "hair drier", "toothbrush"
    };

    cv::Mat image = bgr.clone();

    for (size_t i = 0; i < objects.size(); i++)
    {
        const Object& obj = objects[i];

        fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
                obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);

        cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));

        char text[256];
        sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);

        int baseLine = 0;
        cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);

        int x = obj.rect.x;
        int y = obj.rect.y - label_size.height - baseLine;
        if (y < 0)
            y = 0;
        if (x + label_size.width > image.cols)
            x = image.cols - label_size.width;

        cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
                      cv::Scalar(255, 255, 255), -1);

        cv::putText(image, text, cv::Point(x, y + label_size.height),
                    cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
    }

    imwrite("/newhome/wangjd/tensortest/out.jpg",image);

}


int main(int argc,char ** argv){

    if(*argv[1]== 's') {
        IBuilder *builder = createInferBuilder(logger);

        uint32_t flag = 1U << static_cast<uint32_t>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);

        INetworkDefinition *network = builder->createNetworkV2(flag);

        IParser *parser = createParser(*network, logger);

        parser->parseFromFile("/newhome/wangjd/model/yolov3.onnx",
                              static_cast<int32_t>(ILogger::Severity::kWARNING));
        for (int32_t i = 0; i < parser->getNbErrors(); ++i) {
            std::cout << parser->getError(i)->desc() << std::endl;
        }


        IBuilderConfig *config = builder->createBuilderConfig();
        config->setMemoryPoolLimit(MemoryPoolType::kWORKSPACE,1U<<30);

         IHostMemory *serializedModel = builder->buildSerializedNetwork(*network, *config);

        ofstream p("/newhome/wangjd/engine/yolov3.engine", ios::binary);
        if (!p.good()) {
            cout << "open failed" << endl;
        }
        p.write(reinterpret_cast<const char *>(serializedModel->data()), serializedModel->size());

       delete parser;
       delete network;
       delete config;
       delete builder;
       delete serializedModel;

    }else if(*argv[1]=='d'){
        size_t size{0};
        char * trtModelStream{nullptr};
        ifstream file("/newhome/wangjd/engine/yolov3.engine", ios::binary);
        if(file.good()){
            file.seekg(0,ios::end);
            size = file.tellg();
            file.seekg(0,ios::beg);
            trtModelStream = new char[size];
            file.read(trtModelStream,size);
            file.close();

        }


        IRuntime * runtime = createInferRuntime(logger);
        ICudaEngine * engine = runtime->deserializeCudaEngine(trtModelStream,size);
        IExecutionContext *context = engine->createExecutionContext();
        delete[] trtModelStream;

        int BATCH_SIZE=1;
        int INPUT_H=640;
        int INPUT_W=640;

        const char * images = "images";
        const char * output = "output";
        const char * output422 = "422";
        const char * output481 = "481";
        const char * output540 = "540";


        int32_t images_index = engine->getBindingIndex(images);
        int32_t output_index = engine->getBindingIndex(output);
        int32_t output422_index = engine->getBindingIndex(output422);
        int32_t output481_index = engine->getBindingIndex(output481);
        int32_t output540_index = engine->getBindingIndex(output540);



        cout<<    images_index<<" "
                <<output_index<<" "
                <<output422_index<<" "
                <<output481_index<<" "
                <<output540_index<<" "
                <<endl;

            cout<<engine->getNbBindings()<<endl;

        void * buffers[5];
        cudaMalloc(&buffers[images_index],BATCH_SIZE*3*INPUT_W*INPUT_H*sizeof (float));

        cudaMalloc(&buffers[output422_index],BATCH_SIZE*3*80*80*85*sizeof (float));
        cudaMalloc(&buffers[output481_index],BATCH_SIZE*3*40*40*85*sizeof (float));
        cudaMalloc(&buffers[output540_index],BATCH_SIZE*3*20*20*85*sizeof (float));
        cudaMalloc(&buffers[output_index],BATCH_SIZE*25200*85*sizeof (float));



        Mat img = imread("/newhome/wangjd/tensortest/123.jpg");

        Mat pr_img = preprocess_img(img,INPUT_H,INPUT_W);


        for(int i = 0 ; i < INPUT_W*INPUT_H;++i){
            data[i] = pr_img.at<Vec3b>(i)[2]/255.0;
            data[i+INPUT_W*INPUT_H] = pr_img.at<Vec3b>(i)[1]/255.0;
            data[i+2*INPUT_W*INPUT_H]=pr_img.at<Vec3b>(i)[0]/255.0;

        }



        cudaStream_t stream;
        cudaStreamCreate(&stream);

        cudaMemcpyAsync(buffers[images_index],data,BATCH_SIZE*3*INPUT_W*INPUT_H* sizeof(float ),cudaMemcpyHostToDevice,stream);
        context->enqueueV2(buffers,stream, nullptr);


        cudaMemcpyAsync(prob,buffers[output_index],1*25200*85* sizeof(float ),cudaMemcpyDeviceToHost,stream);
        cudaMemcpyAsync(out422,buffers[output422_index],1*3*80*80*85* sizeof(float ),cudaMemcpyDeviceToHost,stream);
        cudaMemcpyAsync(out481,buffers[output481_index],1*3*40*40*85* sizeof(float ),cudaMemcpyDeviceToHost,stream);
        cudaMemcpyAsync(out540,buffers[output540_index],1*3*20*20*85* sizeof(float ),cudaMemcpyDeviceToHost,stream);
        
        cudaStreamSynchronize(stream);
        cudaStreamDestroy(stream);

        cudaFree(buffers[images_index]);
        cudaFree(buffers[output_index]);
        cudaFree(buffers[output422_index]);
        cudaFree(buffers[output481_index]);
        cudaFree(buffers[output540_index]);
        delete context;
        delete runtime;
        delete engine;

        vector<Object> objects;
        for(int i = 0 ; i<25200;++i){
            if(prob[85*i+4]<=0.5) continue;

            int l ,r,t,b;
            float r_w = INPUT_W/(img.cols*1.0);
            float r_h = INPUT_H/(img.rows*1.0);


            float x = prob[85*i+0];
            float y = prob[85*i+1];
            float w = prob[85*i+2];
            float h = prob[85*i+3];
            float score = prob[85*i+4];
            if(r_h>r_w){
                l = x-w/2.0;
                r = x+w/2.0;
                t = y-h/2.0-(INPUT_H-r_w*img.rows)/2;
                b = y+h/2.0-(INPUT_H-r_w*img.rows)/2;
                l=l/r_w;
                r=r/r_w;
                t=t/r_w;
                b=b/r_w;
            }else{

                l = x-w/2.0-(INPUT_W-r_h*img.cols)/2;
                r = x+w/2.0-(INPUT_W-r_h*img.cols)/2;
                t = y-h/2.0;
                b = y+h/2.0;
                l=l/r_h;
                r=r/r_h;
                t=t/r_h;
                b=b/r_h;
            }


            int label_index = max_element(prob+85*i+5,prob+85*(i+1))-(prob+85*i+5);



            Object obj;
            obj.rect.x = l;
            obj.rect.y = t;
            obj.rect.width=r-l;
            obj.rect.height=b-t;
            obj.label = label_index;
            obj.prob = score;

            objects.push_back(obj);


        }

        qsort_descent_inplace(objects);

        vector<int> picked;
        nms_sorted_bboxes(objects,picked,0.45);



        int count = picked.size();
        vector<Object>obj_out(count);
        for(int i = 0 ; i <count ; ++i){
            obj_out[i] = objects[picked[i]];

        }

        draw_objects(img,obj_out);


   }


}

cmake_minimum_required(VERSION 3.10)
project(tensortest)
aux_source_directory(. src)

set(CMAKE_BUILD_TYPE Debug)
include_directories(/usr/local/cuda-11.1/include)
link_directories(/usr/local/cuda-11.1/lib64)

include_directories(/newhome/wangjd/TensorRT-8.4.0.6/include)
link_directories(/newhome/wangjd/TensorRT-8.4.0.6/lib)

find_package(OpenCV REQUIRED PATHS /newhome/wangjd/local NO_DEFAULT_PATH)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(tensortest ${src})
target_link_libraries(tensortest cudart cudnn cublas nvinfer nvparsers nvinfer_plugin nvonnxparser nvrtc ${OpenCV_LIBS})


  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-26 11:24:29  更:2022-04-26 11:27:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 22:30:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码