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