blob: 1f3d971917c659c1c3274b84f916d2be9c92d77d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 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 __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"
38#include "arm_compute/runtime/CL/CLTensor.h"
39#endif /* ARM_COMPUTE_CL */
40
41#include <cstdlib>
42#include <cstring>
43#include <fstream>
44#include <iostream>
Giorgio Arenacf3935f2017-10-26 17:14:13 +010045#include <random>
46#include <string>
47#include <tuple>
48#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
50namespace arm_compute
51{
52namespace utils
53{
54/** Signature of an example to run
55 *
56 * @param[in] argc Number of command line arguments
57 * @param[in] argv Command line arguments
58 */
59using example = void(int argc, const char **argv);
60
61/** Run an example and handle the potential exceptions it throws
62 *
63 * @param[in] argc Number of command line arguments
64 * @param[in] argv Command line arguments
65 * @param[in] func Pointer to the function containing the code to run
66 */
67int run_example(int argc, const char **argv, example &func);
68
69/** Draw a RGB rectangular window for the detected object
70 *
71 * @param[in, out] tensor Input tensor where the rectangle will be drawn on. Format supported: RGB888
72 * @param[in] rect Geometry of the rectangular window
73 * @param[in] r Red colour to use
74 * @param[in] g Green colour to use
75 * @param[in] b Blue colour to use
76 */
77void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
78
79/** Parse the ppm header from an input file stream. At the end of the execution,
80 * the file position pointer will be located at the first pixel stored in the ppm file
81 *
82 * @param[in] fs Input file stream to parse
83 *
84 * @return The width, height and max value stored in the header of the PPM file
85 */
86std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
87
Giorgio Arenacf3935f2017-10-26 17:14:13 +010088/** Parse the npy header from an input file stream. At the end of the execution,
89 * the file position pointer will be located at the first pixel stored in the npy file //TODO
90 *
91 * @param[in] fs Input file stream to parse
92 *
93 * @return The width and height stored in the header of the NPY file
94 */
95std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs);
96
97/** Obtain numpy type string from DataType.
98 *
99 * @param[in] data_type Data type.
100 *
101 * @return numpy type string.
102 */
103inline std::string get_typestring(DataType data_type)
104{
105 // Check endianness
106 const unsigned int i = 1;
107 const char *c = reinterpret_cast<const char *>(&i);
108 std::string endianness;
109 if(*c == 1)
110 {
111 endianness = std::string("<");
112 }
113 else
114 {
115 endianness = std::string(">");
116 }
117 const std::string no_endianness("|");
118
119 switch(data_type)
120 {
121 case DataType::U8:
122 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
123 case DataType::S8:
124 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
125 case DataType::U16:
126 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
127 case DataType::S16:
128 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
129 case DataType::U32:
130 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
131 case DataType::S32:
132 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
133 case DataType::U64:
134 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
135 case DataType::S64:
136 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
137 case DataType::F32:
138 return endianness + "f" + support::cpp11::to_string(sizeof(float));
139 case DataType::F64:
140 return endianness + "f" + support::cpp11::to_string(sizeof(double));
141 case DataType::SIZET:
142 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
143 default:
144 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
145 }
146}
147
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100148/** Maps a tensor if needed
149 *
150 * @param[in] tensor Tensor to be mapped
151 * @param[in] blocking Specified if map is blocking or not
152 */
153template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100154inline void map(T &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100155{
156 ARM_COMPUTE_UNUSED(tensor);
157 ARM_COMPUTE_UNUSED(blocking);
158}
159
160/** Unmaps a tensor if needed
161 *
162 * @param tensor Tensor to be unmapped
163 */
164template <typename T>
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100165inline void unmap(T &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100166{
167 ARM_COMPUTE_UNUSED(tensor);
168}
169
170#ifdef ARM_COMPUTE_CL
171/** Maps a tensor if needed
172 *
173 * @param[in] tensor Tensor to be mapped
174 * @param[in] blocking Specified if map is blocking or not
175 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100176inline void map(CLTensor &tensor, bool blocking)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100177{
178 tensor.map(blocking);
179}
180
181/** Unmaps a tensor if needed
182 *
183 * @param tensor Tensor to be unmapped
184 */
Gian Marco Iodiceae27e942017-09-28 18:31:26 +0100185inline void unmap(CLTensor &tensor)
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100186{
187 tensor.unmap();
188}
189#endif /* ARM_COMPUTE_CL */
190
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191/** Class to load the content of a PPM file into an Image
192 */
193class PPMLoader
194{
195public:
196 PPMLoader()
197 : _fs(), _width(0), _height(0)
198 {
199 }
200 /** Open a PPM file and reads its metadata (Width, height)
201 *
202 * @param[in] ppm_filename File to open
203 */
204 void open(const std::string &ppm_filename)
205 {
206 ARM_COMPUTE_ERROR_ON(is_open());
207 try
208 {
209 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
210 _fs.open(ppm_filename, std::ios::in | std::ios::binary);
211
212 unsigned int max_val = 0;
213 std::tie(_width, _height, max_val) = parse_ppm_header(_fs);
214
215 ARM_COMPUTE_ERROR_ON_MSG(max_val >= 256, "2 bytes per colour channel not supported in file %s", ppm_filename.c_str());
216 }
217 catch(const std::ifstream::failure &e)
218 {
219 ARM_COMPUTE_ERROR("Accessing %s: %s", ppm_filename.c_str(), e.what());
220 }
221 }
222 /** Return true if a PPM file is currently open
223 */
224 bool is_open()
225 {
226 return _fs.is_open();
227 }
228
229 /** Initialise an image's metadata with the dimensions of the PPM file currently open
230 *
231 * @param[out] image Image to initialise
232 * @param[in] format Format to use for the image (Must be RGB888 or U8)
233 */
234 template <typename T>
235 void init_image(T &image, arm_compute::Format format)
236 {
237 ARM_COMPUTE_ERROR_ON(!is_open());
238 ARM_COMPUTE_ERROR_ON(format != arm_compute::Format::RGB888 && format != arm_compute::Format::U8);
239
240 // Use the size of the input PPM image
241 arm_compute::TensorInfo image_info(_width, _height, format);
242 image.allocator()->init(image_info);
243 }
244
245 /** Fill an image with the content of the currently open PPM file.
246 *
247 * @note If the image is a CLImage, the function maps and unmaps the image
248 *
249 * @param[in,out] image Image to fill (Must be allocated, and of matching dimensions with the opened PPM).
250 */
251 template <typename T>
252 void fill_image(T &image)
253 {
254 ARM_COMPUTE_ERROR_ON(!is_open());
255 ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height);
256 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&image, arm_compute::Format::U8, arm_compute::Format::RGB888);
257 try
258 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100259 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100260 map(image, true);
261
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 // Check if the file is large enough to fill the image
263 const size_t current_position = _fs.tellg();
264 _fs.seekg(0, std::ios_base::end);
265 const size_t end_position = _fs.tellg();
266 _fs.seekg(current_position, std::ios_base::beg);
267
268 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < image.info()->tensor_shape().total_size() * image.info()->element_size(),
269 "Not enough data in file");
270 ARM_COMPUTE_UNUSED(end_position);
271
272 switch(image.info()->format())
273 {
274 case arm_compute::Format::U8:
275 {
276 // We need to convert the data from RGB to grayscale:
277 // Iterate through every pixel of the image
278 arm_compute::Window window;
279 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, _width, 1));
280 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1));
281
282 arm_compute::Iterator out(&image, window);
283
284 unsigned char red = 0;
285 unsigned char green = 0;
286 unsigned char blue = 0;
287
288 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
289 {
290 red = _fs.get();
291 green = _fs.get();
292 blue = _fs.get();
293
294 *out.ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
295 },
296 out);
297
298 break;
299 }
300 case arm_compute::Format::RGB888:
301 {
302 // There is no format conversion needed: we can simply copy the content of the input file to the image one row at the time.
303 // Create a vertical window to iterate through the image's rows:
304 arm_compute::Window window;
305 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1));
306
307 arm_compute::Iterator out(&image, window);
308
309 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
310 {
311 // Copy one row from the input file to the current row of the image:
312 _fs.read(reinterpret_cast<std::fstream::char_type *>(out.ptr()), _width * image.info()->element_size());
313 },
314 out);
315
316 break;
317 }
318 default:
319 ARM_COMPUTE_ERROR("Unsupported format");
320 }
321
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100323 unmap(image);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 }
325 catch(const std::ifstream::failure &e)
326 {
327 ARM_COMPUTE_ERROR("Loading PPM file: %s", e.what());
328 }
329 }
330
Gian Marco44ec2e72017-10-19 14:13:38 +0100331 /** Fill a tensor with 3 planes (one for each channel) with the content of the currently open PPM file.
332 *
333 * @note If the image is a CLImage, the function maps and unmaps the image
334 *
335 * @param[in,out] tensor Tensor with 3 planes to fill (Must be allocated, and of matching dimensions with the opened PPM). Data types supported: U8/F32
336 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false)
337 */
338 template <typename T>
339 void fill_planar_tensor(T &tensor, bool bgr = false)
340 {
341 ARM_COMPUTE_ERROR_ON(!is_open());
342 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::U8, DataType::F32);
343 ARM_COMPUTE_ERROR_ON(tensor.info()->dimension(0) != _width || tensor.info()->dimension(1) != _height || tensor.info()->dimension(2) != 3);
344
345 try
346 {
347 // Map buffer if creating a CLTensor
348 map(tensor, true);
349
350 // Check if the file is large enough to fill the image
351 const size_t current_position = _fs.tellg();
352 _fs.seekg(0, std::ios_base::end);
353 const size_t end_position = _fs.tellg();
354 _fs.seekg(current_position, std::ios_base::beg);
355
356 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size(),
357 "Not enough data in file");
358 ARM_COMPUTE_UNUSED(end_position);
359
360 // Iterate through every pixel of the image
361 arm_compute::Window window;
362 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, _width, 1));
363 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1));
364 window.set(arm_compute::Window::DimZ, arm_compute::Window::Dimension(0, 1, 1));
365
366 arm_compute::Iterator out(&tensor, window);
367
368 unsigned char red = 0;
369 unsigned char green = 0;
370 unsigned char blue = 0;
371
372 size_t stride_z = tensor.info()->strides_in_bytes()[2];
373
374 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
375 {
376 red = _fs.get();
377 green = _fs.get();
378 blue = _fs.get();
379
380 switch(tensor.info()->data_type())
381 {
382 case arm_compute::DataType::U8:
383 {
384 *(out.ptr() + 0 * stride_z) = bgr ? blue : red;
385 *(out.ptr() + 1 * stride_z) = green;
386 *(out.ptr() + 2 * stride_z) = bgr ? red : blue;
387 break;
388 }
389 case arm_compute::DataType::F32:
390 {
391 *reinterpret_cast<float *>(out.ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red);
392 *reinterpret_cast<float *>(out.ptr() + 1 * stride_z) = static_cast<float>(green);
393 *reinterpret_cast<float *>(out.ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue);
394 break;
395 }
396 default:
397 {
398 ARM_COMPUTE_ERROR("Unsupported data type");
399 }
400 }
401 },
402 out);
403
404 // Unmap buffer if creating a CLTensor
405 unmap(tensor);
406 }
407 catch(const std::ifstream::failure &e)
408 {
409 ARM_COMPUTE_ERROR("Loading PPM file: %s", e.what());
410 }
411 }
412
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100413private:
414 std::ifstream _fs;
415 unsigned int _width, _height;
416};
417
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100418class NPYLoader
419{
420public:
421 NPYLoader()
422 : _fs(), _shape(), _fortran_order(false), _typestring()
423 {
424 }
425
426 /** Open a NPY file and reads its metadata
427 *
428 * @param[in] npy_filename File to open
429 */
430 void open(const std::string &npy_filename)
431 {
432 ARM_COMPUTE_ERROR_ON(is_open());
433 try
434 {
435 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
436 _fs.open(npy_filename, std::ios::in | std::ios::binary);
437
438 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
439 }
440 catch(const std::ifstream::failure &e)
441 {
442 ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what());
443 }
444 }
445 /** Return true if a NPY file is currently open */
446 bool is_open()
447 {
448 return _fs.is_open();
449 }
450
451 /** Return true if a NPY file is in fortran order */
452 bool is_fortran()
453 {
454 return _fortran_order;
455 }
456
457 /** Initialise an image's metadata with the dimensions of the NPY file currently open
458 *
459 * @param[out] tensor Tensor to initialise
460 * @param[in] format Format to use for the image
461 */
462 template <typename T>
463 void init_tensor(T &tensor, arm_compute::Format format)
464 {
465 ARM_COMPUTE_ERROR_ON(!is_open());
466 ARM_COMPUTE_ERROR_ON(format != arm_compute::Format::F32);
467
468 // Use the size of the input NPY tensor
469 TensorShape shape;
470 shape.set_num_dimensions(_shape.size());
471 for(size_t i = 0; i < _shape.size(); ++i)
472 {
473 shape.set(i, _shape.at(i));
474 }
475
476 arm_compute::TensorInfo tensor_info(shape, format);
477 tensor.allocator()->init(tensor_info);
478 }
479
480 /** Fill a tensor with the content of the currently open NPY file.
481 *
482 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
483 *
484 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
485 */
486 template <typename T>
487 void fill_tensor(T &tensor)
488 {
489 ARM_COMPUTE_ERROR_ON(!is_open());
490 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::F32);
491 try
492 {
493 // Map buffer if creating a CLTensor
494 map(tensor, true);
495
496 // Check if the file is large enough to fill the tensor
497 const size_t current_position = _fs.tellg();
498 _fs.seekg(0, std::ios_base::end);
499 const size_t end_position = _fs.tellg();
500 _fs.seekg(current_position, std::ios_base::beg);
501
502 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
503 "Not enough data in file");
504 ARM_COMPUTE_UNUSED(end_position);
505
506 // Check if the typestring matches the given one
507 std::string expect_typestr = get_typestring(tensor.info()->data_type());
508 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
509
510 // Validate tensor shape
511 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch");
512 if(_fortran_order)
513 {
514 for(size_t i = 0; i < _shape.size(); ++i)
515 {
516 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != _shape[i], "Tensor dimensions mismatch");
517 }
518 }
519 else
520 {
521 for(size_t i = 0; i < _shape.size(); ++i)
522 {
523 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != _shape[_shape.size() - i - 1], "Tensor dimensions mismatch");
524 }
525 }
526
527 switch(tensor.info()->format())
528 {
529 case arm_compute::Format::F32:
530 {
531 // Read data
532 if(tensor.info()->padding().empty())
533 {
534 // If tensor has no padding read directly from stream.
535 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
536 }
537 else
538 {
539 // If tensor has padding accessing tensor elements through execution window.
540 Window window;
541 window.use_tensor_dimensions(tensor.info()->tensor_shape());
542
543 execute_window_loop(window, [&](const Coordinates & id)
544 {
545 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size());
546 });
547 }
548
549 break;
550 }
551 default:
552 ARM_COMPUTE_ERROR("Unsupported format");
553 }
554
555 // Unmap buffer if creating a CLTensor
556 unmap(tensor);
557 }
558 catch(const std::ifstream::failure &e)
559 {
560 ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what());
561 }
562 }
563
564private:
565 std::ifstream _fs;
566 std::vector<unsigned long> _shape;
567 bool _fortran_order;
568 std::string _typestring;
569};
570
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100571/** Template helper function to save a tensor image to a PPM file.
572 *
573 * @note Only U8 and RGB888 formats supported.
574 * @note Only works with 2D tensors.
575 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
576 *
577 * @param[in] tensor The tensor to save as PPM file
578 * @param[in] ppm_filename Filename of the file to create.
579 */
580template <typename T>
581void save_to_ppm(T &tensor, const std::string &ppm_filename)
582{
583 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
584 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
585
586 std::ofstream fs;
587
588 try
589 {
590 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
591 fs.open(ppm_filename, std::ios::out | std::ios::binary);
592
593 const unsigned int width = tensor.info()->tensor_shape()[0];
594 const unsigned int height = tensor.info()->tensor_shape()[1];
595
596 fs << "P6\n"
597 << width << " " << height << " 255\n";
598
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100599 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100600 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100601
602 switch(tensor.info()->format())
603 {
604 case arm_compute::Format::U8:
605 {
606 arm_compute::Window window;
607 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
608 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
609
610 arm_compute::Iterator in(&tensor, window);
611
612 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
613 {
614 const unsigned char value = *in.ptr();
615
616 fs << value << value << value;
617 },
618 in);
619
620 break;
621 }
622 case arm_compute::Format::RGB888:
623 {
624 arm_compute::Window window;
625 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
626 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
627
628 arm_compute::Iterator in(&tensor, window);
629
630 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
631 {
632 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
633 },
634 in);
635
636 break;
637 }
638 default:
639 ARM_COMPUTE_ERROR("Unsupported format");
640 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100641
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100642 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100643 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100644 }
645 catch(const std::ofstream::failure &e)
646 {
647 ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
648 }
649}
steniu01bee466b2017-06-21 16:45:41 +0100650
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100651/** Template helper function to save a tensor image to a NPY file.
652 *
653 * @note Only F32 format supported.
654 * @note Only works with 2D tensors.
655 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
656 *
657 * @param[in] tensor The tensor to save as NPY file
658 * @param[in] npy_filename Filename of the file to create.
659 * @param[in] fortran_order If true, save matrix in fortran order.
660 */
661template <typename T>
662void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
663{
664 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::F32);
665 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
666
667 std::ofstream fs;
668
669 try
670 {
671 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
672 fs.open(npy_filename, std::ios::out | std::ios::binary);
673
674 const unsigned int width = tensor.info()->tensor_shape()[0];
675 const unsigned int height = tensor.info()->tensor_shape()[1];
676 unsigned long shape[2];
677
678 if(!fortran_order)
679 {
680 shape[0] = height, shape[1] = width;
681 }
682 else
683 {
684 shape[0] = width, shape[1] = height;
685 }
686
687 // Map buffer if creating a CLTensor
688 map(tensor, true);
689
690 switch(tensor.info()->format())
691 {
692 case arm_compute::Format::F32:
693 {
694 std::vector<float> tmp; /* Used only to get the typestring */
695 npy::Typestring typestring_o{ tmp };
696 std::string typestring = typestring_o.str();
697
698 std::ofstream stream(npy_filename, std::ofstream::binary);
699 npy::WriteHeader(stream, typestring, fortran_order, 2, shape);
700
701 arm_compute::Window window;
702 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
703 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
704
705 arm_compute::Iterator in(&tensor, window);
706
707 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
708 {
709 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float));
710 },
711 in);
712
713 break;
714 }
715 default:
716 ARM_COMPUTE_ERROR("Unsupported format");
717 }
718
719 // Unmap buffer if creating a CLTensor
720 unmap(tensor);
721 }
722 catch(const std::ofstream::failure &e)
723 {
724 ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what());
725 }
726}
727
steniu01bee466b2017-06-21 16:45:41 +0100728/** Load the tensor with pre-trained data from a binary file
729 *
730 * @param[in] tensor The tensor to be filled. Data type supported: F32.
731 * @param[in] filename Filename of the binary file to load from.
732 */
733template <typename T>
734void load_trained_data(T &tensor, const std::string &filename)
735{
736 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
737
738 std::ifstream fs;
739
740 try
741 {
742 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
743 // Open file
744 fs.open(filename, std::ios::in | std::ios::binary);
745
746 if(!fs.good())
747 {
748 throw std::runtime_error("Could not load binary data: " + filename);
749 }
750
steniu01bee466b2017-06-21 16:45:41 +0100751 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100752 map(tensor, true);
753
steniu01bee466b2017-06-21 16:45:41 +0100754 Window window;
755
756 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
757
758 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
759 {
760 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
761 }
762
763 arm_compute::Iterator in(&tensor, window);
764
765 execute_window_loop(window, [&](const Coordinates & id)
766 {
767 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
768 },
769 in);
770
771#ifdef ARM_COMPUTE_CL
772 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100773 unmap(tensor);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100774#endif /* ARM_COMPUTE_CL */
steniu01bee466b2017-06-21 16:45:41 +0100775 }
776 catch(const std::ofstream::failure &e)
777 {
778 ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what());
779 }
780}
781
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100782template <typename T>
783void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100784{
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100785 std::random_device rd;
786 std::mt19937 gen(rd());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100787
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100788 TensorShape shape(tensor.info()->dimension(0), tensor.info()->dimension(1));
789
790 Window window;
791 window.set(Window::DimX, Window::Dimension(0, shape.x(), 1));
792 window.set(Window::DimY, Window::Dimension(0, shape.y(), 1));
793
794 map(tensor, true);
795
796 Iterator it(&tensor, window);
797
798 switch(tensor.info()->format())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100799 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100800 case arm_compute::Format::F32:
801 {
802 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
803
804 execute_window_loop(window, [&](const Coordinates & id)
805 {
806 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
807 },
808 it);
809
810 break;
811 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100812 default:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100813 {
814 ARM_COMPUTE_ERROR("Unsupported format");
815 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100816 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100817
818 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100819}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100820
821template <typename T>
822void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::Format format)
823{
824 dst.allocator()->init(TensorInfo(src1.info()->dimension(0), src0.info()->dimension(1), format));
825}
826
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100827} // namespace utils
828} // namespace arm_compute
829#endif /* __UTILS_UTILS_H__*/