Jetson Linux API 参考文档

32.7.4 版本
trt_inference.h
跳转至此文件的文档。
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION。保留所有权利。
3  *
4  * 源代码和二进制形式的再分发和使用,无论是否经过
5  * 修改,均被允许,前提是满足以下条件
6  * :
7  * * 源代码的再分发必须保留上述版权声明
8  * 、此条件列表以及以下免责声明。
9  * * 二进制形式的再分发必须在上述版权声明中复制
10  * 、此条件列表以及以下免责声明,位于
11  * 文档和/或随发行版提供的其他材料中。
12  * * NVIDIA CORPORATION 的名称及其
13  * 贡献者的名称不得用于认可或推广衍生自
14  * 本软件的产品,除非获得明确的事先书面许可。
15  *
16  * 本软件由版权所有者``按原样''提供,并且不作任何
17  * 明示或暗示的保证,包括但不限于
18  * 对适销性和特定用途适用性的暗示保证。
19  * 目的不作担保。在任何情况下,版权所有者或
20  * 贡献者均不对任何直接、间接、偶然、特殊、
21  * 惩戒性或后果性损害(包括但不限于
22  * 采购替代商品或服务; 无法使用、数据或
23  * 利润; 或业务中断)承担责任,无论其成因和任何责任理论如何
24  * 的责任,无论是合同、严格责任还是侵权
25  * (包括疏忽或其他)因使用而以任何方式引起的
26  * 本软件,即使已被告知可能发生此类损害。
27  */
28 #ifndef TRT_INFERENCE_H_
29 #define TRT_INFERENCE_H_
30 
31 #include <fstream>
32 #include <queue>
33 #include "NvInfer.h"
34 #include "NvCaffeParser.h"
35 #include "NvOnnxParser.h"
36 #include "opencv2/video/tracking.hpp"
37 #include "opencv2/imgproc/imgproc.hpp"
38 #include "opencv2/highgui/highgui.hpp"
39 #include <opencv2/objdetect/objdetect.hpp>
40 using namespace nvinfer1;
41 using namespace nvcaffeparser1;
42 using namespace nvonnxparser;
43 using namespace std;
44 
45 // 模型索引
46 #define GOOGLENET_SINGLE_CLASS 0
47 #define GOOGLENET_THREE_CLASS 1
48 #define RESNET_THREE_CLASS 2
49 
50 class Logger;
51 
52 class Profiler;
53 
55 {
56 public
57  // 网络相关参数
58  int getNetWidth() const;
59 
60  int getNetHeight() const;
61 
62  uint32_t getBatchSize() const;
63 
64  int getChannel() const;
65 
66  int getModelClassCnt() const;
67 
68  void* getScales() const;
69 
70  void* getOffsets() const;
71 
72  // 缓冲区在 TRT_Context 中分配,
73  // 暴露此接口以输入数据
74  void*& getBuffer(const int& index);
75 
76  float*& getInputBuf();
77 
78  uint32_t getNumTrtInstances() const;
79 
80  //0 fp16 1 fp32 2 int8
81  void setMode(const int& mode);
82 
83  void setBatchSize(const uint32_t& batchsize);
84 
85  void setDumpResult(const bool& dump_result);
86 
87  void setTrtProfilerEnabled(const bool& enable_trt_profiler);
88 
89  int getFilterNum() const;
90  void setFilterNum(const unsigned int& filter_num);
91 
92  TRT_Context();
93 
94  void setModelIndex(int modelIndex);
95 
96  void buildTrtContext(const string& deployfile,
97  const string& modelfile, bool bUseCPUBuf = false, bool isOnnxModel = false);
98 
99  void doInference(
100  queue< vector<cv::Rect> >* rectList_queue,
101  float *input = NULL);
102 
103  void destroyTrtContext(bool bUseCPUBuf = false);
104 
105  ~TRT_Context();
106 
107 private
108  int net_width;
109  int net_height;
110  int filter_num;
111  void **buffers;
112  float *input_buf;
113  float *output_cov_buf;
114  float *output_bbox_buf;
115  void* offset_gpu;
116  void* scales_gpu;
117  float helnet_scale[4];
118  IRuntime *runtime;
119  ICudaEngine *engine;
120  IExecutionContext *context;
121  uint32_t *pResultArray;
122  int channel; //输入文件通道数
123  int num_bindings;
124  int trtinstance_num; //推理通道数
125  int batch_size;
126  int mode;
127  bool dump_result;
128  ofstream fstream;
129  bool enable_trt_profiler;
130  bool is_onnx_model;
131  IHostMemory *trtModelStream{nullptr};
132  vector<string> outputs;
133  string result_file;
134  Logger *pLogger;
135  Profiler *pProfiler;
136  int frame_num;
137  uint64_t elapsed_frame_num;
138  uint64_t elapsed_time;
139  int inputIndex;
140  int outputIndex;
141  int outputIndexBBOX;
142  Dims3 inputDims;
143  Dims3 outputDims;
144  Dims3 outputDimsBBOX;
145  size_t inputSize;
146  size_t outputSize;
147  size_t outputSizeBBOX;
148 
149  struct {
150  const int classCnt;
151  float THRESHOLD[3];
152  const char *INPUT_BLOB_NAME;
153  const char *OUTPUT_BLOB_NAME;
154  const char *OUTPUT_BBOX_NAME;
155  const int STRIDE;
156  const int WORKSPACE_SIZE;
157  int offsets[3];
158  float input_scale[3];
159  float bbox_output_scales[4];
160  const int ParseFunc_ID;
161  } *g_pModelNetAttr, gModelNetAttr[4] = {
162  {
163  // GOOGLENET_SINGLE_CLASS
164  1,
165  {0.8, 0, 0},
166  "data",
167  "coverage",
168  "bboxes",
169  4,
170  450 * 1024 * 1024,
171  {0, 0, 0},
172  {1.0f, 1.0f, 1.0f},
173  {1, 1, 1, 1},
174  0
175  },
176 
177  {
178  // GOOGLENET_THREE_CLASS
179  3,
180  {0.6, 0.6, 1.0}, //人, 摩托车, 汽车
181  "data",
182  "Layer16_cov",
183  "Layer16_bbox",
184  16,
185  110 * 1024 * 1024,
186  {124, 117, 104},
187  {1.0f, 1.0f, 1.0f},
188  {-640, -368, 640, 368},
189  0
190  },
191 
192  {
193  // RESNET_THREE_CLASS
194  4,
195  {0.1, 0.1, 0.1}, //人, 摩托车, 汽车
196  "data",
197  "Layer7_cov",
198  "Layer7_bbox",
199  16,
200  110 * 1024 * 1024,
201  {0, 0, 0},
202  {0.0039215697906911373, 0.0039215697906911373, 0.0039215697906911373},
203  {-640, -368, 640, 368},
204  1
205  },
206  };
207  enum Mode_type{
208  MODE_FP16 = 0,
209  MODE_FP32 = 1,
210  MODE_INT8 = 2
211  };
212  int parseNet(const string& deployfile);
213  void parseBbox(vector<cv::Rect>* rectList, int batch_th);
214  void ParseResnet10Bbox(vector<cv::Rect>* rectList, int batch_th);
215  void allocateMemory(bool bUseCPUBuf);
216  void releaseMemory(bool bUseCPUBuf);
217  void caffeToTRTModel(const string& deployfile, const string& modelfile);
218  void onnxToTRTModel(const string& modelfile);
219 };
220 
221 #endif
TRT_Context::OUTPUT_BBOX_NAME
const char * OUTPUT_BBOX_NAME
定义位置: trt_inference.h:154
TRT_Context::OUTPUT_BLOB_NAME
const char * OUTPUT_BLOB_NAME
定义位置: trt_inference.h:153
TRT_Context::INPUT_BLOB_NAME
const char * INPUT_BLOB_NAME
定义位置: trt_inference.h:152
TRT_Context::classCnt
const int classCnt
定义位置: trt_inference.h:150
TRT_Context::WORKSPACE_SIZE
const int WORKSPACE_SIZE
定义位置: trt_inference.h:156
TRT_Context::ParseFunc_ID
const int ParseFunc_ID
定义位置: trt_inference.h:160
TRT_Context::STRIDE
const int STRIDE
定义位置: trt_inference.h:155
TRT_Context
定义位置: trt_inference.h:54
. All rights reserved.