blob: d7f24afdd87b418bc9f4e2b9c7d495dbf124a58b [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
Pablo Tello32521432018-11-15 14:43:10 +000066 * @param scale Scale value
Georgios Pinitas140fdc72018-02-16 11:42:38 +000067 * @param bgr Boolean specifying if the preprocessing should assume BGR format
68 */
Pablo Tello32521432018-11-15 14:43:10 +000069 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, float scale = 1.f, bool bgr = true);
Georgios Pinitas140fdc72018-02-16 11:42:38 +000070 void preprocess(ITensor &tensor) override;
71
72private:
73 std::array<float, 3> _mean;
Pablo Tello32521432018-11-15 14:43:10 +000074 float _scale;
75 bool _bgr;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000076};
77
78/** TF preproccessor */
79class TFPreproccessor : public IPreprocessor
80{
81public:
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010082 /** 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
Georgios Pinitas140fdc72018-02-16 11:42:38 +000090 void preprocess(ITensor &tensor) override;
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010091
92private:
93 float _min_range;
94 float _max_range;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000095};
96
Anthony Barbier2a07e182017-08-04 18:20:27 +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 */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100120class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +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
Isabella Gottardi88d5b222018-04-06 12:24:55 +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
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100170/** Image accessor class */
171class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100172{
173public:
174 /** Constructor
175 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100176 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100177 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100178 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100179 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100180 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100181 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100182 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100183
184 // Inherited methods overriden:
185 bool access_tensor(ITensor &tensor) override;
186
187private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100188 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100189 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000190 const bool _bgr;
191 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100192};
193
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100194/** Input Accessor used for network validation */
195class ValidationInputAccessor final : public graph::ITensorAccessor
196{
197public:
198 /** Constructor
199 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100200 * @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
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100207 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100208 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100209 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100210 ValidationInputAccessor(const std::string &image_list,
211 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100212 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);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100217
218 // Inherited methods overriden:
219 bool access_tensor(ITensor &tensor) override;
220
221private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100222 std::string _path;
223 std::vector<std::string> _images;
224 std::unique_ptr<IPreprocessor> _preprocessor;
225 bool _bgr;
226 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100227 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100228};
229
Georgios Pinitas7908de72018-06-27 12:34:20 +0100230/** 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
Georgios Pinitas7908de72018-06-27 12:34:20 +0100237 * @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,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100244 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);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100262 /** 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);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100277
278private:
279 std::vector<int> _results;
280 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100281 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100282 size_t _positive_samples_top1;
283 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100284};
285
Gian Marco44ec2e72017-10-19 14:13:38 +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:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000308 template <typename T>
309 void access_predictions_tensor(ITensor &tensor);
310
Gian Marco44ec2e72017-10-19 14:13:38 +0100311 std::vector<std::string> _labels;
312 std::ostream &_output_stream;
313 size_t _top_n;
314};
315
Michalis Spyrou53b405f2017-09-27 15:55:31 +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
Anthony Barbier2a07e182017-08-04 18:20:27 +0100341/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100342class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100343{
344public:
345 /** Default Constructor
346 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100347 * @param[in] filename Binary file name
348 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100349 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100350 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100351 /** Allows instances to move constructed */
352 NumPyBinLoader(NumPyBinLoader &&) = default;
353
354 // Inherited methods overriden:
355 bool access_tensor(ITensor &tensor) override;
356
357private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100358 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100359 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100360 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100361};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000362
Georgios Pinitas652bde52018-01-10 15:33:28 +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
Isabella Gottardia4c61882017-11-03 12:11:55 +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 *
Georgios Pinitascac13b12018-04-27 19:07:19 +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
Isabella Gottardia4c61882017-11-03 12:11:55 +0000383 *
384 * @return An appropriate tensor accessor
385 */
Georgios Pinitascac13b12018-04-27 19:07:19 +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)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000389{
390 if(path.empty())
391 {
392 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
393 }
394 else
395 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100396 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000397 }
398}
399
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100400/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000401 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100402 * @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)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000405 *
406 * @return An appropriate tensor accessor
407 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100408inline 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)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000411{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100412 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000413 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100414 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);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000420 }
421 else
422 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100423 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"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100426 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100427 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
428 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100429 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"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100432 {
433 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100434 }
435 else
436 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100437 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100438 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000439 }
440}
441
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100442/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000443 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100444 * @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
Isabella Gottardia4c61882017-11-03 12:11:55 +0000446 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100447 * @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)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000451 *
452 * @return An appropriate tensor accessor
453 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100454inline 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)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000458{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100459 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())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000467 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000468 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000469 }
470 else
471 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100472 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000473 }
474}
Isabella Gottardi88d5b222018-04-06 12:24:55 +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}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000497
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100498/** 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
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000516/** 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100522inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000523{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100524 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 +0100525 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000526 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100527 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000528 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100529 else if(target == 3)
530 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100531 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100532 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000533 else
534 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100535 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000536 }
537}
Michalis Spyroue4720822017-10-02 17:44:52 +0100538} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100539} // namespace arm_compute
540
541#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */