aboutsummaryrefslogtreecommitdiff
path: root/tests/networks/LeNet5Network.h
blob: a46489f88cd3eb7623accab65d69717730bc3141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __ARM_COMPUTE_TEST_MODEL_OBJECTS_LENET5_H__
#define __ARM_COMPUTE_TEST_MODEL_OBJECTS_LENET5_H__

#include "tests/AssetsLibrary.h"
#include "tests/Globals.h"
#include "tests/Utils.h"

#include <memory>

using namespace arm_compute;
using namespace arm_compute::test;

namespace arm_compute
{
namespace test
{
namespace networks
{
/** Lenet5 model object */
template <typename TensorType,
          typename Accessor,
          typename ActivationLayerFunction,
          typename ConvolutionLayerFunction,
          typename FullyConnectedLayerFunction,
          typename PoolingLayerFunction,
          typename SoftmaxLayerFunction>
class LeNet5Network
{
public:
    void init(int batches)
    {
        _batches = batches;

        // Initialize input, output, weights and biases
        input.allocator()->init(TensorInfo(TensorShape(28U, 28U, 1U, _batches), 1, DataType::F32));
        output.allocator()->init(TensorInfo(TensorShape(10U, _batches), 1, DataType::F32));
        w[0].allocator()->init(TensorInfo(TensorShape(5U, 5U, 1U, 20U), 1, DataType::F32));
        b[0].allocator()->init(TensorInfo(TensorShape(20U), 1, DataType::F32));
        w[1].allocator()->init(TensorInfo(TensorShape(5U, 5U, 20U, 50U), 1, DataType::F32));
        b[1].allocator()->init(TensorInfo(TensorShape(50U), 1, DataType::F32));
        w[2].allocator()->init(TensorInfo(TensorShape(800U, 500U), 1, DataType::F32));
        b[2].allocator()->init(TensorInfo(TensorShape(500U), 1, DataType::F32));
        w[3].allocator()->init(TensorInfo(TensorShape(500U, 10U), 1, DataType::F32));
        b[3].allocator()->init(TensorInfo(TensorShape(10U), 1, DataType::F32));
    }

    /** Build the model. */
    void build()
    {
        // Initialize intermediate tensors
        // Layer 1
        conv1_out.allocator()->init(TensorInfo(TensorShape(24U, 24U, 20U, _batches), 1, DataType::F32));
        pool1_out.allocator()->init(TensorInfo(TensorShape(12U, 12U, 20U, _batches), 1, DataType::F32));
        // Layer 2
        conv2_out.allocator()->init(TensorInfo(TensorShape(8U, 8U, 50U, _batches), 1, DataType::F32));
        pool2_out.allocator()->init(TensorInfo(TensorShape(4U, 4U, 50U, _batches), 1, DataType::F32));
        // Layer 3
        fc1_out.allocator()->init(TensorInfo(TensorShape(500U, _batches), 1, DataType::F32));
        act1_out.allocator()->init(TensorInfo(TensorShape(500U, _batches), 1, DataType::F32));
        // Layer 6
        fc2_out.allocator()->init(TensorInfo(TensorShape(10U, _batches), 1, DataType::F32));

        // Configure Layers
        conv1.configure(&input, &w[0], &b[0], &conv1_out, PadStrideInfo(1, 1, 0, 0));
        pool1.configure(&conv1_out, &pool1_out, PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)));
        conv2.configure(&pool1_out, &w[1], &b[1], &conv2_out, PadStrideInfo(1, 1, 0, 0));
        pool2.configure(&conv2_out, &pool2_out, PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2, 2, 0, 0)));
        fc1.configure(&pool2_out, &w[2], &b[2], &fc1_out);
        act1.configure(&fc1_out, &act1_out, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
        fc2.configure(&act1_out, &w[3], &b[3], &fc2_out);
        smx.configure(&fc2_out, &output);
    }

    void allocate()
    {
        // Allocate tensors
        input.allocator()->allocate();
        output.allocator()->allocate();
        for(auto &wi : w)
        {
            wi.allocator()->allocate();
        }
        for(auto &bi : b)
        {
            bi.allocator()->allocate();
        }
        conv1_out.allocator()->allocate();
        pool1_out.allocator()->allocate();
        conv2_out.allocator()->allocate();
        pool2_out.allocator()->allocate();
        fc1_out.allocator()->allocate();
        act1_out.allocator()->allocate();
        fc2_out.allocator()->allocate();
    }

    /** Fills the trainable parameters and input with random data. */
    void fill_random()
    {
        std::uniform_real_distribution<> distribution(-1, 1);
        library->fill(Accessor(input), distribution, 0);
        for(unsigned int i = 0; i < w.size(); ++i)
        {
            library->fill(Accessor(w[i]), distribution, i + 1);
            library->fill(Accessor(b[i]), distribution, i + 10);
        }
    }

    /** Fills the trainable parameters from binary files
     *
     * @param weights Files names containing the weights data
     * @param biases  Files names containing the bias data
     */
    void fill(std::vector<std::string> weights, std::vector<std::string> biases)
    {
        ARM_COMPUTE_ERROR_ON(weights.size() != w.size());
        ARM_COMPUTE_ERROR_ON(biases.size() != b.size());

        for(unsigned int i = 0; i < weights.size(); ++i)
        {
            library->fill_layer_data(Accessor(w[i]), weights[i]);
            library->fill_layer_data(Accessor(b[i]), biases[i]);
        }
    }

    /** Feed input to network from file.
     *
     * @param name File name of containing the input data.
     */
    void feed(std::string name)
    {
        library->fill_layer_data(Accessor(input), name);
    }

    /** Get the classification results.
     *
     * @return Vector containing the classified labels
     */
    std::vector<unsigned int> get_classifications()
    {
        std::vector<unsigned int> classified_labels;
        Accessor                  output_accessor(output);

        Window window;
        window.set(Window::DimX, Window::Dimension(0, 1, 1));
        for(unsigned int d = 1; d < output_accessor.shape().num_dimensions(); ++d)
        {
            window.set(d, Window::Dimension(0, output_accessor.shape()[d], 1));
        }

        execute_window_loop(window, [&](const Coordinates & id)
        {
            int               max_idx = 0;
            float             val     = 0;
            const void *const out_ptr = output_accessor(id);
            for(unsigned int l = 0; l < output_accessor.shape().x(); ++l)
            {
                float curr_val = reinterpret_cast<const float *>(out_ptr)[l];
                if(curr_val > val)
                {
                    max_idx = l;
                    val     = curr_val;
                }
            }
            classified_labels.push_back(max_idx);
        });
        return classified_labels;
    }

    /** Clear all allocated memory from the tensor objects */
    void clear()
    {
        input.allocator()->free();
        output.allocator()->free();
        for(auto &wi : w)
        {
            wi.allocator()->free();
        }
        for(auto &bi : b)
        {
            bi.allocator()->free();
        }

        conv1_out.allocator()->free();
        pool1_out.allocator()->free();
        conv2_out.allocator()->free();
        pool2_out.allocator()->free();
        fc1_out.allocator()->free();
        act1_out.allocator()->free();
        fc2_out.allocator()->free();
    }

    /** Runs the model */
    void run()
    {
        // Layer 1
        conv1.run();
        pool1.run();
        // Layer 2
        conv2.run();
        pool2.run();
        // Layer 3
        fc1.run();
        act1.run();
        // Layer 4
        fc2.run();
        // Softmax
        smx.run();
    }

    /** Sync the results */
    void sync()
    {
        sync_if_necessary<TensorType>();
        sync_tensor_if_necessary<TensorType>(output);
    }

private:
    unsigned int _batches{ 0 };

    ActivationLayerFunction     act1{};
    ConvolutionLayerFunction    conv1{}, conv2{};
    FullyConnectedLayerFunction fc1{}, fc2{};
    PoolingLayerFunction        pool1{}, pool2{};
    SoftmaxLayerFunction        smx{};

    TensorType input{}, output{};
    std::array<TensorType, 4> w{ {} }, b{ {} };

    TensorType conv1_out{}, pool1_out{};
    TensorType conv2_out{}, pool2_out{};
    TensorType fc1_out{}, act1_out{};
    TensorType fc2_out{};
};
} // namespace networks
} // namespace test
} // namespace arm_compute
#endif //__ARM_COMPUTE_TEST_MODEL_OBJECTS_LENET5_H__