blob: a6d670d761908b92814d9a6906907bd9c9571edf [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:
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010080 /** Constructor
81 *
82 * @param[in] min_range Min normalization range. (Defaults to -1.f)
83 * @param[in] max_range Max normalization range. (Defaults to 1.f)
84 */
85 TFPreproccessor(float min_range = -1.f, float max_range = 1.f);
86
87 // Inherited overriden methods
Georgios Pinitas140fdc72018-02-16 11:42:38 +000088 void preprocess(ITensor &tensor) override;
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010089
90private:
91 float _min_range;
92 float _max_range;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000093};
94
Anthony Barbier2a07e182017-08-04 18:20:27 +010095/** PPM writer class */
96class PPMWriter : public graph::ITensorAccessor
97{
98public:
99 /** Constructor
100 *
101 * @param[in] name PPM file name
102 * @param[in] maximum Maximum elements to access
103 */
104 PPMWriter(std::string name, unsigned int maximum = 1);
105 /** Allows instances to move constructed */
106 PPMWriter(PPMWriter &&) = default;
107
108 // Inherited methods overriden:
109 bool access_tensor(ITensor &tensor) override;
110
111private:
112 const std::string _name;
113 unsigned int _iterator;
114 unsigned int _maximum;
115};
116
117/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100118class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100119{
120public:
121 /** Constructor
122 *
123 * @param[in] maximum Maximum elements to write
124 */
125 DummyAccessor(unsigned int maximum = 1);
126 /** Allows instances to move constructed */
127 DummyAccessor(DummyAccessor &&) = default;
128
129 // Inherited methods overriden:
130 bool access_tensor(ITensor &tensor) override;
131
132private:
133 unsigned int _iterator;
134 unsigned int _maximum;
135};
136
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100137/** NumPy accessor class */
138class NumPyAccessor final : public graph::ITensorAccessor
139{
140public:
141 /** Constructor
142 *
143 * @param[in] npy_path Path to npy file.
144 * @param[in] shape Shape of the numpy tensor data.
145 * @param[in] data_type DataType of the numpy tensor data.
146 * @param[out] output_stream (Optional) Output stream
147 */
148 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout);
149 /** Allow instances of this class to be move constructed */
150 NumPyAccessor(NumPyAccessor &&) = default;
151 /** Prevent instances of this class from being copied (As this class contains pointers) */
152 NumPyAccessor(const NumPyAccessor &) = delete;
153 /** Prevent instances of this class from being copied (As this class contains pointers) */
154 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
155
156 // Inherited methods overriden:
157 bool access_tensor(ITensor &tensor) override;
158
159private:
160 template <typename T>
161 void access_numpy_tensor(ITensor &tensor);
162
163 Tensor _npy_tensor;
164 const std::string _filename;
165 std::ostream &_output_stream;
166};
167
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100168/** Image accessor class */
169class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100170{
171public:
172 /** Constructor
173 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100174 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100175 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100176 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100177 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100178 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100179 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100180 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100181
182 // Inherited methods overriden:
183 bool access_tensor(ITensor &tensor) override;
184
185private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100186 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100187 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000188 const bool _bgr;
189 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100190};
191
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100192/** Input Accessor used for network validation */
193class ValidationInputAccessor final : public graph::ITensorAccessor
194{
195public:
196 /** Constructor
197 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100198 * @param[in] image_list File containing all the images to validate
199 * @param[in] images_path Path to images.
200 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
201 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
202 * @param[in] start (Optional) Start range
203 * @param[in] end (Optional) End range
204 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100205 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100206 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100207 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100208 ValidationInputAccessor(const std::string &image_list,
209 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100210 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
211 bool bgr = true,
212 unsigned int start = 0,
213 unsigned int end = 0,
214 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100215
216 // Inherited methods overriden:
217 bool access_tensor(ITensor &tensor) override;
218
219private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100220 std::string _path;
221 std::vector<std::string> _images;
222 std::unique_ptr<IPreprocessor> _preprocessor;
223 bool _bgr;
224 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100225 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100226};
227
Georgios Pinitas7908de72018-06-27 12:34:20 +0100228/** Output Accessor used for network validation */
229class ValidationOutputAccessor final : public graph::ITensorAccessor
230{
231public:
232 /** Default Constructor
233 *
234 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100235 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
236 * @param[in] start (Optional) Start range
237 * @param[in] end (Optional) End range
238 *
239 * @note Range is defined as [start, end]
240 */
241 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100242 std::ostream &output_stream = std::cout,
243 unsigned int start = 0,
244 unsigned int end = 0);
245 /** Reset accessor state */
246 void reset();
247
248 // Inherited methods overriden:
249 bool access_tensor(ITensor &tensor) override;
250
251private:
252 /** Access predictions of the tensor
253 *
254 * @tparam T Tensor elements type
255 *
256 * @param[in] tensor Tensor to read the predictions from
257 */
258 template <typename T>
259 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100260 /** Aggregates the results of a sample
261 *
262 * @param[in] res Vector containing the results of a graph
263 * @param[in,out] positive_samples Positive samples to be updated
264 * @param[in] top_n Top n accuracy to measure
265 * @param[in] correct_label Correct label of the current sample
266 */
267 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
268 /** Reports top N accuracy
269 *
270 * @param[in] top_n Top N accuracy that is being reported
271 * @param[in] total_samples Total number of samples
272 * @param[in] positive_samples Positive samples
273 */
274 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100275
276private:
277 std::vector<int> _results;
278 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100279 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100280 size_t _positive_samples_top1;
281 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100282};
283
Gian Marco44ec2e72017-10-19 14:13:38 +0100284/** Result accessor class */
285class TopNPredictionsAccessor final : public graph::ITensorAccessor
286{
287public:
288 /** Constructor
289 *
290 * @param[in] labels_path Path to labels text file.
291 * @param[in] top_n (Optional) Number of output classes to print
292 * @param[out] output_stream (Optional) Output stream
293 */
294 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
295 /** Allow instances of this class to be move constructed */
296 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
297 /** Prevent instances of this class from being copied (As this class contains pointers) */
298 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
299 /** Prevent instances of this class from being copied (As this class contains pointers) */
300 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
301
302 // Inherited methods overriden:
303 bool access_tensor(ITensor &tensor) override;
304
305private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000306 template <typename T>
307 void access_predictions_tensor(ITensor &tensor);
308
Gian Marco44ec2e72017-10-19 14:13:38 +0100309 std::vector<std::string> _labels;
310 std::ostream &_output_stream;
311 size_t _top_n;
312};
313
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100314/** Random accessor class */
315class RandomAccessor final : public graph::ITensorAccessor
316{
317public:
318 /** Constructor
319 *
320 * @param[in] lower Lower bound value.
321 * @param[in] upper Upper bound value.
322 * @param[in] seed (Optional) Seed used to initialise the random number generator.
323 */
324 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
325 /** Allows instances to move constructed */
326 RandomAccessor(RandomAccessor &&) = default;
327
328 // Inherited methods overriden:
329 bool access_tensor(ITensor &tensor) override;
330
331private:
332 template <typename T, typename D>
333 void fill(ITensor &tensor, D &&distribution);
334 PixelValue _lower;
335 PixelValue _upper;
336 std::random_device::result_type _seed;
337};
338
Anthony Barbier2a07e182017-08-04 18:20:27 +0100339/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100340class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100341{
342public:
343 /** Default Constructor
344 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100345 * @param[in] filename Binary file name
346 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100347 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100348 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100349 /** Allows instances to move constructed */
350 NumPyBinLoader(NumPyBinLoader &&) = default;
351
352 // Inherited methods overriden:
353 bool access_tensor(ITensor &tensor) override;
354
355private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100356 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100357 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100358 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100359};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000360
Georgios Pinitas652bde52018-01-10 15:33:28 +0000361/** Generates appropriate random accessor
362 *
363 * @param[in] lower Lower random values bound
364 * @param[in] upper Upper random values bound
365 * @param[in] seed Random generator seed
366 *
367 * @return A ramdom accessor
368 */
369inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
370{
371 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
372}
373
Isabella Gottardia4c61882017-11-03 12:11:55 +0000374/** Generates appropriate weights accessor according to the specified path
375 *
376 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
377 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100378 * @param[in] path Path to the data files
379 * @param[in] data_file Relative path to the data files from path
380 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000381 *
382 * @return An appropriate tensor accessor
383 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100384inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
385 const std::string &data_file,
386 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000387{
388 if(path.empty())
389 {
390 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
391 }
392 else
393 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100394 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000395 }
396}
397
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100398/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000399 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100400 * @param[in] graph_parameters Graph parameters
401 * @param[in] preprocessor (Optional) Preproccessor object
402 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000403 *
404 * @return An appropriate tensor accessor
405 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100406inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
407 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
408 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000409{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100410 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000411 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100412 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
413 graph_parameters.validation_path,
414 std::move(preprocessor),
415 bgr,
416 graph_parameters.validation_range_start,
417 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000418 }
419 else
420 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100421 const std::string &image_file = graph_parameters.image;
422 const std::string &image_file_lower = lower_string(image_file);
423 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100424 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100425 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
426 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100427 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
428 || arm_compute::utility::endswith(image_file_lower, ".jpg")
429 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100430 {
431 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100432 }
433 else
434 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100435 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100436 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000437 }
438}
439
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100440/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000441 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100442 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
443 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000444 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100445 * @param[in] graph_parameters Graph parameters
446 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
447 * @param[in] is_validation (Optional) Validation flag (default = false)
448 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000449 *
450 * @return An appropriate tensor accessor
451 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100452inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
453 size_t top_n = 5,
454 bool is_validation = false,
455 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000456{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100457 if(!graph_parameters.validation_file.empty())
458 {
459 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
460 output_stream,
461 graph_parameters.validation_range_start,
462 graph_parameters.validation_range_end);
463 }
464 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000465 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000466 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000467 }
468 else
469 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100470 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000471 }
472}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100473/** Generates appropriate npy output accessor according to the specified npy_path
474 *
475 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
476 *
477 * @param[in] npy_path Path to npy file.
478 * @param[in] shape Shape of the numpy tensor data.
479 * @param[in] data_type DataType of the numpy tensor data.
480 * @param[out] output_stream (Optional) Output stream
481 *
482 * @return An appropriate tensor accessor
483 */
484inline 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)
485{
486 if(npy_path.empty())
487 {
488 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
489 }
490 else
491 {
492 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream);
493 }
494}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000495
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100496/** Permutes a given tensor shape given the input and output data layout
497 *
498 * @param[in] tensor_shape Tensor shape to permute
499 * @param[in] in_data_layout Input tensor shape data layout
500 * @param[in] out_data_layout Output tensor shape data layout
501 *
502 * @return Permuted tensor shape
503 */
504inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
505{
506 if(in_data_layout != out_data_layout)
507 {
508 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
509 arm_compute::permute(tensor_shape, perm_vec);
510 }
511 return tensor_shape;
512}
513
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000514/** Utility function to return the TargetHint
515 *
516 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
517 *
518 * @return the TargetHint
519 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100520inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000521{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100522 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 +0100523 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000524 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100525 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000526 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100527 else if(target == 3)
528 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100529 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100530 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000531 else
532 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100533 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000534 }
535}
Michalis Spyroue4720822017-10-02 17:44:52 +0100536} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100537} // namespace arm_compute
538
539#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */