Apollo 11.0
自动驾驶开放平台
select.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 *****************************************************************************/
17
18#include "cyber/common/log.h"
19
20namespace apollo {
21namespace perception {
22namespace trafficlight {
23
24bool Select::Init(int rows, int cols) {
25 if (rows < 0 || cols < 0) {
26 return false;
27 }
28
29 munkres_.costs()->Reserve(rows, cols);
30
31 return true;
32}
33
34double Select::Calc2dGaussianScore(base::Point2DI p1, base::Point2DI p2, float sigma1, float sigma2) {
35 return std::exp(
36 -0.5
37 * (static_cast<float>((p1.x - p2.x) * (p1.x - p2.x)) / (sigma1 * sigma1)
38 + (static_cast<float>((p1.y - p2.y) * (p1.y - p2.y)) / (sigma2 * sigma2))));
39}
40
42 const std::vector<base::TrafficLightPtr> &refined_bboxes,
43 std::vector<base::TrafficLightPtr> *hdmap_bboxes) {
44 std::vector<std::pair<size_t, size_t>> assignments;
45 munkres_.costs()->Resize(hdmap_bboxes->size(), refined_bboxes.size());
46
47 for (size_t row = 0; row < hdmap_bboxes->size(); ++row) {
48 auto center_hd = (*hdmap_bboxes)[row]->region.detection_roi.Center();
49 if ((*hdmap_bboxes)[row]->region.outside_image) {
50 AINFO << "projection_roi outside image, set score to 0.";
51 for (size_t col = 0; col < refined_bboxes.size(); ++col) {
52 (*munkres_.costs())(row, col) = 0.0;
53 }
54 continue;
55 }
56 for (size_t col = 0; col < refined_bboxes.size(); ++col) {
57 float gaussian_score = 100.0f;
58 auto center_refine = refined_bboxes[col]->region.detection_roi.Center();
59 // use gaussian score as metrics of distance and width
60 double distance_score = Calc2dGaussianScore(center_hd, center_refine, gaussian_score, gaussian_score);
61
62 double max_score = 0.9;
63 auto detect_score = refined_bboxes[col]->region.detect_score;
64 double detection_score = detect_score > max_score ? max_score : detect_score;
65
66 double distance_weight = 0.7;
67 double detection_weight = 1 - distance_weight;
68 (*munkres_.costs())(row, col)
69 = static_cast<float>(detection_weight * detection_score + distance_weight * distance_score);
70 const auto &crop_roi = (*hdmap_bboxes)[row]->region.crop_roi;
71 const auto &detection_roi = refined_bboxes[col]->region.detection_roi;
72 if ((detection_roi & crop_roi) != detection_roi) {
73 AINFO << "detection_roi outside crop_roi, set score to 0."
74 << " detection_roi: " << detection_roi.x << " " << detection_roi.y << " " << detection_roi.width
75 << " " << detection_roi.height << " crop_roi: " << crop_roi.x << " " << crop_roi.y << " "
76 << crop_roi.width << " " << crop_roi.height;
77 (*munkres_.costs())(row, col) = 0.0;
78 }
79 AINFO << "score " << (*munkres_.costs())(row, col);
80 }
81 }
82
83 munkres_.Maximize(&assignments);
84
85 for (size_t i = 0; i < hdmap_bboxes->size(); ++i) {
86 (*hdmap_bboxes)[i]->region.is_selected = false;
87 (*hdmap_bboxes)[i]->region.is_detected = false;
88 }
89
90 for (size_t i = 0; i < assignments.size(); ++i) {
91 if (static_cast<size_t>(assignments[i].first) >= hdmap_bboxes->size()
92 || static_cast<size_t>(
93 assignments[i].second >= refined_bboxes.size()
94 || (*hdmap_bboxes)[assignments[i].first]->region.is_selected
95 || refined_bboxes[assignments[i].second]->region.is_selected)) {
96 } else {
97 auto &refined_bbox_region = refined_bboxes[assignments[i].second]->region;
98 auto &hdmap_bbox_region = (*hdmap_bboxes)[assignments[i].first]->region;
99 refined_bbox_region.is_selected = true;
100 hdmap_bbox_region.is_selected = true;
101
102 const auto &crop_roi = hdmap_bbox_region.crop_roi;
103 const auto &detection_roi = refined_bbox_region.detection_roi;
104 bool outside_crop_roi = ((crop_roi & detection_roi) != detection_roi);
105 if (hdmap_bbox_region.outside_image || outside_crop_roi) {
106 hdmap_bbox_region.is_detected = false;
107 } else {
108 hdmap_bbox_region.detection_roi = refined_bbox_region.detection_roi;
109 hdmap_bbox_region.detect_class_id = refined_bbox_region.detect_class_id;
110 hdmap_bbox_region.detect_score = refined_bbox_region.detect_score;
111 hdmap_bbox_region.is_detected = refined_bbox_region.is_detected;
112 hdmap_bbox_region.is_selected = refined_bbox_region.is_selected;
113 }
114 }
115 }
116
117 for (size_t i = 0; i < hdmap_bboxes->size(); ++i) {
118 AINFO << "hdmap_bboxes-" << i << ":"
119 << " projection_roi: " << (*hdmap_bboxes)[i]->region.projection_roi.ToStr()
120 << " detection_roi: " << (*hdmap_bboxes)[i]->region.detection_roi.ToStr();
121 }
122}
123
124} // namespace trafficlight
125} // namespace perception
126} // namespace apollo
void Maximize(std::vector< std::pair< size_t, size_t > > *assignments)
void Resize(const size_t resize_height, const size_t resize_width)
void Reserve(const size_t reserve_height, const size_t reserve_width)
void SelectTrafficLights(const std::vector< base::TrafficLightPtr > &refined_bboxes, std::vector< base::TrafficLightPtr > *hdmap_bboxes)
Choose traffic lights using Gaussian calculation scores.
Definition select.cc:41
double Calc2dGaussianScore(base::Point2DI p1, base::Point2DI p2, float sigma1, float sigma2)
Calculate 2d Gaussian score.
Definition select.cc:34
bool Init(int rows, int cols)
Initialize select object parameters.
Definition select.cc:24
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37