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