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