Apollo 11.0
自动驾驶开放平台
base_obstacle_detector.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 *****************************************************************************/
17
18#include <map>
19#include <vector>
20
21#include "cyber/common/file.h"
22#include "cyber/common/log.h"
25
26namespace apollo {
27namespace perception {
28namespace camera {
29
30bool BaseObstacleDetector::InitNetwork(const common::ModelInfo& model_info,
31 const std::string& model_root) {
32 // Network files
33 std::string proto_file = cyber::common::GetAbsolutePath(
34 model_root, model_info.proto_file().file());
35 std::string weight_file = cyber::common::GetAbsolutePath(
36 model_root, model_info.weight_file().file());
37
38 // Network input and output names
39 std::vector<std::string> input_names =
40 inference::GetBlobNames(model_info.inputs());
41 std::vector<std::string> output_names =
42 inference::GetBlobNames(model_info.outputs());
43
44 // Network type
45 const auto& framework = model_info.framework();
46 std::string plugin_name = model_info.infer_plugin();
47 static const std::string class_namespace = "apollo::perception::inference::";
48 if (model_info.has_infer_plugin() && !plugin_name.empty()) {
49 plugin_name = class_namespace + plugin_name;
52 net_->set_model_info(proto_file, input_names, output_names);
53 AINFO << "net load plugin success: " << plugin_name;
54 } else {
55 net_.reset(inference::CreateInferenceByName(framework, proto_file,
56 weight_file, output_names,
57 input_names, model_root));
58 }
59
60 ACHECK(net_ != nullptr);
61 net_->set_gpu_id(gpu_id_);
62
63 std::map<std::string, std::vector<int>> shape_map;
64 inference::AddShape(&shape_map, model_info.inputs());
65 inference::AddShape(&shape_map, model_info.outputs());
66
67 if (!net_->Init(shape_map)) {
68 AERROR << model_info.name() << "init failed!";
69 return false;
70 }
71 return true;
72}
73
74} // namespace camera
75} // namespace perception
76} // namespace apollo
static PluginManager * Instance()
get singleton instance of PluginManager
std::shared_ptr< Base > CreateInstance(const std::string &derived_class)
create plugin instance of derived class based on Base
virtual bool InitNetwork(const common::ModelInfo &model_info, const std::string &model_root)
Interface for network initialization
std::shared_ptr< inference::Inference > net_
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)
Definition inference.cc:29
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
Definition file.cc:179
std::vector< std::string > GetBlobNames(const google::protobuf::RepeatedPtrField< common::ModelBlob > &model_blobs)
Definition model_util.cc:23
void AddShape(std::map< std::string, std::vector< int > > *shape_map, const google::protobuf::RepeatedPtrField< common::ModelBlob > &model_blobs)
Definition model_util.cc:32
Inference * CreateInferenceByName(const std::string &frame_work, const std::string &proto_file, const std::string &weight_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs, const std::string &model_root)
class register implement
Definition arena_queue.h:37