Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +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 | */ |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 24 | #ifndef __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ |
| 25 | #define __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 26 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/PixelValue.h" |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Utils.h" |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/Utility.h" |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 30 | #include "arm_compute/graph/Graph.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 31 | #include "arm_compute/graph/ITensorAccessor.h" |
| 32 | #include "arm_compute/graph/Types.h" |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/Tensor.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 34 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 35 | #include "utils/CommonGraphOptions.h" |
| 36 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 37 | #include <array> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 38 | #include <random> |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <vector> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 41 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace graph_utils |
| 45 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 46 | /** Preprocessor interface **/ |
| 47 | class IPreprocessor |
| 48 | { |
| 49 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 50 | /** Default destructor. */ |
| 51 | virtual ~IPreprocessor() = default; |
| 52 | /** Preprocess the given tensor. |
| 53 | * |
| 54 | * @param[in] tensor Tensor to preprocess. |
| 55 | */ |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 56 | virtual void preprocess(ITensor &tensor) = 0; |
| 57 | }; |
| 58 | |
| 59 | /** Caffe preproccessor */ |
| 60 | class CaffePreproccessor : public IPreprocessor |
| 61 | { |
| 62 | public: |
| 63 | /** Default Constructor |
| 64 | * |
Georgios Pinitas | b54c644 | 2019-04-03 13:18:14 +0100 | [diff] [blame] | 65 | * @param[in] mean Mean array in RGB ordering |
| 66 | * @param[in] bgr Boolean specifying if the preprocessing should assume BGR format |
| 67 | * @param[in] scale Scale value |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 68 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3>{{0, 0, 0}}, bool bgr = true, float scale = 1.f); |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 70 | void preprocess(ITensor &tensor) override; |
| 71 | |
| 72 | private: |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 73 | template <typename T> |
| 74 | void preprocess_typed(ITensor &tensor); |
| 75 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 76 | std::array<float, 3> _mean; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | bool _bgr; |
| 78 | float _scale; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** TF preproccessor */ |
| 82 | class TFPreproccessor : public IPreprocessor |
| 83 | { |
| 84 | public: |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 85 | /** Constructor |
| 86 | * |
| 87 | * @param[in] min_range Min normalization range. (Defaults to -1.f) |
| 88 | * @param[in] max_range Max normalization range. (Defaults to 1.f) |
| 89 | */ |
| 90 | TFPreproccessor(float min_range = -1.f, float max_range = 1.f); |
| 91 | |
| 92 | // Inherited overriden methods |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 93 | void preprocess(ITensor &tensor) override; |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 94 | |
| 95 | private: |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 96 | template <typename T> |
| 97 | void preprocess_typed(ITensor &tensor); |
| 98 | |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 99 | float _min_range; |
| 100 | float _max_range; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 103 | /** PPM writer class */ |
| 104 | class PPMWriter : public graph::ITensorAccessor |
| 105 | { |
| 106 | public: |
| 107 | /** Constructor |
| 108 | * |
| 109 | * @param[in] name PPM file name |
| 110 | * @param[in] maximum Maximum elements to access |
| 111 | */ |
| 112 | PPMWriter(std::string name, unsigned int maximum = 1); |
| 113 | /** Allows instances to move constructed */ |
| 114 | PPMWriter(PPMWriter &&) = default; |
| 115 | |
| 116 | // Inherited methods overriden: |
| 117 | bool access_tensor(ITensor &tensor) override; |
| 118 | |
| 119 | private: |
| 120 | const std::string _name; |
| 121 | unsigned int _iterator; |
| 122 | unsigned int _maximum; |
| 123 | }; |
| 124 | |
| 125 | /** Dummy accessor class */ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 126 | class DummyAccessor final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 127 | { |
| 128 | public: |
| 129 | /** Constructor |
| 130 | * |
| 131 | * @param[in] maximum Maximum elements to write |
| 132 | */ |
| 133 | DummyAccessor(unsigned int maximum = 1); |
| 134 | /** Allows instances to move constructed */ |
| 135 | DummyAccessor(DummyAccessor &&) = default; |
| 136 | |
| 137 | // Inherited methods overriden: |
Giorgio Arena | 9c67d38 | 2021-08-20 15:24:03 +0100 | [diff] [blame] | 138 | bool access_tensor_data() override; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 139 | bool access_tensor(ITensor &tensor) override; |
| 140 | |
| 141 | private: |
| 142 | unsigned int _iterator; |
| 143 | unsigned int _maximum; |
| 144 | }; |
| 145 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 146 | /** NumPy accessor class */ |
| 147 | class NumPyAccessor final : public graph::ITensorAccessor |
| 148 | { |
| 149 | public: |
| 150 | /** Constructor |
| 151 | * |
| 152 | * @param[in] npy_path Path to npy file. |
| 153 | * @param[in] shape Shape of the numpy tensor data. |
| 154 | * @param[in] data_type DataType of the numpy tensor data. |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 155 | * @param[in] data_layout (Optional) DataLayout of the numpy tensor data. |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 156 | * @param[out] output_stream (Optional) Output stream |
| 157 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | NumPyAccessor(std::string npy_path, |
| 159 | TensorShape shape, |
| 160 | DataType data_type, |
| 161 | DataLayout data_layout = DataLayout::NCHW, |
| 162 | std::ostream &output_stream = std::cout); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 163 | /** Allow instances of this class to be move constructed */ |
| 164 | NumPyAccessor(NumPyAccessor &&) = default; |
| 165 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 166 | NumPyAccessor(const NumPyAccessor &) = delete; |
| 167 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 168 | NumPyAccessor &operator=(const NumPyAccessor &) = delete; |
| 169 | |
| 170 | // Inherited methods overriden: |
| 171 | bool access_tensor(ITensor &tensor) override; |
| 172 | |
| 173 | private: |
| 174 | template <typename T> |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 175 | void access_numpy_tensor(ITensor &tensor, T tolerance); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 176 | |
| 177 | Tensor _npy_tensor; |
| 178 | const std::string _filename; |
| 179 | std::ostream &_output_stream; |
| 180 | }; |
| 181 | |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 182 | /** SaveNumPy accessor class */ |
| 183 | class SaveNumPyAccessor final : public graph::ITensorAccessor |
| 184 | { |
| 185 | public: |
| 186 | /** Constructor |
| 187 | * |
| 188 | * @param[in] npy_name Npy file name. |
| 189 | * @param[in] is_fortran (Optional) If true, save tensor in fortran order. |
| 190 | */ |
| 191 | SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false); |
| 192 | /** Allow instances of this class to be move constructed */ |
| 193 | SaveNumPyAccessor(SaveNumPyAccessor &&) = default; |
| 194 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 195 | SaveNumPyAccessor(const SaveNumPyAccessor &) = delete; |
| 196 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 197 | SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete; |
| 198 | |
| 199 | // Inherited methods overriden: |
| 200 | bool access_tensor(ITensor &tensor) override; |
| 201 | |
| 202 | private: |
| 203 | const std::string _npy_name; |
| 204 | const bool _is_fortran; |
| 205 | }; |
| 206 | |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 207 | /** Print accessor class |
| 208 | * @note The print accessor will print only when asserts are enabled. |
| 209 | * */ |
| 210 | class PrintAccessor final : public graph::ITensorAccessor |
| 211 | { |
| 212 | public: |
| 213 | /** Constructor |
| 214 | * |
| 215 | * @param[out] output_stream (Optional) Output stream |
| 216 | * @param[in] io_fmt (Optional) Format information |
| 217 | */ |
| 218 | PrintAccessor(std::ostream &output_stream = std::cout, IOFormatInfo io_fmt = IOFormatInfo()); |
| 219 | /** Allow instances of this class to be move constructed */ |
| 220 | PrintAccessor(PrintAccessor &&) = default; |
| 221 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 222 | PrintAccessor(const PrintAccessor &) = delete; |
| 223 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 224 | PrintAccessor &operator=(const PrintAccessor &) = delete; |
| 225 | |
| 226 | // Inherited methods overriden: |
| 227 | bool access_tensor(ITensor &tensor) override; |
| 228 | |
| 229 | private: |
| 230 | std::ostream &_output_stream; |
| 231 | IOFormatInfo _io_fmt; |
| 232 | }; |
| 233 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 234 | /** Image accessor class */ |
| 235 | class ImageAccessor final : public graph::ITensorAccessor |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 236 | { |
| 237 | public: |
| 238 | /** Constructor |
| 239 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 240 | * @param[in] filename Image file |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 241 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 242 | * @param[in] preprocessor (Optional) Image pre-processing object |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 243 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 244 | ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr); |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 245 | /** Allow instances of this class to be move constructed */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 246 | ImageAccessor(ImageAccessor &&) = default; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 247 | |
| 248 | // Inherited methods overriden: |
| 249 | bool access_tensor(ITensor &tensor) override; |
| 250 | |
| 251 | private: |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 252 | bool _already_loaded; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 253 | const std::string _filename; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 254 | const bool _bgr; |
| 255 | std::unique_ptr<IPreprocessor> _preprocessor; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 256 | }; |
| 257 | |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 258 | /** Input Accessor used for network validation */ |
| 259 | class ValidationInputAccessor final : public graph::ITensorAccessor |
| 260 | { |
| 261 | public: |
| 262 | /** Constructor |
| 263 | * |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 264 | * @param[in] image_list File containing all the images to validate |
| 265 | * @param[in] images_path Path to images. |
| 266 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format) |
| 267 | * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr) |
| 268 | * @param[in] start (Optional) Start range |
| 269 | * @param[in] end (Optional) End range |
| 270 | * @param[out] output_stream (Optional) Output stream |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 271 | * |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 272 | * @note Range is defined as [start, end] |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 273 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 274 | ValidationInputAccessor(const std::string &image_list, |
| 275 | std::string images_path, |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 276 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 277 | bool bgr = true, |
| 278 | unsigned int start = 0, |
| 279 | unsigned int end = 0, |
| 280 | std::ostream &output_stream = std::cout); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 281 | |
| 282 | // Inherited methods overriden: |
| 283 | bool access_tensor(ITensor &tensor) override; |
| 284 | |
| 285 | private: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 286 | std::string _path; |
| 287 | std::vector<std::string> _images; |
| 288 | std::unique_ptr<IPreprocessor> _preprocessor; |
| 289 | bool _bgr; |
| 290 | size_t _offset; |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 291 | std::ostream &_output_stream; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 292 | }; |
| 293 | |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 294 | /** Output Accessor used for network validation */ |
| 295 | class ValidationOutputAccessor final : public graph::ITensorAccessor |
| 296 | { |
| 297 | public: |
| 298 | /** Default Constructor |
| 299 | * |
| 300 | * @param[in] image_list File containing all the images and labels results |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 301 | * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream) |
| 302 | * @param[in] start (Optional) Start range |
| 303 | * @param[in] end (Optional) End range |
| 304 | * |
| 305 | * @note Range is defined as [start, end] |
| 306 | */ |
| 307 | ValidationOutputAccessor(const std::string &image_list, |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 308 | std::ostream &output_stream = std::cout, |
| 309 | unsigned int start = 0, |
| 310 | unsigned int end = 0); |
| 311 | /** Reset accessor state */ |
| 312 | void reset(); |
| 313 | |
| 314 | // Inherited methods overriden: |
| 315 | bool access_tensor(ITensor &tensor) override; |
| 316 | |
| 317 | private: |
| 318 | /** Access predictions of the tensor |
| 319 | * |
| 320 | * @tparam T Tensor elements type |
| 321 | * |
| 322 | * @param[in] tensor Tensor to read the predictions from |
| 323 | */ |
| 324 | template <typename T> |
| 325 | std::vector<size_t> access_predictions_tensor(ITensor &tensor); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 326 | /** Aggregates the results of a sample |
| 327 | * |
| 328 | * @param[in] res Vector containing the results of a graph |
| 329 | * @param[in,out] positive_samples Positive samples to be updated |
| 330 | * @param[in] top_n Top n accuracy to measure |
| 331 | * @param[in] correct_label Correct label of the current sample |
| 332 | */ |
| 333 | void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label); |
| 334 | /** Reports top N accuracy |
| 335 | * |
| 336 | * @param[in] top_n Top N accuracy that is being reported |
| 337 | * @param[in] total_samples Total number of samples |
| 338 | * @param[in] positive_samples Positive samples |
| 339 | */ |
| 340 | void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples); |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 341 | |
| 342 | private: |
| 343 | std::vector<int> _results; |
| 344 | std::ostream &_output_stream; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 345 | size_t _offset; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 346 | size_t _positive_samples_top1; |
| 347 | size_t _positive_samples_top5; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 348 | }; |
| 349 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 350 | /** Detection output accessor class */ |
| 351 | class DetectionOutputAccessor final : public graph::ITensorAccessor |
| 352 | { |
| 353 | public: |
| 354 | /** Constructor |
| 355 | * |
| 356 | * @param[in] labels_path Path to labels text file. |
| 357 | * @param[in] imgs_tensor_shapes Network input images tensor shapes. |
| 358 | * @param[out] output_stream (Optional) Output stream |
| 359 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 360 | DetectionOutputAccessor(const std::string &labels_path, |
| 361 | std::vector<TensorShape> &imgs_tensor_shapes, |
| 362 | std::ostream &output_stream = std::cout); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 363 | /** Allow instances of this class to be move constructed */ |
| 364 | DetectionOutputAccessor(DetectionOutputAccessor &&) = default; |
| 365 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 366 | DetectionOutputAccessor(const DetectionOutputAccessor &) = delete; |
| 367 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 368 | DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete; |
| 369 | |
| 370 | // Inherited methods overriden: |
| 371 | bool access_tensor(ITensor &tensor) override; |
| 372 | |
| 373 | private: |
| 374 | template <typename T> |
| 375 | void access_predictions_tensor(ITensor &tensor); |
| 376 | |
| 377 | std::vector<std::string> _labels; |
| 378 | std::vector<TensorShape> _tensor_shapes; |
| 379 | std::ostream &_output_stream; |
| 380 | }; |
| 381 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 382 | /** Result accessor class */ |
| 383 | class TopNPredictionsAccessor final : public graph::ITensorAccessor |
| 384 | { |
| 385 | public: |
| 386 | /** Constructor |
| 387 | * |
| 388 | * @param[in] labels_path Path to labels text file. |
| 389 | * @param[in] top_n (Optional) Number of output classes to print |
| 390 | * @param[out] output_stream (Optional) Output stream |
| 391 | */ |
| 392 | TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout); |
| 393 | /** Allow instances of this class to be move constructed */ |
| 394 | TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default; |
| 395 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 396 | TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete; |
| 397 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 398 | TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete; |
| 399 | |
| 400 | // Inherited methods overriden: |
| 401 | bool access_tensor(ITensor &tensor) override; |
| 402 | |
| 403 | private: |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 404 | template <typename T> |
| 405 | void access_predictions_tensor(ITensor &tensor); |
| 406 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 407 | std::vector<std::string> _labels; |
| 408 | std::ostream &_output_stream; |
| 409 | size_t _top_n; |
| 410 | }; |
| 411 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 412 | /** Random accessor class */ |
| 413 | class RandomAccessor final : public graph::ITensorAccessor |
| 414 | { |
| 415 | public: |
| 416 | /** Constructor |
| 417 | * |
| 418 | * @param[in] lower Lower bound value. |
| 419 | * @param[in] upper Upper bound value. |
| 420 | * @param[in] seed (Optional) Seed used to initialise the random number generator. |
| 421 | */ |
| 422 | RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0); |
| 423 | /** Allows instances to move constructed */ |
| 424 | RandomAccessor(RandomAccessor &&) = default; |
| 425 | |
| 426 | // Inherited methods overriden: |
| 427 | bool access_tensor(ITensor &tensor) override; |
| 428 | |
| 429 | private: |
| 430 | template <typename T, typename D> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 431 | void fill(ITensor &tensor, D &&distribution); |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 432 | PixelValue _lower; |
| 433 | PixelValue _upper; |
| 434 | std::random_device::result_type _seed; |
| 435 | }; |
| 436 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 437 | /** Numpy Binary loader class*/ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 438 | class NumPyBinLoader final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 439 | { |
| 440 | public: |
| 441 | /** Default Constructor |
| 442 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 443 | * @param[in] filename Binary file name |
| 444 | * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 445 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 446 | NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 447 | /** Allows instances to move constructed */ |
| 448 | NumPyBinLoader(NumPyBinLoader &&) = default; |
| 449 | |
| 450 | // Inherited methods overriden: |
| 451 | bool access_tensor(ITensor &tensor) override; |
| 452 | |
| 453 | private: |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 454 | bool _already_loaded; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 455 | const std::string _filename; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 456 | const DataLayout _file_layout; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 457 | }; |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 458 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 459 | /** Generates appropriate random accessor |
| 460 | * |
| 461 | * @param[in] lower Lower random values bound |
| 462 | * @param[in] upper Upper random values bound |
| 463 | * @param[in] seed Random generator seed |
| 464 | * |
| 465 | * @return A ramdom accessor |
| 466 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 467 | inline std::unique_ptr<graph::ITensorAccessor> |
| 468 | get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0) |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 469 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 470 | return std::make_unique<RandomAccessor>(lower, upper, seed); |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 473 | /** Generates appropriate weights accessor according to the specified path |
| 474 | * |
| 475 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 476 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 477 | * @param[in] path Path to the data files |
| 478 | * @param[in] data_file Relative path to the data files from path |
| 479 | * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 480 | * |
| 481 | * @return An appropriate tensor accessor |
| 482 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 483 | inline std::unique_ptr<graph::ITensorAccessor> |
| 484 | get_weights_accessor(const std::string &path, const std::string &data_file, DataLayout file_layout = DataLayout::NCHW) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 485 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 486 | if (path.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 487 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 488 | return std::make_unique<DummyAccessor>(); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 492 | return std::make_unique<NumPyBinLoader>(path + data_file, file_layout); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 496 | /** Generates appropriate input accessor according to the specified graph parameters |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 497 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 498 | * @param[in] graph_parameters Graph parameters |
| 499 | * @param[in] preprocessor (Optional) Preproccessor object |
| 500 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 501 | * |
| 502 | * @return An appropriate tensor accessor |
| 503 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 504 | inline std::unique_ptr<graph::ITensorAccessor> |
| 505 | get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 506 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 507 | bool bgr = true) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 508 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 509 | if (!graph_parameters.validation_file.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 510 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 511 | return std::make_unique<ValidationInputAccessor>( |
| 512 | graph_parameters.validation_file, graph_parameters.validation_path, std::move(preprocessor), bgr, |
| 513 | graph_parameters.validation_range_start, graph_parameters.validation_range_end); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 514 | } |
| 515 | else |
| 516 | { |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 517 | const std::string &image_file = graph_parameters.image; |
| 518 | const std::string &image_file_lower = lower_string(image_file); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 519 | if (arm_compute::utility::endswith(image_file_lower, ".npy")) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 520 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 521 | return std::make_unique<NumPyBinLoader>(image_file, graph_parameters.data_layout); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 522 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 523 | else if (arm_compute::utility::endswith(image_file_lower, ".jpeg") || |
| 524 | arm_compute::utility::endswith(image_file_lower, ".jpg") || |
| 525 | arm_compute::utility::endswith(image_file_lower, ".ppm")) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 526 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 527 | return std::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor)); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 528 | } |
| 529 | else |
| 530 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 531 | return std::make_unique<DummyAccessor>(); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 532 | } |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 536 | /** Generates appropriate output accessor according to the specified graph parameters |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 537 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 538 | * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated |
| 539 | * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 540 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 541 | * @param[in] graph_parameters Graph parameters |
| 542 | * @param[in] top_n (Optional) Number of output classes to print (default = 5) |
| 543 | * @param[in] is_validation (Optional) Validation flag (default = false) |
| 544 | * @param[out] output_stream (Optional) Output stream (default = std::cout) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 545 | * |
| 546 | * @return An appropriate tensor accessor |
| 547 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 548 | inline std::unique_ptr<graph::ITensorAccessor> |
| 549 | get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 550 | size_t top_n = 5, |
| 551 | bool is_validation = false, |
| 552 | std::ostream &output_stream = std::cout) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 553 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 554 | ARM_COMPUTE_UNUSED(is_validation); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 555 | if (!graph_parameters.validation_file.empty()) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 556 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 557 | return std::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, output_stream, |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 558 | graph_parameters.validation_range_start, |
| 559 | graph_parameters.validation_range_end); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 560 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 561 | else if (graph_parameters.labels.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 562 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 563 | return std::make_unique<DummyAccessor>(0); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 564 | } |
| 565 | else |
| 566 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 567 | return std::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 570 | /** Generates appropriate output accessor according to the specified graph parameters |
| 571 | * |
| 572 | * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated |
| 573 | * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor |
| 574 | * |
| 575 | * @param[in] graph_parameters Graph parameters |
| 576 | * @param[in] tensor_shapes Network input images tensor shapes. |
| 577 | * @param[in] is_validation (Optional) Validation flag (default = false) |
| 578 | * @param[out] output_stream (Optional) Output stream (default = std::cout) |
| 579 | * |
| 580 | * @return An appropriate tensor accessor |
| 581 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 582 | inline std::unique_ptr<graph::ITensorAccessor> |
| 583 | get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 584 | std::vector<TensorShape> tensor_shapes, |
| 585 | bool is_validation = false, |
| 586 | std::ostream &output_stream = std::cout) |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 587 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 588 | ARM_COMPUTE_UNUSED(is_validation); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 589 | if (!graph_parameters.validation_file.empty()) |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 590 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 591 | return std::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, output_stream, |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 592 | graph_parameters.validation_range_start, |
| 593 | graph_parameters.validation_range_end); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 594 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 595 | else if (graph_parameters.labels.empty()) |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 596 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 597 | return std::make_unique<DummyAccessor>(0); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 598 | } |
| 599 | else |
| 600 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 601 | return std::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream); |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 604 | /** Generates appropriate npy output accessor according to the specified npy_path |
| 605 | * |
| 606 | * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor |
| 607 | * |
| 608 | * @param[in] npy_path Path to npy file. |
| 609 | * @param[in] shape Shape of the numpy tensor data. |
| 610 | * @param[in] data_type DataType of the numpy tensor data. |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 611 | * @param[in] data_layout DataLayout of the numpy tensor data. |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 612 | * @param[out] output_stream (Optional) Output stream |
| 613 | * |
| 614 | * @return An appropriate tensor accessor |
| 615 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 616 | inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, |
| 617 | TensorShape shape, |
| 618 | DataType data_type, |
| 619 | DataLayout data_layout = DataLayout::NCHW, |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 620 | std::ostream &output_stream = std::cout) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 621 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 622 | if (npy_path.empty()) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 623 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 624 | return std::make_unique<DummyAccessor>(0); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 625 | } |
| 626 | else |
| 627 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 628 | return std::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 629 | } |
| 630 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 631 | |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 632 | /** Generates appropriate npy output accessor according to the specified npy_path |
| 633 | * |
| 634 | * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor |
| 635 | * |
| 636 | * @param[in] npy_name Npy filename. |
| 637 | * @param[in] is_fortran (Optional) If true, save tensor in fortran order. |
| 638 | * |
| 639 | * @return An appropriate tensor accessor |
| 640 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 641 | inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, |
| 642 | const bool is_fortran = false) |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 643 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 644 | if (npy_name.empty()) |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 645 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 646 | return std::make_unique<DummyAccessor>(0); |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 647 | } |
| 648 | else |
| 649 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 650 | return std::make_unique<SaveNumPyAccessor>(npy_name, is_fortran); |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 654 | /** Generates print tensor accessor |
| 655 | * |
| 656 | * @param[out] output_stream (Optional) Output stream |
| 657 | * |
| 658 | * @return A print tensor accessor |
| 659 | */ |
| 660 | inline std::unique_ptr<graph::ITensorAccessor> get_print_output_accessor(std::ostream &output_stream = std::cout) |
| 661 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 662 | return std::make_unique<PrintAccessor>(output_stream); |
Isabella Gottardi | cd4e9ab | 2019-11-05 17:50:27 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 665 | /** Permutes a given tensor shape given the input and output data layout |
| 666 | * |
| 667 | * @param[in] tensor_shape Tensor shape to permute |
| 668 | * @param[in] in_data_layout Input tensor shape data layout |
| 669 | * @param[in] out_data_layout Output tensor shape data layout |
| 670 | * |
| 671 | * @return Permuted tensor shape |
| 672 | */ |
| 673 | inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout) |
| 674 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 675 | if (in_data_layout != out_data_layout) |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 676 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 677 | arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) |
| 678 | ? arm_compute::PermutationVector(2U, 0U, 1U) |
| 679 | : arm_compute::PermutationVector(1U, 2U, 0U); |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 680 | arm_compute::permute(tensor_shape, perm_vec); |
| 681 | } |
| 682 | return tensor_shape; |
| 683 | } |
| 684 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 685 | /** Utility function to return the TargetHint |
| 686 | * |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 687 | * @param[in] target Integer value which expresses the selected target. Must be 0 for Arm® Neon™ or 1 for OpenCL or 2 (OpenCL with Tuner) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 688 | * |
| 689 | * @return the TargetHint |
| 690 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 691 | inline graph::Target set_target_hint(int target) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 692 | { |
Michele Di Giorgio | 40efd53 | 2021-03-18 17:32:00 +0000 | [diff] [blame] | 693 | ARM_COMPUTE_ERROR_ON_MSG(target > 2, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner)"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 694 | if ((target == 1 || target == 2)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 695 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 696 | return graph::Target::CL; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 697 | } |
| 698 | else |
| 699 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 700 | return graph::Target::NEON; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 703 | } // namespace graph_utils |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 704 | } // namespace arm_compute |
| 705 | |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 706 | #endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */ |