-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.cpp
243 lines (191 loc) · 7.76 KB
/
conv.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <iomanip>
#include <Halide.h>
using Halide::Image;
#include "image_io.h"
#include "clock.h"
// Constants
const int NUM_IMAGES = 2000;
const int IMAGE_SIZE = 28;
const int REDUCE_IMAGE_SIZE = 4;
const int FILTER_SIZE = 5;
const int POOL_SIZE = 2;
const int LAYER0_NODES = 1;
const int LAYER1_NODES = 20;
const int LAYER2_NODES = 50;
const int LAYER3_NODES = 500;
const int LAYER4_NODES = 10;
const int VECTORS = 4;
Halide::Func convolution_layer(Halide::Func input, Halide::Func weights,
Halide::Func bias, int filter_size, int input_layers, int pool_size) {
// Convolution
Halide::Func convolution;
Halide::Var x, y, z, w;
Halide::RDom r(0, filter_size, 0, filter_size, 0, input_layers);
convolution(x, y, z, w) = 0.0f;
convolution(x, y, z, w) += weights(r.x, r.y, r.z, z) *
input(x + r.x, y + r.y, r.z, w);
// Max pool
Halide::Func subsample;
Halide::RDom s(0, pool_size, 0, pool_size);
subsample(x, y, z, w) = 0.0f;
subsample(x, y, z, w) = Halide::max(convolution(pool_size * x + s.x,
pool_size * y + s.y, z, w), subsample(x, y, z, w));
// Non-linear bias
Halide::Func biased;
biased(x, y, z, w) = tanh(subsample(x, y, z, w) + bias(z, 0));
Halide::Var x_inner, x_outer, y_inner, y_outer;
biased.parallel(w);
biased.tile(x, y, x_outer, y_outer, x_inner, y_inner, VECTORS, 2);
biased.vectorize(x_inner);
biased.unroll(y_inner);
return biased;
}
Halide::Func flatten(Halide::Func input, int image_size) {
Halide::Func flatten1, flatten2;
Halide::Var x, y, z;
flatten1(x, y, z) = input(x / image_size, x % image_size, y, z);
// Only y = 0 should be used
int full_size = image_size * image_size;
flatten2(x, y, z) = flatten1(x % full_size, x / full_size, z);
flatten2.parallel(z);
flatten2.vectorize(x, VECTORS);
return flatten2;
}
Halide::Func fully_connected_layer(Halide::Func input, Halide::Func weights,
Halide::Func bias, int size) {
Halide::Func product;
Halide::Var x, y, z;
Halide::RDom r(0, size);
// Only y = 0 should be used
product(x, y, z) = 0.0f;
product(x, y, z) += weights(r.x, x) * input(r.x, y, z);
product(x, y, z) = tanh(product(x, y, z) + bias(x, 0));
product.vectorize(x, VECTORS);
return product;
}
Halide::Func classification(Halide::Func input, int size) {
Halide::Func softmax;
Halide::Var x, y, z;
softmax(x, y, z) = exp(input(x, y, z)); // Ignore normalization
Halide::Func classification;
Halide::RDom r(0, size);
classification(x, y, z) = Halide::argmax(softmax(r.x, 0, z))[0];
return classification;
}
void classify(Halide::Func layer0, Halide::Func *weights,
Halide::Func *bias) {
// Layer 1 -- Convolution
Halide::Func layer1 = convolution_layer(layer0, weights[0], bias[0],
FILTER_SIZE, LAYER0_NODES, POOL_SIZE);
// Layer 2 -- Convolution
Halide::Func layer2 = convolution_layer(layer1, weights[1], bias[1],
FILTER_SIZE, LAYER1_NODES, POOL_SIZE);
// Flatten many feature maps onto a single level for future layers
Halide::Func flattened = flatten(layer2, REDUCE_IMAGE_SIZE);
// Layer 3 -- Fully connected hidden layer
Halide::Func layer3 = fully_connected_layer(flattened, weights[2],
bias[2], LAYER2_NODES * REDUCE_IMAGE_SIZE * REDUCE_IMAGE_SIZE);
// Layer 4 -- Fully connected hidden layer
Halide::Func layer4 = fully_connected_layer(layer3, weights[3],
bias[3], LAYER3_NODES);
// Layer 5 -- Logostic Softmax / classification
Halide::Func layer5 = classification(layer4, LAYER4_NODES);
layer0.compute_root();
layer1.compute_root();
layer2.compute_root();
flattened.compute_root();
layer3.compute_root();
layer4.compute_root();
// Realize to perform computation
Halide::Image<int> output(1, 1, NUM_IMAGES);
layer5.realize(output);
}
int main(int argc, char **argv) {
// Load weight images
// Weights are stored in Image<T> types with dimensions:
// row value, column value, input layer number, output layer number
Halide::Image<float> layer0_weights_image(FILTER_SIZE, FILTER_SIZE,
LAYER0_NODES, LAYER1_NODES);
for (int i = 0; i < LAYER0_NODES; i++) {
for (int j = 0; j < LAYER1_NODES; j++) {
std::string filename = "res/l0w" + std::to_string(j) + ".png";
filename = "res/l0w0.png";
Halide::Image<float> weight = load<float>(filename);
for (int x = 0; x < FILTER_SIZE; x ++) {
for (int y = 0; y < FILTER_SIZE; y++) {
layer0_weights_image(x, y, i, j) =
(255 * weight(y, x, 0) - 127) / 20.0f;
}
}
}
}
Halide::Image<float> layer1_weights_image(FILTER_SIZE, FILTER_SIZE,
LAYER1_NODES, LAYER2_NODES);
for (int i = 0; i < LAYER1_NODES; i++) {
for (int j = 0; j < LAYER2_NODES; j++) {
std::string filename =
"res/l1w" + std::to_string(LAYER1_NODES * i + j) + ".png";
filename = "res/l0w0.png";
Halide::Image<float> weight = load<float>(filename);
for (int x = 0; x < FILTER_SIZE; x ++) {
for (int y = 0; y < FILTER_SIZE; y++) {
layer1_weights_image(x, y, i, j) =
(255 * weight(y, x, 0) - 127) / 20.0f;
}
}
}
}
// Load weight functions
Halide::Var x, y, z, w;
Halide::Func layer0_weights;
Halide::Func layer1_weights;
layer0_weights(x, y, z, w) = layer0_weights_image(x, y, z, w);
layer1_weights(x, y, z, w) = layer1_weights_image(x, y, z, w);
Halide::Image<float> layer2_weight_input = load<float>("res/l2w.png");
Halide::Func layer2_weights;
layer2_weights(x, y) =
(255 * layer2_weight_input(y, x, 0) - 127) / 20.0f;
Halide::Image<float> layer3_weight_input = load<float>("res/l3w.png");
Halide::Func layer3_weights;
layer3_weights(x, y) =
(255 * layer3_weight_input(y, x, 0) - 127) / 20.0f;
// Load biases
Halide::Image<float> layer0_bias_input = load<float>("res/l0b.png");
Halide::Func layer0_bias;
layer0_bias(x, y) = (255*layer0_bias_input(x, y, 0) - 127) / 20.0f;
Halide::Image<float> layer1_bias_input = load<float>("res/l1b.png");
Halide::Func layer1_bias;
layer1_bias(x, y) = (255*layer1_bias_input(x, y, 0) - 127) / 20.0f;
Halide::Image<float> layer2_bias_input = load<float>("res/l2b.png");
Halide::Func layer2_bias;
layer2_bias(x, y) = (255*layer2_bias_input(x, y, 0) - 127) / 20.0f;
Halide::Image<float> layer3_bias_input = load<float>("res/l3b.png");
Halide::Func layer3_bias;
layer3_bias(x, y) = (255*layer3_bias_input(x, y, 0) - 127) / 20.0f;
Halide::Func weights[4] = {layer0_weights, layer1_weights,
layer2_weights, layer3_weights};
Halide::Func bias[4] = {layer0_bias, layer1_bias, layer2_bias,
layer3_bias};
// Load large tiled image for batch classifiation
Halide::Func layer0;
Halide::Image<uint8_t> input(IMAGE_SIZE, IMAGE_SIZE, 1, NUM_IMAGES);
for (int i = 0; i < NUM_IMAGES; i++) {
//std::string filename = "mnist/1-" + std::to_string(i) + ".png";
Halide::Image<uint8_t> image = load<uint8_t>("test.png");
for (int x = 0; x < IMAGE_SIZE; x++) {
for (int y = 0; y < IMAGE_SIZE; y++) {
input(x, y, 0, i) = image(y, x, 0);
}
}
}
Halide::Expr casted = Halide::cast<float>(input(x, y, z, w)) / 255.0f;
layer0(x, y, z, w) = casted;
double start = current_time();
classify(layer0, weights, bias);
double time = current_time() - start;
std::cout << "Time: " << time << std::endl;
}