Apollo 11.0
自动驾驶开放平台
classify.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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/file.h"
26
27namespace apollo {
28namespace perception {
29namespace trafficlight {
30
31void ClassifyBySimple::Init(const ClassifyParam& model_config, const int gpu_id) {
32 AINFO << "Enter Classify init";
33 net_inputs_.clear();
34 net_outputs_.clear();
35 AINFO << "clear success";
36 net_inputs_ = inference::GetBlobNames(model_config.info().inputs());
37 net_outputs_ = inference::GetBlobNames(model_config.info().outputs());
38
39 AINFO << net_inputs_.size();
40 AINFO << net_outputs_.size();
41
42 for (auto name : net_inputs_) {
43 AINFO << "net input blobs: " << name;
44 }
45 for (auto name : net_outputs_) {
46 AINFO << "net output blobs: " << name;
47 }
48
49 std::string model_path = GetModelPath(model_config.info().name());
50
51 std::string proto_file = GetModelFile(model_path, model_config.info().proto_file().file());
52 AINFO << "proto_file " << proto_file;
53
54 std::string weight_file = GetModelFile(model_path, model_config.info().weight_file().file());
55
56 AINFO << "weight_file" << weight_file;
57
58 const auto& model_type = model_config.info().framework();
60 model_type, proto_file, weight_file, net_outputs_, net_inputs_, model_path));
61
62 rt_net_->set_gpu_id(gpu_id);
63 gpu_id_ = gpu_id;
64
65 resize_height_ = model_config.classify_resize_height();
66 resize_width_ = model_config.classify_resize_width();
67 unknown_threshold_ = model_config.classify_threshold();
68 mean_.reset(new base::Blob<float>(1, 3, 1, 1));
69 float* p = mean_->mutable_cpu_data();
70 if (model_config.is_bgr()) {
71 data_provider_image_option_.target_color = base::Color::BGR;
72 p[0] = model_config.mean_b();
73 p[1] = model_config.mean_g();
74 p[2] = model_config.mean_r();
75 } else {
76 data_provider_image_option_.target_color = base::Color::RGB;
77 p[0] = model_config.mean_r();
78 p[1] = model_config.mean_g();
79 p[2] = model_config.mean_b();
80 }
81 scale_ = model_config.scale();
82
83 std::vector<int> shape = {1, resize_height_, resize_width_, 3};
84 mean_buffer_.reset(new base::Blob<float>(shape));
85
86 std::map<std::string, std::vector<int>> input_reshape;
87 inference::AddShape(&input_reshape, model_config.info().inputs());
88
89 AINFO << "input_reshape: " << input_reshape[net_inputs_[0]][0] << ", " << input_reshape[net_inputs_[0]][1] << ", "
90 << input_reshape[net_inputs_[0]][2] << ", " << input_reshape[net_inputs_[0]][3];
91
92 if (!rt_net_->Init(input_reshape)) {
93 AWARN << "net init fail.";
94 }
95
96 image_.reset(new base::Image8U(resize_height_, resize_width_, base::Color::BGR));
97}
98
99void ClassifyBySimple::Perform(const camera::TrafficLightFrame* frame, std::vector<base::TrafficLightPtr>* lights) {
100 if (cudaSetDevice(gpu_id_) != cudaSuccess) {
101 AERROR << "Failed to set device to " << gpu_id_;
102 return;
103 }
104 std::shared_ptr<base::Blob<uint8_t>> rectified_blob;
105
106 auto input_blob_recog = rt_net_->get_blob(net_inputs_[0]);
107 auto output_blob_recog = rt_net_->get_blob(net_outputs_[0]);
108
109 for (base::TrafficLightPtr light : *lights) {
110 if (!light->region.is_detected) {
111 continue;
112 }
113
114 data_provider_image_option_.crop_roi = light->region.detection_roi;
115 data_provider_image_option_.do_crop = true;
116 data_provider_image_option_.target_color = base::Color::BGR;
117 frame->data_provider->GetImage(data_provider_image_option_, image_.get());
118
119 AINFO << "get img done";
120
121 const float* mean = mean_.get()->cpu_data();
123 *image_,
124 input_blob_recog,
125 frame->data_provider->src_width(),
126 0,
127 mean[0],
128 mean[1],
129 mean[2],
130 true,
131 scale_);
132
133 AINFO << "resize gpu finish.";
134 cudaDeviceSynchronize();
135
136 PERF_BLOCK("traffic_light_recognition_inference")
137 rt_net_->Infer();
139
140 cudaDeviceSynchronize();
141 AINFO << "infer finish.";
142
143 float* out_put_data = output_blob_recog->mutable_cpu_data();
144 Prob2Color(out_put_data, unknown_threshold_, light);
145 }
146}
147
148void ClassifyBySimple::Prob2Color(const float* out_put_data, float threshold, base::TrafficLightPtr light) {
149 int max_color_id = 0;
150 std::vector<base::TLColor> status_map
152 std::vector<std::string> name_map = {"Black", "Red", "Yellow", "Green"};
153 std::vector<float> prob(out_put_data, out_put_data + status_map.size());
154 auto max_prob = std::max_element(prob.begin(), prob.end());
155 max_color_id = (*max_prob > threshold) ? static_cast<int>(std::distance(prob.begin(), max_prob)) : 0;
156
157 light->status.color = status_map[max_color_id];
158 light->status.confidence = out_put_data[max_color_id];
159 AINFO << "Light status recognized as " << name_map[max_color_id];
160 AINFO << "Color Prob:";
161 for (size_t j = 0; j < status_map.size(); j++) {
162 AINFO << out_put_data[j];
163 }
164}
165
166} // namespace trafficlight
167} // namespace perception
168} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
A wrapper around Blob holders serving as the basic computational unit for images.
Definition image_8u.h:44
void Init(const ClassifyParam &model_config, const int gpu_id)
Initialize traffic light classify parameters.
Definition classify.cc:31
void Perform(const camera::TrafficLightFrame *frame, std::vector< base::TrafficLightPtr > *lights)
Classify model inference process.
Definition classify.cc:99
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
std::shared_ptr< TrafficLight > TrafficLightPtr
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)
bool ResizeGPU(const base::Image8U &src, std::shared_ptr< apollo::perception::base::Blob< float > > dst, int stepwidth, int start_axis)
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
class register implement
Definition arena_queue.h:37
#define PERF_BLOCK_END
Definition profiler.h:53
#define PERF_BLOCK(...)
Definition profiler.h:52
std::shared_ptr< camera::DataProvider > data_provider