ArmNN
 21.05
TransposeConv.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <boost/test/unit_test.hpp>
8 #include "../TfLiteParser.hpp"
9 
10 BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
11 
12 struct TransposeConvFixture : public ParserFlatbuffersFixture
13 {
14  explicit TransposeConvFixture(const std::string& inputShape,
15  const std::string& outputShape,
16  const std::string& filterShape,
17  const std::string& filterData,
18  const std::string& strideX,
19  const std::string& strideY,
20  const std::string& dataType)
21  {
22  m_JsonString = R"(
23  {
24  "version": 3,
25  "operator_codes": [ { "builtin_code": "TRANSPOSE_CONV" } ],
26  "subgraphs": [ {
27  "tensors": [
28  {
29  "shape": [ 4 ],
30  "type": "UINT8",
31  "buffer": 0,
32  "name": "outputShapeTensor",
33  "quantization": {
34  "min": [ 0.0 ],
35  "max": [ 255.0 ],
36  "scale": [ 1.0 ],
37  "zero_point": [ 0 ],
38  }
39  },
40  {
41  "shape": )" + filterShape + R"(,
42  "type": ")" + dataType + R"(",
43  "buffer": 1,
44  "name": "filterTensor",
45  "quantization": {
46  "min": [ 0.0 ],
47  "max": [ 255.0 ],
48  "scale": [ 1.0 ],
49  "zero_point": [ 0 ],
50  }
51  },
52  {
53  "shape": )" + inputShape + R"(,
54  "type": ")" + dataType + R"(",
55  "buffer": 2,
56  "name": "inputTensor",
57  "quantization": {
58  "min": [ 0.0 ],
59  "max": [ 255.0 ],
60  "scale": [ 1.0 ],
61  "zero_point": [ 0 ],
62  }
63  },
64  {
65  "shape": )" + outputShape + R"(,
66  "type": ")" + dataType + R"(",
67  "buffer": 3,
68  "name": "outputTensor",
69  "quantization": {
70  "min": [ 0.0 ],
71  "max": [ 255.0 ],
72  "scale": [ 1.0 ],
73  "zero_point": [ 0 ],
74  }
75  }
76  ],
77  "inputs": [ 2 ],
78  "outputs": [ 3 ],
79  "operators": [
80  {
81  "opcode_index": 0,
82  "inputs": [ 0, 1, 2 ],
83  "outputs": [ 3 ],
84  "builtin_options_type": "TransposeConvOptions",
85  "builtin_options": {
86  "padding": "VALID",
87  "stride_w": )" + strideX + R"(,
88  "stride_h": )" + strideY + R"(
89  },
90  "custom_options_format": "FLEXBUFFERS"
91  }
92  ],
93  } ],
94  "buffers" : [
95  { "data": )" + outputShape + R"( },
96  { "data": )" + filterData + R"( },
97  { },
98  { }
99  ]
100  }
101  )";
102  SetupSingleInputSingleOutput("inputTensor", "outputTensor");
103  }
104 };
105 
106 struct SimpleTransposeConvFixture : TransposeConvFixture
107 {
108  SimpleTransposeConvFixture()
109  : TransposeConvFixture("[ 1, 2, 2, 1 ]", // inputShape
110  "[ 1, 3, 3, 1 ]", // outputShape
111  "[ 1, 2, 2, 1 ]", // filterShape
112  "[ 0, 1, 2, 4 ]", // filterData
113  "1", // strideX
114  "1", // strideY
115  "UINT8") // dataType
116  {}
117 };
118 
119 BOOST_FIXTURE_TEST_CASE( ParseSimpleTransposeConv, SimpleTransposeConvFixture )
120 {
121  RunTest<4, armnn::DataType::QAsymmU8>(
122  0,
123  {
124  1, 2,
125  3, 4
126  },
127  {
128  0, 1, 2,
129  2, 11, 12,
130  6, 20, 16
131  });
132 }
133 
134 struct TransposeConvFixtureWithBias : public ParserFlatbuffersFixture
135 {
136  explicit TransposeConvFixtureWithBias(const std::string& inputShape,
137  const std::string& outputShape,
138  const std::string& filterShape,
139  const std::string& filterData,
140  const std::string& strideX,
141  const std::string& strideY,
142  const std::string& dataType,
143  const std::string& biasShape,
144  const std::string& biasData)
145  {
146  m_JsonString = R"(
147  {
148  "version": 3,
149  "operator_codes": [ { "builtin_code": "TRANSPOSE_CONV" } ],
150  "subgraphs": [ {
151  "tensors": [
152  {
153  "shape": [ 4 ],
154  "type": "UINT8",
155  "buffer": 0,
156  "name": "outputShapeTensor",
157  "quantization": {
158  "min": [ 0.0 ],
159  "max": [ 255.0 ],
160  "scale": [ 1.0 ],
161  "zero_point": [ 0 ],
162  }
163  },
164  {
165  "shape": )" + filterShape + R"(,
166  "type": ")" + dataType + R"(",
167  "buffer": 1,
168  "name": "filterTensor",
169  "quantization": {
170  "min": [ 0.0 ],
171  "max": [ 255.0 ],
172  "scale": [ 1.0 ],
173  "zero_point": [ 0 ],
174  }
175  },
176  {
177  "shape": )" + inputShape + R"(,
178  "type": ")" + dataType + R"(",
179  "buffer": 2,
180  "name": "inputTensor",
181  "quantization": {
182  "min": [ 0.0 ],
183  "max": [ 255.0 ],
184  "scale": [ 1.0 ],
185  "zero_point": [ 0 ],
186  }
187  },
188  {
189  "shape": )" + biasShape + R"( ,
190  "type": "INT32",
191  "buffer": 3,
192  "name": "biasTensor",
193  "quantization": {
194  "min": [ 0.0 ],
195  "max": [ 255.0 ],
196  "scale": [ 1.0 ],
197  "zero_point": [ 0 ],
198  }
199  },
200  {
201  "shape": )" + outputShape + R"(,
202  "type": ")" + dataType + R"(",
203  "buffer": 4,
204  "name": "outputTensor",
205  "quantization": {
206  "min": [ 0.0 ],
207  "max": [ 255.0 ],
208  "scale": [ 1.0 ],
209  "zero_point": [ 0 ],
210  }
211  }
212  ],
213  "inputs": [ 2 ],
214  "outputs": [ 4 ],
215  "operators": [
216  {
217  "opcode_index": 0,
218  "inputs": [ 0, 1, 2, 3],
219  "outputs": [ 4 ],
220  "builtin_options_type": "TransposeConvOptions",
221  "builtin_options": {
222  "padding": "VALID",
223  "stride_w": )" + strideX + R"(,
224  "stride_h": )" + strideY + R"(
225  },
226  "custom_options_format": "FLEXBUFFERS"
227  }
228  ],
229  } ],
230  "buffers" : [
231  { "data": )" + outputShape + R"( },
232  { "data": )" + filterData + R"( },
233  { },
234  { "data": )" + biasData + R"( },
235  { }
236  ]
237  }
238  )";
239  SetupSingleInputSingleOutput("inputTensor", "outputTensor");
240  }
241 };
242 
243 struct SimpleTransposeConvFixtureWithBias : TransposeConvFixtureWithBias
244 {
245  SimpleTransposeConvFixtureWithBias()
246  : TransposeConvFixtureWithBias("[ 1, 2, 2, 1 ]", // inputShape
247  "[ 1, 3, 3, 1 ]", // outputShape
248  "[ 1, 2, 2, 1 ]", // filterShape
249  "[ 0, 1, 2, 4 ]", // filterData
250  "1", // strideX
251  "1", // strideY
252  "UINT8", // dataType
253  "[ 1 ]", // bias shape
254  "[ 10, 0, 0, 0 ]") // bias data
255  {}
256 };
257 
258 BOOST_FIXTURE_TEST_CASE( ParseSimpleTransposeConvWithBias, SimpleTransposeConvFixtureWithBias )
259 {
260  RunTest<4, armnn::DataType::QAsymmU8>(
261  0,
262  {
263  1, 2,
264  3, 4
265  },
266  {
267  10, 11, 12,
268  12, 21, 22,
269  16, 30, 26
270  });
271 }
272 
273 
274 struct TransposeConvPerChannelFixture : public ParserFlatbuffersFixture
275 {
276  explicit TransposeConvPerChannelFixture()
277  {
278  m_JsonString = R"(
279  {
280  "version": 3,
281  "operator_codes": [
282  {
283  "builtin_code": "TRANSPOSE_CONV",
284  "version": 2
285  }
286  ],
287  "subgraphs": [
288  {
289  "tensors": [
290  {
291  "shape": [
292  1,
293  4,
294  4,
295  2
296  ],
297  "type": "INT8",
298  "buffer": 1,
299  "name": "input",
300  "quantization": {
301  "min": [
302  -50.0
303  ],
304  "max": [
305  49.0
306  ],
307  "scale": [
308  0.388235
309  ],
310  "zero_point": [
311  1
312  ],
313  "details_type": "NONE",
314  "quantized_dimension": 0
315  },
316  "is_variable": false
317  },
318  {
319  "shape": [
320  4
321  ],
322  "type": "INT32",
323  "buffer": 2,
324  "name": "model/conv2d_transpose/stack",
325  "quantization": {
326  "details_type": "NONE",
327  "quantized_dimension": 0
328  },
329  "is_variable": false
330  },
331  {
332  "shape": [
333  8,
334  2,
335  2,
336  2
337  ],
338  "type": "INT8",
339  "buffer": 3,
340  "name": "model/conv2d_transpose/conv2d_transpose",
341  "quantization": {
342  "min": [
343  -0.081948,
344  -0.379918,
345  -0.223632,
346  -0.098629,
347  -0.386369,
348  -0.351057,
349  -0.348749,
350  -0.264848
351  ],
352  "max": [
353  0.35091,
354  0.229681,
355  0.368384,
356  0.176761,
357  0.353717,
358  0.377565,
359  0.373713,
360  0.30141
361  ],
362  "scale": [
363  0.002763,
364  0.002991,
365  0.002901,
366  0.001392,
367  0.003042,
368  0.002973,
369  0.002943,
370  0.002373
371  ],
372  "zero_point": [
373  0,
374  0,
375  0,
376  0,
377  0,
378  0,
379  0,
380  0
381  ],
382  "details_type": "NONE",
383  "quantized_dimension": 0
384  },
385  "is_variable": false
386  },
387  {
388  "shape": [
389  1,
390  4,
391  4,
392  8
393  ],
394  "type": "INT8",
395  "buffer": 4,
396  "name": "Identity",
397  "quantization": {
398  "min": [
399  -63.578175
400  ],
401  "max": [
402  69.305023
403  ],
404  "scale": [
405  0.521111
406  ],
407  "zero_point": [
408  -6
409  ],
410  "details_type": "NONE",
411  "quantized_dimension": 0
412  },
413  "is_variable": false
414  }
415  ],
416  "inputs": [
417  0
418  ],
419  "outputs": [
420  3
421  ],
422  "operators": [
423  {
424  "opcode_index": 0,
425  "inputs": [
426  1,
427  2,
428  0
429  ],
430  "outputs": [
431  3
432  ],
433  "builtin_options_type": "TransposeConvOptions",
434  "builtin_options": {
435  "padding": "SAME",
436  "stride_w": 1,
437  "stride_h": 1
438  },
439  "custom_options_format": "FLEXBUFFERS"
440  }
441  ],
442  "name": "main"
443  }
444  ],
445  "description": "MLIR Converted.",
446  "buffers": [
447  {
448  },
449  {
450  },
451  {
452  "data": [
453  1,
454  0,
455  0,
456  0,
457  4,
458  0,
459  0,
460  0,
461  4,
462  0,
463  0,
464  0,
465  8,
466  0,
467  0,
468  0
469  ]
470  },
471  {
472  "data": [
473  13,
474  239,
475  7,
476  125,
477  35,
478  127,
479  55,
480  226,
481  77,
482  150,
483  159,
484  192,
485  180,
486  129,
487  51,
488  48,
489  108,
490  9,
491  21,
492  179,
493  12,
494  39,
495  127,
496  107,
497  44,
498  206,
499  127,
500  185,
501  108,
502  82,
503  86,
504  218,
505  38,
506  149,
507  16,
508  1,
509  129,
510  163,
511  116,
512  136,
513  138,
514  43,
515  65,
516  186,
517  154,
518  138,
519  64,
520  127,
521  120,
522  127,
523  207,
524  70,
525  43,
526  33,
527  141,
528  137,
529  93,
530  215,
531  65,
532  92,
533  122,
534  144,
535  120,
536  127
537  ]
538  },
539  {
540  },
541  {
542  "data": [
543  49,
544  46,
545  57,
546  46,
547  48,
548  0,
549  0,
550  0,
551  0,
552  0,
553  0,
554  0,
555  0,
556  0,
557  0,
558  0
559  ]
560  }
561  ],
562  "metadata": [
563  {
564  "name": "min_runtime_version",
565  "buffer": 5
566  }
567  ]
568  }
569  )";
570  SetupSingleInputSingleOutput("input", "Identity");
571  }
572 };
573 
574 BOOST_FIXTURE_TEST_CASE( ParseTransposeConvPerChannel, TransposeConvPerChannelFixture )
575 {
576  RunTest<4, armnn::DataType::QAsymmS8>(
577  0,
578  {
579  -11, 40,-26, 11,-28, 8, 0, -8,
580  -10, 34, 47, 0,-33,-14, 28, 35,
581  6,-28,-26, 8, 13, 33,-31,-41,
582  31,-20,-31,-16, 8,-18,-44, 0
583  },
584  {
585  -8,-17, -8, -9,-16, 1, 2,-11,
586  3,-16,-19,-12,-11, -6, -3, -6,
587  -5, -8,-16,-12,-11, -3, -7,-13,
588  -4, 1, -9,-10, -5,-12, -5, -8,
589  2,-25, -5, -6,-20, -7, 2,-21,
590  1, 4, 5,-13,-10,-12, 3, 4,
591  -10,-17,-17, -6, -7, 12,-22,-17,
592  -17, 0, -5,-14,-21,-12, 17,-13,
593  3, -6, -3, -3, -2,-16,-11,-12,
594  -15,-14, -1, -2,-35, 5,-18, 0,
595  -6, 8, 5,-12, 12, 7, -6, -3,
596  11,-28,-28, -3,-18,-29, -5,-13,
597  -12, 11, -2, -5, 6, -9, -6, 7,
598  -9,-11,-14, -2, 12, 5,-21,-23,
599  -4, -4, -6, -6,-21,-25, 0,-18,
600  -26, 10, -7,-13, 3, 39,-39, -4
601  });
602 }
603 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_AUTO_TEST_SUITE_END()
void SetupSingleInputSingleOutput(const std::string &inputName, const std::string &outputName)
BOOST_FIXTURE_TEST_CASE(ParseSimpleTransposeConv, SimpleTransposeConvFixture)