blob: 6cec2dae4d234cc2b6b6d311b8582892894d3e18 [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 Pinitas12be7ab2018-07-03 12:06:23 +010034#include "utils/CommonGraphOptions.h"
35
Georgios Pinitas140fdc72018-02-16 11:42:38 +000036#include <array>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010037#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010038#include <string>
39#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010040
Anthony Barbier2a07e182017-08-04 18:20:27 +010041namespace arm_compute
42{
43namespace graph_utils
44{
Georgios Pinitas140fdc72018-02-16 11:42:38 +000045/** Preprocessor interface **/
46class IPreprocessor
47{
48public:
Alex Gildayc357c472018-03-21 13:54:09 +000049 /** Default destructor. */
50 virtual ~IPreprocessor() = default;
51 /** Preprocess the given tensor.
52 *
53 * @param[in] tensor Tensor to preprocess.
54 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +000055 virtual void preprocess(ITensor &tensor) = 0;
56};
57
58/** Caffe preproccessor */
59class CaffePreproccessor : public IPreprocessor
60{
61public:
62 /** Default Constructor
63 *
64 * @param mean Mean array in RGB ordering
65 * @param bgr Boolean specifying if the preprocessing should assume BGR format
66 */
67 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true);
68 void preprocess(ITensor &tensor) override;
69
70private:
71 std::array<float, 3> _mean;
72 bool _bgr;
73};
74
75/** TF preproccessor */
76class TFPreproccessor : public IPreprocessor
77{
78public:
79 void preprocess(ITensor &tensor) override;
80};
81
Anthony Barbier2a07e182017-08-04 18:20:27 +010082/** PPM writer class */
83class PPMWriter : public graph::ITensorAccessor
84{
85public:
86 /** Constructor
87 *
88 * @param[in] name PPM file name
89 * @param[in] maximum Maximum elements to access
90 */
91 PPMWriter(std::string name, unsigned int maximum = 1);
92 /** Allows instances to move constructed */
93 PPMWriter(PPMWriter &&) = default;
94
95 // Inherited methods overriden:
96 bool access_tensor(ITensor &tensor) override;
97
98private:
99 const std::string _name;
100 unsigned int _iterator;
101 unsigned int _maximum;
102};
103
104/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100105class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100106{
107public:
108 /** Constructor
109 *
110 * @param[in] maximum Maximum elements to write
111 */
112 DummyAccessor(unsigned int maximum = 1);
113 /** Allows instances to move constructed */
114 DummyAccessor(DummyAccessor &&) = default;
115
116 // Inherited methods overriden:
117 bool access_tensor(ITensor &tensor) override;
118
119private:
120 unsigned int _iterator;
121 unsigned int _maximum;
122};
123
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100124/** NumPy accessor class */
125class NumPyAccessor final : public graph::ITensorAccessor
126{
127public:
128 /** Constructor
129 *
130 * @param[in] npy_path Path to npy file.
131 * @param[in] shape Shape of the numpy tensor data.
132 * @param[in] data_type DataType of the numpy tensor data.
133 * @param[out] output_stream (Optional) Output stream
134 */
135 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout);
136 /** Allow instances of this class to be move constructed */
137 NumPyAccessor(NumPyAccessor &&) = default;
138 /** Prevent instances of this class from being copied (As this class contains pointers) */
139 NumPyAccessor(const NumPyAccessor &) = delete;
140 /** Prevent instances of this class from being copied (As this class contains pointers) */
141 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
142
143 // Inherited methods overriden:
144 bool access_tensor(ITensor &tensor) override;
145
146private:
147 template <typename T>
148 void access_numpy_tensor(ITensor &tensor);
149
150 Tensor _npy_tensor;
151 const std::string _filename;
152 std::ostream &_output_stream;
153};
154
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100155/** Image accessor class */
156class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100157{
158public:
159 /** Constructor
160 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100161 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100162 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100163 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100164 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100165 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100166 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100167 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100168
169 // Inherited methods overriden:
170 bool access_tensor(ITensor &tensor) override;
171
172private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100173 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000174 const bool _bgr;
175 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100176};
177
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100178/** Input Accessor used for network validation */
179class ValidationInputAccessor final : public graph::ITensorAccessor
180{
181public:
182 /** Constructor
183 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100184 * @param[in] image_list File containing all the images to validate
185 * @param[in] images_path Path to images.
186 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
187 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
188 * @param[in] start (Optional) Start range
189 * @param[in] end (Optional) End range
190 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100191 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100192 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100193 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100194 ValidationInputAccessor(const std::string &image_list,
195 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100196 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
197 bool bgr = true,
198 unsigned int start = 0,
199 unsigned int end = 0,
200 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100201
202 // Inherited methods overriden:
203 bool access_tensor(ITensor &tensor) override;
204
205private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100206 std::string _path;
207 std::vector<std::string> _images;
208 std::unique_ptr<IPreprocessor> _preprocessor;
209 bool _bgr;
210 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100211 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100212};
213
Georgios Pinitas7908de72018-06-27 12:34:20 +0100214/** Output Accessor used for network validation */
215class ValidationOutputAccessor final : public graph::ITensorAccessor
216{
217public:
218 /** Default Constructor
219 *
220 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100221 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
222 * @param[in] start (Optional) Start range
223 * @param[in] end (Optional) End range
224 *
225 * @note Range is defined as [start, end]
226 */
227 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100228 std::ostream &output_stream = std::cout,
229 unsigned int start = 0,
230 unsigned int end = 0);
231 /** Reset accessor state */
232 void reset();
233
234 // Inherited methods overriden:
235 bool access_tensor(ITensor &tensor) override;
236
237private:
238 /** Access predictions of the tensor
239 *
240 * @tparam T Tensor elements type
241 *
242 * @param[in] tensor Tensor to read the predictions from
243 */
244 template <typename T>
245 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100246 /** Aggregates the results of a sample
247 *
248 * @param[in] res Vector containing the results of a graph
249 * @param[in,out] positive_samples Positive samples to be updated
250 * @param[in] top_n Top n accuracy to measure
251 * @param[in] correct_label Correct label of the current sample
252 */
253 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
254 /** Reports top N accuracy
255 *
256 * @param[in] top_n Top N accuracy that is being reported
257 * @param[in] total_samples Total number of samples
258 * @param[in] positive_samples Positive samples
259 */
260 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100261
262private:
263 std::vector<int> _results;
264 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100265 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100266 size_t _positive_samples_top1;
267 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100268};
269
Gian Marco44ec2e72017-10-19 14:13:38 +0100270/** Result accessor class */
271class TopNPredictionsAccessor final : public graph::ITensorAccessor
272{
273public:
274 /** Constructor
275 *
276 * @param[in] labels_path Path to labels text file.
277 * @param[in] top_n (Optional) Number of output classes to print
278 * @param[out] output_stream (Optional) Output stream
279 */
280 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
281 /** Allow instances of this class to be move constructed */
282 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
283 /** Prevent instances of this class from being copied (As this class contains pointers) */
284 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
285 /** Prevent instances of this class from being copied (As this class contains pointers) */
286 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
287
288 // Inherited methods overriden:
289 bool access_tensor(ITensor &tensor) override;
290
291private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000292 template <typename T>
293 void access_predictions_tensor(ITensor &tensor);
294
Gian Marco44ec2e72017-10-19 14:13:38 +0100295 std::vector<std::string> _labels;
296 std::ostream &_output_stream;
297 size_t _top_n;
298};
299
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100300/** Random accessor class */
301class RandomAccessor final : public graph::ITensorAccessor
302{
303public:
304 /** Constructor
305 *
306 * @param[in] lower Lower bound value.
307 * @param[in] upper Upper bound value.
308 * @param[in] seed (Optional) Seed used to initialise the random number generator.
309 */
310 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
311 /** Allows instances to move constructed */
312 RandomAccessor(RandomAccessor &&) = default;
313
314 // Inherited methods overriden:
315 bool access_tensor(ITensor &tensor) override;
316
317private:
318 template <typename T, typename D>
319 void fill(ITensor &tensor, D &&distribution);
320 PixelValue _lower;
321 PixelValue _upper;
322 std::random_device::result_type _seed;
323};
324
Anthony Barbier2a07e182017-08-04 18:20:27 +0100325/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100326class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100327{
328public:
329 /** Default Constructor
330 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100331 * @param[in] filename Binary file name
332 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100333 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100334 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100335 /** Allows instances to move constructed */
336 NumPyBinLoader(NumPyBinLoader &&) = default;
337
338 // Inherited methods overriden:
339 bool access_tensor(ITensor &tensor) override;
340
341private:
342 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100343 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100344};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000345
Georgios Pinitas652bde52018-01-10 15:33:28 +0000346/** Generates appropriate random accessor
347 *
348 * @param[in] lower Lower random values bound
349 * @param[in] upper Upper random values bound
350 * @param[in] seed Random generator seed
351 *
352 * @return A ramdom accessor
353 */
354inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
355{
356 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
357}
358
Isabella Gottardia4c61882017-11-03 12:11:55 +0000359/** Generates appropriate weights accessor according to the specified path
360 *
361 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
362 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100363 * @param[in] path Path to the data files
364 * @param[in] data_file Relative path to the data files from path
365 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000366 *
367 * @return An appropriate tensor accessor
368 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100369inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
370 const std::string &data_file,
371 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000372{
373 if(path.empty())
374 {
375 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
376 }
377 else
378 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100379 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000380 }
381}
382
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100383/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000384 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100385 * @param[in] graph_parameters Graph parameters
386 * @param[in] preprocessor (Optional) Preproccessor object
387 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000388 *
389 * @return An appropriate tensor accessor
390 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100391inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
392 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
393 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000394{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100395 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000396 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100397 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
398 graph_parameters.validation_path,
399 std::move(preprocessor),
400 bgr,
401 graph_parameters.validation_range_start,
402 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000403 }
404 else
405 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100406 const std::string &image_file = graph_parameters.image;
407 if(arm_compute::utility::endswith(image_file, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100408 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100409 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
410 }
411 else if(arm_compute::utility::endswith(image_file, ".jpeg")
412 || arm_compute::utility::endswith(image_file, ".jpg")
413 || arm_compute::utility::endswith(image_file, ".ppm"))
414 {
415 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100416 }
417 else
418 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100419 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100420 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000421 }
422}
423
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100424/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000425 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100426 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
427 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000428 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100429 * @param[in] graph_parameters Graph parameters
430 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
431 * @param[in] is_validation (Optional) Validation flag (default = false)
432 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000433 *
434 * @return An appropriate tensor accessor
435 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100436inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
437 size_t top_n = 5,
438 bool is_validation = false,
439 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000440{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100441 if(!graph_parameters.validation_file.empty())
442 {
443 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
444 output_stream,
445 graph_parameters.validation_range_start,
446 graph_parameters.validation_range_end);
447 }
448 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000449 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000450 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000451 }
452 else
453 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100454 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000455 }
456}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100457/** Generates appropriate npy output accessor according to the specified npy_path
458 *
459 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
460 *
461 * @param[in] npy_path Path to npy file.
462 * @param[in] shape Shape of the numpy tensor data.
463 * @param[in] data_type DataType of the numpy tensor data.
464 * @param[out] output_stream (Optional) Output stream
465 *
466 * @return An appropriate tensor accessor
467 */
468inline 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)
469{
470 if(npy_path.empty())
471 {
472 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
473 }
474 else
475 {
476 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream);
477 }
478}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000479
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100480/** Permutes a given tensor shape given the input and output data layout
481 *
482 * @param[in] tensor_shape Tensor shape to permute
483 * @param[in] in_data_layout Input tensor shape data layout
484 * @param[in] out_data_layout Output tensor shape data layout
485 *
486 * @return Permuted tensor shape
487 */
488inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
489{
490 if(in_data_layout != out_data_layout)
491 {
492 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
493 arm_compute::permute(tensor_shape, perm_vec);
494 }
495 return tensor_shape;
496}
497
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000498/** Utility function to return the TargetHint
499 *
500 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
501 *
502 * @return the TargetHint
503 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100504inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000505{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100506 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 +0100507 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000508 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100509 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000510 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100511 else if(target == 3)
512 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100513 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100514 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000515 else
516 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100517 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000518 }
519}
Michalis Spyroue4720822017-10-02 17:44:52 +0100520} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100521} // namespace arm_compute
522
523#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */