blob: 28bcfa1e77e94e77fc03568883c98dd9942d48c2 [file] [log] [blame]
Moritz Pflanzeree493ae2017-07-05 10:52:21 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_TEST_MODEL_OBJECTS_ALEXNET_H__
25#define __ARM_COMPUTE_TEST_MODEL_OBJECTS_ALEXNET_H__
26
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010027#include "AssetsLibrary.h"
Moritz Pflanzeree493ae2017-07-05 10:52:21 +010028#include "Globals.h"
Moritz Pflanzeree493ae2017-07-05 10:52:21 +010029#include "Utils.h"
30
31#include <memory>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace networks
38{
39/** AlexNet model object */
40template <typename ITensorType,
41 typename TensorType,
42 typename SubTensorType,
43 typename Accessor,
44 typename ActivationLayerFunction,
45 typename ConvolutionLayerFunction,
steniu01a629da12017-07-28 14:40:58 +010046 typename DirectConvolutionLayerFunction,
Moritz Pflanzeree493ae2017-07-05 10:52:21 +010047 typename FullyConnectedLayerFunction,
48 typename NormalizationLayerFunction,
49 typename PoolingLayerFunction,
50 typename SoftmaxLayerFunction>
51class AlexNetNetwork
52{
53public:
54 void init(DataType data_type, int fixed_point_position, int batches, bool reshaped_weights = false)
55 {
56 _data_type = data_type;
57 _fixed_point_position = fixed_point_position;
58 _batches = batches;
59 _reshaped_weights = reshaped_weights;
60
61 // Initialize weights and biases
62 if(!_reshaped_weights)
63 {
steniu01a629da12017-07-28 14:40:58 +010064 w[0].allocator()->init(TensorInfo(TensorShape(11U, 11U, 3U, 96U), 1, _data_type, _fixed_point_position));
65 b[0].allocator()->init(TensorInfo(TensorShape(96U), 1, _data_type, _fixed_point_position));
66 w[1].allocator()->init(TensorInfo(TensorShape(5U, 5U, 48U, 256U), 1, _data_type, _fixed_point_position));
67 b[1].allocator()->init(TensorInfo(TensorShape(256U), 1, _data_type, _fixed_point_position));
68 w[2].allocator()->init(TensorInfo(TensorShape(3U, 3U, 256U, 384U), 1, _data_type, _fixed_point_position));
69 b[2].allocator()->init(TensorInfo(TensorShape(384U), 1, _data_type, _fixed_point_position));
70 w[3].allocator()->init(TensorInfo(TensorShape(3U, 3U, 192U, 384U), 1, _data_type, _fixed_point_position));
71 b[3].allocator()->init(TensorInfo(TensorShape(384U), 1, _data_type, _fixed_point_position));
72 w[4].allocator()->init(TensorInfo(TensorShape(3U, 3U, 192U, 256U), 1, _data_type, _fixed_point_position));
73 b[4].allocator()->init(TensorInfo(TensorShape(256U), 1, _data_type, _fixed_point_position));
74 w[5].allocator()->init(TensorInfo(TensorShape(9216U, 4096U), 1, _data_type, _fixed_point_position));
75 b[5].allocator()->init(TensorInfo(TensorShape(4096U), 1, _data_type, _fixed_point_position));
76 w[6].allocator()->init(TensorInfo(TensorShape(4096U, 4096U), 1, _data_type, _fixed_point_position));
77 b[6].allocator()->init(TensorInfo(TensorShape(4096U), 1, _data_type, _fixed_point_position));
78 w[7].allocator()->init(TensorInfo(TensorShape(4096U, 1000U), 1, _data_type, _fixed_point_position));
79 b[7].allocator()->init(TensorInfo(TensorShape(1000U), 1, _data_type, _fixed_point_position));
80
81 w21 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[1], TensorShape(5U, 5U, 48U, 128U), Coordinates()));
82 w22 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[1], TensorShape(5U, 5U, 48U, 128U), Coordinates(0, 0, 0, 128)));
83 b21 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[1], TensorShape(128U), Coordinates()));
84 b22 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[1], TensorShape(128U), Coordinates(128)));
85
86 w41 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[3], TensorShape(3U, 3U, 192U, 192U), Coordinates()));
87 w42 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[3], TensorShape(3U, 3U, 192U, 192U), Coordinates(0, 0, 0, 192)));
88 b41 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[3], TensorShape(192U), Coordinates()));
89 b42 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[3], TensorShape(192U), Coordinates(192)));
90
91 w51 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[4], TensorShape(3U, 3U, 192U, 128U), Coordinates()));
92 w52 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[4], TensorShape(3U, 3U, 192U, 128U), Coordinates(0, 0, 0, 128)));
93 b51 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[4], TensorShape(128U), Coordinates()));
94 b52 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[4], TensorShape(128U), Coordinates(128)));
Moritz Pflanzeree493ae2017-07-05 10:52:21 +010095 }
96 else
97 {
steniu01a629da12017-07-28 14:40:58 +010098 const unsigned int data_type_size = 16 / arm_compute::data_size_from_type(_data_type);
99
100 // Create tensor for the reshaped weights
101 auto w21_tensor = std::unique_ptr<TensorType>(new TensorType());
102 auto w22_tensor = std::unique_ptr<TensorType>(new TensorType());
103
104 w[0].allocator()->init(TensorInfo(TensorShape(366U * data_type_size, 96U / data_type_size), 1, _data_type, _fixed_point_position));
105 w21_tensor->allocator()->init(TensorInfo(TensorShape(1248U * data_type_size, 128U / data_type_size), 1, _data_type, _fixed_point_position));
106 w22_tensor->allocator()->init(TensorInfo(TensorShape(1248U * data_type_size, 128U / data_type_size), 1, _data_type, _fixed_point_position));
107 w21 = std::move(w21_tensor);
108 w22 = std::move(w22_tensor);
109
110 // Configure the direct convolution's weights. Direct convolution doesn't need reshape weights
111 if(!_is_direct_conv)
112 {
113 auto w41_tensor = std::unique_ptr<TensorType>(new TensorType());
114 auto w42_tensor = std::unique_ptr<TensorType>(new TensorType());
115 auto w51_tensor = std::unique_ptr<TensorType>(new TensorType());
116 auto w52_tensor = std::unique_ptr<TensorType>(new TensorType());
117 w41_tensor->allocator()->init(TensorInfo(TensorShape(1920U * data_type_size, 192U / data_type_size), 1, _data_type, _fixed_point_position));
118 w42_tensor->allocator()->init(TensorInfo(TensorShape(1920U * data_type_size, 192U / data_type_size), 1, _data_type, _fixed_point_position));
119 w51_tensor->allocator()->init(TensorInfo(TensorShape(1920U * data_type_size, 128U / data_type_size), 1, _data_type, _fixed_point_position));
120 w52_tensor->allocator()->init(TensorInfo(TensorShape(1920U * data_type_size, 128U / data_type_size), 1, _data_type, _fixed_point_position));
121 w[2].allocator()->init(TensorInfo(TensorShape(2560U * data_type_size, 384U / data_type_size), 1, _data_type, _fixed_point_position));
122 w41 = std::move(w41_tensor);
123 w42 = std::move(w42_tensor);
124 w51 = std::move(w51_tensor);
125 w52 = std::move(w52_tensor);
126 }
127 else
128 {
129 w[2].allocator()->init(TensorInfo(TensorShape(3U, 3U, 256U, 384U), 1, _data_type, _fixed_point_position));
130 b[2].allocator()->init(TensorInfo(TensorShape(384U), 1, _data_type, _fixed_point_position));
131 w[3].allocator()->init(TensorInfo(TensorShape(3U, 3U, 192U, 384U), 1, _data_type, _fixed_point_position));
132 b[3].allocator()->init(TensorInfo(TensorShape(384U), 1, _data_type, _fixed_point_position));
133 w[4].allocator()->init(TensorInfo(TensorShape(3U, 3U, 192U, 256U), 1, _data_type, _fixed_point_position));
134 b[4].allocator()->init(TensorInfo(TensorShape(256U), 1, _data_type, _fixed_point_position));
135 w41 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[3], TensorShape(3U, 3U, 192U, 192U), Coordinates()));
136 w42 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[3], TensorShape(3U, 3U, 192U, 192U), Coordinates(0, 0, 0, 192)));
137 b41 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[3], TensorShape(192U), Coordinates()));
138 b42 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[3], TensorShape(192U), Coordinates(192)));
139
140 w51 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[4], TensorShape(3U, 3U, 192U, 128U), Coordinates()));
141 w52 = std::unique_ptr<SubTensorType>(new SubTensorType(&w[4], TensorShape(3U, 3U, 192U, 128U), Coordinates(0, 0, 0, 128)));
142 b51 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[4], TensorShape(128U), Coordinates()));
143 b52 = std::unique_ptr<SubTensorType>(new SubTensorType(&b[4], TensorShape(128U), Coordinates(128)));
144 }
145
146 b[5].allocator()->init(TensorInfo(TensorShape(4096U), 1, _data_type, _fixed_point_position));
147 b[6].allocator()->init(TensorInfo(TensorShape(4096U), 1, _data_type, _fixed_point_position));
148 b[7].allocator()->init(TensorInfo(TensorShape(1000U), 1, _data_type, _fixed_point_position));
149
150 if(_batches > 1)
151 {
152 w[5].allocator()->init(TensorInfo(TensorShape(9216U * data_type_size, 4096U / data_type_size), 1, _data_type, _fixed_point_position));
153 w[6].allocator()->init(TensorInfo(TensorShape(4096U * data_type_size, 4096U / data_type_size), 1, _data_type, _fixed_point_position));
154 w[7].allocator()->init(TensorInfo(TensorShape(4096U * data_type_size, 1000U / data_type_size), 1, _data_type, _fixed_point_position));
155 }
156 else
157 {
158 w[5].allocator()->init(TensorInfo(TensorShape(4096U, 9216U), 1, _data_type, _fixed_point_position));
159 w[6].allocator()->init(TensorInfo(TensorShape(4096U, 4096U), 1, _data_type, _fixed_point_position));
160 w[7].allocator()->init(TensorInfo(TensorShape(1000U, 4096U), 1, _data_type, _fixed_point_position));
161 }
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100162 }
163 }
164
165 void build()
166 {
167 input.allocator()->init(TensorInfo(TensorShape(227U, 227U, 3U, _batches), 1, _data_type, _fixed_point_position));
168 output.allocator()->init(TensorInfo(TensorShape(1000U, _batches), 1, _data_type, _fixed_point_position));
169
170 // Initialize intermediate tensors
171 // Layer 1
172 conv1_out.allocator()->init(TensorInfo(TensorShape(55U, 55U, 96U, _batches), 1, _data_type, _fixed_point_position));
173 act1_out.allocator()->init(TensorInfo(TensorShape(55U, 55U, 96U, _batches), 1, _data_type, _fixed_point_position));
174 norm1_out.allocator()->init(TensorInfo(TensorShape(55U, 55U, 96U, _batches), 1, _data_type, _fixed_point_position));
175 pool1_out.allocator()->init(TensorInfo(TensorShape(27U, 27U, 96U, _batches), 1, _data_type, _fixed_point_position));
176 pool11_out = std::unique_ptr<SubTensorType>(new SubTensorType(&pool1_out, TensorShape(27U, 27U, 48U, _batches), Coordinates()));
177 pool12_out = std::unique_ptr<SubTensorType>(new SubTensorType(&pool1_out, TensorShape(27U, 27U, 48U, _batches), Coordinates(0, 0, 48)));
178 // Layer 2
179 conv2_out.allocator()->init(TensorInfo(TensorShape(27U, 27U, 256U, _batches), 1, _data_type, _fixed_point_position));
180 conv21_out = std::unique_ptr<SubTensorType>(new SubTensorType(&conv2_out, TensorShape(27U, 27U, 128U, _batches), Coordinates()));
181 conv22_out = std::unique_ptr<SubTensorType>(new SubTensorType(&conv2_out, TensorShape(27U, 27U, 128U, _batches), Coordinates(0, 0, 128)));
182 act2_out.allocator()->init(TensorInfo(TensorShape(27U, 27U, 256U, _batches), 1, _data_type, _fixed_point_position));
183 norm2_out.allocator()->init(TensorInfo(TensorShape(27U, 27U, 256U, _batches), 1, _data_type, _fixed_point_position));
184 pool2_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 256U, _batches), 1, _data_type, _fixed_point_position));
185 // Layer 3
186 conv3_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 384U, _batches), 1, _data_type, _fixed_point_position));
187 act3_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 384U, _batches), 1, _data_type, _fixed_point_position));
188 act31_out = std::unique_ptr<SubTensorType>(new SubTensorType(&act3_out, TensorShape(13U, 13U, 192U, _batches), Coordinates()));
189 act32_out = std::unique_ptr<SubTensorType>(new SubTensorType(&act3_out, TensorShape(13U, 13U, 192U, _batches), Coordinates(0, 0, 192)));
190 // Layer 4
191 conv4_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 384U, _batches), 1, _data_type, _fixed_point_position));
192 conv41_out = std::unique_ptr<SubTensorType>(new SubTensorType(&conv4_out, TensorShape(13U, 13U, 192U, _batches), Coordinates()));
193 conv42_out = std::unique_ptr<SubTensorType>(new SubTensorType(&conv4_out, TensorShape(13U, 13U, 192U, _batches), Coordinates(0, 0, 192)));
194 act4_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 384U, _batches), 1, _data_type, _fixed_point_position));
195 act41_out = std::unique_ptr<SubTensorType>(new SubTensorType(&act4_out, TensorShape(13U, 13U, 192U, _batches), Coordinates()));
196 act42_out = std::unique_ptr<SubTensorType>(new SubTensorType(&act4_out, TensorShape(13U, 13U, 192U, _batches), Coordinates(0, 0, 192)));
197 // Layer 5
198 conv5_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 256U, _batches), 1, _data_type, _fixed_point_position));
199 conv51_out = std::unique_ptr<SubTensorType>(new SubTensorType(&conv5_out, TensorShape(13U, 13U, 128U, _batches), Coordinates()));
200 conv52_out = std::unique_ptr<SubTensorType>(new SubTensorType(&conv5_out, TensorShape(13U, 13U, 128U, _batches), Coordinates(0, 0, 128)));
201 act5_out.allocator()->init(TensorInfo(TensorShape(13U, 13U, 256U, _batches), 1, _data_type, _fixed_point_position));
202 pool5_out.allocator()->init(TensorInfo(TensorShape(6U, 6U, 256U, _batches), 1, _data_type, _fixed_point_position));
203 // Layer 6
204 fc6_out.allocator()->init(TensorInfo(TensorShape(4096U, _batches), 1, _data_type, _fixed_point_position));
205 act6_out.allocator()->init(TensorInfo(TensorShape(4096U, _batches), 1, _data_type, _fixed_point_position));
206 // Layer 7
207 fc7_out.allocator()->init(TensorInfo(TensorShape(4096U, _batches), 1, _data_type, _fixed_point_position));
208 act7_out.allocator()->init(TensorInfo(TensorShape(4096U, _batches), 1, _data_type, _fixed_point_position));
209 // Layer 8
210 fc8_out.allocator()->init(TensorInfo(TensorShape(1000U, _batches), 1, _data_type, _fixed_point_position));
211
212 // Configure Layers
213 // Layer 1
214 TensorType *b0 = _reshaped_weights ? nullptr : &b[0];
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100215 conv1.configure(&input, &w[0], b0, &conv1_out, PadStrideInfo(4, 4, 0, 0), WeightsInfo(_reshaped_weights, 11U, 11U, 96U));
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100216 act1.configure(&conv1_out, &act1_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
217 norm1.configure(&act1_out, &norm1_out, NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f));
218 pool1.configure(&norm1_out, &pool1_out, PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)));
219 // Layer 2
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100220 conv21.configure(pool11_out.get(), w21.get(), b21.get(), conv21_out.get(), PadStrideInfo(1, 1, 2, 2), WeightsInfo(_reshaped_weights, 5U, 5U, 128U));
221 conv22.configure(pool12_out.get(), w22.get(), b22.get(), conv22_out.get(), PadStrideInfo(1, 1, 2, 2), WeightsInfo(_reshaped_weights, 5U, 5U, 128U));
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100222 act2.configure(&conv2_out, &act2_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
223 norm2.configure(&act2_out, &norm2_out, NormalizationLayerInfo(NormType::CROSS_MAP, 5, 0.0001f, 0.75f));
224 pool2.configure(&norm2_out, &pool2_out, PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)));
225 // Layer 3
steniu01a629da12017-07-28 14:40:58 +0100226 TensorType *b2 = (_reshaped_weights && !_is_direct_conv) ? nullptr : &b[2];
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100227 conv3.configure(&pool2_out, &w[2], b2, &conv3_out, PadStrideInfo(1, 1, 1, 1), WeightsInfo(_reshaped_weights, 3U, 3U, 384U));
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100228 act3.configure(&conv3_out, &act3_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
229 // Layer 4
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100230 conv41.configure(act31_out.get(), w41.get(), b41.get(), conv41_out.get(), PadStrideInfo(1, 1, 1, 1), WeightsInfo(_reshaped_weights, 3U, 3U, 192U));
231 conv42.configure(act32_out.get(), w42.get(), b42.get(), conv42_out.get(), PadStrideInfo(1, 1, 1, 1), WeightsInfo(_reshaped_weights, 3U, 3U, 192U));
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100232 act4.configure(&conv4_out, &act4_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
233 // Layer 5
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100234 conv51.configure(act41_out.get(), w51.get(), b51.get(), conv51_out.get(), PadStrideInfo(1, 1, 1, 1), WeightsInfo(_reshaped_weights, 3U, 3U, 128U));
235 conv52.configure(act42_out.get(), w52.get(), b52.get(), conv52_out.get(), PadStrideInfo(1, 1, 1, 1), WeightsInfo(_reshaped_weights, 3U, 3U, 128U));
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100236 act5.configure(&conv5_out, &act5_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
237 pool5.configure(&act5_out, &pool5_out, PoolingLayerInfo(PoolingType::MAX, 3, PadStrideInfo(2, 2, 0, 0)));
238 // Layer 6
239 fc6.configure(&pool5_out, &w[5], &b[5], &fc6_out, true, _reshaped_weights);
240 act6.configure(&fc6_out, &act6_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
241 // Layer 7
242 fc7.configure(&act6_out, &w[6], &b[6], &fc7_out, true, _reshaped_weights);
243 act7.configure(&fc7_out, &act7_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
244 // Layer 8
245 fc8.configure(&act7_out, &w[7], &b[7], &fc8_out, true, _reshaped_weights);
246 // Softmax
247 smx.configure(&fc8_out, &output);
248 }
249
250 void allocate()
251 {
252 input.allocator()->allocate();
253 output.allocator()->allocate();
254
255 if(!_reshaped_weights)
256 {
257 for(auto &wi : w)
258 {
259 wi.allocator()->allocate();
260 }
261
262 for(auto &bi : b)
263 {
264 bi.allocator()->allocate();
265 }
266 }
267 else
268 {
269 w[0].allocator()->allocate();
270 w[2].allocator()->allocate();
271 w[5].allocator()->allocate();
272 w[6].allocator()->allocate();
273 w[7].allocator()->allocate();
274
275 b[5].allocator()->allocate();
276 b[6].allocator()->allocate();
277 b[7].allocator()->allocate();
278
279 dynamic_cast<TensorType *>(w21.get())->allocator()->allocate();
280 dynamic_cast<TensorType *>(w22.get())->allocator()->allocate();
steniu01a629da12017-07-28 14:40:58 +0100281 if(!_is_direct_conv)
282 {
283 dynamic_cast<TensorType *>(w41.get())->allocator()->allocate();
284 dynamic_cast<TensorType *>(w42.get())->allocator()->allocate();
285 dynamic_cast<TensorType *>(w51.get())->allocator()->allocate();
286 dynamic_cast<TensorType *>(w52.get())->allocator()->allocate();
287 }
288 else
289 {
290 b[2].allocator()->allocate();
291 b[3].allocator()->allocate();
292 b[4].allocator()->allocate();
293 w[3].allocator()->allocate();
294 w[4].allocator()->allocate();
295 }
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100296 }
297
298 conv1_out.allocator()->allocate();
299 act1_out.allocator()->allocate();
300 norm1_out.allocator()->allocate();
301 pool1_out.allocator()->allocate();
302 conv2_out.allocator()->allocate();
303 act2_out.allocator()->allocate();
304 norm2_out.allocator()->allocate();
305 pool2_out.allocator()->allocate();
306 conv3_out.allocator()->allocate();
307 act3_out.allocator()->allocate();
308 conv4_out.allocator()->allocate();
309 act4_out.allocator()->allocate();
310 conv5_out.allocator()->allocate();
311 act5_out.allocator()->allocate();
312 pool5_out.allocator()->allocate();
313 fc6_out.allocator()->allocate();
314 act6_out.allocator()->allocate();
315 fc7_out.allocator()->allocate();
316 act7_out.allocator()->allocate();
317 fc8_out.allocator()->allocate();
318 }
319
320 /** Fills the trainable parameters and input with random data. */
321 void fill_random()
322 {
323 library->fill_tensor_uniform(Accessor(input), 0);
324
325 if(!_reshaped_weights)
326 {
327 for(unsigned int i = 0; i < w.size(); ++i)
328 {
329 library->fill_tensor_uniform(Accessor(w[i]), i + 1);
330 library->fill_tensor_uniform(Accessor(b[i]), i + 10);
331 }
332 }
333 else
334 {
335 library->fill_tensor_uniform(Accessor(w[0]), 1);
336 library->fill_tensor_uniform(Accessor(w[2]), 2);
337
338 library->fill_tensor_uniform(Accessor(w[5]), 3);
339 library->fill_tensor_uniform(Accessor(b[5]), 4);
340 library->fill_tensor_uniform(Accessor(w[6]), 5);
341 library->fill_tensor_uniform(Accessor(b[6]), 6);
342 library->fill_tensor_uniform(Accessor(w[7]), 7);
343 library->fill_tensor_uniform(Accessor(b[7]), 8);
344
345 library->fill_tensor_uniform(Accessor(*dynamic_cast<TensorType *>(w21.get())), 9);
346 library->fill_tensor_uniform(Accessor(*dynamic_cast<TensorType *>(w22.get())), 10);
steniu01a629da12017-07-28 14:40:58 +0100347
348 if(!_is_direct_conv)
349 {
350 library->fill_tensor_uniform(Accessor(*dynamic_cast<TensorType *>(w41.get())), 11);
351 library->fill_tensor_uniform(Accessor(*dynamic_cast<TensorType *>(w42.get())), 12);
352 library->fill_tensor_uniform(Accessor(*dynamic_cast<TensorType *>(w51.get())), 13);
353 library->fill_tensor_uniform(Accessor(*dynamic_cast<TensorType *>(w52.get())), 14);
354 }
355 else
356 {
357 library->fill_tensor_uniform(Accessor(w[3]), 11);
358 library->fill_tensor_uniform(Accessor(b[3]), 12);
359 library->fill_tensor_uniform(Accessor(w[4]), 13);
360 library->fill_tensor_uniform(Accessor(b[4]), 14);
361 }
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100362 }
363 }
364
365#ifdef INTERNAL_ONLY
366 /** Fills the trainable parameters from binary files
367 *
368 * @param weights Files names containing the weights data
369 * @param biases Files names containing the bias data
370 */
371 void fill(std::vector<std::string> weights, std::vector<std::string> biases)
372 {
373 ARM_COMPUTE_ERROR_ON(weights.size() != w.size());
374 ARM_COMPUTE_ERROR_ON(biases.size() != b.size());
375 ARM_COMPUTE_ERROR_ON(_reshaped_weights);
376
377 for(unsigned int i = 0; i < weights.size(); ++i)
378 {
379 library->fill_layer_data(Accessor(w[i]), weights[i]);
380 library->fill_layer_data(Accessor(b[i]), biases[i]);
381 }
382 }
383
384 /** Feed input to network from file.
385 *
386 * @param name File name of containing the input data.
387 */
388 void feed(std::string name)
389 {
390 library->fill_layer_data(Accessor(input), name);
391 }
392#endif /* INTERNAL_ONLY */
393
394 /** Get the classification results.
395 *
396 * @return Vector containing the classified labels
397 */
398 std::vector<unsigned int> get_classifications()
399 {
400 std::vector<unsigned int> classified_labels;
401 Accessor output_accessor(output);
402
403 Window window;
404 window.set(Window::DimX, Window::Dimension(0, 1, 1));
405 for(unsigned int d = 1; d < output_accessor.shape().num_dimensions(); ++d)
406 {
407 window.set(d, Window::Dimension(0, output_accessor.shape()[d], 1));
408 }
409
410 execute_window_loop(window, [&](const Coordinates & id)
411 {
412 int max_idx = 0;
413 float val = 0;
414 const void *const out_ptr = output_accessor(id);
415 for(unsigned int l = 0; l < output_accessor.shape().x(); ++l)
416 {
417 float curr_val = reinterpret_cast<const float *>(out_ptr)[l];
418 if(curr_val > val)
419 {
420 max_idx = l;
421 val = curr_val;
422 }
423 }
424 classified_labels.push_back(max_idx);
425 });
426 return classified_labels;
427 }
428
429 /** Clear all allocated memory from the tensor objects */
430 void clear()
431 {
432 // Free allocations
433 input.allocator()->free();
434 output.allocator()->free();
435
436 if(!_reshaped_weights)
437 {
438 for(auto &wi : w)
439 {
440 wi.allocator()->free();
441 }
442
443 for(auto &bi : b)
444 {
445 bi.allocator()->free();
446 }
447 }
448 else
449 {
450 w[0].allocator()->free();
451 w[2].allocator()->free();
452 w[5].allocator()->free();
453 w[6].allocator()->free();
454 w[7].allocator()->free();
455
456 b[5].allocator()->free();
457 b[6].allocator()->free();
458 b[7].allocator()->free();
steniu01a629da12017-07-28 14:40:58 +0100459
460 if(_is_direct_conv)
461 {
462 w[3].allocator()->free();
463 w[4].allocator()->free();
464 b[2].allocator()->free();
465 b[3].allocator()->free();
466 b[4].allocator()->free();
467 }
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100468 }
469
470 w21.reset();
471 w22.reset();
472 b21.reset();
473 b21.reset();
474 w41.reset();
475 w42.reset();
476 b41.reset();
477 b42.reset();
478 w51.reset();
479 w52.reset();
480 b51.reset();
481 b52.reset();
482
483 conv1_out.allocator()->free();
484 act1_out.allocator()->free();
485 norm1_out.allocator()->free();
486 pool1_out.allocator()->free();
487 conv2_out.allocator()->free();
488 act2_out.allocator()->free();
489 norm2_out.allocator()->free();
490 pool2_out.allocator()->free();
491 conv3_out.allocator()->free();
492 act3_out.allocator()->free();
493 conv4_out.allocator()->free();
494 act4_out.allocator()->free();
495 conv5_out.allocator()->free();
496 act5_out.allocator()->free();
497 pool5_out.allocator()->free();
498 fc6_out.allocator()->free();
499 act6_out.allocator()->free();
500 fc7_out.allocator()->free();
501 act7_out.allocator()->free();
502 fc8_out.allocator()->free();
503 }
504
505 /** Runs the model */
506 void run()
507 {
508 // Layer 1
509 conv1.run();
510 act1.run();
511 norm1.run();
512 pool1.run();
513 // Layer 2
514 conv21.run();
515 conv22.run();
516 act2.run();
517 norm2.run();
518 pool2.run();
519 // Layer 3
520 conv3.run();
521 act3.run();
522 // Layer 4
523 conv41.run();
524 conv42.run();
525 act4.run();
526 // Layer 5
527 conv51.run();
528 conv52.run();
529 act5.run();
530 pool5.run();
531 // Layer 6
532 fc6.run();
533 act6.run();
534 // Layer 7
535 fc7.run();
536 act7.run();
537 // Layer 8
538 fc8.run();
539 // Softmax
540 smx.run();
541 }
542
543private:
steniu01a629da12017-07-28 14:40:58 +0100544 struct DirectConv
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100545 {
steniu01a629da12017-07-28 14:40:58 +0100546 template <typename ConvolutionLayerFunction1 = ConvolutionLayerFunction, typename DirectConvolutionLayerFunction1 = DirectConvolutionLayerFunction>
547 typename std::enable_if < !std::is_same<ConvolutionLayerFunction1, DirectConvolutionLayerFunction1>::value, void >::type
548 configure(ITensorType *input, const ITensorType *weights, const ITensorType *biases, ITensorType *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo())
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100549 {
steniu01a629da12017-07-28 14:40:58 +0100550 _func.configure(input, weights, biases, output, conv_info);
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100551 }
steniu01a629da12017-07-28 14:40:58 +0100552
553 template <typename ConvolutionLayerFunction1 = ConvolutionLayerFunction, typename DirectConvolutionLayerFunction1 = DirectConvolutionLayerFunction>
554 typename std::enable_if<std::is_same<ConvolutionLayerFunction1, DirectConvolutionLayerFunction1>::value, void>::type
555 configure(ITensorType *input, const ITensorType *weights, const ITensorType *biases, ITensorType *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo())
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100556 {
steniu01a629da12017-07-28 14:40:58 +0100557 _func.configure(input, weights, biases, output, conv_info, weights_info);
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100558 }
steniu01a629da12017-07-28 14:40:58 +0100559
560 void run()
561 {
562 _func.run();
563 }
564
565 DirectConvolutionLayerFunction _func{};
566 };
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100567
568 DataType _data_type{ DataType::UNKNOWN };
569 int _fixed_point_position{ 0 };
570 unsigned int _batches{ 0 };
571 bool _reshaped_weights{ false };
steniu01a629da12017-07-28 14:40:58 +0100572 bool _is_direct_conv{ !std::is_same<ConvolutionLayerFunction, DirectConvolutionLayerFunction>::value };
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100573
574 ActivationLayerFunction act1{}, act2{}, act3{}, act4{}, act5{}, act6{}, act7{};
steniu01a629da12017-07-28 14:40:58 +0100575 ConvolutionLayerFunction conv1{}, conv21{}, conv22{};
576 DirectConv conv3{}, conv41{}, conv42{}, conv51{}, conv52{};
Moritz Pflanzeree493ae2017-07-05 10:52:21 +0100577 FullyConnectedLayerFunction fc6{}, fc7{}, fc8{};
578 NormalizationLayerFunction norm1{}, norm2{};
579 PoolingLayerFunction pool1{}, pool2{}, pool5{};
580 SoftmaxLayerFunction smx{};
581
582 TensorType input{}, output{};
583 std::array<TensorType, 8> w{ {} }, b{ {} };
584 std::unique_ptr<ITensorType> w21{ nullptr }, w22{ nullptr }, b21{ nullptr }, b22{ nullptr };
585 std::unique_ptr<ITensorType> w41{ nullptr }, w42{ nullptr }, b41{ nullptr }, b42{ nullptr };
586 std::unique_ptr<ITensorType> w51{ nullptr }, w52{ nullptr }, b51{ nullptr }, b52{ nullptr };
587
588 TensorType conv1_out{}, act1_out{}, norm1_out{}, pool1_out{};
589 TensorType conv2_out{}, act2_out{}, pool2_out{}, norm2_out{};
590 TensorType conv3_out{}, act3_out{};
591 TensorType conv4_out{}, act4_out{};
592 TensorType conv5_out{}, act5_out{}, pool5_out{};
593 TensorType fc6_out{}, act6_out{};
594 TensorType fc7_out{}, act7_out{};
595 TensorType fc8_out{};
596
597 std::unique_ptr<SubTensorType> pool11_out{}, pool12_out{};
598 std::unique_ptr<SubTensorType> conv21_out{}, conv22_out{};
599 std::unique_ptr<SubTensorType> act31_out{}, act32_out{};
600 std::unique_ptr<SubTensorType> conv41_out{}, conv42_out{}, act41_out{}, act42_out{};
601 std::unique_ptr<SubTensorType> conv51_out{}, conv52_out{};
602};
603} // namespace networks
604} // namespace test
605} // namespace arm_compute
606#endif //__ARM_COMPUTE_TEST_MODEL_OBJECTS_ALEXNET_H__