This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
convolution.cpp
178 lines (159 loc) · 6.86 KB
/
convolution.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
#include "narray/convolution.h"
#include "op/physical_op.h"
namespace minerva {
ImageBatch Convolution::ConvForward(ImageBatch src, Filter filter, NArray bias, ConvInfo info) {
CHECK_EQ(src.GetNumFeatureMaps(), filter.GetNumInputs()) << "#input channels mismatch";
CHECK_EQ(bias.Size().NumDims(), 1) << "bias dimension mismatch";
CHECK_EQ(bias.Size()[0], filter.GetNumOutputs()) << "bias size mismatch";
//no such limit
//CHECK_EQ((src.GetHeight() + 2 * info.pad_height - filter.GetHeight()) % info.stride_vertical, 0) << "filter height mismatch";
//CHECK_EQ((src.GetWidth() + 2 * info.pad_width - filter.GetWidth()) % info.stride_horizontal, 0) << "filter width mismatch";
Scale new_size {
(src.GetWidth() + 2 * info.pad_width - filter.GetWidth()) / info.stride_horizontal + 1,
(src.GetHeight() + 2 * info.pad_height - filter.GetHeight()) / info.stride_vertical + 1,
filter.GetNumOutputs(),
src.GetNumImages()
};
ConvForwardOp* op = new ConvForwardOp();
op->closure = {
info.pad_height,
info.pad_width,
info.stride_vertical,
info.stride_horizontal
};
return NArray::ComputeOne({src, filter, bias}, new_size, op);
}
ImageBatch Convolution::ConvBackwardData(ImageBatch diff, ImageBatch bottom, Filter filter, ConvInfo info) {
CHECK_EQ(diff.GetNumFeatureMaps(), filter.GetNumOutputs()) << "#output channels mismatch";
/*
* We can't get filter size when (top + 2*pad) % stride != 0
Scale new_size {
(diff.GetWidth() - 1) * info.stride_horizontal + filter.GetWidth() - 2 * info.pad_width,
(diff.GetHeight() - 1) * info.stride_vertical + filter.GetHeight() - 2 * info.pad_height,
filter.GetNumInputs(),
diff.GetNumImages()
};
*/
ConvBackwardDataOp* op = new ConvBackwardDataOp();
op->closure = {
info.pad_height,
info.pad_width,
info.stride_vertical,
info.stride_horizontal
};
return NArray::ComputeOne({diff, filter}, bottom.Size(), op);
}
Filter Convolution::ConvBackwardFilter(ImageBatch diff, ImageBatch bottom, Filter filter, ConvInfo info) {
CHECK_EQ(diff.GetNumImages(), bottom.GetNumImages()) << "#images mismatch";
/*
* We can't get filter size when (top + 2*pad) % stride != 0
Scale new_size {
-(diff.GetWidth() - 1) * info.stride_horizontal + bottom.GetWidth() + 2 * info.pad_width,
-(diff.GetHeight() - 1) * info.stride_vertical + bottom.GetHeight() + 2 * info.pad_height,
bottom.GetNumFeatureMaps(),
diff.GetNumFeatureMaps()
};
*/
ConvBackwardFilterOp* op = new ConvBackwardFilterOp();
op->closure = {
info.pad_height,
info.pad_width,
info.stride_vertical,
info.stride_horizontal
};
return NArray::ComputeOne({diff, bottom}, filter.Size(), op);
}
NArray Convolution::ConvBackwardBias(ImageBatch diff) {
Scale new_size {
diff.GetNumFeatureMaps()
};
ConvBackwardBiasOp* op = new ConvBackwardBiasOp();
return NArray::ComputeOne({diff}, new_size, op);
}
ImageBatch Convolution::SoftmaxForward(ImageBatch src, SoftmaxAlgorithm algorithm) {
SoftmaxForwardOp* op = new SoftmaxForwardOp();
op->closure.algorithm = algorithm;
return NArray::ComputeOne({src}, src.Size(), op);
}
ImageBatch Convolution::SoftmaxBackward(ImageBatch diff, ImageBatch top, SoftmaxAlgorithm algorithm) {
CHECK_EQ(diff.Size(), top.Size()) << "inputs sizes mismatch";
SoftmaxBackwardOp* op = new SoftmaxBackwardOp();
op->closure.algorithm = algorithm;
return NArray::ComputeOne({diff, top}, diff.Size(), op);
}
ImageBatch Convolution::ActivationForward(ImageBatch src, ActivationAlgorithm algorithm) {
ActivationForwardOp* op = new ActivationForwardOp();
op->closure.algorithm = algorithm;
return NArray::ComputeOne({src}, src.Size(), op);
}
ImageBatch Convolution::ActivationBackward(ImageBatch diff, ImageBatch top, ImageBatch bottom, ActivationAlgorithm algorithm) {
CHECK_EQ(diff.Size(), top.Size()) << "inputs sizes mismatch";
CHECK_EQ(diff.Size(), bottom.Size()) << "inputs sizes mismatch";
ActivationBackwardOp* op = new ActivationBackwardOp();
op->closure.algorithm = algorithm;
return NArray::ComputeOne({diff, top, bottom}, diff.Size(), op);
}
ImageBatch Convolution::PoolingForward(ImageBatch src, PoolingInfo info) {
int pooled_height = (src.GetHeight() + 2 * info.pad_height - info.height + info.stride_vertical - 1) / info.stride_vertical + 1;
int pooled_width = (src.GetWidth() + 2 * info.pad_width - info.width + info.stride_horizontal - 1) / info.stride_horizontal + 1;
if (0 <= (pooled_height - 1) * info.stride_vertical - src.GetHeight() - info.pad_height) {
--pooled_height;
}
if (0 <= (pooled_width - 1) * info.stride_horizontal - src.GetWidth() - info.pad_width) {
--pooled_width;
}
Scale new_size {
pooled_width,
pooled_height,
src.GetNumFeatureMaps(),
src.GetNumImages()
};
PoolingForwardOp* op = new PoolingForwardOp();
op->closure = {
info.algorithm,
info.height,
info.width,
info.stride_vertical,
info.stride_horizontal,
info.pad_height,
info.pad_width
};
return NArray::ComputeOne({src}, new_size, op);
}
ImageBatch Convolution::PoolingBackward(ImageBatch diff, ImageBatch top, ImageBatch bottom, PoolingInfo info) {
CHECK_EQ(diff.Size(), top.Size()) << "inputs sizes mismatch";
CHECK_EQ(diff.GetNumImages(), bottom.GetNumImages()) << "#images mismatch";
CHECK_EQ(diff.GetNumFeatureMaps(), bottom.GetNumFeatureMaps()) << "#channels mismatch";
int pooled_height = (bottom.GetHeight() + 2 * info.pad_height - info.height + info.stride_vertical - 1) / info.stride_vertical + 1;
int pooled_width = (bottom.GetWidth() + 2 * info.pad_width - info.width + info.stride_horizontal - 1) / info.stride_horizontal + 1;
if (0 <= (pooled_height - 1) * info.stride_vertical - bottom.GetHeight() - info.pad_height) {
--pooled_height;
}
if (0 <= (pooled_width - 1) * info.stride_horizontal - bottom.GetWidth() - info.pad_width) {
--pooled_width;
}
CHECK_EQ(top.GetHeight(), pooled_height) << "height mismatch";
CHECK_EQ(top.GetWidth(), pooled_width) << "width mismatch";
PoolingBackwardOp* op = new PoolingBackwardOp();
op->closure = {
info.algorithm,
info.height,
info.width,
info.stride_vertical,
info.stride_horizontal,
info.pad_height,
info.pad_width
};
return NArray::ComputeOne({diff, top, bottom}, bottom.Size(), op);
}
ImageBatch Convolution::LRNForward(ImageBatch src, ImageBatch scale, int local_size, float alpha, float beta) {
LRNForwardOp* op = new LRNForwardOp();
op->closure = {local_size, alpha, beta, src.Size()};
return NArray::ComputeOne({src, scale}, src.Size(), op);
}
ImageBatch Convolution::LRNBackward(ImageBatch bottom_data, ImageBatch top_data, ImageBatch scale, ImageBatch top_diff , int local_size, float alpha, float beta) {
LRNBackwardOp* op = new LRNBackwardOp();
op->closure = {local_size, alpha, beta, bottom_data.Size()};
return NArray::ComputeOne({bottom_data, top_data, scale, top_diff}, bottom_data.Size(), op);
}
} // namespace minerva