• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2019 Arm Limited.
3  *
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  */
24 #ifndef __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__
25 #define __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__
26 
27 #include "arm_compute/core/PixelValue.h"
28 #include "arm_compute/core/Utils.h"
29 #include "arm_compute/core/utils/misc/Utility.h"
30 #include "arm_compute/graph/Graph.h"
31 #include "arm_compute/graph/ITensorAccessor.h"
32 #include "arm_compute/graph/Types.h"
33 #include "arm_compute/runtime/Tensor.h"
34 
35 #include "utils/CommonGraphOptions.h"
36 
37 #include <array>
38 #include <random>
39 #include <string>
40 #include <vector>
41 
42 namespace arm_compute
43 {
44 namespace graph_utils
45 {
46 /** Preprocessor interface **/
47 class IPreprocessor
48 {
49 public:
50     /** Default destructor. */
51     virtual ~IPreprocessor() = default;
52     /** Preprocess the given tensor.
53      *
54      * @param[in] tensor Tensor to preprocess.
55      */
56     virtual void preprocess(ITensor &tensor) = 0;
57 };
58 
59 /** Caffe preproccessor */
60 class CaffePreproccessor : public IPreprocessor
61 {
62 public:
63     /** Default Constructor
64      *
65      * @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
68      */
69     CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true, float scale = 1.f);
70     void preprocess(ITensor &tensor) override;
71 
72 private:
73     template <typename T>
74     void preprocess_typed(ITensor &tensor);
75 
76     std::array<float, 3> _mean;
77     bool  _bgr;
78     float _scale;
79 };
80 
81 /** TF preproccessor */
82 class TFPreproccessor : public IPreprocessor
83 {
84 public:
85     /** 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
93     void preprocess(ITensor &tensor) override;
94 
95 private:
96     template <typename T>
97     void preprocess_typed(ITensor &tensor);
98 
99     float _min_range;
100     float _max_range;
101 };
102 
103 /** PPM writer class */
104 class PPMWriter : public graph::ITensorAccessor
105 {
106 public:
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 
119 private:
120     const std::string _name;
121     unsigned int      _iterator;
122     unsigned int      _maximum;
123 };
124 
125 /** Dummy accessor class */
126 class DummyAccessor final : public graph::ITensorAccessor
127 {
128 public:
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 
140 private:
141     unsigned int _iterator;
142     unsigned int _maximum;
143 };
144 
145 /** NumPy accessor class */
146 class NumPyAccessor final : public graph::ITensorAccessor
147 {
148 public:
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.
154      * @param[in]  data_layout   (Optional) DataLayout of the numpy tensor data.
155      * @param[out] output_stream (Optional) Output stream
156      */
157     NumPyAccessor(std::string npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW, std::ostream &output_stream = std::cout);
158     /** 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 
168 private:
169     template <typename T>
170     void access_numpy_tensor(ITensor &tensor, T tolerance);
171 
172     Tensor            _npy_tensor;
173     const std::string _filename;
174     std::ostream     &_output_stream;
175 };
176 
177 /** SaveNumPy accessor class */
178 class SaveNumPyAccessor final : public graph::ITensorAccessor
179 {
180 public:
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 
197 private:
198     const std::string _npy_name;
199     const bool        _is_fortran;
200 };
201 
202 /** Print accessor class
203  *  @note The print accessor will print only when asserts are enabled.
204  *  */
205 class PrintAccessor final : public graph::ITensorAccessor
206 {
207 public:
208     /** Constructor
209      *
210      * @param[out] output_stream (Optional) Output stream
211      * @param[in]  io_fmt        (Optional) Format information
212      */
213     PrintAccessor(std::ostream &output_stream = std::cout, IOFormatInfo io_fmt = IOFormatInfo());
214     /** Allow instances of this class to be move constructed */
215     PrintAccessor(PrintAccessor &&) = default;
216     /** Prevent instances of this class from being copied (As this class contains pointers) */
217     PrintAccessor(const PrintAccessor &) = delete;
218     /** Prevent instances of this class from being copied (As this class contains pointers) */
219     PrintAccessor &operator=(const PrintAccessor &) = delete;
220 
221     // Inherited methods overriden:
222     bool access_tensor(ITensor &tensor) override;
223 
224 private:
225     std::ostream &_output_stream;
226     IOFormatInfo  _io_fmt;
227 };
228 
229 /** Image accessor class */
230 class ImageAccessor final : public graph::ITensorAccessor
231 {
232 public:
233     /** Constructor
234      *
235      * @param[in] filename     Image file
236      * @param[in] bgr          (Optional) Fill the first plane with blue channel (default = false - RGB format)
237      * @param[in] preprocessor (Optional) Image pre-processing object
238      */
239     ImageAccessor(std::string filename, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
240     /** Allow instances of this class to be move constructed */
241     ImageAccessor(ImageAccessor &&) = default;
242 
243     // Inherited methods overriden:
244     bool access_tensor(ITensor &tensor) override;
245 
246 private:
247     bool                           _already_loaded;
248     const std::string              _filename;
249     const bool                     _bgr;
250     std::unique_ptr<IPreprocessor> _preprocessor;
251 };
252 
253 /** Input Accessor used for network validation */
254 class ValidationInputAccessor final : public graph::ITensorAccessor
255 {
256 public:
257     /** Constructor
258      *
259      * @param[in]  image_list    File containing all the images to validate
260      * @param[in]  images_path   Path to images.
261      * @param[in]  bgr           (Optional) Fill the first plane with blue channel (default = false - RGB format)
262      * @param[in]  preprocessor  (Optional) Image pre-processing object  (default = nullptr)
263      * @param[in]  start         (Optional) Start range
264      * @param[in]  end           (Optional) End range
265      * @param[out] output_stream (Optional) Output stream
266      *
267      * @note Range is defined as [start, end]
268      */
269     ValidationInputAccessor(const std::string             &image_list,
270                             std::string                    images_path,
271                             std::unique_ptr<IPreprocessor> preprocessor  = nullptr,
272                             bool                           bgr           = true,
273                             unsigned int                   start         = 0,
274                             unsigned int                   end           = 0,
275                             std::ostream                  &output_stream = std::cout);
276 
277     // Inherited methods overriden:
278     bool access_tensor(ITensor &tensor) override;
279 
280 private:
281     std::string                    _path;
282     std::vector<std::string>       _images;
283     std::unique_ptr<IPreprocessor> _preprocessor;
284     bool                           _bgr;
285     size_t                         _offset;
286     std::ostream                  &_output_stream;
287 };
288 
289 /** Output Accessor used for network validation */
290 class ValidationOutputAccessor final : public graph::ITensorAccessor
291 {
292 public:
293     /** Default Constructor
294      *
295      * @param[in]  image_list    File containing all the images and labels results
296      * @param[out] output_stream (Optional) Output stream (Defaults to the standard output stream)
297      * @param[in]  start         (Optional) Start range
298      * @param[in]  end           (Optional) End range
299      *
300      * @note Range is defined as [start, end]
301      */
302     ValidationOutputAccessor(const std::string &image_list,
303                              std::ostream      &output_stream = std::cout,
304                              unsigned int       start         = 0,
305                              unsigned int       end           = 0);
306     /** Reset accessor state */
307     void reset();
308 
309     // Inherited methods overriden:
310     bool access_tensor(ITensor &tensor) override;
311 
312 private:
313     /** Access predictions of the tensor
314      *
315      * @tparam T Tensor elements type
316      *
317      * @param[in] tensor Tensor to read the predictions from
318      */
319     template <typename T>
320     std::vector<size_t> access_predictions_tensor(ITensor &tensor);
321     /** Aggregates the results of a sample
322      *
323      * @param[in]     res              Vector containing the results of a graph
324      * @param[in,out] positive_samples Positive samples to be updated
325      * @param[in]     top_n            Top n accuracy to measure
326      * @param[in]     correct_label    Correct label of the current sample
327      */
328     void aggregate_sample(const std::vector<size_t> &res, size_t &positive_samples, size_t top_n, size_t correct_label);
329     /** Reports top N accuracy
330      *
331      * @param[in] top_n            Top N accuracy that is being reported
332      * @param[in] total_samples    Total number of samples
333      * @param[in] positive_samples Positive samples
334      */
335     void report_top_n(size_t top_n, size_t total_samples, size_t positive_samples);
336 
337 private:
338     std::vector<int> _results;
339     std::ostream    &_output_stream;
340     size_t           _offset;
341     size_t           _positive_samples_top1;
342     size_t           _positive_samples_top5;
343 };
344 
345 /** Detection output accessor class */
346 class DetectionOutputAccessor final : public graph::ITensorAccessor
347 {
348 public:
349     /** Constructor
350      *
351      * @param[in]  labels_path        Path to labels text file.
352      * @param[in]  imgs_tensor_shapes Network input images tensor shapes.
353      * @param[out] output_stream      (Optional) Output stream
354      */
355     DetectionOutputAccessor(const std::string &labels_path, std::vector<TensorShape> &imgs_tensor_shapes, std::ostream &output_stream = std::cout);
356     /** Allow instances of this class to be move constructed */
357     DetectionOutputAccessor(DetectionOutputAccessor &&) = default;
358     /** Prevent instances of this class from being copied (As this class contains pointers) */
359     DetectionOutputAccessor(const DetectionOutputAccessor &) = delete;
360     /** Prevent instances of this class from being copied (As this class contains pointers) */
361     DetectionOutputAccessor &operator=(const DetectionOutputAccessor &) = delete;
362 
363     // Inherited methods overriden:
364     bool access_tensor(ITensor &tensor) override;
365 
366 private:
367     template <typename T>
368     void access_predictions_tensor(ITensor &tensor);
369 
370     std::vector<std::string> _labels;
371     std::vector<TensorShape> _tensor_shapes;
372     std::ostream            &_output_stream;
373 };
374 
375 /** Result accessor class */
376 class TopNPredictionsAccessor final : public graph::ITensorAccessor
377 {
378 public:
379     /** Constructor
380      *
381      * @param[in]  labels_path   Path to labels text file.
382      * @param[in]  top_n         (Optional) Number of output classes to print
383      * @param[out] output_stream (Optional) Output stream
384      */
385     TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
386     /** Allow instances of this class to be move constructed */
387     TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
388     /** Prevent instances of this class from being copied (As this class contains pointers) */
389     TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
390     /** Prevent instances of this class from being copied (As this class contains pointers) */
391     TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
392 
393     // Inherited methods overriden:
394     bool access_tensor(ITensor &tensor) override;
395 
396 private:
397     template <typename T>
398     void access_predictions_tensor(ITensor &tensor);
399 
400     std::vector<std::string> _labels;
401     std::ostream            &_output_stream;
402     size_t                   _top_n;
403 };
404 
405 /** Random accessor class */
406 class RandomAccessor final : public graph::ITensorAccessor
407 {
408 public:
409     /** Constructor
410      *
411      * @param[in] lower Lower bound value.
412      * @param[in] upper Upper bound value.
413      * @param[in] seed  (Optional) Seed used to initialise the random number generator.
414      */
415     RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
416     /** Allows instances to move constructed */
417     RandomAccessor(RandomAccessor &&) = default;
418 
419     // Inherited methods overriden:
420     bool access_tensor(ITensor &tensor) override;
421 
422 private:
423     template <typename T, typename D>
424     void fill(ITensor &tensor, D &&distribution);
425     PixelValue                      _lower;
426     PixelValue                      _upper;
427     std::random_device::result_type _seed;
428 };
429 
430 /** Numpy Binary loader class*/
431 class NumPyBinLoader final : public graph::ITensorAccessor
432 {
433 public:
434     /** Default Constructor
435      *
436      * @param[in] filename    Binary file name
437      * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
438      */
439     NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
440     /** Allows instances to move constructed */
441     NumPyBinLoader(NumPyBinLoader &&) = default;
442 
443     // Inherited methods overriden:
444     bool access_tensor(ITensor &tensor) override;
445 
446 private:
447     bool              _already_loaded;
448     const std::string _filename;
449     const DataLayout  _file_layout;
450 };
451 
452 /** Generates appropriate random accessor
453  *
454  * @param[in] lower Lower random values bound
455  * @param[in] upper Upper random values bound
456  * @param[in] seed  Random generator seed
457  *
458  * @return A ramdom accessor
459  */
460 inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
461 {
462     return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
463 }
464 
465 /** Generates appropriate weights accessor according to the specified path
466  *
467  * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
468  *
469  * @param[in] path        Path to the data files
470  * @param[in] data_file   Relative path to the data files from path
471  * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
472  *
473  * @return An appropriate tensor accessor
474  */
475 inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
476                                                                     const std::string &data_file,
477                                                                     DataLayout         file_layout = DataLayout::NCHW)
478 {
479     if(path.empty())
480     {
481         return arm_compute::support::cpp14::make_unique<DummyAccessor>();
482     }
483     else
484     {
485         return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
486     }
487 }
488 
489 /** Generates appropriate input accessor according to the specified graph parameters
490  *
491  * @param[in] graph_parameters Graph parameters
492  * @param[in] preprocessor     (Optional) Preproccessor object
493  * @param[in] bgr              (Optional) Fill the first plane with blue channel (default = true)
494  *
495  * @return An appropriate tensor accessor
496  */
497 inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
498                                                                   std::unique_ptr<IPreprocessor>               preprocessor = nullptr,
499                                                                   bool                                         bgr          = true)
500 {
501     if(!graph_parameters.validation_file.empty())
502     {
503         return arm_compute::support::cpp14::make_unique<ValidationInputAccessor>(graph_parameters.validation_file,
504                                                                                  graph_parameters.validation_path,
505                                                                                  std::move(preprocessor),
506                                                                                  bgr,
507                                                                                  graph_parameters.validation_range_start,
508                                                                                  graph_parameters.validation_range_end);
509     }
510     else
511     {
512         const std::string &image_file       = graph_parameters.image;
513         const std::string &image_file_lower = lower_string(image_file);
514         if(arm_compute::utility::endswith(image_file_lower, ".npy"))
515         {
516             return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(image_file, graph_parameters.data_layout);
517         }
518         else if(arm_compute::utility::endswith(image_file_lower, ".jpeg")
519                 || arm_compute::utility::endswith(image_file_lower, ".jpg")
520                 || arm_compute::utility::endswith(image_file_lower, ".ppm"))
521         {
522             return arm_compute::support::cpp14::make_unique<ImageAccessor>(image_file, bgr, std::move(preprocessor));
523         }
524         else
525         {
526             return arm_compute::support::cpp14::make_unique<DummyAccessor>();
527         }
528     }
529 }
530 
531 /** 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]  top_n            (Optional) Number of output classes to print (default = 5)
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  */
543 inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
544                                                                    size_t                                       top_n         = 5,
545                                                                    bool                                         is_validation = false,
546                                                                    std::ostream                                &output_stream = std::cout)
547 {
548     ARM_COMPUTE_UNUSED(is_validation);
549     if(!graph_parameters.validation_file.empty())
550     {
551         return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
552                                                                                   output_stream,
553                                                                                   graph_parameters.validation_range_start,
554                                                                                   graph_parameters.validation_range_end);
555     }
556     else if(graph_parameters.labels.empty())
557     {
558         return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
559     }
560     else
561     {
562         return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(graph_parameters.labels, top_n, output_stream);
563     }
564 }
565 /** Generates appropriate output accessor according to the specified graph parameters
566  *
567  * @note If the output accessor is requested to validate the graph then ValidationOutputAccessor is generated
568  *       else if output_accessor_file is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
569  *
570  * @param[in]  graph_parameters Graph parameters
571  * @param[in]  tensor_shapes    Network input images tensor shapes.
572  * @param[in]  is_validation    (Optional) Validation flag (default = false)
573  * @param[out] output_stream    (Optional) Output stream (default = std::cout)
574  *
575  * @return An appropriate tensor accessor
576  */
577 inline std::unique_ptr<graph::ITensorAccessor> get_detection_output_accessor(const arm_compute::utils::CommonGraphParams &graph_parameters,
578                                                                              std::vector<TensorShape>                     tensor_shapes,
579                                                                              bool                                         is_validation = false,
580                                                                              std::ostream                                &output_stream = std::cout)
581 {
582     ARM_COMPUTE_UNUSED(is_validation);
583     if(!graph_parameters.validation_file.empty())
584     {
585         return arm_compute::support::cpp14::make_unique<ValidationOutputAccessor>(graph_parameters.validation_file,
586                                                                                   output_stream,
587                                                                                   graph_parameters.validation_range_start,
588                                                                                   graph_parameters.validation_range_end);
589     }
590     else if(graph_parameters.labels.empty())
591     {
592         return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
593     }
594     else
595     {
596         return arm_compute::support::cpp14::make_unique<DetectionOutputAccessor>(graph_parameters.labels, tensor_shapes, output_stream);
597     }
598 }
599 /** Generates appropriate npy output accessor according to the specified npy_path
600  *
601  * @note If npy_path is empty will generate a DummyAccessor else will generate a NpyAccessor
602  *
603  * @param[in]  npy_path      Path to npy file.
604  * @param[in]  shape         Shape of the numpy tensor data.
605  * @param[in]  data_type     DataType of the numpy tensor data.
606  * @param[in]  data_layout   DataLayout of the numpy tensor data.
607  * @param[out] output_stream (Optional) Output stream
608  *
609  * @return An appropriate tensor accessor
610  */
611 inline std::unique_ptr<graph::ITensorAccessor> get_npy_output_accessor(const std::string &npy_path, TensorShape shape, DataType data_type, DataLayout data_layout = DataLayout::NCHW,
612                                                                        std::ostream &output_stream = std::cout)
613 {
614     if(npy_path.empty())
615     {
616         return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
617     }
618     else
619     {
620         return arm_compute::support::cpp14::make_unique<NumPyAccessor>(npy_path, shape, data_type, data_layout, output_stream);
621     }
622 }
623 
624 /** Generates appropriate npy output accessor according to the specified npy_path
625  *
626  * @note If npy_path is empty will generate a DummyAccessor else will generate a SaveNpyAccessor
627  *
628  * @param[in] npy_name   Npy filename.
629  * @param[in] is_fortran (Optional) If true, save tensor in fortran order.
630  *
631  * @return An appropriate tensor accessor
632  */
633 inline std::unique_ptr<graph::ITensorAccessor> get_save_npy_output_accessor(const std::string &npy_name, const bool is_fortran = false)
634 {
635     if(npy_name.empty())
636     {
637         return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
638     }
639     else
640     {
641         return arm_compute::support::cpp14::make_unique<SaveNumPyAccessor>(npy_name, is_fortran);
642     }
643 }
644 
645 /** Generates print tensor accessor
646  *
647  * @param[out] output_stream (Optional) Output stream
648  *
649  * @return A print tensor accessor
650  */
651 inline std::unique_ptr<graph::ITensorAccessor> get_print_output_accessor(std::ostream &output_stream = std::cout)
652 {
653     return arm_compute::support::cpp14::make_unique<PrintAccessor>(output_stream);
654 }
655 
656 /** Permutes a given tensor shape given the input and output data layout
657  *
658  * @param[in] tensor_shape    Tensor shape to permute
659  * @param[in] in_data_layout  Input tensor shape data layout
660  * @param[in] out_data_layout Output tensor shape data layout
661  *
662  * @return Permuted tensor shape
663  */
permute_shape(TensorShape tensor_shape,DataLayout in_data_layout,DataLayout out_data_layout)664 inline TensorShape permute_shape(TensorShape tensor_shape, DataLayout in_data_layout, DataLayout out_data_layout)
665 {
666     if(in_data_layout != out_data_layout)
667     {
668         arm_compute::PermutationVector perm_vec = (in_data_layout == DataLayout::NCHW) ? arm_compute::PermutationVector(2U, 0U, 1U) : arm_compute::PermutationVector(1U, 2U, 0U);
669         arm_compute::permute(tensor_shape, perm_vec);
670     }
671     return tensor_shape;
672 }
673 
674 /** Utility function to return the TargetHint
675  *
676  * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
677  *
678  * @return the TargetHint
679  */
set_target_hint(int target)680 inline graph::Target set_target_hint(int target)
681 {
682     ARM_COMPUTE_ERROR_ON_MSG(target > 3, "Invalid target. Target must be 0 (NEON), 1 (OpenCL), 2 (OpenCL + Tuner), 3 (GLES)");
683     if((target == 1 || target == 2))
684     {
685         return graph::Target::CL;
686     }
687     else if(target == 3)
688     {
689         return graph::Target::GC;
690     }
691     else
692     {
693         return graph::Target::NEON;
694     }
695 }
696 } // namespace graph_utils
697 } // namespace arm_compute
698 
699 #endif /* __ARM_COMPUTE_UTILS_GRAPH_UTILS_H__ */
700