Apollo 10.0
自动驾驶开放平台
postprocess.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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_detection_multi_stage/detector/yolo/proto/model_param.pb.h"
26
30
31namespace apollo {
32namespace perception {
33namespace camera {
34
35static const char NormalNMS[] = "NormalNMS";
36static const char LinearSoftNMS[] = "LinearSoftNMS";
37static const char GuassianSoftNMS[] = "GuassianSoftNMS";
38static const char BoxVote[] = "BoxVote";
39static const int kBoxBlockSize = 32;
40static const int kMaxObjSize = 1000;
41
43 float xmin = -1;
44 float ymin = -1;
45 float xmax = -1;
46 float ymax = -1;
47 int label = -1;
48 float score = -1;
49 float size = -1;
50 bool mask = false;
51
53 return i.score < j.score;
54 }
55};
56
57struct BBox3D {
58 float h = -1;
59 float w = -1;
60 float l = -1;
61 float alpha = -1;
62};
63
64struct AnchorBox {
65 float w;
66 float h;
67};
68
69struct NMSParam {
70 float threshold;
73 float sigma;
74 std::string type = BoxVote;
75};
76
77struct 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 std::shared_ptr<base::Blob<float>> feat_blob;
114};
115
116struct MinDims {
117 float min_2d_height = 0.0f;
118 float min_3d_height = 0.0f;
119 float min_3d_length = 0.0f;
120 float min_3d_width = 0.0f;
121};
122
123constexpr float minExpPower = -10.0f;
124constexpr float maxExpPower = 5.0f;
125constexpr int anchorSizeFactor = 2;
126constexpr int numScales = 3;
127
128__host__ __device__ float sigmoid_gpu(float x);
129__host__ __device__ float bbox_size_gpu(const float *bbox,
130 const bool normalized);
131__host__ __device__ float jaccard_overlap_gpu(const float *bbox1,
132 const float *bbox2);
133
134template <typename T>
135bool sort_score_pair_descend(const std::pair<float, T> &pair1,
136 const std::pair<float, T> &pair2) {
137 return pair1.first > pair2.first;
138}
147void get_max_score_index(const std::vector<float> &scores,
148 const float threshold, const int top_k,
149 std::vector<std::pair<float, int>> *score_index_vec);
150
151float get_bbox_size(const NormalizedBBox &bbox);
159void get_intersect_bbox(const NormalizedBBox &bbox1,
160 const NormalizedBBox &bbox2,
161 NormalizedBBox *intersect_bbox);
169float get_jaccard_overlap(const NormalizedBBox &bbox1,
170 const NormalizedBBox &bbox2);
178void apply_nms(const bool *overlapped, const int num,
179 std::vector<int> *indices);
195void apply_nms_gpu(const float *bbox_data, const float *conf_data,
196 const std::vector<int> &origin_indices, const int bbox_step,
197 const float confidence_threshold, const int top_k,
198 const float nms_threshold, std::vector<int> *indices,
199 base::Blob<bool> *overlapped, base::Blob<int> *idx_sm,
200 const cudaStream_t &_stream);
212void compute_overlapped_by_idx_gpu(const int nthreads, const float *bbox_data,
213 const float overlap_threshold,
214 const int *idx, const int num_idx,
215 bool *overlapped_data,
216 const cudaStream_t &_stream);
234 const YoloBlobs &yolo_blobs, const cudaStream_t &stream,
235 const std::vector<base::ObjectSubType> &types, const NMSParam &nms,
236 const yolo::ModelParam &model_param, float light_vis_conf_threshold,
237 float light_swt_conf_threshold, base::Blob<bool> *overlapped,
238 base::Blob<int> *idx_sm,
239 const std::map<base::ObjectSubType, std::vector<int>> &indices,
240 const std::map<base::ObjectSubType, std::vector<float>> &conf_scores);
255void get_objects_cpu(const YoloBlobs &yolo_blobs, const cudaStream_t &stream,
256 const std::vector<base::ObjectSubType> &types,
257 const NMSParam &nms, const yolo::ModelParam &model_param,
258 float light_vis_conf_threshold,
259 float light_swt_conf_threshold,
260 base::Blob<bool> *overlapped, base::Blob<int> *idx_sm,
261 std::vector<base::ObjectPtr> *objects);
274void apply_softnms_fast(const std::vector<NormalizedBBox> &bboxes,
275 std::vector<float> *scores, const float score_threshold,
276 const float nms_threshold, const int top_k,
277 std::vector<int> *indices, bool is_linear,
278 const float sigma);
290void apply_boxvoting_fast(std::vector<NormalizedBBox> *bboxes,
291 std::vector<float> *scores,
292 const float conf_threshold, const float nms_threshold,
293 const float sigma, std::vector<int> *indices);
305void apply_nms_fast(const std::vector<NormalizedBBox> &bboxes,
306 const std::vector<float> &scores,
307 const float score_threshold, const float nms_threshold,
308 const float eta, const int top_k,
309 std::vector<int> *indices);
318void recover_bbox(int roi_w, int roi_h, int offset_y,
319 std::vector<base::ObjectPtr> *objects);
327void filter_bbox(const MinDims &min_dims,
328 std::vector<base::ObjectPtr> *objects);
336void fill_bbox3d(bool with_bbox3d, base::ObjectPtr obj, const float *bbox);
344void fill_frbox(bool with_frbox, base::ObjectPtr obj, const float *bbox);
352void fill_lights(bool with_lights, base::ObjectPtr obj, const float *bbox);
360void fill_ratios(bool with_ratios, base::ObjectPtr obj, const float *bbox);
368void fill_area_id(bool with_flag, base::ObjectPtr obj, const float *data);
375void fill_base(base::ObjectPtr obj, const float *bbox);
383const float *get_gpu_data(bool flag, const base::Blob<float> &blob);
390int get_area_id(float visible_ratios[4]);
391
392} // namespace camera
393} // namespace perception
394} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
std::shared_ptr< Object > ObjectPtr
Definition object.h:127
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)
Get the max score index object
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)
softnms for objects
void recover_bbox(int roi_w, int roi_h, int offset_y, std::vector< base::ObjectPtr > *objects)
recover detect bbox result to raw image
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)
filter target detection results based on given thresholds and parameters.
__host__ __device__ float sigmoid_gpu(float x)
void apply_nms(const bool *overlapped, const int num, std::vector< int > *indices)
Get all boxes with score larger than threshold
float get_bbox_size(const NormalizedBBox &bbox)
__host__ __device__ float jaccard_overlap_gpu(const float *bbox1, const float *bbox2)
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)
Get the objects gpu object
void fill_base(base::ObjectPtr obj, const float *bbox)
add bbox value to object
void fill_ratios(bool with_ratios, base::ObjectPtr obj, const float *bbox)
add ratio value to object
constexpr float minExpPower
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)
Get all boxes with score larger than threshold
void fill_frbox(bool with_frbox, base::ObjectPtr obj, const float *bbox)
add car front and backward bbox value to object
bool sort_score_pair_descend(const std::pair< float, T > &pair1, const std::pair< float, T > &pair2)
int get_area_id(float visible_ratios[4])
calculate the area id based on the visible ratios
void get_intersect_bbox(const NormalizedBBox &bbox1, const NormalizedBBox &bbox2, NormalizedBBox *intersect_bbox)
Get the intersect bbox object
__host__ __device__ float bbox_size_gpu(const float *bbox, const bool normalized)
void fill_area_id(bool with_flag, base::ObjectPtr obj, const float *data)
add area id value to object
constexpr float maxExpPower
constexpr int anchorSizeFactor
float get_jaccard_overlap(const NormalizedBBox &bbox1, const NormalizedBBox &bbox2)
Get the jaccard overlap object
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)
Get all boxes with score larger than threshold
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)
Get the objects cpu object
const float * get_gpu_data(bool flag, const base::Blob< float > &blob)
Get the gpu data object
void fill_bbox3d(bool with_box3d, base::ObjectPtr obj, const float *bbox)
add alpha h w l to object
void filter_bbox(const MinDims &min_dims, std::vector< base::ObjectPtr > *objects)
filter out the objects in the object array based on the given minimum dimension conditions and retain...
void fill_lights(bool with_lights, base::ObjectPtr obj, const float *bbox)
add car lights value to object
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)
nms for objects
class register implement
Definition arena_queue.h:37
bool operator()(NormalizedBBox i, NormalizedBBox j)
Definition postprocess.h:52
std::shared_ptr< base::Blob< float > > area_id_blob
std::shared_ptr< base::Blob< float > > lof_blob
Definition postprocess.h:97
std::shared_ptr< base::Blob< float > > det3_dim_blob
Definition postprocess.h:95
std::shared_ptr< base::Blob< float > > det2_obj_blob
Definition postprocess.h:85
std::shared_ptr< base::Blob< float > > det3_ori_conf_blob
Definition postprocess.h:93
std::shared_ptr< base::Blob< float > > expand_blob
std::shared_ptr< base::Blob< float > > visible_ratio_blob
std::shared_ptr< base::Blob< float > > det3_cls_blob
Definition postprocess.h:92
std::shared_ptr< base::Blob< float > > ltvis_blob
std::shared_ptr< base::Blob< float > > res_cls_blob
std::shared_ptr< base::Blob< float > > res_box_blob
std::shared_ptr< base::Blob< float > > det3_loc_blob
Definition postprocess.h:90
std::shared_ptr< base::Blob< float > > feat_blob
std::shared_ptr< base::Blob< float > > det1_loc_blob
Definition postprocess.h:78
std::shared_ptr< base::Blob< float > > det1_cls_blob
Definition postprocess.h:80
std::shared_ptr< base::Blob< float > > det1_dim_blob
Definition postprocess.h:83
std::shared_ptr< base::Blob< float > > lor_blob
Definition postprocess.h:98
std::shared_ptr< base::Blob< float > > anchor_blob
std::shared_ptr< base::Blob< float > > det3_obj_blob
Definition postprocess.h:91
std::shared_ptr< base::Blob< float > > brswt_blob
std::shared_ptr< base::Blob< float > > det1_ori_conf_blob
Definition postprocess.h:81
std::shared_ptr< base::Blob< float > > ltswt_blob
std::shared_ptr< base::Blob< float > > det2_ori_blob
Definition postprocess.h:88
std::shared_ptr< base::Blob< float > > brvis_blob
Definition postprocess.h:99
std::shared_ptr< base::Blob< float > > det3_ori_blob
Definition postprocess.h:94
std::shared_ptr< base::Blob< float > > det1_ori_blob
Definition postprocess.h:82
std::shared_ptr< base::Blob< float > > det1_obj_blob
Definition postprocess.h:79
std::shared_ptr< base::Blob< float > > rtvis_blob
std::shared_ptr< base::Blob< float > > det2_ori_conf_blob
Definition postprocess.h:87
std::shared_ptr< base::Blob< float > > cut_off_ratio_blob
std::shared_ptr< base::Blob< float > > det2_cls_blob
Definition postprocess.h:86
std::shared_ptr< base::Blob< float > > det2_dim_blob
Definition postprocess.h:89
std::shared_ptr< base::Blob< float > > det2_loc_blob
Definition postprocess.h:84
std::shared_ptr< base::Blob< float > > rtswt_blob