blob: 597708369dac0b74edc531e5fb682bcd725123b6 [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"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010028#include "arm_compute/core/utils/misc/Utility.h"
Gian Marcobfa3b522017-12-12 10:08:38 +000029#include "arm_compute/graph/Graph.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010030#include "arm_compute/graph/ITensorAccessor.h"
31#include "arm_compute/graph/Types.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010032#include "arm_compute/runtime/Tensor.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010033
Georgios Pinitas140fdc72018-02-16 11:42:38 +000034#include <array>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010035#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010036#include <string>
37#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010038
Anthony Barbier2a07e182017-08-04 18:20:27 +010039namespace arm_compute
40{
41namespace graph_utils
42{
Georgios Pinitas140fdc72018-02-16 11:42:38 +000043/** Preprocessor interface **/
44class IPreprocessor
45{
46public:
Alex Gildayc357c472018-03-21 13:54:09 +000047 /** Default destructor. */
48 virtual ~IPreprocessor() = default;
49 /** Preprocess the given tensor.
50 *
51 * @param[in] tensor Tensor to preprocess.
52 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +000053 virtual void preprocess(ITensor &tensor) = 0;
54};
55
56/** Caffe preproccessor */
57class CaffePreproccessor : public IPreprocessor
58{
59public:
60 /** Default Constructor
61 *
62 * @param mean Mean array in RGB ordering
63 * @param bgr Boolean specifying if the preprocessing should assume BGR format
64 */
65 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true);
66 void preprocess(ITensor &tensor) override;
67
68private:
69 std::array<float, 3> _mean;
70 bool _bgr;
71};
72
73/** TF preproccessor */
74class TFPreproccessor : public IPreprocessor
75{
76public:
77 void preprocess(ITensor &tensor) override;
78};
79
Anthony Barbier2a07e182017-08-04 18:20:27 +010080/** PPM writer class */
81class PPMWriter : public graph::ITensorAccessor
82{
83public:
84 /** Constructor
85 *
86 * @param[in] name PPM file name
87 * @param[in] maximum Maximum elements to access
88 */
89 PPMWriter(std::string name, unsigned int maximum = 1);
90 /** Allows instances to move constructed */
91 PPMWriter(PPMWriter &&) = default;
92
93 // Inherited methods overriden:
94 bool access_tensor(ITensor &tensor) override;
95
96private:
97 const std::string _name;
98 unsigned int _iterator;
99 unsigned int _maximum;
100};
101
102/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100103class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100104{
105public:
106 /** Constructor
107 *
108 * @param[in] maximum Maximum elements to write
109 */
110 DummyAccessor(unsigned int maximum = 1);
111 /** Allows instances to move constructed */
112 DummyAccessor(DummyAccessor &&) = default;
113
114 // Inherited methods overriden:
115 bool access_tensor(ITensor &tensor) override;
116
117private:
118 unsigned int _iterator;
119 unsigned int _maximum;
120};
121
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100122/** NumPy accessor class */
123class NumPyAccessor final : public graph::ITensorAccessor
124{
125public:
126 /** Constructor
127 *
128 * @param[in] npy_path Path to npy file.
129 * @param[in] shape Shape of the numpy tensor data.
130 * @param[in] data_type DataType of the numpy tensor data.
131 * @param[out] output_stream (Optional) Output stream
132 */
133 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout);
134 /** Allow instances of this class to be move constructed */
135 NumPyAccessor(NumPyAccessor &&) = default;
136 /** Prevent instances of this class from being copied (As this class contains pointers) */
137 NumPyAccessor(const NumPyAccessor &) = delete;
138 /** Prevent instances of this class from being copied (As this class contains pointers) */
139 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
140
141 // Inherited methods overriden:
142 bool access_tensor(ITensor &tensor) override;
143
144private:
145 template <typename T>
146 void access_numpy_tensor(ITensor &tensor);
147
148 Tensor _npy_tensor;
149 const std::string _filename;
150 std::ostream &_output_stream;
151};
152
Gian Marco44ec2e72017-10-19 14:13:38 +0100153/** PPM accessor class */
154class PPMAccessor final : public graph::ITensorAccessor
155{
156public:
157 /** Constructor
158 *
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000159 * @param[in] ppm_path Path to PPM file
160 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false)
161 * @param[in] preprocessor (Optional) PPM pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100162 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000163 PPMAccessor(std::string ppm_path, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100164 /** Allow instances of this class to be move constructed */
165 PPMAccessor(PPMAccessor &&) = default;
166
167 // Inherited methods overriden:
168 bool access_tensor(ITensor &tensor) override;
169
170private:
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000171 const std::string _ppm_path;
172 const bool _bgr;
173 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100174};
175
176/** Result accessor class */
177class TopNPredictionsAccessor final : public graph::ITensorAccessor
178{
179public:
180 /** Constructor
181 *
182 * @param[in] labels_path Path to labels text file.
183 * @param[in] top_n (Optional) Number of output classes to print
184 * @param[out] output_stream (Optional) Output stream
185 */
186 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
187 /** Allow instances of this class to be move constructed */
188 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
189 /** Prevent instances of this class from being copied (As this class contains pointers) */
190 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
191 /** Prevent instances of this class from being copied (As this class contains pointers) */
192 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
193
194 // Inherited methods overriden:
195 bool access_tensor(ITensor &tensor) override;
196
197private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000198 template <typename T>
199 void access_predictions_tensor(ITensor &tensor);
200
Gian Marco44ec2e72017-10-19 14:13:38 +0100201 std::vector<std::string> _labels;
202 std::ostream &_output_stream;
203 size_t _top_n;
204};
205
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100206/** Random accessor class */
207class RandomAccessor final : public graph::ITensorAccessor
208{
209public:
210 /** Constructor
211 *
212 * @param[in] lower Lower bound value.
213 * @param[in] upper Upper bound value.
214 * @param[in] seed (Optional) Seed used to initialise the random number generator.
215 */
216 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
217 /** Allows instances to move constructed */
218 RandomAccessor(RandomAccessor &&) = default;
219
220 // Inherited methods overriden:
221 bool access_tensor(ITensor &tensor) override;
222
223private:
224 template <typename T, typename D>
225 void fill(ITensor &tensor, D &&distribution);
226 PixelValue _lower;
227 PixelValue _upper;
228 std::random_device::result_type _seed;
229};
230
Anthony Barbier2a07e182017-08-04 18:20:27 +0100231/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100232class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100233{
234public:
235 /** Default Constructor
236 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100237 * @param[in] filename Binary file name
238 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100239 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100240 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100241 /** Allows instances to move constructed */
242 NumPyBinLoader(NumPyBinLoader &&) = default;
243
244 // Inherited methods overriden:
245 bool access_tensor(ITensor &tensor) override;
246
247private:
248 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100249 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100250};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000251
Georgios Pinitas652bde52018-01-10 15:33:28 +0000252/** Generates appropriate random accessor
253 *
254 * @param[in] lower Lower random values bound
255 * @param[in] upper Upper random values bound
256 * @param[in] seed Random generator seed
257 *
258 * @return A ramdom accessor
259 */
260inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
261{
262 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
263}
264
Isabella Gottardia4c61882017-11-03 12:11:55 +0000265/** Generates appropriate weights accessor according to the specified path
266 *
267 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
268 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100269 * @param[in] path Path to the data files
270 * @param[in] data_file Relative path to the data files from path
271 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000272 *
273 * @return An appropriate tensor accessor
274 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100275inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
276 const std::string &data_file,
277 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000278{
279 if(path.empty())
280 {
281 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
282 }
283 else
284 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100285 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000286 }
287}
288
289/** Generates appropriate input accessor according to the specified ppm_path
290 *
291 * @note If ppm_path is empty will generate a DummyAccessor else will generate a PPMAccessor
292 *
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000293 * @param[in] ppm_path Path to PPM file
294 * @param[in] preprocessor Preproccessor object
295 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000296 *
297 * @return An appropriate tensor accessor
298 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000299inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const std::string &ppm_path,
300 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
301 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000302{
303 if(ppm_path.empty())
304 {
305 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
306 }
307 else
308 {
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100309 if(arm_compute::utility::endswith(ppm_path, ".npy"))
310 {
311 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(ppm_path);
312 }
313 else
314 {
315 return arm_compute::support::cpp14::make_unique<PPMAccessor>(ppm_path, bgr, std::move(preprocessor));
316 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000317 }
318}
319
320/** Generates appropriate output accessor according to the specified labels_path
321 *
322 * @note If labels_path is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
323 *
324 * @param[in] labels_path Path to labels text file
325 * @param[in] top_n (Optional) Number of output classes to print
326 * @param[out] output_stream (Optional) Output stream
327 *
328 * @return An appropriate tensor accessor
329 */
330inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout)
331{
332 if(labels_path.empty())
333 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000334 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000335 }
336 else
337 {
338 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream);
339 }
340}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100341/** Generates appropriate npy output accessor according to the specified npy_path
342 *
343 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
344 *
345 * @param[in] npy_path Path to npy file.
346 * @param[in] shape Shape of the numpy tensor data.
347 * @param[in] data_type DataType of the numpy tensor data.
348 * @param[out] output_stream (Optional) Output stream
349 *
350 * @return An appropriate tensor accessor
351 */
352inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout)
353{
354 if(npy_path.empty())
355 {
356 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
357 }
358 else
359 {
360 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream);
361 }
362}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000363
364/** Utility function to return the TargetHint
365 *
366 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
367 *
368 * @return the TargetHint
369 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100370inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000371{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100372 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 +0100373 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000374 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100375 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000376 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100377 else if(target == 3)
378 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100379 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100380 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000381 else
382 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100383 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000384 }
385}
Michalis Spyroue4720822017-10-02 17:44:52 +0100386} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100387} // namespace arm_compute
388
389#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */