-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPoolingLayer.cpp
More file actions
203 lines (189 loc) · 7.04 KB
/
Copy pathMaxPoolingLayer.cpp
File metadata and controls
203 lines (189 loc) · 7.04 KB
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
#include "DeepLearning.h"
MaxPoolingLayer::MaxPoolingLayer(int id,int ih,int iw,int kd,int kh,int kw,int st,int ty){
inpDepth = id;
inpHeight = ih;
inpWidth = iw;
kerDepth = kd;
kerHeight = kh;
kerWidth = kw;
if(inpDepth != kerDepth){
fprintf(fpDebug,"Bad inpDepth or kerDepth chosen in MaxPoolingLayer.\n");
exit(0);
}
stride = st;
type = ty;
if(type != 1 && type != 2){
fprintf(fpDebug,"Bad type chosen in MaxPoolingLayer.\n");
exit(0);
}
if((inpHeight - kerHeight) % stride != 0){
fprintf(fpDebug,"Bad kerHeight or stride chosen in MaxPoolingLayer.\n");
exit(0);
}
if((inpWidth - kerWidth) % stride != 0){
fprintf(fpDebug,"Bad kerWidth or stride chosen in MaxPoolingLayer.\n");
exit(0);
}
outDepth = inpDepth;
outHeight = (inpHeight - kerHeight) / stride + 1;
outWidth = (inpWidth - kerWidth) / stride + 1;
for(int i = 0;i < batchSize;i++){
ThirdArray tmp(outDepth, outHeight, outWidth);
output.push_back(tmp);
place.push_back(tmp);
}
for(int i = 0;i < batchSize;i++){
ThirdArray tmp(inpDepth, inpHeight, inpWidth);
error.push_back(tmp);
}
return;
}
void MaxPoolingLayer::forward(const vector<ThirdArray>& inp){
for(int n = 0;n < batchSize;n++){
output[n] = 0;
place[n] = 0;
for(int i = 0;i < outDepth;i++){
for(int j = 0;j < outHeight;j++){
for(int k = 0;k < outWidth;k++){
if(type == 1){
MaxPoolingForward(n,inp[n],i,j,k);
}
else{
AvgPoolingForward(n,inp[n],i,j,k);
}
}
}
}
//inp[n].PrintArray(fpDebug);
//place[n].PrintArray(fpDebug);
}
return;
}
void MaxPoolingLayer::MaxPoolingForward(int tar,const ThirdArray& inp,int d,int h,int w){
int x = 0;
int y = 0;
output[tar].thiArr[d].arr[h][w] = - maxStep;
for(int i = 0;i < kerHeight;i++){
for(int j = 0;j < kerWidth;j++){
x = h * stride + i;
y = w * stride + j;
if(x < 0 || x >= inpHeight){
printf("Unable to compute MaxPoolingForward.\n");
fprintf(fpDebug, "Unable to compute MaxPoolingForward.\n");
fprintf(fpDebug, "i = %d, j = %d\n",i,j);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
if(y < 0 || y >= inpWidth){
printf("Unable to compute MaxPoolingForward.\n");
fprintf(fpDebug, "Unable to compute MaxPoolingForward.\n");
fprintf(fpDebug, "i = %d, j = %d\n",i,j);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
if(output[tar].thiArr[d].arr[h][w] < inp.thiArr[d].arr[x][y]){
output[tar].thiArr[d].arr[h][w] = inp.thiArr[d].arr[x][y];
place[tar].thiArr[d].arr[h][w] = i * kerWidth + j;
}
}
}
return;
}
void MaxPoolingLayer::AvgPoolingForward(int tar,const ThirdArray& inp,int d,int h,int w){
int coff = kerHeight * kerWidth;
for(int i = 0;i < kerHeight;i++){
for(int j = 0;j < kerWidth;j++){
int x = h * stride + i;
int y = w * stride + j;
if(x < 0 || x >= inpHeight){
printf("Unable to compute AvgPoolingForward.\n");
fprintf(fpDebug, "Unable to compute AvgPoolingForward.\n");
fprintf(fpDebug, "i = %d, j = %d\n",i,j);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
if(y < 0 || y >= inpWidth){
printf("Unable to compute AvgPoolingForward.\n");
fprintf(fpDebug, "Unable to compute AvgPoolingForward.\n");
fprintf(fpDebug, "i = %d, j = %d\n",i,j);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
output[tar].thiArr[d].arr[h][w] += (inp.thiArr[d].arr[x][y] / coff);
}
}
return;
}
void MaxPoolingLayer::backward(const vector<ThirdArray>& err){
for(int n = 0;n < batchSize;n++){
error[n] = 0;
for(int i = 0;i < inpDepth;i++){
for(int j = 0;j < outHeight;j++){
for(int k = 0;k < outWidth;k++){
if(type == 1){
MaxPoolingBackward(n,err[n],i,j,k);
}
else{
AvgPoolingBackward(n,err[n],i,j,k);
}
}
}
}
}
return;
}
void MaxPoolingLayer::MaxPoolingBackward(int tar,const ThirdArray& err,int d,int h,int w){
int dx = place[tar].thiArr[d].arr[h][w] / kerWidth;
int dy = (int)place[tar].thiArr[d].arr[h][w] % kerWidth;
int x = h * stride + dx;
int y = w * stride + dy;
if(x < 0 || x >= inpHeight){
printf("Unable to compute MaxPoolingBackward.\n");
fprintf(fpDebug, "Unable to compute MaxPoolingBackward.\n");
fprintf(fpDebug, "dx = %d, dy = %d\n",dx,dy);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
if(y < 0 || y >= inpWidth){
printf("Unable to compute MaxPoolingBackward.\n");
fprintf(fpDebug, "Unable to compute MaxPoolingBackward.\n");
fprintf(fpDebug, "dx = %d, dy = %d\n",dx,dy);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
error[tar].thiArr[d].arr[x][y] = err.thiArr[d].arr[h][w];
return;
}
void MaxPoolingLayer::AvgPoolingBackward(int tar,const ThirdArray& err,int d,int h,int w){
int coff = kerHeight * kerWidth;
for(int i = 0;i < kerHeight;i++){
for(int j = 0;j < kerWidth;j++){
int x = h * stride + i;
int y = w * stride + j;
if(x < 0 || x >= inpHeight){
printf("Unable to compute AvgPoolingBackward.\n");
fprintf(fpDebug, "Unable to compute AvgPoolingBackward.\n");
fprintf(fpDebug, "i = %d, j = %d\n",i,j);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
if(y < 0 || y >= inpWidth){
printf("Unable to compute AvgPoolingBackward.\n");
fprintf(fpDebug, "Unable to compute AvgPoolingBackward.\n");
fprintf(fpDebug, "i = %d, j = %d\n",i,j);
fprintf(fpDebug, "x = %d, y = %d\n",x,y);
fflush(fpDebug);
exit(0);
}
error[tar].thiArr[d].arr[x][y] += (err.thiArr[d].arr[h][w] / coff);
}
}
return;
}