blob: 3417135f17863800e06abc690eb24c43e5e5a75e [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:
73 std::array<float, 3> _mean;
Pablo Tello32521432018-11-15 14:43:10 +000074 bool _bgr;
Georgios Pinitasb54c6442019-04-03 13:18:14 +010075 float _scale;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000076};
77
78/** TF preproccessor */
79class TFPreproccessor : public IPreprocessor
80{
81public:
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010082 /** Constructor
83 *
84 * @param[in] min_range Min normalization range. (Defaults to -1.f)
85 * @param[in] max_range Max normalization range. (Defaults to 1.f)
86 */
87 TFPreproccessor(float min_range = -1.f, float max_range = 1.f);
88
89 // Inherited overriden methods
Georgios Pinitas140fdc72018-02-16 11:42:38 +000090 void preprocess(ITensor &tensor) override;
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010091
92private:
93 float _min_range;
94 float _max_range;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000095};
96
Anthony Barbier2a07e182017-08-04 18:20:27 +010097/** PPM writer class */
98class PPMWriter : public graph::ITensorAccessor
99{
100public:
101 /** Constructor
102 *
103 * @param[in] name PPM file name
104 * @param[in] maximum Maximum elements to access
105 */
106 PPMWriter(std::string name, unsigned int maximum = 1);
107 /** Allows instances to move constructed */
108 PPMWriter(PPMWriter &&) = default;
109
110 // Inherited methods overriden:
111 bool access_tensor(ITensor &tensor) override;
112
113private:
114 const std::string _name;
115 unsigned int _iterator;
116 unsigned int _maximum;
117};
118
119/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100120class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121{
122public:
123 /** Constructor
124 *
125 * @param[in] maximum Maximum elements to write
126 */
127 DummyAccessor(unsigned int maximum = 1);
128 /** Allows instances to move constructed */
129 DummyAccessor(DummyAccessor &&) = default;
130
131 // Inherited methods overriden:
132 bool access_tensor(ITensor &tensor) override;
133
134private:
135 unsigned int _iterator;
136 unsigned int _maximum;
137};
138
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100139/** NumPy accessor class */
140class NumPyAccessor final : public graph::ITensorAccessor
141{
142public:
143 /** Constructor
144 *
145 * @param[in] npy_path Path to npy file.
146 * @param[in] shape Shape of the numpy tensor data.
147 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000148 * @param[in] data_layout (Optional) DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100149 * @param[out] output_stream (Optional) Output stream
150 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000151 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 +0100152 /** Allow instances of this class to be move constructed */
153 NumPyAccessor(NumPyAccessor &&) = default;
154 /** Prevent instances of this class from being copied (As this class contains pointers) */
155 NumPyAccessor(const NumPyAccessor &) = delete;
156 /** Prevent instances of this class from being copied (As this class contains pointers) */
157 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
158
159 // Inherited methods overriden:
160 bool access_tensor(ITensor &tensor) override;
161
162private:
163 template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000164 void access_numpy_tensor(ITensor &tensor, T tolerance);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100165
166 Tensor _npy_tensor;
167 const std::string _filename;
168 std::ostream &_output_stream;
169};
170
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100171/** SaveNumPy accessor class */
172class SaveNumPyAccessor final : public graph::ITensorAccessor
173{
174public:
175 /** Constructor
176 *
177 * @param[in] npy_name Npy file name.
178 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
179 */
180 SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false);
181 /** Allow instances of this class to be move constructed */
182 SaveNumPyAccessor(SaveNumPyAccessor &&) = default;
183 /** Prevent instances of this class from being copied (As this class contains pointers) */
184 SaveNumPyAccessor(const SaveNumPyAccessor &) = delete;
185 /** Prevent instances of this class from being copied (As this class contains pointers) */
186 SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete;
187
188 // Inherited methods overriden:
189 bool access_tensor(ITensor &tensor) override;
190
191private:
192 const std::string _npy_name;
193 const bool _is_fortran;
194};
195
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100196/** Image accessor class */
197class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100198{
199public:
200 /** Constructor
201 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100202 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100203 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100204 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100205 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100206 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100207 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100208 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100209
210 // Inherited methods overriden:
211 bool access_tensor(ITensor &tensor) override;
212
213private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100214 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100215 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000216 const bool _bgr;
217 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100218};
219
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100220/** Input Accessor used for network validation */
221class ValidationInputAccessor final : public graph::ITensorAccessor
222{
223public:
224 /** Constructor
225 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100226 * @param[in] image_list File containing all the images to validate
227 * @param[in] images_path Path to images.
228 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
229 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
230 * @param[in] start (Optional) Start range
231 * @param[in] end (Optional) End range
232 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100233 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100234 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100235 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100236 ValidationInputAccessor(const std::string &image_list,
237 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100238 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
239 bool bgr = true,
240 unsigned int start = 0,
241 unsigned int end = 0,
242 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100243
244 // Inherited methods overriden:
245 bool access_tensor(ITensor &tensor) override;
246
247private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100248 std::string _path;
249 std::vector<std::string> _images;
250 std::unique_ptr<IPreprocessor> _preprocessor;
251 bool _bgr;
252 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100253 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100254};
255
Georgios Pinitas7908de72018-06-27 12:34:20 +0100256/** Output Accessor used for network validation */
257class ValidationOutputAccessor final : public graph::ITensorAccessor
258{
259public:
260 /** Default Constructor
261 *
262 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100263 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
264 * @param[in] start (Optional) Start range
265 * @param[in] end (Optional) End range
266 *
267 * @note Range is defined as [start, end]
268 */
269 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100270 std::ostream &output_stream = std::cout,
271 unsigned int start = 0,
272 unsigned int end = 0);
273 /** Reset accessor state */
274 void reset();
275
276 // Inherited methods overriden:
277 bool access_tensor(ITensor &tensor) override;
278
279private:
280 /** Access predictions of the tensor
281 *
282 * @tparam T Tensor elements type
283 *
284 * @param[in] tensor Tensor to read the predictions from
285 */
286 template <typename T>
287 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100288 /** Aggregates the results of a sample
289 *
290 * @param[in] res Vector containing the results of a graph
291 * @param[in,out] positive_samples Positive samples to be updated
292 * @param[in] top_n Top n accuracy to measure
293 * @param[in] correct_label Correct label of the current sample
294 */
295 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
296 /** Reports top N accuracy
297 *
298 * @param[in] top_n Top N accuracy that is being reported
299 * @param[in] total_samples Total number of samples
300 * @param[in] positive_samples Positive samples
301 */
302 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100303
304private:
305 std::vector<int> _results;
306 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100307 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100308 size_t _positive_samples_top1;
309 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100310};
311
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000312/** Detection output accessor class */
313class DetectionOutputAccessor final : public graph::ITensorAccessor
314{
315public:
316 /** Constructor
317 *
318 * @param[in] labels_path Path to labels text file.
319 * @param[in] imgs_tensor_shapes Network input images tensor shapes.
320 * @param[out] output_stream (Optional) Output stream
321 */
322 DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout);
323 /** Allow instances of this class to be move constructed */
324 DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
325 /** Prevent instances of this class from being copied (As this class contains pointers) */
326 DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
327 /** Prevent instances of this class from being copied (As this class contains pointers) */
328 DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
329
330 // Inherited methods overriden:
331 bool access_tensor(ITensor &tensor) override;
332
333private:
334 template <typename T>
335 void access_predictions_tensor(ITensor &tensor);
336
337 std::vector<std::string> _labels;
338 std::vector<TensorShape> _tensor_shapes;
339 std::ostream &_output_stream;
340};
341
Gian Marco44ec2e72017-10-19 14:13:38 +0100342/** Result accessor class */
343class TopNPredictionsAccessor final : public graph::ITensorAccessor
344{
345public:
346 /** Constructor
347 *
348 * @param[in] labels_path Path to labels text file.
349 * @param[in] top_n (Optional) Number of output classes to print
350 * @param[out] output_stream (Optional) Output stream
351 */
352 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
353 /** Allow instances of this class to be move constructed */
354 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
355 /** Prevent instances of this class from being copied (As this class contains pointers) */
356 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
357 /** Prevent instances of this class from being copied (As this class contains pointers) */
358 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
359
360 // Inherited methods overriden:
361 bool access_tensor(ITensor &tensor) override;
362
363private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000364 template <typename T>
365 void access_predictions_tensor(ITensor &tensor);
366
Gian Marco44ec2e72017-10-19 14:13:38 +0100367 std::vector<std::string> _labels;
368 std::ostream &_output_stream;
369 size_t _top_n;
370};
371
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100372/** Random accessor class */
373class RandomAccessor final : public graph::ITensorAccessor
374{
375public:
376 /** Constructor
377 *
378 * @param[in] lower Lower bound value.
379 * @param[in] upper Upper bound value.
380 * @param[in] seed (Optional) Seed used to initialise the random number generator.
381 */
382 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
383 /** Allows instances to move constructed */
384 RandomAccessor(RandomAccessor &&) = default;
385
386 // Inherited methods overriden:
387 bool access_tensor(ITensor &tensor) override;
388
389private:
390 template <typename T, typename D>
391 void fill(ITensor &tensor, D &&distribution);
392 PixelValue _lower;
393 PixelValue _upper;
394 std::random_device::result_type _seed;
395};
396
Anthony Barbier2a07e182017-08-04 18:20:27 +0100397/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100398class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100399{
400public:
401 /** Default Constructor
402 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100403 * @param[in] filename Binary file name
404 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100405 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100406 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100407 /** Allows instances to move constructed */
408 NumPyBinLoader(NumPyBinLoader &&) = default;
409
410 // Inherited methods overriden:
411 bool access_tensor(ITensor &tensor) override;
412
413private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100414 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100415 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100416 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100417};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000418
Georgios Pinitas652bde52018-01-10 15:33:28 +0000419/** Generates appropriate random accessor
420 *
421 * @param[in] lower Lower random values bound
422 * @param[in] upper Upper random values bound
423 * @param[in] seed Random generator seed
424 *
425 * @return A ramdom accessor
426 */
427inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
428{
429 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
430}
431
Isabella Gottardia4c61882017-11-03 12:11:55 +0000432/** Generates appropriate weights accessor according to the specified path
433 *
434 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
435 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100436 * @param[in] path Path to the data files
437 * @param[in] data_file Relative path to the data files from path
438 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000439 *
440 * @return An appropriate tensor accessor
441 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100442inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
443 const std::string &data_file,
444 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000445{
446 if(path.empty())
447 {
448 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
449 }
450 else
451 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100452 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000453 }
454}
455
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100456/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000457 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100458 * @param[in] graph_parameters Graph parameters
459 * @param[in] preprocessor (Optional) Preproccessor object
460 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000461 *
462 * @return An appropriate tensor accessor
463 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100464inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
465 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
466 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000467{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100468 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000469 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100470 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
471 graph_parameters.validation_path,
472 std::move(preprocessor),
473 bgr,
474 graph_parameters.validation_range_start,
475 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000476 }
477 else
478 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100479 const std::string &image_file = graph_parameters.image;
480 const std::string &image_file_lower = lower_string(image_file);
481 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100482 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100483 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
484 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100485 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
486 || arm_compute::utility::endswith(image_file_lower, ".jpg")
487 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100488 {
489 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100490 }
491 else
492 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100493 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100494 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000495 }
496}
497
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100498/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000499 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100500 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
501 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000502 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100503 * @param[in] graph_parameters Graph parameters
504 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
505 * @param[in] is_validation (Optional) Validation flag (default = false)
506 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000507 *
508 * @return An appropriate tensor accessor
509 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100510inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
511 size_t top_n = 5,
512 bool is_validation = false,
513 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000514{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100515 if(!graph_parameters.validation_file.empty())
516 {
517 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
518 output_stream,
519 graph_parameters.validation_range_start,
520 graph_parameters.validation_range_end);
521 }
522 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000523 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000524 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000525 }
526 else
527 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100528 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000529 }
530}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000531/** Generates appropriate output accessor according to the specified graph parameters
532 *
533 * @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
535 *
536 * @param[in] graph_parameters Graph parameters
537 * @param[in] tensor_shapes Network input images tensor shapes.
538 * @param[in] is_validation (Optional) Validation flag (default = false)
539 * @param[out] output_stream (Optional) Output stream (default = std::cout)
540 *
541 * @return An appropriate tensor accessor
542 */
543inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
544 std::vector<TensorShape> tensor_shapes,
545 bool is_validation = false,
546 std::ostream &output_stream = std::cout)
547{
548 if(!graph_parameters.validation_file.empty())
549 {
550 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
551 output_stream,
552 graph_parameters.validation_range_start,
553 graph_parameters.validation_range_end);
554 }
555 else if(graph_parameters.labels.empty())
556 {
557 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
558 }
559 else
560 {
561 return arm_compute::support::cpp14::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
562 }
563}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100564/** Generates appropriate npy output accessor according to the specified npy_path
565 *
566 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
567 *
568 * @param[in] npy_path Path to npy file.
569 * @param[in] shape Shape of the numpy tensor data.
570 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000571 * @param[in] data_layout DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100572 * @param[out] output_stream (Optional) Output stream
573 *
574 * @return An appropriate tensor accessor
575 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000576inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW,
577 std::ostream &output_stream = std::cout)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100578{
579 if(npy_path.empty())
580 {
581 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
582 }
583 else
584 {
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000585 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100586 }
587}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000588
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100589/** Generates appropriate npy output accessor according to the specified npy_path
590 *
591 * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor
592 *
593 * @param[in] npy_name Npy filename.
594 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
595 *
596 * @return An appropriate tensor accessor
597 */
598inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, const bool is_fortran = false)
599{
600 if(npy_name.empty())
601 {
602 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
603 }
604 else
605 {
606 return arm_compute::support::cpp14::make_unique<SaveNumPyAccessor>(npy_name, is_fortran);
607 }
608}
609
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100610/** Permutes a given tensor shape given the input and output data layout
611 *
612 * @param[in] tensor_shape Tensor shape to permute
613 * @param[in] in_data_layout Input tensor shape data layout
614 * @param[in] out_data_layout Output tensor shape data layout
615 *
616 * @return Permuted tensor shape
617 */
618inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
619{
620 if(in_data_layout != out_data_layout)
621 {
622 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
623 arm_compute::permute(tensor_shape, perm_vec);
624 }
625 return tensor_shape;
626}
627
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000628/** Utility function to return the TargetHint
629 *
630 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
631 *
632 * @return the TargetHint
633 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100634inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000635{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100636 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 +0100637 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000638 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100639 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000640 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100641 else if(target == 3)
642 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100643 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100644 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000645 else
646 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100647 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000648 }
649}
Michalis Spyroue4720822017-10-02 17:44:52 +0100650} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100651} // namespace arm_compute
652
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000653#endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */