-
Notifications
You must be signed in to change notification settings - Fork 22
/
layer_factory.cpp
270 lines (252 loc) · 9.5 KB
/
layer_factory.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
#include <string>
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/vision_layers.hpp"
namespace caffe {
// GetLayer() defines the overall layer factory. The Get*Layer() functions
// define factories for layers with multiple computational engines.
// Get convolution layer according to engine.
template <typename Dtype>
ConvolutionLayer<Dtype>* GetConvolutionLayer(const string& name,
const LayerParameter& param) {
ConvolutionParameter_Engine engine = param.convolution_param().engine();
if (engine == ConvolutionParameter_Engine_DEFAULT) {
engine = ConvolutionParameter_Engine_CAFFE;
#ifdef USE_CUDNN
engine = ConvolutionParameter_Engine_CUDNN;
#endif
}
if (engine == ConvolutionParameter_Engine_CAFFE) {
return new ConvolutionLayer<Dtype>(param);
#ifdef USE_CUDNN
} else if (engine == ConvolutionParameter_Engine_CUDNN) {
return new CuDNNConvolutionLayer<Dtype>(param);
#endif
} else {
LOG(FATAL) << "Layer " << name << " has unknown engine.";
}
}
template ConvolutionLayer<float>* GetConvolutionLayer(const string& name,
const LayerParameter& param);
template ConvolutionLayer<double>* GetConvolutionLayer(const string& name,
const LayerParameter& param);
// Get pooling layer according to engine.
template <typename Dtype>
PoolingLayer<Dtype>* GetPoolingLayer(const string& name,
const LayerParameter& param) {
PoolingParameter_Engine engine = param.pooling_param().engine();
if (engine == PoolingParameter_Engine_DEFAULT) {
engine = PoolingParameter_Engine_CAFFE;
#ifdef USE_CUDNN
engine = PoolingParameter_Engine_CUDNN;
#endif
}
if (engine == PoolingParameter_Engine_CAFFE) {
return new PoolingLayer<Dtype>(param);
#ifdef USE_CUDNN
} else if (engine == PoolingParameter_Engine_CUDNN) {
return new CuDNNPoolingLayer<Dtype>(param);
#endif
} else {
LOG(FATAL) << "Layer " << name << " has unknown engine.";
}
}
template PoolingLayer<float>* GetPoolingLayer(const string& name,
const LayerParameter& param);
template PoolingLayer<double>* GetPoolingLayer(const string& name,
const LayerParameter& param);
// Get relu layer according to engine.
template <typename Dtype>
ReLULayer<Dtype>* GetReLULayer(const string& name,
const LayerParameter& param) {
ReLUParameter_Engine engine = param.relu_param().engine();
if (engine == ReLUParameter_Engine_DEFAULT) {
engine = ReLUParameter_Engine_CAFFE;
#ifdef USE_CUDNN
engine = ReLUParameter_Engine_CUDNN;
#endif
}
if (engine == ReLUParameter_Engine_CAFFE) {
return new ReLULayer<Dtype>(param);
#ifdef USE_CUDNN
} else if (engine == ReLUParameter_Engine_CUDNN) {
return new CuDNNReLULayer<Dtype>(param);
#endif
} else {
LOG(FATAL) << "Layer " << name << " has unknown engine.";
}
}
template ReLULayer<float>* GetReLULayer(const string& name,
const LayerParameter& param);
template ReLULayer<double>* GetReLULayer(const string& name,
const LayerParameter& param);
// Get sigmoid layer according to engine.
template <typename Dtype>
SigmoidLayer<Dtype>* GetSigmoidLayer(const string& name,
const LayerParameter& param) {
SigmoidParameter_Engine engine = param.sigmoid_param().engine();
if (engine == SigmoidParameter_Engine_DEFAULT) {
engine = SigmoidParameter_Engine_CAFFE;
#ifdef USE_CUDNN
engine = SigmoidParameter_Engine_CUDNN;
#endif
}
if (engine == SigmoidParameter_Engine_CAFFE) {
return new SigmoidLayer<Dtype>(param);
#ifdef USE_CUDNN
} else if (engine == SigmoidParameter_Engine_CUDNN) {
return new CuDNNSigmoidLayer<Dtype>(param);
#endif
} else {
LOG(FATAL) << "Layer " << name << " has unknown engine.";
}
}
template SigmoidLayer<float>* GetSigmoidLayer(const string& name,
const LayerParameter& param);
template SigmoidLayer<double>* GetSigmoidLayer(const string& name,
const LayerParameter& param);
// Get tanh layer according to engine.
template <typename Dtype>
TanHLayer<Dtype>* GetTanHLayer(const string& name,
const LayerParameter& param) {
TanHParameter_Engine engine = param.tanh_param().engine();
if (engine == TanHParameter_Engine_DEFAULT) {
engine = TanHParameter_Engine_CAFFE;
#ifdef USE_CUDNN
engine = TanHParameter_Engine_CUDNN;
#endif
}
if (engine == TanHParameter_Engine_CAFFE) {
return new TanHLayer<Dtype>(param);
#ifdef USE_CUDNN
} else if (engine == TanHParameter_Engine_CUDNN) {
return new CuDNNTanHLayer<Dtype>(param);
#endif
} else {
LOG(FATAL) << "Layer " << name << " has unknown engine.";
}
}
template TanHLayer<float>* GetTanHLayer(const string& name,
const LayerParameter& param);
template TanHLayer<double>* GetTanHLayer(const string& name,
const LayerParameter& param);
// Get softmax layer according to engine.
template <typename Dtype>
SoftmaxLayer<Dtype>* GetSoftmaxLayer(const string& name,
const LayerParameter& param) {
SoftmaxParameter_Engine engine = param.softmax_param().engine();
if (engine == SoftmaxParameter_Engine_DEFAULT) {
engine = SoftmaxParameter_Engine_CAFFE;
#ifdef USE_CUDNN
engine = SoftmaxParameter_Engine_CUDNN;
#endif
}
if (engine == SoftmaxParameter_Engine_CAFFE) {
return new SoftmaxLayer<Dtype>(param);
#ifdef USE_CUDNN
} else if (engine == SoftmaxParameter_Engine_CUDNN) {
return new CuDNNSoftmaxLayer<Dtype>(param);
#endif
} else {
LOG(FATAL) << "Layer " << name << " has unknown engine.";
}
}
template SoftmaxLayer<float>* GetSoftmaxLayer(const string& name,
const LayerParameter& param);
template SoftmaxLayer<double>* GetSoftmaxLayer(const string& name,
const LayerParameter& param);
// A function to get a specific layer from the specification given in
// LayerParameter. Ideally this would be replaced by a factory pattern,
// but we will leave it this way for now.
template <typename Dtype>
Layer<Dtype>* GetLayer(const LayerParameter& param) {
const string& name = param.name();
const LayerParameter_LayerType& type = param.type();
switch (type) {
case LayerParameter_LayerType_ACCURACY:
return new AccuracyLayer<Dtype>(param);
case LayerParameter_LayerType_ABSVAL:
return new AbsValLayer<Dtype>(param);
case LayerParameter_LayerType_ARGMAX:
return new ArgMaxLayer<Dtype>(param);
case LayerParameter_LayerType_BN:
return new BNLayer<Dtype>(param);
case LayerParameter_LayerType_BNLL:
return new BNLLLayer<Dtype>(param);
case LayerParameter_LayerType_COMPACT_DATA:
return new CompactDataLayer<Dtype>(param);
case LayerParameter_LayerType_CONCAT:
return new ConcatLayer<Dtype>(param);
case LayerParameter_LayerType_CONVOLUTION:
return GetConvolutionLayer<Dtype>(name, param);
case LayerParameter_LayerType_DATA:
return new DataLayer<Dtype>(param);
case LayerParameter_LayerType_DROPOUT:
return new DropoutLayer<Dtype>(param);
case LayerParameter_LayerType_DUMMY_DATA:
return new DummyDataLayer<Dtype>(param);
case LayerParameter_LayerType_EUCLIDEAN_LOSS:
return new EuclideanLossLayer<Dtype>(param);
case LayerParameter_LayerType_ELTWISE:
return new EltwiseLayer<Dtype>(param);
case LayerParameter_LayerType_FLATTEN:
return new FlattenLayer<Dtype>(param);
case LayerParameter_LayerType_HDF5_DATA:
return new HDF5DataLayer<Dtype>(param);
case LayerParameter_LayerType_HDF5_OUTPUT:
return new HDF5OutputLayer<Dtype>(param);
case LayerParameter_LayerType_HINGE_LOSS:
return new HingeLossLayer<Dtype>(param);
case LayerParameter_LayerType_IMAGE_DATA:
return new ImageDataLayer<Dtype>(param);
case LayerParameter_LayerType_IM2COL:
return new Im2colLayer<Dtype>(param);
case LayerParameter_LayerType_INFOGAIN_LOSS:
return new InfogainLossLayer<Dtype>(param);
case LayerParameter_LayerType_INNER_PRODUCT:
return new InnerProductLayer<Dtype>(param);
case LayerParameter_LayerType_LRN:
return new LRNLayer<Dtype>(param);
case LayerParameter_LayerType_MEMORY_DATA:
return new MemoryDataLayer<Dtype>(param);
case LayerParameter_LayerType_MVN:
return new MVNLayer<Dtype>(param);
case LayerParameter_LayerType_MULTINOMIAL_LOGISTIC_LOSS:
return new MultinomialLogisticLossLayer<Dtype>(param);
case LayerParameter_LayerType_POOLING:
return GetPoolingLayer<Dtype>(name, param);
case LayerParameter_LayerType_POWER:
return new PowerLayer<Dtype>(param);
case LayerParameter_LayerType_PRELU:
return new PReLULayer<Dtype>(param);
case LayerParameter_LayerType_RELU:
return GetReLULayer<Dtype>(name, param);
case LayerParameter_LayerType_SILENCE:
return new SilenceLayer<Dtype>(param);
case LayerParameter_LayerType_SIGMOID:
return GetSigmoidLayer<Dtype>(name, param);
case LayerParameter_LayerType_SIGMOID_CROSS_ENTROPY_LOSS:
return new SigmoidCrossEntropyLossLayer<Dtype>(param);
case LayerParameter_LayerType_SLICE:
return new SliceLayer<Dtype>(param);
case LayerParameter_LayerType_SOFTMAX:
return GetSoftmaxLayer<Dtype>(name, param);
case LayerParameter_LayerType_SOFTMAX_LOSS:
return new SoftmaxWithLossLayer<Dtype>(param);
case LayerParameter_LayerType_SPLIT:
return new SplitLayer<Dtype>(param);
case LayerParameter_LayerType_TANH:
return GetTanHLayer<Dtype>(name, param);
case LayerParameter_LayerType_WINDOW_DATA:
return new WindowDataLayer<Dtype>(param);
case LayerParameter_LayerType_NONE:
LOG(FATAL) << "Layer " << name << " has unspecified type.";
default:
LOG(FATAL) << "Layer " << name << " has unknown type " << type;
}
// just to suppress old compiler warnings.
return (Layer<Dtype>*)(NULL);
}
template Layer<float>* GetLayer(const LayerParameter& param);
template Layer<double>* GetLayer(const LayerParameter& param);
} // namespace caffe