Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/ITensorAccessor.h" |
| 29 | #include "arm_compute/graph/Types.h" |
| 30 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 31 | #include <random> |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <vector> |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 34 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace graph_utils |
| 38 | { |
| 39 | /** PPM writer class */ |
| 40 | class PPMWriter : public graph::ITensorAccessor |
| 41 | { |
| 42 | public: |
| 43 | /** Constructor |
| 44 | * |
| 45 | * @param[in] name PPM file name |
| 46 | * @param[in] maximum Maximum elements to access |
| 47 | */ |
| 48 | PPMWriter(std::string name, unsigned int maximum = 1); |
| 49 | /** Allows instances to move constructed */ |
| 50 | PPMWriter(PPMWriter &&) = default; |
| 51 | |
| 52 | // Inherited methods overriden: |
| 53 | bool access_tensor(ITensor &tensor) override; |
| 54 | |
| 55 | private: |
| 56 | const std::string _name; |
| 57 | unsigned int _iterator; |
| 58 | unsigned int _maximum; |
| 59 | }; |
| 60 | |
| 61 | /** Dummy accessor class */ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 62 | class DummyAccessor final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 63 | { |
| 64 | public: |
| 65 | /** Constructor |
| 66 | * |
| 67 | * @param[in] maximum Maximum elements to write |
| 68 | */ |
| 69 | DummyAccessor(unsigned int maximum = 1); |
| 70 | /** Allows instances to move constructed */ |
| 71 | DummyAccessor(DummyAccessor &&) = default; |
| 72 | |
| 73 | // Inherited methods overriden: |
| 74 | bool access_tensor(ITensor &tensor) override; |
| 75 | |
| 76 | private: |
| 77 | unsigned int _iterator; |
| 78 | unsigned int _maximum; |
| 79 | }; |
| 80 | |
Gian Marco | 44ec2e7 | 2017-10-19 14:13:38 +0100 | [diff] [blame] | 81 | /** PPM accessor class */ |
| 82 | class PPMAccessor final : public graph::ITensorAccessor |
| 83 | { |
| 84 | public: |
| 85 | /** Constructor |
| 86 | * |
| 87 | * @param[in] ppm_path Path to PPM file |
| 88 | * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false) |
| 89 | * @param[in] mean_r (Optional) Red mean value to be subtracted from red channel |
| 90 | * @param[in] mean_g (Optional) Green mean value to be subtracted from green channel |
| 91 | * @param[in] mean_b (Optional) Blue mean value to be subtracted from blue channel |
| 92 | */ |
| 93 | PPMAccessor(const std::string &ppm_path, bool bgr = true, float mean_r = 0.0f, float mean_g = 0.0f, float mean_b = 0.0f); |
| 94 | /** Allow instances of this class to be move constructed */ |
| 95 | PPMAccessor(PPMAccessor &&) = default; |
| 96 | |
| 97 | // Inherited methods overriden: |
| 98 | bool access_tensor(ITensor &tensor) override; |
| 99 | |
| 100 | private: |
| 101 | const std::string &_ppm_path; |
| 102 | const bool _bgr; |
| 103 | const float _mean_r; |
| 104 | const float _mean_g; |
| 105 | const float _mean_b; |
| 106 | }; |
| 107 | |
| 108 | /** Result accessor class */ |
| 109 | class TopNPredictionsAccessor final : public graph::ITensorAccessor |
| 110 | { |
| 111 | public: |
| 112 | /** Constructor |
| 113 | * |
| 114 | * @param[in] labels_path Path to labels text file. |
| 115 | * @param[in] top_n (Optional) Number of output classes to print |
| 116 | * @param[out] output_stream (Optional) Output stream |
| 117 | */ |
| 118 | TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout); |
| 119 | /** Allow instances of this class to be move constructed */ |
| 120 | TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default; |
| 121 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 122 | TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete; |
| 123 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 124 | TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete; |
| 125 | |
| 126 | // Inherited methods overriden: |
| 127 | bool access_tensor(ITensor &tensor) override; |
| 128 | |
| 129 | private: |
| 130 | std::vector<std::string> _labels; |
| 131 | std::ostream &_output_stream; |
| 132 | size_t _top_n; |
| 133 | }; |
| 134 | |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 135 | /** Random accessor class */ |
| 136 | class RandomAccessor final : public graph::ITensorAccessor |
| 137 | { |
| 138 | public: |
| 139 | /** Constructor |
| 140 | * |
| 141 | * @param[in] lower Lower bound value. |
| 142 | * @param[in] upper Upper bound value. |
| 143 | * @param[in] seed (Optional) Seed used to initialise the random number generator. |
| 144 | */ |
| 145 | RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0); |
| 146 | /** Allows instances to move constructed */ |
| 147 | RandomAccessor(RandomAccessor &&) = default; |
| 148 | |
| 149 | // Inherited methods overriden: |
| 150 | bool access_tensor(ITensor &tensor) override; |
| 151 | |
| 152 | private: |
| 153 | template <typename T, typename D> |
| 154 | void fill(ITensor &tensor, D &&distribution); |
| 155 | PixelValue _lower; |
| 156 | PixelValue _upper; |
| 157 | std::random_device::result_type _seed; |
| 158 | }; |
| 159 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 160 | /** Numpy Binary loader class*/ |
Michalis Spyrou | 53b405f | 2017-09-27 15:55:31 +0100 | [diff] [blame] | 161 | class NumPyBinLoader final : public graph::ITensorAccessor |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 162 | { |
| 163 | public: |
| 164 | /** Default Constructor |
| 165 | * |
| 166 | * @param filename Binary file name |
| 167 | */ |
| 168 | NumPyBinLoader(std::string filename); |
| 169 | /** Allows instances to move constructed */ |
| 170 | NumPyBinLoader(NumPyBinLoader &&) = default; |
| 171 | |
| 172 | // Inherited methods overriden: |
| 173 | bool access_tensor(ITensor &tensor) override; |
| 174 | |
| 175 | private: |
| 176 | const std::string _filename; |
| 177 | }; |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 178 | } // namespace graph_utils |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 179 | } // namespace arm_compute |
| 180 | |
| 181 | #endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */ |