blob: 39b3f115bd86c33a72bacccfd65fdddccbe83e36 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
2 * Copyright (c) 2017 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_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"
Anthony Barbier2a07e182017-08-04 18:20:27 +010028#include "arm_compute/graph/ITensorAccessor.h"
29#include "arm_compute/graph/Types.h"
30
Michalis Spyrou53b405f2017-09-27 15:55:31 +010031#include <random>
Gian Marco44ec2e72017-10-19 14:13:38 +010032#include <string>
33#include <vector>
Michalis Spyrou53b405f2017-09-27 15:55:31 +010034
Anthony Barbier2a07e182017-08-04 18:20:27 +010035namespace arm_compute
36{
37namespace graph_utils
38{
39/** PPM writer class */
40class PPMWriter : public graph::ITensorAccessor
41{
42public:
43 /** Constructor
44 *
45 * @param[in] name PPM file name
46 * @param[in] maximum Maximum elements to access
47 */
48 PPMWriter(std::string name, unsigned int maximum = 1);
49 /** Allows instances to move constructed */
50 PPMWriter(PPMWriter &&) = default;
51
52 // Inherited methods overriden:
53 bool access_tensor(ITensor &tensor) override;
54
55private:
56 const std::string _name;
57 unsigned int _iterator;
58 unsigned int _maximum;
59};
60
61/** Dummy accessor class */
Michalis Spyrou53b405f2017-09-27 15:55:31 +010062class DummyAccessor final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +010063{
64public:
65 /** Constructor
66 *
67 * @param[in] maximum Maximum elements to write
68 */
69 DummyAccessor(unsigned int maximum = 1);
70 /** Allows instances to move constructed */
71 DummyAccessor(DummyAccessor &&) = default;
72
73 // Inherited methods overriden:
74 bool access_tensor(ITensor &tensor) override;
75
76private:
77 unsigned int _iterator;
78 unsigned int _maximum;
79};
80
Gian Marco44ec2e72017-10-19 14:13:38 +010081/** PPM accessor class */
82class PPMAccessor final : public graph::ITensorAccessor
83{
84public:
85 /** Constructor
86 *
87 * @param[in] ppm_path Path to PPM file
88 * @param[in] bgr (Optional) Fill the first plane with blue channel (default = false)
89 * @param[in] mean_r (Optional) Red mean value to be subtracted from red channel
90 * @param[in] mean_g (Optional) Green mean value to be subtracted from green channel
91 * @param[in] mean_b (Optional) Blue mean value to be subtracted from blue channel
92 */
93 PPMAccessor(const std::string &ppm_path, bool bgr = true, float mean_r = 0.0f, float mean_g = 0.0f, float mean_b = 0.0f);
94 /** Allow instances of this class to be move constructed */
95 PPMAccessor(PPMAccessor &&) = default;
96
97 // Inherited methods overriden:
98 bool access_tensor(ITensor &tensor) override;
99
100private:
101 const std::string &_ppm_path;
102 const bool _bgr;
103 const float _mean_r;
104 const float _mean_g;
105 const float _mean_b;
106};
107
108/** Result accessor class */
109class TopNPredictionsAccessor final : public graph::ITensorAccessor
110{
111public:
112 /** Constructor
113 *
114 * @param[in] labels_path Path to labels text file.
115 * @param[in] top_n (Optional) Number of output classes to print
116 * @param[out] output_stream (Optional) Output stream
117 */
118 TopNPredictionsAccessor(const std::string &labels_path, size_t top_n = 5, std::ostream &output_stream = std::cout);
119 /** Allow instances of this class to be move constructed */
120 TopNPredictionsAccessor(TopNPredictionsAccessor &&) = default;
121 /** Prevent instances of this class from being copied (As this class contains pointers) */
122 TopNPredictionsAccessor(const TopNPredictionsAccessor &) = delete;
123 /** Prevent instances of this class from being copied (As this class contains pointers) */
124 TopNPredictionsAccessor &operator=(const TopNPredictionsAccessor &) = delete;
125
126 // Inherited methods overriden:
127 bool access_tensor(ITensor &tensor) override;
128
129private:
130 std::vector<std::string> _labels;
131 std::ostream &_output_stream;
132 size_t _top_n;
133};
134
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100135/** Random accessor class */
136class RandomAccessor final : public graph::ITensorAccessor
137{
138public:
139 /** Constructor
140 *
141 * @param[in] lower Lower bound value.
142 * @param[in] upper Upper bound value.
143 * @param[in] seed (Optional) Seed used to initialise the random number generator.
144 */
145 RandomAccessor(PixelValue lower, PixelValue upper, const std::random_device::result_type seed = 0);
146 /** Allows instances to move constructed */
147 RandomAccessor(RandomAccessor &&) = default;
148
149 // Inherited methods overriden:
150 bool access_tensor(ITensor &tensor) override;
151
152private:
153 template <typename T, typename D>
154 void fill(ITensor &tensor, D &&distribution);
155 PixelValue _lower;
156 PixelValue _upper;
157 std::random_device::result_type _seed;
158};
159
Anthony Barbier2a07e182017-08-04 18:20:27 +0100160/** Numpy Binary loader class*/
Michalis Spyrou53b405f2017-09-27 15:55:31 +0100161class NumPyBinLoader final : public graph::ITensorAccessor
Anthony Barbier2a07e182017-08-04 18:20:27 +0100162{
163public:
164 /** Default Constructor
165 *
166 * @param filename Binary file name
167 */
168 NumPyBinLoader(std::string filename);
169 /** Allows instances to move constructed */
170 NumPyBinLoader(NumPyBinLoader &&) = default;
171
172 // Inherited methods overriden:
173 bool access_tensor(ITensor &tensor) override;
174
175private:
176 const std::string _filename;
177};
Michalis Spyroue4720822017-10-02 17:44:52 +0100178} // namespace graph_utils
Anthony Barbier2a07e182017-08-04 18:20:27 +0100179} // namespace arm_compute
180
181#endif /* __ARM_COMPUTE_GRAPH_UTILS_H__ */