1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <gtest/gtest.h>
6 #include <inference_engine/shape_infer/built-in/ie_built_in_holder.hpp>
7 #include <xml_net_builder.hpp>
8 #include <inference_engine/cnn_network_impl.hpp>
9 #include <inference_engine/ie_format_parser.h>
10 #include <xml_helper.hpp>
11 #include <inference_engine/shape_infer/ie_reshaper.hpp>
12 #include "built_in_shape_infer_general_test.hpp"
14 using namespace InferenceEngine;
15 using namespace ShapeInfer;
17 class BuiltInShapeInferConvImplTest
18 : public BuiltInShapeInferTestWithParam<std::tuple<InOutShapes, kernel, stride, pad, auto_pad, out_channels, group, dilation_factor, NewInOutShapes, CanInfer, pad_end, IsTransposed>> {
20 void SetUp() override {
21 BuiltInShapeInferCommon::SetUp();
22 auto params = GetParam();
23 inOutShapes = std::get<0>(params);
24 kernel = std::get<1>(params);
25 stride = std::get<2>(params);
26 pad = std::get<3>(params);
27 auto_pad = std::get<4>(params);
28 out_channels = std::get<5>(params);
29 group = std::get<6>(params);
30 dilation_factor = std::get<7>(params);
31 newInOutShapes = std::get<8>(params);
32 canInfer = std::get<9>(params);
33 pad_end = std::get<10>(params);
34 isTransposed = std::get<11>(params);
36 type = "Deconvolution";
37 dataName = "deconvolution_data";
41 std::map<std::string, std::string> getMapParams() {
42 std::map<std::string, std::string> params = {
43 {"kernel", kernel.toSeparetedRow(",")},
44 {"strides", stride.toSeparetedRow(",")},
45 {"pads_begin", pad.toSeparetedRow(",")},
46 {"output", std::to_string(out_channels)},
47 {"group", std::to_string(group)},
48 {"dilations", dilation_factor.toSeparetedRow(",")}
50 if (!auto_pad.empty()) params["auto_pad"] = auto_pad;
51 if (!pad_end.empty()) params["pads_end"] = pad_end.toSeparetedRow(",");
56 std::string type = "Convolution";
57 std::string dataName = "convolution_data";
58 testing::InOutShapes inOutShapes;
59 testing::InOutShapes newInOutShapes;
64 param_size dilation_factor{};
66 unsigned out_channels{};
73 TEST_P(BuiltInShapeInferConvImplTest, impl) {
74 auto impl = getShapeInferImpl(type);
75 ASSERT_NE(nullptr, impl);
76 if (!group) group = 1;
77 unsigned w_dim = out_channels * inOutShapes.inDims[0][1] / group;
78 for (auto k : kernel.dims)
80 SizeVector weightsDim{w_dim};
81 blobs["weights"] = make_shared_blob(Precision::fromType<size_t>(), weightsDim);
82 ASSERT_NO_THROW(sts = impl->inferShapes(getBlobs(inOutShapes.inDims), getMapParams(), blobs, outShapes, &resp));
83 ASSERT_EQ(int(OK), sts) << resp.msg;
84 ASSERT_EQ(inOutShapes.outDims, outShapes);
87 TEST_P(BuiltInShapeInferConvImplTest, batch) {
88 auto layerParams = getMapParams();
89 auto cnnNetworkImplPtr = buildSingleLayerNetwork<4>(type, inOutShapes, &layerParams, dataName);
90 auto reshaper = std::make_shared<Reshaper>(*cnnNetworkImplPtr);
91 sts = cnnNetworkImplPtr->setBatchSizeReshape(BATCH, &resp);
92 ASSERT_EQ((int) OK, sts) << resp.msg;
93 inOutShapes.inDims[0][0] = inOutShapes.outDims[0][0] = BATCH;
94 checkNetworkInOut(*cnnNetworkImplPtr, inOutShapes);
97 TEST_P(BuiltInShapeInferConvImplTest, reshaper) {
98 auto layerParams = getMapParams();
99 auto cnnNetworkImplPtr = buildSingleLayerNetwork<4>(type, inOutShapes, &layerParams, dataName);
100 auto reshaper = std::make_shared<Reshaper>(*cnnNetworkImplPtr);
101 auto inputShapes = setInputShapes(*cnnNetworkImplPtr, newInOutShapes.inDims);
102 reshaper->run(inputShapes);
103 checkNetworkInOut(*cnnNetworkImplPtr, newInOutShapes);
106 INSTANTIATE_TEST_CASE_P(
107 BuiltInImplsConv, BuiltInShapeInferConvImplTest,
110 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
111 {{4, 64, 229, 115}}}), kernel({4, 2}), stride({2, 1}),
112 pad({2, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0}),
113 NewInOutShapes({{{1, 3, 228, 228}},
114 {{1, 64, 229, 115}}}),
115 CanInfer(true), pad_end(), IsTransposed(false)),
116 // fixate pad + dilation
117 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
118 {{4, 64, 225, 109}}}), kernel({4, 2}), stride({2, 1}),
119 pad({2, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({5, 5}),
120 NewInOutShapes({{{1, 3, 228, 228}},
121 {{1, 64, 225, 109}}}),
122 CanInfer(true), pad_end(), IsTransposed(false)),
123 // fixate pad + right/bottom
124 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
125 {{4, 64, 230, 115}}}), kernel({4, 2}), stride({2, 1}),
126 pad({2, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0}),
127 NewInOutShapes({{{1, 3, 228, 228}},
128 {{1, 64, 230, 115}}}),
129 CanInfer(true), pad_end({3, 2}), IsTransposed(false)),
130 // valid + empty paddings
131 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
132 {{4, 64, 227, 113}}}), kernel({4, 2}), stride({2, 1}),
133 pad({0, 0}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({0, 0}),
134 NewInOutShapes({{{1, 3, 228, 228}},
135 {{1, 64, 227, 113}}}),
136 CanInfer(true), pad_end(), IsTransposed(false)),
138 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
139 {{4, 64, 223, 107}}}), kernel({4, 2}), stride({2, 1}),
140 pad({0, 0}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({5, 5}),
141 NewInOutShapes({{{1, 3, 228, 228}},
142 {{1, 64, 223, 107}}}),
143 CanInfer(true), pad_end({0, 0}), IsTransposed(false)),
144 // valid + fixated paddings (shouldn't affect)
145 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
146 {{4, 64, 227, 113}}}), kernel({4, 2}), stride({2, 1}),
147 pad({2, 4}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({0, 0}),
148 NewInOutShapes({{{1, 3, 228, 228}},
149 {{1, 64, 227, 113}}}),
150 CanInfer(true), pad_end({3, 2}), IsTransposed(false)),
151 // same_upper + empty paddings
152 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
153 {{4, 64, 227, 114}}}), kernel({4, 2}), stride({2, 1}),
154 pad({0, 0}), auto_pad("same_upper"), out_channels(64), group(1),
155 dilation_factor({0, 0}),
156 NewInOutShapes({{{1, 3, 227, 227}},
157 {{1, 64, 227, 114}}}),
158 CanInfer(true), pad_end(), IsTransposed(false)),
159 // same_upper + dilation paddings
160 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
161 {{4, 64, 227, 114}}}), kernel({4, 2}), stride({2, 1}),
162 pad({0, 0}), auto_pad("same_upper"), out_channels(64), group(1),
163 dilation_factor({5, 5}),
164 NewInOutShapes({{{1, 3, 227, 227}},
165 {{1, 64, 227, 114}}}),
166 CanInfer(true), pad_end({0, 0}), IsTransposed(false)),
167 // same_upper + fixated paddings (shouldn't affect)
168 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
169 {{4, 64, 227, 114}}}), kernel({4, 2}), stride({2, 1}),
170 pad({2, 4}), auto_pad("same_upper"), out_channels(64), group(1),
171 dilation_factor({0, 0}),
172 NewInOutShapes({{{1, 3, 227, 227}},
173 {{1, 64, 227, 114}}}),
174 CanInfer(true), pad_end({0, 0}), IsTransposed(false)),
175 // same_lower + empty paddings
176 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
177 {{4, 64, 227, 113}}}), kernel({4, 2}), stride({2, 1}),
178 pad({0, 0}), auto_pad("same_lower"), out_channels(64), group(1),
179 dilation_factor({0, 0}),
180 NewInOutShapes({{{1, 3, 227, 227}},
181 {{1, 64, 227, 113}}}),
182 CanInfer(true), pad_end(), IsTransposed(false)),
183 // same_lower + dilation
184 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
185 {{4, 64, 227, 113}}}), kernel({4, 2}), stride({2, 1}),
186 pad({0, 0}), auto_pad("same_lower"), out_channels(64), group(1),
187 dilation_factor({0, 0}),
188 NewInOutShapes({{{1, 3, 227, 227}},
189 {{1, 64, 227, 113}}}),
190 CanInfer(true), pad_end({0, 0}), IsTransposed(false)),
191 // same_lower + fixated paddings (shouldn't affect)
192 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
193 {{4, 64, 227, 113}}}), kernel({4, 2}), stride({2, 1}),
194 pad({2, 4}), auto_pad("same_lower"), out_channels(64), group(1),
195 dilation_factor({0, 0}),
196 NewInOutShapes({{{1, 3, 227, 227}},
197 {{1, 64, 227, 113}}}),
198 CanInfer(true), pad_end({0, 0}), IsTransposed(false)),
201 ::testing::make_tuple(InOutShapes({{{4, 3, 64, 100, 120}},
202 {{4, 64, 66, 101, 61}}}), kernel({4, 2, 1}), stride({2, 1, 1}),
203 pad({2, 1, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0, 0}),
204 NewInOutShapes({{{1, 3, 64, 100, 120}},
205 {{1, 64, 66, 101, 61}}}),
206 CanInfer(true), pad_end(), IsTransposed(false)),
207 // fixate pad + right/bottom
208 ::testing::make_tuple(InOutShapes({{{4, 3, 16, 128, 128}},
209 {{4, 64, 18, 130, 65}}}), kernel({4, 2, 2}), stride({2, 1, 1}),
210 pad({2, 1, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0, 0}),
211 NewInOutShapes({{{1, 3, 16, 128, 128}},
212 {{1, 64, 18, 130, 65}}}),
213 CanInfer(true), pad_end({3, 2, 2}), IsTransposed(false)),
214 // valid + fixated paddings (shouldn't affect)
215 ::testing::make_tuple(InOutShapes({{{4, 3, 16, 128, 130}},
216 {{4, 64, 15, 127, 64}}}), kernel({4, 2, 2}), stride({2, 1, 1}),
217 pad({2, 4, 2}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({0, 0, 0}),
218 NewInOutShapes({{{1, 3, 16, 128, 130}},
219 {{1, 64, 15, 127, 64}}}),
220 CanInfer(true), pad_end({3, 2, 2}), IsTransposed(false)),
221 // same_lower + empty paddings
222 ::testing::make_tuple(InOutShapes({{{4, 3, 16, 128, 130}},
223 {{4, 64, 16, 128, 65}}}), kernel({4, 2, 1}), stride({2, 1, 1}),
224 pad({0, 0, 0}), auto_pad("same_lower"), out_channels(64), group(1),
225 dilation_factor({0, 0, 0}),
226 NewInOutShapes({{{1, 3, 16, 128, 130}},
227 {{1, 64, 16, 128, 65}}}),
228 CanInfer(true), pad_end(), IsTransposed(false))
232 INSTANTIATE_TEST_CASE_P(
233 BuiltInImplsDeConv, BuiltInShapeInferConvImplTest,
236 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
237 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
238 pad({2, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0}),
239 NewInOutShapes({{{1, 3, 228, 228}},
240 {{1, 64, 227, 454}}}),
241 CanInfer(true), pad_end(), IsTransposed(true)),
242 // fixate pad + dilation
243 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
244 {{4, 64, 231, 466}}}), kernel({4, 2}), stride({2, 1}),
245 pad({2, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({5, 5}),
246 NewInOutShapes({{{1, 3, 228, 228}},
247 {{1, 64, 231, 466}}}),
248 CanInfer(true), pad_end(), IsTransposed(true)),
249 // fixate pad + right/bottom
250 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
251 {{4, 64, 226, 453}}}), kernel({4, 2}), stride({2, 1}),
252 pad({2, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0}),
253 NewInOutShapes({{{1, 3, 228, 228}},
254 {{1, 64, 226, 453}}}),
255 CanInfer(true), pad_end({3, 2}), IsTransposed(true)),
256 // valid + empty paddings
257 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
258 {{4, 64, 229, 459}}}), kernel({4, 2}), stride({2, 1}),
259 pad({0, 0}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({0, 0}),
260 NewInOutShapes({{{1, 3, 228, 228}},
261 {{1, 64, 229, 459}}}),
262 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
264 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
265 {{4, 64, 233, 471}}}), kernel({4, 2}), stride({2, 1}),
266 pad({0, 0}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({5, 5}),
267 NewInOutShapes({{{1, 3, 228, 228}},
268 {{1, 64, 233, 471}}}),
269 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
270 // valid + fixated paddings (shouldn't affect)
271 ::testing::make_tuple(InOutShapes({{{4, 3, 228, 228}},
272 {{4, 64, 233, 471}}}), kernel({4, 2}), stride({2, 1}),
273 pad({2, 4}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({5, 5}),
274 NewInOutShapes({{{1, 3, 228, 228}},
275 {{1, 64, 233, 471}}}),
276 CanInfer(true), pad_end({3, 2}), IsTransposed(true)),
277 // same_upper + empty paddings
278 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
279 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
280 pad({0, 0}), auto_pad("same_upper"), out_channels(64), group(1),
281 dilation_factor({0, 0}),
282 NewInOutShapes({{{1, 3, 227, 227}},
283 {{1, 64, 227, 454}}}),
284 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
285 // same_upper + dilation paddings
286 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
287 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
288 pad({0, 0}), auto_pad("same_upper"), out_channels(64), group(1),
289 dilation_factor({5, 5}),
290 NewInOutShapes({{{1, 3, 227, 227}},
291 {{1, 64, 227, 454}}}),
292 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
293 // same_upper + fixated paddings (shouldn't affect)
294 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
295 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
296 pad({2, 4}), auto_pad("same_upper"), out_channels(64), group(1),
297 dilation_factor({0, 0}),
298 NewInOutShapes({{{1, 3, 227, 227}},
299 {{1, 64, 227, 454}}}),
300 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
301 // same_lower + empty paddings
302 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
303 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
304 pad({0, 0}), auto_pad("same_lower"), out_channels(64), group(1),
305 dilation_factor({0, 0}),
306 NewInOutShapes({{{1, 3, 227, 227}},
307 {{1, 64, 227, 454}}}),
308 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
309 // same_lower + dilation
310 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
311 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
312 pad({0, 0}), auto_pad("same_lower"), out_channels(64), group(1),
313 dilation_factor({0, 0}),
314 NewInOutShapes({{{1, 3, 227, 227}},
315 {{1, 64, 227, 454}}}),
316 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
317 // same_lower + fixated paddings (shouldn't affect)
318 ::testing::make_tuple(InOutShapes({{{4, 3, 227, 227}},
319 {{4, 64, 227, 454}}}), kernel({4, 2}), stride({2, 1}),
320 pad({2, 4}), auto_pad("same_lower"), out_channels(64), group(1),
321 dilation_factor({0, 0}),
322 NewInOutShapes({{{1, 3, 227, 227}},
323 {{1, 64, 227, 454}}}),
324 CanInfer(true), pad_end({0, 0}), IsTransposed(true)),
327 ::testing::make_tuple(InOutShapes({{{4, 3, 64, 100, 120}},
328 {{4, 64, 66, 101, 61}}}), kernel({4, 2, 1}), stride({2, 1, 1}),
329 pad({2, 1, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0 ,0}),
330 NewInOutShapes({{{1, 3, 64, 100, 120}},
331 {{1, 64, 66, 101, 61}}}),
332 CanInfer(true), pad_end(), IsTransposed(false)),
333 // fixate pad + right/bottom
334 ::testing::make_tuple(InOutShapes({{{4, 3, 16, 128, 130}},
335 {{4, 64, 14, 126, 257}}}), kernel({4, 2, 2}), stride({2, 1, 1}),
336 pad({2, 1, 1}), auto_pad(""), out_channels(64), group(1), dilation_factor({0, 0, 0}),
337 NewInOutShapes({{{1, 3, 16, 128, 130}},
338 {{1, 64, 14, 126, 257 }}}),
339 CanInfer(true), pad_end({3, 2, 2}), IsTransposed(true)),
340 // valid + fixated paddings (shouldn't affect)
341 ::testing::make_tuple(InOutShapes({{{4, 3, 16, 128, 130}},
342 {{4, 64, 15, 127, 64}}}), kernel({4, 2, 2}), stride({2, 1, 1}),
343 pad({2, 4, 2}), auto_pad("valid"), out_channels(64), group(1), dilation_factor({0, 0, 0}),
344 NewInOutShapes({{{1, 3, 16, 128, 130}},
345 {{1, 64, 15, 127, 64}}}),
346 CanInfer(true), pad_end({3, 2, 2}), IsTransposed(false)),
347 // same_lower + empty paddings
348 ::testing::make_tuple(InOutShapes({{{4, 3, 16, 128, 130}},
349 {{4, 64, 16, 128, 65}}}), kernel({4, 2, 1}), stride({2, 1, 1}),
350 pad({0, 0, 0}), auto_pad("same_lower"), out_channels(64), group(1),
351 dilation_factor({0, 0, 0}),
352 NewInOutShapes({{{1, 3, 16, 128, 130}},
353 {{1, 64, 16, 128, 65}}}),
354 CanInfer(true), pad_end(), IsTransposed(false))