Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 24 | #ifndef __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ |
| 25 | #define __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 26 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/PixelValue.h" |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Utils.h" |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/Utility.h" |
Gian Marco | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 30 | #include "arm_compute/graph/Graph.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 31 | #include "arm_compute/graph/ITensorAccessor.h" |
| 32 | #include "arm_compute/graph/Types.h" |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/Tensor.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 34 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 35 | #include "utils/CommonGraphOptions.h" |
| 36 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 37 | #include <array> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 38 | #include <random> |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <vector> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 41 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace graph_utils |
| 45 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 46 | /** Preprocessor interface **/ |
| 47 | class IPreprocessor |
| 48 | { |
| 49 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 50 | /** Default destructor. */ |
| 51 | virtual ~IPreprocessor() = default; |
| 52 | /** Preprocess the given tensor. |
| 53 | * |
| 54 | * @param[in] tensor Tensor to preprocess. |
| 55 | */ |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 56 | virtual void preprocess(ITensor &tensor) = 0; |
| 57 | }; |
| 58 | |
| 59 | /** Caffe preproccessor */ |
| 60 | class CaffePreproccessor : public IPreprocessor |
| 61 | { |
| 62 | public: |
| 63 | /** Default Constructor |
| 64 | * |
Georgios Pinitas | b54c644 | 2019-04-03 13:18:14 +0100 | [diff] [blame] | 65 | * @param[in] mean Mean array in RGB ordering |
| 66 | * @param[in] bgr Boolean specifying if the preprocessing should assume BGR format |
| 67 | * @param[in] scale Scale value |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 68 | */ |
Georgios Pinitas | b54c644 | 2019-04-03 13:18:14 +0100 | [diff] [blame] | 69 | CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true, float scale = 1.f); |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 70 | void preprocess(ITensor &tensor) override; |
| 71 | |
| 72 | private: |
| 73 | std::array<float, 3> _mean; |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 74 | bool _bgr; |
Georgios Pinitas | b54c644 | 2019-04-03 13:18:14 +0100 | [diff] [blame] | 75 | float _scale; |
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. |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 148 | * @param[in] data_layout (Optional) DataLayout of the numpy tensor data. |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 149 | * @param[out] output_stream (Optional) Output stream |
| 150 | */ |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 151 | NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW, std::ostream &output_stream = std::cout); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 152 | /** Allow instances of this class to be move constructed */ |
| 153 | NumPyAccessor(NumPyAccessor &&) = default; |
| 154 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 155 | NumPyAccessor(const NumPyAccessor &) = delete; |
| 156 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 157 | NumPyAccessor &operator=(const NumPyAccessor &) = delete; |
| 158 | |
| 159 | // Inherited methods overriden: |
| 160 | bool access_tensor(ITensor &tensor) override; |
| 161 | |
| 162 | private: |
| 163 | template <typename T> |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 164 | void access_numpy_tensor(ITensor &tensor, T tolerance); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 165 | |
| 166 | Tensor _npy_tensor; |
| 167 | const std::string _filename; |
| 168 | std::ostream &_output_stream; |
| 169 | }; |
| 170 | |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 171 | /** SaveNumPy accessor class */ |
| 172 | class SaveNumPyAccessor final : public graph::ITensorAccessor |
| 173 | { |
| 174 | public: |
| 175 | /** Constructor |
| 176 | * |
| 177 | * @param[in] npy_name Npy file name. |
| 178 | * @param[in] is_fortran (Optional) If true, save tensor in fortran order. |
| 179 | */ |
| 180 | SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false); |
| 181 | /** Allow instances of this class to be move constructed */ |
| 182 | SaveNumPyAccessor(SaveNumPyAccessor &&) = default; |
| 183 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 184 | SaveNumPyAccessor(const SaveNumPyAccessor &) = delete; |
| 185 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 186 | SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete; |
| 187 | |
| 188 | // Inherited methods overriden: |
| 189 | bool access_tensor(ITensor &tensor) override; |
| 190 | |
| 191 | private: |
| 192 | const std::string _npy_name; |
| 193 | const bool _is_fortran; |
| 194 | }; |
| 195 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 196 | /** Image accessor class */ |
| 197 | class ImageAccessor final : public graph::ITensorAccessor |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 198 | { |
| 199 | public: |
| 200 | /** Constructor |
| 201 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 202 | * @param[in] filename Image file |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 203 | * @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] | 204 | * @param[in] preprocessor (Optional) Image pre-processing object |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 205 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 206 | 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] | 207 | /** Allow instances of this class to be move constructed */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 208 | ImageAccessor(ImageAccessor &&) = default; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 209 | |
| 210 | // Inherited methods overriden: |
| 211 | bool access_tensor(ITensor &tensor) override; |
| 212 | |
| 213 | private: |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 214 | bool _already_loaded; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 215 | const std::string _filename; |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 216 | const bool _bgr; |
| 217 | std::unique_ptr<IPreprocessor> _preprocessor; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 218 | }; |
| 219 | |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 220 | /** Input Accessor used for network validation */ |
| 221 | class ValidationInputAccessor final : public graph::ITensorAccessor |
| 222 | { |
| 223 | public: |
| 224 | /** Constructor |
| 225 | * |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 226 | * @param[in] image_list File containing all the images to validate |
| 227 | * @param[in] images_path Path to images. |
| 228 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format) |
| 229 | * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr) |
| 230 | * @param[in] start (Optional) Start range |
| 231 | * @param[in] end (Optional) End range |
| 232 | * @param[out] output_stream (Optional) Output stream |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 233 | * |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 234 | * @note Range is defined as [start, end] |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 235 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 236 | ValidationInputAccessor(const std::string &image_list, |
| 237 | std::string images_path, |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 238 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 239 | bool bgr = true, |
| 240 | unsigned int start = 0, |
| 241 | unsigned int end = 0, |
| 242 | std::ostream &output_stream = std::cout); |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 243 | |
| 244 | // Inherited methods overriden: |
| 245 | bool access_tensor(ITensor &tensor) override; |
| 246 | |
| 247 | private: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 248 | std::string _path; |
| 249 | std::vector<std::string> _images; |
| 250 | std::unique_ptr<IPreprocessor> _preprocessor; |
| 251 | bool _bgr; |
| 252 | size_t _offset; |
Anthony Barbier | 40606df | 2018-07-23 14:41:59 +0100 | [diff] [blame] | 253 | std::ostream &_output_stream; |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 254 | }; |
| 255 | |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 256 | /** Output Accessor used for network validation */ |
| 257 | class ValidationOutputAccessor final : public graph::ITensorAccessor |
| 258 | { |
| 259 | public: |
| 260 | /** Default Constructor |
| 261 | * |
| 262 | * @param[in] image_list File containing all the images and labels results |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 263 | * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream) |
| 264 | * @param[in] start (Optional) Start range |
| 265 | * @param[in] end (Optional) End range |
| 266 | * |
| 267 | * @note Range is defined as [start, end] |
| 268 | */ |
| 269 | ValidationOutputAccessor(const std::string &image_list, |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 270 | std::ostream &output_stream = std::cout, |
| 271 | unsigned int start = 0, |
| 272 | unsigned int end = 0); |
| 273 | /** Reset accessor state */ |
| 274 | void reset(); |
| 275 | |
| 276 | // Inherited methods overriden: |
| 277 | bool access_tensor(ITensor &tensor) override; |
| 278 | |
| 279 | private: |
| 280 | /** Access predictions of the tensor |
| 281 | * |
| 282 | * @tparam T Tensor elements type |
| 283 | * |
| 284 | * @param[in] tensor Tensor to read the predictions from |
| 285 | */ |
| 286 | template <typename T> |
| 287 | std::vector<size_t> access_predictions_tensor(ITensor &tensor); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 288 | /** Aggregates the results of a sample |
| 289 | * |
| 290 | * @param[in] res Vector containing the results of a graph |
| 291 | * @param[in,out] positive_samples Positive samples to be updated |
| 292 | * @param[in] top_n Top n accuracy to measure |
| 293 | * @param[in] correct_label Correct label of the current sample |
| 294 | */ |
| 295 | void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label); |
| 296 | /** Reports top N accuracy |
| 297 | * |
| 298 | * @param[in] top_n Top N accuracy that is being reported |
| 299 | * @param[in] total_samples Total number of samples |
| 300 | * @param[in] positive_samples Positive samples |
| 301 | */ |
| 302 | 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] | 303 | |
| 304 | private: |
| 305 | std::vector<int> _results; |
| 306 | std::ostream &_output_stream; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 307 | size_t _offset; |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 308 | size_t _positive_samples_top1; |
| 309 | size_t _positive_samples_top5; |
Georgios Pinitas | 7908de7 | 2018-06-27 12:34:20 +0100 | [diff] [blame] | 310 | }; |
| 311 | |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 312 | /** Detection output accessor class */ |
| 313 | class DetectionOutputAccessor final : public graph::ITensorAccessor |
| 314 | { |
| 315 | public: |
| 316 | /** Constructor |
| 317 | * |
| 318 | * @param[in] labels_path Path to labels text file. |
| 319 | * @param[in] imgs_tensor_shapes Network input images tensor shapes. |
| 320 | * @param[out] output_stream (Optional) Output stream |
| 321 | */ |
| 322 | DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout); |
| 323 | /** Allow instances of this class to be move constructed */ |
| 324 | DetectionOutputAccessor(DetectionOutputAccessor &&) = default; |
| 325 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 326 | DetectionOutputAccessor(const DetectionOutputAccessor &) = delete; |
| 327 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 328 | DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete; |
| 329 | |
| 330 | // Inherited methods overriden: |
| 331 | bool access_tensor(ITensor &tensor) override; |
| 332 | |
| 333 | private: |
| 334 | template <typename T> |
| 335 | void access_predictions_tensor(ITensor &tensor); |
| 336 | |
| 337 | std::vector<std::string> _labels; |
| 338 | std::vector<TensorShape> _tensor_shapes; |
| 339 | std::ostream &_output_stream; |
| 340 | }; |
| 341 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 342 | /** Result accessor class */ |
| 343 | class TopNPredictionsAccessor final : public graph::ITensorAccessor |
| 344 | { |
| 345 | public: |
| 346 | /** Constructor |
| 347 | * |
| 348 | * @param[in] labels_path Path to labels text file. |
| 349 | * @param[in] top_n (Optional) Number of output classes to print |
| 350 | * @param[out] output_stream (Optional) Output stream |
| 351 | */ |
| 352 | TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout); |
| 353 | /** Allow instances of this class to be move constructed */ |
| 354 | TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default; |
| 355 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 356 | TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete; |
| 357 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 358 | TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete; |
| 359 | |
| 360 | // Inherited methods overriden: |
| 361 | bool access_tensor(ITensor &tensor) override; |
| 362 | |
| 363 | private: |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 364 | template <typename T> |
| 365 | void access_predictions_tensor(ITensor &tensor); |
| 366 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 367 | std::vector<std::string> _labels; |
| 368 | std::ostream &_output_stream; |
| 369 | size_t _top_n; |
| 370 | }; |
| 371 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 372 | /** Random accessor class */ |
| 373 | class RandomAccessor final : public graph::ITensorAccessor |
| 374 | { |
| 375 | public: |
| 376 | /** Constructor |
| 377 | * |
| 378 | * @param[in] lower Lower bound value. |
| 379 | * @param[in] upper Upper bound value. |
| 380 | * @param[in] seed (Optional) Seed used to initialise the random number generator. |
| 381 | */ |
| 382 | RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0); |
| 383 | /** Allows instances to move constructed */ |
| 384 | RandomAccessor(RandomAccessor &&) = default; |
| 385 | |
| 386 | // Inherited methods overriden: |
| 387 | bool access_tensor(ITensor &tensor) override; |
| 388 | |
| 389 | private: |
| 390 | template <typename T, typename D> |
| 391 | void fill(ITensor &tensor, D &&distribution); |
| 392 | PixelValue _lower; |
| 393 | PixelValue _upper; |
| 394 | std::random_device::result_type _seed; |
| 395 | }; |
| 396 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 397 | /** Numpy Binary loader class*/ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 398 | class NumPyBinLoader final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 399 | { |
| 400 | public: |
| 401 | /** Default Constructor |
| 402 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 403 | * @param[in] filename Binary file name |
| 404 | * @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] | 405 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 406 | NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 407 | /** Allows instances to move constructed */ |
| 408 | NumPyBinLoader(NumPyBinLoader &&) = default; |
| 409 | |
| 410 | // Inherited methods overriden: |
| 411 | bool access_tensor(ITensor &tensor) override; |
| 412 | |
| 413 | private: |
Anthony Barbier | 8a04211 | 2018-08-21 18:16:53 +0100 | [diff] [blame] | 414 | bool _already_loaded; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 415 | const std::string _filename; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 416 | const DataLayout _file_layout; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 417 | }; |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 418 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 419 | /** Generates appropriate random accessor |
| 420 | * |
| 421 | * @param[in] lower Lower random values bound |
| 422 | * @param[in] upper Upper random values bound |
| 423 | * @param[in] seed Random generator seed |
| 424 | * |
| 425 | * @return A ramdom accessor |
| 426 | */ |
| 427 | inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0) |
| 428 | { |
| 429 | return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed); |
| 430 | } |
| 431 | |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 432 | /** Generates appropriate weights accessor according to the specified path |
| 433 | * |
| 434 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 435 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 436 | * @param[in] path Path to the data files |
| 437 | * @param[in] data_file Relative path to the data files from path |
| 438 | * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 439 | * |
| 440 | * @return An appropriate tensor accessor |
| 441 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 442 | inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path, |
| 443 | const std::string &data_file, |
| 444 | DataLayout file_layout = DataLayout::NCHW) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 445 | { |
| 446 | if(path.empty()) |
| 447 | { |
| 448 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 449 | } |
| 450 | else |
| 451 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 452 | 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] | 453 | } |
| 454 | } |
| 455 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 456 | /** Generates appropriate input accessor according to the specified graph parameters |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 457 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 458 | * @param[in] graph_parameters Graph parameters |
| 459 | * @param[in] preprocessor (Optional) Preproccessor object |
| 460 | * @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] | 461 | * |
| 462 | * @return An appropriate tensor accessor |
| 463 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 464 | inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 465 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 466 | bool bgr = true) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 467 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 468 | if(!graph_parameters.validation_file.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 469 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 470 | return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file, |
| 471 | graph_parameters.validation_path, |
| 472 | std::move(preprocessor), |
| 473 | bgr, |
| 474 | graph_parameters.validation_range_start, |
| 475 | graph_parameters.validation_range_end); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 476 | } |
| 477 | else |
| 478 | { |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 479 | const std::string &image_file = graph_parameters.image; |
| 480 | const std::string &image_file_lower = lower_string(image_file); |
| 481 | if(arm_compute::utility::endswith(image_file_lower, ".npy")) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 482 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 483 | return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file); |
| 484 | } |
Gian Marco Iodice | ad486e2 | 2018-08-07 17:17:06 +0100 | [diff] [blame] | 485 | else if(arm_compute::utility::endswith(image_file_lower, ".jpeg") |
| 486 | || arm_compute::utility::endswith(image_file_lower, ".jpg") |
| 487 | || arm_compute::utility::endswith(image_file_lower, ".ppm")) |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 488 | { |
| 489 | 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] | 490 | } |
| 491 | else |
| 492 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 493 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 494 | } |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 498 | /** Generates appropriate output accessor according to the specified graph parameters |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 499 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 500 | * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated |
| 501 | * 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] | 502 | * |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 503 | * @param[in] graph_parameters Graph parameters |
| 504 | * @param[in] top_n (Optional) Number of output classes to print (default = 5) |
| 505 | * @param[in] is_validation (Optional) Validation flag (default = false) |
| 506 | * @param[out] output_stream (Optional) Output stream (default = std::cout) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 507 | * |
| 508 | * @return An appropriate tensor accessor |
| 509 | */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 510 | inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 511 | size_t top_n = 5, |
| 512 | bool is_validation = false, |
| 513 | std::ostream &output_stream = std::cout) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 514 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 515 | if(!graph_parameters.validation_file.empty()) |
| 516 | { |
| 517 | return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, |
| 518 | output_stream, |
| 519 | graph_parameters.validation_range_start, |
| 520 | graph_parameters.validation_range_end); |
| 521 | } |
| 522 | else if(graph_parameters.labels.empty()) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 523 | { |
Anthony Barbier | e1a905a | 2017-12-22 13:53:46 +0000 | [diff] [blame] | 524 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 525 | } |
| 526 | else |
| 527 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 528 | 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] | 529 | } |
| 530 | } |
Isabella Gottardi | 7234ed8 | 2018-11-27 08:51:10 +0000 | [diff] [blame] | 531 | /** Generates appropriate output accessor according to the specified graph parameters |
| 532 | * |
| 533 | * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated |
| 534 | * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor |
| 535 | * |
| 536 | * @param[in] graph_parameters Graph parameters |
| 537 | * @param[in] tensor_shapes Network input images tensor shapes. |
| 538 | * @param[in] is_validation (Optional) Validation flag (default = false) |
| 539 | * @param[out] output_stream (Optional) Output stream (default = std::cout) |
| 540 | * |
| 541 | * @return An appropriate tensor accessor |
| 542 | */ |
| 543 | inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters, |
| 544 | std::vector<TensorShape> tensor_shapes, |
| 545 | bool is_validation = false, |
| 546 | std::ostream &output_stream = std::cout) |
| 547 | { |
| 548 | if(!graph_parameters.validation_file.empty()) |
| 549 | { |
| 550 | return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, |
| 551 | output_stream, |
| 552 | graph_parameters.validation_range_start, |
| 553 | graph_parameters.validation_range_end); |
| 554 | } |
| 555 | else if(graph_parameters.labels.empty()) |
| 556 | { |
| 557 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | return arm_compute::support::cpp14::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream); |
| 562 | } |
| 563 | } |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 564 | /** Generates appropriate npy output accessor according to the specified npy_path |
| 565 | * |
| 566 | * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor |
| 567 | * |
| 568 | * @param[in] npy_path Path to npy file. |
| 569 | * @param[in] shape Shape of the numpy tensor data. |
| 570 | * @param[in] data_type DataType of the numpy tensor data. |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 571 | * @param[in] data_layout DataLayout of the numpy tensor data. |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 572 | * @param[out] output_stream (Optional) Output stream |
| 573 | * |
| 574 | * @return An appropriate tensor accessor |
| 575 | */ |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 576 | inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW, |
| 577 | std::ostream &output_stream = std::cout) |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 578 | { |
| 579 | if(npy_path.empty()) |
| 580 | { |
| 581 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
| 582 | } |
| 583 | else |
| 584 | { |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 585 | return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream); |
Isabella Gottardi | 88d5b22 | 2018-04-06 12:24:55 +0100 | [diff] [blame] | 586 | } |
| 587 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 588 | |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 589 | /** Generates appropriate npy output accessor according to the specified npy_path |
| 590 | * |
| 591 | * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor |
| 592 | * |
| 593 | * @param[in] npy_name Npy filename. |
| 594 | * @param[in] is_fortran (Optional) If true, save tensor in fortran order. |
| 595 | * |
| 596 | * @return An appropriate tensor accessor |
| 597 | */ |
| 598 | inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, const bool is_fortran = false) |
| 599 | { |
| 600 | if(npy_name.empty()) |
| 601 | { |
| 602 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | return arm_compute::support::cpp14::make_unique<SaveNumPyAccessor>(npy_name, is_fortran); |
| 607 | } |
| 608 | } |
| 609 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 610 | /** Permutes a given tensor shape given the input and output data layout |
| 611 | * |
| 612 | * @param[in] tensor_shape Tensor shape to permute |
| 613 | * @param[in] in_data_layout Input tensor shape data layout |
| 614 | * @param[in] out_data_layout Output tensor shape data layout |
| 615 | * |
| 616 | * @return Permuted tensor shape |
| 617 | */ |
| 618 | inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout) |
| 619 | { |
| 620 | if(in_data_layout != out_data_layout) |
| 621 | { |
| 622 | arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U); |
| 623 | arm_compute::permute(tensor_shape, perm_vec); |
| 624 | } |
| 625 | return tensor_shape; |
| 626 | } |
| 627 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 628 | /** Utility function to return the TargetHint |
| 629 | * |
| 630 | * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner) |
| 631 | * |
| 632 | * @return the TargetHint |
| 633 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 634 | inline graph::Target set_target_hint(int target) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 635 | { |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 636 | 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] | 637 | if((target == 1 || target == 2)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 638 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 639 | return graph::Target::CL; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 640 | } |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 641 | else if(target == 3) |
| 642 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 643 | return graph::Target::GC; |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 644 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 645 | else |
| 646 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 647 | return graph::Target::NEON; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 650 | } // namespace graph_utils |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 651 | } // namespace arm_compute |
| 652 | |
Georgios Pinitas | f52cd78 | 2019-03-25 14:06:14 +0000 | [diff] [blame] | 653 | #endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */ |