blob: eec6972470d1e5e16870ea2b890aeb2f6c7eb650 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini053e7512018-12-28 15:05:20 +00002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 __UTILS_UTILS_H__
25#define __UTILS_UTILS_H__
26
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
steniu01bee466b2017-06-21 16:45:41 +010031#include "arm_compute/core/Window.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/runtime/Tensor.h"
Giorgio Arenacf3935f2017-10-26 17:14:13 +010033#include "libnpy/npy.hpp"
Anthony Barbier2a07e182017-08-04 18:20:27 +010034#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36#ifdef ARM_COMPUTE_CL
37#include "arm_compute/core/CL/OpenCL.h"
Isabella Gottardi02aabcc2017-10-12 17:28:51 +010038#include "arm_compute/runtime/CL/CLDistribution1D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include "arm_compute/runtime/CL/CLTensor.h"
40#endif /* ARM_COMPUTE_CL */
Anthony Barbier7068f992017-10-26 15:23:08 +010041#ifdef ARM_COMPUTE_GC
42#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
43#endif /* ARM_COMPUTE_GC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45#include <cstdlib>
46#include <cstring>
47#include <fstream>
48#include <iostream>
Giorgio Arenacf3935f2017-10-26 17:14:13 +010049#include <random>
50#include <string>
51#include <tuple>
52#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
54namespace arm_compute
55{
56namespace utils
57{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010058/** Supported image types */
59enum class ImageType
60{
61 UNKNOWN,
62 PPM,
63 JPEG
64};
65
Anthony Barbier6db0ff52018-01-05 10:59:12 +000066/** Abstract Example class.
67 *
68 * All examples have to inherit from this class.
69 */
70class Example
71{
72public:
Alex Gildayc357c472018-03-21 13:54:09 +000073 /** Setup the example.
74 *
75 * @param[in] argc Argument count.
76 * @param[in] argv Argument values.
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010077 *
78 * @return True in case of no errors in setup else false
Alex Gildayc357c472018-03-21 13:54:09 +000079 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010080 virtual bool do_setup(int argc, char **argv)
81 {
82 return true;
83 };
Alex Gildayc357c472018-03-21 13:54:09 +000084 /** Run the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000085 virtual void do_run() {};
Alex Gildayc357c472018-03-21 13:54:09 +000086 /** Teardown the example. */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000087 virtual void do_teardown() {};
88
89 /** Default destructor. */
90 virtual ~Example() = default;
91};
92
93/** Run an example and handle the potential exceptions it throws
94 *
95 * @param[in] argc Number of command line arguments
96 * @param[in] argv Command line arguments
97 * @param[in] example Example to run
98 */
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010099int run_example(int argc, char **argv, std::unique_ptr<Example> example);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000100
101template <typename T>
102int run_example(int argc, char **argv)
103{
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100104 return run_example(argc, argv, support::cpp14::make_unique<T>());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000105}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
107/** Draw a RGB rectangular window for the detected object
108 *
109 * @param[in, out] tensor Input tensor where the rectangle will be drawn on. Format supported: RGB888
110 * @param[in] rect Geometry of the rectangular window
111 * @param[in] r Red colour to use
112 * @param[in] g Green colour to use
113 * @param[in] b Blue colour to use
114 */
115void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
116
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100117/** Gets image type given a file
118 *
119 * @param[in] filename File to identify its image type
120 *
121 * @return Image type
122 */
123ImageType get_image_type_from_file(const std::string &filename);
124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125/** Parse the ppm header from an input file stream. At the end of the execution,
126 * the file position pointer will be located at the first pixel stored in the ppm file
127 *
128 * @param[in] fs Input file stream to parse
129 *
130 * @return The width, height and max value stored in the header of the PPM file
131 */
132std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
133
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100134/** Parse the npy header from an input file stream. At the end of the execution,
135 * the file position pointer will be located at the first pixel stored in the npy file //TODO
136 *
137 * @param[in] fs Input file stream to parse
138 *
139 * @return The width and height stored in the header of the NPY file
140 */
141std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs);
142
143/** Obtain numpy type string from DataType.
144 *
145 * @param[in] data_type Data type.
146 *
147 * @return numpy type string.
148 */
149inline std::string get_typestring(DataType data_type)
150{
151 // Check endianness
152 const unsigned int i = 1;
153 const char *c = reinterpret_cast<const char *>(&i);
154 std::string endianness;
155 if(*c == 1)
156 {
157 endianness = std::string("<");
158 }
159 else
160 {
161 endianness = std::string(">");
162 }
163 const std::string no_endianness("|");
164
165 switch(data_type)
166 {
167 case DataType::U8:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000168 case DataType::QASYMM8:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100169 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
170 case DataType::S8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100171 case DataType::QSYMM8:
172 case DataType::QSYMM8_PER_CHANNEL:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100173 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
174 case DataType::U16:
175 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
176 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100177 case DataType::QSYMM16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100178 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
179 case DataType::U32:
180 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
181 case DataType::S32:
182 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
183 case DataType::U64:
184 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
185 case DataType::S64:
186 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000187 case DataType::F16:
188 return endianness + "f" + support::cpp11::to_string(sizeof(half));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100189 case DataType::F32:
190 return endianness + "f" + support::cpp11::to_string(sizeof(float));
191 case DataType::F64:
192 return endianness + "f" + support::cpp11::to_string(sizeof(double));
193 case DataType::SIZET:
194 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
195 default:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100196 ARM_COMPUTE_ERROR("Data type not supported");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100197 }
198}
199
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100200/** Maps a tensor if needed
201 *
202 * @param[in] tensor Tensor to be mapped
203 * @param[in] blocking Specified if map is blocking or not
204 */
205template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100206inline void map(T &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100207{
208 ARM_COMPUTE_UNUSED(tensor);
209 ARM_COMPUTE_UNUSED(blocking);
210}
211
212/** Unmaps a tensor if needed
213 *
214 * @param tensor Tensor to be unmapped
215 */
216template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100217inline void unmap(T &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100218{
219 ARM_COMPUTE_UNUSED(tensor);
220}
221
222#ifdef ARM_COMPUTE_CL
223/** Maps a tensor if needed
224 *
225 * @param[in] tensor Tensor to be mapped
226 * @param[in] blocking Specified if map is blocking or not
227 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100228inline void map(CLTensor &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100229{
230 tensor.map(blocking);
231}
232
233/** Unmaps a tensor if needed
234 *
235 * @param tensor Tensor to be unmapped
236 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100237inline void unmap(CLTensor &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100238{
239 tensor.unmap();
240}
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100241
242/** Maps a distribution if needed
243 *
244 * @param[in] distribution Distribution to be mapped
245 * @param[in] blocking Specified if map is blocking or not
246 */
247inline void map(CLDistribution1D &distribution, bool blocking)
248{
249 distribution.map(blocking);
250}
251
252/** Unmaps a distribution if needed
253 *
254 * @param distribution Distribution to be unmapped
255 */
256inline void unmap(CLDistribution1D &distribution)
257{
258 distribution.unmap();
259}
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100260#endif /* ARM_COMPUTE_CL */
261
Anthony Barbier7068f992017-10-26 15:23:08 +0100262#ifdef ARM_COMPUTE_GC
263/** Maps a tensor if needed
264 *
265 * @param[in] tensor Tensor to be mapped
266 * @param[in] blocking Specified if map is blocking or not
267 */
268inline void map(GCTensor &tensor, bool blocking)
269{
270 tensor.map(blocking);
271}
272
273/** Unmaps a tensor if needed
274 *
275 * @param tensor Tensor to be unmapped
276 */
277inline void unmap(GCTensor &tensor)
278{
279 tensor.unmap();
280}
281#endif /* ARM_COMPUTE_GC */
282
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000283/** Specialized class to generate random non-zero FP16 values.
284 * uniform_real_distribution<half> generates values that get rounded off to zero, causing
285 * differences between ACL and reference implementation
286*/
287class uniform_real_distribution_fp16
288{
289 half min{ 0.0f }, max{ 0.0f };
290 std::uniform_real_distribution<float> neg{ min, -0.3f };
291 std::uniform_real_distribution<float> pos{ 0.3f, max };
292 std::uniform_int_distribution<uint8_t> sign_picker{ 0, 1 };
293
294public:
295 using result_type = half;
296 /** Constructor
297 *
298 * @param[in] a Minimum value of the distribution
299 * @param[in] b Maximum value of the distribution
300 */
301 explicit uniform_real_distribution_fp16(half a = half(0.0), half b = half(1.0))
302 : min(a), max(b)
303 {
304 }
305
306 /** () operator to generate next value
307 *
308 * @param[in] gen an uniform random bit generator object
309 */
310 half operator()(std::mt19937 &gen)
311 {
312 if(sign_picker(gen))
313 {
314 return (half)neg(gen);
315 }
316 return (half)pos(gen);
317 }
318};
319
Alex Gildayc357c472018-03-21 13:54:09 +0000320/** Numpy data loader */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100321class NPYLoader
322{
323public:
Alex Gildayc357c472018-03-21 13:54:09 +0000324 /** Default constructor */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100325 NPYLoader()
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100326 : _fs(), _shape(), _fortran_order(false), _typestring(), _file_layout(DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100327 {
328 }
329
330 /** Open a NPY file and reads its metadata
331 *
332 * @param[in] npy_filename File to open
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100333 * @param[in] file_layout (Optional) Layout in which the weights are stored in the file.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100334 */
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100335 void open(const std::string &npy_filename, DataLayout file_layout = DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100336 {
337 ARM_COMPUTE_ERROR_ON(is_open());
338 try
339 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100340 _fs.open(npy_filename, std::ios::in | std::ios::binary);
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100341 ARM_COMPUTE_EXIT_ON_MSG(!_fs.good(), "Failed to load binary data from %s", npy_filename.c_str());
342 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
343 _file_layout = file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100344
345 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
346 }
347 catch(const std::ifstream::failure &e)
348 {
349 ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what());
350 }
351 }
352 /** Return true if a NPY file is currently open */
353 bool is_open()
354 {
355 return _fs.is_open();
356 }
357
358 /** Return true if a NPY file is in fortran order */
359 bool is_fortran()
360 {
361 return _fortran_order;
362 }
363
Gian Marco0bc5a252017-12-04 13:55:08 +0000364 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100365 *
366 * @param[out] tensor Tensor to initialise
Gian Marco0bc5a252017-12-04 13:55:08 +0000367 * @param[in] dt Data type to use for the tensor
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100368 */
369 template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000370 void init_tensor(T &tensor, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100371 {
372 ARM_COMPUTE_ERROR_ON(!is_open());
Gian Marco0bc5a252017-12-04 13:55:08 +0000373 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100374
375 // Use the size of the input NPY tensor
376 TensorShape shape;
377 shape.set_num_dimensions(_shape.size());
378 for(size_t i = 0; i < _shape.size(); ++i)
379 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100380 size_t src = i;
381 if(_fortran_order)
382 {
383 src = _shape.size() - 1 - i;
384 }
385 shape.set(i, _shape.at(src));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100386 }
387
Gian Marco0bc5a252017-12-04 13:55:08 +0000388 arm_compute::TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100389 tensor.allocator()->init(tensor_info);
390 }
391
392 /** Fill a tensor with the content of the currently open NPY file.
393 *
394 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
395 *
396 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
397 */
398 template <typename T>
399 void fill_tensor(T &tensor)
400 {
401 ARM_COMPUTE_ERROR_ON(!is_open());
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100402 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::QASYMM8, arm_compute::DataType::S32, arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100403 try
404 {
405 // Map buffer if creating a CLTensor
406 map(tensor, true);
407
408 // Check if the file is large enough to fill the tensor
409 const size_t current_position = _fs.tellg();
410 _fs.seekg(0, std::ios_base::end);
411 const size_t end_position = _fs.tellg();
412 _fs.seekg(current_position, std::ios_base::beg);
413
414 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
415 "Not enough data in file");
416 ARM_COMPUTE_UNUSED(end_position);
417
418 // Check if the typestring matches the given one
419 std::string expect_typestr = get_typestring(tensor.info()->data_type());
420 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
421
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100422 bool are_layouts_different = (_file_layout != tensor.info()->data_layout());
423 // Correct dimensions (Needs to match TensorShape dimension corrections)
424 if(_shape.size() != tensor.info()->tensor_shape().num_dimensions())
425 {
426 for(int i = static_cast<int>(_shape.size()) - 1; i > 0; --i)
427 {
428 if(_shape[i] == 1)
429 {
430 _shape.pop_back();
431 }
432 else
433 {
434 break;
435 }
436 }
437 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100438
439 TensorShape permuted_shape = tensor.info()->tensor_shape();
440 arm_compute::PermutationVector perm;
441 if(are_layouts_different && tensor.info()->tensor_shape().num_dimensions() > 2)
442 {
443 perm = (tensor.info()->data_layout() == arm_compute::DataLayout::NHWC) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
444 arm_compute::PermutationVector perm_vec = (tensor.info()->data_layout() == arm_compute::DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
445
446 arm_compute::permute(permuted_shape, perm_vec);
447 }
448
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100449 // Validate tensor shape
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000450 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
Michalis Spyrou39412952018-08-14 17:06:16 +0100451 for(size_t i = 0; i < _shape.size(); ++i)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100452 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100453 ARM_COMPUTE_ERROR_ON_MSG(permuted_shape[i] != _shape[i], "Tensor dimensions mismatch");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100454 }
455
Gian Marco0bc5a252017-12-04 13:55:08 +0000456 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100457 {
Georgios Pinitasa799ce02018-09-12 20:11:34 +0100458 case arm_compute::DataType::QASYMM8:
459 case arm_compute::DataType::S32:
Gian Marco0bc5a252017-12-04 13:55:08 +0000460 case arm_compute::DataType::F32:
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000461 case arm_compute::DataType::F16:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100462 {
463 // Read data
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100464 if(!are_layouts_different && !_fortran_order && tensor.info()->padding().empty())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100465 {
466 // If tensor has no padding read directly from stream.
467 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
468 }
469 else
470 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100471 // If tensor has padding or is in fortran order accessing tensor elements through execution window.
Michalis Spyrou39412952018-08-14 17:06:16 +0100472 Window window;
473 const unsigned int num_dims = _shape.size();
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100474 if(_fortran_order)
475 {
476 for(unsigned int dim = 0; dim < num_dims; dim++)
477 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100478 permuted_shape.set(dim, _shape[num_dims - dim - 1]);
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100479 perm.set(dim, num_dims - dim - 1);
480 }
Michalis Spyrou39412952018-08-14 17:06:16 +0100481 if(are_layouts_different)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100482 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100483 // Permute only if num_dimensions greater than 2
484 if(num_dims > 2)
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100485 {
Michalis Spyrou39412952018-08-14 17:06:16 +0100486 if(_file_layout == DataLayout::NHWC) // i.e destination is NCHW --> permute(1,2,0)
487 {
488 arm_compute::permute(perm, arm_compute::PermutationVector(1U, 2U, 0U));
489 }
490 else
491 {
492 arm_compute::permute(perm, arm_compute::PermutationVector(2U, 0U, 1U));
493 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100494 }
495 }
496 }
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100497 window.use_tensor_dimensions(permuted_shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100498
499 execute_window_loop(window, [&](const Coordinates & id)
500 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100501 Coordinates dst(id);
502 arm_compute::permute(dst, perm);
503 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(dst)), tensor.info()->element_size());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100504 });
505 }
506
507 break;
508 }
509 default:
Gian Marco0bc5a252017-12-04 13:55:08 +0000510 ARM_COMPUTE_ERROR("Unsupported data type");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100511 }
512
513 // Unmap buffer if creating a CLTensor
514 unmap(tensor);
515 }
516 catch(const std::ifstream::failure &e)
517 {
518 ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what());
519 }
520 }
521
522private:
523 std::ifstream _fs;
524 std::vector<unsigned long> _shape;
525 bool _fortran_order;
526 std::string _typestring;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100527 DataLayout _file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100528};
529
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100530/** Template helper function to save a tensor image to a PPM file.
531 *
532 * @note Only U8 and RGB888 formats supported.
533 * @note Only works with 2D tensors.
534 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
535 *
536 * @param[in] tensor The tensor to save as PPM file
537 * @param[in] ppm_filename Filename of the file to create.
538 */
539template <typename T>
540void save_to_ppm(T &tensor, const std::string &ppm_filename)
541{
542 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
543 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
544
545 std::ofstream fs;
546
547 try
548 {
549 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
550 fs.open(ppm_filename, std::ios::out | std::ios::binary);
551
552 const unsigned int width = tensor.info()->tensor_shape()[0];
553 const unsigned int height = tensor.info()->tensor_shape()[1];
554
555 fs << "P6\n"
556 << width << " " << height << " 255\n";
557
Anthony Barbier7068f992017-10-26 15:23:08 +0100558 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100559 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100560
561 switch(tensor.info()->format())
562 {
563 case arm_compute::Format::U8:
564 {
565 arm_compute::Window window;
566 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
567 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
568
569 arm_compute::Iterator in(&tensor, window);
570
571 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
572 {
573 const unsigned char value = *in.ptr();
574
575 fs << value << value << value;
576 },
577 in);
578
579 break;
580 }
581 case arm_compute::Format::RGB888:
582 {
583 arm_compute::Window window;
584 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
585 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
586
587 arm_compute::Iterator in(&tensor, window);
588
589 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
590 {
591 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
592 },
593 in);
594
595 break;
596 }
597 default:
598 ARM_COMPUTE_ERROR("Unsupported format");
599 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100600
Anthony Barbier7068f992017-10-26 15:23:08 +0100601 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100602 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100603 }
604 catch(const std::ofstream::failure &e)
605 {
606 ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
607 }
608}
steniu01bee466b2017-06-21 16:45:41 +0100609
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100610/** Template helper function to save a tensor image to a NPY file.
611 *
Gian Marcobfa3b522017-12-12 10:08:38 +0000612 * @note Only F32 data type supported.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100613 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
614 *
615 * @param[in] tensor The tensor to save as NPY file
616 * @param[in] npy_filename Filename of the file to create.
617 * @param[in] fortran_order If true, save matrix in fortran order.
618 */
619template <typename T>
620void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
621{
Gian Marcobfa3b522017-12-12 10:08:38 +0000622 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100623
624 std::ofstream fs;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100625 try
626 {
627 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
628 fs.open(npy_filename, std::ios::out | std::ios::binary);
629
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100630 std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100631
Pablo Tello32521432018-11-15 14:43:10 +0000632 for(unsigned int i = 0, j = tensor.info()->num_dimensions() - 1; i < tensor.info()->num_dimensions(); ++i, --j)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100633 {
Pablo Tello32521432018-11-15 14:43:10 +0000634 shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i];
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100635 }
636
637 // Map buffer if creating a CLTensor
638 map(tensor, true);
639
Gian Marcobfa3b522017-12-12 10:08:38 +0000640 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100641 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000642 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100643 {
644 std::vector<float> tmp; /* Used only to get the typestring */
645 npy::Typestring typestring_o{ tmp };
646 std::string typestring = typestring_o.str();
647
648 std::ofstream stream(npy_filename, std::ofstream::binary);
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000649 npy::write_header(stream, typestring, fortran_order, shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100650
651 arm_compute::Window window;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100652 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100653
654 arm_compute::Iterator in(&tensor, window);
655
656 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
657 {
658 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float));
659 },
660 in);
661
662 break;
663 }
664 default:
665 ARM_COMPUTE_ERROR("Unsupported format");
666 }
667
668 // Unmap buffer if creating a CLTensor
669 unmap(tensor);
670 }
671 catch(const std::ofstream::failure &e)
672 {
673 ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what());
674 }
675}
676
steniu01bee466b2017-06-21 16:45:41 +0100677/** Load the tensor with pre-trained data from a binary file
678 *
679 * @param[in] tensor The tensor to be filled. Data type supported: F32.
680 * @param[in] filename Filename of the binary file to load from.
681 */
682template <typename T>
683void load_trained_data(T &tensor, const std::string &filename)
684{
685 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
686
687 std::ifstream fs;
688
689 try
690 {
691 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
692 // Open file
693 fs.open(filename, std::ios::in | std::ios::binary);
694
695 if(!fs.good())
696 {
697 throw std::runtime_error("Could not load binary data: " + filename);
698 }
699
Anthony Barbier7068f992017-10-26 15:23:08 +0100700 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100701 map(tensor, true);
702
steniu01bee466b2017-06-21 16:45:41 +0100703 Window window;
704
705 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
706
707 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
708 {
709 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
710 }
711
712 arm_compute::Iterator in(&tensor, window);
713
714 execute_window_loop(window, [&](const Coordinates & id)
715 {
716 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
717 },
718 in);
719
Anthony Barbier7068f992017-10-26 15:23:08 +0100720 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100721 unmap(tensor);
steniu01bee466b2017-06-21 16:45:41 +0100722 }
723 catch(const std::ofstream::failure &e)
724 {
725 ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what());
726 }
727}
728
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100729template <typename T>
730void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100731{
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100732 std::random_device rd;
733 std::mt19937 gen(rd());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100734
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100735 Window window;
Michalis Spyrou5e69bb42018-03-09 16:36:00 +0000736 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100737
738 map(tensor, true);
739
740 Iterator it(&tensor, window);
741
Gian Marcobfa3b522017-12-12 10:08:38 +0000742 switch(tensor.info()->data_type())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100743 {
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000744 case arm_compute::DataType::F16:
745 {
746 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
747
748 execute_window_loop(window, [&](const Coordinates & id)
749 {
750 *reinterpret_cast<half *>(it.ptr()) = (half)dist(gen);
751 },
752 it);
753
754 break;
755 }
Gian Marcobfa3b522017-12-12 10:08:38 +0000756 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100757 {
758 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
759
760 execute_window_loop(window, [&](const Coordinates & id)
761 {
762 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
763 },
764 it);
765
766 break;
767 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100768 default:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100769 {
770 ARM_COMPUTE_ERROR("Unsupported format");
771 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100772 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100773
774 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100775}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100776
777template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000778void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100779{
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000780 dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1), src0.info()->dimension(2)), 1, dt));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100781}
Gian Marco5ca74092018-02-08 16:21:54 +0000782/** This function returns the amount of memory free reading from /proc/meminfo
783 *
784 * @return The free memory in kB
785 */
786uint64_t get_mem_free_from_meminfo();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100787
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000788/** Compare two tensors
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100789 *
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000790 * @param[in] tensor1 First tensor to be compared.
791 * @param[in] tensor2 Second tensor to be compared.
792 * @param[in] tolerance Tolerance used for the comparison.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100793 *
794 * @return The number of mismatches
795 */
796template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000797int compare_tensor(ITensor &tensor1, ITensor &tensor2, T tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100798{
799 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
800 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
801
802 int num_mismatches = 0;
803 Window window;
804 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
805
806 map(tensor1, true);
807 map(tensor2, true);
Pablo Tello32521432018-11-15 14:43:10 +0000808
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100809 Iterator itensor1(&tensor1, window);
810 Iterator itensor2(&tensor2, window);
811
812 execute_window_loop(window, [&](const Coordinates & id)
813 {
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000814 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > tolerance)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100815 {
816 ++num_mismatches;
817 }
818 },
819 itensor1, itensor2);
820
821 unmap(itensor1);
822 unmap(itensor2);
823
824 return num_mismatches;
825}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100826} // namespace utils
827} // namespace arm_compute
828#endif /* __UTILS_UTILS_H__*/