blob: a8507b1ac72969d076b6ba64a6bf33cb1a71e387 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Giorgio Arenaa66eaa22017-12-21 19:50:06 +00002 * Copyright (c) 2017-2018 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 */
24#ifndef __ARM_COMPUTE_GRAPH_UTILS_H__
25#define __ARM_COMPUTE_GRAPH_UTILS_H__
26
Michalis Spyrou53b405f2017-09-27 15:55:31 +010027#include "arm_compute/core/PixelValue.h"
Gian Marcobfa3b522017-12-12 10:08:38 +000028#include "arm_compute/graph/Graph.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010029#include "arm_compute/graph/ITensorAccessor.h"
30#include "arm_compute/graph/Types.h"
31
Georgios Pinitas140fdc72018-02-16 11:42:38 +000032#include <array>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010033#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010034#include <string>
35#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010036
Anthony Barbier2a07e182017-08-04 18:20:27 +010037namespace arm_compute
38{
39namespace graph_utils
40{
Georgios Pinitas140fdc72018-02-16 11:42:38 +000041/** Preprocessor interface **/
42class IPreprocessor
43{
44public:
Alex Gildayc357c472018-03-21 13:54:09 +000045 /** Default destructor. */
46 virtual ~IPreprocessor() = default;
47 /** Preprocess the given tensor.
48 *
49 * @param[in] tensor Tensor to preprocess.
50 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +000051 virtual void preprocess(ITensor &tensor) = 0;
52};
53
54/** Caffe preproccessor */
55class CaffePreproccessor : public IPreprocessor
56{
57public:
58 /** Default Constructor
59 *
60 * @param mean Mean array in RGB ordering
61 * @param bgr Boolean specifying if the preprocessing should assume BGR format
62 */
63 CaffePreproccessor(std::array<float, 3> mean = std::array<float, 3> { { 0, 0, 0 } }, bool bgr = true);
64 void preprocess(ITensor &tensor) override;
65
66private:
67 std::array<float, 3> _mean;
68 bool _bgr;
69};
70
71/** TF preproccessor */
72class TFPreproccessor : public IPreprocessor
73{
74public:
75 void preprocess(ITensor &tensor) override;
76};
77
Anthony Barbier2a07e182017-08-04 18:20:27 +010078/** PPM writer class */
79class PPMWriter : public graph::ITensorAccessor
80{
81public:
82 /** Constructor
83 *
84 * @param[in] name PPM file name
85 * @param[in] maximum Maximum elements to access
86 */
87 PPMWriter(std::string name, unsigned int maximum = 1);
88 /** Allows instances to move constructed */
89 PPMWriter(PPMWriter &&) = default;
90
91 // Inherited methods overriden:
92 bool access_tensor(ITensor &tensor) override;
93
94private:
95 const std::string _name;
96 unsigned int _iterator;
97 unsigned int _maximum;
98};
99
100/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100101class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100102{
103public:
104 /** Constructor
105 *
106 * @param[in] maximum Maximum elements to write
107 */
108 DummyAccessor(unsigned int maximum = 1);
109 /** Allows instances to move constructed */
110 DummyAccessor(DummyAccessor &&) = default;
111
112 // Inherited methods overriden:
113 bool access_tensor(ITensor &tensor) override;
114
115private:
116 unsigned int _iterator;
117 unsigned int _maximum;
118};
119
Gian Marco44ec2e72017-10-19 14:13:38 +0100120/** PPM accessor class */
121class PPMAccessor final : public graph::ITensorAccessor
122{
123public:
124 /** Constructor
125 *
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000126 * @param[in] ppm_path Path to PPM file
127 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false)
128 * @param[in] preprocessor (Optional) PPM pre-processing object
Gian Marco44ec2e72017-10-19 14:13:38 +0100129 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000130 PPMAccessor(std::string ppm_path, bool bgr = true, std::unique_ptr<IPreprocessor> preprocessor = nullptr);
Gian Marco44ec2e72017-10-19 14:13:38 +0100131 /** Allow instances of this class to be move constructed */
132 PPMAccessor(PPMAccessor &&) = default;
133
134 // Inherited methods overriden:
135 bool access_tensor(ITensor &tensor) override;
136
137private:
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000138 const std::string _ppm_path;
139 const bool _bgr;
140 std::unique_ptr<IPreprocessor> _preprocessor;
Gian Marco44ec2e72017-10-19 14:13:38 +0100141};
142
143/** Result accessor class */
144class TopNPredictionsAccessor final : public graph::ITensorAccessor
145{
146public:
147 /** Constructor
148 *
149 * @param[in] labels_path Path to labels text file.
150 * @param[in] top_n (Optional) Number of output classes to print
151 * @param[out] output_stream (Optional) Output stream
152 */
153 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
154 /** Allow instances of this class to be move constructed */
155 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
156 /** Prevent instances of this class from being copied (As this class contains pointers) */
157 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
158 /** Prevent instances of this class from being copied (As this class contains pointers) */
159 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
160
161 // Inherited methods overriden:
162 bool access_tensor(ITensor &tensor) override;
163
164private:
Giorgio Arenaa66eaa22017-12-21 19:50:06 +0000165 template <typename T>
166 void access_predictions_tensor(ITensor &tensor);
167
Gian Marco44ec2e72017-10-19 14:13:38 +0100168 std::vector<std::string> _labels;
169 std::ostream &_output_stream;
170 size_t _top_n;
171};
172
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100173/** Random accessor class */
174class RandomAccessor final : public graph::ITensorAccessor
175{
176public:
177 /** Constructor
178 *
179 * @param[in] lower Lower bound value.
180 * @param[in] upper Upper bound value.
181 * @param[in] seed (Optional) Seed used to initialise the random number generator.
182 */
183 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
184 /** Allows instances to move constructed */
185 RandomAccessor(RandomAccessor &&) = default;
186
187 // Inherited methods overriden:
188 bool access_tensor(ITensor &tensor) override;
189
190private:
191 template <typename T, typename D>
192 void fill(ITensor &tensor, D &&distribution);
193 PixelValue _lower;
194 PixelValue _upper;
195 std::random_device::result_type _seed;
196};
197
Anthony Barbier2a07e182017-08-04 18:20:27 +0100198/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100199class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100200{
201public:
202 /** Default Constructor
203 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100204 * @param[in] filename Binary file name
205 * @param[in] file_layout (Optional) Layout of the numpy tensor data. Defaults to NCHW
Anthony Barbier2a07e182017-08-04 18:20:27 +0100206 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100207 NumPyBinLoader(std::string filename, DataLayout file_layout = DataLayout::NCHW);
Anthony Barbier2a07e182017-08-04 18:20:27 +0100208 /** Allows instances to move constructed */
209 NumPyBinLoader(NumPyBinLoader &&) = default;
210
211 // Inherited methods overriden:
212 bool access_tensor(ITensor &tensor) override;
213
214private:
215 const std::string _filename;
Georgios Pinitascac13b12018-04-27 19:07:19 +0100216 const DataLayout _file_layout;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100217};
Isabella Gottardia4c61882017-11-03 12:11:55 +0000218
Georgios Pinitas652bde52018-01-10 15:33:28 +0000219/** Generates appropriate random accessor
220 *
221 * @param[in] lower Lower random values bound
222 * @param[in] upper Upper random values bound
223 * @param[in] seed Random generator seed
224 *
225 * @return A ramdom accessor
226 */
227inline std::unique_ptr<graph::ITensorAccessor> get_random_accessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0)
228{
229 return arm_compute::support::cpp14::make_unique<RandomAccessor>(lower, upper, seed);
230}
231
Isabella Gottardia4c61882017-11-03 12:11:55 +0000232/** Generates appropriate weights accessor according to the specified path
233 *
234 * @note If path is empty will generate a DummyAccessor else will generate a NumPyBinLoader
235 *
Georgios Pinitascac13b12018-04-27 19:07:19 +0100236 * @param[in] path Path to the data files
237 * @param[in] data_file Relative path to the data files from path
238 * @param[in] file_layout (Optional) Layout of file. Defaults to NCHW
Isabella Gottardia4c61882017-11-03 12:11:55 +0000239 *
240 * @return An appropriate tensor accessor
241 */
Georgios Pinitascac13b12018-04-27 19:07:19 +0100242inline std::unique_ptr<graph::ITensorAccessor> get_weights_accessor(const std::string &path,
243 const std::string &data_file,
244 DataLayout file_layout = DataLayout::NCHW)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000245{
246 if(path.empty())
247 {
248 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
249 }
250 else
251 {
Georgios Pinitascac13b12018-04-27 19:07:19 +0100252 return arm_compute::support::cpp14::make_unique<NumPyBinLoader>(path + data_file, file_layout);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000253 }
254}
255
256/** Generates appropriate input accessor according to the specified ppm_path
257 *
258 * @note If ppm_path is empty will generate a DummyAccessor else will generate a PPMAccessor
259 *
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000260 * @param[in] ppm_path Path to PPM file
261 * @param[in] preprocessor Preproccessor object
262 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000263 *
264 * @return An appropriate tensor accessor
265 */
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000266inline std::unique_ptr<graph::ITensorAccessor> get_input_accessor(const std::string &ppm_path,
267 std::unique_ptr<IPreprocessor> preprocessor = nullptr,
268 bool bgr = true)
Isabella Gottardia4c61882017-11-03 12:11:55 +0000269{
270 if(ppm_path.empty())
271 {
272 return arm_compute::support::cpp14::make_unique<DummyAccessor>();
273 }
274 else
275 {
Georgios Pinitas140fdc72018-02-16 11:42:38 +0000276 return arm_compute::support::cpp14::make_unique<PPMAccessor>(ppm_path, bgr, std::move(preprocessor));
Isabella Gottardia4c61882017-11-03 12:11:55 +0000277 }
278}
279
280/** Generates appropriate output accessor according to the specified labels_path
281 *
282 * @note If labels_path is empty will generate a DummyAccessor else will generate a TopNPredictionsAccessor
283 *
284 * @param[in] labels_path Path to labels text file
285 * @param[in] top_n (Optional) Number of output classes to print
286 * @param[out] output_stream (Optional) Output stream
287 *
288 * @return An appropriate tensor accessor
289 */
290inline std::unique_ptr<graph::ITensorAccessor> get_output_accessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout)
291{
292 if(labels_path.empty())
293 {
Anthony Barbiere1a905a2017-12-22 13:53:46 +0000294 return arm_compute::support::cpp14::make_unique<DummyAccessor>(0);
Isabella Gottardia4c61882017-11-03 12:11:55 +0000295 }
296 else
297 {
298 return arm_compute::support::cpp14::make_unique<TopNPredictionsAccessor>(labels_path, top_n, output_stream);
299 }
300}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000301
302/** Utility function to return the TargetHint
303 *
304 * @param[in] target Integer value which expresses the selected target. Must be 0 for NEON or 1 for OpenCL or 2 (OpenCL with Tuner)
305 *
306 * @return the TargetHint
307 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100308inline graph::Target set_target_hint(int target)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000309{
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100310 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 +0100311 if((target == 1 || target == 2))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000312 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100313 return graph::Target::CL;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000314 }
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100315 else if(target == 3)
316 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100317 return graph::Target::GC;
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100318 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000319 else
320 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100321 return graph::Target::NEON;
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000322 }
323}
Michalis Spyroue4720822017-10-02 17:44:52 +0100324} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100325} // namespace arm_compute
326
327#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */