blob: 4ae484f430ba054a4b99a44191980d31994be9b2 [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 *
65 * @param mean Mean array in RGB ordering
Pablo Tello32521432018-11-15 14:43:10 +000066 * @param scale Scale value
Georgios Pinitas140fdc72018-02-16 11:42:38 +000067 * @param bgr Boolean specifying if the preprocessing should assume BGR format
68 */
Pablo Tello32521432018-11-15 14:43:10 +000069 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, float scale = 1.f, bool bgr = true);
Georgios Pinitas140fdc72018-02-16 11:42:38 +000070 void preprocess(ITensor &tensor) override;
71
72private:
73 std::array<float, 3> _mean;
Pablo Tello32521432018-11-15 14:43:10 +000074 float _scale;
75 bool _bgr;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000076};
77
78/** TF preproccessor */
79class TFPreproccessor : public IPreprocessor
80{
81public:
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010082 /** Constructor
83 *
84 * @param[in] min_range Min normalization range. (Defaults to -1.f)
85 * @param[in] max_range Max normalization range. (Defaults to 1.f)
86 */
87 TFPreproccessor(float min_range = -1.f, float max_range = 1.f);
88
89 // Inherited overriden methods
Georgios Pinitas140fdc72018-02-16 11:42:38 +000090 void preprocess(ITensor &tensor) override;
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010091
92private:
93 float _min_range;
94 float _max_range;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000095};
96
Anthony Barbier2a07e182017-08-04 18:20:27 +010097/** PPM writer class */
98class PPMWriter : public graph::ITensorAccessor
99{
100public:
101 /** Constructor
102 *
103 * @param[in] name PPM file name
104 * @param[in] maximum Maximum elements to access
105 */
106 PPMWriter(std::string name, unsigned int maximum = 1);
107 /** Allows instances to move constructed */
108 PPMWriter(PPMWriter &&) = default;
109
110 // Inherited methods overriden:
111 bool access_tensor(ITensor &tensor) override;
112
113private:
114 const std::string _name;
115 unsigned int _iterator;
116 unsigned int _maximum;
117};
118
119/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100120class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121{
122public:
123 /** Constructor
124 *
125 * @param[in] maximum Maximum elements to write
126 */
127 DummyAccessor(unsigned int maximum = 1);
128 /** Allows instances to move constructed */
129 DummyAccessor(DummyAccessor &&) = default;
130
131 // Inherited methods overriden:
132 bool access_tensor(ITensor &tensor) override;
133
134private:
135 unsigned int _iterator;
136 unsigned int _maximum;
137};
138
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100139/** NumPy accessor class */
140class NumPyAccessor final : public graph::ITensorAccessor
141{
142public:
143 /** Constructor
144 *
145 * @param[in] npy_path Path to npy file.
146 * @param[in] shape Shape of the numpy tensor data.
147 * @param[in] data_type DataType of the numpy tensor data.
148 * @param[out] output_stream (Optional) Output stream
149 */
150 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout);
151 /** Allow instances of this class to be move constructed */
152 NumPyAccessor(NumPyAccessor &&) = default;
153 /** Prevent instances of this class from being copied (As this class contains pointers) */
154 NumPyAccessor(const NumPyAccessor &) = delete;
155 /** Prevent instances of this class from being copied (As this class contains pointers) */
156 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
157
158 // Inherited methods overriden:
159 bool access_tensor(ITensor &tensor) override;
160
161private:
162 template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000163 void access_numpy_tensor(ITensor &tensor, T tolerance);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100164
165 Tensor _npy_tensor;
166 const std::string _filename;
167 std::ostream &_output_stream;
168};
169
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100170/** Image accessor class */
171class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100172{
173public:
174 /** Constructor
175 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100176 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100177 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100178 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100179 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100180 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100181 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100182 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100183
184 // Inherited methods overriden:
185 bool access_tensor(ITensor &tensor) override;
186
187private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100188 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100189 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000190 const bool _bgr;
191 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100192};
193
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100194/** Input Accessor used for network validation */
195class ValidationInputAccessor final : public graph::ITensorAccessor
196{
197public:
198 /** Constructor
199 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100200 * @param[in] image_list File containing all the images to validate
201 * @param[in] images_path Path to images.
202 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
203 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
204 * @param[in] start (Optional) Start range
205 * @param[in] end (Optional) End range
206 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100207 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100208 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100209 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100210 ValidationInputAccessor(const std::string &image_list,
211 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100212 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
213 bool bgr = true,
214 unsigned int start = 0,
215 unsigned int end = 0,
216 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100217
218 // Inherited methods overriden:
219 bool access_tensor(ITensor &tensor) override;
220
221private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100222 std::string _path;
223 std::vector<std::string> _images;
224 std::unique_ptr<IPreprocessor> _preprocessor;
225 bool _bgr;
226 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100227 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100228};
229
Georgios Pinitas7908de72018-06-27 12:34:20 +0100230/** Output Accessor used for network validation */
231class ValidationOutputAccessor final : public graph::ITensorAccessor
232{
233public:
234 /** Default Constructor
235 *
236 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100237 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
238 * @param[in] start (Optional) Start range
239 * @param[in] end (Optional) End range
240 *
241 * @note Range is defined as [start, end]
242 */
243 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100244 std::ostream &output_stream = std::cout,
245 unsigned int start = 0,
246 unsigned int end = 0);
247 /** Reset accessor state */
248 void reset();
249
250 // Inherited methods overriden:
251 bool access_tensor(ITensor &tensor) override;
252
253private:
254 /** Access predictions of the tensor
255 *
256 * @tparam T Tensor elements type
257 *
258 * @param[in] tensor Tensor to read the predictions from
259 */
260 template <typename T>
261 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100262 /** Aggregates the results of a sample
263 *
264 * @param[in] res Vector containing the results of a graph
265 * @param[in,out] positive_samples Positive samples to be updated
266 * @param[in] top_n Top n accuracy to measure
267 * @param[in] correct_label Correct label of the current sample
268 */
269 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
270 /** Reports top N accuracy
271 *
272 * @param[in] top_n Top N accuracy that is being reported
273 * @param[in] total_samples Total number of samples
274 * @param[in] positive_samples Positive samples
275 */
276 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100277
278private:
279 std::vector<int> _results;
280 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100281 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100282 size_t _positive_samples_top1;
283 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100284};
285
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000286/** Detection output accessor class */
287class DetectionOutputAccessor final : public graph::ITensorAccessor
288{
289public:
290 /** Constructor
291 *
292 * @param[in] labels_path Path to labels text file.
293 * @param[in] imgs_tensor_shapes Network input images tensor shapes.
294 * @param[out] output_stream (Optional) Output stream
295 */
296 DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout);
297 /** Allow instances of this class to be move constructed */
298 DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
299 /** Prevent instances of this class from being copied (As this class contains pointers) */
300 DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
301 /** Prevent instances of this class from being copied (As this class contains pointers) */
302 DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
303
304 // Inherited methods overriden:
305 bool access_tensor(ITensor &tensor) override;
306
307private:
308 template <typename T>
309 void access_predictions_tensor(ITensor &tensor);
310
311 std::vector<std::string> _labels;
312 std::vector<TensorShape> _tensor_shapes;
313 std::ostream &_output_stream;
314};
315
Gian Marco44ec2e72017-10-19 14:13:38 +0100316/** Result accessor class */
317class TopNPredictionsAccessor final : public graph::ITensorAccessor
318{
319public:
320 /** Constructor
321 *
322 * @param[in] labels_path Path to labels text file.
323 * @param[in] top_n (Optional) Number of output classes to print
324 * @param[out] output_stream (Optional) Output stream
325 */
326 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
327 /** Allow instances of this class to be move constructed */
328 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
329 /** Prevent instances of this class from being copied (As this class contains pointers) */
330 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
331 /** Prevent instances of this class from being copied (As this class contains pointers) */
332 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
333
334 // Inherited methods overriden:
335 bool access_tensor(ITensor &tensor) override;
336
337private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000338 template <typename T>
339 void access_predictions_tensor(ITensor &tensor);
340
Gian Marco44ec2e72017-10-19 14:13:38 +0100341 std::vector<std::string> _labels;
342 std::ostream &_output_stream;
343 size_t _top_n;
344};
345
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100346/** Random accessor class */
347class RandomAccessor final : public graph::ITensorAccessor
348{
349public:
350 /** Constructor
351 *
352 * @param[in] lower Lower bound value.
353 * @param[in] upper Upper bound value.
354 * @param[in] seed (Optional) Seed used to initialise the random number generator.
355 */
356 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
357 /** Allows instances to move constructed */
358 RandomAccessor(RandomAccessor &&) = default;
359
360 // Inherited methods overriden:
361 bool access_tensor(ITensor &tensor) override;
362
363private:
364 template <typename T, typename D>
365 void fill(ITensor &tensor, D &&distribution);
366 PixelValue _lower;
367 PixelValue _upper;
368 std::random_device::result_type _seed;
369};
370
Anthony Barbier2a07e182017-08-04 18:20:27 +0100371/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100372class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100373{
374public:
375 /** Default Constructor
376 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100377 * @param[in] filename Binary file name
378 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100379 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100380 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100381 /** Allows instances to move constructed */
382 NumPyBinLoader(NumPyBinLoader &&) = default;
383
384 // Inherited methods overriden:
385 bool access_tensor(ITensor &tensor) override;
386
387private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100388 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100389 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100390 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100391};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000392
Georgios Pinitas652bde52018-01-10 15:33:28 +0000393/** Generates appropriate random accessor
394 *
395 * @param[in] lower Lower random values bound
396 * @param[in] upper Upper random values bound
397 * @param[in] seed Random generator seed
398 *
399 * @return A ramdom accessor
400 */
401inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
402{
403 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
404}
405
Isabella Gottardia4c61882017-11-03 12:11:55 +0000406/** Generates appropriate weights accessor according to the specified path
407 *
408 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
409 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100410 * @param[in] path Path to the data files
411 * @param[in] data_file Relative path to the data files from path
412 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000413 *
414 * @return An appropriate tensor accessor
415 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100416inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
417 const std::string &data_file,
418 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000419{
420 if(path.empty())
421 {
422 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
423 }
424 else
425 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100426 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000427 }
428}
429
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100430/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000431 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100432 * @param[in] graph_parameters Graph parameters
433 * @param[in] preprocessor (Optional) Preproccessor object
434 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000435 *
436 * @return An appropriate tensor accessor
437 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100438inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
439 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
440 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000441{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100442 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000443 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100444 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
445 graph_parameters.validation_path,
446 std::move(preprocessor),
447 bgr,
448 graph_parameters.validation_range_start,
449 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000450 }
451 else
452 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100453 const std::string &image_file = graph_parameters.image;
454 const std::string &image_file_lower = lower_string(image_file);
455 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100456 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100457 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file);
458 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100459 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
460 || arm_compute::utility::endswith(image_file_lower, ".jpg")
461 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100462 {
463 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100464 }
465 else
466 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100467 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100468 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000469 }
470}
471
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100472/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000473 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100474 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
475 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000476 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100477 * @param[in] graph_parameters Graph parameters
478 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
479 * @param[in] is_validation (Optional) Validation flag (default = false)
480 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000481 *
482 * @return An appropriate tensor accessor
483 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100484inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
485 size_t top_n = 5,
486 bool is_validation = false,
487 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000488{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100489 if(!graph_parameters.validation_file.empty())
490 {
491 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
492 output_stream,
493 graph_parameters.validation_range_start,
494 graph_parameters.validation_range_end);
495 }
496 else if(graph_parameters.labels.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000497 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000498 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000499 }
500 else
501 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100502 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000503 }
504}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000505/** Generates appropriate output accessor according to the specified graph parameters
506 *
507 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
508 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
509 *
510 * @param[in] graph_parameters Graph parameters
511 * @param[in] tensor_shapes Network input images tensor shapes.
512 * @param[in] is_validation (Optional) Validation flag (default = false)
513 * @param[out] output_stream (Optional) Output stream (default = std::cout)
514 *
515 * @return An appropriate tensor accessor
516 */
517inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
518 std::vector<TensorShape> tensor_shapes,
519 bool is_validation = false,
520 std::ostream &output_stream = std::cout)
521{
522 if(!graph_parameters.validation_file.empty())
523 {
524 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
525 output_stream,
526 graph_parameters.validation_range_start,
527 graph_parameters.validation_range_end);
528 }
529 else if(graph_parameters.labels.empty())
530 {
531 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
532 }
533 else
534 {
535 return arm_compute::support::cpp14::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
536 }
537}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100538/** Generates appropriate npy output accessor according to the specified npy_path
539 *
540 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
541 *
542 * @param[in] npy_path Path to npy file.
543 * @param[in] shape Shape of the numpy tensor data.
544 * @param[in] data_type DataType of the numpy tensor data.
545 * @param[out] output_stream (Optional) Output stream
546 *
547 * @return An appropriate tensor accessor
548 */
549inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, std::ostream &output_stream = std::cout)
550{
551 if(npy_path.empty())
552 {
553 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
554 }
555 else
556 {
557 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, output_stream);
558 }
559}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000560
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100561/** Permutes a given tensor shape given the input and output data layout
562 *
563 * @param[in] tensor_shape Tensor shape to permute
564 * @param[in] in_data_layout Input tensor shape data layout
565 * @param[in] out_data_layout Output tensor shape data layout
566 *
567 * @return Permuted tensor shape
568 */
569inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
570{
571 if(in_data_layout != out_data_layout)
572 {
573 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
574 arm_compute::permute(tensor_shape, perm_vec);
575 }
576 return tensor_shape;
577}
578
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000579/** Utility function to return the TargetHint
580 *
581 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
582 *
583 * @return the TargetHint
584 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100585inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000586{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100587 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 +0100588 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000589 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100590 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000591 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100592 else if(target == 3)
593 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100594 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100595 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000596 else
597 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100598 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000599 }
600}
Michalis Spyroue4720822017-10-02 17:44:52 +0100601} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100602} // namespace arm_compute
603
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000604#endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */