blob: d7f24afdd87b418bc9f4e2b9c7d495dbf124a58b [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +01001/*
Anthony Barbierf45d5a92018-01-24 16:23:15 +00002 * Copyright (c) 2017-2018 ARM Limited.
Kaizen8938bd32017-09-28 14:38:23 +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
Kaizenbf8b01d2017-10-12 14:26:51 +010027#include "arm_compute/core/PixelValue.h"
Jenkins52ba29e2018-08-29 15:32:11 +000028#include "arm_compute/core/Utils.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010029#include "arm_compute/core/utils/misc/Utility.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/graph/Graph.h"
Kaizen8938bd32017-09-28 14:38:23 +010031#include "arm_compute/graph/ITensorAccessor.h"
32#include "arm_compute/graph/Types.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010033#include "arm_compute/runtime/Tensor.h"
Kaizen8938bd32017-09-28 14:38:23 +010034
Jenkins52ba29e2018-08-29 15:32:11 +000035#include "utils/CommonGraphOptions.h"
36
Anthony Barbier06ea0482018-02-22 15:45:35 +000037#include <array>
Kaizenbf8b01d2017-10-12 14:26:51 +010038#include <random>
Anthony Barbier8a3da6f2017-10-23 18:55:17 +010039#include <string>
40#include <vector>
Kaizenbf8b01d2017-10-12 14:26:51 +010041
Kaizen8938bd32017-09-28 14:38:23 +010042namespace arm_compute
43{
44namespace graph_utils
45{
Anthony Barbier06ea0482018-02-22 15:45:35 +000046/** Preprocessor interface **/
47class IPreprocessor
48{
49public:
Jenkinsb3a371b2018-05-23 11:36:53 +010050 /** Default destructor. */
51 virtual ~IPreprocessor() = default;
52 /** Preprocess the given tensor.
53 *
54 * @param[in] tensor Tensor to preprocess.
55 */
Anthony Barbier06ea0482018-02-22 15:45:35 +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
Jenkinsb9abeae2018-11-22 11:58:08 +000066 * @param scale Scale value
Anthony Barbier06ea0482018-02-22 15:45:35 +000067 * @param bgr Boolean specifying if the preprocessing should assume BGR format
68 */
Jenkinsb9abeae2018-11-22 11:58:08 +000069 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, float scale = 1.f, bool bgr = true);
Anthony Barbier06ea0482018-02-22 15:45:35 +000070 void preprocess(ITensor &tensor) override;
71
72private:
73 std::array<float, 3> _mean;
Jenkinsb9abeae2018-11-22 11:58:08 +000074 float _scale;
75 bool _bgr;
Anthony Barbier06ea0482018-02-22 15:45:35 +000076};
77
78/** TF preproccessor */
79class TFPreproccessor : public IPreprocessor
80{
81public:
Jenkinsb9abeae2018-11-22 11:58:08 +000082 /** Constructor
83 *
84 * @param[in] min_range Min normalization range. (Defaults to -1.f)
85 * @param[in] max_range Max normalization range. (Defaults to 1.f)
86 */
87 TFPreproccessor(float min_range = -1.f, float max_range = 1.f);
88
89 // Inherited overriden methods
Anthony Barbier06ea0482018-02-22 15:45:35 +000090 void preprocess(ITensor &tensor) override;
Jenkinsb9abeae2018-11-22 11:58:08 +000091
92private:
93 float _min_range;
94 float _max_range;
Anthony Barbier06ea0482018-02-22 15:45:35 +000095};
96
Kaizen8938bd32017-09-28 14:38:23 +010097/** PPM writer class */
98class PPMWriter : public graph::ITensorAccessor
99{
100public:
101 /** Constructor
102 *
103 * @param[in] name PPM file name
104 * @param[in] maximum Maximum elements to access
105 */
106 PPMWriter(std::string name, unsigned int maximum = 1);
107 /** Allows instances to move constructed */
108 PPMWriter(PPMWriter &&) = default;
109
110 // Inherited methods overriden:
111 bool access_tensor(ITensor &tensor) override;
112
113private:
114 const std::string _name;
115 unsigned int _iterator;
116 unsigned int _maximum;
117};
118
119/** Dummy accessor class */
Kaizenbf8b01d2017-10-12 14:26:51 +0100120class DummyAccessor final : public graph::ITensorAccessor
Kaizen8938bd32017-09-28 14:38:23 +0100121{
122public:
123 /** Constructor
124 *
125 * @param[in] maximum Maximum elements to write
126 */
127 DummyAccessor(unsigned int maximum = 1);
128 /** Allows instances to move constructed */
129 DummyAccessor(DummyAccessor &&) = default;
130
131 // Inherited methods overriden:
132 bool access_tensor(ITensor &tensor) override;
133
134private:
135 unsigned int _iterator;
136 unsigned int _maximum;
137};
138
Jenkinsb3a371b2018-05-23 11:36:53 +0100139/** NumPy accessor class */
140class NumPyAccessor final : public graph::ITensorAccessor
141{
142public:
143 /** Constructor
144 *
145 * @param[in] npy_path Path to npy file.
146 * @param[in] shape Shape of the numpy tensor data.
147 * @param[in] data_type DataType of the numpy tensor data.
148 * @param[out] output_stream (Optional) Output stream
149 */
150 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout);
151 /** Allow instances of this class to be move constructed */
152 NumPyAccessor(NumPyAccessor &&) = default;
153 /** Prevent instances of this class from being copied (As this class contains pointers) */
154 NumPyAccessor(const NumPyAccessor &) = delete;
155 /** Prevent instances of this class from being copied (As this class contains pointers) */
156 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
157
158 // Inherited methods overriden:
159 bool access_tensor(ITensor &tensor) override;
160
161private:
162 template <typename T>
163 void access_numpy_tensor(ITensor &tensor);
164
165 Tensor _npy_tensor;
166 const std::string _filename;
167 std::ostream &_output_stream;
168};
169
Jenkins52ba29e2018-08-29 15:32:11 +0000170/** Image accessor class */
171class ImageAccessor final : public graph::ITensorAccessor
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100172{
173public:
174 /** Constructor
175 *
Jenkins52ba29e2018-08-29 15:32:11 +0000176 * @param[in] filename Image file
177 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
178 * @param[in] preprocessor (Optional) Image pre-processing object
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100179 */
Jenkins52ba29e2018-08-29 15:32:11 +0000180 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100181 /** Allow instances of this class to be move constructed */
Jenkins52ba29e2018-08-29 15:32:11 +0000182 ImageAccessor(ImageAccessor &&) = default;
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100183
184 // Inherited methods overriden:
185 bool access_tensor(ITensor &tensor) override;
186
187private:
Jenkins52ba29e2018-08-29 15:32:11 +0000188 bool _already_loaded;
189 const std::string _filename;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000190 const bool _bgr;
191 std::unique_ptr<IPreprocessor> _preprocessor;
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100192};
193
Jenkins52ba29e2018-08-29 15:32:11 +0000194/** Input Accessor used for network validation */
195class ValidationInputAccessor final : public graph::ITensorAccessor
196{
197public:
198 /** Constructor
199 *
200 * @param[in] image_list File containing all the images to validate
201 * @param[in] images_path Path to images.
202 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
203 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
204 * @param[in] start (Optional) Start range
205 * @param[in] end (Optional) End range
206 * @param[out] output_stream (Optional) Output stream
207 *
208 * @note Range is defined as [start, end]
209 */
210 ValidationInputAccessor(const std::string &image_list,
211 std::string images_path,
212 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
213 bool bgr = true,
214 unsigned int start = 0,
215 unsigned int end = 0,
216 std::ostream &output_stream = std::cout);
217
218 // Inherited methods overriden:
219 bool access_tensor(ITensor &tensor) override;
220
221private:
222 std::string _path;
223 std::vector<std::string> _images;
224 std::unique_ptr<IPreprocessor> _preprocessor;
225 bool _bgr;
226 size_t _offset;
227 std::ostream &_output_stream;
228};
229
230/** Output Accessor used for network validation */
231class ValidationOutputAccessor final : public graph::ITensorAccessor
232{
233public:
234 /** Default Constructor
235 *
236 * @param[in] image_list File containing all the images and labels results
237 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
238 * @param[in] start (Optional) Start range
239 * @param[in] end (Optional) End range
240 *
241 * @note Range is defined as [start, end]
242 */
243 ValidationOutputAccessor(const std::string &image_list,
244 std::ostream &output_stream = std::cout,
245 unsigned int start = 0,
246 unsigned int end = 0);
247 /** Reset accessor state */
248 void reset();
249
250 // Inherited methods overriden:
251 bool access_tensor(ITensor &tensor) override;
252
253private:
254 /** Access predictions of the tensor
255 *
256 * @tparam T Tensor elements type
257 *
258 * @param[in] tensor Tensor to read the predictions from
259 */
260 template <typename T>
261 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
262 /** Aggregates the results of a sample
263 *
264 * @param[in] res Vector containing the results of a graph
265 * @param[in,out] positive_samples Positive samples to be updated
266 * @param[in] top_n Top n accuracy to measure
267 * @param[in] correct_label Correct label of the current sample
268 */
269 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
270 /** Reports top N accuracy
271 *
272 * @param[in] top_n Top N accuracy that is being reported
273 * @param[in] total_samples Total number of samples
274 * @param[in] positive_samples Positive samples
275 */
276 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
277
278private:
279 std::vector<int> _results;
280 std::ostream &_output_stream;
281 size_t _offset;
282 size_t _positive_samples_top1;
283 size_t _positive_samples_top5;
284};
285
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100286/** Result accessor class */
287class TopNPredictionsAccessor final : public graph::ITensorAccessor
288{
289public:
290 /** Constructor
291 *
292 * @param[in] labels_path Path to labels text file.
293 * @param[in] top_n (Optional) Number of output classes to print
294 * @param[out] output_stream (Optional) Output stream
295 */
296 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
297 /** Allow instances of this class to be move constructed */
298 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
299 /** Prevent instances of this class from being copied (As this class contains pointers) */
300 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
301 /** Prevent instances of this class from being copied (As this class contains pointers) */
302 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
303
304 // Inherited methods overriden:
305 bool access_tensor(ITensor &tensor) override;
306
307private:
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000308 template <typename T>
309 void access_predictions_tensor(ITensor &tensor);
310
Anthony Barbier8a3da6f2017-10-23 18:55:17 +0100311 std::vector<std::string> _labels;
312 std::ostream &_output_stream;
313 size_t _top_n;
314};
315
Kaizenbf8b01d2017-10-12 14:26:51 +0100316/** Random accessor class */
317class RandomAccessor final : public graph::ITensorAccessor
318{
319public:
320 /** Constructor
321 *
322 * @param[in] lower Lower bound value.
323 * @param[in] upper Upper bound value.
324 * @param[in] seed (Optional) Seed used to initialise the random number generator.
325 */
326 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
327 /** Allows instances to move constructed */
328 RandomAccessor(RandomAccessor &&) = default;
329
330 // Inherited methods overriden:
331 bool access_tensor(ITensor &tensor) override;
332
333private:
334 template <typename T, typename D>
335 void fill(ITensor &tensor, D &&distribution);
336 PixelValue _lower;
337 PixelValue _upper;
338 std::random_device::result_type _seed;
339};
340
Kaizen8938bd32017-09-28 14:38:23 +0100341/** Numpy Binary loader class*/
Kaizenbf8b01d2017-10-12 14:26:51 +0100342class NumPyBinLoader final : public graph::ITensorAccessor
Kaizen8938bd32017-09-28 14:38:23 +0100343{
344public:
345 /** Default Constructor
346 *
Jenkinsb3a371b2018-05-23 11:36:53 +0100347 * @param[in] filename Binary file name
348 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Kaizen8938bd32017-09-28 14:38:23 +0100349 */
Jenkinsb3a371b2018-05-23 11:36:53 +0100350 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Kaizen8938bd32017-09-28 14:38:23 +0100351 /** Allows instances to move constructed */
352 NumPyBinLoader(NumPyBinLoader &&) = default;
353
354 // Inherited methods overriden:
355 bool access_tensor(ITensor &tensor) override;
356
357private:
Jenkins52ba29e2018-08-29 15:32:11 +0000358 bool _already_loaded;
Kaizen8938bd32017-09-28 14:38:23 +0100359 const std::string _filename;
Jenkinsb3a371b2018-05-23 11:36:53 +0100360 const DataLayout _file_layout;
Kaizen8938bd32017-09-28 14:38:23 +0100361};
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000362
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000363/** Generates appropriate random accessor
364 *
365 * @param[in] lower Lower random values bound
366 * @param[in] upper Upper random values bound
367 * @param[in] seed Random generator seed
368 *
369 * @return A ramdom accessor
370 */
371inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
372{
373 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
374}
375
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000376/** Generates appropriate weights accessor according to the specified path
377 *
378 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
379 *
Jenkinsb3a371b2018-05-23 11:36:53 +0100380 * @param[in] path Path to the data files
381 * @param[in] data_file Relative path to the data files from path
382 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000383 *
384 * @return An appropriate tensor accessor
385 */
Jenkinsb3a371b2018-05-23 11:36:53 +0100386inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
387 const std::string &data_file,
388 DataLayout file_layout = DataLayout::NCHW)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000389{
390 if(path.empty())
391 {
392 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
393 }
394 else
395 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100396 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000397 }
398}
399
Jenkins52ba29e2018-08-29 15:32:11 +0000400/** Generates appropriate input accessor according to the specified graph parameters
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000401 *
Jenkins52ba29e2018-08-29 15:32:11 +0000402 * @param[in] graph_parameters Graph parameters
403 * @param[in] preprocessor (Optional) Preproccessor object
404 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000405 *
406 * @return An appropriate tensor accessor
407 */
Jenkins52ba29e2018-08-29 15:32:11 +0000408inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
409 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
410 bool bgr = true)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000411{
Jenkins52ba29e2018-08-29 15:32:11 +0000412 if(!graph_parameters.validation_file.empty())
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000413 {
Jenkins52ba29e2018-08-29 15:32:11 +0000414 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
415 graph_parameters.validation_path,
416 std::move(preprocessor),
417 bgr,
418 graph_parameters.validation_range_start,
419 graph_parameters.validation_range_end);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000420 }
421 else
422 {
Jenkins52ba29e2018-08-29 15:32:11 +0000423 const std::string &image_file = graph_parameters.image;
424 const std::string &image_file_lower = lower_string(image_file);
425 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Jenkinsb3a371b2018-05-23 11:36:53 +0100426 {
Jenkins52ba29e2018-08-29 15:32:11 +0000427 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
428 }
429 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
430 || arm_compute::utility::endswith(image_file_lower, ".jpg")
431 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
432 {
433 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Jenkinsb3a371b2018-05-23 11:36:53 +0100434 }
435 else
436 {
Jenkins52ba29e2018-08-29 15:32:11 +0000437 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Jenkinsb3a371b2018-05-23 11:36:53 +0100438 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000439 }
440}
441
Jenkins52ba29e2018-08-29 15:32:11 +0000442/** Generates appropriate output accessor according to the specified graph parameters
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000443 *
Jenkins52ba29e2018-08-29 15:32:11 +0000444 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
445 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000446 *
Jenkins52ba29e2018-08-29 15:32:11 +0000447 * @param[in] graph_parameters Graph parameters
448 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
449 * @param[in] is_validation (Optional) Validation flag (default = false)
450 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000451 *
452 * @return An appropriate tensor accessor
453 */
Jenkins52ba29e2018-08-29 15:32:11 +0000454inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
455 size_t top_n = 5,
456 bool is_validation = false,
457 std::ostream &output_stream = std::cout)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000458{
Jenkins52ba29e2018-08-29 15:32:11 +0000459 if(!graph_parameters.validation_file.empty())
460 {
461 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
462 output_stream,
463 graph_parameters.validation_range_start,
464 graph_parameters.validation_range_end);
465 }
466 else if(graph_parameters.labels.empty())
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000467 {
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000468 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000469 }
470 else
471 {
Jenkins52ba29e2018-08-29 15:32:11 +0000472 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000473 }
474}
Jenkinsb3a371b2018-05-23 11:36:53 +0100475/** Generates appropriate npy output accessor according to the specified npy_path
476 *
477 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
478 *
479 * @param[in] npy_path Path to npy file.
480 * @param[in] shape Shape of the numpy tensor data.
481 * @param[in] data_type DataType of the numpy tensor data.
482 * @param[out] output_stream (Optional) Output stream
483 *
484 * @return An appropriate tensor accessor
485 */
486inline 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)
487{
488 if(npy_path.empty())
489 {
490 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
491 }
492 else
493 {
494 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream);
495 }
496}
497
Jenkins52ba29e2018-08-29 15:32:11 +0000498/** Permutes a given tensor shape given the input and output data layout
499 *
500 * @param[in] tensor_shape Tensor shape to permute
501 * @param[in] in_data_layout Input tensor shape data layout
502 * @param[in] out_data_layout Output tensor shape data layout
503 *
504 * @return Permuted tensor shape
505 */
506inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
507{
508 if(in_data_layout != out_data_layout)
509 {
510 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
511 arm_compute::permute(tensor_shape, perm_vec);
512 }
513 return tensor_shape;
514}
515
Jenkinsb3a371b2018-05-23 11:36:53 +0100516/** Utility function to return the TargetHint
517 *
518 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
519 *
520 * @return the TargetHint
521 */
522inline graph::Target set_target_hint(int target)
523{
524 ARM_COMPUTE_ERROR_ON_MSG(target > 3, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner), 3 (GLES)");
525 if((target == 1 || target == 2))
526 {
527 return graph::Target::CL;
528 }
529 else if(target == 3)
530 {
531 return graph::Target::GC;
532 }
533 else
534 {
535 return graph::Target::NEON;
536 }
537}
Kaizenbf8b01d2017-10-12 14:26:51 +0100538} // namespace graph_utils
Kaizen8938bd32017-09-28 14:38:23 +0100539} // namespace arm_compute
540
541#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */