42 AERROR <<
"Failed to load traffic light detection parameters: " << config_file;
46 AINFO <<
"Traffic light detection param: " << detection_param_.DebugString();
48 const auto &model_info = detection_param_.
info();
49 std::string model_path =
GetModelPath(model_info.name());
50 AINFO <<
"Traffic light detection model_path: " << model_path;
52 std::string proto_file =
GetModelFile(model_path, model_info.proto_file().file());
53 AINFO <<
"Traffic light detection proto_file: " << proto_file;
55 std::string weight_file =
GetModelFile(model_path, model_info.weight_file().file());
56 AINFO <<
"Traffic light detection weight_file: " << weight_file;
58 if (detection_param_.
is_bgr()) {
60 mean_[0] = detection_param_.
mean_b();
61 mean_[1] = detection_param_.
mean_g();
62 mean_[2] = detection_param_.
mean_r();
65 mean_[0] = detection_param_.
mean_r();
66 mean_[1] = detection_param_.
mean_g();
67 mean_[2] = detection_param_.
mean_b();
74 const auto &model_type = model_info.framework();
75 AINFO <<
"model_type: " << model_type;
78 model_type, proto_file, weight_file, net_outputs_, net_inputs_, model_path));
80 AINFO <<
"rt_net_ create succeed";
81 rt_net_->set_gpu_id(options.
gpu_id);
88 param_blob_length_ = 6;
90 CHECK_GT(resize_height, 0);
91 CHECK_GT(resize_width, 0);
92 CHECK_GT(max_batch_size_, 0);
94 std::map<std::string, std::vector<int>> input_reshape;
97 if (!rt_net_->Init(input_reshape)) {
98 AINFO <<
"net init fail.";
101 AINFO <<
"net init success.";
105 param_blob_ = rt_net_->get_blob(net_inputs_[1]);
106 float *param_data = param_blob_->mutable_cpu_data();
107 for (
int i = 0; i < max_batch_size_; ++i) {
108 auto offset = i * param_blob_length_;
109 param_data[offset + 0] =
static_cast<float>(resize_width);
110 param_data[offset + 1] =
static_cast<float>(resize_height);
111 param_data[offset + 2] = 1;
112 param_data[offset + 3] = 1;
113 param_data[offset + 4] = 0;
114 param_data[offset + 5] = 0;
127 select_.
Init(resize_width, resize_height);
135 if (cudaSetDevice(gpu_id_) != cudaSuccess) {
136 AERROR <<
"Failed to set device to " << gpu_id_;
139 auto batch_num = lights->size();
140 for (
size_t i = 0; i < batch_num; ++i) {
141 crop_box_list_.clear();
142 resize_scale_list_.clear();
143 int img_width = data_provider->
src_width();
145 int resize_index = 0;
146 auto input_img_blob = rt_net_->get_blob(net_inputs_[0]);
147 auto input_param = rt_net_->get_blob(net_inputs_[1]);
149 input_img_blob->Reshape(
154 param_blob_->Reshape(1, 6, 1, 1);
155 float *param_data = param_blob_->mutable_cpu_data();
156 param_data[0] =
static_cast<float>(detection_param_.
min_crop_size());
157 param_data[1] =
static_cast<float>(detection_param_.
min_crop_size());
163 AINFO <<
"reshape inputblob " << input_img_blob->shape_string();
167 crop_->getCropBox(img_width, img_height, light, &cbox);
168 AINFO <<
"get crop box success " << cbox.
x <<
" " << cbox.
y <<
" " << cbox.
width <<
" " << cbox.
height;
171 crop_box_list_.push_back(cbox);
172 light->region.debug_roi[0] = cbox;
173 light->region.crop_roi = cbox;
175 data_provider_image_option_.
do_crop =
true;
176 data_provider_image_option_.
crop_roi = cbox;
178 data_provider->
GetImage(data_provider_image_option_, image_.get());
179 AINFO <<
"get image data success ";
181 float resize_scale =
static_cast<float>(detection_param_.
min_crop_size())
182 /
static_cast<float>(std::min(cbox.
width, cbox.
height));
183 resize_scale_list_.push_back(resize_scale);
186 *image_, input_img_blob, img_width, resize_index, mean_[0], mean_[1], mean_[2],
true, 1.0);
191 PERF_BLOCK(
"traffic_light_detection_inference")
192 cudaDeviceSynchronize();
194 cudaDeviceSynchronize();
196 AINFO <<
"rt_net run success";
199 SelectOutputBoxes(crop_box_list_, resize_scale_list_, resize_scale_list_, &detected_bboxes_);
209 AINFO <<
"no lights to detect";
214 auto input_blob = rt_net_->get_blob(net_inputs_[0]);
215 int img_width = data_provider->src_width();
216 int img_height = data_provider->src_height();
217 std::vector<base::TrafficLightPtr> &lights_ref = frame->
traffic_lights;
219 AINFO <<
"detection input " << lights_ref.size() <<
" lights";
221 selected_bboxes_.clear();
222 detected_bboxes_.clear();
224 for (
auto &light : lights_ref) {
226 light->region.detection_roi = light->region.projection_roi;
227 light->region.debug_roi.clear();
228 light->region.debug_roi_detect_scores.clear();
229 light->region.debug_roi.push_back(debug_rect);
230 light->region.debug_roi_detect_scores.push_back(0.0f);
233 for (
auto &light : lights_ref) {
235 || light->region.projection_roi.Area() <= 0) {
236 light->region.projection_roi.x = 0;
237 light->region.projection_roi.y = 0;
238 light->region.projection_roi.width = 0;
239 light->region.projection_roi.height = 0;
243 PERF_BLOCK(
"traffic_light_detector_inference")
247 AINFO <<
"Dump output Done! Get box num:" << detected_bboxes_.size();
249 for (
size_t j = 0; j < detected_bboxes_.size(); ++j) {
250 base::RectI ®ion = detected_bboxes_[j]->region.detection_roi;
251 float score = detected_bboxes_[j]->region.detect_score;
252 lights_ref[0]->region.debug_roi.push_back(region);
253 lights_ref[0]->region.debug_roi_detect_scores.push_back(score);
256 AINFO <<
"start select";
258 AINFO <<
"select success";
260 AINFO <<
"detection success";
265 const std::vector<base::RectI> &crop_box_list,
266 const std::vector<float> &resize_scale_list_col,
267 const std::vector<float> &resize_scale_list_row,
268 std::vector<base::TrafficLightPtr> *lights) {
269 auto output_blob = rt_net_->get_blob(net_outputs_[0]);
270 auto model_type = detection_param_.
info().framework();
271 int result_box_num, each_box_length;
272 if (model_type == common::TensorRT) {
273 result_box_num = output_blob->shape(1);
274 each_box_length = output_blob->shape(2);
276 result_box_num = output_blob->shape(0);
277 each_box_length = output_blob->shape(1);
280 AINFO <<
"output blob size " << output_blob->shape(0) <<
" " << output_blob->shape(1) <<
" "
281 << output_blob->shape(2) <<
" " << output_blob->shape(3);
282 AINFO <<
"result box number: " << result_box_num <<
" each box length: " << each_box_length;
284 for (
int candidate_id = 0; candidate_id < result_box_num; candidate_id++) {
285 const float *result_data = output_blob->cpu_data() + candidate_id * each_box_length;
286 int img_id =
static_cast<int>(result_data[0]);
289 }
else if (img_id >=
static_cast<int>(crop_box_list.size())) {
290 AINFO <<
"img id " << img_id <<
" > " << crop_box_list.size();
294 float inflate_col = 1 / resize_scale_list_col.at(img_id);
295 float inflate_row = 1 / resize_scale_list_row.at(img_id);
296 float x1 = result_data[1];
297 float y1 = result_data[2];
298 float x2 = result_data[3];
299 float y2 = result_data[4];
300 std::vector<float> score{result_data[5], result_data[6], result_data[7], result_data[8]};
301 for (
int i = 0; i < 9; ++i) {
302 ADEBUG <<
"result_data " << result_data[i];
305 std::vector<float>::iterator biggest = std::max_element(std::begin(score), std::end(score));
308 if (
static_cast<int>(tmp->region.detect_class_id) >= 0) {
309 tmp->region.detection_roi.x =
static_cast<int>(x1 * inflate_col);
310 tmp->region.detection_roi.y =
static_cast<int>(y1 * inflate_row);
311 tmp->region.detection_roi.width =
static_cast<int>((x2 - x1 + 1) * inflate_col);
312 tmp->region.detection_roi.height =
static_cast<int>((y2 - y1 + 1) * inflate_row);
313 tmp->region.detect_score = *biggest;
316 tmp->region.detection_roi, crop_box_list.at(img_id).width, crop_box_list.at(img_id).height)
317 || tmp->region.detection_roi.Area() <= 0) {
318 AINFO <<
"Invalid width or height or x or y: " << tmp->region.detection_roi.width <<
" | "
319 << tmp->region.detection_roi.height <<
" | " << tmp->region.detection_roi.x <<
" | "
320 << tmp->region.detection_roi.y;
321 AINFO <<
" max width " << crop_box_list.at(img_id).width <<
" max height "
322 << crop_box_list.at(img_id).height <<
" at img_id " << img_id;
327 tmp->region.detection_roi,
328 crop_box_list.at(img_id).width,
329 crop_box_list.at(img_id).height,
330 &(tmp->region.detection_roi));
331 tmp->region.detection_roi.x += crop_box_list.at(img_id).x;
332 tmp->region.detection_roi.y += crop_box_list.at(img_id).y;
333 tmp->region.is_detected =
true;
334 AINFO <<
"detect roi x " << tmp->region.detection_roi.x <<
" " << tmp->region.detection_roi.y <<
" "
335 << tmp->region.detection_roi.width <<
" " << tmp->region.detection_roi.height;
337 lights->push_back(tmp);
339 AWARN <<
"Invalid class id: " <<
static_cast<int>(tmp->region.detect_class_id);
347 if (lights ==
nullptr) {
348 AERROR <<
"lights are not available";
353 std::vector<std::pair<float, int>> score_index_vec(lights->size());
354 for (
size_t i = 0; i < lights->size(); ++i) {
355 score_index_vec[i].first = lights->at(i)->region.detect_score;
356 score_index_vec[i].second =
static_cast<int>(i);
359 score_index_vec.begin(),
360 score_index_vec.end(),
361 [](
const std::pair<float, int> &pr1,
const std::pair<float, int> &pr2) { return pr1.first < pr2.first; });
363 std::vector<int> kept_indices;
364 while (!score_index_vec.empty()) {
365 const int idx = score_index_vec.back().second;
367 for (
size_t k = 0; k < kept_indices.size(); ++k) {
368 const int kept_idx = kept_indices[k];
369 const auto &rect1 = lights->at(idx)->region.detection_roi;
370 const auto &rect2 = lights->at(kept_idx)->region.detection_roi;
371 float overlap =
static_cast<float>((rect1 & rect2).Area() / (rect1 | rect2).Area());
374 keep = std::fabs(overlap) < iou_thresh;
380 kept_indices.push_back(idx);
382 score_index_vec.pop_back();
386 auto parted_itr = std::stable_partition(lights->begin(), lights->end(), [&](
const base::TrafficLightPtr &light) {
387 return std::find(kept_indices.begin(), kept_indices.end(), idx++) != kept_indices.end();
389 lights->erase(parted_itr, lights->end());