Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_GRAPH_UTILS_H__ |
| 25 | #define __ARM_COMPUTE_GRAPH_UTILS_H__ |
| 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 | * |
| 65 | * @param mean Mean array in RGB ordering |
| 66 | * @param bgr Boolean specifying if the preprocessing should assume BGR format |
| 67 | */ |
| 68 | CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true); |
| 69 | void preprocess(ITensor &tensor) override; |
| 70 | |
| 71 | private: |
| 72 | std::array<float, 3> _mean; |
| 73 | bool _bgr; |
| 74 | }; |
| 75 | |
| 76 | /** TF preproccessor */ |
| 77 | class TFPreproccessor : public IPreprocessor |
| 78 | { |
| 79 | public: |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 80 | /** Constructor |
| 81 | * |
| 82 | * @param[in] min_range Min normalization range. (Defaults to -1.f) |
| 83 | * @param[in] max_range Max normalization range. (Defaults to 1.f) |
| 84 | */ |
| 85 | TFPreproccessor(float min_range = -1.f, float max_range = 1.f); |
| 86 | |
| 87 | // Inherited overriden methods |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 88 | void preprocess(ITensor &tensor) override; |
Georgios Pinitas | be2772a | 2018-08-17 15:33:39 +0100 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | float _min_range; |
| 92 | float _max_range; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 95 | /** PPM writer class */ |
| 96 | class PPMWriter : public graph::ITensorAccessor |
| 97 | { |
| 98 | public: |
| 99 | /** Constructor |
| 100 | * |
| 101 | * @param[in] name PPM file name |
| 102 | * @param[in] maximum Maximum elements to access |
| 103 | */ |
| 104 | PPMWriter(std::string name, unsigned int maximum = 1); |
| 105 | /** Allows instances to move constructed */ |
| 106 | PPMWriter(PPMWriter &&) = default; |
| 107 | |
| 108 | // Inherited methods overriden: |
| 109 | bool access_tensor(ITensor &tensor) override; |
| 110 | |
| 111 | private: |
| 112 | const std::string _name; |
| 113 | unsigned int _iterator; |
| 114 | unsigned int _maximum; |
| 115 | }; |
| 116 | |
| 117 | /** Dummy accessor class */ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 118 | class DummyAccessor final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 119 | { |
| 120 | public: |
| 121 | /** Constructor |
| 122 | * |
| 123 | * @param[in] maximum Maximum elements to write |
| 124 | */ |
| 125 | DummyAccessor(unsigned int maximum = 1); |
| 126 | /** Allows instances to move constructed */ |
| 127 | DummyAccessor(DummyAccessor &&) = default; |
| 128 | |
| 129 | // Inherited methods overriden: |
| 130 | bool access_tensor(ITensor &tensor) override; |
| 131 | |
| 132 | private: |
| 133 | unsigned int _iterator; |
| 134 | unsigned int _maximum; |
| 135 | }; |
| 136 | |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 137 | /** NumPy accessor class */ |
| 138 | class NumPyAccessor final : public graph::ITensorAccessor |
| 139 | { |
| 140 | public: |
| 141 | /** Constructor |
| 142 | * |
| 143 | * @param[in] npy_path Path to npy file. |
| 144 | * @param[in] shape Shape of the numpy tensor data. |
| 145 | * @param[in] data_type DataType of the numpy tensor data. |
| 146 | * @param[out] output_stream (Optional) Output stream |
| 147 | */ |
| 148 | NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout); |
| 149 | /** Allow instances of this class to be move constructed */ |
| 150 | NumPyAccessor(NumPyAccessor &&) = default; |
| 151 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 152 | NumPyAccessor(const NumPyAccessor &) = delete; |
| 153 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 154 | NumPyAccessor &operator=(const NumPyAccessor &) = delete; |
| 155 | |
| 156 | // Inherited methods overriden: |
| 157 | bool access_tensor(ITensor &tensor) override; |
| 158 | |
| 159 | private: |
| 160 | template <typename T> |
| 161 | void access_numpy_tensor(ITensor &tensor); |
| 162 | |
| 163 | Tensor _npy_tensor; |
| 164 | const std::string _filename; |
| 165 | std::ostream &_output_stream; |
| 166 | }; |
| 167 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 168 | /** Image accessor class */ |
| 169 | class ImageAccessor final : public graph::ITensorAccessor |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 170 | { |
| 171 | public: |
| 172 | /** Constructor |
| 173 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 174 | * @param[in] filename Image file |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 175 | * @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] | 176 | * @param[in] preprocessor (Optional) Image pre-processing object |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 177 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 178 | 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] | 179 | /** Allow instances of this class to be move constructed */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 180 | ImageAccessor(ImageAccessor &&) = default; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 181 | |
| 182 | // Inherited methods overriden: |
| 183 | bool access_tensor(ITensor &tensor) override; |
| 184 | |
| 185 | private: |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 186 | bool _already_loaded; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 187 | const std::string _filename; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 188 | const bool _bgr; |
| 189 | std::unique_ptr<IPreprocessor> _preprocessor; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 192 | /** Input Accessor used for network validation */ |
| 193 | class ValidationInputAccessor final : public graph::ITensorAccessor |
| 194 | { |
| 195 | public: |
| 196 | /** Constructor |
| 197 | * |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 198 | * @param[in] image_list File containing all the images to validate |
| 199 | * @param[in] images_path Path to images. |
| 200 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format) |
| 201 | * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr) |
| 202 | * @param[in] start (Optional) Start range |
| 203 | * @param[in] end (Optional) End range |
| 204 | * @param[out] output_stream (Optional) Output stream |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 205 | * |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 206 | * @note Range is defined as [start, end] |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 207 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 208 | ValidationInputAccessor(const std::string &image_list, |
| 209 | std::string images_path, |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 210 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 211 | bool bgr = true, |
| 212 | unsigned int start = 0, |
| 213 | unsigned int end = 0, |
| 214 | std::ostream &output_stream = std::cout); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 215 | |
| 216 | // Inherited methods overriden: |
| 217 | bool access_tensor(ITensor &tensor) override; |
| 218 | |
| 219 | private: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 220 | std::string _path; |
| 221 | std::vector<std::string> _images; |
| 222 | std::unique_ptr<IPreprocessor> _preprocessor; |
| 223 | bool _bgr; |
| 224 | size_t _offset; |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 225 | std::ostream &_output_stream; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 226 | }; |
| 227 | |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 228 | /** Output Accessor used for network validation */ |
| 229 | class ValidationOutputAccessor final : public graph::ITensorAccessor |
| 230 | { |
| 231 | public: |
| 232 | /** Default Constructor |
| 233 | * |
| 234 | * @param[in] image_list File containing all the images and labels results |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 235 | * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream) |
| 236 | * @param[in] start (Optional) Start range |
| 237 | * @param[in] end (Optional) End range |
| 238 | * |
| 239 | * @note Range is defined as [start, end] |
| 240 | */ |
| 241 | ValidationOutputAccessor(const std::string &image_list, |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 242 | std::ostream &output_stream = std::cout, |
| 243 | unsigned int start = 0, |
| 244 | unsigned int end = 0); |
| 245 | /** Reset accessor state */ |
| 246 | void reset(); |
| 247 | |
| 248 | // Inherited methods overriden: |
| 249 | bool access_tensor(ITensor &tensor) override; |
| 250 | |
| 251 | private: |
| 252 | /** Access predictions of the tensor |
| 253 | * |
| 254 | * @tparam T Tensor elements type |
| 255 | * |
| 256 | * @param[in] tensor Tensor to read the predictions from |
| 257 | */ |
| 258 | template <typename T> |
| 259 | std::vector<size_t> access_predictions_tensor(ITensor &tensor); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 260 | /** Aggregates the results of a sample |
| 261 | * |
| 262 | * @param[in] res Vector containing the results of a graph |
| 263 | * @param[in,out] positive_samples Positive samples to be updated |
| 264 | * @param[in] top_n Top n accuracy to measure |
| 265 | * @param[in] correct_label Correct label of the current sample |
| 266 | */ |
| 267 | void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label); |
| 268 | /** Reports top N accuracy |
| 269 | * |
| 270 | * @param[in] top_n Top N accuracy that is being reported |
| 271 | * @param[in] total_samples Total number of samples |
| 272 | * @param[in] positive_samples Positive samples |
| 273 | */ |
| 274 | 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] | 275 | |
| 276 | private: |
| 277 | std::vector<int> _results; |
| 278 | std::ostream &_output_stream; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 279 | size_t _offset; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 280 | size_t _positive_samples_top1; |
| 281 | size_t _positive_samples_top5; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 282 | }; |
| 283 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 284 | /** Result accessor class */ |
| 285 | class TopNPredictionsAccessor final : public graph::ITensorAccessor |
| 286 | { |
| 287 | public: |
| 288 | /** Constructor |
| 289 | * |
| 290 | * @param[in] labels_path Path to labels text file. |
| 291 | * @param[in] top_n (Optional) Number of output classes to print |
| 292 | * @param[out] output_stream (Optional) Output stream |
| 293 | */ |
| 294 | TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout); |
| 295 | /** Allow instances of this class to be move constructed */ |
| 296 | TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default; |
| 297 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 298 | TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete; |
| 299 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 300 | TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete; |
| 301 | |
| 302 | // Inherited methods overriden: |
| 303 | bool access_tensor(ITensor &tensor) override; |
| 304 | |
| 305 | private: |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 306 | template <typename T> |
| 307 | void access_predictions_tensor(ITensor &tensor); |
| 308 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 309 | std::vector<std::string> _labels; |
| 310 | std::ostream &_output_stream; |
| 311 | size_t _top_n; |
| 312 | }; |
| 313 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 314 | /** Random accessor class */ |
| 315 | class RandomAccessor final : public graph::ITensorAccessor |
| 316 | { |
| 317 | public: |
| 318 | /** Constructor |
| 319 | * |
| 320 | * @param[in] lower Lower bound value. |
| 321 | * @param[in] upper Upper bound value. |
| 322 | * @param[in] seed (Optional) Seed used to initialise the random number generator. |
| 323 | */ |
| 324 | RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0); |
| 325 | /** Allows instances to move constructed */ |
| 326 | RandomAccessor(RandomAccessor &&) = default; |
| 327 | |
| 328 | // Inherited methods overriden: |
| 329 | bool access_tensor(ITensor &tensor) override; |
| 330 | |
| 331 | private: |
| 332 | template <typename T, typename D> |
| 333 | void fill(ITensor &tensor, D &&distribution); |
| 334 | PixelValue _lower; |
| 335 | PixelValue _upper; |
| 336 | std::random_device::result_type _seed; |
| 337 | }; |
| 338 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 339 | /** Numpy Binary loader class*/ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 340 | class NumPyBinLoader final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 341 | { |
| 342 | public: |
| 343 | /** Default Constructor |
| 344 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 345 | * @param[in] filename Binary file name |
| 346 | * @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] | 347 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 348 | NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 349 | /** Allows instances to move constructed */ |
| 350 | NumPyBinLoader(NumPyBinLoader &&) = default; |
| 351 | |
| 352 | // Inherited methods overriden: |
| 353 | bool access_tensor(ITensor &tensor) override; |
| 354 | |
| 355 | private: |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 356 | bool _already_loaded; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 357 | const std::string _filename; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 358 | const DataLayout _file_layout; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 359 | }; |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 360 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 361 | /** Generates appropriate random accessor |
| 362 | * |
| 363 | * @param[in] lower Lower random values bound |
| 364 | * @param[in] upper Upper random values bound |
| 365 | * @param[in] seed Random generator seed |
| 366 | * |
| 367 | * @return A ramdom accessor |
| 368 | */ |
| 369 | inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0) |
| 370 | { |
| 371 | return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed); |
| 372 | } |
| 373 | |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 374 | /** Generates appropriate weights accessor according to the specified path |
| 375 | * |
| 376 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 377 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 378 | * @param[in] path Path to the data files |
| 379 | * @param[in] data_file Relative path to the data files from path |
| 380 | * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 381 | * |
| 382 | * @return An appropriate tensor accessor |
| 383 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 384 | inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path, |
| 385 | const std::string &data_file, |
| 386 | DataLayout file_layout = DataLayout::NCHW) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 387 | { |
| 388 | if(path.empty()) |
| 389 | { |
| 390 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 391 | } |
| 392 | else |
| 393 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 394 | return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 398 | /** Generates appropriate input accessor according to the specified graph parameters |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 399 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 400 | * @param[in] graph_parameters Graph parameters |
| 401 | * @param[in] preprocessor (Optional) Preproccessor object |
| 402 | * @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] | 403 | * |
| 404 | * @return An appropriate tensor accessor |
| 405 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 406 | inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 407 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 408 | bool bgr = true) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 409 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 410 | if(!graph_parameters.validation_file.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 411 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 412 | return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file, |
| 413 | graph_parameters.validation_path, |
| 414 | std::move(preprocessor), |
| 415 | bgr, |
| 416 | graph_parameters.validation_range_start, |
| 417 | graph_parameters.validation_range_end); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 418 | } |
| 419 | else |
| 420 | { |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 421 | const std::string &image_file = graph_parameters.image; |
| 422 | const std::string &image_file_lower = lower_string(image_file); |
| 423 | if(arm_compute::utility::endswith(image_file_lower, ".npy")) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 424 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 425 | return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file); |
| 426 | } |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 427 | else if(arm_compute::utility::endswith(image_file_lower, ".jpeg") |
| 428 | || arm_compute::utility::endswith(image_file_lower, ".jpg") |
| 429 | || arm_compute::utility::endswith(image_file_lower, ".ppm")) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 430 | { |
| 431 | return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor)); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 432 | } |
| 433 | else |
| 434 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 435 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 436 | } |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 440 | /** Generates appropriate output accessor according to the specified graph parameters |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 441 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 442 | * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated |
| 443 | * 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] | 444 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 445 | * @param[in] graph_parameters Graph parameters |
| 446 | * @param[in] top_n (Optional) Number of output classes to print (default = 5) |
| 447 | * @param[in] is_validation (Optional) Validation flag (default = false) |
| 448 | * @param[out] output_stream (Optional) Output stream (default = std::cout) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 449 | * |
| 450 | * @return An appropriate tensor accessor |
| 451 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 452 | inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 453 | size_t top_n = 5, |
| 454 | bool is_validation = false, |
| 455 | std::ostream &output_stream = std::cout) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 456 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 457 | if(!graph_parameters.validation_file.empty()) |
| 458 | { |
| 459 | return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, |
| 460 | output_stream, |
| 461 | graph_parameters.validation_range_start, |
| 462 | graph_parameters.validation_range_end); |
| 463 | } |
| 464 | else if(graph_parameters.labels.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 465 | { |
Anthony Barbier | e1a905a | 2017-12-22 13:53:46 +0000 | [diff] [blame] | 466 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 467 | } |
| 468 | else |
| 469 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 470 | return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 473 | /** Generates appropriate npy output accessor according to the specified npy_path |
| 474 | * |
| 475 | * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor |
| 476 | * |
| 477 | * @param[in] npy_path Path to npy file. |
| 478 | * @param[in] shape Shape of the numpy tensor data. |
| 479 | * @param[in] data_type DataType of the numpy tensor data. |
| 480 | * @param[out] output_stream (Optional) Output stream |
| 481 | * |
| 482 | * @return An appropriate tensor accessor |
| 483 | */ |
| 484 | inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout) |
| 485 | { |
| 486 | if(npy_path.empty()) |
| 487 | { |
| 488 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream); |
| 493 | } |
| 494 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 495 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 496 | /** Permutes a given tensor shape given the input and output data layout |
| 497 | * |
| 498 | * @param[in] tensor_shape Tensor shape to permute |
| 499 | * @param[in] in_data_layout Input tensor shape data layout |
| 500 | * @param[in] out_data_layout Output tensor shape data layout |
| 501 | * |
| 502 | * @return Permuted tensor shape |
| 503 | */ |
| 504 | inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout) |
| 505 | { |
| 506 | if(in_data_layout != out_data_layout) |
| 507 | { |
| 508 | arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U); |
| 509 | arm_compute::permute(tensor_shape, perm_vec); |
| 510 | } |
| 511 | return tensor_shape; |
| 512 | } |
| 513 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 514 | /** Utility function to return the TargetHint |
| 515 | * |
| 516 | * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner) |
| 517 | * |
| 518 | * @return the TargetHint |
| 519 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 520 | inline graph::Target set_target_hint(int target) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 521 | { |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 522 | ARM_COMPUTE_ERROR_ON_MSG(target > 3, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner), 3 (GLES)"); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 523 | if((target == 1 || target == 2)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 524 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 525 | return graph::Target::CL; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 526 | } |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 527 | else if(target == 3) |
| 528 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 529 | return graph::Target::GC; |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 530 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 531 | else |
| 532 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 533 | return graph::Target::NEON; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 536 | } // namespace graph_utils |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 537 | } // namespace arm_compute |
| 538 | |
| 539 | #endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */ |