当前位置:   article > 正文

TensorRT部署推理yolov5s,完整代码和整个流程_python yolo5 配置tensorrt

python yolo5 配置tensorrt

一、通过yolov5s的源码生成onnx

(1)在生成onnx的过程中遇到的几个问题

已解决TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1.Downgrade the protobuf package to 3.20.x or lower.
2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).的正确解决方法

pip install protobuf==3.19.0

(2)ONNX export failure: No module named ‘onnx‘

pip install onnx==1.7.0

(3)生成指令

python export.py --weights ./yolov5s.pt --include onnx --imgsz 640 --device 0

(4)查看模型的网络结构

Netron

(5)对于输出四个维度的输出,为了便于以后部署,可以修改moudles下的yolo.py,具体修改如下,修改之后再次使用

yolov5s.onnx (netron.app)查看网络结构,可以看到只有一个输出了

(5)进行测试

python detect.py --weights yolov5s.onnx --imgsz 640 --device 0

二、通过yolov5s生成engine

(1)安装tensorrt,在python的虚拟环境下

找到Tensorrt的解压目录,pip install 目录:/tensorrt-8.2.3.0-cp38-none-win_amd64.whl

(2)在通过命令导出engine

python3 export.py --weights ./yolov5s.pt --include engine --imgsz 640 --device 0

的时候遇到了这样一个问题,卡在这里不动了,去源码中查找问题,将

这一行注释掉(前提已经存在onnx模型),也可以改为12,这里不太明白为什么

(3)导出半精度FP16的engine文件,试试证明使用半精度的engine文件大小约为FP32的一半左右,但是准确行几乎不受影响

python export.py --weights ./yolov5s.pt --include engine --half --device 0

三、测试onnx和engine模型的推理情况

(1)普通的pt模型的情况

python detect.py --weights yolov5s.pt --imgsz 640 --device 0

运行结果:

(2)onnx模型的情况

python detect.py --weights yolov5s.onnx --imgsz 640 --device 0

运行结果:

(3)FP32   engine模型的情况

python detect.py --weights yolov5s.engine --imgsz 640 --device 0

运行结果:

(4)FP16   engine模型的情况

四、在C++中进行推理部署

