Apollo 11.0
自动驾驶开放平台
rpn_proposal_ssd_plugin.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#pragma once
18
19#include <algorithm>
20#include <NvInferVersion.h>
21
23
24namespace apollo {
25namespace perception {
26namespace inference {
27
28// TODO(chenjiahao): complete member functions
29// Custom layer for RPNProposalSSD operation, i.e.
30// anchor generation and nms filtering
31#ifdef NV_TENSORRT_MAJOR
32#if NV_TENSORRT_MAJOR != 8
33class RPNProposalSSDPlugin : public nvinfer1::IPlugin {
34 public:
36 const BBoxRegParameter &bbox_reg_param,
37 const DetectionOutputSSDParameter &detection_output_ssd_param,
38 nvinfer1::Dims *in_dims) {
39 height_ = in_dims[0].d[1];
40 width_ = in_dims[0].d[2];
41
42 for (int i = 0; i < 4; ++i) {
43 bbox_mean_[i] = bbox_reg_param.bbox_mean(i);
44 bbox_std_[i] = bbox_reg_param.bbox_std(i);
45 }
46
47 GenAnchorParameter gen_anchor_param =
48 detection_output_ssd_param.gen_anchor_param();
49 num_anchor_per_point_ = std::min(gen_anchor_param.anchor_width().size(),
50 gen_anchor_param.anchor_height().size());
51 anchor_heights_ = new float[num_anchor_per_point_]();
52 anchor_widths_ = new float[num_anchor_per_point_]();
53 for (int i = 0; i < num_anchor_per_point_; ++i) {
54 anchor_heights_[i] = gen_anchor_param.anchor_height(i);
55 anchor_widths_[i] = gen_anchor_param.anchor_width(i);
56 }
57
58 heat_map_a_ = detection_output_ssd_param.heat_map_a();
59
60 min_size_mode_ =
61 static_cast<int>(detection_output_ssd_param.min_size_mode());
62 min_size_h_ = detection_output_ssd_param.min_size_h();
63 min_size_w_ = detection_output_ssd_param.min_size_w();
64
65 threshold_objectness_ = detection_output_ssd_param.threshold_objectness();
66 refine_out_of_map_bbox_ =
67 detection_output_ssd_param.refine_out_of_map_bbox();
68
69 NMSSSDParameter nms_param = detection_output_ssd_param.nms_param();
70 max_candidate_n_ = nms_param.max_candidate_n(0);
71 overlap_ratio_ = nms_param.overlap_ratio(0);
72 top_n_ = nms_param.top_n(0);
73 }
74
75 virtual ~RPNProposalSSDPlugin() {}
76
77 virtual int initialize() { return 0; }
78 virtual void terminate() {}
79 int getNbOutputs() const override { return 1; }
80
81 nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs,
82 int nbInputDims) override {
83 // TODO(chenjiahao): complete inputs dims assertion
84 // TODO(chenjiahao): batch size is hard coded to 1 here
85 return nvinfer1::Dims4(top_n_ * 1, 5, 1, 1);
86 }
87
88 void configure(const nvinfer1::Dims *inputDims, int nbInputs,
89 const nvinfer1::Dims *outputDims, int nbOutputs,
90 int maxBatchSize) override {}
91
92 size_t getWorkspaceSize(int maxBatchSize) const override { return 0; }
93
94 virtual int enqueue(int batchSize, const void *const *inputs, void **outputs,
95 void *workspace, cudaStream_t stream);
96
97 size_t getSerializationSize() override { return 0; }
98
99 void serialize(void *buffer) override {
100 char *d = reinterpret_cast<char *>(buffer), *a = d;
101 size_t size = getSerializationSize();
102 CHECK_EQ(d, a + size);
103 }
104
105 private:
106 const int thread_size_ = 512;
107 bool refine_out_of_map_bbox_ = true;
108 float bbox_mean_[4];
109 float bbox_std_[4];
110 float heat_map_a_;
111 float min_size_h_ = 0.0f;
112 float min_size_w_ = 0.0f;
113 float threshold_objectness_ = 0.2f;
114 float overlap_ratio_ = 0.7f;
115 int height_;
116 int width_;
117 int max_candidate_n_ = 3000;
118 int min_size_mode_ = 0;
119 int num_anchor_per_point_;
120 int top_n_ = 300;
121 int out_rois_num_;
122
123 float *anchor_heights_;
124 float *anchor_widths_;
125};
126
127#else
128class RPNProposalSSDPlugin : public nvinfer1::IPluginV2Ext {
129 public:
131 const BBoxRegParameter &bbox_reg_param,
132 const DetectionOutputSSDParameter &detection_output_ssd_param,
133 nvinfer1::Dims *in_dims) {
134 height_ = in_dims[0].d[1];
135 width_ = in_dims[0].d[2];
136
137 for (int i = 0; i < 4; ++i) {
138 bbox_mean_[i] = bbox_reg_param.bbox_mean(i);
139 bbox_std_[i] = bbox_reg_param.bbox_std(i);
140 }
141
142 GenAnchorParameter gen_anchor_param =
143 detection_output_ssd_param.gen_anchor_param();
144 num_anchor_per_point_ = std::min(gen_anchor_param.anchor_width().size(),
145 gen_anchor_param.anchor_height().size());
146 anchor_heights_ = new float[num_anchor_per_point_]();
147 anchor_widths_ = new float[num_anchor_per_point_]();
148 for (int i = 0; i < num_anchor_per_point_; ++i) {
149 anchor_heights_[i] = gen_anchor_param.anchor_height(i);
150 anchor_widths_[i] = gen_anchor_param.anchor_width(i);
151 }
152
153 heat_map_a_ = detection_output_ssd_param.heat_map_a();
154
155 min_size_mode_ =
156 static_cast<int>(detection_output_ssd_param.min_size_mode());
157 min_size_h_ = detection_output_ssd_param.min_size_h();
158 min_size_w_ = detection_output_ssd_param.min_size_w();
159
160 threshold_objectness_ = detection_output_ssd_param.threshold_objectness();
161 refine_out_of_map_bbox_ =
162 detection_output_ssd_param.refine_out_of_map_bbox();
163
164 NMSSSDParameter nms_param = detection_output_ssd_param.nms_param();
165 max_candidate_n_ = nms_param.max_candidate_n(0);
166 overlap_ratio_ = nms_param.overlap_ratio(0);
167 top_n_ = nms_param.top_n(0);
168 }
169
171 virtual ~RPNProposalSSDPlugin() {}
172
173 virtual int initialize() noexcept { return 0; }
174 virtual void terminate() noexcept {}
175 int32_t getNbOutputs() const noexcept override { return 1; }
176
177 nvinfer1::Dims getOutputDimensions(int32_t index,
178 const nvinfer1::Dims *inputs, int32_t nbInputDims)
179 noexcept override {
180 // TODO(chenjiahao): complete inputs dims assertion
181 // TODO(chenjiahao): batch size is hard coded to 1 here
182 return nvinfer1::Dims4(top_n_ * 1, 5, 1, 1);
183 }
184
185 void configureWithFormat(const nvinfer1::Dims *inputDims, int32_t nbInputs,
186 const nvinfer1::Dims *outputDims, int32_t nbOutputs,
187 nvinfer1::DataType type, nvinfer1::PluginFormat format,
188 int32_t maxBatchSize) noexcept override {}
189
190 size_t getWorkspaceSize(int32_t maxBatchSize)
191 const noexcept override { return 0; }
192
193 int32_t enqueue(int32_t batchSize, const void *const *inputs,
194 void *const *outputs, void *workspace,
195 cudaStream_t stream) noexcept override;
196
197 size_t getSerializationSize() const noexcept override { return 0; }
198
199 void serialize(void *buffer) const noexcept override {
200 char *d = reinterpret_cast<char *>(buffer), *a = d;
201 size_t size = getSerializationSize();
202 CHECK_EQ(d, a + size);
203 }
204
205 nvinfer1::AsciiChar const* getPluginType()
206 const noexcept override {
207 return plugin_type;
208 }
209
210 nvinfer1::AsciiChar const* getPluginVersion()
211 const noexcept override {
212 return plugin_version;
213 }
214
215 void setPluginNamespace(const nvinfer1::AsciiChar* libNamespace)
216 noexcept override {
217 plugin_namespace = const_cast<nvinfer1::AsciiChar*>(libNamespace);
218 }
219
220 nvinfer1::AsciiChar const* getPluginNamespace()
221 const noexcept override {
222 return const_cast<nvinfer1::AsciiChar*>(plugin_namespace);
223 }
224
225 bool supportsFormat(nvinfer1::DataType type,
226 nvinfer1::PluginFormat format) const noexcept override {
227 return true;
228 }
229
230 void destroy() noexcept override {
231 delete this;
232 }
233
234 nvinfer1::IPluginV2Ext* clone() const noexcept override {
236 p->refine_out_of_map_bbox_ = refine_out_of_map_bbox_;
237
238 for (int i = 0; i < 4; i++) {
239 p->bbox_mean_[i] = bbox_mean_[i];
240 }
241 for (int i = 0; i < 4; i++) {
242 p->bbox_std_[i] = bbox_std_[i];
243 }
244
245 p->heat_map_a_ = heat_map_a_;
246 p->min_size_h_ = min_size_h_;
247 p->min_size_w_ = min_size_w_;
248 p->threshold_objectness_ = threshold_objectness_;
249 p->overlap_ratio_ = overlap_ratio_;
250 p->height_ = height_;
251 p->width_ = width_;
252 p->max_candidate_n_ = max_candidate_n_;
253 p->min_size_mode_ = min_size_mode_;
254 p->num_anchor_per_point_ = num_anchor_per_point_;
255 p->top_n_ = top_n_;
256 p->out_rois_num_ = out_rois_num_;
257
258 p->anchor_heights_ = new float[num_anchor_per_point_]();
259 p->anchor_widths_ = new float[num_anchor_per_point_]();
260 for (int i = 0; i < num_anchor_per_point_; ++i) {
261 p->anchor_heights_[i] = anchor_heights_[i];
262 p->anchor_widths_[i] = anchor_widths_[i];
263 }
264 p->plugin_namespace = plugin_namespace;
265 return p;
266 }
267
268 bool isOutputBroadcastAcrossBatch(int32_t outputIndex,
269 bool const *inputIsBroadcasted, int32_t nbInputs)
270 const noexcept override {
271 return false;
272 }
273
274 bool canBroadcastInputAcrossBatch(int32_t inputIndex)
275 const noexcept override {
276 return false;
277 }
278
279 nvinfer1::DataType getOutputDataType(int32_t index,
280 nvinfer1::DataType const *inputTypes, int32_t nbInputs) const noexcept {
281 return nvinfer1::DataType::kFLOAT;
282 }
283
284 void configurePlugin(
285 nvinfer1::Dims const *inputDims, int32_t nbInputs,
286 nvinfer1::Dims const *outputDims, int32_t nbOutputs,
287 nvinfer1::DataType const *inputTypes, nvinfer1::DataType const *outputTypes,
288 bool const *inputIsBroadcast, bool const *outputIsBroadcast,
289 nvinfer1::PluginFormat floatFormat, int32_t maxBatchSize) noexcept {}
290
291 private:
292 const int thread_size_ = 512;
293 bool refine_out_of_map_bbox_ = true;
294 float bbox_mean_[4];
295 float bbox_std_[4];
296 float heat_map_a_;
297 float min_size_h_ = 0.0f;
298 float min_size_w_ = 0.0f;
299 float threshold_objectness_ = 0.2f;
300 float overlap_ratio_ = 0.7f;
301 int height_;
302 int width_;
303 int max_candidate_n_ = 3000;
304 int min_size_mode_ = 0;
305 int num_anchor_per_point_;
306 int top_n_ = 300;
307 int out_rois_num_;
308
309 float *anchor_heights_;
310 float *anchor_widths_;
311 nvinfer1::AsciiChar* plugin_namespace;
312 const nvinfer1::AsciiChar* plugin_type = "";
313 const nvinfer1::AsciiChar* plugin_version = "";
314};
315
316#endif
317#endif
318
319} // namespace inference
320} // namespace perception
321} // namespace apollo
Dims getOutputDimensions(int index, const Dims *inputs, int nbInputDims) override
RPNProposalSSDPlugin(const BBoxRegParameter &bbox_reg_param, const DetectionOutputSSDParameter &detection_output_ssd_param, Dims *in_dims)
int enqueue(int batchSize, const void *const *inputs, void **outputs, void *workspace, cudaStream_t stream) override
void configure(const Dims *inputDims, int nbInputs, const Dims *outputDims, int nbOutputs, int maxBatchSize) override
size_t getWorkspaceSize(int maxBatchSize) const override
class register implement
Definition arena_queue.h:37