Apollo  6.0
Open source self driving car software
region_output.h
Go to the documentation of this file.
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 #pragma once
17 
18 #include <algorithm>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "modules/perception/camera/proto/yolo.pb.h"
26 
33 
34 namespace apollo {
35 namespace perception {
36 namespace camera {
37 
38 static const char NormalNMS[] = "NormalNMS";
39 static const char LinearSoftNMS[] = "LinearSoftNMS";
40 static const char GuassianSoftNMS[] = "GuassianSoftNMS";
41 static const char BoxVote[] = "BoxVote";
42 static const int kBoxBlockSize = 32;
43 static const int kMaxObjSize = 1000;
44 
46  float xmin = -1;
47  float ymin = -1;
48  float xmax = -1;
49  float ymax = -1;
50  int label = -1;
51  float score = -1;
52  float size = -1;
53  bool mask = false;
54 
56  return i.score < j.score;
57  }
58 };
59 
60 struct BBox3D {
61  float h = -1;
62  float w = -1;
63  float l = -1;
64  float alpha = -1;
65 };
66 
67 struct AnchorBox {
68  float w;
69  float h;
70 };
71 struct NMSParam {
72  float threshold;
75  float sigma;
76  std::string type = BoxVote;
77 };
78 struct YoloBlobs {
79  std::shared_ptr<base::Blob<float>> det1_loc_blob;
80  std::shared_ptr<base::Blob<float>> det1_obj_blob;
81  std::shared_ptr<base::Blob<float>> det1_cls_blob;
82  std::shared_ptr<base::Blob<float>> det1_ori_conf_blob;
83  std::shared_ptr<base::Blob<float>> det1_ori_blob;
84  std::shared_ptr<base::Blob<float>> det1_dim_blob;
85  std::shared_ptr<base::Blob<float>> det2_loc_blob;
86  std::shared_ptr<base::Blob<float>> det2_obj_blob;
87  std::shared_ptr<base::Blob<float>> det2_cls_blob;
88  std::shared_ptr<base::Blob<float>> det2_ori_conf_blob;
89  std::shared_ptr<base::Blob<float>> det2_ori_blob;
90  std::shared_ptr<base::Blob<float>> det2_dim_blob;
91  std::shared_ptr<base::Blob<float>> det3_loc_blob;
92  std::shared_ptr<base::Blob<float>> det3_obj_blob;
93  std::shared_ptr<base::Blob<float>> det3_cls_blob;
94  std::shared_ptr<base::Blob<float>> det3_ori_conf_blob;
95  std::shared_ptr<base::Blob<float>> det3_ori_blob;
96  std::shared_ptr<base::Blob<float>> det3_dim_blob;
97 
98  std::shared_ptr<base::Blob<float>> lof_blob;
99  std::shared_ptr<base::Blob<float>> lor_blob;
100  std::shared_ptr<base::Blob<float>> brvis_blob;
101  std::shared_ptr<base::Blob<float>> brswt_blob;
102  std::shared_ptr<base::Blob<float>> ltvis_blob;
103  std::shared_ptr<base::Blob<float>> ltswt_blob;
104  std::shared_ptr<base::Blob<float>> rtvis_blob;
105  std::shared_ptr<base::Blob<float>> rtswt_blob;
106  std::shared_ptr<base::Blob<float>> area_id_blob;
107  std::shared_ptr<base::Blob<float>> visible_ratio_blob;
108  std::shared_ptr<base::Blob<float>> cut_off_ratio_blob;
109  std::shared_ptr<base::Blob<float>> res_box_blob;
110  std::shared_ptr<base::Blob<float>> res_cls_blob;
111  std::shared_ptr<base::Blob<float>> anchor_blob;
112  std::shared_ptr<base::Blob<float>> expand_blob;
113 };
114 struct MinDims {
115  float min_2d_height = 0.0f;
116  float min_3d_height = 0.0f;
117  float min_3d_length = 0.0f;
118  float min_3d_width = 0.0f;
119 };
120 
121 constexpr float minExpPower = -10.0f;
122 constexpr float maxExpPower = 5.0f;
123 constexpr int anchorSizeFactor = 2;
124 constexpr int numScales = 3;
125 
126 __host__ __device__ float sigmoid_gpu(float x);
127 __host__ __device__ float bbox_size_gpu(const float *bbox,
128  const bool normalized);
129 __host__ __device__ float jaccard_overlap_gpu(const float *bbox1,
130  const float *bbox2);
131 
132 template <typename T>
133 bool sort_score_pair_descend(const std::pair<float, T> &pair1,
134  const std::pair<float, T> &pair2) {
135  return pair1.first > pair2.first;
136 }
137 
138 void get_max_score_index(const std::vector<float> &scores,
139  const float threshold, const int top_k,
140  std::vector<std::pair<float, int>> *score_index_vec);
141 
142 float get_bbox_size(const NormalizedBBox &bbox);
143 
144 void get_intersect_bbox(const NormalizedBBox &bbox1,
145  const NormalizedBBox &bbox2,
146  NormalizedBBox *intersect_bbox);
147 
148 float get_jaccard_overlap(const NormalizedBBox &bbox1,
149  const NormalizedBBox &bbox2);
150 
151 void apply_nms(const bool *overlapped, const int num,
152  std::vector<int> *indices);
153 
154 void apply_nms_gpu(const float *bbox_data, const float *conf_data,
155  const std::vector<int> &origin_indices, const int bbox_step,
156  const float confidence_threshold, const int top_k,
157  const float nms_threshold, std::vector<int> *indices,
158  base::Blob<bool> *overlapped, base::Blob<int> *idx_sm,
159  const cudaStream_t &_stream);
160 
161 void compute_overlapped_by_idx_gpu(const int nthreads, const float *bbox_data,
162  const float overlap_threshold,
163  const int *idx, const int num_idx,
164  bool *overlapped_data,
165  const cudaStream_t &_stream);
166 
167 int get_objects_gpu(
168  const YoloBlobs &yolo_blobs, const cudaStream_t &stream,
169  const std::vector<base::ObjectSubType> &types, const NMSParam &nms,
170  const yolo::ModelParam &model_param, float light_vis_conf_threshold,
171  float light_swt_conf_threshold, base::Blob<bool> *overlapped,
172  base::Blob<int> *idx_sm,
173  const std::map<base::ObjectSubType, std::vector<int>> &indices,
174  const std::map<base::ObjectSubType, std::vector<float>> &conf_scores);
175 
176 void get_objects_cpu(const YoloBlobs &yolo_blobs, const cudaStream_t &stream,
177  const std::vector<base::ObjectSubType> &types,
178  const NMSParam &nms, const yolo::ModelParam &model_param,
179  float light_vis_conf_threshold,
180  float light_swt_conf_threshold,
181  base::Blob<bool> *overlapped, base::Blob<int> *idx_sm,
182  std::vector<base::ObjectPtr> *objects);
183 
184 void apply_softnms_fast(const std::vector<NormalizedBBox> &bboxes,
185  std::vector<float> *scores, const float score_threshold,
186  const float nms_threshold, const int top_k,
187  std::vector<int> *indices, bool is_linear,
188  const float sigma);
189 
190 void apply_boxvoting_fast(std::vector<NormalizedBBox> *bboxes,
191  std::vector<float> *scores,
192  const float conf_threshold, const float nms_threshold,
193  const float sigma, std::vector<int> *indices);
194 
195 void apply_nms_fast(const std::vector<NormalizedBBox> &bboxes,
196  const std::vector<float> &scores,
197  const float score_threshold, const float nms_threshold,
198  const float eta, const int top_k,
199  std::vector<int> *indices);
200 
201 void recover_bbox(int roi_w, int roi_h, int offset_y,
202  std::vector<base::ObjectPtr> *objects);
203 
204 void filter_bbox(const MinDims &min_dims,
205  std::vector<base::ObjectPtr> *objects);
206 
207 void fill_bbox3d(bool with_bbox3d, base::ObjectPtr obj, const float *bbox);
208 
209 void fill_frbox(bool with_frbox, base::ObjectPtr obj, const float *bbox);
210 
211 void fill_lights(bool with_lights, base::ObjectPtr obj, const float *bbox);
212 void fill_ratios(bool with_ratios, base::ObjectPtr obj, const float *bbox);
213 void fill_area_id(bool with_flag, base::ObjectPtr obj, const float *data);
214 
215 void fill_base(base::ObjectPtr obj, const float *bbox);
216 
217 const float *get_gpu_data(bool flag, const base::Blob<float> &blob);
218 
219 int get_area_id(float visible_ratios[4]);
220 
221 } // namespace camera
222 } // namespace perception
223 } // namespace apollo
std::shared_ptr< base::Blob< float > > det3_ori_blob
Definition: region_output.h:95
void compute_overlapped_by_idx_gpu(const int nthreads, const float *bbox_data, const float overlap_threshold, const int *idx, const int num_idx, bool *overlapped_data, const cudaStream_t &_stream)
Definition: region_output.h:67
void apply_softnms_fast(const std::vector< NormalizedBBox > &bboxes, std::vector< float > *scores, const float score_threshold, const float nms_threshold, const int top_k, std::vector< int > *indices, bool is_linear, const float sigma)
bool operator()(NormalizedBBox i, NormalizedBBox j)
Definition: region_output.h:55
void filter_bbox(const SmokeMinDims &min_dims, std::vector< base::ObjectPtr > *objects)
std::shared_ptr< base::Blob< float > > det2_loc_blob
Definition: region_output.h:85
int get_area_id(float visible_ratios[4])
std::shared_ptr< base::Blob< float > > lof_blob
Definition: region_output.h:98
void apply_nms(const bool *overlapped, const int num, std::vector< int > *indices)
std::shared_ptr< base::Blob< float > > anchor_blob
Definition: region_output.h:111
std::shared_ptr< base::Blob< float > > det2_cls_blob
Definition: region_output.h:87
std::shared_ptr< base::Blob< float > > det3_obj_blob
Definition: region_output.h:92
std::shared_ptr< base::Blob< float > > area_id_blob
Definition: region_output.h:106
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void fill_frbox(bool with_frbox, base::ObjectPtr obj, const float *bbox)
float inter_cls_nms_thresh
Definition: region_output.h:73
std::shared_ptr< base::Blob< float > > det3_cls_blob
Definition: region_output.h:93
float get_jaccard_overlap(const NormalizedBBox &bbox1, const NormalizedBBox &bbox2)
std::shared_ptr< base::Blob< float > > res_box_blob
Definition: region_output.h:109
constexpr float minExpPower
Definition: region_output.h:121
Definition: region_output.h:71
Definition: region_output.h:114
constexpr int anchorSizeFactor
Definition: region_output.h:123
std::shared_ptr< base::Blob< float > > rtswt_blob
Definition: region_output.h:105
void apply_boxvoting_fast(std::vector< NormalizedBBox > *bboxes, std::vector< float > *scores, const float conf_threshold, const float nms_threshold, const float sigma, std::vector< int > *indices)
void get_objects_cpu(const YoloBlobs &yolo_blobs, const cudaStream_t &stream, const std::vector< base::ObjectSubType > &types, const NMSParam &nms, const yolo::ModelParam &model_param, float light_vis_conf_threshold, float light_swt_conf_threshold, base::Blob< bool > *overlapped, base::Blob< int > *idx_sm, std::vector< base::ObjectPtr > *objects)
Definition: region_output.h:45
std::shared_ptr< base::Blob< float > > lor_blob
Definition: region_output.h:99
void recover_bbox(int roi_w, int roi_h, int offset_y, std::vector< base::ObjectPtr > *objects)
void fill_ratios(bool with_ratios, base::ObjectPtr obj, const float *bbox)
void fill_bbox3d(bool with_bbox3d, base::ObjectPtr obj, const float *bbox)
void fill_lights(bool with_lights, base::ObjectPtr obj, const float *bbox)
std::shared_ptr< base::Blob< float > > det1_ori_conf_blob
Definition: region_output.h:82
constexpr int numScales
Definition: region_output.h:124
float w
Definition: region_output.h:68
A wrapper around SyncedMemory holders serving as the basic computational unit for images...
Definition: blob.h:88
std::shared_ptr< base::Blob< float > > rtvis_blob
Definition: region_output.h:104
__host__ __device__ float jaccard_overlap_gpu(const float *bbox1, const float *bbox2)
std::shared_ptr< base::Blob< float > > ltswt_blob
Definition: region_output.h:103
float ymax
Definition: region_output.h:49
std::shared_ptr< base::Blob< float > > det1_obj_blob
Definition: region_output.h:80
std::shared_ptr< base::Blob< float > > det1_dim_blob
Definition: region_output.h:84
void get_max_score_index(const std::vector< float > &scores, const float threshold, const int top_k, std::vector< std::pair< float, int >> *score_index_vec)
float get_bbox_size(const NormalizedBBox &bbox)
constexpr float maxExpPower
Definition: region_output.h:122
std::shared_ptr< base::Blob< float > > det1_cls_blob
Definition: region_output.h:81
std::shared_ptr< base::Blob< float > > det3_dim_blob
Definition: region_output.h:96
float ymin
Definition: region_output.h:47
float h
Definition: region_output.h:69
void fill_base(base::ObjectPtr obj, const float *bbox)
void apply_nms_fast(const std::vector< NormalizedBBox > &bboxes, const std::vector< float > &scores, const float score_threshold, const float nms_threshold, const float eta, const int top_k, std::vector< int > *indices)
void fill_area_id(bool with_flag, base::ObjectPtr obj, const float *data)
Definition: region_output.h:60
std::shared_ptr< base::Blob< float > > res_cls_blob
Definition: region_output.h:110
std::shared_ptr< base::Blob< float > > det1_ori_blob
Definition: region_output.h:83
std::shared_ptr< base::Blob< float > > det2_obj_blob
Definition: region_output.h:86
std::shared_ptr< base::Blob< float > > det2_dim_blob
Definition: region_output.h:90
std::shared_ptr< base::Blob< float > > det2_ori_blob
Definition: region_output.h:89
bool sort_score_pair_descend(const std::pair< float, T > &pair1, const std::pair< float, T > &pair2)
Definition: region_output.h:133
std::shared_ptr< base::Blob< float > > ltvis_blob
Definition: region_output.h:102
std::shared_ptr< base::Blob< float > > brswt_blob
Definition: region_output.h:101
float score
Definition: region_output.h:51
std::shared_ptr< base::Blob< float > > brvis_blob
Definition: region_output.h:100
Definition: region_output.h:78
__host__ __device__ float bbox_size_gpu(const float *bbox, const bool normalized)
std::shared_ptr< base::Blob< float > > det2_ori_conf_blob
Definition: region_output.h:88
std::shared_ptr< base::Blob< float > > det3_ori_conf_blob
Definition: region_output.h:94
std::shared_ptr< base::Blob< float > > cut_off_ratio_blob
Definition: region_output.h:108
std::shared_ptr< base::Blob< float > > det1_loc_blob
Definition: region_output.h:79
void apply_nms_gpu(const float *bbox_data, const float *conf_data, const std::vector< int > &origin_indices, const int bbox_step, const float confidence_threshold, const int top_k, const float nms_threshold, std::vector< int > *indices, base::Blob< bool > *overlapped, base::Blob< int > *idx_sm, const cudaStream_t &_stream)
Image< uchar > * threshold(Image< T > *src, int t)
Definition: imutil.h:63
int label
Definition: region_output.h:50
std::shared_ptr< base::Blob< float > > expand_blob
Definition: region_output.h:112
int get_objects_gpu(const YoloBlobs &yolo_blobs, const cudaStream_t &stream, const std::vector< base::ObjectSubType > &types, const NMSParam &nms, const yolo::ModelParam &model_param, float light_vis_conf_threshold, float light_swt_conf_threshold, base::Blob< bool > *overlapped, base::Blob< int > *idx_sm, const std::map< base::ObjectSubType, std::vector< int >> &indices, const std::map< base::ObjectSubType, std::vector< float >> &conf_scores)
float threshold
Definition: region_output.h:72
float xmax
Definition: region_output.h:48
ObjectSubType
Definition: object_types.h:63
float sigma
Definition: region_output.h:75
__host__ __device__ float sigmoid_gpu(float x)
bool mask
Definition: region_output.h:53
std::shared_ptr< base::Blob< float > > det3_loc_blob
Definition: region_output.h:91
float xmin
Definition: region_output.h:46
float size
Definition: region_output.h:52
std::shared_ptr< Object > ObjectPtr
Definition: object.h:123
void get_intersect_bbox(const NormalizedBBox &bbox1, const NormalizedBBox &bbox2, NormalizedBBox *intersect_bbox)
const float * get_gpu_data(bool flag, const base::Blob< float > &blob)
float inter_cls_conf_thresh
Definition: region_output.h:74
std::shared_ptr< base::Blob< float > > visible_ratio_blob
Definition: region_output.h:107