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

#include <libtorch_obstacle_detector.h>

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

Public 成员函数

 ObstacleDetector (const std::string &net_file, const std::string &model_file, const std::vector< std::string > &outputs)
 
 ObstacleDetector (const std::string &net_file, const std::string &model_file, const std::vector< std::string > &outputs, const std::vector< std::string > &inputs)
 
virtual ~ObstacleDetector ()
 
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 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_
 

详细描述

在文件 libtorch_obstacle_detector.h 第 34 行定义.

构造及析构函数说明

◆ ObstacleDetector() [1/2]

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

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

31 : net_file_(net_file), model_file_(model_file), output_names_(outputs) {}

◆ ObstacleDetector() [2/2]

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

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

37 : net_file_(net_file),
38 model_file_(model_file),
39 output_names_(outputs),
40 input_names_(inputs) {}

◆ ~ObstacleDetector()

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

在文件 libtorch_obstacle_detector.h 第 43 行定义.

43{}

成员函数说明

◆ get_blob()

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

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

在文件 libtorch_obstacle_detector.cc 第 96 行定义.

97 {
98 auto iter = blobs_.find(name);
99 if (iter == blobs_.end()) {
100 return nullptr;
101 }
102 return iter->second;
103}

◆ Infer()

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

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

在文件 libtorch_obstacle_detector.cc 第 115 行定义.

115 {
116 torch::Device device(device_type_, device_id_);
117 auto blob = blobs_[input_names_[0]];
118 auto input_K = blobs_[input_names_[1]];
119 auto input_ratio = blobs_[input_names_[2]];
120
121 torch::Tensor tensor_image = torch::from_blob(
122 blob->data()->mutable_gpu_data(),
123 {blob->shape(0), blob->shape(1), blob->shape(2), blob->shape(3)},
124 torch::kFloat32);
125 torch::Tensor tensor_K = torch::from_blob(
126 input_K->data()->mutable_cpu_data(),
127 {input_K->shape(0), input_K->shape(1), input_K->shape(2)},
128 torch::kFloat32);
129 torch::Tensor tensor_ratio = torch::from_blob(
130 input_ratio->data()->mutable_cpu_data(),
131 {input_ratio->shape(0), input_ratio->shape(1)}, torch::kFloat32);
132
133 std::vector<torch::jit::IValue> torch_inputs;
134 torch_inputs.push_back(tensor_image.to(device));
135 torch_inputs.push_back(
136 std::make_tuple(tensor_K.to(device), tensor_ratio.to(device)));
137
138 AINFO << "Start to do inference";
139 auto outputs = net_.forward(torch_inputs).toTuple()->elements();
140 AINFO << "Finished inference";
141
142 for (size_t i = 0; i < output_names_.size(); ++i) {
143 auto blob = get_blob(output_names_[i]);
144 if (blob != nullptr) {
145 torch::Tensor output_tensor = outputs[i].toTensor();
146 std::vector<int64_t> output_size_ = output_tensor.sizes().vec();
147 std::vector<int> output_size(begin(output_size_), end(output_size_));
148 blob->Reshape(output_size);
149 blob->set_gpu_data(output_tensor.data_ptr<float>());
150 }
151 }
152}
base::BlobPtr< float > get_blob(const std::string &name) override
first check imutoantoffset saved in device
Definition readme.txt:2
#define AINFO
Definition log.h:42

◆ Init()

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

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

在文件 libtorch_obstacle_detector.cc 第 42 行定义.

43 {
44 if (gpu_id_ >= 0) {
45 device_type_ = torch::kCUDA;
46 device_id_ = gpu_id_;
47 } else {
48 device_type_ = torch::kCPU;
49 }
50
51 for (const auto &name : output_names_) {
52 auto iter = shapes.find(name);
53 if (iter != shapes.end()) {
54 auto blob = std::make_shared<Blob<float>>(iter->second);
55 blobs_.emplace(name, blob);
56 }
57 }
58
59 for (const auto &name : input_names_) {
60 auto iter = shapes.find(name);
61 if (iter != shapes.end()) {
62 auto blob = std::make_shared<Blob<float>>(iter->second);
63 blobs_.emplace(name, blob);
64 }
65 }
66
67 // Load model without grad, equal to torch.no_grad()
68 // torch::NoGradGuard no_grad;
69 // Init net
70 torch::Device device(device_type_, device_id_);
71 net_ = torch::jit::load(model_file_, device);
72 net_.eval();
73
74 // run a fake inference at init time as first inference is relative slow
75 int height_ = blobs_[input_names_[0]]->shape(1);
76 int width_ = blobs_[input_names_[0]]->shape(2);
77 torch::Tensor input_feature_tensor = torch::zeros({1, 3, height_, width_});
78 std::array<float, 2> down_ratio{8.0f, 8.0f};
79 torch::Tensor tensor_downratio = torch::from_blob(down_ratio.data(), {1, 2});
80 std::array<float, 9> K{1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
81 torch::Tensor tensor_K = torch::from_blob(K.data(), {1, 3, 3});
82
83 std::vector<torch::jit::IValue> torch_inputs;
84 torch_inputs.push_back(input_feature_tensor.to(device));
85 torch_inputs.push_back(
86 std::make_tuple(tensor_K.to(device), tensor_downratio.to(device)));
87 auto torch_output_tensor = net_.forward(torch_inputs).toTuple()->elements();
88 if (torch_output_tensor[0].toTensor().requires_grad()) {
89 AWARN << "Require grad";
90 }
91
92 emptyCache();
93 return true;
94}
#define AWARN
Definition log.h:43

◆ shape()

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

在文件 libtorch_obstacle_detector.cc 第 105 行定义.

105 {
106 auto blob = get_blob(name);
107 if (blob == nullptr) {
108 return false;
109 }
110
111 *res = blob->shape();
112 return true;
113}

类成员变量说明

◆ net_

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

在文件 libtorch_obstacle_detector.h 第 52 行定义.


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