blob: bc0822cc89ebe67bafe1253ffb52daced7d611c6 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Isabella Gottardi0ae5de92019-03-14 10:32:11 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitasf52cd782019-03-25 14:06:14 +000024#ifndef __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__
25#define __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__
Anthony Barbier2a07e182017-08-04 18:20:27 +010026
Michalis Spyrou53b405f2017-09-27 15:55:31 +010027#include "arm_compute/core/PixelValue.h"
Gian Marco Iodicead486e22018-08-07 17:17:06 +010028#include "arm_compute/core/Utils.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010029#include "arm_compute/core/utils/misc/Utility.h"
Gian Marcobfa3b522017-12-12 10:08:38 +000030#include "arm_compute/graph/Graph.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010031#include "arm_compute/graph/ITensorAccessor.h"
32#include "arm_compute/graph/Types.h"
Isabella Gottardi88d5b222018-04-06 12:24:55 +010033#include "arm_compute/runtime/Tensor.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010034
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010035#include "utils/CommonGraphOptions.h"
36
Georgios Pinitas140fdc72018-02-16 11:42:38 +000037#include <array>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010038#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010039#include <string>
40#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010041
Anthony Barbier2a07e182017-08-04 18:20:27 +010042namespace arm_compute
43{
44namespace graph_utils
45{
Georgios Pinitas140fdc72018-02-16 11:42:38 +000046/** Preprocessor interface **/
47class IPreprocessor
48{
49public:
Alex Gildayc357c472018-03-21 13:54:09 +000050 /** Default destructor. */
51 virtual ~IPreprocessor() = default;
52 /** Preprocess the given tensor.
53 *
54 * @param[in] tensor Tensor to preprocess.
55 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +000056 virtual void preprocess(ITensor &tensor) = 0;
57};
58
59/** Caffe preproccessor */
60class CaffePreproccessor : public IPreprocessor
61{
62public:
63 /** Default Constructor
64 *
Georgios Pinitasb54c6442019-04-03 13:18:14 +010065 * @param[in] mean Mean array in RGB ordering
66 * @param[in] bgr Boolean specifying if the preprocessing should assume BGR format
67 * @param[in] scale Scale value
Georgios Pinitas140fdc72018-02-16 11:42:38 +000068 */
Georgios Pinitasb54c6442019-04-03 13:18:14 +010069 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true, float scale = 1.f);
Georgios Pinitas140fdc72018-02-16 11:42:38 +000070 void preprocess(ITensor &tensor) override;
71
72private:
giuros01351bd132019-08-23 14:27:30 +010073 template <typename T>
74 void preprocess_typed(ITensor &tensor);
75
Georgios Pinitas140fdc72018-02-16 11:42:38 +000076 std::array<float, 3> _mean;
Pablo Tello32521432018-11-15 14:43:10 +000077 bool _bgr;
Georgios Pinitasb54c6442019-04-03 13:18:14 +010078 float _scale;
Georgios Pinitas140fdc72018-02-16 11:42:38 +000079};
80
81/** TF preproccessor */
82class TFPreproccessor : public IPreprocessor
83{
84public:
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010085 /** Constructor
86 *
87 * @param[in] min_range Min normalization range. (Defaults to -1.f)
88 * @param[in] max_range Max normalization range. (Defaults to 1.f)
89 */
90 TFPreproccessor(float min_range = -1.f, float max_range = 1.f);
91
92 // Inherited overriden methods
Georgios Pinitas140fdc72018-02-16 11:42:38 +000093 void preprocess(ITensor &tensor) override;
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010094
95private:
giuros01351bd132019-08-23 14:27:30 +010096 template <typename T>
97 void preprocess_typed(ITensor &tensor);
98
Georgios Pinitasbe2772a2018-08-17 15:33:39 +010099 float _min_range;
100 float _max_range;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000101};
102
Anthony Barbier2a07e182017-08-04 18:20:27 +0100103/** PPM writer class */
104class PPMWriter : public graph::ITensorAccessor
105{
106public:
107 /** Constructor
108 *
109 * @param[in] name PPM file name
110 * @param[in] maximum Maximum elements to access
111 */
112 PPMWriter(std::string name, unsigned int maximum = 1);
113 /** Allows instances to move constructed */
114 PPMWriter(PPMWriter &&) = default;
115
116 // Inherited methods overriden:
117 bool access_tensor(ITensor &tensor) override;
118
119private:
120 const std::string _name;
121 unsigned int _iterator;
122 unsigned int _maximum;
123};
124
125/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100126class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100127{
128public:
129 /** Constructor
130 *
131 * @param[in] maximum Maximum elements to write
132 */
133 DummyAccessor(unsigned int maximum = 1);
134 /** Allows instances to move constructed */
135 DummyAccessor(DummyAccessor &&) = default;
136
137 // Inherited methods overriden:
138 bool access_tensor(ITensor &tensor) override;
139
140private:
141 unsigned int _iterator;
142 unsigned int _maximum;
143};
144
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100145/** NumPy accessor class */
146class NumPyAccessor final : public graph::ITensorAccessor
147{
148public:
149 /** Constructor
150 *
151 * @param[in] npy_path Path to npy file.
152 * @param[in] shape Shape of the numpy tensor data.
153 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000154 * @param[in] data_layout (Optional) DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100155 * @param[out] output_stream (Optional) Output stream
156 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000157 NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW, std::ostream &output_stream = std::cout);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100158 /** Allow instances of this class to be move constructed */
159 NumPyAccessor(NumPyAccessor &&) = default;
160 /** Prevent instances of this class from being copied (As this class contains pointers) */
161 NumPyAccessor(const NumPyAccessor &) = delete;
162 /** Prevent instances of this class from being copied (As this class contains pointers) */
163 NumPyAccessor &operator=(const NumPyAccessor &) = delete;
164
165 // Inherited methods overriden:
166 bool access_tensor(ITensor &tensor) override;
167
168private:
169 template <typename T>
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000170 void access_numpy_tensor(ITensor &tensor, T tolerance);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100171
172 Tensor _npy_tensor;
173 const std::string _filename;
174 std::ostream &_output_stream;
175};
176
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100177/** SaveNumPy accessor class */
178class SaveNumPyAccessor final : public graph::ITensorAccessor
179{
180public:
181 /** Constructor
182 *
183 * @param[in] npy_name Npy file name.
184 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
185 */
186 SaveNumPyAccessor(const std::string npy_name, const bool is_fortran = false);
187 /** Allow instances of this class to be move constructed */
188 SaveNumPyAccessor(SaveNumPyAccessor &&) = default;
189 /** Prevent instances of this class from being copied (As this class contains pointers) */
190 SaveNumPyAccessor(const SaveNumPyAccessor &) = delete;
191 /** Prevent instances of this class from being copied (As this class contains pointers) */
192 SaveNumPyAccessor &operator=(const SaveNumPyAccessor &) = delete;
193
194 // Inherited methods overriden:
195 bool access_tensor(ITensor &tensor) override;
196
197private:
198 const std::string _npy_name;
199 const bool _is_fortran;
200};
201
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100202/** Image accessor class */
203class ImageAccessor final : public graph::ITensorAccessor
Gian Marco44ec2e72017-10-19 14:13:38 +0100204{
205public:
206 /** Constructor
207 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100208 * @param[in] filename Image file
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100209 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100210 * @param[in] preprocessor (Optional) Image pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100211 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100212 ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100213 /** Allow instances of this class to be move constructed */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100214 ImageAccessor(ImageAccessor &&) = default;
Gian Marco44ec2e72017-10-19 14:13:38 +0100215
216 // Inherited methods overriden:
217 bool access_tensor(ITensor &tensor) override;
218
219private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100220 bool _already_loaded;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100221 const std::string _filename;
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000222 const bool _bgr;
223 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100224};
225
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100226/** Input Accessor used for network validation */
227class ValidationInputAccessor final : public graph::ITensorAccessor
228{
229public:
230 /** Constructor
231 *
Anthony Barbier40606df2018-07-23 14:41:59 +0100232 * @param[in] image_list File containing all the images to validate
233 * @param[in] images_path Path to images.
234 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false - RGB format)
235 * @param[in] preprocessor (Optional) Image pre-processing object (default = nullptr)
236 * @param[in] start (Optional) Start range
237 * @param[in] end (Optional) End range
238 * @param[out] output_stream (Optional) Output stream
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100239 *
Georgios Pinitas7908de72018-06-27 12:34:20 +0100240 * @note Range is defined as [start, end]
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100241 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100242 ValidationInputAccessor(const std::string &image_list,
243 std::string images_path,
Anthony Barbier40606df2018-07-23 14:41:59 +0100244 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
245 bool bgr = true,
246 unsigned int start = 0,
247 unsigned int end = 0,
248 std::ostream &output_stream = std::cout);
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100249
250 // Inherited methods overriden:
251 bool access_tensor(ITensor &tensor) override;
252
253private:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100254 std::string _path;
255 std::vector<std::string> _images;
256 std::unique_ptr<IPreprocessor> _preprocessor;
257 bool _bgr;
258 size_t _offset;
Anthony Barbier40606df2018-07-23 14:41:59 +0100259 std::ostream &_output_stream;
Georgios Pinitas7c3b9242018-06-21 19:01:25 +0100260};
261
Georgios Pinitas7908de72018-06-27 12:34:20 +0100262/** Output Accessor used for network validation */
263class ValidationOutputAccessor final : public graph::ITensorAccessor
264{
265public:
266 /** Default Constructor
267 *
268 * @param[in] image_list File containing all the images and labels results
Georgios Pinitas7908de72018-06-27 12:34:20 +0100269 * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
270 * @param[in] start (Optional) Start range
271 * @param[in] end (Optional) End range
272 *
273 * @note Range is defined as [start, end]
274 */
275 ValidationOutputAccessor(const std::string &image_list,
Georgios Pinitas7908de72018-06-27 12:34:20 +0100276 std::ostream &output_stream = std::cout,
277 unsigned int start = 0,
278 unsigned int end = 0);
279 /** Reset accessor state */
280 void reset();
281
282 // Inherited methods overriden:
283 bool access_tensor(ITensor &tensor) override;
284
285private:
286 /** Access predictions of the tensor
287 *
288 * @tparam T Tensor elements type
289 *
290 * @param[in] tensor Tensor to read the predictions from
291 */
292 template <typename T>
293 std::vector<size_t> access_predictions_tensor(ITensor &tensor);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100294 /** Aggregates the results of a sample
295 *
296 * @param[in] res Vector containing the results of a graph
297 * @param[in,out] positive_samples Positive samples to be updated
298 * @param[in] top_n Top n accuracy to measure
299 * @param[in] correct_label Correct label of the current sample
300 */
301 void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
302 /** Reports top N accuracy
303 *
304 * @param[in] top_n Top N accuracy that is being reported
305 * @param[in] total_samples Total number of samples
306 * @param[in] positive_samples Positive samples
307 */
308 void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
Georgios Pinitas7908de72018-06-27 12:34:20 +0100309
310private:
311 std::vector<int> _results;
312 std::ostream &_output_stream;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100313 size_t _offset;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100314 size_t _positive_samples_top1;
315 size_t _positive_samples_top5;
Georgios Pinitas7908de72018-06-27 12:34:20 +0100316};
317
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000318/** Detection output accessor class */
319class DetectionOutputAccessor final : public graph::ITensorAccessor
320{
321public:
322 /** Constructor
323 *
324 * @param[in] labels_path Path to labels text file.
325 * @param[in] imgs_tensor_shapes Network input images tensor shapes.
326 * @param[out] output_stream (Optional) Output stream
327 */
328 DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout);
329 /** Allow instances of this class to be move constructed */
330 DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
331 /** Prevent instances of this class from being copied (As this class contains pointers) */
332 DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
333 /** Prevent instances of this class from being copied (As this class contains pointers) */
334 DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
335
336 // Inherited methods overriden:
337 bool access_tensor(ITensor &tensor) override;
338
339private:
340 template <typename T>
341 void access_predictions_tensor(ITensor &tensor);
342
343 std::vector<std::string> _labels;
344 std::vector<TensorShape> _tensor_shapes;
345 std::ostream &_output_stream;
346};
347
Gian Marco44ec2e72017-10-19 14:13:38 +0100348/** Result accessor class */
349class TopNPredictionsAccessor final : public graph::ITensorAccessor
350{
351public:
352 /** Constructor
353 *
354 * @param[in] labels_path Path to labels text file.
355 * @param[in] top_n (Optional) Number of output classes to print
356 * @param[out] output_stream (Optional) Output stream
357 */
358 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
359 /** Allow instances of this class to be move constructed */
360 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
361 /** Prevent instances of this class from being copied (As this class contains pointers) */
362 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
363 /** Prevent instances of this class from being copied (As this class contains pointers) */
364 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
365
366 // Inherited methods overriden:
367 bool access_tensor(ITensor &tensor) override;
368
369private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000370 template <typename T>
371 void access_predictions_tensor(ITensor &tensor);
372
Gian Marco44ec2e72017-10-19 14:13:38 +0100373 std::vector<std::string> _labels;
374 std::ostream &_output_stream;
375 size_t _top_n;
376};
377
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100378/** Random accessor class */
379class RandomAccessor final : public graph::ITensorAccessor
380{
381public:
382 /** Constructor
383 *
384 * @param[in] lower Lower bound value.
385 * @param[in] upper Upper bound value.
386 * @param[in] seed (Optional) Seed used to initialise the random number generator.
387 */
388 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
389 /** Allows instances to move constructed */
390 RandomAccessor(RandomAccessor &&) = default;
391
392 // Inherited methods overriden:
393 bool access_tensor(ITensor &tensor) override;
394
395private:
396 template <typename T, typename D>
397 void fill(ITensor &tensor, D &&distribution);
398 PixelValue _lower;
399 PixelValue _upper;
400 std::random_device::result_type _seed;
401};
402
Anthony Barbier2a07e182017-08-04 18:20:27 +0100403/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100404class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100405{
406public:
407 /** Default Constructor
408 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100409 * @param[in] filename Binary file name
410 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100411 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100412 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100413 /** Allows instances to move constructed */
414 NumPyBinLoader(NumPyBinLoader &&) = default;
415
416 // Inherited methods overriden:
417 bool access_tensor(ITensor &tensor) override;
418
419private:
Anthony Barbier8a042112018-08-21 18:16:53 +0100420 bool _already_loaded;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100421 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100422 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100423};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000424
Georgios Pinitas652bde52018-01-10 15:33:28 +0000425/** Generates appropriate random accessor
426 *
427 * @param[in] lower Lower random values bound
428 * @param[in] upper Upper random values bound
429 * @param[in] seed Random generator seed
430 *
431 * @return A ramdom accessor
432 */
433inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
434{
435 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
436}
437
Isabella Gottardia4c61882017-11-03 12:11:55 +0000438/** Generates appropriate weights accessor according to the specified path
439 *
440 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
441 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100442 * @param[in] path Path to the data files
443 * @param[in] data_file Relative path to the data files from path
444 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000445 *
446 * @return An appropriate tensor accessor
447 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100448inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
449 const std::string &data_file,
450 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000451{
452 if(path.empty())
453 {
454 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
455 }
456 else
457 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100458 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000459 }
460}
461
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100462/** Generates appropriate input accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000463 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100464 * @param[in] graph_parameters Graph parameters
465 * @param[in] preprocessor (Optional) Preproccessor object
466 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000467 *
468 * @return An appropriate tensor accessor
469 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100470inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
471 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
472 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000473{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100474 if(!graph_parameters.validation_file.empty())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000475 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100476 return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
477 graph_parameters.validation_path,
478 std::move(preprocessor),
479 bgr,
480 graph_parameters.validation_range_start,
481 graph_parameters.validation_range_end);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000482 }
483 else
484 {
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100485 const std::string &image_file = graph_parameters.image;
486 const std::string &image_file_lower = lower_string(image_file);
487 if(arm_compute::utility::endswith(image_file_lower, ".npy"))
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100488 {
Manuel Bottini32527952019-11-05 16:55:57 +0000489 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file, graph_parameters.data_layout);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100490 }
Gian Marco Iodicead486e22018-08-07 17:17:06 +0100491 else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
492 || arm_compute::utility::endswith(image_file_lower, ".jpg")
493 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100494 {
495 return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100496 }
497 else
498 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100499 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100500 }
Isabella Gottardia4c61882017-11-03 12:11:55 +0000501 }
502}
503
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100504/** Generates appropriate output accessor according to the specified graph parameters
Isabella Gottardia4c61882017-11-03 12:11:55 +0000505 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100506 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
507 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
Isabella Gottardia4c61882017-11-03 12:11:55 +0000508 *
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100509 * @param[in] graph_parameters Graph parameters
510 * @param[in] top_n (Optional) Number of output classes to print (default = 5)
511 * @param[in] is_validation (Optional) Validation flag (default = false)
512 * @param[out] output_stream (Optional) Output stream (default = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000513 *
514 * @return An appropriate tensor accessor
515 */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100516inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
517 size_t top_n = 5,
518 bool is_validation = false,
519 std::ostream &output_stream = std::cout)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000520{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100521 ARM_COMPUTE_UNUSED(is_validation);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100522 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())
Isabella Gottardia4c61882017-11-03 12:11:55 +0000530 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000531 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000532 }
533 else
534 {
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100535 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000536 }
537}
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000538/** Generates appropriate output accessor according to the specified graph parameters
539 *
540 * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
541 * else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
542 *
543 * @param[in] graph_parameters Graph parameters
544 * @param[in] tensor_shapes Network input images tensor shapes.
545 * @param[in] is_validation (Optional) Validation flag (default = false)
546 * @param[out] output_stream (Optional) Output stream (default = std::cout)
547 *
548 * @return An appropriate tensor accessor
549 */
550inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
551 std::vector<TensorShape> tensor_shapes,
552 bool is_validation = false,
553 std::ostream &output_stream = std::cout)
554{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100555 ARM_COMPUTE_UNUSED(is_validation);
Isabella Gottardi7234ed82018-11-27 08:51:10 +0000556 if(!graph_parameters.validation_file.empty())
557 {
558 return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
559 output_stream,
560 graph_parameters.validation_range_start,
561 graph_parameters.validation_range_end);
562 }
563 else if(graph_parameters.labels.empty())
564 {
565 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
566 }
567 else
568 {
569 return arm_compute::support::cpp14::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
570 }
571}
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100572/** Generates appropriate npy output accessor according to the specified npy_path
573 *
574 * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
575 *
576 * @param[in] npy_path Path to npy file.
577 * @param[in] shape Shape of the numpy tensor data.
578 * @param[in] data_type DataType of the numpy tensor data.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000579 * @param[in] data_layout DataLayout of the numpy tensor data.
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100580 * @param[out] output_stream (Optional) Output stream
581 *
582 * @return An appropriate tensor accessor
583 */
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000584inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW,
585 std::ostream &output_stream = std::cout)
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100586{
587 if(npy_path.empty())
588 {
589 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
590 }
591 else
592 {
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000593 return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream);
Isabella Gottardi88d5b222018-04-06 12:24:55 +0100594 }
595}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000596
Isabella Gottardi2ea37612019-07-16 11:48:51 +0100597/** Generates appropriate npy output accessor according to the specified npy_path
598 *
599 * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor
600 *
601 * @param[in] npy_name Npy filename.
602 * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
603 *
604 * @return An appropriate tensor accessor
605 */
606inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, const bool is_fortran = false)
607{
608 if(npy_name.empty())
609 {
610 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
611 }
612 else
613 {
614 return arm_compute::support::cpp14::make_unique<SaveNumPyAccessor>(npy_name, is_fortran);
615 }
616}
617
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100618/** Permutes a given tensor shape given the input and output data layout
619 *
620 * @param[in] tensor_shape Tensor shape to permute
621 * @param[in] in_data_layout Input tensor shape data layout
622 * @param[in] out_data_layout Output tensor shape data layout
623 *
624 * @return Permuted tensor shape
625 */
626inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
627{
628 if(in_data_layout != out_data_layout)
629 {
630 arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
631 arm_compute::permute(tensor_shape, perm_vec);
632 }
633 return tensor_shape;
634}
635
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000636/** Utility function to return the TargetHint
637 *
638 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
639 *
640 * @return the TargetHint
641 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100642inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000643{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100644 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 +0100645 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000646 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100647 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000648 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100649 else if(target == 3)
650 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100651 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100652 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000653 else
654 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100655 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000656 }
657}
Michalis Spyroue4720822017-10-02 17:44:52 +0100658} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100659} // namespace arm_compute
660
Georgios Pinitasf52cd782019-03-25 14:06:14 +0000661#endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */