27 const std::string& params_file,
28 const std::vector<std::string>& outputs,
29 const std::vector<std::string>& inputs)
30 : model_file_(model_file),
31 params_file_(params_file),
32 output_names_(outputs),
33 input_names_(inputs) {}
38 paddle_infer::Config config;
39 config.SetModel(model_file_, params_file_);
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;
54 AERROR <<
"Tensorrt type can only support 0, 1, 2, but recieved is"
55 << FLAGS_trt_precision <<
"\n";
58 config.EnableTensorRtEngine(1 << 30, 1, 3, precision, FLAGS_trt_use_static,
59 FLAGS_use_calibration);
61 if (FLAGS_collect_shape_info) {
62 config.CollectShapeRangeInfo(FLAGS_dynamic_shape_file);
65 if (FLAGS_use_dynamicshape)
66 config.EnableTunedTensorRtDynamicShape(FLAGS_dynamic_shape_file,
true);
69 config.EnableMemoryOptim();
70 config.SwitchIrOptim(
true);
71 predictor_ = paddle_infer::CreatePredictor(config);
77 auto input_names =
predictor_->GetInputNames();
78 auto output_names =
predictor_->GetOutputNames();
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) !=
84 exist_names.push_back(name);
87 input_names_ = exist_names;
90 for (
const std::string& name : output_names_) {
91 if (std::find(output_names.begin(), output_names.end(), name) !=
93 exist_names.push_back(name);
96 output_names_ = exist_names;
99 for (
const auto&
shape : shapes) {
101 std::make_shared<apollo::perception::base::Blob<float>>(
shape.second);
102 blobs_.emplace(
shape.first, blob);
110 for (
const auto& name : input_names_) {
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());
124 for (
const auto& name : output_names_) {
126 if (blob !=
nullptr) {
135 for (
const auto& name : output_names_) {
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);
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));
151 paddle_blob->CopyToCpu(blob->mutable_cpu_data());