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