blob: 52180ca495799ccc5a267e73735d8d2f12017dec [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 Marco Iodicead486e22018-08-07 17:17:06 +010028#include "arm_compute/core/Utils.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010029#include "arm_compute/core/utils/misc/Utility.h"
Gian Marcobfa3b522017-12-12 10:08:38 +000030#include "arm_compute/graph/Graph.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010031#include "arm_compute/graph/ITensorAccessor.h"
32#include "arm_compute/graph/Types.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010033#include "arm_compute/runtime/Tensor.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010034
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010035#include "utils/CommonGraphOptions.h"
36
Georgios Pinitas140fdc72018-02-16 11:42:38 +000037#include <array>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010038#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010039#include <string>
40#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010041
Anthony Barbier2a07e182017-08-04 18:20:27 +010042namespace arm_compute
43{
44namespace graph_utils
45{
Georgios Pinitas140fdc72018-02-16 11:42:38 +000046/** Preprocessor interface **/
47class IPreprocessor
48{
49public:
Alex Gildayc357c472018-03-21 13:54:09 +000050 /** Default destructor. */
51 virtual ~IPreprocessor() = default;
52 /** Preprocess the given tensor.
53 *
54 * @param[in] tensor Tensor to preprocess.
55 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +000056 virtual void preprocess(ITensor &tensor) = 0;
57};
58
59/** Caffe preproccessor */
60class CaffePreproccessor : public IPreprocessor
61{
62public:
63 /** Default Constructor
64 *
65 * @param mean Mean array in RGB ordering
66 * @param bgr Boolean specifying if the preprocessing should assume BGR format
67 */
68 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true);
69 void preprocess(ITensor &tensor) override;
70
71private:
72 std::array<float, 3> _mean;
73 bool _bgr;
74};
75
76/** TF preproccessor */
77class TFPreproccessor : public IPreprocessor
78{
79public:
80 void preprocess(ITensor &tensor) override;
81};
82
Anthony Barbier2a07e182017-08-04 18:20:27 +010083/** PPM writer class */
84class PPMWriter : public graph::ITensorAccessor
85{
86public:
87 /** Constructor
88 *
89 * @param[in] name PPM file name
90 * @param[in] maximum Maximum elements to access
91 */
92 PPMWriter(std::string name, unsigned int maximum = 1);
93 /** Allows instances to move constructed */
94 PPMWriter(PPMWriter &&) = default;
95
96 // Inherited methods overriden:
97 bool access_tensor(ITensor &tensor) override;
98
99private:
100 const std::string _name;
101 unsigned int _iterator;
102 unsigned int _maximum;
103};
104
105/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100106class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100107{
108public:
109 /** Constructor
110 *
111 * @param[in] maximum Maximum elements to write
112 */
113 DummyAccessor(unsigned int maximum = 1);
114 /** Allows instances to move constructed */
115 DummyAccessor(DummyAccessor &&) = default;
116
117 // Inherited methods overriden:
118 bool access_tensor(ITensor &tensor) override;
119
120private:
121 unsigned int _iterator;
122 unsigned int _maximum;
123};
124
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100125/** NumPy accessor class */
126class NumPyAccessor final : public graph::ITensorAccessor
127{
128public:
129 /** Constructor
130 *
131 * @param[in] npy_path Path to npy file.
132 * @param[in] shape Shape of the numpy tensor data.
133 * @param[in] data_type DataType of the numpy tensor data.
134 * @param[out] output_stream (Optional) Output stream
135 */
136 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout);
137 /** Allow instances of this class to be move constructed */
138 NumPyAccessor(NumPyAccessor &&) = default;
139 /** Prevent instances of this class from being copied (As this class contains pointers) */
140 NumPyAccessor(const NumPyAccessor &) = delete;
141 /** Prevent instances of this class from being copied (As this class contains pointers) */
142 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
143
144 // Inherited methods overriden:
145 bool access_tensor(ITensor &tensor) override;
146
147private:
148 template <typename T>
149 void access_numpy_tensor(ITensor &tensor);
150
151 Tensor _npy_tensor;
152 const std::string _filename;
153 std::ostream &_output_stream;
154};
155
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100156/** Image accessor class */
157class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100158{
159public:
160 /** Constructor
161 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100162 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100163 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100164 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100165 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100166 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100167 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100168 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100169
170 // Inherited methods overriden:
171 bool access_tensor(ITensor &tensor) override;
172
173private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100174 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000175 const bool _bgr;
176 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100177};
178
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100179/** Input Accessor used for network validation */
180class ValidationInputAccessor final : public graph::ITensorAccessor
181{
182public:
183 /** Constructor
184 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100185 * @param[in] image_list File containing all the images to validate
186 * @param[in] images_path Path to images.
187 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
188 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
189 * @param[in] start (Optional) Start range
190 * @param[in] end (Optional) End range
191 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100192 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100193 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100194 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100195 ValidationInputAccessor(const std::string &image_list,
196 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100197 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
198 bool bgr = true,
199 unsigned int start = 0,
200 unsigned int end = 0,
201 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100202
203 // Inherited methods overriden:
204 bool access_tensor(ITensor &tensor) override;
205
206private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100207 std::string _path;
208 std::vector<std::string> _images;
209 std::unique_ptr<IPreprocessor> _preprocessor;
210 bool _bgr;
211 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100212 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100213};
214
Georgios Pinitas7908de72018-06-27 12:34:20 +0100215/** Output Accessor used for network validation */
216class ValidationOutputAccessor final : public graph::ITensorAccessor
217{
218public:
219 /** Default Constructor
220 *
221 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100222 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
223 * @param[in] start (Optional) Start range
224 * @param[in] end (Optional) End range
225 *
226 * @note Range is defined as [start, end]
227 */
228 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100229 std::ostream &output_stream = std::cout,
230 unsigned int start = 0,
231 unsigned int end = 0);
232 /** Reset accessor state */
233 void reset();
234
235 // Inherited methods overriden:
236 bool access_tensor(ITensor &tensor) override;
237
238private:
239 /** Access predictions of the tensor
240 *
241 * @tparam T Tensor elements type
242 *
243 * @param[in] tensor Tensor to read the predictions from
244 */
245 template <typename T>
246 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100247 /** Aggregates the results of a sample
248 *
249 * @param[in] res Vector containing the results of a graph
250 * @param[in,out] positive_samples Positive samples to be updated
251 * @param[in] top_n Top n accuracy to measure
252 * @param[in] correct_label Correct label of the current sample
253 */
254 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
255 /** Reports top N accuracy
256 *
257 * @param[in] top_n Top N accuracy that is being reported
258 * @param[in] total_samples Total number of samples
259 * @param[in] positive_samples Positive samples
260 */
261 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100262
263private:
264 std::vector<int> _results;
265 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100266 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100267 size_t _positive_samples_top1;
268 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100269};
270
Gian Marco44ec2e72017-10-19 14:13:38 +0100271/** Result accessor class */
272class TopNPredictionsAccessor final : public graph::ITensorAccessor
273{
274public:
275 /** Constructor
276 *
277 * @param[in] labels_path Path to labels text file.
278 * @param[in] top_n (Optional) Number of output classes to print
279 * @param[out] output_stream (Optional) Output stream
280 */
281 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
282 /** Allow instances of this class to be move constructed */
283 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
284 /** Prevent instances of this class from being copied (As this class contains pointers) */
285 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
286 /** Prevent instances of this class from being copied (As this class contains pointers) */
287 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
288
289 // Inherited methods overriden:
290 bool access_tensor(ITensor &tensor) override;
291
292private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000293 template <typename T>
294 void access_predictions_tensor(ITensor &tensor);
295
Gian Marco44ec2e72017-10-19 14:13:38 +0100296 std::vector<std::string> _labels;
297 std::ostream &_output_stream;
298 size_t _top_n;
299};
300
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100301/** Random accessor class */
302class RandomAccessor final : public graph::ITensorAccessor
303{
304public:
305 /** Constructor
306 *
307 * @param[in] lower Lower bound value.
308 * @param[in] upper Upper bound value.
309 * @param[in] seed (Optional) Seed used to initialise the random number generator.
310 */
311 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
312 /** Allows instances to move constructed */
313 RandomAccessor(RandomAccessor &&) = default;
314
315 // Inherited methods overriden:
316 bool access_tensor(ITensor &tensor) override;
317
318private:
319 template <typename T, typename D>
320 void fill(ITensor &tensor, D &&distribution);
321 PixelValue _lower;
322 PixelValue _upper;
323 std::random_device::result_type _seed;
324};
325
Anthony Barbier2a07e182017-08-04 18:20:27 +0100326/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100327class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100328{
329public:
330 /** Default Constructor
331 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100332 * @param[in] filename Binary file name
333 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100334 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100335 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100336 /** Allows instances to move constructed */
337 NumPyBinLoader(NumPyBinLoader &&) = default;
338
339 // Inherited methods overriden:
340 bool access_tensor(ITensor &tensor) override;
341
342private:
343 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100344 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100345};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000346
Georgios Pinitas652bde52018-01-10 15:33:28 +0000347/** Generates appropriate random accessor
348 *
349 * @param[in] lower Lower random values bound
350 * @param[in] upper Upper random values bound
351 * @param[in] seed Random generator seed
352 *
353 * @return A ramdom accessor
354 */
355inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
356{
357 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
358}
359
Isabella Gottardia4c61882017-11-03 12:11:55 +0000360/** Generates appropriate weights accessor according to the specified path
361 *
362 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
363 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100364 * @param[in] path Path to the data files
365 * @param[in] data_file Relative path to the data files from path
366 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000367 *
368 * @return An appropriate tensor accessor
369 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100370inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
371 const std::string &data_file,
372 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000373{
374 if(path.empty())
375 {
376 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
377 }
378 else
379 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100380 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000381 }
382}
383
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100384/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000385 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100386 * @param[in] graph_parameters Graph parameters
387 * @param[in] preprocessor (Optional) Preproccessor object
388 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000389 *
390 * @return An appropriate tensor accessor
391 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100392inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
393 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
394 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000395{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100396 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000397 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100398 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
399 graph_parameters.validation_path,
400 std::move(preprocessor),
401 bgr,
402 graph_parameters.validation_range_start,
403 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000404 }
405 else
406 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100407 const std::string &image_file = graph_parameters.image;
408 const std::string &image_file_lower = lower_string(image_file);
409 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100410 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100411 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
412 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100413 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
414 || arm_compute::utility::endswith(image_file_lower, ".jpg")
415 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100416 {
417 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100418 }
419 else
420 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100421 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100422 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000423 }
424}
425
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100426/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000427 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100428 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
429 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000430 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100431 * @param[in] graph_parameters Graph parameters
432 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
433 * @param[in] is_validation (Optional) Validation flag (default = false)
434 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000435 *
436 * @return An appropriate tensor accessor
437 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100438inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
439 size_t top_n = 5,
440 bool is_validation = false,
441 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000442{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100443 if(!graph_parameters.validation_file.empty())
444 {
445 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
446 output_stream,
447 graph_parameters.validation_range_start,
448 graph_parameters.validation_range_end);
449 }
450 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000451 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000452 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000453 }
454 else
455 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100456 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000457 }
458}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100459/** Generates appropriate npy output accessor according to the specified npy_path
460 *
461 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
462 *
463 * @param[in] npy_path Path to npy file.
464 * @param[in] shape Shape of the numpy tensor data.
465 * @param[in] data_type DataType of the numpy tensor data.
466 * @param[out] output_stream (Optional) Output stream
467 *
468 * @return An appropriate tensor accessor
469 */
470inline 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)
471{
472 if(npy_path.empty())
473 {
474 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
475 }
476 else
477 {
478 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream);
479 }
480}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000481
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100482/** Permutes a given tensor shape given the input and output data layout
483 *
484 * @param[in] tensor_shape Tensor shape to permute
485 * @param[in] in_data_layout Input tensor shape data layout
486 * @param[in] out_data_layout Output tensor shape data layout
487 *
488 * @return Permuted tensor shape
489 */
490inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
491{
492 if(in_data_layout != out_data_layout)
493 {
494 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
495 arm_compute::permute(tensor_shape, perm_vec);
496 }
497 return tensor_shape;
498}
499
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000500/** Utility function to return the TargetHint
501 *
502 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
503 *
504 * @return the TargetHint
505 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100506inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000507{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100508 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 +0100509 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000510 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100511 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000512 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100513 else if(target == 3)
514 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100515 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100516 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000517 else
518 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100519 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000520 }
521}
Michalis Spyroue4720822017-10-02 17:44:52 +0100522} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100523} // namespace arm_compute
524
525#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */