blob: adb0e54f543ad9fa5de3b0fe3c2920fd5d60fd40 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arenaa66eaa22017-12-21 19:50:06 +00002 * Copyright (c) 2016-2018 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:
171 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
172 case DataType::U16:
173 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
174 case DataType::S16:
175 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
176 case DataType::U32:
177 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
178 case DataType::S32:
179 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
180 case DataType::U64:
181 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
182 case DataType::S64:
183 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
184 case DataType::F32:
185 return endianness + "f" + support::cpp11::to_string(sizeof(float));
186 case DataType::F64:
187 return endianness + "f" + support::cpp11::to_string(sizeof(double));
188 case DataType::SIZET:
189 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
190 default:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100191 ARM_COMPUTE_ERROR("Data type not supported");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100192 }
193}
194
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100195/** Maps a tensor if needed
196 *
197 * @param[in] tensor Tensor to be mapped
198 * @param[in] blocking Specified if map is blocking or not
199 */
200template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100201inline void map(T &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100202{
203 ARM_COMPUTE_UNUSED(tensor);
204 ARM_COMPUTE_UNUSED(blocking);
205}
206
207/** Unmaps a tensor if needed
208 *
209 * @param tensor Tensor to be unmapped
210 */
211template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100212inline void unmap(T &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100213{
214 ARM_COMPUTE_UNUSED(tensor);
215}
216
217#ifdef ARM_COMPUTE_CL
218/** Maps a tensor if needed
219 *
220 * @param[in] tensor Tensor to be mapped
221 * @param[in] blocking Specified if map is blocking or not
222 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100223inline void map(CLTensor &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100224{
225 tensor.map(blocking);
226}
227
228/** Unmaps a tensor if needed
229 *
230 * @param tensor Tensor to be unmapped
231 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100232inline void unmap(CLTensor &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100233{
234 tensor.unmap();
235}
Isabella Gottardi02aabcc2017-10-12 17:28:51 +0100236
237/** Maps a distribution if needed
238 *
239 * @param[in] distribution Distribution to be mapped
240 * @param[in] blocking Specified if map is blocking or not
241 */
242inline void map(CLDistribution1D &distribution, bool blocking)
243{
244 distribution.map(blocking);
245}
246
247/** Unmaps a distribution if needed
248 *
249 * @param distribution Distribution to be unmapped
250 */
251inline void unmap(CLDistribution1D &distribution)
252{
253 distribution.unmap();
254}
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100255#endif /* ARM_COMPUTE_CL */
256
Anthony Barbier7068f992017-10-26 15:23:08 +0100257#ifdef ARM_COMPUTE_GC
258/** Maps a tensor if needed
259 *
260 * @param[in] tensor Tensor to be mapped
261 * @param[in] blocking Specified if map is blocking or not
262 */
263inline void map(GCTensor &tensor, bool blocking)
264{
265 tensor.map(blocking);
266}
267
268/** Unmaps a tensor if needed
269 *
270 * @param tensor Tensor to be unmapped
271 */
272inline void unmap(GCTensor &tensor)
273{
274 tensor.unmap();
275}
276#endif /* ARM_COMPUTE_GC */
277
Alex Gildayc357c472018-03-21 13:54:09 +0000278/** Numpy data loader */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100279class NPYLoader
280{
281public:
Alex Gildayc357c472018-03-21 13:54:09 +0000282 /** Default constructor */
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100283 NPYLoader()
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100284 : _fs(), _shape(), _fortran_order(false), _typestring(), _file_layout(DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100285 {
286 }
287
288 /** Open a NPY file and reads its metadata
289 *
290 * @param[in] npy_filename File to open
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100291 * @param[in] file_layout (Optional) Layout in which the weights are stored in the file.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100292 */
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100293 void open(const std::string &npy_filename, DataLayout file_layout = DataLayout::NCHW)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100294 {
295 ARM_COMPUTE_ERROR_ON(is_open());
296 try
297 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100298 _fs.open(npy_filename, std::ios::in | std::ios::binary);
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100299 ARM_COMPUTE_EXIT_ON_MSG(!_fs.good(), "Failed to load binary data from %s", npy_filename.c_str());
300 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
301 _file_layout = file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100302
303 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
304 }
305 catch(const std::ifstream::failure &e)
306 {
307 ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what());
308 }
309 }
310 /** Return true if a NPY file is currently open */
311 bool is_open()
312 {
313 return _fs.is_open();
314 }
315
316 /** Return true if a NPY file is in fortran order */
317 bool is_fortran()
318 {
319 return _fortran_order;
320 }
321
Gian Marco0bc5a252017-12-04 13:55:08 +0000322 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100323 *
324 * @param[out] tensor Tensor to initialise
Gian Marco0bc5a252017-12-04 13:55:08 +0000325 * @param[in] dt Data type to use for the tensor
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100326 */
327 template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000328 void init_tensor(T &tensor, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100329 {
330 ARM_COMPUTE_ERROR_ON(!is_open());
Gian Marco0bc5a252017-12-04 13:55:08 +0000331 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100332
333 // Use the size of the input NPY tensor
334 TensorShape shape;
335 shape.set_num_dimensions(_shape.size());
336 for(size_t i = 0; i < _shape.size(); ++i)
337 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100338 size_t src = i;
339 if(_fortran_order)
340 {
341 src = _shape.size() - 1 - i;
342 }
343 shape.set(i, _shape.at(src));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100344 }
345
Gian Marco0bc5a252017-12-04 13:55:08 +0000346 arm_compute::TensorInfo tensor_info(shape, 1, dt);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100347 tensor.allocator()->init(tensor_info);
348 }
349
350 /** Fill a tensor with the content of the currently open NPY file.
351 *
352 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
353 *
354 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
355 */
356 template <typename T>
357 void fill_tensor(T &tensor)
358 {
359 ARM_COMPUTE_ERROR_ON(!is_open());
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000360 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100361 try
362 {
363 // Map buffer if creating a CLTensor
364 map(tensor, true);
365
366 // Check if the file is large enough to fill the tensor
367 const size_t current_position = _fs.tellg();
368 _fs.seekg(0, std::ios_base::end);
369 const size_t end_position = _fs.tellg();
370 _fs.seekg(current_position, std::ios_base::beg);
371
372 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
373 "Not enough data in file");
374 ARM_COMPUTE_UNUSED(end_position);
375
376 // Check if the typestring matches the given one
377 std::string expect_typestr = get_typestring(tensor.info()->data_type());
378 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
379
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100380 bool are_layouts_different = (_file_layout != tensor.info()->data_layout());
381 // Correct dimensions (Needs to match TensorShape dimension corrections)
382 if(_shape.size() != tensor.info()->tensor_shape().num_dimensions())
383 {
384 for(int i = static_cast<int>(_shape.size()) - 1; i > 0; --i)
385 {
386 if(_shape[i] == 1)
387 {
388 _shape.pop_back();
389 }
390 else
391 {
392 break;
393 }
394 }
395 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100396 // Validate tensor shape
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000397 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100398 if(!_fortran_order)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100399 {
400 for(size_t i = 0; i < _shape.size(); ++i)
401 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000402 ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[i], "Tensor dimensions mismatch");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100403 }
404 }
405 else
406 {
407 for(size_t i = 0; i < _shape.size(); ++i)
408 {
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000409 ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[_shape.size() - i - 1], "Tensor dimensions mismatch");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100410 }
411 }
412
Gian Marco0bc5a252017-12-04 13:55:08 +0000413 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100414 {
Gian Marco0bc5a252017-12-04 13:55:08 +0000415 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100416 {
417 // Read data
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100418 if(!are_layouts_different && !_fortran_order && tensor.info()->padding().empty())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100419 {
420 // If tensor has no padding read directly from stream.
421 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
422 }
423 else
424 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100425 // If tensor has padding or is in fortran order accessing tensor elements through execution window.
426 Window window;
427 TensorShape permuted_shape = tensor.info()->tensor_shape();
428 const unsigned int num_dims = _shape.size();
429 arm_compute::PermutationVector perm;
430 if(_fortran_order)
431 {
432 for(unsigned int dim = 0; dim < num_dims; dim++)
433 {
434 permuted_shape.set(dim, _shape[dim]);
435 perm.set(dim, num_dims - dim - 1);
436 }
437 }
438 else
439 {
440 for(unsigned int dim = 0; dim < num_dims; dim++)
441 {
442 perm.set(dim, dim);
443 }
444 }
445 if(are_layouts_different)
446 {
447 // Permute only if num_dimensions greater than 2
448 if(num_dims > 2)
449 {
450 if(_file_layout == DataLayout::NHWC) // i.e destination is NCHW --> permute(1,2,0)
451 {
452 size_t perm_0 = perm[0];
453 perm[0] = perm[1];
454 perm[1] = perm[2];
455 perm[2] = perm_0;
456 }
457 else
458 {
459 // destination layout is NHWC --> permute (2,0,1)
460 size_t perm_0 = perm[0];
461 perm[0] = perm[2];
462 perm[2] = perm[1];
463 perm[1] = perm_0;
464 }
465 }
466 }
467
468 window.use_tensor_dimensions(permuted_shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100469
470 execute_window_loop(window, [&](const Coordinates & id)
471 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100472 Coordinates dst(id);
473 arm_compute::permute(dst, perm);
474 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(dst)), tensor.info()->element_size());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100475 });
476 }
477
478 break;
479 }
480 default:
Gian Marco0bc5a252017-12-04 13:55:08 +0000481 ARM_COMPUTE_ERROR("Unsupported data type");
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100482 }
483
484 // Unmap buffer if creating a CLTensor
485 unmap(tensor);
486 }
487 catch(const std::ifstream::failure &e)
488 {
489 ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what());
490 }
491 }
492
493private:
494 std::ifstream _fs;
495 std::vector<unsigned long> _shape;
496 bool _fortran_order;
497 std::string _typestring;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100498 DataLayout _file_layout;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100499};
500
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501/** Template helper function to save a tensor image to a PPM file.
502 *
503 * @note Only U8 and RGB888 formats supported.
504 * @note Only works with 2D tensors.
505 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
506 *
507 * @param[in] tensor The tensor to save as PPM file
508 * @param[in] ppm_filename Filename of the file to create.
509 */
510template <typename T>
511void save_to_ppm(T &tensor, const std::string &ppm_filename)
512{
513 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
514 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
515
516 std::ofstream fs;
517
518 try
519 {
520 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
521 fs.open(ppm_filename, std::ios::out | std::ios::binary);
522
523 const unsigned int width = tensor.info()->tensor_shape()[0];
524 const unsigned int height = tensor.info()->tensor_shape()[1];
525
526 fs << "P6\n"
527 << width << " " << height << " 255\n";
528
Anthony Barbier7068f992017-10-26 15:23:08 +0100529 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100530 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100531
532 switch(tensor.info()->format())
533 {
534 case arm_compute::Format::U8:
535 {
536 arm_compute::Window window;
537 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
538 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
539
540 arm_compute::Iterator in(&tensor, window);
541
542 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
543 {
544 const unsigned char value = *in.ptr();
545
546 fs << value << value << value;
547 },
548 in);
549
550 break;
551 }
552 case arm_compute::Format::RGB888:
553 {
554 arm_compute::Window window;
555 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
556 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
557
558 arm_compute::Iterator in(&tensor, window);
559
560 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
561 {
562 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
563 },
564 in);
565
566 break;
567 }
568 default:
569 ARM_COMPUTE_ERROR("Unsupported format");
570 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100571
Anthony Barbier7068f992017-10-26 15:23:08 +0100572 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100573 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100574 }
575 catch(const std::ofstream::failure &e)
576 {
577 ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
578 }
579}
steniu01bee466b2017-06-21 16:45:41 +0100580
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100581/** Template helper function to save a tensor image to a NPY file.
582 *
Gian Marcobfa3b522017-12-12 10:08:38 +0000583 * @note Only F32 data type supported.
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100584 * @note Only works with 2D tensors.
585 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
586 *
587 * @param[in] tensor The tensor to save as NPY file
588 * @param[in] npy_filename Filename of the file to create.
589 * @param[in] fortran_order If true, save matrix in fortran order.
590 */
591template <typename T>
592void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
593{
Gian Marcobfa3b522017-12-12 10:08:38 +0000594 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100595
596 std::ofstream fs;
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100597 try
598 {
599 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
600 fs.open(npy_filename, std::ios::out | std::ios::binary);
601
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100602 std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100603
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100604 for(unsigned int i = 0; i < tensor.info()->num_dimensions(); ++i)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100605 {
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100606 shape[i] = tensor.info()->tensor_shape()[i];
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100607 }
608
609 // Map buffer if creating a CLTensor
610 map(tensor, true);
611
Gian Marcobfa3b522017-12-12 10:08:38 +0000612 switch(tensor.info()->data_type())
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100613 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000614 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100615 {
616 std::vector<float> tmp; /* Used only to get the typestring */
617 npy::Typestring typestring_o{ tmp };
618 std::string typestring = typestring_o.str();
619
620 std::ofstream stream(npy_filename, std::ofstream::binary);
Anthony Barbier87f21cd2017-11-10 16:27:32 +0000621 npy::write_header(stream, typestring, fortran_order, shape);
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100622
623 arm_compute::Window window;
Anthony Barbier4ead11a2018-08-06 09:25:36 +0100624 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100625
626 arm_compute::Iterator in(&tensor, window);
627
628 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
629 {
630 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float));
631 },
632 in);
633
634 break;
635 }
636 default:
637 ARM_COMPUTE_ERROR("Unsupported format");
638 }
639
640 // Unmap buffer if creating a CLTensor
641 unmap(tensor);
642 }
643 catch(const std::ofstream::failure &e)
644 {
645 ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what());
646 }
647}
648
steniu01bee466b2017-06-21 16:45:41 +0100649/** Load the tensor with pre-trained data from a binary file
650 *
651 * @param[in] tensor The tensor to be filled. Data type supported: F32.
652 * @param[in] filename Filename of the binary file to load from.
653 */
654template <typename T>
655void load_trained_data(T &tensor, const std::string &filename)
656{
657 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
658
659 std::ifstream fs;
660
661 try
662 {
663 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
664 // Open file
665 fs.open(filename, std::ios::in | std::ios::binary);
666
667 if(!fs.good())
668 {
669 throw std::runtime_error("Could not load binary data: " + filename);
670 }
671
Anthony Barbier7068f992017-10-26 15:23:08 +0100672 // Map buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100673 map(tensor, true);
674
steniu01bee466b2017-06-21 16:45:41 +0100675 Window window;
676
677 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
678
679 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
680 {
681 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
682 }
683
684 arm_compute::Iterator in(&tensor, window);
685
686 execute_window_loop(window, [&](const Coordinates & id)
687 {
688 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
689 },
690 in);
691
Anthony Barbier7068f992017-10-26 15:23:08 +0100692 // Unmap buffer if creating a CLTensor/GCTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100693 unmap(tensor);
steniu01bee466b2017-06-21 16:45:41 +0100694 }
695 catch(const std::ofstream::failure &e)
696 {
697 ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what());
698 }
699}
700
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100701template <typename T>
702void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100703{
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100704 std::random_device rd;
705 std::mt19937 gen(rd());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100706
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100707 Window window;
Michalis Spyrou5e69bb42018-03-09 16:36:00 +0000708 window.use_tensor_dimensions(tensor.info()->tensor_shape());
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100709
710 map(tensor, true);
711
712 Iterator it(&tensor, window);
713
Gian Marcobfa3b522017-12-12 10:08:38 +0000714 switch(tensor.info()->data_type())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100715 {
Gian Marcobfa3b522017-12-12 10:08:38 +0000716 case arm_compute::DataType::F32:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100717 {
718 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
719
720 execute_window_loop(window, [&](const Coordinates & id)
721 {
722 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
723 },
724 it);
725
726 break;
727 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100728 default:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100729 {
730 ARM_COMPUTE_ERROR("Unsupported format");
731 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100732 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100733
734 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100735}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100736
737template <typename T>
Gian Marco0bc5a252017-12-04 13:55:08 +0000738void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100739{
Gian Marco0bc5a252017-12-04 13:55:08 +0000740 dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1)), 1, dt));
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100741}
Gian Marco5ca74092018-02-08 16:21:54 +0000742/** This function returns the amount of memory free reading from /proc/meminfo
743 *
744 * @return The free memory in kB
745 */
746uint64_t get_mem_free_from_meminfo();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100747
748/** Compare to tensor
749 *
750 * @param[in] tensor1 First tensor to be compared.
751 * @param[in] tensor2 Second tensor to be compared.
752 *
753 * @return The number of mismatches
754 */
755template <typename T>
756int compare_tensor(ITensor &tensor1, ITensor &tensor2)
757{
758 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
759 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
760
761 int num_mismatches = 0;
762 Window window;
763 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
764
765 map(tensor1, true);
766 map(tensor2, true);
767 Iterator itensor1(&tensor1, window);
768 Iterator itensor2(&tensor2, window);
769
770 execute_window_loop(window, [&](const Coordinates & id)
771 {
772 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > 0.00001)
773 {
774 ++num_mismatches;
775 }
776 },
777 itensor1, itensor2);
778
779 unmap(itensor1);
780 unmap(itensor2);
781
782 return num_mismatches;
783}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100784} // namespace utils
785} // namespace arm_compute
786#endif /* __UTILS_UTILS_H__*/