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