TensorRT转化代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.6KB

  1. #include "cuda_utils.h"
  2. #include "logging.h"
  3. #include "utils.h"
  4. #include "preprocess.h"
  5. #include "postprocess.h"
  6. #include "model.h"
  7. #include <iostream>
  8. #include <chrono>
  9. #include <cmath>
  10. using namespace nvinfer1;
  11. static Logger gLogger;
  12. const static int kOutputSize = kMaxNumOutputBbox * sizeof(Detection) / sizeof(float) + 1;
  13. bool parse_args(int argc, char** argv, std::string& wts, std::string& engine, bool& is_p6, float& gd, float& gw, std::string& img_dir) {
  14. if (argc < 4) return false;
  15. if (std::string(argv[1]) == "-s" && (argc == 5 || argc == 7)) {
  16. wts = std::string(argv[2]);
  17. engine = std::string(argv[3]);
  18. auto net = std::string(argv[4]);
  19. if (net[0] == 'n') {
  20. gd = 0.33;
  21. gw = 0.25;
  22. } else if (net[0] == 's') {
  23. gd = 0.33;
  24. gw = 0.50;
  25. } else if (net[0] == 'm') {
  26. gd = 0.67;
  27. gw = 0.75;
  28. } else if (net[0] == 'l') {
  29. gd = 1.0;
  30. gw = 1.0;
  31. } else if (net[0] == 'x') {
  32. gd = 1.33;
  33. gw = 1.25;
  34. } else if (net[0] == 'c' && argc == 7) {
  35. gd = atof(argv[5]);
  36. gw = atof(argv[6]);
  37. } else {
  38. return false;
  39. }
  40. if (net.size() == 2 && net[1] == '6') {
  41. is_p6 = true;
  42. }
  43. } else if (std::string(argv[1]) == "-d" && argc == 4) {
  44. engine = std::string(argv[2]);
  45. img_dir = std::string(argv[3]);
  46. } else {
  47. return false;
  48. }
  49. return true;
  50. }
  51. void prepare_buffers(ICudaEngine* engine, float** gpu_input_buffer, float** gpu_output_buffer, float** cpu_output_buffer) {
  52. assert(engine->getNbBindings() == 2);
  53. // In order to bind the buffers, we need to know the names of the input and output tensors.
  54. // Note that indices are guaranteed to be less than IEngine::getNbBindings()
  55. const int inputIndex = engine->getBindingIndex(kInputTensorName);
  56. const int outputIndex = engine->getBindingIndex(kOutputTensorName);
  57. assert(inputIndex == 0);
  58. assert(outputIndex == 1);
  59. // Create GPU buffers on device
  60. CUDA_CHECK(cudaMalloc((void**)gpu_input_buffer, kBatchSize * 3 * kInputH * kInputW * sizeof(float)));
  61. CUDA_CHECK(cudaMalloc((void**)gpu_output_buffer, kBatchSize * kOutputSize * sizeof(float)));
  62. *cpu_output_buffer = new float[kBatchSize * kOutputSize];
  63. }
  64. void infer(IExecutionContext& context, cudaStream_t& stream, void** gpu_buffers, float* output, int batchsize) {
  65. context.enqueue(batchsize, gpu_buffers, stream, nullptr);
  66. CUDA_CHECK(cudaMemcpyAsync(output, gpu_buffers[1], batchsize * kOutputSize * sizeof(float), cudaMemcpyDeviceToHost, stream));
  67. cudaStreamSynchronize(stream);
  68. }
  69. void serialize_engine(unsigned int max_batchsize, bool& is_p6, float& gd, float& gw, std::string& wts_name, std::string& engine_name) {
  70. // Create builder
  71. IBuilder* builder = createInferBuilder(gLogger);
  72. IBuilderConfig* config = builder->createBuilderConfig();
  73. // Create model to populate the network, then set the outputs and create an engine
  74. ICudaEngine *engine = nullptr;
  75. if (is_p6) {
  76. engine = build_det_p6_engine(max_batchsize, builder, config, DataType::kFLOAT, gd, gw, wts_name);
  77. } else {
  78. engine = build_det_engine(max_batchsize, builder, config, DataType::kFLOAT, gd, gw, wts_name);
  79. }
  80. assert(engine != nullptr);
  81. // Serialize the engine
  82. IHostMemory* serialized_engine = engine->serialize();
  83. assert(serialized_engine != nullptr);
  84. // Save engine to file
  85. std::ofstream p(engine_name, std::ios::binary);
  86. if (!p) {
  87. std::cerr << "Could not open plan output file" << std::endl;
  88. assert(false);
  89. }
  90. p.write(reinterpret_cast<const char*>(serialized_engine->data()), serialized_engine->size());
  91. // Close everything down
  92. engine->destroy();
  93. config->destroy();
  94. serialized_engine->destroy();
  95. builder->destroy();
  96. }
  97. void deserialize_engine(std::string& engine_name, IRuntime** runtime, ICudaEngine** engine, IExecutionContext** context) {
  98. std::ifstream file(engine_name, std::ios::binary);
  99. if (!file.good()) {
  100. std::cerr << "read " << engine_name << " error!" << std::endl;
  101. assert(false);
  102. }
  103. size_t size = 0;
  104. file.seekg(0, file.end);
  105. size = file.tellg();
  106. file.seekg(0, file.beg);
  107. char* serialized_engine = new char[size];
  108. assert(serialized_engine);
  109. file.read(serialized_engine, size);
  110. file.close();
  111. *runtime = createInferRuntime(gLogger);
  112. assert(*runtime);
  113. *engine = (*runtime)->deserializeCudaEngine(serialized_engine, size);
  114. assert(*engine);
  115. *context = (*engine)->createExecutionContext();
  116. assert(*context);
  117. delete[] serialized_engine;
  118. }
  119. int main(int argc, char** argv) {
  120. cudaSetDevice(kGpuId);
  121. std::string wts_name = "";
  122. std::string engine_name = "";
  123. bool is_p6 = false;
  124. float gd = 0.0f, gw = 0.0f;
  125. std::string img_dir;
  126. if (!parse_args(argc, argv, wts_name, engine_name, is_p6, gd, gw, img_dir)) {
  127. std::cerr << "arguments not right!" << std::endl;
  128. std::cerr << "./yolov5_det -s [.wts] [.engine] [n/s/m/l/x/n6/s6/m6/l6/x6 or c/c6 gd gw] // serialize model to plan file" << std::endl;
  129. std::cerr << "./yolov5_det -d [.engine] ../images // deserialize plan file and run inference" << std::endl;
  130. return -1;
  131. }
  132. // Create a model using the API directly and serialize it to a file
  133. if (!wts_name.empty()) {
  134. serialize_engine(kBatchSize, is_p6, gd, gw, wts_name, engine_name);
  135. return 0;
  136. }
  137. // Deserialize the engine from file
  138. IRuntime* runtime = nullptr;
  139. ICudaEngine* engine = nullptr;
  140. IExecutionContext* context = nullptr;
  141. deserialize_engine(engine_name, &runtime, &engine, &context);
  142. cudaStream_t stream;
  143. CUDA_CHECK(cudaStreamCreate(&stream));
  144. // Init CUDA preprocessing
  145. cuda_preprocess_init(kMaxInputImageSize);
  146. // Prepare cpu and gpu buffers
  147. float* gpu_buffers[2];
  148. float* cpu_output_buffer = nullptr;
  149. prepare_buffers(engine, &gpu_buffers[0], &gpu_buffers[1], &cpu_output_buffer);
  150. // Read images from directory
  151. std::vector<std::string> file_names;
  152. if (read_files_in_dir(img_dir.c_str(), file_names) < 0) {
  153. std::cerr << "read_files_in_dir failed." << std::endl;
  154. return -1;
  155. }
  156. // batch predict
  157. for (size_t i = 0; i < file_names.size(); i += kBatchSize) {
  158. // Get a batch of images
  159. std::vector<cv::Mat> img_batch;
  160. std::vector<std::string> img_name_batch;
  161. for (size_t j = i; j < i + kBatchSize && j < file_names.size(); j++) {
  162. cv::Mat img = cv::imread(img_dir + "/" + file_names[j]);
  163. img_batch.push_back(img);
  164. img_name_batch.push_back(file_names[j]);
  165. }
  166. // Preprocess
  167. cuda_batch_preprocess(img_batch, gpu_buffers[0], kInputW, kInputH, stream);
  168. // Run inference
  169. auto start = std::chrono::system_clock::now();
  170. infer(*context, stream, (void**)gpu_buffers, cpu_output_buffer, kBatchSize);
  171. auto end = std::chrono::system_clock::now();
  172. std::cout << "inference time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
  173. // NMS
  174. std::vector<std::vector<Detection>> res_batch;
  175. batch_nms(res_batch, cpu_output_buffer, img_batch.size(), kOutputSize, kConfThresh, kNmsThresh);
  176. // Draw bounding boxes
  177. draw_bbox(img_batch, res_batch);
  178. // Save images
  179. for (size_t j = 0; j < img_batch.size(); j++) {
  180. cv::imwrite("_" + img_name_batch[j], img_batch[j]);
  181. }
  182. }
  183. // Release stream and buffers
  184. cudaStreamDestroy(stream);
  185. CUDA_CHECK(cudaFree(gpu_buffers[0]));
  186. CUDA_CHECK(cudaFree(gpu_buffers[1]));
  187. delete[] cpu_output_buffer;
  188. cuda_preprocess_destroy();
  189. // Destroy the engine
  190. context->destroy();
  191. engine->destroy();
  192. runtime->destroy();
  193. // Print histogram of the output distribution
  194. // std::cout << "\nOutput:\n\n";
  195. // for (unsigned int i = 0; i < kOutputSize; i++) {
  196. // std::cout << prob[i] << ", ";
  197. // if (i % 10 == 0) std::cout << std::endl;
  198. // }
  199. // std::cout << std::endl;
  200. return 0;
  201. }