ArmNN
 20.05
ConcatEndToEndTestImpl.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "CommonTestUtils.hpp"
8 
9 #include <ResolveType.hpp>
10 
11 #include <armnn/INetwork.hpp>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 #include <vector>
16 
17 namespace
18 {
19 
20 template<typename armnn::DataType DataType>
21 INetworkPtr CreateConcatNetwork(const std::vector<TensorShape>& inputShapes,
22  const TensorShape &outputShape,
23  unsigned int concatAxis,
24  const float qScale = 1.0f,
25  const int32_t qOffset = 0)
26 {
27  using namespace armnn;
28  // Builds up the structure of the network.
30 
31  OriginsDescriptor descriptor;
32 
33  descriptor = CreateDescriptorForConcatenation(inputShapes.begin(),
34  inputShapes.end(),
35  concatAxis);
36  IConnectableLayer* concat = net->AddConcatLayer(descriptor, "concat");
37 
38  for (unsigned int i = 0; i < inputShapes.size(); ++i)
39  {
40  TensorInfo inputTensorInfo(inputShapes[i], DataType, qScale, qOffset);
41  IConnectableLayer* input = net->AddInputLayer(boost::numeric_cast<LayerBindingId>(i));
42  Connect(input, concat, inputTensorInfo, 0, i);
43  }
44 
45  TensorInfo outputTensorInfo(outputShape, DataType, qScale, qOffset);
46  IConnectableLayer* output = net->AddOutputLayer(0, "output");
47  Connect(concat, output, outputTensorInfo, 0, 0);
48 
49  return net;
50 }
51 
52 template<armnn::DataType ArmnnType>
53 void ConcatDim0EndToEnd(const std::vector<BackendId>& backends)
54 {
55  using namespace armnn;
56  using T = ResolveType<ArmnnType>;
57 
58  unsigned int concatAxis = 0;
59  const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }};
60  const TensorShape& outputShape = { 4, 3, 2, 2 };
61 
62  // Builds up the structure of the network
63  INetworkPtr net = CreateConcatNetwork<ArmnnType>(inputShapes, outputShape, concatAxis);
64 
65  BOOST_TEST_CHECKPOINT("create a network");
66 
67  // Creates structures for input & output.
68  std::vector<T> inputData{
69  1, 2,
70  3, 4,
71  5, 6,
72  7, 8,
73  9, 10,
74  11, 12,
75  1, 2,
76  3, 4,
77  5, 6,
78  7, 8,
79  9, 10,
80  11, 12
81  };
82 
83  std::vector<T> expectedOutput{
84  1, 2,
85  3, 4,
86  5, 6,
87  7, 8,
88  9, 10,
89  11, 12,
90  1, 2,
91  3, 4,
92  5, 6,
93  7, 8,
94  9, 10,
95  11, 12,
96  1, 2,
97  3, 4,
98  5, 6,
99  7, 8,
100  9, 10,
101  11, 12,
102  1, 2,
103  3, 4,
104  5, 6,
105  7, 8,
106  9, 10,
107  11, 12
108  };
109 
110  std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }};
111  std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }};
112 
113  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net), inputTensorData, expectedOutputData, backends);
114 }
115 
116 template<armnn::DataType ArmnnType>
117 void ConcatDim1EndToEnd(const std::vector<BackendId>& backends)
118 {
119  using namespace armnn;
120  using T = ResolveType<ArmnnType>;
121 
122  unsigned int concatAxis = 1;
123  const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }};
124  const TensorShape& outputShape = { 2, 6, 2, 2 };
125 
126  // Builds up the structure of the network
127  INetworkPtr net = CreateConcatNetwork<ArmnnType>(inputShapes, outputShape, concatAxis);
128 
129  BOOST_TEST_CHECKPOINT("create a network");
130 
131  // Creates structures for input & output.
132  std::vector<T> inputData{
133  1, 2,
134  3, 4,
135  5, 6,
136  7, 8,
137  9, 10,
138  11, 12,
139  1, 2,
140  3, 4,
141  5, 6,
142  7, 8,
143  9, 10,
144  11, 12
145  };
146 
147  std::vector<T> expectedOutput{
148  1, 2,
149  3, 4,
150  5, 6,
151  7, 8,
152  9, 10,
153  11, 12,
154  1, 2,
155  3, 4,
156  5, 6,
157  7, 8,
158  9, 10,
159  11, 12,
160  1, 2,
161  3, 4,
162  5, 6,
163  7, 8,
164  9, 10,
165  11, 12,
166  1, 2,
167  3, 4,
168  5, 6,
169  7, 8,
170  9, 10,
171  11, 12
172  };
173 
174  std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }};
175  std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }};
176 
177  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net), inputTensorData, expectedOutputData, backends);
178 }
179 
180 template<armnn::DataType ArmnnType>
181 void ConcatDim2EndToEnd(const std::vector<BackendId>& backends)
182 {
183  using namespace armnn;
184  using T = ResolveType<ArmnnType>;
185 
186  unsigned int concatAxis = 2;
187  const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }};
188  const TensorShape& outputShape = { 2, 3, 4, 2 };
189 
190  // Builds up the structure of the network
191  INetworkPtr net = CreateConcatNetwork<ArmnnType>(inputShapes, outputShape, concatAxis);
192 
193  BOOST_TEST_CHECKPOINT("create a network");
194 
195  // Creates structures for input & output.
196  std::vector<T> inputData{
197  1, 2,
198  3, 4,
199  5, 6,
200  7, 8,
201  9, 10,
202  11, 12,
203  1, 2,
204  3, 4,
205  5, 6,
206  7, 8,
207  9, 10,
208  11, 12
209  };
210 
211  std::vector<T> expectedOutput{
212  1, 2,
213  3, 4,
214  1, 2,
215  3, 4,
216  5, 6,
217  7, 8,
218  5, 6,
219  7, 8,
220  9, 10,
221  11, 12,
222  9, 10,
223  11, 12,
224  1, 2,
225  3, 4,
226  1, 2,
227  3, 4,
228  5, 6,
229  7, 8,
230  5, 6,
231  7, 8,
232  9, 10,
233  11, 12,
234  9, 10,
235  11, 12
236  };
237 
238  std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }};
239  std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }};
240 
241  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net), inputTensorData, expectedOutputData, backends);
242 }
243 
244 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
245 void ConcatDim3EndToEnd(const std::vector<BackendId>& backends)
246 {
247  using namespace armnn;
248 
249  unsigned int concatAxis = 3;
250  const std::vector<TensorShape> inputShapes{{ 2, 3, 2, 2 }, { 2, 3, 2, 2 }};
251  const TensorShape& outputShape = { 2, 3, 2, 4 };
252 
253  // Builds up the structure of the network
254  INetworkPtr net = CreateConcatNetwork<ArmnnType>(inputShapes, outputShape, concatAxis);
255 
256  BOOST_TEST_CHECKPOINT("create a network");
257 
258  // Creates structures for input & output.
259  std::vector<T> inputData{
260  1, 2,
261  3, 4,
262  5, 6,
263  7, 8,
264  9, 10,
265  11, 12,
266  1, 2,
267  3, 4,
268  5, 6,
269  7, 8,
270  9, 10,
271  11, 12
272  };
273 
274  std::vector<T> expectedOutput{
275  1, 2,
276  1, 2,
277  3, 4,
278  3, 4,
279  5, 6,
280  5, 6,
281  7, 8,
282  7, 8,
283  9, 10,
284  9, 10,
285  11, 12,
286  11, 12,
287  1, 2,
288  1, 2,
289  3, 4,
290  3, 4,
291  5, 6,
292  5, 6,
293  7, 8,
294  7, 8,
295  9, 10,
296  9, 10,
297  11, 12,
298  11, 12
299  };
300 
301  std::map<int, std::vector<T>> inputTensorData = {{ 0,inputData }, { 1,inputData }};
302  std::map<int, std::vector<T>> expectedOutputData = {{ 0,expectedOutput }};
303 
304  EndToEndLayerTestImpl<ArmnnType, ArmnnType>(move(net), inputTensorData, expectedOutputData, backends);
305 }
306 
307 } // anonymous namespace
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:73
Copyright (c) 2020 ARM Limited.
DataType
Definition: Types.hpp:32
An OriginsDescriptor for the ConcatLayer.
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:12
OriginsDescriptor CreateDescriptorForConcatenation(TensorShapeIt first, TensorShapeIt last, unsigned int concatenationDimension)
Convenience template to create an OriginsDescriptor to use when creating a ConcatLayer for performing...
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:101
static INetworkPtr Create()
Definition: Network.cpp:50