Apollo 11.0
自动驾驶开放平台
point_pillars.h
浏览该文件的文档.
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
17/*
18 * Copyright 2018-2019 Autoware Foundation. All rights reserved.
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
40#pragma once
41
42// headers in STL
43#include <algorithm>
44#include <cmath>
45#include <iomanip>
46#include <limits>
47#include <map>
48#include <memory>
49#include <string>
50#include <vector>
51
52// headers in TensorRT
53#if GPU_PLATFORM == NVIDIA
54 #include "NvInfer.h"
55 #include "NvOnnxParser.h"
56#endif
57#include "torch/script.h"
58#include "torch/torch.h"
59
60// headers in local files
70
71namespace apollo {
72namespace perception {
73namespace lidar {
74
75// Logger for TensorRT info/warning/errors
76#if GPU_PLATFORM == NVIDIA
77class Logger : public nvinfer1::ILogger {
78 public:
79 explicit Logger(Severity severity = Severity::kWARNING)
80 : reportable_severity(severity) {}
81
82 void log(Severity severity, const char* msg) noexcept override {
83 // suppress messages with severity enum value greater than the reportable
84 if (severity > reportable_severity) return;
85
86 switch (severity) {
87 case Severity::kINTERNAL_ERROR:
88 std::cerr << "INTERNAL_ERROR: ";
89 break;
90 case Severity::kERROR:
91 std::cerr << "ERROR: ";
92 break;
93 case Severity::kWARNING:
94 std::cerr << "WARNING: ";
95 break;
96 case Severity::kINFO:
97 std::cerr << "INFO: ";
98 break;
99 default:
100 std::cerr << "UNKNOWN: ";
101 break;
102 }
103 std::cerr << msg << std::endl;
104 }
105
107};
108#elif GPU_PLATFORM == AMD
109 class Logger {};
110namespace nvinfer1 {
111 class ICudaEngine {};
112 class IExecutionContext {};
113}
114#endif
115
117 private:
118 friend class TestClass;
119 static const float kPillarXSize;
120 static const float kPillarYSize;
121 static const float kPillarZSize;
122 static const float kMinXRange;
123 static const float kMinYRange;
124 static const float kMinZRange;
125 static const float kMaxXRange;
126 static const float kMaxYRange;
127 static const float kMaxZRange;
128 static const int kNumClass;
129 static const int kMaxNumPillars;
130 static const int kMaxNumPointsPerPillar;
131 static const int kNumPointFeature;
132 static const int kGridXSize;
133 static const int kGridYSize;
134 static const int kGridZSize;
135 static const int kRpnInputSize;
136 static const int kNumAnchor;
137 static const int kNumOutputBoxFeature;
138 static const int kRpnBoxOutputSize;
139 static const int kRpnClsOutputSize;
140 static const int kRpnDirOutputSize;
141 static const int kBatchSize;
142 static const int kNumIndsForScan;
143 static const int kNumThreads;
144 static const int kGridSize;
145 static const int kNumAnchorPerGrid;
146 // if you change kNumThreads, need to modify NUM_THREADS_MACRO in
147 // common.h
148 static const int kNumBoxCorners;
149 static const std::vector<int> kAnchorStrides;
150 static const std::vector<int> kAnchorRanges;
151 static const std::vector<int> kNumAnchorSets;
152 static const std::vector<std::vector<float>> kAnchorDxSizes;
153 static const std::vector<std::vector<float>> kAnchorDySizes;
154 static const std::vector<std::vector<float>> kAnchorDzSizes;
155 static const std::vector<std::vector<float>> kAnchorZCoors;
156 static const std::vector<std::vector<int>> kNumAnchorRo;
157 static const std::vector<std::vector<float>> kAnchorRo;
158
159 // initialize in initializer list
160 const bool reproduce_result_mode_;
161 const float score_threshold_;
162 const float nms_overlap_threshold_;
163 const std::string pfe_onnx_file_;
164 const std::string rpn_onnx_file_;
165 const common::Framework& model_type_;
166 const std::string model_file_;
167 // end initializer list
168
169 int host_pillar_count_[1];
170
171 float* anchors_px_;
172 float* anchors_py_;
173 float* anchors_pz_;
174 float* anchors_dx_;
175 float* anchors_dy_;
176 float* anchors_dz_;
177 float* anchors_ro_;
178
179 float* box_anchors_min_x_;
180 float* box_anchors_min_y_;
181 float* box_anchors_max_x_;
182 float* box_anchors_max_y_;
183
184 int* dev_x_coors_;
185 int* dev_y_coors_;
186 float* dev_num_points_per_pillar_;
187 int* dev_sparse_pillar_map_;
188 int* dev_cumsum_along_x_;
189 int* dev_cumsum_along_y_;
190
191 float* dev_pillar_point_feature_;
192 float* dev_pillar_coors_;
193
194 float* dev_box_anchors_min_x_;
195 float* dev_box_anchors_min_y_;
196 float* dev_box_anchors_max_x_;
197 float* dev_box_anchors_max_y_;
198 int* dev_anchor_mask_;
199
200 void* pfe_buffers_[3];
201 void* rpn_buffers_[4];
202
203 float* dev_scattered_feature_;
204
205 float* dev_anchors_px_;
206 float* dev_anchors_py_;
207 float* dev_anchors_pz_;
208 float* dev_anchors_dx_;
209 float* dev_anchors_dy_;
210 float* dev_anchors_dz_;
211 float* dev_anchors_ro_;
212 float* dev_filtered_box_;
213 float* dev_filtered_score_;
214 int* dev_filtered_label_;
215 int* dev_filtered_dir_;
216 float* dev_box_for_nms_;
217 int* dev_filter_count_;
218
219 std::unique_ptr<PreprocessPoints> preprocess_points_ptr_;
220 std::unique_ptr<PreprocessPointsCuda> preprocess_points_cuda_ptr_;
221 std::unique_ptr<AnchorMaskCuda> anchor_mask_cuda_ptr_;
222 std::unique_ptr<PostprocessCuda> postprocess_cuda_ptr_;
223
224 Logger g_logger_;
225 nvinfer1::ICudaEngine* pfe_engine_;
226 nvinfer1::ICudaEngine* rpn_engine_;
227 nvinfer1::IExecutionContext* pfe_context_;
228 nvinfer1::IExecutionContext* rpn_context_;
229
230 int device_id_ = 0;
231 std::vector<std::string> input_blob_names_ = {
232 "pillar_point_feature",
233 "points_per_pillar",
234 "pillar_coors"
235 };
236 std::vector<std::string> output_blob_names_ = {
237 "cls_score",
238 "bbox_pred",
239 "dir_cls_preds"
240 };
241 std::shared_ptr<inference::Inference> inference_;
242
247 void DeviceMemoryMalloc();
248
253 void InitAnchors();
254
259 void InitTorch();
260
265 void InitTRT();
266
285 void GenerateAnchors(float* anchors_px_, float* anchors_py_,
286 float* anchors_pz_, float* anchors_dx_,
287 float* anchors_dy_, float* anchors_dz_,
288 float* anchors_ro_);
289
296 void OnnxToTRTModel(const std::string& model_file,
297 nvinfer1::ICudaEngine** engine_ptr);
298
305 void Preprocess(const float* in_points_array, const int in_num_points);
306
314 void PreprocessCPU(const float* in_points_array, const int in_num_points);
315
322 void PreprocessGPU(const float* in_points_array, const int in_num_points);
323
340 void ConvertAnchors2BoxAnchors(float* anchors_px_, float* anchors_py_,
341 float* box_anchors_min_x_,
342 float* box_anchors_min_y_,
343 float* box_anchors_max_x_,
344 float* box_anchors_max_y_);
345
350 void PutAnchorsInDeviceMemory();
351
352 public:
363 PointPillars(const bool reproduce_result_mode,
364 const float score_threshold,
365 const float nms_overlap_threshold,
366 const common::Framework& model_type,
367 const std::string& model_file);
368
374
383 void DoInference(const float* in_points_array, const int in_num_points,
384 std::vector<float>* out_detections,
385 std::vector<int>* out_labels);
386};
387
388} // namespace lidar
389} // namespace perception
390} // namespace apollo
void log(Severity severity, const char *msg) noexcept override
Logger(Severity severity=Severity::kWARNING)
~PointPillars()
Destroy the Point Pillars object
void DoInference(const float *in_points_array, const int in_num_points, std::vector< float > *out_detections, std::vector< int > *out_labels)
Call PointPillars for the inference
Make anchor mask for filtering output
Postprocess for network output
CPU version of preprocess points
GPU version of preprocess points
CUDA code for scatter operation
class register implement
Definition arena_queue.h:37
MACRO for CUDA codes