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 | bfa3b52 | 2017-12-12 10:08:38 +0000 | [diff] [blame] | 28 | #include "arm_compute/graph/Graph.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/ITensorAccessor.h" |
| 30 | #include "arm_compute/graph/Types.h" |
| 31 | |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 32 | #include <array> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 33 | #include <random> |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <vector> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 36 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace graph_utils |
| 40 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 41 | /** Preprocessor interface **/ |
| 42 | class IPreprocessor |
| 43 | { |
| 44 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 45 | /** Default destructor. */ |
| 46 | virtual ~IPreprocessor() = default; |
| 47 | /** Preprocess the given tensor. |
| 48 | * |
| 49 | * @param[in] tensor Tensor to preprocess. |
| 50 | */ |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 51 | virtual void preprocess(ITensor &tensor) = 0; |
| 52 | }; |
| 53 | |
| 54 | /** Caffe preproccessor */ |
| 55 | class CaffePreproccessor : public IPreprocessor |
| 56 | { |
| 57 | public: |
| 58 | /** Default Constructor |
| 59 | * |
| 60 | * @param mean Mean array in RGB ordering |
| 61 | * @param bgr Boolean specifying if the preprocessing should assume BGR format |
| 62 | */ |
| 63 | CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true); |
| 64 | void preprocess(ITensor &tensor) override; |
| 65 | |
| 66 | private: |
| 67 | std::array<float, 3> _mean; |
| 68 | bool _bgr; |
| 69 | }; |
| 70 | |
| 71 | /** TF preproccessor */ |
| 72 | class TFPreproccessor : public IPreprocessor |
| 73 | { |
| 74 | public: |
| 75 | void preprocess(ITensor &tensor) override; |
| 76 | }; |
| 77 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 78 | /** PPM writer class */ |
| 79 | class PPMWriter : public graph::ITensorAccessor |
| 80 | { |
| 81 | public: |
| 82 | /** Constructor |
| 83 | * |
| 84 | * @param[in] name PPM file name |
| 85 | * @param[in] maximum Maximum elements to access |
| 86 | */ |
| 87 | PPMWriter(std::string name, unsigned int maximum = 1); |
| 88 | /** Allows instances to move constructed */ |
| 89 | PPMWriter(PPMWriter &&) = default; |
| 90 | |
| 91 | // Inherited methods overriden: |
| 92 | bool access_tensor(ITensor &tensor) override; |
| 93 | |
| 94 | private: |
| 95 | const std::string _name; |
| 96 | unsigned int _iterator; |
| 97 | unsigned int _maximum; |
| 98 | }; |
| 99 | |
| 100 | /** Dummy accessor class */ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 101 | class DummyAccessor final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 102 | { |
| 103 | public: |
| 104 | /** Constructor |
| 105 | * |
| 106 | * @param[in] maximum Maximum elements to write |
| 107 | */ |
| 108 | DummyAccessor(unsigned int maximum = 1); |
| 109 | /** Allows instances to move constructed */ |
| 110 | DummyAccessor(DummyAccessor &&) = default; |
| 111 | |
| 112 | // Inherited methods overriden: |
| 113 | bool access_tensor(ITensor &tensor) override; |
| 114 | |
| 115 | private: |
| 116 | unsigned int _iterator; |
| 117 | unsigned int _maximum; |
| 118 | }; |
| 119 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 120 | /** PPM accessor class */ |
| 121 | class PPMAccessor final : public graph::ITensorAccessor |
| 122 | { |
| 123 | public: |
| 124 | /** Constructor |
| 125 | * |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 126 | * @param[in] ppm_path Path to PPM file |
| 127 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false) |
| 128 | * @param[in] preprocessor (Optional) PPM pre-processing object |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 129 | */ |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 130 | PPMAccessor(std::string ppm_path, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr); |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 131 | /** Allow instances of this class to be move constructed */ |
| 132 | PPMAccessor(PPMAccessor &&) = default; |
| 133 | |
| 134 | // Inherited methods overriden: |
| 135 | bool access_tensor(ITensor &tensor) override; |
| 136 | |
| 137 | private: |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 138 | const std::string _ppm_path; |
| 139 | const bool _bgr; |
| 140 | std::unique_ptr<IPreprocessor> _preprocessor; |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /** Result accessor class */ |
| 144 | class TopNPredictionsAccessor final : public graph::ITensorAccessor |
| 145 | { |
| 146 | public: |
| 147 | /** Constructor |
| 148 | * |
| 149 | * @param[in] labels_path Path to labels text file. |
| 150 | * @param[in] top_n (Optional) Number of output classes to print |
| 151 | * @param[out] output_stream (Optional) Output stream |
| 152 | */ |
| 153 | TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout); |
| 154 | /** Allow instances of this class to be move constructed */ |
| 155 | TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default; |
| 156 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 157 | TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete; |
| 158 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 159 | TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete; |
| 160 | |
| 161 | // Inherited methods overriden: |
| 162 | bool access_tensor(ITensor &tensor) override; |
| 163 | |
| 164 | private: |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame] | 165 | template <typename T> |
| 166 | void access_predictions_tensor(ITensor &tensor); |
| 167 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 168 | std::vector<std::string> _labels; |
| 169 | std::ostream &_output_stream; |
| 170 | size_t _top_n; |
| 171 | }; |
| 172 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 173 | /** Random accessor class */ |
| 174 | class RandomAccessor final : public graph::ITensorAccessor |
| 175 | { |
| 176 | public: |
| 177 | /** Constructor |
| 178 | * |
| 179 | * @param[in] lower Lower bound value. |
| 180 | * @param[in] upper Upper bound value. |
| 181 | * @param[in] seed (Optional) Seed used to initialise the random number generator. |
| 182 | */ |
| 183 | RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0); |
| 184 | /** Allows instances to move constructed */ |
| 185 | RandomAccessor(RandomAccessor &&) = default; |
| 186 | |
| 187 | // Inherited methods overriden: |
| 188 | bool access_tensor(ITensor &tensor) override; |
| 189 | |
| 190 | private: |
| 191 | template <typename T, typename D> |
| 192 | void fill(ITensor &tensor, D &&distribution); |
| 193 | PixelValue _lower; |
| 194 | PixelValue _upper; |
| 195 | std::random_device::result_type _seed; |
| 196 | }; |
| 197 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 198 | /** Numpy Binary loader class*/ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 199 | class NumPyBinLoader final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 200 | { |
| 201 | public: |
| 202 | /** Default Constructor |
| 203 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame^] | 204 | * @param[in] filename Binary file name |
| 205 | * @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] | 206 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame^] | 207 | NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 208 | /** Allows instances to move constructed */ |
| 209 | NumPyBinLoader(NumPyBinLoader &&) = default; |
| 210 | |
| 211 | // Inherited methods overriden: |
| 212 | bool access_tensor(ITensor &tensor) override; |
| 213 | |
| 214 | private: |
| 215 | const std::string _filename; |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame^] | 216 | const DataLayout _file_layout; |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 217 | }; |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 218 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 219 | /** Generates appropriate random accessor |
| 220 | * |
| 221 | * @param[in] lower Lower random values bound |
| 222 | * @param[in] upper Upper random values bound |
| 223 | * @param[in] seed Random generator seed |
| 224 | * |
| 225 | * @return A ramdom accessor |
| 226 | */ |
| 227 | inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0) |
| 228 | { |
| 229 | return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed); |
| 230 | } |
| 231 | |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 232 | /** Generates appropriate weights accessor according to the specified path |
| 233 | * |
| 234 | * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader |
| 235 | * |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame^] | 236 | * @param[in] path Path to the data files |
| 237 | * @param[in] data_file Relative path to the data files from path |
| 238 | * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 239 | * |
| 240 | * @return An appropriate tensor accessor |
| 241 | */ |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame^] | 242 | inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path, |
| 243 | const std::string &data_file, |
| 244 | DataLayout file_layout = DataLayout::NCHW) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 245 | { |
| 246 | if(path.empty()) |
| 247 | { |
| 248 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 249 | } |
| 250 | else |
| 251 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame^] | 252 | 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] | 253 | } |
| 254 | } |
| 255 | |
| 256 | /** Generates appropriate input accessor according to the specified ppm_path |
| 257 | * |
| 258 | * @note If ppm_path is empty will generate a DummyAccessor else will generate a PPMAccessor |
| 259 | * |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 260 | * @param[in] ppm_path Path to PPM file |
| 261 | * @param[in] preprocessor Preproccessor object |
| 262 | * @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] | 263 | * |
| 264 | * @return An appropriate tensor accessor |
| 265 | */ |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 266 | inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const std::string &ppm_path, |
| 267 | std::unique_ptr<IPreprocessor> preprocessor = nullptr, |
| 268 | bool bgr = true) |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 269 | { |
| 270 | if(ppm_path.empty()) |
| 271 | { |
| 272 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(); |
| 273 | } |
| 274 | else |
| 275 | { |
Georgios Pinitas | 140fdc7 | 2018-02-16 11:42:38 +0000 | [diff] [blame] | 276 | return arm_compute::support::cpp14::make_unique<PPMAccessor>(ppm_path, bgr, std::move(preprocessor)); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | /** Generates appropriate output accessor according to the specified labels_path |
| 281 | * |
| 282 | * @note If labels_path is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor |
| 283 | * |
| 284 | * @param[in] labels_path Path to labels text file |
| 285 | * @param[in] top_n (Optional) Number of output classes to print |
| 286 | * @param[out] output_stream (Optional) Output stream |
| 287 | * |
| 288 | * @return An appropriate tensor accessor |
| 289 | */ |
| 290 | inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout) |
| 291 | { |
| 292 | if(labels_path.empty()) |
| 293 | { |
Anthony Barbier | e1a905a | 2017-12-22 13:53:46 +0000 | [diff] [blame] | 294 | return arm_compute::support::cpp14::make_unique<DummyAccessor>(0); |
Isabella Gottardi | a4c6188 | 2017-11-03 12:11:55 +0000 | [diff] [blame] | 295 | } |
| 296 | else |
| 297 | { |
| 298 | return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream); |
| 299 | } |
| 300 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 301 | |
| 302 | /** Utility function to return the TargetHint |
| 303 | * |
| 304 | * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner) |
| 305 | * |
| 306 | * @return the TargetHint |
| 307 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 308 | inline graph::Target set_target_hint(int target) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 309 | { |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 310 | 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] | 311 | if((target == 1 || target == 2)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 312 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 313 | return graph::Target::CL; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 314 | } |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 315 | else if(target == 3) |
| 316 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 317 | return graph::Target::GC; |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 318 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 319 | else |
| 320 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 321 | return graph::Target::NEON; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 324 | } // namespace graph_utils |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 325 | } // namespace arm_compute |
| 326 | |
| 327 | #endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */ |