Apollo 11.0
自动驾驶开放平台
torch_net.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 "cyber/common/log.h"
21
22namespace apollo {
23namespace perception {
24namespace inference {
25
27
28TorchNet::TorchNet(const std::string &model_file,
29 const std::vector<std::string> &outputs,
30 const std::vector<std::string> &inputs)
31 : model_file_(model_file), output_names_(outputs), input_names_(inputs) {}
32
33bool TorchNet::Init(const std::map<std::string, std::vector<int>> &shapes) {
34 if (gpu_id_ >= 0) {
35 device_type_ = torch::kCUDA;
36 device_id_ = gpu_id_;
37 } else {
38 device_type_ = torch::kCPU;
39 }
40
41 // Init net
42 torch::Device device(device_type_, device_id_);
43 net_ = torch::jit::load(model_file_, device);
44 net_.eval();
45
46 // add blobs
47 for (const auto &name : input_names_) {
48 auto iter = shapes.find(name);
49 if (iter != shapes.end()) {
50 auto blob = std::make_shared<Blob<float>>(iter->second);
51 blobs_.emplace(name, blob);
52 }
53 }
54
55 for (const auto &name : output_names_) {
56 auto iter = shapes.find(name);
57 if (iter != shapes.end()) {
58 auto blob = std::make_shared<Blob<float>>(iter->second);
59 blobs_.emplace(name, blob);
60 }
61 }
62 return true;
63}
64
65std::shared_ptr<Blob<float>> TorchNet::get_blob(const std::string &name) {
66 auto iter = blobs_.find(name);
67 if (iter == blobs_.end()) {
68 return nullptr;
69 }
70 return iter->second;
71}
72
73bool TorchNet::reshape() { return true; }
74
75bool TorchNet::shape(const std::string &name, std::vector<int> *res) {
76 auto blob = get_blob(name);
77 if (blob == nullptr) {
78 return false;
79 }
80
81 *res = blob->shape();
82 return true;
83}
84
86 torch::Device device(device_type_, device_id_);
87 // Get input data from blob to torch_blob.
88 std::vector<torch::jit::IValue> torch_inputs;
89 for (const auto &name : input_names_) {
90 auto blob = get_blob(name);
91 if (blob != nullptr) {
92 std::vector<int64_t> shape(blob->shape().begin(), blob->shape().end());
93 torch::Tensor torch_blob = torch::from_blob(
94 blob->data()->mutable_cpu_data(), shape, torch::kFloat32);
95 torch_blob = torch_blob.to(device);
96 torch_inputs.push_back(torch_blob);
97 }
98 }
99 // If `out_blob->mutable_cpu_data()` is invoked outside,
100 // HEAD will be set to CPU, and `out_blob->mutable_gpu_data()`
101 // after `enqueue` will copy data from CPU to GPU,
102 // which will overwrite the `inference` results.
103 // `out_blob->gpu_data()` will set HEAD to SYNCED,
104 // then no copy happends after `enqueue`.
105 for (const auto &name : output_names_) {
106 auto blob = get_blob(name);
107 if (blob != nullptr) {
108 blob->gpu_data();
109 }
110 }
111
112 // Infer
113 std::vector<torch::Tensor> output =
114 net_.forward(torch_inputs).toTensorVector();
115
116 // Fill output
117 for (size_t i = 0; i < output_names_.size(); ++i) {
118 auto blob = get_blob(output_names_[i]);
119 if (blob != nullptr && i < output.size()) {
120 std::vector<int64_t> output_size = output[i].sizes().vec();
121 std::vector<int> shape(output_size.begin(), output_size.end());
122 blob->Reshape(shape);
123 blob->set_gpu_data(output[i].data_ptr<float>());
124 }
125 }
126 emptyCache();
127}
128
129} // namespace inference
130} // namespace perception
131} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
base::BlobPtr< float > get_blob(const std::string &name) override
Definition torch_net.cc:65
TorchNet(const std::string &model_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs)
Definition torch_net.cc:28
bool shape(const std::string &name, std::vector< int > *res)
Definition torch_net.cc:75
bool Init(const std::map< std::string, std::vector< int > > &shapes) override
Definition torch_net.cc:33
torch::jit::script::Module net_
Definition torch_net.h:49
first check imutoantoffset saved in device
Definition readme.txt:2
class register implement
Definition arena_queue.h:37