Apollo 11.0
自动驾驶开放平台
apollo::perception::inference::TorchNet类 参考

#include <torch_net.h>

类 apollo::perception::inference::TorchNet 继承关系图:
apollo::perception::inference::TorchNet 的协作图:

Public 成员函数

 TorchNet (const std::string &model_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs)
 
virtual ~TorchNet ()
 
bool Init (const std::map< std::string, std::vector< int > > &shapes) override
 
void Infer () override
 
base::BlobPtr< float > get_blob (const std::string &name) override
 
- Public 成员函数 继承自 apollo::perception::inference::Inference
 Inference ()=default
 
virtual ~Inference ()=default
 
virtual void SetStream (cudaStream_t stream)
 
void set_max_batch_size (const int &batch_size)
 
void set_gpu_id (const int &gpu_id)
 
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)
 

Protected 成员函数

bool reshape ()
 
bool shape (const std::string &name, std::vector< int > *res)
 

Protected 属性

torch::jit::script::Module net_
 
- Protected 属性 继承自 apollo::perception::inference::Inference
int max_batch_size_ = 1
 
int gpu_id_ = 0
 
std::string proto_file_ = ""
 
std::vector< std::string > net_output_names_
 
std::vector< std::string > net_input_names_
 

详细描述

在文件 torch_net.h 第 33 行定义.

构造及析构函数说明

◆ TorchNet()

apollo::perception::inference::TorchNet::TorchNet ( const std::string &  model_file,
const std::vector< std::string > &  outputs,
const std::vector< std::string > &  inputs 
)

在文件 torch_net.cc 第 28 行定义.

31 : model_file_(model_file), output_names_(outputs), input_names_(inputs) {}

◆ ~TorchNet()

virtual apollo::perception::inference::TorchNet::~TorchNet ( )
inlinevirtual

在文件 torch_net.h 第 39 行定义.

39{}

成员函数说明

◆ get_blob()

std::shared_ptr< Blob< float > > apollo::perception::inference::TorchNet::get_blob ( const std::string &  name)
overridevirtual

实现了 apollo::perception::inference::Inference.

在文件 torch_net.cc 第 65 行定义.

65 {
66 auto iter = blobs_.find(name);
67 if (iter == blobs_.end()) {
68 return nullptr;
69 }
70 return iter->second;
71}

◆ Infer()

void apollo::perception::inference::TorchNet::Infer ( )
overridevirtual

实现了 apollo::perception::inference::Inference.

在文件 torch_net.cc 第 85 行定义.

85 {
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}
base::BlobPtr< float > get_blob(const std::string &name) override
Definition torch_net.cc:65
bool shape(const std::string &name, std::vector< int > *res)
Definition torch_net.cc:75
torch::jit::script::Module net_
Definition torch_net.h:49
first check imutoantoffset saved in device
Definition readme.txt:2

◆ Init()

bool apollo::perception::inference::TorchNet::Init ( const std::map< std::string, std::vector< int > > &  shapes)
overridevirtual

实现了 apollo::perception::inference::Inference.

在文件 torch_net.cc 第 33 行定义.

33 {
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}

◆ reshape()

bool apollo::perception::inference::TorchNet::reshape ( )
protected

在文件 torch_net.cc 第 73 行定义.

73{ return true; }

◆ shape()

bool apollo::perception::inference::TorchNet::shape ( const std::string &  name,
std::vector< int > *  res 
)
protected

在文件 torch_net.cc 第 75 行定义.

75 {
76 auto blob = get_blob(name);
77 if (blob == nullptr) {
78 return false;
79 }
80
81 *res = blob->shape();
82 return true;
83}

类成员变量说明

◆ net_

torch::jit::script::Module apollo::perception::inference::TorchNet::net_
protected

在文件 torch_net.h 第 49 行定义.


该类的文档由以下文件生成: