Jetson Linux API Reference

36.4 Release
trt_inference.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2016-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef TRT_INFERENCE_H_
32 #define TRT_INFERENCE_H_
33 
34 #include <fstream>
35 #include <queue>
36 #include "NvInfer.h"
37 #include <opencv2/objdetect/objdetect.hpp>
38 using namespace nvinfer1;
39 using namespace std;
40 
41 // Model Index
42 #define GOOGLENET_SINGLE_CLASS 0
43 #define GOOGLENET_THREE_CLASS 1
44 #define RESNET_THREE_CLASS 2
45 
46 class Logger;
47 
48 class Profiler;
49 
51 {
52 public:
53  //net related parameter
54  int getNetWidth() const;
55 
56  int getNetHeight() const;
57 
58  uint32_t getBatchSize() const;
59 
60  int getChannel() const;
61 
62  int getModelClassCnt() const;
63 
64  void* getScales() const;
65 
66  void* getOffsets() const;
67 
68  // Buffer is allocated in TRT_Conxtex,
69  // Expose this interface for inputing data
70  void*& getBuffer(const int& index);
71 
72  float*& getInputBuf();
73 
74  uint32_t getNumTrtInstances() const;
75 
76  void setBatchSize(const uint32_t& batchsize);
77 
78  void setDumpResult(const bool& dump_result);
79 
80  void setTrtProfilerEnabled(const bool& enable_trt_profiler);
81 
82  int getFilterNum() const;
83  void setFilterNum(const unsigned int& filter_num);
84 
85  TRT_Context();
86 
87  void setModelIndex(int modelIndex);
88 
89  void buildTrtContext(const string& modelfile, bool bUseCPUBuf = false);
90 
91  void doInference(
92  queue< vector<cv::Rect> >* rectList_queue,
93  float *input = NULL);
94 
95  void destroyTrtContext(bool bUseCPUBuf = false);
96 
97  ~TRT_Context();
98 
99 private:
100  int net_width;
101  int net_height;
102  int filter_num;
103  void **buffers;
104  float *input_buf;
105  float *output_cov_buf;
106  float *output_bbox_buf;
107  void* offset_gpu;
108  void* scales_gpu;
109  float helnet_scale[4];
110  IRuntime *runtime;
111  ICudaEngine *engine;
112  IExecutionContext *context;
113  uint32_t *pResultArray;
114  int channel; //input file's channel
115  int num_bindings;
116  int trtinstance_num; //inference channel num
117  int batch_size;
118  int mode;
119  bool dump_result;
120  ofstream fstream;
121  bool enable_trt_profiler;
122  IHostMemory *trtModelStream{nullptr};
123  vector<string> outputs;
124  string result_file;
125  Logger *pLogger;
126  Profiler *pProfiler;
127  int frame_num;
128  uint64_t elapsed_frame_num;
129  uint64_t elapsed_time;
130  int inputIndex;
131  int outputIndex;
132  int outputIndexBBOX;
133  Dims3 inputDims;
134  Dims3 outputDims;
135  Dims3 outputDimsBBOX;
136  size_t inputSize;
137  size_t outputSize;
138  size_t outputSizeBBOX;
139 
140  struct {
141  const int classCnt;
142  float THRESHOLD[3];
143  const char *INPUT_BLOB_NAME;
144  const char *OUTPUT_BLOB_NAME;
145  const char *OUTPUT_BBOX_NAME;
146  const int STRIDE;
147  const int WORKSPACE_SIZE;
148  int offsets[3];
149  float input_scale[3];
150  float bbox_output_scales[4];
151  const int ParseFunc_ID;
152  } *g_pModelNetAttr, gModelNetAttr[4] = {
153  {
154  // GOOGLENET_SINGLE_CLASS
155  1,
156  {0.8, 0, 0},
157  "data",
158  "coverage",
159  "bboxes",
160  4,
161  450 * 1024 * 1024,
162  {0, 0, 0},
163  {1.0f, 1.0f, 1.0f},
164  {1, 1, 1, 1},
165  0
166  },
167 
168  {
169  // GOOGLENET_THREE_CLASS
170  3,
171  {0.6, 0.6, 1.0}, //People, Motorbike, Car
172  "data",
173  "Layer16_cov",
174  "Layer16_bbox",
175  16,
176  110 * 1024 * 1024,
177  {124, 117, 104},
178  {1.0f, 1.0f, 1.0f},
179  {-640, -368, 640, 368},
180  0
181  },
182 
183  {
184  // RESNET_THREE_CLASS
185  4,
186  {0.1, 0.1, 0.1}, //People, Motorbike, Car
187  "data",
188  "Layer7_cov",
189  "Layer7_bbox",
190  16,
191  110 * 1024 * 1024,
192  {0, 0, 0},
193  {0.0039215697906911373, 0.0039215697906911373, 0.0039215697906911373},
194  {-640, -368, 640, 368},
195  1
196  },
197  };
198  void parseBbox(vector<cv::Rect>* rectList, int batch_th);
199  void ParseResnet10Bbox(vector<cv::Rect>* rectList, int batch_th);
200  void allocateMemory(bool bUseCPUBuf);
201  void releaseMemory(bool bUseCPUBuf);
202 };
203 
204 #endif
TRT_Context::OUTPUT_BBOX_NAME
const char * OUTPUT_BBOX_NAME
Definition: trt_inference.h:145
TRT_Context::OUTPUT_BLOB_NAME
const char * OUTPUT_BLOB_NAME
Definition: trt_inference.h:144
TRT_Context::INPUT_BLOB_NAME
const char * INPUT_BLOB_NAME
Definition: trt_inference.h:143
TRT_Context::classCnt
const int classCnt
Definition: trt_inference.h:141
TRT_Context::WORKSPACE_SIZE
const int WORKSPACE_SIZE
Definition: trt_inference.h:147
TRT_Context::ParseFunc_ID
const int ParseFunc_ID
Definition: trt_inference.h:151
TRT_Context::STRIDE
const int STRIDE
Definition: trt_inference.h:146
TRT_Context
Definition: trt_inference.h:50