源代码(如果是单输出的话更改相应代码即可):

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include "NvInfer.h"
  5. #include "NvOnnxParser.h"
  6. #include "NvinferRuntime.h"
  7. #include<string>
  8. #include <opencv2/opencv.hpp>
  9. #include<windows.h>
  10. #include <opencv2/imgproc.hpp>
  11. #include <opencv2/highgui.hpp>
  12. #include <opencv2/core/utils/logger.hpp>
  13. #include<opencv2/dnn/dnn.hpp>
  14. using namespace nvinfer1;
  15. using namespace nvonnxparser;
  16. using namespace cv;
  17. Mat resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left)
  18. {
  19. int srch = srcimg.rows, srcw = srcimg.cols;
  20. int inpHeight = 640;
  21. int inpWidth = 640;
  22. *newh = inpHeight;
  23. *neww = inpWidth;
  24. bool keep_ratio = true;
  25. Mat dstimg;
  26. if (keep_ratio && srch != srcw) {
  27. float hw_scale = (float)srch / srcw;
  28. if (hw_scale > 1) {
  29. *newh = inpHeight;
  30. *neww = int(inpWidth / hw_scale);
  31. resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
  32. *left = int((inpWidth - *neww) * 0.5);
  33. copyMakeBorder(dstimg, dstimg, 0, 0, *left, inpWidth - *neww - *left, BORDER_CONSTANT, 0);
  34. }
  35. else {
  36. *newh = (int)inpHeight * hw_scale;
  37. *neww = inpWidth;
  38. resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
  39. *top = (int)(inpHeight - *newh) * 0.5;
  40. copyMakeBorder(dstimg, dstimg, *top, inpHeight - *newh - *top, 0, 0, BORDER_CONSTANT, 0);
  41. }
  42. }
  43. else {
  44. resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA);
  45. }
  46. return dstimg;
  47. }
  48. class Logger : public ILogger
  49. {
  50. virtual void log(Severity severity, const char* msg) noexcept override
  51. {
  52. // suppress info-level messages
  53. if (severity != Severity::kINFO)
  54. std::cout << msg << std::endl;
  55. }
  56. } gLogger;
  57. int main()
  58. {
  59. cudaSetDevice(0);
  60. const char* model_path_onnx = "yolov5s.onnx";
  61. const char* model_path_engine = "yolov5s.engine";
  62. const char* image_path = "zidane.jpg";
  63. std::string lable_path = "coco.txt";
  64. const char* input_node_name = "images";
  65. const char* output_node_name = "output";
  66. std::vector<std::string> class_names;
  67. std::string classesFile = "coco.txt";//加载label
  68. std::ifstream ifs(classesFile.c_str());//用了classesFile.c_str()函数将字符串classesFile转换为C风格的字符串(即以null结尾的字符数组),并将其作为参数传递给std::ifstream类的构造函数。
  69. std::string line;
  70. while (getline(ifs, line)) class_names.push_back(line);
  71. //五个索引
  72. int num_ionode = 5;
  73. std::ifstream file_ptr(model_path_engine, std::ios::binary);
  74. if (!file_ptr.good()) {
  75. std::cerr << "文件无法打开,请确定文件是否可用!" << std::endl;
  76. }
  77. size_t size = 0;
  78. file_ptr.seekg(0, file_ptr.end); // 将读指针从文件末尾开始移动0个字节
  79. size = file_ptr.tellg(); // 返回读指针的位置,此时读指针的位置就是文件的字节数
  80. file_ptr.seekg(0, file_ptr.beg); // 将读指针从文件开头开始移动0个字节
  81. char* model_stream = new char[size];
  82. file_ptr.read(model_stream, size);
  83. file_ptr.close();
  84. Logger logger;
  85. // 反序列化引擎
  86. nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(logger);
  87. // 推理引擎
  88. nvinfer1::ICudaEngine* engine = runtime->deserializeCudaEngine(model_stream, size);
  89. // 上下文
  90. nvinfer1::IExecutionContext* context = engine->createExecutionContext();
  91. void** data_buffer = new void* [num_ionode];
  92. // 创建GPU显存输入缓冲区
  93. int input_node_index = engine->getBindingIndex(input_node_name);
  94. //auto it = engine->getNbBindings();
  95. //std::cout << "input_node_index" << input_node_index <<it<< std::endl;
  96. nvinfer1::Dims input_node_dim = engine->getBindingDimensions(input_node_index);
  97. //断点检测
  98. std::cout << "input_node_dim.d[0]" << input_node_dim.d[0]
  99. << "input_node_dim.d[1]" << input_node_dim.d[1] <<
  100. "input_node_dim.d[2]" << input_node_dim.d[2] <<
  101. "input_node_dim.d[3]" << input_node_dim.d[3] << std::endl;
  102. size_t input_data_length = input_node_dim.d[1] * input_node_dim.d[2] * input_node_dim.d[3];
  103. std::cout << "input_data_length" << input_data_length << std::endl;
  104. cudaError_t err1 = cudaMalloc(&(data_buffer[input_node_index]), input_data_length * sizeof(float));
  105. if (err1 != cudaSuccess) {
  106. std::cout << "Failed to allocate memory for input data: " << cudaGetErrorString(err1) << std::endl;
  107. return -1;
  108. }
  109. // 创建GPU显存输出缓冲区
  110. int output_node_index = engine->getBindingIndex(output_node_name);
  111. std::cout << "output_node_index" << " " << output_node_index << std::endl;
  112. nvinfer1::Dims output_node_dim = engine->getBindingDimensions(output_node_index);
  113. size_t output_data_length = output_node_dim.d[1] * output_node_dim.d[2];
  114. //std::cout << "output_node_dim.d[0]:" << output_node_dim.d[0] <<
  115. // "output_node_dim.d[1]:" << output_node_dim.d[1] <<
  116. // "output_node_dim.d[2]" << output_node_dim.d[2] <<
  117. // "output_node_dim.d[3]"<< output_node_dim.d[3]<<
  118. // "output_node_dim.d[4]" << output_node_dim.d[4]<<std::endl;
  119. std::cout << "output_data_length" << output_data_length<<std::endl;
  120. cudaMalloc(&(data_buffer[output_node_index]), output_data_length * sizeof(float));
  121. //创建另外三个维度的输出缓存区
  122. nvinfer1::Dims output_node_dim1 = engine->getBindingDimensions(output_node_index-1);
  123. size_t output_data_length1 = output_node_dim1.d[1] * output_node_dim1.d[2]* output_node_dim1.d[3]* output_node_dim1.d[4];
  124. std::cout << "output_data_length1" << output_data_length1 << std::endl;
  125. cudaMalloc(&(data_buffer[output_node_index-1]), output_data_length1 * sizeof(float));
  126. nvinfer1::Dims output_node_dim2 = engine->getBindingDimensions(output_node_index - 2);
  127. size_t output_data_length2 = output_node_dim2.d[1] * output_node_dim2.d[2] * output_node_dim2.d[3] * output_node_dim2.d[4];
  128. std::cout << "output_data_length2" << output_data_length2 << std::endl;
  129. cudaMalloc(&(data_buffer[output_node_index - 2]), output_data_length2 * sizeof(float));
  130. nvinfer1::Dims output_node_dim3 = engine->getBindingDimensions(output_node_index - 3);
  131. size_t output_data_length3 = output_node_dim3.d[1] * output_node_dim3.d[2] * output_node_dim3.d[3] * output_node_dim3.d[4];
  132. std::cout << "output_data_length3" << output_data_length3 << std::endl;
  133. cudaMalloc(&(data_buffer[output_node_index - 3]), output_data_length3 * sizeof(float));
  134. // 图象预处理 - 格式化操作2
  135. cv::Mat image = cv::imread(image_path);
  136. //int max_side_length = std::max(image.cols, image.rows);
  137. //std::cout << "max_side_length0" << max_side_length << std::endl;
  138. //cv::Mat max_image = cv::Mat::zeros(cv::Size(max_side_length, max_side_length), CV_8UC3);
  139. //cv::Rect roi(0, 0, image.cols, image.rows);
  140. //image.copyTo(max_image(roi));
  141. // 将图像归一化,并放缩到指定大小
  142. //cv::Size input_node_shape(input_node_dim.d[2], input_node_dim.d[3]);
  143. //cv::Mat BN_image = cv::dnn::blobFromImage(image, 1 / 255.0, input_node_shape, cv::Scalar(0, 0, 0), true, false);
  144. int newh = 0, neww = 0, padh = 0, padw = 0;
  145. //将图像的大小调整为指定的尺寸并在调整过程中保持图像的完整性和比例
  146. Mat dstimg = resize_image(image, &newh, &neww, &padh, &padw);//Padded resize
  147. // 创建输入cuda流
  148. cudaStream_t stream;
  149. cudaStreamCreate(&stream);
  150. std::vector<float> input_data(input_data_length);
  151. for (int c = 0; c < 3; c++)
  152. {
  153. for (int i = 0; i < 640; i++)
  154. {
  155. for (int j = 0; j < 640; j++)
  156. {
  157. float pix = dstimg.ptr<uchar>(i)[j * 3 + 2 - c];//转换通道,输入onnx模型的图片通道顺序是RGB,但是opencv存储默认是BGR
  158. input_data[c * 640 * 640 + i * 640 + size_t(j)] = pix / 255.0;//归一化
  159. }
  160. }
  161. }
  162. //memcpy(input_data.data(), BN_image.ptr<float>(), input_data_length * sizeof(float));
  163. // 输入数据由内存到GPU显存
  164. float* result_array = new float[output_data_length];
  165. for (int i = 0; i < 10; i++)
  166. {
  167. clock_t start_time = clock();
  168. cudaError_t err3 = cudaMemcpyAsync(data_buffer[input_node_index], input_data.data(), input_data_length * sizeof(float), cudaMemcpyHostToDevice, stream);
  169. if (err3 != cudaSuccess) {
  170. std::cout << "Failed to transfer input data to GPU1: " << cudaGetErrorString(err3) << std::endl;
  171. return -1;
  172. }
  173. context->enqueueV2(data_buffer, stream, nullptr);
  174. //处理推理结果
  175. //float* result_array = new float[output_data_length];
  176. cudaError_t err4 = cudaMemcpyAsync(result_array, data_buffer[output_node_index], output_data_length * sizeof(float), cudaMemcpyDeviceToHost, stream);
  177. if (err4 != cudaSuccess) {
  178. std::cout << "Failed to transfer input data to HOST: " << cudaGetErrorString(err4) << std::endl;
  179. return -1;
  180. }
  181. clock_t end_time = clock();
  182. double exec_time = static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC;
  183. // 输出执行时间
  184. std::cout << "Execution time: " << exec_time << " seconds" << std::endl;
  185. }
  186. //推理完成
  187. //std::cout << result_array[2] << std::endl;
  188. //解析结果
  189. std::vector<float> output(result_array, result_array + output_data_length);
  190. std::vector<cv::Rect> boxes;
  191. std::vector<float> confs;
  192. std::vector<int> classIds;
  193. int numClasses = (int)output_node_dim.d[2] - 5;
  194. float confThreshold = 0.5;//设定阈值
  195. for (auto it = output.begin(); it != output.begin() + output_data_length; it += output_node_dim.d[2])
  196. {
  197. float clsConf = *(it + 4);//object scores
  198. if (clsConf > confThreshold)
  199. {
  200. int centerX = (int)(*it);
  201. int centerY = (int)(*(it + 1));
  202. int width = (int)(*(it + 2));
  203. int height = (int)(*(it + 3));
  204. int x1 = centerX - width / 2;
  205. int y1 = centerY - height / 2;
  206. boxes.emplace_back(cv::Rect(x1, y1, width, height));
  207. // first 5 element are x y w h and obj confidence
  208. int bestClassId = -1;
  209. float bestConf = 0.0;
  210. for (int i = 5; i < numClasses + 5; i++)
  211. {
  212. if ((*(it + i)) > bestConf)
  213. {
  214. bestConf = it[i];
  215. bestClassId = i - 5;
  216. }
  217. }
  218. //confs.emplace_back(bestConf * clsConf);
  219. confs.emplace_back(clsConf);
  220. classIds.emplace_back(bestClassId);
  221. }
  222. }
  223. float iouThreshold = 0.5;
  224. std::vector<int> indices;
  225. // Perform non maximum suppression to eliminate redundant overlapping boxes with
  226. // lower confidences极大值抑制
  227. cv::dnn::NMSBoxes(boxes, confs, confThreshold, iouThreshold, indices);
  228. //随机数种子
  229. RNG rng((unsigned)time(NULL));
  230. for (size_t i = 0; i < indices.size(); ++i)
  231. {
  232. int index = indices[i];
  233. int colorR = rng.uniform(0, 255);
  234. int colorG = rng.uniform(0, 255);
  235. int colorB = rng.uniform(0, 255);
  236. //保留两位小数
  237. float scores = round(confs[index] * 100) / 100;
  238. std::ostringstream oss;
  239. oss << scores;
  240. rectangle(dstimg, Point(boxes[index].tl().x, boxes[index].tl().y), Point(boxes[index].br().x, boxes[index].br().y), Scalar(colorR, colorG, colorB), 1.5);
  241. putText(dstimg, class_names[classIds[index]] + " " + oss.str(), Point(boxes[index].tl().x, boxes[index].tl().y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(colorR, colorG, colorB), 2);
  242. }
  243. imshow("检测结果", dstimg);
  244. cv::waitKey();
  245. return 0;
  246. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/553091
推荐阅读
相关标签
  

闽ICP备14008679号