blob: d6bae3ea3ff517f595e7444d409739fad1dabbbe [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Isabella Gottardi0ae5de92019-03-14 10:32:11 +00002 * Copyright (c) 2017-2019 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 */
Georgios Pinitasf52cd782019-03-25 14:06:14 +000024#ifndef __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__
25#define __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__
Anthony Barbier2a07e182017-08-04 18:20:27 +010026
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 *
Georgios Pinitasb54c6442019-04-03 13:18:14 +010065 * @param[in] mean Mean array in RGB ordering
66 * @param[in] bgr Boolean specifying if the preprocessing should assume BGR format
67 * @param[in] scale Scale value
Georgios Pinitas140fdc72018-02-16 11:42:38 +000068 */
Georgios Pinitasb54c6442019-04-03 13:18:14 +010069 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true, float scale = 1.f);
Georgios Pinitas140fdc72018-02-16 11:42:38 +000070 void preprocess(ITensor &tensor) override;
71
72private:
giuros01351bd132019-08-23 14:27:30 +010073 template <typename T>
74 void preprocess_typed(ITensor &tensor);
75
Georgios Pinitas140fdc72018-02-16 11:42:38 +000076 std::array<float, 3> _mean;
Pablo Tello32521432018-11-15 14:43:10 +000077 bool _bgr;
Georgios Pinitasb54c6442019-04-03 13:18:14 +010078 float _scale;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000079};
80
81/** TF preproccessor */
82class TFPreproccessor : public IPreprocessor
83{
84public:
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010085 /** Constructor
86 *
87 * @param[in] min_range Min normalization range. (Defaults to -1.f)
88 * @param[in] max_range Max normalization range. (Defaults to 1.f)
89 */
90 TFPreproccessor(float min_range = -1.f, float max_range = 1.f);
91
92 // Inherited overriden methods
Georgios Pinitas140fdc72018-02-16 11:42:38 +000093 void preprocess(ITensor &tensor) override;
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010094
95private:
giuros01351bd132019-08-23 14:27:30 +010096 template <typename T>
97 void preprocess_typed(ITensor &tensor);
98
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010099 float _min_range;
100 float _max_range;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000101};
102
Anthony Barbier2a07e182017-08-04 18:20:27 +0100103/** PPM writer class */
104class PPMWriter : public graph::ITensorAccessor
105{
106public:
107 /** Constructor
108 *
109 * @param[in] name PPM file name
110 * @param[in] maximum Maximum elements to access
111 */
112 PPMWriter(std::string name, unsigned int maximum = 1);
113 /** Allows instances to move constructed */
114 PPMWriter(PPMWriter &&) = default;
115
116 // Inherited methods overriden:
117 bool access_tensor(ITensor &tensor) override;
118
119private:
120 const std::string _name;
121 unsigned int _iterator;
122 unsigned int _maximum;
123};
124
125/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100126class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100127{
128public:
129 /** Constructor
130 *
131 * @param[in] maximum Maximum elements to write
132 */
133 DummyAccessor(unsigned int maximum = 1);
134 /** Allows instances to move constructed */
135 DummyAccessor(DummyAccessor &&) = default;
136
137 // Inherited methods overriden:
138 bool access_tensor(ITensor &tensor) override;
139
140private:
141 unsigned int _iterator;
142 unsigned int _maximum;
143};
144
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100145/** NumPy accessor class */
146class NumPyAccessor final : public graph::ITensorAccessor
147{
148public:
149 /** Constructor
150 *
151 * @param[in] npy_path Path to npy file.
152 * @param[in] shape Shape of the numpy tensor data.
153 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000154 * @param[in] data_layout (Optional) DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100155 * @param[out] output_stream (Optional) Output stream
156 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000157 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW, std::ostream &output_stream = std::cout);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100158 /** Allow instances of this class to be move constructed */
159 NumPyAccessor(NumPyAccessor &&) = default;
160 /** Prevent instances of this class from being copied (As this class contains pointers) */
161 NumPyAccessor(const NumPyAccessor &) = delete;
162 /** Prevent instances of this class from being copied (As this class contains pointers) */
163 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
164
165 // Inherited methods overriden:
166 bool access_tensor(ITensor &tensor) override;
167
168private:
169 template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000170 void access_numpy_tensor(ITensor &tensor, T tolerance);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100171
172 Tensor _npy_tensor;
173 const std::string _filename;
174 std::ostream &_output_stream;
175};
176
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100177/** SaveNumPy accessor class */
178class SaveNumPyAccessor final : public graph::ITensorAccessor
179{
180public:
181 /** Constructor
182 *
183 * @param[in] npy_name Npy file name.
184 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
185 */
186 SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false);
187 /** Allow instances of this class to be move constructed */
188 SaveNumPyAccessor(SaveNumPyAccessor &&) = default;
189 /** Prevent instances of this class from being copied (As this class contains pointers) */
190 SaveNumPyAccessor(const SaveNumPyAccessor &) = delete;
191 /** Prevent instances of this class from being copied (As this class contains pointers) */
192 SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete;
193
194 // Inherited methods overriden:
195 bool access_tensor(ITensor &tensor) override;
196
197private:
198 const std::string _npy_name;
199 const bool _is_fortran;
200};
201
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000202/** Print accessor class
203 * @note The print accessor will print only when asserts are enabled.
204 * */
205class PrintAccessor final : public graph::ITensorAccessor
206{
207public:
208 /** Constructor
209 *
210 * @param[out] output_stream (Optional) Output stream
211 * @param[in] io_fmt (Optional) Format information
212 */
213 PrintAccessor(std::ostream &output_stream = std::cout, IOFormatInfo io_fmt = IOFormatInfo());
214 /** Allow instances of this class to be move constructed */
215 PrintAccessor(PrintAccessor &&) = default;
216 /** Prevent instances of this class from being copied (As this class contains pointers) */
217 PrintAccessor(const PrintAccessor &) = delete;
218 /** Prevent instances of this class from being copied (As this class contains pointers) */
219 PrintAccessor &operator=(const PrintAccessor &) = delete;
220
221 // Inherited methods overriden:
222 bool access_tensor(ITensor &tensor) override;
223
224private:
225 std::ostream &_output_stream;
226 IOFormatInfo _io_fmt;
227};
228
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100229/** Image accessor class */
230class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100231{
232public:
233 /** Constructor
234 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100235 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100236 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100237 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100238 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100239 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100240 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100241 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100242
243 // Inherited methods overriden:
244 bool access_tensor(ITensor &tensor) override;
245
246private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100247 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100248 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000249 const bool _bgr;
250 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100251};
252
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100253/** Input Accessor used for network validation */
254class ValidationInputAccessor final : public graph::ITensorAccessor
255{
256public:
257 /** Constructor
258 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100259 * @param[in] image_list File containing all the images to validate
260 * @param[in] images_path Path to images.
261 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
262 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
263 * @param[in] start (Optional) Start range
264 * @param[in] end (Optional) End range
265 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100266 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100267 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100268 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100269 ValidationInputAccessor(const std::string &image_list,
270 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100271 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
272 bool bgr = true,
273 unsigned int start = 0,
274 unsigned int end = 0,
275 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100276
277 // Inherited methods overriden:
278 bool access_tensor(ITensor &tensor) override;
279
280private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100281 std::string _path;
282 std::vector<std::string> _images;
283 std::unique_ptr<IPreprocessor> _preprocessor;
284 bool _bgr;
285 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100286 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100287};
288
Georgios Pinitas7908de72018-06-27 12:34:20 +0100289/** Output Accessor used for network validation */
290class ValidationOutputAccessor final : public graph::ITensorAccessor
291{
292public:
293 /** Default Constructor
294 *
295 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100296 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
297 * @param[in] start (Optional) Start range
298 * @param[in] end (Optional) End range
299 *
300 * @note Range is defined as [start, end]
301 */
302 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100303 std::ostream &output_stream = std::cout,
304 unsigned int start = 0,
305 unsigned int end = 0);
306 /** Reset accessor state */
307 void reset();
308
309 // Inherited methods overriden:
310 bool access_tensor(ITensor &tensor) override;
311
312private:
313 /** Access predictions of the tensor
314 *
315 * @tparam T Tensor elements type
316 *
317 * @param[in] tensor Tensor to read the predictions from
318 */
319 template <typename T>
320 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100321 /** Aggregates the results of a sample
322 *
323 * @param[in] res Vector containing the results of a graph
324 * @param[in,out] positive_samples Positive samples to be updated
325 * @param[in] top_n Top n accuracy to measure
326 * @param[in] correct_label Correct label of the current sample
327 */
328 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
329 /** Reports top N accuracy
330 *
331 * @param[in] top_n Top N accuracy that is being reported
332 * @param[in] total_samples Total number of samples
333 * @param[in] positive_samples Positive samples
334 */
335 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100336
337private:
338 std::vector<int> _results;
339 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100340 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100341 size_t _positive_samples_top1;
342 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100343};
344
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000345/** Detection output accessor class */
346class DetectionOutputAccessor final : public graph::ITensorAccessor
347{
348public:
349 /** Constructor
350 *
351 * @param[in] labels_path Path to labels text file.
352 * @param[in] imgs_tensor_shapes Network input images tensor shapes.
353 * @param[out] output_stream (Optional) Output stream
354 */
355 DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout);
356 /** Allow instances of this class to be move constructed */
357 DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
358 /** Prevent instances of this class from being copied (As this class contains pointers) */
359 DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
360 /** Prevent instances of this class from being copied (As this class contains pointers) */
361 DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
362
363 // Inherited methods overriden:
364 bool access_tensor(ITensor &tensor) override;
365
366private:
367 template <typename T>
368 void access_predictions_tensor(ITensor &tensor);
369
370 std::vector<std::string> _labels;
371 std::vector<TensorShape> _tensor_shapes;
372 std::ostream &_output_stream;
373};
374
Gian Marco44ec2e72017-10-19 14:13:38 +0100375/** Result accessor class */
376class TopNPredictionsAccessor final : public graph::ITensorAccessor
377{
378public:
379 /** Constructor
380 *
381 * @param[in] labels_path Path to labels text file.
382 * @param[in] top_n (Optional) Number of output classes to print
383 * @param[out] output_stream (Optional) Output stream
384 */
385 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
386 /** Allow instances of this class to be move constructed */
387 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
388 /** Prevent instances of this class from being copied (As this class contains pointers) */
389 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
390 /** Prevent instances of this class from being copied (As this class contains pointers) */
391 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
392
393 // Inherited methods overriden:
394 bool access_tensor(ITensor &tensor) override;
395
396private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000397 template <typename T>
398 void access_predictions_tensor(ITensor &tensor);
399
Gian Marco44ec2e72017-10-19 14:13:38 +0100400 std::vector<std::string> _labels;
401 std::ostream &_output_stream;
402 size_t _top_n;
403};
404
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100405/** Random accessor class */
406class RandomAccessor final : public graph::ITensorAccessor
407{
408public:
409 /** Constructor
410 *
411 * @param[in] lower Lower bound value.
412 * @param[in] upper Upper bound value.
413 * @param[in] seed (Optional) Seed used to initialise the random number generator.
414 */
415 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
416 /** Allows instances to move constructed */
417 RandomAccessor(RandomAccessor &&) = default;
418
419 // Inherited methods overriden:
420 bool access_tensor(ITensor &tensor) override;
421
422private:
423 template <typename T, typename D>
424 void fill(ITensor &tensor, D &&distribution);
425 PixelValue _lower;
426 PixelValue _upper;
427 std::random_device::result_type _seed;
428};
429
Anthony Barbier2a07e182017-08-04 18:20:27 +0100430/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100431class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100432{
433public:
434 /** Default Constructor
435 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100436 * @param[in] filename Binary file name
437 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100438 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100439 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100440 /** Allows instances to move constructed */
441 NumPyBinLoader(NumPyBinLoader &&) = default;
442
443 // Inherited methods overriden:
444 bool access_tensor(ITensor &tensor) override;
445
446private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100447 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100448 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100449 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100450};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000451
Georgios Pinitas652bde52018-01-10 15:33:28 +0000452/** Generates appropriate random accessor
453 *
454 * @param[in] lower Lower random values bound
455 * @param[in] upper Upper random values bound
456 * @param[in] seed Random generator seed
457 *
458 * @return A ramdom accessor
459 */
460inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
461{
462 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
463}
464
Isabella Gottardia4c61882017-11-03 12:11:55 +0000465/** Generates appropriate weights accessor according to the specified path
466 *
467 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
468 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100469 * @param[in] path Path to the data files
470 * @param[in] data_file Relative path to the data files from path
471 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000472 *
473 * @return An appropriate tensor accessor
474 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100475inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
476 const std::string &data_file,
477 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000478{
479 if(path.empty())
480 {
481 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
482 }
483 else
484 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100485 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000486 }
487}
488
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100489/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000490 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100491 * @param[in] graph_parameters Graph parameters
492 * @param[in] preprocessor (Optional) Preproccessor object
493 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000494 *
495 * @return An appropriate tensor accessor
496 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100497inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
498 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
499 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000500{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100501 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000502 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100503 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
504 graph_parameters.validation_path,
505 std::move(preprocessor),
506 bgr,
507 graph_parameters.validation_range_start,
508 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000509 }
510 else
511 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100512 const std::string &image_file = graph_parameters.image;
513 const std::string &image_file_lower = lower_string(image_file);
514 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100515 {
Manuel Bottini32527952019-11-05 16:55:57 +0000516 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file, graph_parameters.data_layout);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100517 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100518 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
519 || arm_compute::utility::endswith(image_file_lower, ".jpg")
520 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100521 {
522 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100523 }
524 else
525 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100526 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100527 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000528 }
529}
530
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100531/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000532 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100533 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
534 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000535 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100536 * @param[in] graph_parameters Graph parameters
537 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
538 * @param[in] is_validation (Optional) Validation flag (default = false)
539 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000540 *
541 * @return An appropriate tensor accessor
542 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100543inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
544 size_t top_n = 5,
545 bool is_validation = false,
546 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000547{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100548 ARM_COMPUTE_UNUSED(is_validation);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100549 if(!graph_parameters.validation_file.empty())
550 {
551 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
552 output_stream,
553 graph_parameters.validation_range_start,
554 graph_parameters.validation_range_end);
555 }
556 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000557 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000558 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000559 }
560 else
561 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100562 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000563 }
564}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000565/** Generates appropriate output accessor according to the specified graph parameters
566 *
567 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
568 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
569 *
570 * @param[in] graph_parameters Graph parameters
571 * @param[in] tensor_shapes Network input images tensor shapes.
572 * @param[in] is_validation (Optional) Validation flag (default = false)
573 * @param[out] output_stream (Optional) Output stream (default = std::cout)
574 *
575 * @return An appropriate tensor accessor
576 */
577inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
578 std::vector<TensorShape> tensor_shapes,
579 bool is_validation = false,
580 std::ostream &output_stream = std::cout)
581{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100582 ARM_COMPUTE_UNUSED(is_validation);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000583 if(!graph_parameters.validation_file.empty())
584 {
585 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
586 output_stream,
587 graph_parameters.validation_range_start,
588 graph_parameters.validation_range_end);
589 }
590 else if(graph_parameters.labels.empty())
591 {
592 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
593 }
594 else
595 {
596 return arm_compute::support::cpp14::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
597 }
598}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100599/** Generates appropriate npy output accessor according to the specified npy_path
600 *
601 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
602 *
603 * @param[in] npy_path Path to npy file.
604 * @param[in] shape Shape of the numpy tensor data.
605 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000606 * @param[in] data_layout DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100607 * @param[out] output_stream (Optional) Output stream
608 *
609 * @return An appropriate tensor accessor
610 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000611inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW,
612 std::ostream &output_stream = std::cout)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100613{
614 if(npy_path.empty())
615 {
616 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
617 }
618 else
619 {
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000620 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100621 }
622}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000623
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100624/** Generates appropriate npy output accessor according to the specified npy_path
625 *
626 * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor
627 *
628 * @param[in] npy_name Npy filename.
629 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
630 *
631 * @return An appropriate tensor accessor
632 */
633inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, const bool is_fortran = false)
634{
635 if(npy_name.empty())
636 {
637 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
638 }
639 else
640 {
641 return arm_compute::support::cpp14::make_unique<SaveNumPyAccessor>(npy_name, is_fortran);
642 }
643}
644
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000645/** Generates print tensor accessor
646 *
647 * @param[out] output_stream (Optional) Output stream
648 *
649 * @return A print tensor accessor
650 */
651inline std::unique_ptr<graph::ITensorAccessor> get_print_output_accessor(std::ostream &output_stream = std::cout)
652{
653 return arm_compute::support::cpp14::make_unique<PrintAccessor>(output_stream);
654}
655
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100656/** Permutes a given tensor shape given the input and output data layout
657 *
658 * @param[in] tensor_shape Tensor shape to permute
659 * @param[in] in_data_layout Input tensor shape data layout
660 * @param[in] out_data_layout Output tensor shape data layout
661 *
662 * @return Permuted tensor shape
663 */
664inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
665{
666 if(in_data_layout != out_data_layout)
667 {
668 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
669 arm_compute::permute(tensor_shape, perm_vec);
670 }
671 return tensor_shape;
672}
673
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000674/** Utility function to return the TargetHint
675 *
676 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
677 *
678 * @return the TargetHint
679 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100680inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000681{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100682 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 +0100683 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000684 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100685 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000686 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100687 else if(target == 3)
688 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100689 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100690 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000691 else
692 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100693 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000694 }
695}
Michalis Spyroue4720822017-10-02 17:44:52 +0100696} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100697} // namespace arm_compute
698
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000699#endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */