Apollo 11.0
自动驾驶开放平台
rt_common.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
18
19#include <NvInferVersion.h>
20#include <utility>
21
22#ifdef NV_TENSORRT_MAJOR
23 #if NV_TENSORRT_MAJOR == 8
25 #endif
26#endif
27
28#include "absl/strings/str_cat.h"
29
30namespace apollo {
31namespace perception {
32namespace inference {
33
35 const nvinfer1::DimsCHW &inputDims) {
36 nvinfer1::DimsCHW outDims = inputDims;
37 int count = inputDims.d[0] * inputDims.d[1] * inputDims.d[2];
38 int constant = 1;
39 int axis_inference = -1;
40 for (int i = 0; i < inputDims.nbDims; ++i) {
41 if (dims.d[i] == 0) {
42 outDims.d[i] = inputDims.d[i];
43 constant *= outDims.d[i];
44 } else if (dims.d[i] > 0) {
45 constant *= dims.d[i];
46 } else if (dims.d[i] == -1) {
47 CHECK_EQ(axis_inference, -1);
48 outDims.d[i] = count / constant;
49 axis_inference = i;
50 }
51 }
52 return outDims;
53}
54
55void ParseNetParam(const NetParameter &net_param,
56 TensorDimsMap *tensor_dims_map,
57 std::map<std::string, std::string> *tensor_modify_map,
58 std::vector<LayerParameter> *order) {
59 for (int i = 0; i < net_param.layer_size(); ++i) {
60 LayerParameter tensorrt_layer_param = net_param.layer(i);
61 if (tensorrt_layer_param.type() == "Input") {
62 InputParameter input = tensorrt_layer_param.input_param();
63 for (int j = 0; j < tensorrt_layer_param.top().size(); ++j) {
64 nvinfer1::DimsCHW dims{static_cast<int>(input.shape(j).dim(1)),
65 static_cast<int>(input.shape(j).dim(2)),
66 static_cast<int>(input.shape(j).dim(3))};
67 auto name = tensorrt_layer_param.top(j);
68 tensor_dims_map->insert(std::make_pair(name, dims));
69 tensor_modify_map->insert(
70 std::make_pair(name, tensorrt_layer_param.top(j)));
71 }
72 } else if (net_param.layer(i).type() == "Pooling") {
73 if (tensorrt_layer_param.pooling_param().pad() == 0) {
74 order->push_back(tensorrt_layer_param);
75 continue;
76 }
77 LayerParameter fake_layer_param = tensorrt_layer_param;
78 fake_layer_param.set_type("Padding");
79 fake_layer_param.set_name(absl::StrCat("padding_split_", i));
80 fake_layer_param.clear_pooling_param();
81 auto pad_param = fake_layer_param.mutable_padding_param();
82 auto pool_param = tensorrt_layer_param.mutable_pooling_param();
83 modify_pool_param(pool_param);
84 pad_param->set_pad_l(pool_param->pad_w());
85 pad_param->set_pad_t(pool_param->pad_h());
86 pad_param->set_pad_b(pool_param->pad_h());
87 pad_param->set_pad_r(pool_param->pad_w());
88 fake_layer_param.set_top(0, fake_layer_param.name());
89 pool_param->set_pad(0);
90 tensorrt_layer_param.set_bottom(0, fake_layer_param.name());
91
92 order->push_back(fake_layer_param);
93 order->push_back(tensorrt_layer_param);
94 } else {
95 order->push_back(tensorrt_layer_param);
96 }
97 }
98}
99
100bool ParserConvParam(const ConvolutionParameter &conv, ConvParam *param) {
101 if (conv.has_kernel_h() || conv.has_kernel_w()) {
102 if (conv.kernel_size_size() != 0) {
103 return false;
104 }
105 param->kernel_h = conv.kernel_h();
106 param->kernel_w = conv.kernel_w();
107 } else {
108 if (conv.kernel_size_size() < 1) {
109 return false;
110 }
111 param->kernel_h = conv.kernel_size(0);
112 param->kernel_w = (conv.kernel_size_size() > 1 ? conv.kernel_size(1)
113 : conv.kernel_size(0));
114 }
115 if (param->kernel_h == 0 || param->kernel_w == 0) {
116 return false;
117 }
118
119 if (conv.has_pad_h() || conv.has_pad_w()) {
120 if (conv.pad_size() != 0) {
121 return false;
122 }
123 param->padding_h = conv.pad_h();
124 param->padding_w = conv.pad_w();
125 } else {
126 param->padding_h = (conv.pad().empty() ? 0 : conv.pad(0));
127 param->padding_w = (conv.pad_size() > 1 ? conv.pad(1) : param->padding_h);
128 }
129
130 if (conv.has_stride_h() || conv.has_stride_w()) {
131 if (conv.stride_size() != 0) {
132 return false;
133 }
134 param->stride_h = conv.stride_h();
135 param->stride_w = conv.stride_w();
136 } else {
137 param->stride_h = (conv.stride().empty() ? 1 : conv.stride(0));
138 param->stride_w =
139 (conv.stride_size() > 1 ? conv.stride(1) : param->stride_h);
140 }
141 if (param->stride_h == 0 || param->stride_w == 0) {
142 return false;
143 }
144
145 param->dilation = conv.dilation().empty() ? 1 : conv.dilation(0);
146 return true;
147}
148
149bool modify_pool_param(PoolingParameter *pool_param) {
150 if (pool_param->has_kernel_size()) {
151 pool_param->set_kernel_h(pool_param->kernel_size());
152 pool_param->set_kernel_w(pool_param->kernel_size());
153 }
154 if (pool_param->kernel_w() == 0 || pool_param->kernel_h() == 0) {
155 return false;
156 }
157 if (pool_param->has_pad()) {
158 pool_param->set_pad_h(pool_param->pad());
159 pool_param->set_pad_w(pool_param->pad());
160 }
161 if (pool_param->has_stride()) {
162 pool_param->set_stride_h(pool_param->stride());
163 pool_param->set_stride_w(pool_param->stride());
164 }
165 if (pool_param->stride_w() == 0 || pool_param->stride_h() == 0) {
166 return false;
167 }
168 return true;
169}
170
171} // namespace inference
172} // namespace perception
173} // namespace apollo
bool ParserConvParam(const ConvolutionParameter &conv, ConvParam *param)
Definition mi_common.cc:51
void ParseNetParam(const NetParameter &net_param, TensorDimsMap *tensor_dims_map, std::map< std::string, std::string > *tensor_modify_map, std::vector< LayerParameter > *order)
Definition mi_common.cc:27
nvinfer1::DimsCHW ReshapeDims(const nvinfer1::DimsCHW &dims, const nvinfer1::DimsCHW &inputDims)
Definition rt_common.cc:34
std::map< std::string, TensorDims > TensorDimsMap
Definition mi_common.h:33
bool modify_pool_param(PoolingParameter *pool_param)
Definition mi_common.cc:101
class register implement
Definition arena_queue.h:37