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