blob: 28382f47e4ca2197b6bd732202318db192ba99cc [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
Isabella Gottardia4c61882017-11-03 12:11:55 +0000413 /** Return the width of the currently open PPM file.
414 */
415 unsigned int width() const
416 {
417 return _width;
418 }
419
420 /** Return the height of the currently open PPM file.
421 */
422 unsigned int height() const
423 {
424 return _height;
425 }
426
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100427private:
428 std::ifstream _fs;
429 unsigned int _width, _height;
430};
431
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100432class NPYLoader
433{
434public:
435 NPYLoader()
436 : _fs(), _shape(), _fortran_order(false), _typestring()
437 {
438 }
439
440 /** Open a NPY file and reads its metadata
441 *
442 * @param[in] npy_filename File to open
443 */
444 void open(const std::string &npy_filename)
445 {
446 ARM_COMPUTE_ERROR_ON(is_open());
447 try
448 {
449 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
450 _fs.open(npy_filename, std::ios::in | std::ios::binary);
451
452 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
453 }
454 catch(const std::ifstream::failure &e)
455 {
456 ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what());
457 }
458 }
459 /** Return true if a NPY file is currently open */
460 bool is_open()
461 {
462 return _fs.is_open();
463 }
464
465 /** Return true if a NPY file is in fortran order */
466 bool is_fortran()
467 {
468 return _fortran_order;
469 }
470
471 /** Initialise an image's metadata with the dimensions of the NPY file currently open
472 *
473 * @param[out] tensor Tensor to initialise
474 * @param[in] format Format to use for the image
475 */
476 template <typename T>
477 void init_tensor(T &tensor, arm_compute::Format format)
478 {
479 ARM_COMPUTE_ERROR_ON(!is_open());
480 ARM_COMPUTE_ERROR_ON(format != arm_compute::Format::F32);
481
482 // Use the size of the input NPY tensor
483 TensorShape shape;
484 shape.set_num_dimensions(_shape.size());
485 for(size_t i = 0; i < _shape.size(); ++i)
486 {
487 shape.set(i, _shape.at(i));
488 }
489
490 arm_compute::TensorInfo tensor_info(shape, format);
491 tensor.allocator()->init(tensor_info);
492 }
493
494 /** Fill a tensor with the content of the currently open NPY file.
495 *
496 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
497 *
498 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
499 */
500 template <typename T>
501 void fill_tensor(T &tensor)
502 {
503 ARM_COMPUTE_ERROR_ON(!is_open());
504 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::F32);
505 try
506 {
507 // Map buffer if creating a CLTensor
508 map(tensor, true);
509
510 // Check if the file is large enough to fill the tensor
511 const size_t current_position = _fs.tellg();
512 _fs.seekg(0, std::ios_base::end);
513 const size_t end_position = _fs.tellg();
514 _fs.seekg(current_position, std::ios_base::beg);
515
516 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
517 "Not enough data in file");
518 ARM_COMPUTE_UNUSED(end_position);
519
520 // Check if the typestring matches the given one
521 std::string expect_typestr = get_typestring(tensor.info()->data_type());
522 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
523
524 // Validate tensor shape
525 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.shape().num_dimensions(), "Tensor ranks mismatch");
526 if(_fortran_order)
527 {
528 for(size_t i = 0; i < _shape.size(); ++i)
529 {
530 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != _shape[i], "Tensor dimensions mismatch");
531 }
532 }
533 else
534 {
535 for(size_t i = 0; i < _shape.size(); ++i)
536 {
537 ARM_COMPUTE_ERROR_ON_MSG(tensor.shape()[i] != _shape[_shape.size() - i - 1], "Tensor dimensions mismatch");
538 }
539 }
540
541 switch(tensor.info()->format())
542 {
543 case arm_compute::Format::F32:
544 {
545 // Read data
546 if(tensor.info()->padding().empty())
547 {
548 // If tensor has no padding read directly from stream.
549 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
550 }
551 else
552 {
553 // If tensor has padding accessing tensor elements through execution window.
554 Window window;
555 window.use_tensor_dimensions(tensor.info()->tensor_shape());
556
557 execute_window_loop(window, [&](const Coordinates & id)
558 {
559 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size());
560 });
561 }
562
563 break;
564 }
565 default:
566 ARM_COMPUTE_ERROR("Unsupported format");
567 }
568
569 // Unmap buffer if creating a CLTensor
570 unmap(tensor);
571 }
572 catch(const std::ifstream::failure &e)
573 {
574 ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what());
575 }
576 }
577
578private:
579 std::ifstream _fs;
580 std::vector<unsigned long> _shape;
581 bool _fortran_order;
582 std::string _typestring;
583};
584
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100585/** Template helper function to save a tensor image to a PPM file.
586 *
587 * @note Only U8 and RGB888 formats supported.
588 * @note Only works with 2D tensors.
589 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
590 *
591 * @param[in] tensor The tensor to save as PPM file
592 * @param[in] ppm_filename Filename of the file to create.
593 */
594template <typename T>
595void save_to_ppm(T &tensor, const std::string &ppm_filename)
596{
597 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
598 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
599
600 std::ofstream fs;
601
602 try
603 {
604 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
605 fs.open(ppm_filename, std::ios::out | std::ios::binary);
606
607 const unsigned int width = tensor.info()->tensor_shape()[0];
608 const unsigned int height = tensor.info()->tensor_shape()[1];
609
610 fs << "P6\n"
611 << width << " " << height << " 255\n";
612
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100613 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100614 map(tensor, true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100615
616 switch(tensor.info()->format())
617 {
618 case arm_compute::Format::U8:
619 {
620 arm_compute::Window window;
621 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
622 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
623
624 arm_compute::Iterator in(&tensor, window);
625
626 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
627 {
628 const unsigned char value = *in.ptr();
629
630 fs << value << value << value;
631 },
632 in);
633
634 break;
635 }
636 case arm_compute::Format::RGB888:
637 {
638 arm_compute::Window window;
639 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
640 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
641
642 arm_compute::Iterator in(&tensor, window);
643
644 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
645 {
646 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
647 },
648 in);
649
650 break;
651 }
652 default:
653 ARM_COMPUTE_ERROR("Unsupported format");
654 }
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100655
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100656 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100657 unmap(tensor);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100658 }
659 catch(const std::ofstream::failure &e)
660 {
661 ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
662 }
663}
steniu01bee466b2017-06-21 16:45:41 +0100664
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100665/** Template helper function to save a tensor image to a NPY file.
666 *
667 * @note Only F32 format supported.
668 * @note Only works with 2D tensors.
669 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
670 *
671 * @param[in] tensor The tensor to save as NPY file
672 * @param[in] npy_filename Filename of the file to create.
673 * @param[in] fortran_order If true, save matrix in fortran order.
674 */
675template <typename T>
676void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
677{
678 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::F32);
679 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
680
681 std::ofstream fs;
682
683 try
684 {
685 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
686 fs.open(npy_filename, std::ios::out | std::ios::binary);
687
688 const unsigned int width = tensor.info()->tensor_shape()[0];
689 const unsigned int height = tensor.info()->tensor_shape()[1];
690 unsigned long shape[2];
691
692 if(!fortran_order)
693 {
694 shape[0] = height, shape[1] = width;
695 }
696 else
697 {
698 shape[0] = width, shape[1] = height;
699 }
700
701 // Map buffer if creating a CLTensor
702 map(tensor, true);
703
704 switch(tensor.info()->format())
705 {
706 case arm_compute::Format::F32:
707 {
708 std::vector<float> tmp; /* Used only to get the typestring */
709 npy::Typestring typestring_o{ tmp };
710 std::string typestring = typestring_o.str();
711
712 std::ofstream stream(npy_filename, std::ofstream::binary);
713 npy::WriteHeader(stream, typestring, fortran_order, 2, shape);
714
715 arm_compute::Window window;
716 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
717 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
718
719 arm_compute::Iterator in(&tensor, window);
720
721 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
722 {
723 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float));
724 },
725 in);
726
727 break;
728 }
729 default:
730 ARM_COMPUTE_ERROR("Unsupported format");
731 }
732
733 // Unmap buffer if creating a CLTensor
734 unmap(tensor);
735 }
736 catch(const std::ofstream::failure &e)
737 {
738 ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what());
739 }
740}
741
steniu01bee466b2017-06-21 16:45:41 +0100742/** Load the tensor with pre-trained data from a binary file
743 *
744 * @param[in] tensor The tensor to be filled. Data type supported: F32.
745 * @param[in] filename Filename of the binary file to load from.
746 */
747template <typename T>
748void load_trained_data(T &tensor, const std::string &filename)
749{
750 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
751
752 std::ifstream fs;
753
754 try
755 {
756 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
757 // Open file
758 fs.open(filename, std::ios::in | std::ios::binary);
759
760 if(!fs.good())
761 {
762 throw std::runtime_error("Could not load binary data: " + filename);
763 }
764
steniu01bee466b2017-06-21 16:45:41 +0100765 // Map buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100766 map(tensor, true);
767
steniu01bee466b2017-06-21 16:45:41 +0100768 Window window;
769
770 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
771
772 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
773 {
774 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
775 }
776
777 arm_compute::Iterator in(&tensor, window);
778
779 execute_window_loop(window, [&](const Coordinates & id)
780 {
781 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
782 },
783 in);
784
785#ifdef ARM_COMPUTE_CL
786 // Unmap buffer if creating a CLTensor
Georgios Pinitasdc836b62017-09-20 14:02:37 +0100787 unmap(tensor);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100788#endif /* ARM_COMPUTE_CL */
steniu01bee466b2017-06-21 16:45:41 +0100789 }
790 catch(const std::ofstream::failure &e)
791 {
792 ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what());
793 }
794}
795
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100796template <typename T>
797void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100798{
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100799 std::random_device rd;
800 std::mt19937 gen(rd());
Anthony Barbier2a07e182017-08-04 18:20:27 +0100801
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100802 TensorShape shape(tensor.info()->dimension(0), tensor.info()->dimension(1));
803
804 Window window;
805 window.set(Window::DimX, Window::Dimension(0, shape.x(), 1));
806 window.set(Window::DimY, Window::Dimension(0, shape.y(), 1));
807
808 map(tensor, true);
809
810 Iterator it(&tensor, window);
811
812 switch(tensor.info()->format())
Anthony Barbier2a07e182017-08-04 18:20:27 +0100813 {
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100814 case arm_compute::Format::F32:
815 {
816 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
817
818 execute_window_loop(window, [&](const Coordinates & id)
819 {
820 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
821 },
822 it);
823
824 break;
825 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100826 default:
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100827 {
828 ARM_COMPUTE_ERROR("Unsupported format");
829 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100830 }
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100831
832 unmap(tensor);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100833}
Giorgio Arenacf3935f2017-10-26 17:14:13 +0100834
835template <typename T>
836void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::Format format)
837{
838 dst.allocator()->init(TensorInfo(src1.info()->dimension(0), src0.info()->dimension(1), format));
839}
840
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100841} // namespace utils
842} // namespace arm_compute
843#endif /* __UTILS_UTILS_H__*/