blob: 80055acc0f66f6e4623e08a4870b4f46206f0252 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2017-2021 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:
Giorgio Arena9c67d382021-08-20 15:24:03 +0100138 bool access_tensor_data() override;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100139 bool access_tensor(ITensor &tensor) override;
140
141private:
142 unsigned int _iterator;
143 unsigned int _maximum;
144};
145
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100146/** NumPy accessor class */
147class NumPyAccessor final : public graph::ITensorAccessor
148{
149public:
150 /** Constructor
151 *
152 * @param[in] npy_path Path to npy file.
153 * @param[in] shape Shape of the numpy tensor data.
154 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000155 * @param[in] data_layout (Optional) DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100156 * @param[out] output_stream (Optional) Output stream
157 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000158 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 +0100159 /** Allow instances of this class to be move constructed */
160 NumPyAccessor(NumPyAccessor &&) = default;
161 /** Prevent instances of this class from being copied (As this class contains pointers) */
162 NumPyAccessor(const NumPyAccessor &) = delete;
163 /** Prevent instances of this class from being copied (As this class contains pointers) */
164 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
165
166 // Inherited methods overriden:
167 bool access_tensor(ITensor &tensor) override;
168
169private:
170 template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000171 void access_numpy_tensor(ITensor &tensor, T tolerance);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100172
173 Tensor _npy_tensor;
174 const std::string _filename;
175 std::ostream &_output_stream;
176};
177
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100178/** SaveNumPy accessor class */
179class SaveNumPyAccessor final : public graph::ITensorAccessor
180{
181public:
182 /** Constructor
183 *
184 * @param[in] npy_name Npy file name.
185 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
186 */
187 SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false);
188 /** Allow instances of this class to be move constructed */
189 SaveNumPyAccessor(SaveNumPyAccessor &&) = default;
190 /** Prevent instances of this class from being copied (As this class contains pointers) */
191 SaveNumPyAccessor(const SaveNumPyAccessor &) = delete;
192 /** Prevent instances of this class from being copied (As this class contains pointers) */
193 SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete;
194
195 // Inherited methods overriden:
196 bool access_tensor(ITensor &tensor) override;
197
198private:
199 const std::string _npy_name;
200 const bool _is_fortran;
201};
202
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000203/** Print accessor class
204 * @note The print accessor will print only when asserts are enabled.
205 * */
206class PrintAccessor final : public graph::ITensorAccessor
207{
208public:
209 /** Constructor
210 *
211 * @param[out] output_stream (Optional) Output stream
212 * @param[in] io_fmt (Optional) Format information
213 */
214 PrintAccessor(std::ostream &output_stream = std::cout, IOFormatInfo io_fmt = IOFormatInfo());
215 /** Allow instances of this class to be move constructed */
216 PrintAccessor(PrintAccessor &&) = default;
217 /** Prevent instances of this class from being copied (As this class contains pointers) */
218 PrintAccessor(const PrintAccessor &) = delete;
219 /** Prevent instances of this class from being copied (As this class contains pointers) */
220 PrintAccessor &operator=(const PrintAccessor &) = delete;
221
222 // Inherited methods overriden:
223 bool access_tensor(ITensor &tensor) override;
224
225private:
226 std::ostream &_output_stream;
227 IOFormatInfo _io_fmt;
228};
229
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100230/** Image accessor class */
231class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100232{
233public:
234 /** Constructor
235 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100236 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100237 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100238 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100239 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100240 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100241 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100242 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100243
244 // Inherited methods overriden:
245 bool access_tensor(ITensor &tensor) override;
246
247private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100248 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100249 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000250 const bool _bgr;
251 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100252};
253
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100254/** Input Accessor used for network validation */
255class ValidationInputAccessor final : public graph::ITensorAccessor
256{
257public:
258 /** Constructor
259 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100260 * @param[in] image_list File containing all the images to validate
261 * @param[in] images_path Path to images.
262 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
263 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
264 * @param[in] start (Optional) Start range
265 * @param[in] end (Optional) End range
266 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100267 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100268 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100269 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100270 ValidationInputAccessor(const std::string &image_list,
271 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100272 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
273 bool bgr = true,
274 unsigned int start = 0,
275 unsigned int end = 0,
276 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100277
278 // Inherited methods overriden:
279 bool access_tensor(ITensor &tensor) override;
280
281private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100282 std::string _path;
283 std::vector<std::string> _images;
284 std::unique_ptr<IPreprocessor> _preprocessor;
285 bool _bgr;
286 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100287 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100288};
289
Georgios Pinitas7908de72018-06-27 12:34:20 +0100290/** Output Accessor used for network validation */
291class ValidationOutputAccessor final : public graph::ITensorAccessor
292{
293public:
294 /** Default Constructor
295 *
296 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100297 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
298 * @param[in] start (Optional) Start range
299 * @param[in] end (Optional) End range
300 *
301 * @note Range is defined as [start, end]
302 */
303 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100304 std::ostream &output_stream = std::cout,
305 unsigned int start = 0,
306 unsigned int end = 0);
307 /** Reset accessor state */
308 void reset();
309
310 // Inherited methods overriden:
311 bool access_tensor(ITensor &tensor) override;
312
313private:
314 /** Access predictions of the tensor
315 *
316 * @tparam T Tensor elements type
317 *
318 * @param[in] tensor Tensor to read the predictions from
319 */
320 template <typename T>
321 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100322 /** Aggregates the results of a sample
323 *
324 * @param[in] res Vector containing the results of a graph
325 * @param[in,out] positive_samples Positive samples to be updated
326 * @param[in] top_n Top n accuracy to measure
327 * @param[in] correct_label Correct label of the current sample
328 */
329 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
330 /** Reports top N accuracy
331 *
332 * @param[in] top_n Top N accuracy that is being reported
333 * @param[in] total_samples Total number of samples
334 * @param[in] positive_samples Positive samples
335 */
336 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100337
338private:
339 std::vector<int> _results;
340 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100341 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100342 size_t _positive_samples_top1;
343 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100344};
345
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000346/** Detection output accessor class */
347class DetectionOutputAccessor final : public graph::ITensorAccessor
348{
349public:
350 /** Constructor
351 *
352 * @param[in] labels_path Path to labels text file.
353 * @param[in] imgs_tensor_shapes Network input images tensor shapes.
354 * @param[out] output_stream (Optional) Output stream
355 */
356 DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout);
357 /** Allow instances of this class to be move constructed */
358 DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
359 /** Prevent instances of this class from being copied (As this class contains pointers) */
360 DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
361 /** Prevent instances of this class from being copied (As this class contains pointers) */
362 DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
363
364 // Inherited methods overriden:
365 bool access_tensor(ITensor &tensor) override;
366
367private:
368 template <typename T>
369 void access_predictions_tensor(ITensor &tensor);
370
371 std::vector<std::string> _labels;
372 std::vector<TensorShape> _tensor_shapes;
373 std::ostream &_output_stream;
374};
375
Gian Marco44ec2e72017-10-19 14:13:38 +0100376/** Result accessor class */
377class TopNPredictionsAccessor final : public graph::ITensorAccessor
378{
379public:
380 /** Constructor
381 *
382 * @param[in] labels_path Path to labels text file.
383 * @param[in] top_n (Optional) Number of output classes to print
384 * @param[out] output_stream (Optional) Output stream
385 */
386 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
387 /** Allow instances of this class to be move constructed */
388 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
389 /** Prevent instances of this class from being copied (As this class contains pointers) */
390 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
391 /** Prevent instances of this class from being copied (As this class contains pointers) */
392 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
393
394 // Inherited methods overriden:
395 bool access_tensor(ITensor &tensor) override;
396
397private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000398 template <typename T>
399 void access_predictions_tensor(ITensor &tensor);
400
Gian Marco44ec2e72017-10-19 14:13:38 +0100401 std::vector<std::string> _labels;
402 std::ostream &_output_stream;
403 size_t _top_n;
404};
405
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100406/** Random accessor class */
407class RandomAccessor final : public graph::ITensorAccessor
408{
409public:
410 /** Constructor
411 *
412 * @param[in] lower Lower bound value.
413 * @param[in] upper Upper bound value.
414 * @param[in] seed (Optional) Seed used to initialise the random number generator.
415 */
416 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
417 /** Allows instances to move constructed */
418 RandomAccessor(RandomAccessor &&) = default;
419
420 // Inherited methods overriden:
421 bool access_tensor(ITensor &tensor) override;
422
423private:
424 template <typename T, typename D>
425 void fill(ITensor &tensor, D &&distribution);
426 PixelValue _lower;
427 PixelValue _upper;
428 std::random_device::result_type _seed;
429};
430
Anthony Barbier2a07e182017-08-04 18:20:27 +0100431/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100432class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100433{
434public:
435 /** Default Constructor
436 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100437 * @param[in] filename Binary file name
438 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100439 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100440 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100441 /** Allows instances to move constructed */
442 NumPyBinLoader(NumPyBinLoader &&) = default;
443
444 // Inherited methods overriden:
445 bool access_tensor(ITensor &tensor) override;
446
447private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100448 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100449 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100450 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100451};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000452
Georgios Pinitas652bde52018-01-10 15:33:28 +0000453/** Generates appropriate random accessor
454 *
455 * @param[in] lower Lower random values bound
456 * @param[in] upper Upper random values bound
457 * @param[in] seed Random generator seed
458 *
459 * @return A ramdom accessor
460 */
461inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
462{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000463 return std::make_unique<RandomAccessor>(lower, upper, seed);
Georgios Pinitas652bde52018-01-10 15:33:28 +0000464}
465
Isabella Gottardia4c61882017-11-03 12:11:55 +0000466/** Generates appropriate weights accessor according to the specified path
467 *
468 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
469 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100470 * @param[in] path Path to the data files
471 * @param[in] data_file Relative path to the data files from path
472 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000473 *
474 * @return An appropriate tensor accessor
475 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100476inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
477 const std::string &data_file,
478 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000479{
480 if(path.empty())
481 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000482 return std::make_unique<DummyAccessor>();
Isabella Gottardia4c61882017-11-03 12:11:55 +0000483 }
484 else
485 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000486 return std::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000487 }
488}
489
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100490/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000491 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100492 * @param[in] graph_parameters Graph parameters
493 * @param[in] preprocessor (Optional) Preproccessor object
494 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000495 *
496 * @return An appropriate tensor accessor
497 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100498inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
499 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
500 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000501{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100502 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000503 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000504 return std::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
505 graph_parameters.validation_path,
506 std::move(preprocessor),
507 bgr,
508 graph_parameters.validation_range_start,
509 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000510 }
511 else
512 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100513 const std::string &image_file = graph_parameters.image;
514 const std::string &image_file_lower = lower_string(image_file);
515 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100516 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000517 return std::make_unique<NumPyBinLoader>(image_file, graph_parameters.data_layout);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100518 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100519 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
520 || arm_compute::utility::endswith(image_file_lower, ".jpg")
521 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100522 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000523 return std::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100524 }
525 else
526 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000527 return std::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100528 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000529 }
530}
531
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100532/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000533 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100534 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
535 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000536 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100537 * @param[in] graph_parameters Graph parameters
538 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
539 * @param[in] is_validation (Optional) Validation flag (default = false)
540 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000541 *
542 * @return An appropriate tensor accessor
543 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100544inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
545 size_t top_n = 5,
546 bool is_validation = false,
547 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000548{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100549 ARM_COMPUTE_UNUSED(is_validation);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100550 if(!graph_parameters.validation_file.empty())
551 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000552 return std::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
553 output_stream,
554 graph_parameters.validation_range_start,
555 graph_parameters.validation_range_end);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100556 }
557 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000558 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000559 return std::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000560 }
561 else
562 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000563 return std::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000564 }
565}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000566/** Generates appropriate output accessor according to the specified graph parameters
567 *
568 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
569 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
570 *
571 * @param[in] graph_parameters Graph parameters
572 * @param[in] tensor_shapes Network input images tensor shapes.
573 * @param[in] is_validation (Optional) Validation flag (default = false)
574 * @param[out] output_stream (Optional) Output stream (default = std::cout)
575 *
576 * @return An appropriate tensor accessor
577 */
578inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
579 std::vector<TensorShape> tensor_shapes,
580 bool is_validation = false,
581 std::ostream &output_stream = std::cout)
582{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100583 ARM_COMPUTE_UNUSED(is_validation);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000584 if(!graph_parameters.validation_file.empty())
585 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000586 return std::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
587 output_stream,
588 graph_parameters.validation_range_start,
589 graph_parameters.validation_range_end);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000590 }
591 else if(graph_parameters.labels.empty())
592 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000593 return std::make_unique<DummyAccessor>(0);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000594 }
595 else
596 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000597 return std::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000598 }
599}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100600/** Generates appropriate npy output accessor according to the specified npy_path
601 *
602 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
603 *
604 * @param[in] npy_path Path to npy file.
605 * @param[in] shape Shape of the numpy tensor data.
606 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000607 * @param[in] data_layout DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100608 * @param[out] output_stream (Optional) Output stream
609 *
610 * @return An appropriate tensor accessor
611 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000612inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW,
613 std::ostream &output_stream = std::cout)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100614{
615 if(npy_path.empty())
616 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000617 return std::make_unique<DummyAccessor>(0);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100618 }
619 else
620 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000621 return std::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100622 }
623}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000624
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100625/** Generates appropriate npy output accessor according to the specified npy_path
626 *
627 * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor
628 *
629 * @param[in] npy_name Npy filename.
630 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
631 *
632 * @return An appropriate tensor accessor
633 */
634inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, const bool is_fortran = false)
635{
636 if(npy_name.empty())
637 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000638 return std::make_unique<DummyAccessor>(0);
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100639 }
640 else
641 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000642 return std::make_unique<SaveNumPyAccessor>(npy_name, is_fortran);
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100643 }
644}
645
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000646/** Generates print tensor accessor
647 *
648 * @param[out] output_stream (Optional) Output stream
649 *
650 * @return A print tensor accessor
651 */
652inline std::unique_ptr<graph::ITensorAccessor> get_print_output_accessor(std::ostream &output_stream = std::cout)
653{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000654 return std::make_unique<PrintAccessor>(output_stream);
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000655}
656
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100657/** Permutes a given tensor shape given the input and output data layout
658 *
659 * @param[in] tensor_shape Tensor shape to permute
660 * @param[in] in_data_layout Input tensor shape data layout
661 * @param[in] out_data_layout Output tensor shape data layout
662 *
663 * @return Permuted tensor shape
664 */
665inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
666{
667 if(in_data_layout != out_data_layout)
668 {
669 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
670 arm_compute::permute(tensor_shape, perm_vec);
671 }
672 return tensor_shape;
673}
674
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000675/** Utility function to return the TargetHint
676 *
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000677 * @param[in] target Integer value which expresses the selected target. Must be 0 for Arm® Neon™ or 1 for OpenCL or 2 (OpenCL with Tuner)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000678 *
679 * @return the TargetHint
680 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100681inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000682{
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000683 ARM_COMPUTE_ERROR_ON_MSG(target > 2, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner)");
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100684 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000685 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100686 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000687 }
688 else
689 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100690 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000691 }
692}
Michalis Spyroue4720822017-10-02 17:44:52 +0100693} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100694} // namespace arm_compute
695
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000696#endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */