blob: 11f1e0590abebba9df92eab2311571aab68631e5 [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
Georgios Pinitas140fdc72018-02-16 11:42:38 +000032#include <array>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010033#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010034#include <string>
35#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010036
Anthony Barbier2a07e182017-08-04 18:20:27 +010037namespace arm_compute
38{
39namespace graph_utils
40{
Georgios Pinitas140fdc72018-02-16 11:42:38 +000041/** Preprocessor interface **/
42class IPreprocessor
43{
44public:
Alex Gildayc357c472018-03-21 13:54:09 +000045 /** Default destructor. */
46 virtual ~IPreprocessor() = default;
47 /** Preprocess the given tensor.
48 *
49 * @param[in] tensor Tensor to preprocess.
50 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +000051 virtual void preprocess(ITensor &tensor) = 0;
52};
53
54/** Caffe preproccessor */
55class CaffePreproccessor : public IPreprocessor
56{
57public:
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
66private:
67 std::array<float, 3> _mean;
68 bool _bgr;
69};
70
71/** TF preproccessor */
72class TFPreproccessor : public IPreprocessor
73{
74public:
75 void preprocess(ITensor &tensor) override;
76};
77
Anthony Barbier2a07e182017-08-04 18:20:27 +010078/** PPM writer class */
79class PPMWriter : public graph::ITensorAccessor
80{
81public:
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
94private:
95 const std::string _name;
96 unsigned int _iterator;
97 unsigned int _maximum;
98};
99
100/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100101class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100102{
103public:
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
115private:
116 unsigned int _iterator;
117 unsigned int _maximum;
118};
119
Gian Marco44ec2e72017-10-19 14:13:38 +0100120/** PPM accessor class */
121class PPMAccessor final : public graph::ITensorAccessor
122{
123public:
124 /** Constructor
125 *
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000126 * @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 Marco44ec2e72017-10-19 14:13:38 +0100129 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000130 PPMAccessor(std::string ppm_path, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100131 /** 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
137private:
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000138 const std::string _ppm_path;
139 const bool _bgr;
140 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100141};
142
143/** Result accessor class */
144class TopNPredictionsAccessor final : public graph::ITensorAccessor
145{
146public:
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
164private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000165 template <typename T>
166 void access_predictions_tensor(ITensor &tensor);
167
Gian Marco44ec2e72017-10-19 14:13:38 +0100168 std::vector<std::string> _labels;
169 std::ostream &_output_stream;
170 size_t _top_n;
171};
172
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100173/** Random accessor class */
174class RandomAccessor final : public graph::ITensorAccessor
175{
176public:
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
190private:
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 Barbier2a07e182017-08-04 18:20:27 +0100198/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100199class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100200{
201public:
202 /** Default Constructor
203 *
204 * @param filename Binary file name
205 */
206 NumPyBinLoader(std::string filename);
207 /** Allows instances to move constructed */
208 NumPyBinLoader(NumPyBinLoader &&) = default;
209
210 // Inherited methods overriden:
211 bool access_tensor(ITensor &tensor) override;
212
213private:
214 const std::string _filename;
215};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000216
Georgios Pinitas652bde52018-01-10 15:33:28 +0000217/** Generates appropriate random accessor
218 *
219 * @param[in] lower Lower random values bound
220 * @param[in] upper Upper random values bound
221 * @param[in] seed Random generator seed
222 *
223 * @return A ramdom accessor
224 */
225inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
226{
227 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
228}
229
Isabella Gottardia4c61882017-11-03 12:11:55 +0000230/** Generates appropriate weights accessor according to the specified path
231 *
232 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
233 *
234 * @param[in] path Path to the data files
235 * @param[in] data_file Relative path to the data files from path
236 *
237 * @return An appropriate tensor accessor
238 */
239inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path, const std::string &data_file)
240{
241 if(path.empty())
242 {
243 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
244 }
245 else
246 {
247 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file);
248 }
249}
250
251/** Generates appropriate input accessor according to the specified ppm_path
252 *
253 * @note If ppm_path is empty will generate a DummyAccessor else will generate a PPMAccessor
254 *
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000255 * @param[in] ppm_path Path to PPM file
256 * @param[in] preprocessor Preproccessor object
257 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000258 *
259 * @return An appropriate tensor accessor
260 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000261inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const std::string &ppm_path,
262 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
263 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000264{
265 if(ppm_path.empty())
266 {
267 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
268 }
269 else
270 {
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000271 return arm_compute::support::cpp14::make_unique<PPMAccessor>(ppm_path, bgr, std::move(preprocessor));
Isabella Gottardia4c61882017-11-03 12:11:55 +0000272 }
273}
274
275/** Generates appropriate output accessor according to the specified labels_path
276 *
277 * @note If labels_path is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
278 *
279 * @param[in] labels_path Path to labels text file
280 * @param[in] top_n (Optional) Number of output classes to print
281 * @param[out] output_stream (Optional) Output stream
282 *
283 * @return An appropriate tensor accessor
284 */
285inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout)
286{
287 if(labels_path.empty())
288 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000289 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000290 }
291 else
292 {
293 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream);
294 }
295}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000296
297/** Utility function to return the TargetHint
298 *
299 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
300 *
301 * @return the TargetHint
302 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100303inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000304{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100305 ARM_COMPUTE_ERROR_ON_MSG(target > 3, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner), 3 (GLES)");
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100306 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000307 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100308 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000309 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100310 else if(target == 3)
311 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100312 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100313 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000314 else
315 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100316 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000317 }
318}
Michalis Spyroue4720822017-10-02 17:44:52 +0100319} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100320} // namespace arm_compute
321
322#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */