Apollo 11.0
自动驾驶开放平台
apollo::perception::inference::SingleBatchInference类 参考

#include <onnx_single_batch_infer.h>

类 apollo::perception::inference::SingleBatchInference 继承关系图:
apollo::perception::inference::SingleBatchInference 的协作图:

Public 成员函数

 SingleBatchInference (const std::string &model_file, const float score_threshold, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs)
 
 SingleBatchInference (const std::string &model_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs)
 
virtual ~SingleBatchInference ()
 
bool Init (const std::map< std::string, std::vector< int > > &shapes) override
 
void Infer () override
 
base::BlobPtr< float > get_blob (const std::string &name) override
 
- Public 成员函数 继承自 apollo::perception::inference::Inference
 Inference ()=default
 
virtual ~Inference ()=default
 
virtual void SetStream (cudaStream_t stream)
 
void set_max_batch_size (const int &batch_size)
 
void set_gpu_id (const int &gpu_id)
 
void set_model_info (const std::string &proto_file, const std::vector< std::string > &net_input_names, const std::vector< std::string > &net_output_names)
 

额外继承的成员函数

- Protected 属性 继承自 apollo::perception::inference::Inference
int max_batch_size_ = 1
 
int gpu_id_ = 0
 
std::string proto_file_ = ""
 
std::vector< std::string > net_output_names_
 
std::vector< std::string > net_input_names_
 

详细描述

在文件 onnx_single_batch_infer.h 第 45 行定义.

构造及析构函数说明

◆ SingleBatchInference() [1/2]

apollo::perception::inference::SingleBatchInference::SingleBatchInference ( const std::string &  model_file,
const float  score_threshold,
const std::vector< std::string > &  outputs,
const std::vector< std::string > &  inputs 
)

在文件 onnx_single_batch_infer.cc 第 88 行定义.

92 : model_file_(model_file),
93 score_threshold_(score_threshold),
94 output_names_(outputs),
95 input_names_(inputs) {}

◆ SingleBatchInference() [2/2]

apollo::perception::inference::SingleBatchInference::SingleBatchInference ( const std::string &  model_file,
const std::vector< std::string > &  outputs,
const std::vector< std::string > &  inputs 
)

在文件 onnx_single_batch_infer.cc 第 97 行定义.

100 : model_file_(model_file), output_names_(outputs), input_names_(inputs) {}

◆ ~SingleBatchInference()

apollo::perception::inference::SingleBatchInference::~SingleBatchInference ( )
virtual

在文件 onnx_single_batch_infer.cc 第 102 行定义.

102{}

成员函数说明

◆ get_blob()

base::BlobPtr< float > apollo::perception::inference::SingleBatchInference::get_blob ( const std::string &  name)
overridevirtual

实现了 apollo::perception::inference::Inference.

在文件 onnx_single_batch_infer.cc 第 80 行定义.

80 {
81 auto iter = blobs_.find(name);
82 if (iter == blobs_.end()) {
83 return nullptr;
84 }
85 return iter->second;
86}

◆ Infer()

void apollo::perception::inference::SingleBatchInference::Infer ( )
overridevirtual

实现了 apollo::perception::inference::Inference.

在文件 onnx_single_batch_infer.cc 第 243 行定义.

243 {
244 BASE_GPU_CHECK(cudaSetDevice(gpu_id_));
245 BASE_GPU_CHECK(cudaStreamSynchronize(stream_));
246
247 for (auto name : input_names_) {
248 auto blob = get_blob(name);
249 if (blob != nullptr) {
250 blob->gpu_data();
251 }
252 }
253 // If `out_blob->mutable_cpu_data()` is invoked outside,
254 // HEAD will be set to CPU, and `out_blob->mutable_gpu_data()`
255 // after `enqueue` will copy data from CPU to GPU,
256 // which will overwrite the `inference` results.
257 // `out_blob->gpu_data()` will set HEAD to SYNCED,
258 // then no copy happends after `enqueue`.
259 for (auto name : output_names_) {
260 auto blob = get_blob(name);
261 if (blob != nullptr) {
262 blob->gpu_data();
263 }
264 }
265
266 {
267 std::lock_guard<std::mutex> lock(mutex_);
268 context_->enqueue(max_batch_size_, &buffers_[0], stream_, nullptr);
269 }
270
271 for (auto name : output_names_) {
272 auto blob = get_blob(name);
273 if (blob != nullptr) {
274 blob->mutable_gpu_data();
275 }
276 }
277}
base::BlobPtr< float > get_blob(const std::string &name) override

◆ Init()

bool apollo::perception::inference::SingleBatchInference::Init ( const std::map< std::string, std::vector< int > > &  shapes)
overridevirtual

实现了 apollo::perception::inference::Inference.

在文件 onnx_single_batch_infer.cc 第 114 行定义.

115 {
116 BASE_GPU_CHECK(cudaSetDevice(gpu_id_));
117 BASE_GPU_CHECK(cudaStreamCreate(&stream_));
118
119 // create a TensorRT model from the onnx model and load it into an engine
120 OnnxToTRTModel(model_file_);
121
122 if (engine_ == nullptr) {
123 AERROR << "Fail to load obstacle ONNX model";
124 return false;
125 }
126
127 // create execution context from the engine
128#if GPU_PLATFORM == NVIDIA
129 context_ = engine_->createExecutionContext();
130 if (context_ == nullptr) {
131 AERROR << "Fail to create Exceution Context";
132 return false;
133 }
134
135 for (const auto& name : output_names_) {
136 auto iter = shapes.find(name);
137 if (iter != shapes.end()) {
138 auto blob = std::make_shared<Blob<float>>(iter->second);
139 blobs_.emplace(name, blob);
140 }
141 }
142
143 for (const auto& name : input_names_) {
144 auto iter = shapes.find(name);
145 if (iter != shapes.end()) {
146 auto blob = std::make_shared<Blob<float>>(iter->second);
147 blobs_.emplace(name, blob);
148 }
149 }
150 buffers_.resize(input_names_.size() + output_names_.size());
151
152 init_blob(&input_names_);
153 init_blob(&output_names_);
154
155#elif GPU_PLATFORM == AMD
156 assert(0 && "OnnxObstacleDetector::Init() is not implemented yet");
157#endif
158 return true;
159}
#define AERROR
Definition log.h:44

该类的文档由以下文件生成: