blob: b48300bd01e5d072c11b8566c5e5bb06da57c277 [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 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +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;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 bool _bgr;
78 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 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 NumPyAccessor(std::string npy_path,
159 TensorShape shape,
160 DataType data_type,
161 DataLayout data_layout = DataLayout::NCHW,
162 std::ostream &output_stream = std::cout);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100163 /** Allow instances of this class to be move constructed */
164 NumPyAccessor(NumPyAccessor &&) = default;
165 /** Prevent instances of this class from being copied (As this class contains pointers) */
166 NumPyAccessor(const NumPyAccessor &) = delete;
167 /** Prevent instances of this class from being copied (As this class contains pointers) */
168 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
169
170 // Inherited methods overriden:
171 bool access_tensor(ITensor &tensor) override;
172
173private:
174 template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000175 void access_numpy_tensor(ITensor &tensor, T tolerance);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100176
177 Tensor _npy_tensor;
178 const std::string _filename;
179 std::ostream &_output_stream;
180};
181
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100182/** SaveNumPy accessor class */
183class SaveNumPyAccessor final : public graph::ITensorAccessor
184{
185public:
186 /** Constructor
187 *
188 * @param[in] npy_name Npy file name.
189 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
190 */
191 SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false);
192 /** Allow instances of this class to be move constructed */
193 SaveNumPyAccessor(SaveNumPyAccessor &&) = default;
194 /** Prevent instances of this class from being copied (As this class contains pointers) */
195 SaveNumPyAccessor(const SaveNumPyAccessor &) = delete;
196 /** Prevent instances of this class from being copied (As this class contains pointers) */
197 SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete;
198
199 // Inherited methods overriden:
200 bool access_tensor(ITensor &tensor) override;
201
202private:
203 const std::string _npy_name;
204 const bool _is_fortran;
205};
206
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000207/** Print accessor class
208 * @note The print accessor will print only when asserts are enabled.
209 * */
210class PrintAccessor final : public graph::ITensorAccessor
211{
212public:
213 /** Constructor
214 *
215 * @param[out] output_stream (Optional) Output stream
216 * @param[in] io_fmt (Optional) Format information
217 */
218 PrintAccessor(std::ostream &output_stream = std::cout, IOFormatInfo io_fmt = IOFormatInfo());
219 /** Allow instances of this class to be move constructed */
220 PrintAccessor(PrintAccessor &&) = default;
221 /** Prevent instances of this class from being copied (As this class contains pointers) */
222 PrintAccessor(const PrintAccessor &) = delete;
223 /** Prevent instances of this class from being copied (As this class contains pointers) */
224 PrintAccessor &operator=(const PrintAccessor &) = delete;
225
226 // Inherited methods overriden:
227 bool access_tensor(ITensor &tensor) override;
228
229private:
230 std::ostream &_output_stream;
231 IOFormatInfo _io_fmt;
232};
233
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100234/** Image accessor class */
235class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100236{
237public:
238 /** Constructor
239 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100240 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100241 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100242 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100243 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100244 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100245 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100246 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100247
248 // Inherited methods overriden:
249 bool access_tensor(ITensor &tensor) override;
250
251private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100252 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100253 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000254 const bool _bgr;
255 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100256};
257
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100258/** Input Accessor used for network validation */
259class ValidationInputAccessor final : public graph::ITensorAccessor
260{
261public:
262 /** Constructor
263 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100264 * @param[in] image_list File containing all the images to validate
265 * @param[in] images_path Path to images.
266 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
267 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
268 * @param[in] start (Optional) Start range
269 * @param[in] end (Optional) End range
270 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100271 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100272 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100273 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100274 ValidationInputAccessor(const std::string &image_list,
275 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100276 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
277 bool bgr = true,
278 unsigned int start = 0,
279 unsigned int end = 0,
280 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100281
282 // Inherited methods overriden:
283 bool access_tensor(ITensor &tensor) override;
284
285private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100286 std::string _path;
287 std::vector<std::string> _images;
288 std::unique_ptr<IPreprocessor> _preprocessor;
289 bool _bgr;
290 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100291 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100292};
293
Georgios Pinitas7908de72018-06-27 12:34:20 +0100294/** Output Accessor used for network validation */
295class ValidationOutputAccessor final : public graph::ITensorAccessor
296{
297public:
298 /** Default Constructor
299 *
300 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100301 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
302 * @param[in] start (Optional) Start range
303 * @param[in] end (Optional) End range
304 *
305 * @note Range is defined as [start, end]
306 */
307 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100308 std::ostream &output_stream = std::cout,
309 unsigned int start = 0,
310 unsigned int end = 0);
311 /** Reset accessor state */
312 void reset();
313
314 // Inherited methods overriden:
315 bool access_tensor(ITensor &tensor) override;
316
317private:
318 /** Access predictions of the tensor
319 *
320 * @tparam T Tensor elements type
321 *
322 * @param[in] tensor Tensor to read the predictions from
323 */
324 template <typename T>
325 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100326 /** Aggregates the results of a sample
327 *
328 * @param[in] res Vector containing the results of a graph
329 * @param[in,out] positive_samples Positive samples to be updated
330 * @param[in] top_n Top n accuracy to measure
331 * @param[in] correct_label Correct label of the current sample
332 */
333 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
334 /** Reports top N accuracy
335 *
336 * @param[in] top_n Top N accuracy that is being reported
337 * @param[in] total_samples Total number of samples
338 * @param[in] positive_samples Positive samples
339 */
340 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100341
342private:
343 std::vector<int> _results;
344 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100345 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100346 size_t _positive_samples_top1;
347 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100348};
349
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000350/** Detection output accessor class */
351class DetectionOutputAccessor final : public graph::ITensorAccessor
352{
353public:
354 /** Constructor
355 *
356 * @param[in] labels_path Path to labels text file.
357 * @param[in] imgs_tensor_shapes Network input images tensor shapes.
358 * @param[out] output_stream (Optional) Output stream
359 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360 DetectionOutputAccessor(const std::string &labels_path,
361 std::vector<TensorShape> &imgs_tensor_shapes,
362 std::ostream &output_stream = std::cout);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000363 /** Allow instances of this class to be move constructed */
364 DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
365 /** Prevent instances of this class from being copied (As this class contains pointers) */
366 DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
367 /** Prevent instances of this class from being copied (As this class contains pointers) */
368 DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
369
370 // Inherited methods overriden:
371 bool access_tensor(ITensor &tensor) override;
372
373private:
374 template <typename T>
375 void access_predictions_tensor(ITensor &tensor);
376
377 std::vector<std::string> _labels;
378 std::vector<TensorShape> _tensor_shapes;
379 std::ostream &_output_stream;
380};
381
Gian Marco44ec2e72017-10-19 14:13:38 +0100382/** Result accessor class */
383class TopNPredictionsAccessor final : public graph::ITensorAccessor
384{
385public:
386 /** Constructor
387 *
388 * @param[in] labels_path Path to labels text file.
389 * @param[in] top_n (Optional) Number of output classes to print
390 * @param[out] output_stream (Optional) Output stream
391 */
392 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
393 /** Allow instances of this class to be move constructed */
394 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
395 /** Prevent instances of this class from being copied (As this class contains pointers) */
396 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
397 /** Prevent instances of this class from being copied (As this class contains pointers) */
398 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
399
400 // Inherited methods overriden:
401 bool access_tensor(ITensor &tensor) override;
402
403private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000404 template <typename T>
405 void access_predictions_tensor(ITensor &tensor);
406
Gian Marco44ec2e72017-10-19 14:13:38 +0100407 std::vector<std::string> _labels;
408 std::ostream &_output_stream;
409 size_t _top_n;
410};
411
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100412/** Random accessor class */
413class RandomAccessor final : public graph::ITensorAccessor
414{
415public:
416 /** Constructor
417 *
418 * @param[in] lower Lower bound value.
419 * @param[in] upper Upper bound value.
420 * @param[in] seed (Optional) Seed used to initialise the random number generator.
421 */
422 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
423 /** Allows instances to move constructed */
424 RandomAccessor(RandomAccessor &&) = default;
425
426 // Inherited methods overriden:
427 bool access_tensor(ITensor &tensor) override;
428
429private:
430 template <typename T, typename D>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100431 void fill(ITensor &tensor, D &&distribution);
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100432 PixelValue _lower;
433 PixelValue _upper;
434 std::random_device::result_type _seed;
435};
436
Anthony Barbier2a07e182017-08-04 18:20:27 +0100437/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100438class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100439{
440public:
441 /** Default Constructor
442 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100443 * @param[in] filename Binary file name
444 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100445 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100446 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100447 /** Allows instances to move constructed */
448 NumPyBinLoader(NumPyBinLoader &&) = default;
449
450 // Inherited methods overriden:
451 bool access_tensor(ITensor &tensor) override;
452
453private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100454 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100455 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100456 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100457};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000458
Georgios Pinitas652bde52018-01-10 15:33:28 +0000459/** Generates appropriate random accessor
460 *
461 * @param[in] lower Lower random values bound
462 * @param[in] upper Upper random values bound
463 * @param[in] seed Random generator seed
464 *
465 * @return A ramdom accessor
466 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100467inline std::unique_ptr<graph::ITensorAccessor>
468get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
Georgios Pinitas652bde52018-01-10 15:33:28 +0000469{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000470 return std::make_unique<RandomAccessor>(lower, upper, seed);
Georgios Pinitas652bde52018-01-10 15:33:28 +0000471}
472
Isabella Gottardia4c61882017-11-03 12:11:55 +0000473/** Generates appropriate weights accessor according to the specified path
474 *
475 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
476 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100477 * @param[in] path Path to the data files
478 * @param[in] data_file Relative path to the data files from path
479 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000480 *
481 * @return An appropriate tensor accessor
482 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100483inline std::unique_ptr<graph::ITensorAccessor>
484get_weights_accessor(const std::string &path, const std::string &data_file, DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000485{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100486 if (path.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000487 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000488 return std::make_unique<DummyAccessor>();
Isabella Gottardia4c61882017-11-03 12:11:55 +0000489 }
490 else
491 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000492 return std::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000493 }
494}
495
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100496/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000497 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100498 * @param[in] graph_parameters Graph parameters
499 * @param[in] preprocessor (Optional) Preproccessor object
500 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000501 *
502 * @return An appropriate tensor accessor
503 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100504inline std::unique_ptr<graph::ITensorAccessor>
505get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
506 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
507 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000508{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100509 if (!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000510 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100511 return std::make_unique<ValidationInputAccessor>(
512 graph_parameters.validation_file, graph_parameters.validation_path, std::move(preprocessor), bgr,
513 graph_parameters.validation_range_start, graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000514 }
515 else
516 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100517 const std::string &image_file = graph_parameters.image;
518 const std::string &image_file_lower = lower_string(image_file);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100519 if (arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100520 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000521 return std::make_unique<NumPyBinLoader>(image_file, graph_parameters.data_layout);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100522 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100523 else if (arm_compute::utility::endswith(image_file_lower, ".jpeg") ||
524 arm_compute::utility::endswith(image_file_lower, ".jpg") ||
525 arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100526 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000527 return std::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100528 }
529 else
530 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000531 return std::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100532 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000533 }
534}
535
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100536/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000537 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100538 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
539 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000540 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100541 * @param[in] graph_parameters Graph parameters
542 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
543 * @param[in] is_validation (Optional) Validation flag (default = false)
544 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000545 *
546 * @return An appropriate tensor accessor
547 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100548inline std::unique_ptr<graph::ITensorAccessor>
549get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
550 size_t top_n = 5,
551 bool is_validation = false,
552 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000553{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100554 ARM_COMPUTE_UNUSED(is_validation);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100555 if (!graph_parameters.validation_file.empty())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100556 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100557 return std::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, output_stream,
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000558 graph_parameters.validation_range_start,
559 graph_parameters.validation_range_end);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100560 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100561 else if (graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000562 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000563 return std::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000564 }
565 else
566 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000567 return std::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000568 }
569}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000570/** Generates appropriate output accessor according to the specified graph parameters
571 *
572 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
573 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
574 *
575 * @param[in] graph_parameters Graph parameters
576 * @param[in] tensor_shapes Network input images tensor shapes.
577 * @param[in] is_validation (Optional) Validation flag (default = false)
578 * @param[out] output_stream (Optional) Output stream (default = std::cout)
579 *
580 * @return An appropriate tensor accessor
581 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100582inline std::unique_ptr<graph::ITensorAccessor>
583get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
584 std::vector<TensorShape> tensor_shapes,
585 bool is_validation = false,
586 std::ostream &output_stream = std::cout)
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000587{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100588 ARM_COMPUTE_UNUSED(is_validation);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100589 if (!graph_parameters.validation_file.empty())
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000590 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100591 return std::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file, output_stream,
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000592 graph_parameters.validation_range_start,
593 graph_parameters.validation_range_end);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000594 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100595 else if (graph_parameters.labels.empty())
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000596 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000597 return std::make_unique<DummyAccessor>(0);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000598 }
599 else
600 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000601 return std::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000602 }
603}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100604/** Generates appropriate npy output accessor according to the specified npy_path
605 *
606 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
607 *
608 * @param[in] npy_path Path to npy file.
609 * @param[in] shape Shape of the numpy tensor data.
610 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000611 * @param[in] data_layout DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100612 * @param[out] output_stream (Optional) Output stream
613 *
614 * @return An appropriate tensor accessor
615 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100616inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path,
617 TensorShape shape,
618 DataType data_type,
619 DataLayout data_layout = DataLayout::NCHW,
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000620 std::ostream &output_stream = std::cout)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100621{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100622 if (npy_path.empty())
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100623 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000624 return std::make_unique<DummyAccessor>(0);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100625 }
626 else
627 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000628 return std::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100629 }
630}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000631
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100632/** Generates appropriate npy output accessor according to the specified npy_path
633 *
634 * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor
635 *
636 * @param[in] npy_name Npy filename.
637 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
638 *
639 * @return An appropriate tensor accessor
640 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100641inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name,
642 const bool is_fortran = false)
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100643{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100644 if (npy_name.empty())
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100645 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000646 return std::make_unique<DummyAccessor>(0);
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100647 }
648 else
649 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000650 return std::make_unique<SaveNumPyAccessor>(npy_name, is_fortran);
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100651 }
652}
653
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000654/** Generates print tensor accessor
655 *
656 * @param[out] output_stream (Optional) Output stream
657 *
658 * @return A print tensor accessor
659 */
660inline std::unique_ptr<graph::ITensorAccessor> get_print_output_accessor(std::ostream &output_stream = std::cout)
661{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000662 return std::make_unique<PrintAccessor>(output_stream);
Isabella Gottardicd4e9ab2019-11-05 17:50:27 +0000663}
664
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100665/** Permutes a given tensor shape given the input and output data layout
666 *
667 * @param[in] tensor_shape Tensor shape to permute
668 * @param[in] in_data_layout Input tensor shape data layout
669 * @param[in] out_data_layout Output tensor shape data layout
670 *
671 * @return Permuted tensor shape
672 */
673inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
674{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100675 if (in_data_layout != out_data_layout)
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100676 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100677 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW)
678 ? arm_compute::PermutationVector(2U, 0U, 1U)
679 : arm_compute::PermutationVector(1U, 2U, 0U);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100680 arm_compute::permute(tensor_shape, perm_vec);
681 }
682 return tensor_shape;
683}
684
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000685/** Utility function to return the TargetHint
686 *
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000687 * @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 +0000688 *
689 * @return the TargetHint
690 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100691inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000692{
Michele Di Giorgio40efd532021-03-18 17:32:00 +0000693 ARM_COMPUTE_ERROR_ON_MSG(target > 2, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner)");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100694 if ((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000695 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100696 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000697 }
698 else
699 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100700 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000701 }
702}
Michalis Spyroue4720822017-10-02 17:44:52 +0100703} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100704} // namespace arm_compute
705
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000706#endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */