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

#include <paddle_net.h>

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

Public 成员函数

 PaddleNet (const std::string &model_file, const std::string &params_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs)
 
virtual ~PaddleNet ()
 
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 成员函数

bool shape (const std::string &name, std::vector< int > *res)
 

Protected 属性

std::shared_ptr< paddle_infer::Predictor > predictor_
 
- 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_
 

详细描述

在文件 paddle_net.h 第 35 行定义.

构造及析构函数说明

◆ PaddleNet()

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

在文件 paddle_net.cc 第 26 行定义.

30 : model_file_(model_file),
31 params_file_(params_file),
32 output_names_(outputs),
33 input_names_(inputs) {}

◆ ~PaddleNet()

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

在文件 paddle_net.cc 第 35 行定义.

35{}

成员函数说明

◆ get_blob()

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

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

在文件 paddle_net.cc 第 167 行定义.

167 {
168 auto iter = blobs_.find(name);
169 if (iter == blobs_.end()) {
170 return nullptr;
171 }
172 return iter->second;
173}

◆ Infer()

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

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

在文件 paddle_net.cc 第 108 行定义.

108 {
109 // reshape and get input data from blob to paddle_blob.
110 for (const auto& name : input_names_) {
111 auto blob = get_blob(name);
112 auto paddle_blob = predictor_->GetInputHandle(name);
113 if (paddle_blob != nullptr && blob != nullptr) {
114 paddle_blob->Reshape(blob->shape());
115 paddle_blob->CopyFromCpu(blob->cpu_data());
116 }
117 }
118 // If `out_blob->mutable_cpu_data()` is invoked outside,
119 // HEAD will be set to CPU, and `out_blob->mutable_gpu_data()`
120 // after `enqueue` will copy data from CPU to GPU,
121 // which will overwrite the `inference` results.
122 // `out_blob->gpu_data()` will set HEAD to SYNCED,
123 // then no copy happends after `enqueue`.
124 for (const auto& name : output_names_) {
125 auto blob = get_blob(name);
126 if (blob != nullptr) {
127 blob->gpu_data();
128 }
129 }
130
131 // Infer
132 predictor_->Run();
133
134 // Fill output
135 for (const auto& name : output_names_) {
136 auto blob = get_blob(name);
137 auto paddle_blob = predictor_->GetOutputHandle(name);
138 if (blob != nullptr && paddle_blob != nullptr) {
139 std::vector<int> paddle_blob_shape = paddle_blob->shape();
140 blob->Reshape(paddle_blob_shape);
141 // label : int64_t
142 if (paddle_blob->type() == paddle_infer::INT64) {
143 ACHECK(1 == paddle_blob_shape.size());
144 std::vector<int64_t> label_i(paddle_blob_shape.at(0));
145 paddle_blob->CopyToCpu(label_i.data());
146 std::vector<float> label_f(label_i.data(),
147 label_i.data() + paddle_blob_shape.at(0));
148 memcpy(blob->mutable_cpu_data(), label_f.data(),
149 paddle_blob_shape.at(0) * sizeof(float));
150 } else {
151 paddle_blob->CopyToCpu(blob->mutable_cpu_data());
152 }
153 }
154 }
155}
std::shared_ptr< paddle_infer::Predictor > predictor_
Definition paddle_net.h:51
base::BlobPtr< float > get_blob(const std::string &name) override
#define ACHECK(cond)
Definition log.h:80

◆ Init()

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

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

在文件 paddle_net.cc 第 37 行定义.

37 {
38 paddle_infer::Config config;
39 config.SetModel(model_file_, params_file_);
40
41 if (gpu_id_ >= 0) {
42 config.EnableUseGpu(MemoryPoolInitSizeMb, gpu_id_);
43 }
44
45 if (FLAGS_use_trt) {
46 paddle::AnalysisConfig::Precision precision;
47 if (FLAGS_trt_precision == 0) {
48 precision = paddle_infer::PrecisionType::kFloat32;
49 } else if (FLAGS_trt_precision == 1) {
50 precision = paddle_infer::PrecisionType::kInt8;
51 } else if (FLAGS_trt_precision == 2) {
52 precision = paddle_infer::PrecisionType::kHalf;
53 } else {
54 AERROR << "Tensorrt type can only support 0, 1, 2, but recieved is"
55 << FLAGS_trt_precision << "\n";
56 return false;
57 }
58 config.EnableTensorRtEngine(1 << 30, 1, 3, precision, FLAGS_trt_use_static,
59 FLAGS_use_calibration);
60
61 if (FLAGS_collect_shape_info) {
62 config.CollectShapeRangeInfo(FLAGS_dynamic_shape_file);
63 }
64
65 if (FLAGS_use_dynamicshape)
66 config.EnableTunedTensorRtDynamicShape(FLAGS_dynamic_shape_file, true);
67 }
68
69 config.EnableMemoryOptim();
70 config.SwitchIrOptim(true);
71 predictor_ = paddle_infer::CreatePredictor(config);
72 if (predictor_ == nullptr) {
73 return false;
74 }
75
76 // Check input and output
77 auto input_names = predictor_->GetInputNames();
78 auto output_names = predictor_->GetOutputNames();
79
80 std::vector<std::string> exist_names;
81 for (const std::string& name : input_names_) {
82 if (std::find(input_names.begin(), input_names.end(), name) !=
83 input_names.end()) {
84 exist_names.push_back(name);
85 }
86 }
87 input_names_ = exist_names;
88
89 exist_names.clear();
90 for (const std::string& name : output_names_) {
91 if (std::find(output_names.begin(), output_names.end(), name) !=
92 output_names.end()) {
93 exist_names.push_back(name);
94 }
95 }
96 output_names_ = exist_names;
97
98 // add blobs
99 for (const auto& shape : shapes) {
100 auto blob =
101 std::make_shared<apollo::perception::base::Blob<float>>(shape.second);
102 blobs_.emplace(shape.first, blob);
103 }
104
105 return true;
106}
bool shape(const std::string &name, std::vector< int > *res)
#define AERROR
Definition log.h:44
constexpr uint64_t MemoryPoolInitSizeMb
Definition paddle_net.h:33

◆ shape()

bool apollo::perception::inference::PaddleNet::shape ( const std::string &  name,
std::vector< int > *  res 
)
protected

在文件 paddle_net.cc 第 157 行定义.

157 {
158 auto blob = get_blob(name);
159 if (blob == nullptr) {
160 return false;
161 }
162
163 *res = blob->shape();
164 return true;
165}

类成员变量说明

◆ predictor_

std::shared_ptr<paddle_infer::Predictor> apollo::perception::inference::PaddleNet::predictor_
protected

在文件 paddle_net.h 第 51 行定义.


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