• tensorrt C++推理


    1. char* trtModelStream{ nullptr }; //char* trtModelStream==nullptr; 开辟空指针后 要和new配合使用,比如89行 trtModelStream = new char[size]
    2. size_t size{ 0 };//与int固定四个字节不同有所不同,size_t的取值range是目标平台下最大可能的数组尺寸,一些平台下size_t的范围小于int的正数范围,又或者大于unsigned int. 使用Int既有可能浪费,又有可能范围不够大。
    3. std::ifstream file("D:/match/SimpleNet/pt/feature_aggregator.trt", std::ios::binary);
    4. std::cout << file.good();
    5. if (file.good()) {
    6. std::cout << "load engine success" << std::endl;
    7. file.seekg(0, file.end);//指向文件的最后地址
    8. size = file.tellg();//把文件长度告诉给size
    9. file.seekg(0, file.beg);//指回文件的开始地址
    10. trtModelStream = new char[size];//开辟一个char 长度是文件的长度
    11. assert(trtModelStream);//
    12. file.read(trtModelStream, size);//将文件内容传给trtModelStream
    13. file.close();//关闭
    14. }
    15. else {
    16. std::cout << "load engine failed" << std::endl;
    17. return 1;
    18. }
    19. IRuntime* runtime = createInferRuntime(gLogger);
    20. ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size);
    21. IExecutionContext *context = engine->createExecutionContext();
    22. void* buffers[2];
    23. // In order to bind the buffers, we need to know the names of the input and output tensors.
    24. // Note that indices are guaranteed to be less than IEngine::getNbBindings()
    25. const int inputIndex = engine->getBindingIndex(INPUT_BLOB_NAME);
    26. const int outputIndex = engine->getBindingIndex(OUTPUT_BLOB_NAME);
    27. // Create GPU buffers on device
    28. cudaMalloc(&buffers[inputIndex], 3 * INPUT_H * INPUT_W * sizeof(float));
    29. cudaMalloc(&buffers[outputIndex], OUTPUT_SIZE * sizeof(float));
    30. context->setTensorAddress(INPUT_BLOB_NAME, buffers[inputIndex]);
    31. context->setTensorAddress(OUTPUT_BLOB_NAME,buffers[outputIndex]);
    32. cudaStream_t stream;
    33. cudaStreamCreate(&stream);
    34. Mat src = imread("D:/dataset/coco/blag/5FC4EZ01A00009_CD_20221223111215_1.png", 1);
    35. // DMA input batch data to device, infer on the batch asynchronously, and DMA output back to host
    36. //uchar * data = new uchar[512 * 512 * 3];
    37. cudaMemcpyAsync(buffers[inputIndex],src.data,3 * INPUT_H * INPUT_W * sizeof(float), cudaMemcpyHostToDevice, stream);
    38. //context.enqueue(batchSize, buffers, stream, nullptr);
    39. context->enqueueV3(stream);
    40. // 假设CUDA stream中的数据是一张图像,大小为128x128,数据类型为float
    41. int width = 4096;
    42. int height = 512;
    43. //int channel = 512;
    44. size_t dataSize = width * height * sizeof(float);
    45. // 在主机内存中分配空间
    46. float* hostData = new float[dataSize];
    47. //在CUDA stream中分配空间
    48. float* deviceData = nullptr;
    49. //cudaError_t cudaMemcpy(void *dist, const void* src, size_t count, CudaMemcpyKind kind);
    50. cudaMalloc((void **)&deviceData, dataSize);
    51. //cudaMalloc(float(**)&addr, n * sizeof(float))
    52. // 将数据从CUDA stream拷贝到主机内存
    53. cudaMemcpyAsync(hostData, deviceData, dataSize, cudaMemcpyDeviceToHost, stream);
    54. // 等待CUDA操作完成
    55. cudaStreamSynchronize(stream);
    56. // 将主机内存中的数据转换为cv::Mat类型
    57. cv::Mat img = cv::Mat::zeros(cv::Size(width,height),CV_8U);
    58. memcpy(img.data,hostData,dataSize);

    onnx导engine:

    trtexec.exe --onnx=resnet18.onnx --saveEngine=resnet18.engine --fp16

  • 相关阅读:
    C语言一个奇奇怪怪的小细节(定制初始化中的一个坑)
    华为李鹏:加速5G商业正循环,拥抱更繁荣的5.5G(5G-A)
    快捷键查询大全(包括VSCode、PyCharm 强烈建议收藏)
    阿里15年技术老兵用140个案例整合出Java微服务架构实战
    MySQL-视图:视图概述、使用视图注意点、视图是否影响基本表
    Unity3D学习笔记11——后处理
    Git的使用
    Windows 下编译 TensorFlow 2.12.0 CC库
    unity学习之汇总
    springboot2.0 读取yml 配置 array,list,map,单值,及其组合
  • 原文地址:https://blog.csdn.net/weixin_38241876/article/details/133177813