Apollo  6.0
Open source self driving car software
rpn_proposal_ssd_plugin.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 #pragma once
18 
19 #include <algorithm>
20 
22 
23 namespace apollo {
24 namespace perception {
25 namespace inference {
26 
27 // TODO(chenjiahao): complete member functions
28 // Custom layer for RPNProposalSSD operation, i.e.
29 // anchor generation and nms filtering
30 class RPNProposalSSDPlugin : public nvinfer1::IPlugin {
31  public:
33  const BBoxRegParameter &bbox_reg_param,
34  const DetectionOutputSSDParameter &detection_output_ssd_param,
35  nvinfer1::Dims *in_dims) {
36  height_ = in_dims[0].d[1];
37  width_ = in_dims[0].d[2];
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  GenAnchorParameter gen_anchor_param =
45  detection_output_ssd_param.gen_anchor_param();
46  num_anchor_per_point_ = std::min(gen_anchor_param.anchor_width().size(),
47  gen_anchor_param.anchor_height().size());
48  anchor_heights_ = new float[num_anchor_per_point_]();
49  anchor_widths_ = new float[num_anchor_per_point_]();
50  for (int i = 0; i < num_anchor_per_point_; ++i) {
51  anchor_heights_[i] = gen_anchor_param.anchor_height(i);
52  anchor_widths_[i] = gen_anchor_param.anchor_width(i);
53  }
54 
55  heat_map_a_ = detection_output_ssd_param.heat_map_a();
56 
57  min_size_mode_ =
58  static_cast<int>(detection_output_ssd_param.min_size_mode());
59  min_size_h_ = detection_output_ssd_param.min_size_h();
60  min_size_w_ = detection_output_ssd_param.min_size_w();
61 
62  threshold_objectness_ = detection_output_ssd_param.threshold_objectness();
63  refine_out_of_map_bbox_ =
64  detection_output_ssd_param.refine_out_of_map_bbox();
65 
66  NMSSSDParameter nms_param = detection_output_ssd_param.nms_param();
67  max_candidate_n_ = nms_param.max_candidate_n(0);
68  overlap_ratio_ = nms_param.overlap_ratio(0);
69  top_n_ = nms_param.top_n(0);
70  }
71 
72  virtual ~RPNProposalSSDPlugin() {}
73 
74  virtual int initialize() { return 0; }
75  virtual void terminate() {}
76  int getNbOutputs() const override { return 1; }
77 
78  nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs,
79  int nbInputDims) override {
80  // TODO(chenjiahao): complete inputs dims assertion
81  // TODO(chenjiahao): batch size is hard coded to 1 here
82  return nvinfer1::Dims4(top_n_ * 1, 5, 1, 1);
83  }
84 
85  void configure(const nvinfer1::Dims *inputDims, int nbInputs,
86  const nvinfer1::Dims *outputDims, int nbOutputs,
87  int maxBatchSize) override {}
88 
89  size_t getWorkspaceSize(int maxBatchSize) const override { return 0; }
90 
91  virtual int enqueue(int batchSize, const void *const *inputs, void **outputs,
92  void *workspace, cudaStream_t stream);
93 
94  size_t getSerializationSize() override { return 0; }
95 
96  void serialize(void *buffer) override {
97  char *d = reinterpret_cast<char *>(buffer), *a = d;
98  size_t size = getSerializationSize();
99  CHECK_EQ(d, a + size);
100  }
101 
102  private:
103  const int thread_size_ = 512;
104  bool refine_out_of_map_bbox_ = true;
105  float bbox_mean_[4];
106  float bbox_std_[4];
107  float heat_map_a_;
108  float min_size_h_ = 0.0f;
109  float min_size_w_ = 0.0f;
110  float threshold_objectness_ = 0.2f;
111  float overlap_ratio_ = 0.7f;
112  int height_;
113  int width_;
114  int max_candidate_n_ = 3000;
115  int min_size_mode_ = 0;
116  int num_anchor_per_point_;
117  int top_n_ = 300;
118  int out_rois_num_;
119 
120  float *anchor_heights_;
121  float *anchor_widths_;
122 };
123 
124 } // namespace inference
125 } // namespace perception
126 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int getNbOutputs() const override
Definition: rpn_proposal_ssd_plugin.h:76
size_t getWorkspaceSize(int maxBatchSize) const override
Definition: rpn_proposal_ssd_plugin.h:89
void configure(const nvinfer1::Dims *inputDims, int nbInputs, const nvinfer1::Dims *outputDims, int nbOutputs, int maxBatchSize) override
Definition: rpn_proposal_ssd_plugin.h:85
virtual ~RPNProposalSSDPlugin()
Definition: rpn_proposal_ssd_plugin.h:72
virtual void terminate()
Definition: rpn_proposal_ssd_plugin.h:75
nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs, int nbInputDims) override
Definition: rpn_proposal_ssd_plugin.h:78
Definition: rpn_proposal_ssd_plugin.h:30
void serialize(void *buffer) override
Definition: rpn_proposal_ssd_plugin.h:96
RPNProposalSSDPlugin(const BBoxRegParameter &bbox_reg_param, const DetectionOutputSSDParameter &detection_output_ssd_param, nvinfer1::Dims *in_dims)
Definition: rpn_proposal_ssd_plugin.h:32
size_t getSerializationSize() override
Definition: rpn_proposal_ssd_plugin.h:94
virtual int initialize()
Definition: rpn_proposal_ssd_plugin.h:74
virtual int enqueue(int batchSize, const void *const *inputs, void **outputs, void *workspace, cudaStream_t stream)