Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Window.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/Tensor.h" |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 33 | #include "libnpy/npy.hpp" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 34 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | #ifdef ARM_COMPUTE_CL |
| 37 | #include "arm_compute/core/CL/OpenCL.h" |
Isabella Gottardi | 02aabcc | 2017-10-12 17:28:51 +0100 | [diff] [blame] | 38 | #include "arm_compute/runtime/CL/CLDistribution1D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | #include "arm_compute/runtime/CL/CLTensor.h" |
| 40 | #endif /* ARM_COMPUTE_CL */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 41 | #ifdef ARM_COMPUTE_GC |
| 42 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 43 | #endif /* ARM_COMPUTE_GC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
| 45 | #include <cstdlib> |
| 46 | #include <cstring> |
| 47 | #include <fstream> |
| 48 | #include <iostream> |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 49 | #include <random> |
| 50 | #include <string> |
| 51 | #include <tuple> |
| 52 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | |
| 54 | namespace arm_compute |
| 55 | { |
| 56 | namespace utils |
| 57 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 58 | /** Abstract Example class. |
| 59 | * |
| 60 | * All examples have to inherit from this class. |
| 61 | */ |
| 62 | class Example |
| 63 | { |
| 64 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 65 | /** Setup the example. |
| 66 | * |
| 67 | * @param[in] argc Argument count. |
| 68 | * @param[in] argv Argument values. |
| 69 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 70 | virtual void do_setup(int argc, char **argv) {}; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 71 | /** Run the example. */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 72 | virtual void do_run() {}; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 73 | /** Teardown the example. */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 74 | virtual void do_teardown() {}; |
| 75 | |
| 76 | /** Default destructor. */ |
| 77 | virtual ~Example() = default; |
| 78 | }; |
| 79 | |
| 80 | /** Run an example and handle the potential exceptions it throws |
| 81 | * |
| 82 | * @param[in] argc Number of command line arguments |
| 83 | * @param[in] argv Command line arguments |
| 84 | * @param[in] example Example to run |
| 85 | */ |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 86 | int run_example(int argc, char **argv, std::unique_ptr<Example> example); |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 87 | |
| 88 | template <typename T> |
| 89 | int run_example(int argc, char **argv) |
| 90 | { |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 91 | return run_example(argc, argv, support::cpp14::make_unique<T>()); |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 92 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | /** Draw a RGB rectangular window for the detected object |
| 95 | * |
| 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 |
| 101 | */ |
| 102 | void draw_detection_rectangle(arm_compute::ITensor *tensor, const arm_compute::DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b); |
| 103 | |
| 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 |
| 106 | * |
| 107 | * @param[in] fs Input file stream to parse |
| 108 | * |
| 109 | * @return The width, height and max value stored in the header of the PPM file |
| 110 | */ |
| 111 | std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs); |
| 112 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 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 //TODO |
| 115 | * |
| 116 | * @param[in] fs Input file stream to parse |
| 117 | * |
| 118 | * @return The width and height stored in the header of the NPY file |
| 119 | */ |
| 120 | std::tuple<std::vector<unsigned long>, bool, std::string> parse_npy_header(std::ifstream &fs); |
| 121 | |
| 122 | /** Obtain numpy type string from DataType. |
| 123 | * |
| 124 | * @param[in] data_type Data type. |
| 125 | * |
| 126 | * @return numpy type string. |
| 127 | */ |
| 128 | inline std::string get_typestring(DataType data_type) |
| 129 | { |
| 130 | // Check endianness |
| 131 | const unsigned int i = 1; |
| 132 | const char *c = reinterpret_cast<const char *>(&i); |
| 133 | std::string endianness; |
| 134 | if(*c == 1) |
| 135 | { |
| 136 | endianness = std::string("<"); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | endianness = std::string(">"); |
| 141 | } |
| 142 | const std::string no_endianness("|"); |
| 143 | |
| 144 | switch(data_type) |
| 145 | { |
| 146 | case DataType::U8: |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 147 | case DataType::QASYMM8: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 148 | return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t)); |
| 149 | case DataType::S8: |
| 150 | return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t)); |
| 151 | case DataType::U16: |
| 152 | return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t)); |
| 153 | case DataType::S16: |
| 154 | return endianness + "i" + support::cpp11::to_string(sizeof(int16_t)); |
| 155 | case DataType::U32: |
| 156 | return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t)); |
| 157 | case DataType::S32: |
| 158 | return endianness + "i" + support::cpp11::to_string(sizeof(int32_t)); |
| 159 | case DataType::U64: |
| 160 | return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t)); |
| 161 | case DataType::S64: |
| 162 | return endianness + "i" + support::cpp11::to_string(sizeof(int64_t)); |
| 163 | case DataType::F32: |
| 164 | return endianness + "f" + support::cpp11::to_string(sizeof(float)); |
| 165 | case DataType::F64: |
| 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)); |
| 169 | default: |
| 170 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 171 | } |
| 172 | } |
| 173 | |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 174 | /** Maps a tensor if needed |
| 175 | * |
| 176 | * @param[in] tensor Tensor to be mapped |
| 177 | * @param[in] blocking Specified if map is blocking or not |
| 178 | */ |
| 179 | template <typename T> |
Gian Marco Iodice | ae27e94 | 2017-09-28 18:31:26 +0100 | [diff] [blame] | 180 | inline void map(T &tensor, bool blocking) |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 181 | { |
| 182 | ARM_COMPUTE_UNUSED(tensor); |
| 183 | ARM_COMPUTE_UNUSED(blocking); |
| 184 | } |
| 185 | |
| 186 | /** Unmaps a tensor if needed |
| 187 | * |
| 188 | * @param tensor Tensor to be unmapped |
| 189 | */ |
| 190 | template <typename T> |
Gian Marco Iodice | ae27e94 | 2017-09-28 18:31:26 +0100 | [diff] [blame] | 191 | inline void unmap(T &tensor) |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 192 | { |
| 193 | ARM_COMPUTE_UNUSED(tensor); |
| 194 | } |
| 195 | |
| 196 | #ifdef ARM_COMPUTE_CL |
| 197 | /** Maps a tensor if needed |
| 198 | * |
| 199 | * @param[in] tensor Tensor to be mapped |
| 200 | * @param[in] blocking Specified if map is blocking or not |
| 201 | */ |
Gian Marco Iodice | ae27e94 | 2017-09-28 18:31:26 +0100 | [diff] [blame] | 202 | inline void map(CLTensor &tensor, bool blocking) |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 203 | { |
| 204 | tensor.map(blocking); |
| 205 | } |
| 206 | |
| 207 | /** Unmaps a tensor if needed |
| 208 | * |
| 209 | * @param tensor Tensor to be unmapped |
| 210 | */ |
Gian Marco Iodice | ae27e94 | 2017-09-28 18:31:26 +0100 | [diff] [blame] | 211 | inline void unmap(CLTensor &tensor) |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 212 | { |
| 213 | tensor.unmap(); |
| 214 | } |
Isabella Gottardi | 02aabcc | 2017-10-12 17:28:51 +0100 | [diff] [blame] | 215 | |
| 216 | /** Maps a distribution if needed |
| 217 | * |
| 218 | * @param[in] distribution Distribution to be mapped |
| 219 | * @param[in] blocking Specified if map is blocking or not |
| 220 | */ |
| 221 | inline void map(CLDistribution1D &distribution, bool blocking) |
| 222 | { |
| 223 | distribution.map(blocking); |
| 224 | } |
| 225 | |
| 226 | /** Unmaps a distribution if needed |
| 227 | * |
| 228 | * @param distribution Distribution to be unmapped |
| 229 | */ |
| 230 | inline void unmap(CLDistribution1D &distribution) |
| 231 | { |
| 232 | distribution.unmap(); |
| 233 | } |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 234 | #endif /* ARM_COMPUTE_CL */ |
| 235 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 236 | #ifdef ARM_COMPUTE_GC |
| 237 | /** Maps a tensor if needed |
| 238 | * |
| 239 | * @param[in] tensor Tensor to be mapped |
| 240 | * @param[in] blocking Specified if map is blocking or not |
| 241 | */ |
| 242 | inline void map(GCTensor &tensor, bool blocking) |
| 243 | { |
| 244 | tensor.map(blocking); |
| 245 | } |
| 246 | |
| 247 | /** Unmaps a tensor if needed |
| 248 | * |
| 249 | * @param tensor Tensor to be unmapped |
| 250 | */ |
| 251 | inline void unmap(GCTensor &tensor) |
| 252 | { |
| 253 | tensor.unmap(); |
| 254 | } |
| 255 | #endif /* ARM_COMPUTE_GC */ |
| 256 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 257 | /** Numpy data loader */ |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 258 | class NPYLoader |
| 259 | { |
| 260 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 261 | /** Default constructor */ |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 262 | NPYLoader() |
| 263 | : _fs(), _shape(), _fortran_order(false), _typestring() |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | /** Open a NPY file and reads its metadata |
| 268 | * |
| 269 | * @param[in] npy_filename File to open |
| 270 | */ |
| 271 | void open(const std::string &npy_filename) |
| 272 | { |
| 273 | ARM_COMPUTE_ERROR_ON(is_open()); |
| 274 | try |
| 275 | { |
| 276 | _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 277 | _fs.open(npy_filename, std::ios::in | std::ios::binary); |
| 278 | |
| 279 | std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs); |
| 280 | } |
| 281 | catch(const std::ifstream::failure &e) |
| 282 | { |
| 283 | ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what()); |
| 284 | } |
| 285 | } |
| 286 | /** Return true if a NPY file is currently open */ |
| 287 | bool is_open() |
| 288 | { |
| 289 | return _fs.is_open(); |
| 290 | } |
| 291 | |
| 292 | /** Return true if a NPY file is in fortran order */ |
| 293 | bool is_fortran() |
| 294 | { |
| 295 | return _fortran_order; |
| 296 | } |
| 297 | |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 298 | /** Initialise the tensor's metadata with the dimensions of the NPY file currently open |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 299 | * |
| 300 | * @param[out] tensor Tensor to initialise |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 301 | * @param[in] dt Data type to use for the tensor |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 302 | */ |
| 303 | template <typename T> |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 304 | void init_tensor(T &tensor, arm_compute::DataType dt) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 305 | { |
| 306 | ARM_COMPUTE_ERROR_ON(!is_open()); |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 307 | ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 308 | |
| 309 | // Use the size of the input NPY tensor |
| 310 | TensorShape shape; |
| 311 | shape.set_num_dimensions(_shape.size()); |
| 312 | for(size_t i = 0; i < _shape.size(); ++i) |
| 313 | { |
| 314 | shape.set(i, _shape.at(i)); |
| 315 | } |
| 316 | |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 317 | arm_compute::TensorInfo tensor_info(shape, 1, dt); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 318 | tensor.allocator()->init(tensor_info); |
| 319 | } |
| 320 | |
| 321 | /** Fill a tensor with the content of the currently open NPY file. |
| 322 | * |
| 323 | * @note If the tensor is a CLTensor, the function maps and unmaps the tensor |
| 324 | * |
| 325 | * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY). |
| 326 | */ |
| 327 | template <typename T> |
| 328 | void fill_tensor(T &tensor) |
| 329 | { |
| 330 | ARM_COMPUTE_ERROR_ON(!is_open()); |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 331 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 332 | try |
| 333 | { |
| 334 | // Map buffer if creating a CLTensor |
| 335 | map(tensor, true); |
| 336 | |
| 337 | // Check if the file is large enough to fill the tensor |
| 338 | const size_t current_position = _fs.tellg(); |
| 339 | _fs.seekg(0, std::ios_base::end); |
| 340 | const size_t end_position = _fs.tellg(); |
| 341 | _fs.seekg(current_position, std::ios_base::beg); |
| 342 | |
| 343 | ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(), |
| 344 | "Not enough data in file"); |
| 345 | ARM_COMPUTE_UNUSED(end_position); |
| 346 | |
| 347 | // Check if the typestring matches the given one |
| 348 | std::string expect_typestr = get_typestring(tensor.info()->data_type()); |
| 349 | ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch"); |
| 350 | |
| 351 | // Validate tensor shape |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 352 | ARM_COMPUTE_ERROR_ON_MSG(_shape.size() != tensor.info()->tensor_shape().num_dimensions(), "Tensor ranks mismatch"); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 353 | if(_fortran_order) |
| 354 | { |
| 355 | for(size_t i = 0; i < _shape.size(); ++i) |
| 356 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 357 | ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[i], "Tensor dimensions mismatch"); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | for(size_t i = 0; i < _shape.size(); ++i) |
| 363 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 364 | ARM_COMPUTE_ERROR_ON_MSG(tensor.info()->tensor_shape()[i] != _shape[_shape.size() - i - 1], "Tensor dimensions mismatch"); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 368 | switch(tensor.info()->data_type()) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 369 | { |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 370 | case arm_compute::DataType::F32: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 371 | { |
| 372 | // Read data |
| 373 | if(tensor.info()->padding().empty()) |
| 374 | { |
| 375 | // If tensor has no padding read directly from stream. |
| 376 | _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size()); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | // If tensor has padding accessing tensor elements through execution window. |
| 381 | Window window; |
| 382 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 383 | |
| 384 | execute_window_loop(window, [&](const Coordinates & id) |
| 385 | { |
| 386 | _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size()); |
| 387 | }); |
| 388 | } |
| 389 | |
| 390 | break; |
| 391 | } |
| 392 | default: |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 393 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Unmap buffer if creating a CLTensor |
| 397 | unmap(tensor); |
| 398 | } |
| 399 | catch(const std::ifstream::failure &e) |
| 400 | { |
| 401 | ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what()); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private: |
| 406 | std::ifstream _fs; |
| 407 | std::vector<unsigned long> _shape; |
| 408 | bool _fortran_order; |
| 409 | std::string _typestring; |
| 410 | }; |
| 411 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 412 | /** Template helper function to save a tensor image to a PPM file. |
| 413 | * |
| 414 | * @note Only U8 and RGB888 formats supported. |
| 415 | * @note Only works with 2D tensors. |
| 416 | * @note If the input tensor is a CLTensor, the function maps and unmaps the image |
| 417 | * |
| 418 | * @param[in] tensor The tensor to save as PPM file |
| 419 | * @param[in] ppm_filename Filename of the file to create. |
| 420 | */ |
| 421 | template <typename T> |
| 422 | void save_to_ppm(T &tensor, const std::string &ppm_filename) |
| 423 | { |
| 424 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8); |
| 425 | ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2); |
| 426 | |
| 427 | std::ofstream fs; |
| 428 | |
| 429 | try |
| 430 | { |
| 431 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 432 | fs.open(ppm_filename, std::ios::out | std::ios::binary); |
| 433 | |
| 434 | const unsigned int width = tensor.info()->tensor_shape()[0]; |
| 435 | const unsigned int height = tensor.info()->tensor_shape()[1]; |
| 436 | |
| 437 | fs << "P6\n" |
| 438 | << width << " " << height << " 255\n"; |
| 439 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 440 | // Map buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 441 | map(tensor, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 442 | |
| 443 | switch(tensor.info()->format()) |
| 444 | { |
| 445 | case arm_compute::Format::U8: |
| 446 | { |
| 447 | arm_compute::Window window; |
| 448 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1)); |
| 449 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 450 | |
| 451 | arm_compute::Iterator in(&tensor, window); |
| 452 | |
| 453 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 454 | { |
| 455 | const unsigned char value = *in.ptr(); |
| 456 | |
| 457 | fs << value << value << value; |
| 458 | }, |
| 459 | in); |
| 460 | |
| 461 | break; |
| 462 | } |
| 463 | case arm_compute::Format::RGB888: |
| 464 | { |
| 465 | arm_compute::Window window; |
| 466 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width)); |
| 467 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 468 | |
| 469 | arm_compute::Iterator in(&tensor, window); |
| 470 | |
| 471 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 472 | { |
| 473 | fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size()); |
| 474 | }, |
| 475 | in); |
| 476 | |
| 477 | break; |
| 478 | } |
| 479 | default: |
| 480 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 481 | } |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 482 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 483 | // Unmap buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 484 | unmap(tensor); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 485 | } |
| 486 | catch(const std::ofstream::failure &e) |
| 487 | { |
| 488 | ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what()); |
| 489 | } |
| 490 | } |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 491 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 492 | /** Template helper function to save a tensor image to a NPY file. |
| 493 | * |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 494 | * @note Only F32 data type supported. |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 495 | * @note Only works with 2D tensors. |
| 496 | * @note If the input tensor is a CLTensor, the function maps and unmaps the image |
| 497 | * |
| 498 | * @param[in] tensor The tensor to save as NPY file |
| 499 | * @param[in] npy_filename Filename of the file to create. |
| 500 | * @param[in] fortran_order If true, save matrix in fortran order. |
| 501 | */ |
| 502 | template <typename T> |
| 503 | void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order) |
| 504 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 505 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 506 | ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2); |
| 507 | |
| 508 | std::ofstream fs; |
| 509 | |
| 510 | try |
| 511 | { |
| 512 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 513 | fs.open(npy_filename, std::ios::out | std::ios::binary); |
| 514 | |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 515 | const unsigned int width = tensor.info()->tensor_shape()[0]; |
| 516 | const unsigned int height = tensor.info()->tensor_shape()[1]; |
| 517 | std::vector<npy::ndarray_len_t> shape(2); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 518 | |
| 519 | if(!fortran_order) |
| 520 | { |
| 521 | shape[0] = height, shape[1] = width; |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | shape[0] = width, shape[1] = height; |
| 526 | } |
| 527 | |
| 528 | // Map buffer if creating a CLTensor |
| 529 | map(tensor, true); |
| 530 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 531 | switch(tensor.info()->data_type()) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 532 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 533 | case arm_compute::DataType::F32: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 534 | { |
| 535 | std::vector<float> tmp; /* Used only to get the typestring */ |
| 536 | npy::Typestring typestring_o{ tmp }; |
| 537 | std::string typestring = typestring_o.str(); |
| 538 | |
| 539 | std::ofstream stream(npy_filename, std::ofstream::binary); |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 540 | npy::write_header(stream, typestring, fortran_order, shape); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 541 | |
| 542 | arm_compute::Window window; |
| 543 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1)); |
| 544 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 545 | |
| 546 | arm_compute::Iterator in(&tensor, window); |
| 547 | |
| 548 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 549 | { |
| 550 | stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float)); |
| 551 | }, |
| 552 | in); |
| 553 | |
| 554 | break; |
| 555 | } |
| 556 | default: |
| 557 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 558 | } |
| 559 | |
| 560 | // Unmap buffer if creating a CLTensor |
| 561 | unmap(tensor); |
| 562 | } |
| 563 | catch(const std::ofstream::failure &e) |
| 564 | { |
| 565 | ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what()); |
| 566 | } |
| 567 | } |
| 568 | |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 569 | /** Load the tensor with pre-trained data from a binary file |
| 570 | * |
| 571 | * @param[in] tensor The tensor to be filled. Data type supported: F32. |
| 572 | * @param[in] filename Filename of the binary file to load from. |
| 573 | */ |
| 574 | template <typename T> |
| 575 | void load_trained_data(T &tensor, const std::string &filename) |
| 576 | { |
| 577 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 578 | |
| 579 | std::ifstream fs; |
| 580 | |
| 581 | try |
| 582 | { |
| 583 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 584 | // Open file |
| 585 | fs.open(filename, std::ios::in | std::ios::binary); |
| 586 | |
| 587 | if(!fs.good()) |
| 588 | { |
| 589 | throw std::runtime_error("Could not load binary data: " + filename); |
| 590 | } |
| 591 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 592 | // Map buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 593 | map(tensor, true); |
| 594 | |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 595 | Window window; |
| 596 | |
| 597 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1)); |
| 598 | |
| 599 | for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d) |
| 600 | { |
| 601 | window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1)); |
| 602 | } |
| 603 | |
| 604 | arm_compute::Iterator in(&tensor, window); |
| 605 | |
| 606 | execute_window_loop(window, [&](const Coordinates & id) |
| 607 | { |
| 608 | fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size()); |
| 609 | }, |
| 610 | in); |
| 611 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 612 | // Unmap buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 613 | unmap(tensor); |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 614 | } |
| 615 | catch(const std::ofstream::failure &e) |
| 616 | { |
| 617 | ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what()); |
| 618 | } |
| 619 | } |
| 620 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 621 | template <typename T> |
| 622 | void fill_random_tensor(T &tensor, float lower_bound, float upper_bound) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 623 | { |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 624 | std::random_device rd; |
| 625 | std::mt19937 gen(rd()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 626 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 627 | Window window; |
Michalis Spyrou | 5e69bb4 | 2018-03-09 16:36:00 +0000 | [diff] [blame] | 628 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 629 | |
| 630 | map(tensor, true); |
| 631 | |
| 632 | Iterator it(&tensor, window); |
| 633 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 634 | switch(tensor.info()->data_type()) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 635 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 636 | case arm_compute::DataType::F32: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 637 | { |
| 638 | std::uniform_real_distribution<float> dist(lower_bound, upper_bound); |
| 639 | |
| 640 | execute_window_loop(window, [&](const Coordinates & id) |
| 641 | { |
| 642 | *reinterpret_cast<float *>(it.ptr()) = dist(gen); |
| 643 | }, |
| 644 | it); |
| 645 | |
| 646 | break; |
| 647 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 648 | default: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 649 | { |
| 650 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 651 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 652 | } |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 653 | |
| 654 | unmap(tensor); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 655 | } |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 656 | |
| 657 | template <typename T> |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 658 | void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 659 | { |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 660 | dst.allocator()->init(TensorInfo(TensorShape(src1.info()->dimension(0), src0.info()->dimension(1)), 1, dt)); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 661 | } |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 662 | /** This function returns the amount of memory free reading from /proc/meminfo |
| 663 | * |
| 664 | * @return The free memory in kB |
| 665 | */ |
| 666 | uint64_t get_mem_free_from_meminfo(); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 667 | |
| 668 | /** Compare to tensor |
| 669 | * |
| 670 | * @param[in] tensor1 First tensor to be compared. |
| 671 | * @param[in] tensor2 Second tensor to be compared. |
| 672 | * |
| 673 | * @return The number of mismatches |
| 674 | */ |
| 675 | template <typename T> |
| 676 | int compare_tensor(ITensor &tensor1, ITensor &tensor2) |
| 677 | { |
| 678 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(&tensor1, &tensor2); |
| 679 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(&tensor1, &tensor2); |
| 680 | |
| 681 | int num_mismatches = 0; |
| 682 | Window window; |
| 683 | window.use_tensor_dimensions(tensor1.info()->tensor_shape()); |
| 684 | |
| 685 | map(tensor1, true); |
| 686 | map(tensor2, true); |
| 687 | Iterator itensor1(&tensor1, window); |
| 688 | Iterator itensor2(&tensor2, window); |
| 689 | |
| 690 | execute_window_loop(window, [&](const Coordinates & id) |
| 691 | { |
| 692 | if(std::abs(*reinterpret_cast<T *>(itensor1.ptr()) - *reinterpret_cast<T *>(itensor2.ptr())) > 0.00001) |
| 693 | { |
| 694 | ++num_mismatches; |
| 695 | } |
| 696 | }, |
| 697 | itensor1, itensor2); |
| 698 | |
| 699 | unmap(itensor1); |
| 700 | unmap(itensor2); |
| 701 | |
| 702 | return num_mismatches; |
| 703 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 704 | } // namespace utils |
| 705 | } // namespace arm_compute |
| 706 | #endif /* __UTILS_UTILS_H__*/ |