blob: 0957c6b555440516aac9f19d17572e7b0f913c0d [file] [log] [blame]
Georgios Pinitas236bfe72017-11-23 15:59:55 +00001/*
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_MOBILENETV1_H__
25#define __ARM_COMPUTE_TEST_MODEL_OBJECTS_MOBILENETV1_H__
26
27#include "tests/AssetsLibrary.h"
28#include "tests/Globals.h"
29#include "tests/Utils.h"
30
31#include "utils/Utils.h"
32
33#include <memory>
34
35using namespace arm_compute;
36using namespace arm_compute::test;
37
38namespace arm_compute
39{
40namespace test
41{
42namespace networks
43{
44/** MobileNet model object */
45template <typename TensorType,
46 typename Accessor,
47 typename ActivationLayerFunction,
48 typename BatchNormalizationLayerFunction,
49 typename ConvolutionLayerFunction,
50 typename DirectConvolutionLayerFunction,
51 typename DepthwiseConvolutionFunction,
52 typename ReshapeFunction,
53 typename PoolingLayerFunction,
54 typename SoftmaxLayerFunction>
55class MobileNetV1Network
56{
57public:
58 void init(unsigned int input_spatial_size, int batches)
59 {
60 _batches = batches;
61 _input_spatial_size = input_spatial_size;
62
63 // Currently supported sizes
64 ARM_COMPUTE_ERROR_ON(input_spatial_size != 128 && input_spatial_size != 224);
65
66 // Initialize input, output
67 input.allocator()->init(TensorInfo(TensorShape(input_spatial_size, input_spatial_size, 3U, _batches), 1, DataType::F32));
68 output.allocator()->init(TensorInfo(TensorShape(1001U, _batches), 1, DataType::F32));
69 // Initialize weights and biases
70 w_conv3x3.allocator()->init(TensorInfo(TensorShape(3U, 3U, 3U, 32U), 1, DataType::F32));
71 mean_conv3x3.allocator()->init(TensorInfo(TensorShape(32U), 1, DataType::F32));
72 var_conv3x3.allocator()->init(TensorInfo(TensorShape(32U), 1, DataType::F32));
73 beta_conv3x3.allocator()->init(TensorInfo(TensorShape(32U), 1, DataType::F32));
74 gamma_conv3x3.allocator()->init(TensorInfo(TensorShape(32U), 1, DataType::F32));
75 depthwise_conv_block_init(0, 32, 32);
76 depthwise_conv_block_init(1, 32, 64);
77 depthwise_conv_block_init(2, 64, 64);
78 depthwise_conv_block_init(3, 64, 128);
79 depthwise_conv_block_init(4, 128, 256);
80 depthwise_conv_block_init(5, 256, 512);
81 depthwise_conv_block_init(6, 512, 512);
82 depthwise_conv_block_init(7, 512, 512);
83 depthwise_conv_block_init(8, 512, 512);
84 depthwise_conv_block_init(9, 512, 512);
85 depthwise_conv_block_init(10, 512, 512);
86 depthwise_conv_block_init(11, 512, 1024);
87 depthwise_conv_block_init(12, 1024, 1024);
88 w_conv1c.allocator()->init(TensorInfo(TensorShape(1U, 1U, 1024U, 1001U), 1, DataType::F32));
89 b_conv1c.allocator()->init(TensorInfo(TensorShape(1001U), 1, DataType::F32));
90 // Init reshaped output
91 reshape_out.allocator()->init(TensorInfo(TensorShape(1001U, _batches), 1, DataType::F32));
92 }
93
94 /** Build the model. */
95 void build()
96 {
97 // Configure Layers
98 conv3x3.configure(&input, &w_conv3x3, nullptr, &conv_out[0], PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR));
99 conv3x3_bn.configure(&conv_out[0], nullptr, &mean_conv3x3, &var_conv3x3, &beta_conv3x3, &gamma_conv3x3, 0.001f);
100 conv3x3_act.configure(&conv_out[0], nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f));
101 depthwise_conv_block_build(0, PadStrideInfo(1, 1, 1, 1), PadStrideInfo(1, 1, 0, 0));
102 depthwise_conv_block_build(1, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
103 depthwise_conv_block_build(2, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
104 depthwise_conv_block_build(3, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
105 depthwise_conv_block_build(4, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
106 depthwise_conv_block_build(5, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
107 depthwise_conv_block_build(6, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
108 depthwise_conv_block_build(7, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
109 depthwise_conv_block_build(8, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
110 depthwise_conv_block_build(9, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
111 depthwise_conv_block_build(10, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
112 depthwise_conv_block_build(11, PadStrideInfo(2, 2, 0, 1, 0, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
113 depthwise_conv_block_build(12, PadStrideInfo(1, 1, 1, 1, 1, 1, DimensionRoundingType::FLOOR), PadStrideInfo(1, 1, 0, 0));
114 pool.configure(&conv_out[13], &pool_out, PoolingLayerInfo(PoolingType::AVG));
115 conv1c.configure(&pool_out, &w_conv1c, &b_conv1c, &conv_out[14], PadStrideInfo(1, 1, 0, 0));
116 reshape.configure(&conv_out[14], &reshape_out);
117 smx.configure(&reshape_out, &output);
118 }
119
120 void allocate()
121 {
122 input.allocator()->allocate();
123 output.allocator()->allocate();
124
125 w_conv3x3.allocator()->allocate();
126 mean_conv3x3.allocator()->allocate();
127 var_conv3x3.allocator()->allocate();
128 beta_conv3x3.allocator()->allocate();
129 gamma_conv3x3.allocator()->allocate();
130
131 ARM_COMPUTE_ERROR_ON(w_conv.size() != w_dwc.size());
132 for(unsigned int i = 0; i < w_conv.size(); ++i)
133 {
134 w_dwc[i].allocator()->allocate();
135 bn_mean[2 * i].allocator()->allocate();
136 bn_var[2 * i].allocator()->allocate();
137 bn_beta[2 * i].allocator()->allocate();
138 bn_gamma[2 * i].allocator()->allocate();
139 w_conv[i].allocator()->allocate();
140 bn_mean[2 * i + 1].allocator()->allocate();
141 bn_var[2 * i + 1].allocator()->allocate();
142 bn_beta[2 * i + 1].allocator()->allocate();
143 bn_gamma[2 * i + 1].allocator()->allocate();
144 }
145 w_conv1c.allocator()->allocate();
146 b_conv1c.allocator()->allocate();
147
148 // Allocate intermediate buffers
149 for(auto &o : conv_out)
150 {
151 o.allocator()->allocate();
152 }
153 for(auto &o : dwc_out)
154 {
155 o.allocator()->allocate();
156 }
157 pool_out.allocator()->allocate();
158 reshape_out.allocator()->allocate();
159 }
160
161 /** Fills the trainable parameters and input with random data. */
162 void fill_random()
163 {
164 unsigned int seed_idx = 0;
165 std::uniform_real_distribution<> distribution(-1, 1);
166 library->fill(Accessor(input), distribution, seed_idx++);
167
168 library->fill(Accessor(w_conv3x3), distribution, seed_idx++);
169 library->fill(Accessor(mean_conv3x3), distribution, seed_idx++);
170 library->fill(Accessor(var_conv3x3), distribution, seed_idx++);
171 library->fill(Accessor(beta_conv3x3), distribution, seed_idx++);
172 library->fill(Accessor(gamma_conv3x3), distribution, seed_idx++);
173
174 ARM_COMPUTE_ERROR_ON(w_conv.size() != w_dwc.size());
175 for(unsigned int i = 0; i < w_conv.size(); ++i)
176 {
177 library->fill(Accessor(w_dwc[i]), distribution, seed_idx++);
178 library->fill(Accessor(bn_mean[2 * i]), distribution, seed_idx++);
179 library->fill(Accessor(bn_var[2 * i]), distribution, seed_idx++);
180 library->fill(Accessor(bn_beta[2 * i]), distribution, seed_idx++);
181 library->fill(Accessor(bn_gamma[2 * i]), distribution, seed_idx++);
182 library->fill(Accessor(w_conv[i]), distribution, seed_idx++);
183 library->fill(Accessor(bn_mean[2 * i + 1]), distribution, seed_idx++);
184 library->fill(Accessor(bn_var[2 * i + 1]), distribution, seed_idx++);
185 library->fill(Accessor(bn_beta[2 * i + 1]), distribution, seed_idx++);
186 library->fill(Accessor(bn_gamma[2 * i + 1]), distribution, seed_idx++);
187 }
188 library->fill(Accessor(w_conv1c), distribution, seed_idx++);
189 library->fill(Accessor(b_conv1c), distribution, seed_idx++);
190 }
191
192 /** Feed input to network from file.
193 *
194 * @param name File name of containing the input data.
195 */
196 void feed(std::string name)
197 {
198 library->fill_layer_data(Accessor(input), name);
199 }
200
201 /** Get the classification results.
202 *
203 * @return Vector containing the classified labels
204 */
205 std::vector<unsigned int> get_classifications()
206 {
207 std::vector<unsigned int> classified_labels;
208 Accessor output_accessor(output);
209
210 Window window;
211 window.set(Window::DimX, Window::Dimension(0, 1, 1));
212 for(unsigned int d = 1; d < output_accessor.shape().num_dimensions(); ++d)
213 {
214 window.set(d, Window::Dimension(0, output_accessor.shape()[d], 1));
215 }
216
217 execute_window_loop(window, [&](const Coordinates & id)
218 {
219 int max_idx = 0;
220 float val = 0;
221 const void *const out_ptr = output_accessor(id);
222 for(unsigned int l = 0; l < output_accessor.shape().x(); ++l)
223 {
224 float curr_val = reinterpret_cast<const float *>(out_ptr)[l];
225 if(curr_val > val)
226 {
227 max_idx = l;
228 val = curr_val;
229 }
230 }
231 classified_labels.push_back(max_idx);
232 });
233 return classified_labels;
234 }
235
236 /** Clear all allocated memory from the tensor objects */
237 void clear()
238 {
239 input.allocator()->free();
240 output.allocator()->free();
241
242 w_conv3x3.allocator()->free();
243 mean_conv3x3.allocator()->free();
244 var_conv3x3.allocator()->free();
245 beta_conv3x3.allocator()->free();
246 gamma_conv3x3.allocator()->free();
247
248 ARM_COMPUTE_ERROR_ON(w_conv.size() != w_dwc.size());
249 for(unsigned int i = 0; i < w_conv.size(); ++i)
250 {
251 w_dwc[i].allocator()->free();
252 bn_mean[2 * i].allocator()->free();
253 bn_var[2 * i].allocator()->free();
254 bn_beta[2 * i].allocator()->free();
255 bn_gamma[2 * i].allocator()->free();
256 w_conv[i].allocator()->free();
257 bn_mean[2 * i + 1].allocator()->free();
258 bn_var[2 * i + 1].allocator()->free();
259 bn_beta[2 * i + 1].allocator()->free();
260 bn_gamma[2 * i + 1].allocator()->free();
261 }
262 w_conv1c.allocator()->free();
263 b_conv1c.allocator()->free();
264
265 // Free intermediate buffers
266 for(auto &o : conv_out)
267 {
268 o.allocator()->free();
269 }
270 for(auto &o : dwc_out)
271 {
272 o.allocator()->free();
273 }
274 pool_out.allocator()->free();
275 reshape_out.allocator()->free();
276 }
277
278 /** Runs the model */
279 void run()
280 {
281 conv3x3.run();
282 conv3x3_bn.run();
283 conv3x3_act.run();
284 depthwise_conv_block_run(0);
285 depthwise_conv_block_run(1);
286 depthwise_conv_block_run(2);
287 depthwise_conv_block_run(3);
288 depthwise_conv_block_run(4);
289 depthwise_conv_block_run(5);
290 depthwise_conv_block_run(6);
291 depthwise_conv_block_run(7);
292 depthwise_conv_block_run(8);
293 depthwise_conv_block_run(9);
294 depthwise_conv_block_run(10);
295 depthwise_conv_block_run(11);
296 depthwise_conv_block_run(12);
297 pool.run();
298 conv1c.run();
299 reshape.run();
300 smx.run();
301 }
302
Joel Liang1c5ffd62017-12-28 10:09:51 +0800303 /** Sync the results */
304 void sync()
305 {
306 sync_if_necessary<TensorType>();
307 sync_tensor_if_necessary<TensorType>(output);
308 }
309
Georgios Pinitas236bfe72017-11-23 15:59:55 +0000310private:
311 void depthwise_conv_block_init(unsigned int idx, unsigned int ifm, unsigned int ofm)
312 {
313 // Depthwise Convolution weights
314 w_dwc[idx].allocator()->init(TensorInfo(TensorShape(3U, 3U, ifm), 1, DataType::F32));
315 // Batch normalization parameters
316 bn_mean[2 * idx].allocator()->init(TensorInfo(TensorShape(ifm), 1, DataType::F32));
317 bn_var[2 * idx].allocator()->init(TensorInfo(TensorShape(ifm), 1, DataType::F32));
318 bn_beta[2 * idx].allocator()->init(TensorInfo(TensorShape(ifm), 1, DataType::F32));
319 bn_gamma[2 * idx].allocator()->init(TensorInfo(TensorShape(ifm), 1, DataType::F32));
320 // Convolution weights
321 w_conv[idx].allocator()->init(TensorInfo(TensorShape(1U, 1U, ifm, ofm), 1, DataType::F32));
322 // Batch normalization parameters
323 bn_mean[2 * idx + 1].allocator()->init(TensorInfo(TensorShape(ofm), 1, DataType::F32));
324 bn_var[2 * idx + 1].allocator()->init(TensorInfo(TensorShape(ofm), 1, DataType::F32));
325 bn_beta[2 * idx + 1].allocator()->init(TensorInfo(TensorShape(ofm), 1, DataType::F32));
326 bn_gamma[2 * idx + 1].allocator()->init(TensorInfo(TensorShape(ofm), 1, DataType::F32));
327 }
328 void depthwise_conv_block_build(unsigned int idx, PadStrideInfo dwc_ps, PadStrideInfo conv_ps)
329 {
330 // Configure depthwise convolution block
331 dwc3x3[idx].configure(&conv_out[idx], &w_dwc[idx], nullptr, &dwc_out[idx], dwc_ps);
332 bn[2 * idx].configure(&dwc_out[idx], nullptr, &bn_mean[2 * idx], &bn_var[2 * idx], &bn_beta[2 * idx], &bn_gamma[2 * idx], 0.001f);
333 act[2 * idx].configure(&dwc_out[idx], nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f));
334 // Configure pointwise convolution block
335 conv1x1[idx].configure(&dwc_out[idx], &w_conv[idx], nullptr, &conv_out[idx + 1], conv_ps);
336 bn[2 * idx + 1].configure(&conv_out[idx + 1], nullptr, &bn_mean[2 * idx + 1], &bn_var[2 * idx + 1], &bn_beta[2 * idx + 1], &bn_gamma[2 * idx + 1], 0.001f);
337 act[2 * idx + 1].configure(&conv_out[idx], nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f));
338 }
339 void depthwise_conv_block_run(unsigned int idx)
340 {
341 dwc3x3[idx].run();
342 bn[2 * idx].run();
343 act[2 * idx].run();
344 conv1x1[idx].run();
345 bn[2 * idx + 1].run();
346 act[2 * idx + 1].run();
347 }
348
349private:
350 unsigned int _batches{ 0 };
351 unsigned int _input_spatial_size{ 0 };
352
353 ConvolutionLayerFunction conv3x3{};
354 BatchNormalizationLayerFunction conv3x3_bn{};
355 ActivationLayerFunction conv3x3_act{};
356 std::array<ActivationLayerFunction, 26> act{ {} };
357 std::array<BatchNormalizationLayerFunction, 26> bn{ {} };
358 std::array<DepthwiseConvolutionFunction, 13> dwc3x3{ {} };
359 std::array<DirectConvolutionLayerFunction, 13> conv1x1{ {} };
360 DirectConvolutionLayerFunction conv1c{};
361 PoolingLayerFunction pool{};
362 ReshapeFunction reshape{};
363 SoftmaxLayerFunction smx{};
364
365 TensorType w_conv3x3{}, mean_conv3x3{}, var_conv3x3{}, beta_conv3x3{}, gamma_conv3x3{};
366 std::array<TensorType, 13> w_conv{ {} };
367 std::array<TensorType, 13> w_dwc{ {} };
368 std::array<TensorType, 26> bn_mean{ {} };
369 std::array<TensorType, 26> bn_var{ {} };
370 std::array<TensorType, 26> bn_beta{ {} };
371 std::array<TensorType, 26> bn_gamma{ {} };
372 TensorType w_conv1c{}, b_conv1c{};
373
374 TensorType input{}, output{};
375
376 std::array<TensorType, 15> conv_out{ {} };
377 std::array<TensorType, 13> dwc_out{ {} };
378 TensorType pool_out{};
379 TensorType reshape_out{};
380};
381} // namespace networks
382} // namespace test
383} // namespace arm_compute
384#endif //__ARM_COMPUTE_TEST_MODEL_OBJECTS_MOBILENETV1_H__