Apollo 11.0
自动驾驶开放平台
semantic_lstm_vehicle_torch_model.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 *****************************************************************************/
16
18
19#include <string>
20#include <utility>
21#include <vector>
22
23#include "modules/prediction/proto/prediction_conf.pb.h"
24
25#include "cyber/common/file.h"
27
28namespace apollo {
29namespace prediction {
30
32 ModelConf model_config;
33 int status;
34
35 if (init_ != 0) {
36 return true;
37 }
38
39 std::string class_name =
40 abi::__cxa_demangle(typeid(*this).name(), 0, 0, &status);
41 std::string default_config_path =
43 ->GetPluginConfPath<ModelBase>(class_name,
44 "conf/default_conf.pb.txt");
45
46 if (!cyber::common::GetProtoFromFile(default_config_path, &model_config)) {
47 AERROR << "Unable to load model conf file: " << default_config_path;
48 return false;
49 }
50 model_path_ = model_config.model_path();
51 init_ = 1;
52
53 return LoadModel();
54}
55
57 auto device = torch::Device(torch::kCPU);
58 if (torch::cuda::is_available()) {
59 device = torch::Device(torch::kCUDA);
60 }
61
62 model_instance_ = torch::jit::load(model_path_, device);
63
64 torch::set_num_threads(1);
65
66 // Fake intput for the first frame
67 torch::Tensor img_tensor = torch::randn({1, 3, 224, 224});
68 torch::Tensor obstacle_pos = torch::randn({1, 20, 2});
69 torch::Tensor obstacle_pos_step = torch::randn({1, 20, 2});
70 std::vector<torch::jit::IValue> torch_inputs;
71 torch::Tensor torch_default_output_tensor;
72
73 torch_inputs.push_back(c10::ivalue::Tuple::create(
74 {std::move(img_tensor.to(device)), std::move(obstacle_pos.to(device)),
75 std::move(obstacle_pos_step.to(device))}));
76
77 // warm up to avoid very slow first inference later
78 WarmUp(torch_inputs, &model_instance_, &torch_default_output_tensor);
79 return true;
80}
81
83 const std::vector<void*>& input_buffer, unsigned int input_size,
84 std::vector<void*>* output_buffer, unsigned int output_size) {
85 ACHECK(input_size == input_buffer.size() && input_size == 3);
86 ACHECK(output_size == output_buffer->size() && output_size == 1);
87
88 if (init_ == 0) {
89 Init();
90 }
91
92 auto device = torch::Device(torch::kCPU);
93 if (torch::cuda::is_available()) {
94 device = torch::Device(torch::kCUDA);
95 }
96 torch::Tensor img_tensor =
97 torch::from_blob(input_buffer[0], {1, 3, 224, 224});
98 torch::Tensor obstacle_pos = torch::from_blob(input_buffer[1], {1, 20, 2});
99 torch::Tensor obstacle_pos_step =
100 torch::from_blob(input_buffer[2], {1, 20, 2});
101
102 std::vector<torch::jit::IValue> torch_inputs;
103
104 torch_inputs.push_back(c10::ivalue::Tuple::create(
105 {std::move(img_tensor.to(device)), std::move(obstacle_pos.to(device)),
106 std::move(obstacle_pos_step.to(device))}));
107
108 torch::Tensor torch_output_tensor =
109 model_instance_.forward(torch_inputs).toTensor().to(torch::kCPU);
110 memcpy((*output_buffer)[0], torch_output_tensor.data_ptr<float>(),
111 1 * 30 * 2 * sizeof(float));
112 return true;
113}
114
116
117} // namespace prediction
118} // namespace apollo
std::string GetPluginConfPath(const std::string &class_name, const std::string &conf_name)
get plugin configuration file location
static PluginManager * Instance()
get singleton instance of PluginManager
virtual bool Init()
parse model description class and load the model
virtual void Destory()
free all memory requested, gpu or cpu
virtual bool Inference(const std::vector< void * > &input_buffer, unsigned int input_size, std::vector< void * > *output_buffer, unsigned int output_size)
performing network inference
first check imutoantoffset saved in device
Definition readme.txt:2
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
void WarmUp(const std::vector< torch::jit::IValue > &torch_inputs, torch::jit::script::Module *model, at::Tensor *default_output_ptr)
warm up function to avoid slowly inference of torch model
Definition warm_up.cc:28
class register implement
Definition arena_queue.h:37
warm-up function for torch model to avoid first multiple slowly inference