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