blob: da52c265204ef8360b39f720e63684b88179270f [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Giorgio Arenaa66eaa22017-12-21 19:50:06 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +01003 *
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 Spyrou53b405f2017-09-27 15:55:31 +010027#include "arm_compute/core/PixelValue.h"
Gian Marcobfa3b522017-12-12 10:08:38 +000028#include "arm_compute/graph/Graph.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010029#include "arm_compute/graph/ITensorAccessor.h"
30#include "arm_compute/graph/Types.h"
31
Michalis Spyrou53b405f2017-09-27 15:55:31 +010032#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010033#include <string>
34#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010035
Anthony Barbier2a07e182017-08-04 18:20:27 +010036namespace arm_compute
37{
38namespace graph_utils
39{
40/** PPM writer class */
41class PPMWriter : public graph::ITensorAccessor
42{
43public:
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
56private:
57 const std::string _name;
58 unsigned int _iterator;
59 unsigned int _maximum;
60};
61
62/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +010063class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +010064{
65public:
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
77private:
78 unsigned int _iterator;
79 unsigned int _maximum;
80};
81
Gian Marco44ec2e72017-10-19 14:13:38 +010082/** PPM accessor class */
83class PPMAccessor final : public graph::ITensorAccessor
84{
85public:
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 Pinitas652bde52018-01-10 15:33:28 +000093 * @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 Marco44ec2e72017-10-19 14:13:38 +010096 */
Georgios Pinitas652bde52018-01-10 15:33:28 +000097 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 Marco44ec2e72017-10-19 14:13:38 +0100100 /** 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
106private:
Georgios Pinitas652bde52018-01-10 15:33:28 +0000107 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 Marco44ec2e72017-10-19 14:13:38 +0100115};
116
117/** Result accessor class */
118class TopNPredictionsAccessor final : public graph::ITensorAccessor
119{
120public:
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
138private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000139 template <typename T>
140 void access_predictions_tensor(ITensor &tensor);
141
Gian Marco44ec2e72017-10-19 14:13:38 +0100142 std::vector<std::string> _labels;
143 std::ostream &_output_stream;
144 size_t _top_n;
145};
146
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100147/** Random accessor class */
148class RandomAccessor final : public graph::ITensorAccessor
149{
150public:
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
164private:
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 Barbier2a07e182017-08-04 18:20:27 +0100172/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100173class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100174{
175public:
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
187private:
188 const std::string _filename;
189};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000190
Georgios Pinitas652bde52018-01-10 15:33:28 +0000191/** 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 */
199inline 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 Gottardia4c61882017-11-03 12:11:55 +0000204/** 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 */
213inline 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 Pinitas652bde52018-01-10 15:33:28 +0000233 * @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 Gottardia4c61882017-11-03 12:11:55 +0000237 *
238 * @return An appropriate tensor accessor
239 */
Georgios Pinitas652bde52018-01-10 15:33:28 +0000240inline 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 Gottardia4c61882017-11-03 12:11:55 +0000244{
245 if(ppm_path.empty())
246 {
247 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
248 }
249 else
250 {
Georgios Pinitas652bde52018-01-10 15:33:28 +0000251 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 Gottardia4c61882017-11-03 12:11:55 +0000254 }
255}
256
Gian Marcobfa3b522017-12-12 10:08:38 +0000257/** 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 */
263inline 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 Gottardia4c61882017-11-03 12:11:55 +0000277/** 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 */
287inline 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 Barbiere1a905a2017-12-22 13:53:46 +0000291 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000292 }
293 else
294 {
295 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream);
296 }
297}
Michalis Spyroue4720822017-10-02 17:44:52 +0100298} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100299} // namespace arm_compute
300
301#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */