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