Apollo 10.0
自动驾驶开放平台
apollo::perception::trafficlight::ClassifyBySimple类 参考

#include <classify.h>

apollo::perception::trafficlight::ClassifyBySimple 的协作图:

Public 成员函数

 ClassifyBySimple ()=default
 Construct a new classify by simple object.
 
 ~ClassifyBySimple ()=default
 Destroy the new classify by simple object.
 
void Init (const ClassifyParam &model_config, const int gpu_id)
 Initialize traffic light classify parameters.
 
void Perform (const camera::TrafficLightFrame *frame, std::vector< base::TrafficLightPtr > *lights)
 Classify model inference process.
 

详细描述

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

构造及析构函数说明

◆ ClassifyBySimple()

apollo::perception::trafficlight::ClassifyBySimple::ClassifyBySimple ( )
default

Construct a new classify by simple object.

◆ ~ClassifyBySimple()

apollo::perception::trafficlight::ClassifyBySimple::~ClassifyBySimple ( )
default

Destroy the new classify by simple object.

成员函数说明

◆ Init()

void apollo::perception::trafficlight::ClassifyBySimple::Init ( const ClassifyParam &  model_config,
const int  gpu_id 
)

Initialize traffic light classify parameters.

参数
model_config
gpu_id

在文件 classify.cc 第 31 行定义.

32 {
33 AINFO << "Enter Classify init";
34 net_inputs_.clear();
35 net_outputs_.clear();
36 AINFO << "clear success";
37 net_inputs_ =
38 inference::GetBlobNames(model_config.info().inputs());
39 net_outputs_ =
40 inference::GetBlobNames(model_config.info().outputs());
41
42 AINFO << net_inputs_.size();
43 AINFO << net_outputs_.size();
44
45 for (auto name : net_inputs_) {
46 AINFO << "net input blobs: " << name;
47 }
48 for (auto name : net_outputs_) {
49 AINFO << "net output blobs: " << name;
50 }
51
52 std::string model_path = GetModelPath(model_config.info().name());
53
54 std::string proto_file =
55 GetModelFile(model_path, model_config.info().proto_file().file());
56 AINFO << "proto_file " << proto_file;
57
58 std::string weight_file =
59 GetModelFile(model_path, model_config.info().weight_file().file());
60
61 AINFO << "weight_file" << weight_file;
62
63 const auto& model_type = model_config.info().framework();
64 rt_net_.reset(inference::CreateInferenceByName(model_type, proto_file,
65 weight_file, net_outputs_,
66 net_inputs_, model_path));
67
68 rt_net_->set_gpu_id(gpu_id);
69 gpu_id_ = gpu_id;
70
71 resize_height_ = model_config.classify_resize_height();
72 resize_width_ = model_config.classify_resize_width();
73 unknown_threshold_ = model_config.classify_threshold();
74 mean_.reset(new base::Blob<float>(1, 3, 1, 1));
75 float* p = mean_->mutable_cpu_data();
76 if (model_config.is_bgr()) {
77 data_provider_image_option_.target_color = base::Color::BGR;
78 p[0] = model_config.mean_b();
79 p[1] = model_config.mean_g();
80 p[2] = model_config.mean_r();
81 } else {
82 data_provider_image_option_.target_color = base::Color::RGB;
83 p[0] = model_config.mean_r();
84 p[1] = model_config.mean_g();
85 p[2] = model_config.mean_b();
86 }
87 scale_ = model_config.scale();
88
89 std::vector<int> shape = {1, resize_height_, resize_width_, 3};
90 mean_buffer_.reset(new base::Blob<float>(shape));
91
92 std::map<std::string, std::vector<int>> input_reshape;
93 inference::AddShape(&input_reshape, model_config.info().inputs());
94
95 AINFO << "input_reshape: " << input_reshape[net_inputs_[0]][0] << ", "
96 << input_reshape[net_inputs_[0]][1] << ", "
97 << input_reshape[net_inputs_[0]][2] << ", "
98 << input_reshape[net_inputs_[0]][3];
99
100 if (!rt_net_->Init(input_reshape)) {
101 AWARN << "net init fail.";
102 }
103
104 image_.reset(
105 new base::Image8U(resize_height_, resize_width_, base::Color::BGR));
106}
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
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)
std::string GetModelFile(const std::string &model_name, const std::string &file_name)
Get the model file path by model path and file name
Definition util.cc:55
std::string GetModelPath(const std::string &model_name)
Get the model path by model name, search from APOLLO_MODEL_PATH
Definition util.cc:44

◆ Perform()

void apollo::perception::trafficlight::ClassifyBySimple::Perform ( const camera::TrafficLightFrame *  frame,
std::vector< base::TrafficLightPtr > *  lights 
)

Classify model inference process.

参数
frame
lights

在文件 classify.cc 第 108 行定义.

109 {
110 if (cudaSetDevice(gpu_id_) != cudaSuccess) {
111 AERROR << "Failed to set device to " << gpu_id_;
112 return;
113 }
114 std::shared_ptr<base::Blob<uint8_t>> rectified_blob;
115
116 auto input_blob_recog = rt_net_->get_blob(net_inputs_[0]);
117 auto output_blob_recog = rt_net_->get_blob(net_outputs_[0]);
118
119 for (base::TrafficLightPtr light : *lights) {
120 if (!light->region.is_detected) {
121 continue;
122 }
123
124 data_provider_image_option_.crop_roi = light->region.detection_roi;
125 data_provider_image_option_.do_crop = true;
126 data_provider_image_option_.target_color = base::Color::BGR;
127 frame->data_provider->GetImage(data_provider_image_option_, image_.get());
128
129 AINFO << "get img done";
130
131 const float* mean = mean_.get()->cpu_data();
132 inference::ResizeGPU(*image_, input_blob_recog,
133 frame->data_provider->src_width(), 0, mean[0], mean[1],
134 mean[2], true, scale_);
135
136 AINFO << "resize gpu finish.";
137 cudaDeviceSynchronize();
138
139 PERF_BLOCK("traffic_light_recognition_inference")
140 rt_net_->Infer();
142
143 cudaDeviceSynchronize();
144 AINFO << "infer finish.";
145
146 float* out_put_data = output_blob_recog->mutable_cpu_data();
147 Prob2Color(out_put_data, unknown_threshold_, light);
148 }
149}
#define AERROR
Definition log.h:44
std::shared_ptr< TrafficLight > TrafficLightPtr
bool ResizeGPU(const base::Image8U &src, std::shared_ptr< apollo::perception::base::Blob< float > > dst, int stepwidth, int start_axis)
#define PERF_BLOCK_END
Definition profiler.h:53
#define PERF_BLOCK(...)
Definition profiler.h:52

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