-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn.cpp
349 lines (296 loc) · 11 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include "tensor.hpp"
using namespace std;
//filtersize : 1channelあたりのフィルターの大きさ(3*3filterなら3)
void im2col(Tensor &image, Tensor &expanded, int32_t padsize, int32_t filtersize, int32_t stride) {
int32_t datanum = image.d;
int32_t channel = image.c;
int32_t height = image.h;
int32_t width = image.w;
if(expanded.d != 1 || expanded.c != 1) {
cout << "Tensor size mismatch in img2col" << endl;
return;
}
int32_t exp_idx = 0;
int32_t imgsize3 = image.size / datanum;
int32_t imgsize2 = imgsize3 / channel;
int32_t convoluted1_h = (image.h + padsize*2 - filtersize + 1) / stride;
int32_t convoluted1_w = (image.w + padsize*2 - filtersize + 1) / stride;
int32_t filterspace = image.c * filtersize * filtersize; //フィルターが一回に畳みこむ要素数
int32_t expsize_data = convoluted1_h * convoluted1_w * filterspace; //1dataあたりのexpandedのサイズ
#pragma acc kernels present(image, expanded)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t ih=-padsize; ih <= height+padsize-filtersize; ih+=stride) { //filterに取り込まれる最左上の要素のy座標
for (int32_t iw=-padsize; iw <= width+padsize-filtersize; iw+=stride) { //同上のx座標
for (int32_t c=0; c < channel; c++) {
for (int32_t fh=0; fh < filtersize; fh++) {
for (int32_t fw=0; fw < filtersize; fw++) {
int32_t h_Img = ih + fh;
int32_t w_Img = iw + fw;
int32_t h_conv_times = (ih + padsize) / stride;
int32_t w_conv_times = (iw + padsize) / stride;
exp_idx = d*expsize_data + h_conv_times*convoluted1_w*filterspace + w_conv_times*filterspace + c*filtersize*filtersize + fh*filtersize + fw;
if (h_Img < 0 || w_Img < 0 || h_Img >= height || w_Img >= width) {
expanded[exp_idx] = 0.;
}
else {
expanded[exp_idx] = image[d*imgsize3 + c*imgsize2 + h_Img*width + w_Img];
}
//exp_idx++;
}
}
}
}
}
}
}
void im2col_inverse(Tensor &image, Tensor &expanded, int32_t padsize, int32_t filtersize, int32_t stride) {
int32_t datanum = image.d;
int32_t channel = image.c;
int32_t height = image.h;
int32_t width = image.w;
if(expanded.d != 1 || expanded.c != 1) {
cout << "Tensor size mismatch in img2col" << endl;
return;
}
int32_t exp_idx = 0;
int32_t imgsize3 = image.size / datanum;
int32_t imgsize2 = imgsize3 / channel;
for (int32_t d=0; d < datanum; d++) {
for (int32_t ih=-padsize; ih <= height+padsize-filtersize; ih+=stride) { //filterに取り込まれる最左上の要素のy座標
for (int32_t iw=-padsize; iw <= width+padsize-filtersize; iw+=stride) { //同上のx座標
for (int32_t c=0; c < channel; c++) {
for (int32_t fh=0; fh < filtersize; fh++) {
for (int32_t fw=0; fw < filtersize; fw++) {
int32_t h_Img = ih + fh;
int32_t w_Img = iw + fw;
if (h_Img < 0 || w_Img < 0 || h_Img >= height || w_Img >= width) {
//expanded[exp_idx] = 0.;
}
else {
image[d*imgsize3 + c*imgsize2 + h_Img*width + w_Img] = expanded[exp_idx];
}
exp_idx++;
}
}
}
}
}
}
}
void col2im(Tensor &expanded, Tensor &image) {
int32_t datanum = image.d;
int32_t channel = image.c;
int32_t height = image.h;
int32_t width = image.w;
if(expanded.d != 1 || expanded.c != 1 || expanded.size != image.size) {
cout << "Tensor size mismatch in img2col" << endl;
return;
}
int32_t imgsize3 = image.size / datanum;
int32_t imgsize2 = imgsize3 / channel;
#pragma acc kernels present(expanded, image)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
for (int32_t w=0; w < width; w++) {
image[d*imgsize3 + c*imgsize2 + h*width + w] = expanded[d*imgsize3 + (h*width + w)*channel + c];
}
}
}
}
}
void col2im_inverse(Tensor &expanded, Tensor &image) {
int32_t datanum = image.d;
int32_t channel = image.c;
int32_t height = image.h;
int32_t width = image.w;
if(expanded.d != 1 || expanded.c != 1 || expanded.size != image.size) {
cout << "Tensor size mismatch in img2col" << endl;
return;
}
int32_t imgsize3 = image.size / datanum;
int32_t imgsize2 = imgsize3 / channel;
#pragma acc kernels present(expanded, image)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
for (int32_t w=0; w < width; w++) {
expanded[d*imgsize3 + (h*width + w)*channel + c] = image[d*imgsize3 + c*imgsize2 + h*width + w];
}
}
}
}
}
//pooling用im2col.
//4次元データを、d,c,wの3次元に展開
void im2col_pool(Tensor &image, Tensor &expanded, int32_t filtersize) {
int32_t datanum = image.d;
int32_t channel = image.c;
int32_t height = image.h;
int32_t width = image.w;
if(image.size != expanded.size) {
cout << "Tensor size mismatch in img2col_pool" << endl;
return;
}
int32_t exp_idx = 0;
int32_t imgsize3 = image.size / datanum;
int32_t imgsize2 = imgsize3 / channel;
#pragma acc kernels present(image, expanded)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t c=0; c < channel; c++) {
for (int32_t ih=0; ih <= height-filtersize; ih+=filtersize) { //filterに取り込まれる最左上の要素のy座標
for (int32_t iw=0; iw <= width-filtersize; iw+=filtersize) { //同上のx座標
for (int32_t fh=0; fh < filtersize; fh++) {
for (int32_t fw=0; fw < filtersize; fw++) {
int32_t h_pool_times = ih / filtersize;
int32_t w_pool_times = iw / filtersize;
int32_t h_Img = ih + fh;
int32_t w_Img = iw + fw;
exp_idx = d*imgsize3 + c*imgsize2 + h_pool_times*width*filtersize + w_pool_times*filtersize*filtersize + fh*filtersize + fw;
expanded[exp_idx] = image[d*imgsize3 + c*imgsize2 + h_Img*width + w_Img];
//exp_idx++;
}
}
}
}
}
}
}
void col2im_pool(Tensor &expanded, Tensor &image, int32_t filtersize) {
int32_t datanum = image.d;
int32_t channel = image.c;
int32_t height = image.h;
int32_t width = image.w;
if(image.size != expanded.size) {
cout << "Tensor size mismatch in img2col_pool" << endl;
return;
}
int32_t exp_idx = 0;
int32_t imgsize3 = image.size / datanum;
int32_t imgsize2 = imgsize3 / channel;
#pragma acc kernels present(image, expanded)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t c=0; c < channel; c++) {
for (int32_t ih=0; ih <= height-filtersize; ih+=filtersize) { //filterに取り込まれる最左上の要素のy座標
for (int32_t iw=0; iw <= width-filtersize; iw+=filtersize) { //同上のx座標
for (int32_t fh=0; fh < filtersize; fh++) {
for (int32_t fw=0; fw < filtersize; fw++) {
int32_t h_pool_times = ih / filtersize;
int32_t w_pool_times = iw / filtersize;
int32_t h_Img = ih + fh;
int32_t w_Img = iw + fw;
exp_idx = d*imgsize3 + c*imgsize2 + h_pool_times*width*filtersize + w_pool_times*filtersize*filtersize + fh*filtersize + fw;
image[d*imgsize3 + c*imgsize2 + h_Img*width + w_Img] = expanded[exp_idx];
//exp_idx++;
}
}
}
}
}
}
}
void back_pooling(Tensor &d_before_pool, Tensor &d_pooled, IntTensor &pooled_idx, int32_t filtersize) {
if (d_before_pool.d != d_pooled.d || d_pooled.d != pooled_idx.d || d_before_pool.c != d_pooled.c || d_pooled.c != pooled_idx.c || d_before_pool.h != d_pooled.h || d_pooled.h != pooled_idx.h || d_before_pool.w != filtersize*filtersize || d_pooled.w != 1 || pooled_idx.w != 1) {
cout << "Tensor size mismatch in back_pooling" << endl;
return;
}
int32_t datanum = d_before_pool.d;
int32_t channel = d_before_pool.c;
int32_t height = d_before_pool.h; //widthだけはd_before_poolとpooledで異なる
int32_t bpsize3 = d_before_pool.size / d_before_pool.d;
int32_t bpsize2 = bpsize3 / d_before_pool.c;
int32_t poolsize3 = d_pooled.size / d_pooled.d;
int32_t poolsize2 = poolsize3 / d_pooled.c;
#pragma acc kernels present(image, expanded)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
int32_t max_idx = pooled_idx[d*channel*height + c*height + h];
for (int32_t w=0; w < d_before_pool.w; w++) {
if (w == max_idx) {
d_before_pool[d*bpsize3 + c*bpsize2 + h*d_before_pool.w + w] = d_pooled[d*channel*height + c*height + h];
}
else {
d_before_pool[d*bpsize3 + c*bpsize2 + h*d_before_pool.w + w] = 0.;
}
}
}
}
}
}
//expandedのw方向のmaxを取ってpooledにする
void pooling(Tensor &expanded, Tensor &pooled, IntTensor &pooled_idx, int32_t filtersize) {
if (expanded.d != pooled.d || pooled.d != pooled_idx.d || expanded.c != pooled.c || pooled.c != pooled_idx.c || expanded.h != pooled.h || pooled.h != pooled_idx.h || expanded.w != filtersize*filtersize || pooled.w != 1 || pooled_idx.w != 1) {
cout << "Tensor size mismatch in pooling" << endl;
return;
}
int32_t datanum = expanded.d;
int32_t channel = expanded.c;
int32_t height = expanded.h; //widthだけはexpandedとpooledで異なる
int32_t expsize3 = expanded.size / expanded.d;
int32_t expsize2 = expsize3 / expanded.c;
int32_t poolsize3 = pooled.size / pooled.d;
int32_t poolsize2 = poolsize3 / pooled.c;
#pragma acc kernels present(expanded, pooled, pooled_idx)
#pragma acc loop independent gang
for (int32_t d=0; d < datanum; d++) {
#pragma acc loop independent vector
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
float max = -9999.;
int32_t max_idx = -1;
for (int32_t w=0; w < expanded.w; w++) {
if (max < expanded[d*expsize3 + c*expsize2 + h*expanded.w + w]) {
max = expanded[d*expsize3 + c*expsize2 + h*expanded.w + w];
max_idx = w;
}
}
//if (max_idx == -1) {
//cout << "error in pooling!" << endl;
//return;
//}
pooled[d*poolsize3 + c*poolsize2 + h] = max;
pooled_idx[d*poolsize3 + c*poolsize2 + h] = max_idx; //maxとして選ばれたもののw方向のidxを記録
}
}
}
}
//int main(){
//int32_t datanum = 2;
//int32_t channel = 3;
//int32_t width = 3;
//int32_t height = 3;
//int32_t padsize = 1;
//int32_t filtersize = 3;
//int32_t stride = 1;
//Tensor a(datanum, channel, height, width);
//for (int32_t i=0; i < a.size; i++) {
//a[i] = (float)i;
//}
////a.Print();
//cout << endl;
//Tensor b(1, 1, ((width+padsize*2-filtersize+1) / stride)*((height+padsize*2-filtersize+1) / stride)*datanum, filtersize*filtersize*channel);
//im2col(a, b, padsize, filtersize, stride);
////b.Print();
//Tensor c(1,1,datanum*height*width,channel);
//for (int32_t i=0; i < c.size; i++) {
//c[i] = (float)i;
//}
//Tensor d(datanum, channel, height, width);
//col2im(c, d);
//d.Print();
//return 0;
//}