Apollo  6.0
Open source self driving car software
point_pillars.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 
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 #include "NvInfer.h"
54 #include "NvOnnxParser.h"
55 #include "torch/script.h"
56 #include "torch/torch.h"
57 
58 // headers in local files
66 
67 namespace apollo {
68 namespace perception {
69 namespace lidar {
70 
71 // Logger for TensorRT info/warning/errors
72 class Logger : public nvinfer1::ILogger {
73  public:
74  explicit Logger(Severity severity = Severity::kWARNING)
75  : reportable_severity(severity) {}
76 
77  void log(Severity severity, const char* msg) override {
78  // suppress messages with severity enum value greater than the reportable
79  if (severity > reportable_severity) return;
80 
81  switch (severity) {
82  case Severity::kINTERNAL_ERROR:
83  std::cerr << "INTERNAL_ERROR: ";
84  break;
85  case Severity::kERROR:
86  std::cerr << "ERROR: ";
87  break;
88  case Severity::kWARNING:
89  std::cerr << "WARNING: ";
90  break;
91  case Severity::kINFO:
92  std::cerr << "INFO: ";
93  break;
94  default:
95  std::cerr << "UNKNOWN: ";
96  break;
97  }
98  std::cerr << msg << std::endl;
99  }
100 
102 };
103 
105  private:
106  friend class TestClass;
107  static const float kPillarXSize;
108  static const float kPillarYSize;
109  static const float kPillarZSize;
110  static const float kMinXRange;
111  static const float kMinYRange;
112  static const float kMinZRange;
113  static const float kMaxXRange;
114  static const float kMaxYRange;
115  static const float kMaxZRange;
116  static const int kNumClass;
117  static const int kMaxNumPillars;
118  static const int kMaxNumPointsPerPillar;
119  static const int kNumPointFeature;
120  static const int kGridXSize;
121  static const int kGridYSize;
122  static const int kGridZSize;
123  static const int kRpnInputSize;
124  static const int kNumAnchor;
125  static const int kNumOutputBoxFeature;
126  static const int kRpnBoxOutputSize;
127  static const int kRpnClsOutputSize;
128  static const int kRpnDirOutputSize;
129  static const int kBatchSize;
130  static const int kNumIndsForScan;
131  static const int kNumThreads;
132  // if you change kNumThreads, need to modify NUM_THREADS_MACRO in
133  // common.h
134  static const int kNumBoxCorners;
135  static const std::vector<int> kAnchorStrides;
136  static const std::vector<int> kAnchorRanges;
137  static const std::vector<int> kNumAnchorSets;
138  static const std::vector<std::vector<float>> kAnchorDxSizes;
139  static const std::vector<std::vector<float>> kAnchorDySizes;
140  static const std::vector<std::vector<float>> kAnchorDzSizes;
141  static const std::vector<std::vector<float>> kAnchorZCoors;
142  static const std::vector<std::vector<int>> kNumAnchorRo;
143  static const std::vector<std::vector<float>> kAnchorRo;
144 
145  // initialize in initializer list
146  const bool reproduce_result_mode_;
147  const float score_threshold_;
148  const float nms_overlap_threshold_;
149  const std::string pfe_onnx_file_;
150  const std::string rpn_onnx_file_;
151  const std::string pfe_torch_file_;
152  const std::string scattered_torch_file_;
153  const std::string backbone_torch_file_;
154  const std::string fpn_torch_file_;
155  const std::string bbox_head_torch_file_;
156  // end initializer list
157 
158  int host_pillar_count_[1];
159 
160  float* anchors_px_;
161  float* anchors_py_;
162  float* anchors_pz_;
163  float* anchors_dx_;
164  float* anchors_dy_;
165  float* anchors_dz_;
166  float* anchors_ro_;
167 
168  float* box_anchors_min_x_;
169  float* box_anchors_min_y_;
170  float* box_anchors_max_x_;
171  float* box_anchors_max_y_;
172 
173  int* dev_x_coors_;
174  int* dev_y_coors_;
175  float* dev_num_points_per_pillar_;
176  int* dev_sparse_pillar_map_;
177  int* dev_cumsum_along_x_;
178  int* dev_cumsum_along_y_;
179 
180  float* dev_pillar_point_feature_;
181  float* dev_pillar_coors_;
182 
183  float* dev_box_anchors_min_x_;
184  float* dev_box_anchors_min_y_;
185  float* dev_box_anchors_max_x_;
186  float* dev_box_anchors_max_y_;
187  int* dev_anchor_mask_;
188 
189  void* pfe_buffers_[3];
190  void* rpn_buffers_[4];
191 
192  float* dev_scattered_feature_;
193 
194  float* dev_anchors_px_;
195  float* dev_anchors_py_;
196  float* dev_anchors_pz_;
197  float* dev_anchors_dx_;
198  float* dev_anchors_dy_;
199  float* dev_anchors_dz_;
200  float* dev_anchors_ro_;
201  float* dev_filtered_box_;
202  float* dev_filtered_score_;
203  int* dev_filtered_label_;
204  int* dev_filtered_dir_;
205  float* dev_box_for_nms_;
206  int* dev_filter_count_;
207 
208  std::unique_ptr<PreprocessPoints> preprocess_points_ptr_;
209  std::unique_ptr<PreprocessPointsCuda> preprocess_points_cuda_ptr_;
210  std::unique_ptr<AnchorMaskCuda> anchor_mask_cuda_ptr_;
211  std::unique_ptr<PostprocessCuda> postprocess_cuda_ptr_;
212 
213  Logger g_logger_;
214  nvinfer1::ICudaEngine* pfe_engine_;
215  nvinfer1::ICudaEngine* rpn_engine_;
216  nvinfer1::IExecutionContext* pfe_context_;
217  nvinfer1::IExecutionContext* rpn_context_;
218 
219  int device_id_ = -1;
220  int gpu_id_ = 0;
221  torch::DeviceType device_type_;
222  torch::jit::script::Module pfe_net_;
223  torch::jit::script::Module scattered_net_;
224  torch::jit::script::Module backbone_net_;
225  torch::jit::script::Module fpn_net_;
226  torch::jit::script::Module bbox_head_net_;
227 
232  void DeviceMemoryMalloc();
233 
238  void InitAnchors();
239 
244  void InitTorch();
245 
250  void InitTRT();
251 
270  void GenerateAnchors(float* anchors_px_, float* anchors_py_,
271  float* anchors_pz_, float* anchors_dx_,
272  float* anchors_dy_, float* anchors_dz_,
273  float* anchors_ro_);
274 
281  void OnnxToTRTModel(const std::string& model_file,
282  nvinfer1::ICudaEngine** engine_ptr);
283 
290  void Preprocess(const float* in_points_array, const int in_num_points);
291 
299  void PreprocessCPU(const float* in_points_array, const int in_num_points);
300 
307  void PreprocessGPU(const float* in_points_array, const int in_num_points);
308 
325  void ConvertAnchors2BoxAnchors(float* anchors_px_, float* anchors_py_,
326  float* box_anchors_min_x_,
327  float* box_anchors_min_y_,
328  float* box_anchors_max_x_,
329  float* box_anchors_max_y_);
330 
335  void PutAnchorsInDeviceMemory();
336 
337  public:
351  PointPillars(const bool reproduce_result_mode, const float score_threshold,
352  const float nms_overlap_threshold,
353  const std::string& pfe_torch_file,
354  const std::string& scattered_torch_file,
355  const std::string& backbone_torch_file,
356  const std::string& fpn_torch_file,
357  const std::string& bbox_head_torch_file);
358  ~PointPillars();
359 
368  void DoInference(const float* in_points_array, const int in_num_points,
369  std::vector<float>* out_detections,
370  std::vector<int>* out_labels);
371 };
372 
373 } // namespace lidar
374 } // namespace perception
375 } // namespace apollo
void log(Severity severity, const char *msg) override
Definition: point_pillars.h:77
Postprocess for network output.
Definition: point_pillars.h:104
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
CPU version of preprocess points.
GPU version of preprocess points.
Severity reportable_severity
Definition: point_pillars.h:101
Make anchor mask for filtering output.
Logger(Severity severity=Severity::kWARNING)
Definition: point_pillars.h:74
CUDA code for scatter operation.
Definition: point_pillars.h:72