Apollo  6.0
Open source self driving car software
dfmb_psroi_align_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 "cyber/common/log.h"
21 
22 namespace apollo {
23 namespace perception {
24 namespace inference {
25 
26 // TODO(chenjiahao): complete member functions
27 // Custom layer for DFMBPSROIAlign operation,
28 // i.e. DeForMaBle Position Sensitive ROI Align.
29 // input0 dims: [C, H, W], input1 dims: [num_rois, 5, 1, 1]
30 // input2 dims: [N, C2, H2, W2]
31 class DFMBPSROIAlignPlugin : public nvinfer1::IPlugin {
32  public:
34  const DFMBPSROIAlignParameter &dfmb_psroi_align_parameter,
35  nvinfer1::Dims *in_dims, int nbInputs) {
36  heat_map_a_ = dfmb_psroi_align_parameter.heat_map_a();
37  output_channel_ = dfmb_psroi_align_parameter.output_dim();
38  group_height_ = dfmb_psroi_align_parameter.group_height();
39  group_width_ = dfmb_psroi_align_parameter.group_width();
40  pooled_height_ = dfmb_psroi_align_parameter.pooled_height();
41  pooled_width_ = dfmb_psroi_align_parameter.pooled_width();
42  pad_ratio_ = dfmb_psroi_align_parameter.pad_ratio();
43  sample_per_part_ = dfmb_psroi_align_parameter.sample_per_part();
44 
45  trans_std_ = dfmb_psroi_align_parameter.trans_std();
46  part_height_ = dfmb_psroi_align_parameter.part_height();
47  part_width_ = dfmb_psroi_align_parameter.part_width();
48  heat_map_b_ = dfmb_psroi_align_parameter.heat_map_b();
49  no_trans_ = (nbInputs < 3);
50  num_classes_ = no_trans_ ? 1 : in_dims[2].d[1];
51 
52  CHECK_GT(heat_map_a_, 0);
53  CHECK_GE(heat_map_b_, 0);
54  CHECK_GE(pad_ratio_, 0);
55  CHECK_GT(output_channel_, 0);
56  CHECK_GT(sample_per_part_, 0);
57  CHECK_GT(group_height_, 0);
58  CHECK_GT(group_width_, 0);
59  CHECK_GT(pooled_height_, 0);
60  CHECK_GT(pooled_width_, 0);
61  CHECK_GE(part_height_, 0);
62  CHECK_GE(part_width_, 0);
63 
64  channels_ = in_dims[0].d[0];
65  height_ = in_dims[0].d[1];
66  width_ = in_dims[0].d[2];
67  output_dims_ = nvinfer1::Dims4(in_dims[1].d[0], output_channel_,
68  pooled_height_, pooled_width_);
69  output_size_ =
70  in_dims[1].d[0] * output_channel_ * pooled_height_ * pooled_width_;
71 
72  CHECK_EQ(channels_, output_channel_ * group_height_ * group_width_);
73  CHECK_EQ(in_dims[1].d[1], 5);
74  if (!no_trans_) {
75  CHECK_EQ(in_dims[2].d[1] % 2, 0);
76  int num_classes = in_dims[2].d[1] / 2;
77  CHECK_EQ(output_channel_ % num_classes, 0);
78  CHECK_EQ(part_height_, in_dims[2].d[2]);
79  CHECK_EQ(part_width_, in_dims[2].d[3]);
80  }
81  }
82 
83  virtual ~DFMBPSROIAlignPlugin() {}
84 
85  virtual int initialize() { return 0; }
86  virtual void terminate() {}
87  int getNbOutputs() const override { return 1; }
88 
89  nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs,
90  int nbInputDims) override {
91  // TODO(chenjiahao): complete input dims assertion
92  return output_dims_;
93  }
94 
95  void configure(const nvinfer1::Dims *inputDims, int nbInputs,
96  const nvinfer1::Dims *outputDims, int nbOutputs,
97  int maxBatchSize) override {}
98 
99  size_t getWorkspaceSize(int maxBatchSize) const override { return 0; }
100 
101  virtual int enqueue(int batchSize, const void *const *inputs, void **outputs,
102  void *workspace, cudaStream_t stream);
103 
104  size_t getSerializationSize() override { return 0; }
105 
106  void serialize(void *buffer) override {
107  char *d = reinterpret_cast<char *>(buffer), *a = d;
108  size_t size = getSerializationSize();
109  CHECK_EQ(d, a + size);
110  }
111 
112  private:
113  const int thread_size_ = 512;
114  float heat_map_a_;
115  float heat_map_b_;
116  float pad_ratio_;
117 
118  int output_channel_;
119  bool no_trans_;
120  float trans_std_;
121  int sample_per_part_;
122  int group_height_;
123  int group_width_;
124  int pooled_height_;
125  int pooled_width_;
126  int part_height_;
127  int part_width_;
128  int num_classes_;
129 
130  int channels_;
131  int height_;
132  int width_;
133  int output_size_;
134 
135  nvinfer1::Dims output_dims_;
136 };
137 
138 } // namespace inference
139 } // namespace perception
140 } // namespace apollo
size_t getSerializationSize() override
Definition: dfmb_psroi_align_plugin.h:104
virtual int enqueue(int batchSize, const void *const *inputs, void **outputs, void *workspace, cudaStream_t stream)
int getNbOutputs() const override
Definition: dfmb_psroi_align_plugin.h:87
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual int initialize()
Definition: dfmb_psroi_align_plugin.h:85
void serialize(void *buffer) override
Definition: dfmb_psroi_align_plugin.h:106
virtual void terminate()
Definition: dfmb_psroi_align_plugin.h:86
size_t getWorkspaceSize(int maxBatchSize) const override
Definition: dfmb_psroi_align_plugin.h:99
void configure(const nvinfer1::Dims *inputDims, int nbInputs, const nvinfer1::Dims *outputDims, int nbOutputs, int maxBatchSize) override
Definition: dfmb_psroi_align_plugin.h:95
virtual ~DFMBPSROIAlignPlugin()
Definition: dfmb_psroi_align_plugin.h:83
Definition: dfmb_psroi_align_plugin.h:31
nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs, int nbInputDims) override
Definition: dfmb_psroi_align_plugin.h:89
DFMBPSROIAlignPlugin(const DFMBPSROIAlignParameter &dfmb_psroi_align_parameter, nvinfer1::Dims *in_dims, int nbInputs)
Definition: dfmb_psroi_align_plugin.h:33