Apollo 11.0
自动驾驶开放平台
onnx_single_batch_infer.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#pragma once
18
19#include <iostream>
20#include <map>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <utility>
25#include <vector>
26
27#if GPU_PLATFORM == NVIDIA
28#include <NvInferPlugin.h>
29#include <NvInferRuntimeCommon.h>
30#include <NvInferVersion.h>
31
32#include "NvInfer.h"
33#include "NvInferVersion.h"
34#include "NvOnnxParser.h"
35#endif
36
38
39namespace apollo {
40namespace perception {
41namespace inference {
42
43using BlobPtr = std::shared_ptr<apollo::perception::base::Blob<float>>;
44
46 public:
47 SingleBatchInference(const std::string &model_file,
48 const float score_threshold,
49 const std::vector<std::string> &outputs,
50 const std::vector<std::string> &inputs);
51
52 SingleBatchInference(const std::string &model_file,
53 const std::vector<std::string> &outputs,
54 const std::vector<std::string> &inputs);
55
56 virtual ~SingleBatchInference();
57
58 bool Init(const std::map<std::string, std::vector<int>> &shapes) override;
59 void Infer() override;
60 base::BlobPtr<float> get_blob(const std::string &name) override;
61
62 private:
67 bool LoadCache(const std::string &path);
68
69 void init_blob(std::vector<std::string> *names);
75 bool OnnxToTRTModel(const std::string &model_file);
76
77 std::string model_file_;
78 float score_threshold_;
79 std::vector<std::string> output_names_;
80 std::vector<std::string> input_names_;
81 BlobMap blobs_;
82
83 std::mutex mutex_;
84 int bindingIndex_ = 0;
85
86 nvinfer1::ICudaEngine *engine_;
87 nvinfer1::IExecutionContext *context_;
88 nvinfer1::IBuilder *builder_ = nullptr;
89 nvinfer1::IBuilderConfig *builder_config_ = nullptr;
90 nvinfer1::INetworkDefinition *network_ = nullptr;
91
92 std::vector<void *> buffers_;
93 cudaStream_t stream_ = 0;
94 int workspaceSize_ = 1;
95
96 int num_classes_;
97 int kBatchSize;
98};
99
100} // namespace inference
101} // namespace perception
102} // namespace apollo
base::BlobPtr< float > get_blob(const std::string &name) override
bool Init(const std::map< std::string, std::vector< int > > &shapes) override
std::shared_ptr< Blob< Dtype > > BlobPtr
Definition blob.h:313
std::map< std::string, base::BlobPtr< float > > BlobMap
Definition inference.h:39
std::shared_ptr< apollo::perception::base::Blob< float > > BlobPtr
class register implement
Definition arena_queue.h:37