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 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 257 | /** Class to load the content of a PPM file into an Image |
| 258 | */ |
| 259 | class PPMLoader |
| 260 | { |
| 261 | public: |
| 262 | PPMLoader() |
| 263 | : _fs(), _width(0), _height(0) |
| 264 | { |
| 265 | } |
| 266 | /** Open a PPM file and reads its metadata (Width, height) |
| 267 | * |
| 268 | * @param[in] ppm_filename File to open |
| 269 | */ |
| 270 | void open(const std::string &ppm_filename) |
| 271 | { |
| 272 | ARM_COMPUTE_ERROR_ON(is_open()); |
| 273 | try |
| 274 | { |
| 275 | _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 276 | _fs.open(ppm_filename, std::ios::in | std::ios::binary); |
| 277 | |
| 278 | unsigned int max_val = 0; |
| 279 | std::tie(_width, _height, max_val) = parse_ppm_header(_fs); |
| 280 | |
| 281 | ARM_COMPUTE_ERROR_ON_MSG(max_val >= 256, "2 bytes per colour channel not supported in file %s", ppm_filename.c_str()); |
| 282 | } |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 283 | catch(std::runtime_error &e) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 284 | { |
| 285 | ARM_COMPUTE_ERROR("Accessing %s: %s", ppm_filename.c_str(), e.what()); |
| 286 | } |
| 287 | } |
| 288 | /** Return true if a PPM file is currently open |
| 289 | */ |
| 290 | bool is_open() |
| 291 | { |
| 292 | return _fs.is_open(); |
| 293 | } |
| 294 | |
| 295 | /** Initialise an image's metadata with the dimensions of the PPM file currently open |
| 296 | * |
| 297 | * @param[out] image Image to initialise |
| 298 | * @param[in] format Format to use for the image (Must be RGB888 or U8) |
| 299 | */ |
| 300 | template <typename T> |
| 301 | void init_image(T &image, arm_compute::Format format) |
| 302 | { |
| 303 | ARM_COMPUTE_ERROR_ON(!is_open()); |
| 304 | ARM_COMPUTE_ERROR_ON(format != arm_compute::Format::RGB888 && format != arm_compute::Format::U8); |
| 305 | |
| 306 | // Use the size of the input PPM image |
| 307 | arm_compute::TensorInfo image_info(_width, _height, format); |
| 308 | image.allocator()->init(image_info); |
| 309 | } |
| 310 | |
| 311 | /** Fill an image with the content of the currently open PPM file. |
| 312 | * |
| 313 | * @note If the image is a CLImage, the function maps and unmaps the image |
| 314 | * |
| 315 | * @param[in,out] image Image to fill (Must be allocated, and of matching dimensions with the opened PPM). |
| 316 | */ |
| 317 | template <typename T> |
| 318 | void fill_image(T &image) |
| 319 | { |
| 320 | ARM_COMPUTE_ERROR_ON(!is_open()); |
| 321 | ARM_COMPUTE_ERROR_ON(image.info()->dimension(0) != _width || image.info()->dimension(1) != _height); |
| 322 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&image, arm_compute::Format::U8, arm_compute::Format::RGB888); |
| 323 | try |
| 324 | { |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 325 | // Map buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 326 | map(image, true); |
| 327 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 328 | // Check if the file is large enough to fill the image |
| 329 | const size_t current_position = _fs.tellg(); |
| 330 | _fs.seekg(0, std::ios_base::end); |
| 331 | const size_t end_position = _fs.tellg(); |
| 332 | _fs.seekg(current_position, std::ios_base::beg); |
| 333 | |
| 334 | ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < image.info()->tensor_shape().total_size() * image.info()->element_size(), |
| 335 | "Not enough data in file"); |
| 336 | ARM_COMPUTE_UNUSED(end_position); |
| 337 | |
| 338 | switch(image.info()->format()) |
| 339 | { |
| 340 | case arm_compute::Format::U8: |
| 341 | { |
| 342 | // We need to convert the data from RGB to grayscale: |
| 343 | // Iterate through every pixel of the image |
| 344 | arm_compute::Window window; |
| 345 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, _width, 1)); |
| 346 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1)); |
| 347 | |
| 348 | arm_compute::Iterator out(&image, window); |
| 349 | |
| 350 | unsigned char red = 0; |
| 351 | unsigned char green = 0; |
| 352 | unsigned char blue = 0; |
| 353 | |
| 354 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 355 | { |
| 356 | red = _fs.get(); |
| 357 | green = _fs.get(); |
| 358 | blue = _fs.get(); |
| 359 | |
| 360 | *out.ptr() = 0.2126f * red + 0.7152f * green + 0.0722f * blue; |
| 361 | }, |
| 362 | out); |
| 363 | |
| 364 | break; |
| 365 | } |
| 366 | case arm_compute::Format::RGB888: |
| 367 | { |
| 368 | // There is no format conversion needed: we can simply copy the content of the input file to the image one row at the time. |
| 369 | // Create a vertical window to iterate through the image's rows: |
| 370 | arm_compute::Window window; |
| 371 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1)); |
| 372 | |
| 373 | arm_compute::Iterator out(&image, window); |
| 374 | |
| 375 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 376 | { |
| 377 | // Copy one row from the input file to the current row of the image: |
| 378 | _fs.read(reinterpret_cast<std::fstream::char_type *>(out.ptr()), _width * image.info()->element_size()); |
| 379 | }, |
| 380 | out); |
| 381 | |
| 382 | break; |
| 383 | } |
| 384 | default: |
| 385 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 386 | } |
| 387 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 388 | // Unmap buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 389 | unmap(image); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 390 | } |
| 391 | catch(const std::ifstream::failure &e) |
| 392 | { |
| 393 | ARM_COMPUTE_ERROR("Loading PPM file: %s", e.what()); |
| 394 | } |
| 395 | } |
| 396 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 397 | /** Fill a tensor with 3 planes (one for each channel) with the content of the currently open PPM file. |
| 398 | * |
| 399 | * @note If the image is a CLImage, the function maps and unmaps the image |
| 400 | * |
| 401 | * @param[in,out] tensor Tensor with 3 planes to fill (Must be allocated, and of matching dimensions with the opened PPM). Data types supported: U8/F32 |
| 402 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false) |
| 403 | */ |
| 404 | template <typename T> |
| 405 | void fill_planar_tensor(T &tensor, bool bgr = false) |
| 406 | { |
| 407 | ARM_COMPUTE_ERROR_ON(!is_open()); |
| 408 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::U8, DataType::F32); |
| 409 | ARM_COMPUTE_ERROR_ON(tensor.info()->dimension(0) != _width || tensor.info()->dimension(1) != _height || tensor.info()->dimension(2) != 3); |
| 410 | |
| 411 | try |
| 412 | { |
| 413 | // Map buffer if creating a CLTensor |
| 414 | map(tensor, true); |
| 415 | |
| 416 | // Check if the file is large enough to fill the image |
| 417 | const size_t current_position = _fs.tellg(); |
| 418 | _fs.seekg(0, std::ios_base::end); |
| 419 | const size_t end_position = _fs.tellg(); |
| 420 | _fs.seekg(current_position, std::ios_base::beg); |
| 421 | |
| 422 | ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size(), |
| 423 | "Not enough data in file"); |
| 424 | ARM_COMPUTE_UNUSED(end_position); |
| 425 | |
| 426 | // Iterate through every pixel of the image |
| 427 | arm_compute::Window window; |
| 428 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, _width, 1)); |
| 429 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, _height, 1)); |
| 430 | window.set(arm_compute::Window::DimZ, arm_compute::Window::Dimension(0, 1, 1)); |
| 431 | |
| 432 | arm_compute::Iterator out(&tensor, window); |
| 433 | |
| 434 | unsigned char red = 0; |
| 435 | unsigned char green = 0; |
| 436 | unsigned char blue = 0; |
| 437 | |
| 438 | size_t stride_z = tensor.info()->strides_in_bytes()[2]; |
| 439 | |
| 440 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 441 | { |
| 442 | red = _fs.get(); |
| 443 | green = _fs.get(); |
| 444 | blue = _fs.get(); |
| 445 | |
| 446 | switch(tensor.info()->data_type()) |
| 447 | { |
| 448 | case arm_compute::DataType::U8: |
| 449 | { |
| 450 | *(out.ptr() + 0 * stride_z) = bgr ? blue : red; |
| 451 | *(out.ptr() + 1 * stride_z) = green; |
| 452 | *(out.ptr() + 2 * stride_z) = bgr ? red : blue; |
| 453 | break; |
| 454 | } |
| 455 | case arm_compute::DataType::F32: |
| 456 | { |
| 457 | *reinterpret_cast<float *>(out.ptr() + 0 * stride_z) = static_cast<float>(bgr ? blue : red); |
| 458 | *reinterpret_cast<float *>(out.ptr() + 1 * stride_z) = static_cast<float>(green); |
| 459 | *reinterpret_cast<float *>(out.ptr() + 2 * stride_z) = static_cast<float>(bgr ? red : blue); |
| 460 | break; |
| 461 | } |
| 462 | default: |
| 463 | { |
| 464 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 465 | } |
| 466 | } |
| 467 | }, |
| 468 | out); |
| 469 | |
| 470 | // Unmap buffer if creating a CLTensor |
| 471 | unmap(tensor); |
| 472 | } |
| 473 | catch(const std::ifstream::failure &e) |
| 474 | { |
| 475 | ARM_COMPUTE_ERROR("Loading PPM file: %s", e.what()); |
| 476 | } |
| 477 | } |
| 478 | |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 479 | /** Return the width of the currently open PPM file. |
| 480 | */ |
| 481 | unsigned int width() const |
| 482 | { |
| 483 | return _width; |
| 484 | } |
| 485 | |
| 486 | /** Return the height of the currently open PPM file. |
| 487 | */ |
| 488 | unsigned int height() const |
| 489 | { |
| 490 | return _height; |
| 491 | } |
| 492 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 493 | private: |
| 494 | std::ifstream _fs; |
| 495 | unsigned int _width, _height; |
| 496 | }; |
| 497 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 498 | /** Numpy data loader */ |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 499 | class NPYLoader |
| 500 | { |
| 501 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 502 | /** Default constructor */ |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 503 | NPYLoader() |
| 504 | : _fs(), _shape(), _fortran_order(false), _typestring() |
| 505 | { |
| 506 | } |
| 507 | |
| 508 | /** Open a NPY file and reads its metadata |
| 509 | * |
| 510 | * @param[in] npy_filename File to open |
| 511 | */ |
| 512 | void open(const std::string &npy_filename) |
| 513 | { |
| 514 | ARM_COMPUTE_ERROR_ON(is_open()); |
| 515 | try |
| 516 | { |
| 517 | _fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 518 | _fs.open(npy_filename, std::ios::in | std::ios::binary); |
| 519 | |
| 520 | std::tie(_shape, _fortran_order, _typestring) = parse_npy_header(_fs); |
| 521 | } |
| 522 | catch(const std::ifstream::failure &e) |
| 523 | { |
| 524 | ARM_COMPUTE_ERROR("Accessing %s: %s", npy_filename.c_str(), e.what()); |
| 525 | } |
| 526 | } |
| 527 | /** Return true if a NPY file is currently open */ |
| 528 | bool is_open() |
| 529 | { |
| 530 | return _fs.is_open(); |
| 531 | } |
| 532 | |
| 533 | /** Return true if a NPY file is in fortran order */ |
| 534 | bool is_fortran() |
| 535 | { |
| 536 | return _fortran_order; |
| 537 | } |
| 538 | |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 539 | /** 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] | 540 | * |
| 541 | * @param[out] tensor Tensor to initialise |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 542 | * @param[in] dt Data type to use for the tensor |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 543 | */ |
| 544 | template <typename T> |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 545 | void init_tensor(T &tensor, arm_compute::DataType dt) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 546 | { |
| 547 | ARM_COMPUTE_ERROR_ON(!is_open()); |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 548 | ARM_COMPUTE_ERROR_ON(dt != arm_compute::DataType::F32); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 549 | |
| 550 | // Use the size of the input NPY tensor |
| 551 | TensorShape shape; |
| 552 | shape.set_num_dimensions(_shape.size()); |
| 553 | for(size_t i = 0; i < _shape.size(); ++i) |
| 554 | { |
| 555 | shape.set(i, _shape.at(i)); |
| 556 | } |
| 557 | |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 558 | arm_compute::TensorInfo tensor_info(shape, 1, dt); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 559 | tensor.allocator()->init(tensor_info); |
| 560 | } |
| 561 | |
| 562 | /** Fill a tensor with the content of the currently open NPY file. |
| 563 | * |
| 564 | * @note If the tensor is a CLTensor, the function maps and unmaps the tensor |
| 565 | * |
| 566 | * @param[in,out] tensor Tensor to fill (Must be allocated, and of matching dimensions with the opened NPY). |
| 567 | */ |
| 568 | template <typename T> |
| 569 | void fill_tensor(T &tensor) |
| 570 | { |
| 571 | ARM_COMPUTE_ERROR_ON(!is_open()); |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 572 | 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] | 573 | try |
| 574 | { |
| 575 | // Map buffer if creating a CLTensor |
| 576 | map(tensor, true); |
| 577 | |
| 578 | // Check if the file is large enough to fill the tensor |
| 579 | const size_t current_position = _fs.tellg(); |
| 580 | _fs.seekg(0, std::ios_base::end); |
| 581 | const size_t end_position = _fs.tellg(); |
| 582 | _fs.seekg(current_position, std::ios_base::beg); |
| 583 | |
| 584 | ARM_COMPUTE_ERROR_ON_MSG((end_position - current_position) < tensor.info()->tensor_shape().total_size() * tensor.info()->element_size(), |
| 585 | "Not enough data in file"); |
| 586 | ARM_COMPUTE_UNUSED(end_position); |
| 587 | |
| 588 | // Check if the typestring matches the given one |
| 589 | std::string expect_typestr = get_typestring(tensor.info()->data_type()); |
| 590 | ARM_COMPUTE_ERROR_ON_MSG(_typestring != expect_typestr, "Typestrings mismatch"); |
| 591 | |
| 592 | // Validate tensor shape |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 593 | 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] | 594 | if(_fortran_order) |
| 595 | { |
| 596 | for(size_t i = 0; i < _shape.size(); ++i) |
| 597 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 598 | 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] | 599 | } |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | for(size_t i = 0; i < _shape.size(); ++i) |
| 604 | { |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 605 | 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] | 606 | } |
| 607 | } |
| 608 | |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 609 | switch(tensor.info()->data_type()) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 610 | { |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 611 | case arm_compute::DataType::F32: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 612 | { |
| 613 | // Read data |
| 614 | if(tensor.info()->padding().empty()) |
| 615 | { |
| 616 | // If tensor has no padding read directly from stream. |
| 617 | _fs.read(reinterpret_cast<char *>(tensor.buffer()), tensor.info()->total_size()); |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | // If tensor has padding accessing tensor elements through execution window. |
| 622 | Window window; |
| 623 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 624 | |
| 625 | execute_window_loop(window, [&](const Coordinates & id) |
| 626 | { |
| 627 | _fs.read(reinterpret_cast<char *>(tensor.ptr_to_element(id)), tensor.info()->element_size()); |
| 628 | }); |
| 629 | } |
| 630 | |
| 631 | break; |
| 632 | } |
| 633 | default: |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 634 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // Unmap buffer if creating a CLTensor |
| 638 | unmap(tensor); |
| 639 | } |
| 640 | catch(const std::ifstream::failure &e) |
| 641 | { |
| 642 | ARM_COMPUTE_ERROR("Loading NPY file: %s", e.what()); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | private: |
| 647 | std::ifstream _fs; |
| 648 | std::vector<unsigned long> _shape; |
| 649 | bool _fortran_order; |
| 650 | std::string _typestring; |
| 651 | }; |
| 652 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 653 | /** Template helper function to save a tensor image to a PPM file. |
| 654 | * |
| 655 | * @note Only U8 and RGB888 formats supported. |
| 656 | * @note Only works with 2D tensors. |
| 657 | * @note If the input tensor is a CLTensor, the function maps and unmaps the image |
| 658 | * |
| 659 | * @param[in] tensor The tensor to save as PPM file |
| 660 | * @param[in] ppm_filename Filename of the file to create. |
| 661 | */ |
| 662 | template <typename T> |
| 663 | void save_to_ppm(T &tensor, const std::string &ppm_filename) |
| 664 | { |
| 665 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8); |
| 666 | ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2); |
| 667 | |
| 668 | std::ofstream fs; |
| 669 | |
| 670 | try |
| 671 | { |
| 672 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 673 | fs.open(ppm_filename, std::ios::out | std::ios::binary); |
| 674 | |
| 675 | const unsigned int width = tensor.info()->tensor_shape()[0]; |
| 676 | const unsigned int height = tensor.info()->tensor_shape()[1]; |
| 677 | |
| 678 | fs << "P6\n" |
| 679 | << width << " " << height << " 255\n"; |
| 680 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 681 | // Map buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 682 | map(tensor, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 683 | |
| 684 | switch(tensor.info()->format()) |
| 685 | { |
| 686 | case arm_compute::Format::U8: |
| 687 | { |
| 688 | arm_compute::Window window; |
| 689 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1)); |
| 690 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 691 | |
| 692 | arm_compute::Iterator in(&tensor, window); |
| 693 | |
| 694 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 695 | { |
| 696 | const unsigned char value = *in.ptr(); |
| 697 | |
| 698 | fs << value << value << value; |
| 699 | }, |
| 700 | in); |
| 701 | |
| 702 | break; |
| 703 | } |
| 704 | case arm_compute::Format::RGB888: |
| 705 | { |
| 706 | arm_compute::Window window; |
| 707 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width)); |
| 708 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 709 | |
| 710 | arm_compute::Iterator in(&tensor, window); |
| 711 | |
| 712 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 713 | { |
| 714 | fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), width * tensor.info()->element_size()); |
| 715 | }, |
| 716 | in); |
| 717 | |
| 718 | break; |
| 719 | } |
| 720 | default: |
| 721 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 722 | } |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 723 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 724 | // Unmap buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 725 | unmap(tensor); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 726 | } |
| 727 | catch(const std::ofstream::failure &e) |
| 728 | { |
| 729 | ARM_COMPUTE_ERROR("Writing %s: (%s)", ppm_filename.c_str(), e.what()); |
| 730 | } |
| 731 | } |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 732 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 733 | /** Template helper function to save a tensor image to a NPY file. |
| 734 | * |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 735 | * @note Only F32 data type supported. |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 736 | * @note Only works with 2D tensors. |
| 737 | * @note If the input tensor is a CLTensor, the function maps and unmaps the image |
| 738 | * |
| 739 | * @param[in] tensor The tensor to save as NPY file |
| 740 | * @param[in] npy_filename Filename of the file to create. |
| 741 | * @param[in] fortran_order If true, save matrix in fortran order. |
| 742 | */ |
| 743 | template <typename T> |
| 744 | void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order) |
| 745 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 746 | 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] | 747 | ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2); |
| 748 | |
| 749 | std::ofstream fs; |
| 750 | |
| 751 | try |
| 752 | { |
| 753 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 754 | fs.open(npy_filename, std::ios::out | std::ios::binary); |
| 755 | |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 756 | const unsigned int width = tensor.info()->tensor_shape()[0]; |
| 757 | const unsigned int height = tensor.info()->tensor_shape()[1]; |
| 758 | std::vector<npy::ndarray_len_t> shape(2); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 759 | |
| 760 | if(!fortran_order) |
| 761 | { |
| 762 | shape[0] = height, shape[1] = width; |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | shape[0] = width, shape[1] = height; |
| 767 | } |
| 768 | |
| 769 | // Map buffer if creating a CLTensor |
| 770 | map(tensor, true); |
| 771 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 772 | switch(tensor.info()->data_type()) |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 773 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 774 | case arm_compute::DataType::F32: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 775 | { |
| 776 | std::vector<float> tmp; /* Used only to get the typestring */ |
| 777 | npy::Typestring typestring_o{ tmp }; |
| 778 | std::string typestring = typestring_o.str(); |
| 779 | |
| 780 | std::ofstream stream(npy_filename, std::ofstream::binary); |
Anthony Barbier | 87f21cd | 2017-11-10 16:27:32 +0000 | [diff] [blame] | 781 | npy::write_header(stream, typestring, fortran_order, shape); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 782 | |
| 783 | arm_compute::Window window; |
| 784 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1)); |
| 785 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 786 | |
| 787 | arm_compute::Iterator in(&tensor, window); |
| 788 | |
| 789 | arm_compute::execute_window_loop(window, [&](const arm_compute::Coordinates & id) |
| 790 | { |
| 791 | stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(float)); |
| 792 | }, |
| 793 | in); |
| 794 | |
| 795 | break; |
| 796 | } |
| 797 | default: |
| 798 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 799 | } |
| 800 | |
| 801 | // Unmap buffer if creating a CLTensor |
| 802 | unmap(tensor); |
| 803 | } |
| 804 | catch(const std::ofstream::failure &e) |
| 805 | { |
| 806 | ARM_COMPUTE_ERROR("Writing %s: (%s)", npy_filename.c_str(), e.what()); |
| 807 | } |
| 808 | } |
| 809 | |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 810 | /** Load the tensor with pre-trained data from a binary file |
| 811 | * |
| 812 | * @param[in] tensor The tensor to be filled. Data type supported: F32. |
| 813 | * @param[in] filename Filename of the binary file to load from. |
| 814 | */ |
| 815 | template <typename T> |
| 816 | void load_trained_data(T &tensor, const std::string &filename) |
| 817 | { |
| 818 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&tensor, 1, DataType::F32); |
| 819 | |
| 820 | std::ifstream fs; |
| 821 | |
| 822 | try |
| 823 | { |
| 824 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 825 | // Open file |
| 826 | fs.open(filename, std::ios::in | std::ios::binary); |
| 827 | |
| 828 | if(!fs.good()) |
| 829 | { |
| 830 | throw std::runtime_error("Could not load binary data: " + filename); |
| 831 | } |
| 832 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 833 | // Map buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 834 | map(tensor, true); |
| 835 | |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 836 | Window window; |
| 837 | |
| 838 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, 1, 1)); |
| 839 | |
| 840 | for(unsigned int d = 1; d < tensor.info()->num_dimensions(); ++d) |
| 841 | { |
| 842 | window.set(d, Window::Dimension(0, tensor.info()->tensor_shape()[d], 1)); |
| 843 | } |
| 844 | |
| 845 | arm_compute::Iterator in(&tensor, window); |
| 846 | |
| 847 | execute_window_loop(window, [&](const Coordinates & id) |
| 848 | { |
| 849 | fs.read(reinterpret_cast<std::fstream::char_type *>(in.ptr()), tensor.info()->tensor_shape()[0] * tensor.info()->element_size()); |
| 850 | }, |
| 851 | in); |
| 852 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 853 | // Unmap buffer if creating a CLTensor/GCTensor |
Georgios Pinitas | dc836b6 | 2017-09-20 14:02:37 +0100 | [diff] [blame] | 854 | unmap(tensor); |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 855 | } |
| 856 | catch(const std::ofstream::failure &e) |
| 857 | { |
| 858 | ARM_COMPUTE_ERROR("Writing %s: (%s)", filename.c_str(), e.what()); |
| 859 | } |
| 860 | } |
| 861 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 862 | template <typename T> |
| 863 | void fill_random_tensor(T &tensor, float lower_bound, float upper_bound) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 864 | { |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 865 | std::random_device rd; |
| 866 | std::mt19937 gen(rd()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 867 | |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 868 | Window window; |
Michalis Spyrou | 5e69bb4 | 2018-03-09 16:36:00 +0000 | [diff] [blame] | 869 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 870 | |
| 871 | map(tensor, true); |
| 872 | |
| 873 | Iterator it(&tensor, window); |
| 874 | |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 875 | switch(tensor.info()->data_type()) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 876 | { |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 877 | case arm_compute::DataType::F32: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 878 | { |
| 879 | std::uniform_real_distribution<float> dist(lower_bound, upper_bound); |
| 880 | |
| 881 | execute_window_loop(window, [&](const Coordinates & id) |
| 882 | { |
| 883 | *reinterpret_cast<float *>(it.ptr()) = dist(gen); |
| 884 | }, |
| 885 | it); |
| 886 | |
| 887 | break; |
| 888 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 889 | default: |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 890 | { |
| 891 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 892 | } |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 893 | } |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 894 | |
| 895 | unmap(tensor); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 896 | } |
Giorgio Arena | cf3935f | 2017-10-26 17:14:13 +0100 | [diff] [blame] | 897 | |
| 898 | template <typename T> |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 899 | 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] | 900 | { |
Gian Marco | 0bc5a25 | 2017-12-04 13:55:08 +0000 | [diff] [blame] | 901 | 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] | 902 | } |
Gian Marco | 5ca7409 | 2018-02-08 16:21:54 +0000 | [diff] [blame] | 903 | /** This function returns the amount of memory free reading from /proc/meminfo |
| 904 | * |
| 905 | * @return The free memory in kB |
| 906 | */ |
| 907 | uint64_t get_mem_free_from_meminfo(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 908 | } // namespace utils |
| 909 | } // namespace arm_compute |
| 910 | #endif /* __UTILS_UTILS_H__*/ |