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