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