2 * Copyright (c) 2016-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef __UTILS_UTILS_H__
25 #define __UTILS_UTILS_H__
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"
31 #include "arm_compute/core/Window.h"
32 #include "arm_compute/runtime/Tensor.h"
33 #include "libnpy/npy.hpp"
34 #include "support/ToolchainSupport.h"
37 #include "arm_compute/core/CL/OpenCL.h"
38 #include "arm_compute/runtime/CL/CLDistribution1D.h"
39 #include "arm_compute/runtime/CL/CLTensor.h"
40 #endif /* ARM_COMPUTE_CL */
42 #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
43 #endif /* ARM_COMPUTE_GC */
58 /** Abstract Example class.
60 * All examples have to inherit from this class.
65 /** Setup the example.
67 * @param[in] argc Argument count.
68 * @param[in] argv Argument values.
70 virtual void do_setup(int argc, char **argv) {};
71 /** Run the example. */
72 virtual void do_run() {};
73 /** Teardown the example. */
74 virtual void do_teardown() {};
76 /** Default destructor. */
77 virtual ~Example() = default;
80 /** Run an example and handle the potential exceptions it throws
82 * @param[in] argc Number of command line arguments
83 * @param[in] argv Command line arguments
84 * @param[in] example Example to run
86 int run_example(int argc, char **argv, std::unique_ptr<Example> example);
89 int run_example(int argc, char **argv)
91 return run_example(argc, argv, support::cpp14::make_unique<T>());
94 /** Draw a RGB rectangular window for the detected object
96 * @param[in, out] tensor Input tensor where the rectangle will be drawn on. Format supported: RGB888
97 * @param[in] rect Geometry of the rectangular window
98 * @param[in] r Red colour to use
99 * @param[in] g Green colour to use
100 * @param[in] b Blue colour to use
102 void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b);
104 /** Parse the ppm header from an input file stream. At the end of the execution,
105 * the file position pointer will be located at the first pixel stored in the ppm file
107 * @param[in] fs Input file stream to parse
109 * @return The width, height and max value stored in the header of the PPM file
111 std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs);
113 /** Parse the npy header from an input file stream. At the end of the execution,
114 * the file position pointer will be located at the first pixel stored in the npy file
116 * @param[in] fs Input file stream to parse
118 * @return The width and height stored in the header of the NPY file
120 std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs);
122 /** Obtain numpy type string from DataType.
124 * @param[in] data_type Data type.
126 * @return numpy type string.
128 inline std::string get_typestring(DataType data_type)
131 const unsigned int i = 1;
132 const char *c = reinterpret_cast<const char *>(&i);
133 std::string endianness;
136 endianness = std::string("<");
140 endianness = std::string(">");
142 const std::string no_endianness("|");
147 case DataType::QASYMM8:
148 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
150 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
152 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
154 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
156 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
158 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
160 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
162 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
164 return endianness + "f" + support::cpp11::to_string(sizeof(float));
166 return endianness + "f" + support::cpp11::to_string(sizeof(double));
167 case DataType::SIZET:
168 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
170 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
174 /** Maps a tensor if needed
176 * @param[in] tensor Tensor to be mapped
177 * @param[in] blocking Specified if map is blocking or not
179 template <typename T>
180 inline void map(T &tensor, bool blocking)
182 ARM_COMPUTE_UNUSED(tensor);
183 ARM_COMPUTE_UNUSED(blocking);
186 /** Unmaps a tensor if needed
188 * @param tensor Tensor to be unmapped
190 template <typename T>
191 inline void unmap(T &tensor)
193 ARM_COMPUTE_UNUSED(tensor);
196 #ifdef ARM_COMPUTE_CL
197 /** Maps a tensor if needed
199 * @param[in] tensor Tensor to be mapped
200 * @param[in] blocking Specified if map is blocking or not
202 inline void map(CLTensor &tensor, bool blocking)
204 tensor.map(blocking);
207 /** Unmaps a tensor if needed
209 * @param tensor Tensor to be unmapped
211 inline void unmap(CLTensor &tensor)
216 /** Maps a distribution if needed
218 * @param[in] distribution Distribution to be mapped
219 * @param[in] blocking Specified if map is blocking or not
221 inline void map(CLDistribution1D &distribution, bool blocking)
223 distribution.map(blocking);
226 /** Unmaps a distribution if needed
228 * @param distribution Distribution to be unmapped
230 inline void unmap(CLDistribution1D &distribution)
232 distribution.unmap();
234 #endif /* ARM_COMPUTE_CL */
236 #ifdef ARM_COMPUTE_GC
237 /** Maps a tensor if needed
239 * @param[in] tensor Tensor to be mapped
240 * @param[in] blocking Specified if map is blocking or not
242 inline void map(GCTensor &tensor, bool blocking)
244 tensor.map(blocking);
247 /** Unmaps a tensor if needed
249 * @param tensor Tensor to be unmapped
251 inline void unmap(GCTensor &tensor)
255 #endif /* ARM_COMPUTE_GC */
257 /** Class to load the content of a PPM file into an Image
263 : _fs(), _width(0), _height(0)
266 /** Open a PPM file and reads its metadata (Width, height)
268 * @param[in] ppm_filename File to open
270 void open(const std::string &ppm_filename)
272 ARM_COMPUTE_ERROR_ON(is_open());
275 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
276 _fs.open(ppm_filename, std::ios::in | std::ios::binary);
278 unsigned int max_val = 0;
279 std::tie(_width, _height, max_val) = parse_ppm_header(_fs);
281 ARM_COMPUTE_ERROR_ON_MSG(max_val >= 256, "2 bytes per colour channel not supported in file %s", ppm_filename.c_str());
283 catch(std::runtime_error &e)
285 ARM_COMPUTE_ERROR("Accessing %s: %s", ppm_filename.c_str(), e.what());
288 /** Return true if a PPM file is currently open
292 return _fs.is_open();
295 /** Initialise an image's metadata with the dimensions of the PPM file currently open
297 * @param[out] image Image to initialise
298 * @param[in] format Format to use for the image (Must be RGB888 or U8)
300 template <typename T>
301 void init_image(T &image, arm_compute::Format format)
303 ARM_COMPUTE_ERROR_ON(!is_open());
304 ARM_COMPUTE_ERROR_ON(format != arm_compute::Format::RGB888 && format != arm_compute::Format::U8);
306 // Use the size of the input PPM image
307 arm_compute::TensorInfo image_info(_width, _height, format);
308 image.allocator()->init(image_info);
311 /** Fill an image with the content of the currently open PPM file.
313 * @note If the image is a CLImage, the function maps and unmaps the image
315 * @param[in,out] image Image to fill (Must be allocated, and of matching dimensions with the opened PPM).
317 template <typename T>
318 void fill_image(T &image)
320 ARM_COMPUTE_ERROR_ON(!is_open());
321 ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height);
322 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&image, arm_compute::Format::U8, arm_compute::Format::RGB888);
325 // Map buffer if creating a CLTensor/GCTensor
328 // Check if the file is large enough to fill the image
329 const size_t current_position = _fs.tellg();
330 _fs.seekg(0, std::ios_base::end);
331 const size_t end_position = _fs.tellg();
332 _fs.seekg(current_position, std::ios_base::beg);
334 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < image.info()->tensor_shape().total_size() * image.info()->element_size(),
335 "Not enough data in file");
336 ARM_COMPUTE_UNUSED(end_position);
338 switch(image.info()->format())
340 case arm_compute::Format::U8:
342 // We need to convert the data from RGB to grayscale:
343 // Iterate through every pixel of the image
344 arm_compute::Window window;
345 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, _width, 1));
346 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1));
348 arm_compute::Iterator out(&image, window);
350 unsigned char red = 0;
351 unsigned char green = 0;
352 unsigned char blue = 0;
354 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
360 *out.ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue;
366 case arm_compute::Format::RGB888:
368 // There is no format conversion needed: we can simply copy the content of the input file to the image one row at the time.
369 // Create a vertical window to iterate through the image's rows:
370 arm_compute::Window window;
371 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1));
373 arm_compute::Iterator out(&image, window);
375 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
377 // Copy one row from the input file to the current row of the image:
378 _fs.read(reinterpret_cast<std::fstream::char_type *>(out.ptr()), _width * image.info()->element_size());
385 ARM_COMPUTE_ERROR("Unsupported format");
388 // Unmap buffer if creating a CLTensor/GCTensor
391 catch(const std::ifstream::failure &e)
393 ARM_COMPUTE_ERROR("Loading PPM file: %s", e.what());
397 /** Fill a tensor with 3 planes (one for each channel) with the content of the currently open PPM file.
399 * @note If the image is a CLImage, the function maps and unmaps the image
401 * @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
402 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false)
404 template <typename T>
405 void fill_planar_tensor(T &tensor, bool bgr = false)
407 ARM_COMPUTE_ERROR_ON(!is_open());
408 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::U8, DataType::F32);
410 const DataLayout data_layout = tensor.info()->data_layout();
411 const TensorShape tensor_shape = tensor.info()->tensor_shape();
413 ARM_COMPUTE_UNUSED(tensor_shape);
414 ARM_COMPUTE_ERROR_ON(tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] != _width);
415 ARM_COMPUTE_ERROR_ON(tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] != _height);
416 ARM_COMPUTE_ERROR_ON(tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)] != 3);
420 // Map buffer if creating a CLTensor
423 // Check if the file is large enough to fill the image
424 const size_t current_position = _fs.tellg();
425 _fs.seekg(0, std::ios_base::end);
426 const size_t end_position = _fs.tellg();
427 _fs.seekg(current_position, std::ios_base::beg);
429 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size(),
430 "Not enough data in file");
431 ARM_COMPUTE_UNUSED(end_position);
433 // Stride across channels
436 // Iterate through every pixel of the image
437 arm_compute::Window window;
438 if(data_layout == DataLayout::NCHW)
440 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, _width, 1));
441 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1));
442 window.set(arm_compute::Window::DimZ, arm_compute::Window::Dimension(0, 1, 1));
443 stride_z = tensor.info()->strides_in_bytes()[2];
447 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
448 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _width, 1));
449 window.set(arm_compute::Window::DimZ, arm_compute::Window::Dimension(0, _height, 1));
450 stride_z = tensor.info()->strides_in_bytes()[0];
453 arm_compute::Iterator out(&tensor, window);
455 unsigned char red = 0;
456 unsigned char green = 0;
457 unsigned char blue = 0;
459 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
465 switch(tensor.info()->data_type())
467 case arm_compute::DataType::U8:
469 *(out.ptr() + 0 * stride_z) = bgr ? blue : red;
470 *(out.ptr() + 1 * stride_z) = green;
471 *(out.ptr() + 2 * stride_z) = bgr ? red : blue;
474 case arm_compute::DataType::F32:
476 *reinterpret_cast<float *>(out.ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red);
477 *reinterpret_cast<float *>(out.ptr() + 1 * stride_z) = static_cast<float>(green);
478 *reinterpret_cast<float *>(out.ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue);
483 ARM_COMPUTE_ERROR("Unsupported data type");
489 // Unmap buffer if creating a CLTensor
492 catch(const std::ifstream::failure &e)
494 ARM_COMPUTE_ERROR("Loading PPM file: %s", e.what());
498 /** Return the width of the currently open PPM file.
500 unsigned int width() const
505 /** Return the height of the currently open PPM file.
507 unsigned int height() const
514 unsigned int _width, _height;
517 /** Numpy data loader */
521 /** Default constructor */
523 : _fs(), _shape(), _fortran_order(false), _typestring()
527 /** Open a NPY file and reads its metadata
529 * @param[in] npy_filename File to open
531 void open(const std::string &npy_filename)
533 ARM_COMPUTE_ERROR_ON(is_open());
536 _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
537 _fs.open(npy_filename, std::ios::in | std::ios::binary);
539 std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs);
541 catch(const std::ifstream::failure &e)
543 ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what());
546 /** Return true if a NPY file is currently open */
549 return _fs.is_open();
552 /** Return true if a NPY file is in fortran order */
555 return _fortran_order;
558 /** Initialise the tensor's metadata with the dimensions of the NPY file currently open
560 * @param[out] tensor Tensor to initialise
561 * @param[in] dt Data type to use for the tensor
563 template <typename T>
564 void init_tensor(T &tensor, arm_compute::DataType dt)
566 ARM_COMPUTE_ERROR_ON(!is_open());
567 ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32);
569 // Use the size of the input NPY tensor
571 shape.set_num_dimensions(_shape.size());
572 for(size_t i = 0; i < _shape.size(); ++i)
574 shape.set(i, _shape.at(i));
577 arm_compute::TensorInfo tensor_info(shape, 1, dt);
578 tensor.allocator()->init(tensor_info);
581 /** Fill a tensor with the content of the currently open NPY file.
583 * @note If the tensor is a CLTensor, the function maps and unmaps the tensor
585 * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY).
587 template <typename T>
588 void fill_tensor(T &tensor)
590 ARM_COMPUTE_ERROR_ON(!is_open());
591 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32);
594 // Map buffer if creating a CLTensor
597 // Check if the file is large enough to fill the tensor
598 const size_t current_position = _fs.tellg();
599 _fs.seekg(0, std::ios_base::end);
600 const size_t end_position = _fs.tellg();
601 _fs.seekg(current_position, std::ios_base::beg);
603 ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(),
604 "Not enough data in file");
605 ARM_COMPUTE_UNUSED(end_position);
607 // Check if the typestring matches the given one
608 std::string expect_typestr = get_typestring(tensor.info()->data_type());
609 ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch");
611 // Validate tensor shape
612 ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch");
615 for(size_t i = 0; i < _shape.size(); ++i)
617 ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[i], "Tensor dimensions mismatch");
622 for(size_t i = 0; i < _shape.size(); ++i)
624 ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[_shape.size() - i - 1], "Tensor dimensions mismatch");
628 switch(tensor.info()->data_type())
630 case arm_compute::DataType::F32:
633 if(tensor.info()->padding().empty())
635 // If tensor has no padding read directly from stream.
636 _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size());
640 // If tensor has padding accessing tensor elements through execution window.
642 window.use_tensor_dimensions(tensor.info()->tensor_shape());
644 execute_window_loop(window, [&](const Coordinates & id)
646 _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size());
653 ARM_COMPUTE_ERROR("Unsupported data type");
656 // Unmap buffer if creating a CLTensor
659 catch(const std::ifstream::failure &e)
661 ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what());
667 std::vector<unsigned long> _shape;
669 std::string _typestring;
672 /** Template helper function to save a tensor image to a PPM file.
674 * @note Only U8 and RGB888 formats supported.
675 * @note Only works with 2D tensors.
676 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
678 * @param[in] tensor The tensor to save as PPM file
679 * @param[in] ppm_filename Filename of the file to create.
681 template <typename T>
682 void save_to_ppm(T &tensor, const std::string &ppm_filename)
684 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8);
685 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
691 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
692 fs.open(ppm_filename, std::ios::out | std::ios::binary);
694 const unsigned int width = tensor.info()->tensor_shape()[0];
695 const unsigned int height = tensor.info()->tensor_shape()[1];
698 << width << " " << height << " 255\n";
700 // Map buffer if creating a CLTensor/GCTensor
703 switch(tensor.info()->format())
705 case arm_compute::Format::U8:
707 arm_compute::Window window;
708 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
709 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
711 arm_compute::Iterator in(&tensor, window);
713 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
715 const unsigned char value = *in.ptr();
717 fs << value << value << value;
723 case arm_compute::Format::RGB888:
725 arm_compute::Window window;
726 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width));
727 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
729 arm_compute::Iterator in(&tensor, window);
731 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
733 fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size());
740 ARM_COMPUTE_ERROR("Unsupported format");
743 // Unmap buffer if creating a CLTensor/GCTensor
746 catch(const std::ofstream::failure &e)
748 ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what());
752 /** Template helper function to save a tensor image to a NPY file.
754 * @note Only F32 data type supported.
755 * @note Only works with 2D tensors.
756 * @note If the input tensor is a CLTensor, the function maps and unmaps the image
758 * @param[in] tensor The tensor to save as NPY file
759 * @param[in] npy_filename Filename of the file to create.
760 * @param[in] fortran_order If true, save matrix in fortran order.
762 template <typename T>
763 void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order)
765 ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32);
766 ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2);
772 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
773 fs.open(npy_filename, std::ios::out | std::ios::binary);
775 const unsigned int width = tensor.info()->tensor_shape()[0];
776 const unsigned int height = tensor.info()->tensor_shape()[1];
777 std::vector<npy::ndarray_len_t> shape(2);
781 shape[0] = height, shape[1] = width;
785 shape[0] = width, shape[1] = height;
788 // Map buffer if creating a CLTensor
791 switch(tensor.info()->data_type())
793 case arm_compute::DataType::F32:
795 std::vector<float> tmp; /* Used only to get the typestring */
796 npy::Typestring typestring_o{ tmp };
797 std::string typestring = typestring_o.str();
799 std::ofstream stream(npy_filename, std::ofstream::binary);
800 npy::write_header(stream, typestring, fortran_order, shape);
802 arm_compute::Window window;
803 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1));
804 window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1));
806 arm_compute::Iterator in(&tensor, window);
808 arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id)
810 stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float));
817 ARM_COMPUTE_ERROR("Unsupported format");
820 // Unmap buffer if creating a CLTensor
823 catch(const std::ofstream::failure &e)
825 ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what());
829 /** Load the tensor with pre-trained data from a binary file
831 * @param[in] tensor The tensor to be filled. Data type supported: F32.
832 * @param[in] filename Filename of the binary file to load from.
834 template <typename T>
835 void load_trained_data(T &tensor, const std::string &filename)
837 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32);
843 fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);
845 fs.open(filename, std::ios::in | std::ios::binary);
849 throw std::runtime_error("Could not load binary data: " + filename);
852 // Map buffer if creating a CLTensor/GCTensor
857 window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1));
859 for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d)
861 window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1));
864 arm_compute::Iterator in(&tensor, window);
866 execute_window_loop(window, [&](const Coordinates & id)
868 fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size());
872 // Unmap buffer if creating a CLTensor/GCTensor
875 catch(const std::ofstream::failure &e)
877 ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what());
881 template <typename T>
882 void fill_random_tensor(T &tensor, float lower_bound, float upper_bound)
884 std::random_device rd;
885 std::mt19937 gen(rd());
888 window.use_tensor_dimensions(tensor.info()->tensor_shape());
892 Iterator it(&tensor, window);
894 switch(tensor.info()->data_type())
896 case arm_compute::DataType::F32:
898 std::uniform_real_distribution<float> dist(lower_bound, upper_bound);
900 execute_window_loop(window, [&](const Coordinates & id)
902 *reinterpret_cast<float *>(it.ptr()) = dist(gen);
910 ARM_COMPUTE_ERROR("Unsupported format");
917 template <typename T>
918 void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt)
920 dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1)), 1, dt));
922 /** This function returns the amount of memory free reading from /proc/meminfo
924 * @return The free memory in kB
926 uint64_t get_mem_free_from_meminfo();
928 /** Compare to tensor
930 * @param[in] tensor1 First tensor to be compared.
931 * @param[in] tensor2 Second tensor to be compared.
933 * @return The number of mismatches
935 template <typename T>
936 int compare_tensor(ITensor &tensor1, ITensor &tensor2)
938 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2);
939 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2);
941 int num_mismatches = 0;
943 window.use_tensor_dimensions(tensor1.info()->tensor_shape());
947 Iterator itensor1(&tensor1, window);
948 Iterator itensor2(&tensor2, window);
950 execute_window_loop(window, [&](const Coordinates & id)
952 if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > 0.00001)
962 return num_mismatches;
965 } // namespace arm_compute
966 #endif /* __UTILS_UTILS_H__*/