-
Notifications
You must be signed in to change notification settings - Fork 5
/
cnn.cpp
290 lines (262 loc) · 4.31 KB
/
cnn.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "cnn.h"
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
cnn::cnn()
{
}
cnn::~cnn()
{
}
bool cnn::getImg(Mat matImg)
{
if(matImg.empty())
{
return false;
}
else
{
Mat_<uchar>::iterator it = matImg.begin<uchar>();
Mat_<uchar>::iterator end = matImg.end<uchar>();
for(int i=0; it != end; ++it,++i)
{
*(&img[0][0]+i) = static_cast<double>(*it) / 255.0;
}
return true;
}
}
bool cnn::loadModel(string filename)
{
ifstream infile(filename.c_str());
if(!infile.is_open())
{
return false;
}
//conv 1 weight 16*1*5*5
for(int i=0; i<16; ++i)
{
for(int j=0; j<1; ++j)
{
for(int m=0; m<5; ++m)
{
for(int n=0; n<5; ++n)
{
infile>>conv1Weight[i][j][m][n];
}
}
}
}
//conv 1 bias 16
for(int i=0; i<16; ++i)
{
infile>>conv1Bias[i];
}
//conv 4 weight 32*16*5*5
for(int i=0; i<32; ++i)
{
for(int j=0; j<16; ++j)
{
for(int m=0; m<5; ++m)
{
for(int n=0; n<5; ++n)
{
infile>>conv4Weight[i][j][m][n];
}
}
}
}
//conv 4 bias 32
for(int i=0; i<32; ++i)
{
infile>>conv4Bias[i];
}
//linear 8 weight 256*800
for(int i=0; i<256; ++i)
{
for(int j=0; j<800; ++j)
{
infile>>linear8Weight[i][j];
}
}
//linear 8 bias 256
for(int i=0; i<256; ++i)
{
infile>>linear8Bias[i];
}
//linear 10 weight 43*256
for(int i=0; i<43; ++i)
{
for(int j=0; j<256; ++j)
{
infile>>linear10Weight[i][j];
}
}
//linear 10 bias 43
for(int i=0; i<43; ++i)
{
infile>>linear10Bias[i];
}
//cout<<setprecision(16)<<linear10Bias[0]<<endl;
return true;
}
//conv1 and tanh2
void cnn::forward12()
{
for(int t=0; t<16; ++t)//layer to
{
for(int i=0; i<28; ++i)//to image row
{
for(int j=0; j<28; ++j)//to image col
{
conv1tanh2[t][i][j] = 0;
for(int f=0; f<1; ++f)//layer from
{
for(int m=0; m<5; ++m)//filter row
{
for(int n=0; n<5; ++n)//filter col
{
conv1tanh2[t][i][j] += img[i+m][j+n] * conv1Weight[t][f][m][n];
}
}
}
conv1tanh2[t][i][j] += conv1Bias[t];
conv1tanh2[t][i][j] = tanh(conv1tanh2[t][i][j]);
}
}
}
}
//pooling3
void cnn::forward3()
{
for(int t=0; t<16; ++t)
{
for(int i=0; i<14; ++i)
{
for(int j=0; j<14; ++j)
{
pooling3[t][i][j] = 0;
for(int m=0; m<2; ++m)
{
for(int n=0; n<2; ++n)
{
pooling3[t][i][j] += pow(conv1tanh2[t][2*i+m][2*j+n], 2.0);
}
}
pooling3[t][i][j] = sqrt(pooling3[t][i][j]);
}
}
}
}
//conv4 and tanh5
void cnn::forward45()
{
for(int t=0; t<32; ++t)
{
for(int i=0; i<10; ++i)
{
for(int j=0; j<10; ++j)
{
conv4tanh5[t][i][j] = 0;
for(int f=0; f<16; ++f)
{
for(int m=0; m<5; ++m)
{
for(int n=0; n<5; ++n)
{
conv4tanh5[t][i][j] += pooling3[f][i+m][j+n] * conv4Weight[t][f][m][n];
}
}
}
conv4tanh5[t][i][j] += conv4Bias[t];
conv4tanh5[t][i][j] = tanh(conv4tanh5[t][i][j]);
}
}
}
}
//pooling6
void cnn::forward6()
{
for(int t=0; t<32; ++t)
{
for(int i=0; i<5; ++i)
{
for(int j=0; j<5; ++j)
{
pooling6[t][i][j] = 0;
for(int m=0; m<2; ++m)
{
for(int n=0; n<2; ++n)
{
pooling6[t][i][j] += pow(conv4tanh5[t][2*i+m][2*j+n], 2.0);
}
}
pooling6[t][i][j] = sqrt(pooling6[t][i][j]);
}
}
}
}
//reshape 7
void cnn::forward7()
{
int idx = 0;
for(int t=0; t<32; ++t)
{
for(int i=0; i<5; ++i)
{
for(int j=0; j<5; ++j)
{
reshape7[idx++] = pooling6[t][i][j];
}
}
}
}
//linear8 and tanh9
void cnn::forward89()
{
for(int t=0; t<256; ++t)// to layer
{
linear8tanh9[t] = 0;
for(int f=0; f<800; ++f)// from layer
{
linear8tanh9[t] += reshape7[f] * linear8Weight[t][f];
}
linear8tanh9[t] += linear8Bias[t];
linear8tanh9[t] = tanh(linear8tanh9[t]);
}
}
//linear10
void cnn::forward10()
{
for(int t=0; t<43; ++t)
{
linear10[t] = 0;
for(int f=0; f<256; ++f)
{
linear10[t] += linear8tanh9[f] * linear10Weight[t][f];
}
linear10[t] += linear10Bias[t];
}
}
int cnn::forward()
{
forward12();
forward3();
forward45();
forward6();
forward7();
forward89();
forward10();
double maxVal = linear10[0];
int maxIdx = 0;
for(int i=0; i<43; ++i)
{
if(linear10[i] > maxVal)
{
maxVal = linear10[i];
maxIdx = i;
}
}
classLabel = maxIdx + 1;//because the index of the label data is from 1
return classLabel;
}