1 // Copyright 2013 Yangqing Jia
3 #ifndef CAFFE_VISION_LAYERS_HPP_
4 #define CAFFE_VISION_LAYERS_HPP_
6 #include <leveldb/db.h>
11 #include "caffe/layer.hpp"
16 // The neuron layer is a specific type of layers that just works on single
18 template <typename Dtype>
19 class NeuronLayer : public Layer<Dtype> {
21 explicit NeuronLayer(const LayerParameter& param)
22 : Layer<Dtype>(param) {}
23 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
24 vector<Blob<Dtype>*>* top);
28 template <typename Dtype>
29 class ReLULayer : public NeuronLayer<Dtype> {
31 explicit ReLULayer(const LayerParameter& param)
32 : NeuronLayer<Dtype>(param) {}
35 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
36 vector<Blob<Dtype>*>* top);
37 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
38 vector<Blob<Dtype>*>* top);
40 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
41 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
42 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
43 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
47 template <typename Dtype>
48 class DropoutLayer : public NeuronLayer<Dtype> {
50 explicit DropoutLayer(const LayerParameter& param)
51 : NeuronLayer<Dtype>(param) {}
52 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
53 vector<Blob<Dtype>*>* top);
56 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
57 vector<Blob<Dtype>*>* top);
58 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
59 vector<Blob<Dtype>*>* top);
61 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
62 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
63 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
64 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
65 shared_ptr<SyncedMemory> rand_vec_;
68 unsigned int uint_thres_;
72 template <typename Dtype>
73 class InnerProductLayer : public Layer<Dtype> {
75 explicit InnerProductLayer(const LayerParameter& param)
76 : Layer<Dtype>(param) {}
77 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
78 vector<Blob<Dtype>*>* top);
81 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
82 vector<Blob<Dtype>*>* top);
83 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
84 vector<Blob<Dtype>*>* top);
86 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
87 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
88 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
89 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
94 shared_ptr<SyncedMemory> bias_multiplier_;
98 template <typename Dtype>
99 class PaddingLayer : public Layer<Dtype> {
101 explicit PaddingLayer(const LayerParameter& param)
102 : Layer<Dtype>(param) {}
103 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
104 vector<Blob<Dtype>*>* top);
107 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
108 vector<Blob<Dtype>*>* top);
109 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
110 vector<Blob<Dtype>*>* top);
111 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
112 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
113 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
114 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
125 template <typename Dtype>
126 class LRNLayer : public Layer<Dtype> {
128 explicit LRNLayer(const LayerParameter& param)
129 : Layer<Dtype>(param) {}
130 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
131 vector<Blob<Dtype>*>* top);
134 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
135 vector<Blob<Dtype>*>* top);
136 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
137 vector<Blob<Dtype>*>* top);
138 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
139 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
140 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
141 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
142 // scale_ stores the intermediate summing results
155 template <typename Dtype>
156 class Im2colLayer : public Layer<Dtype> {
158 explicit Im2colLayer(const LayerParameter& param)
159 : Layer<Dtype>(param) {}
160 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
161 vector<Blob<Dtype>*>* top);
164 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
165 vector<Blob<Dtype>*>* top);
166 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
167 vector<Blob<Dtype>*>* top);
168 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
169 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
170 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
171 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
180 template <typename Dtype>
181 class PoolingLayer : public Layer<Dtype> {
183 explicit PoolingLayer(const LayerParameter& param)
184 : Layer<Dtype>(param) {}
185 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
186 vector<Blob<Dtype>*>* top);
189 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
190 vector<Blob<Dtype>*>* top);
191 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
192 vector<Blob<Dtype>*>* top);
193 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
194 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
195 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
196 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
207 template <typename Dtype>
208 class ConvolutionLayer : public Layer<Dtype> {
210 explicit ConvolutionLayer(const LayerParameter& param)
211 : Layer<Dtype>(param) {}
212 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
213 vector<Blob<Dtype>*>* top);
216 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
217 vector<Blob<Dtype>*>* top);
218 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
219 vector<Blob<Dtype>*>* top);
220 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
221 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
222 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
223 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
224 Blob<Dtype> col_bob_;
234 Blob<Dtype> col_buffer_;
235 shared_ptr<SyncedMemory> bias_multiplier_;
243 // This function is used to create a pthread that prefetches the data.
244 template <typename Dtype>
245 void* DataLayerPrefetch(void* layer_pointer);
247 template <typename Dtype>
248 class DataLayer : public Layer<Dtype> {
249 // The function used to perform prefetching.
250 friend void* DataLayerPrefetch<Dtype>(void* layer_pointer);
253 explicit DataLayer(const LayerParameter& param)
254 : Layer<Dtype>(param) {}
255 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
256 vector<Blob<Dtype>*>* top);
259 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
260 vector<Blob<Dtype>*>* top);
261 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
262 vector<Blob<Dtype>*>* top);
263 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
264 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
265 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
266 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
268 shared_ptr<leveldb::DB> db_;
269 shared_ptr<leveldb::Iterator> iter_;
275 shared_ptr<Blob<Dtype> > prefetch_data_;
276 shared_ptr<Blob<Dtype> > prefetch_label_;
277 Blob<Dtype> data_mean_;
281 template <typename Dtype>
282 class SoftmaxLayer : public Layer<Dtype> {
284 explicit SoftmaxLayer(const LayerParameter& param)
285 : Layer<Dtype>(param) {}
286 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
287 vector<Blob<Dtype>*>* top);
290 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
291 vector<Blob<Dtype>*>* top);
292 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
293 vector<Blob<Dtype>*>* top);
294 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
295 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
296 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
297 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
299 // sum_multiplier is just used to carry out sum using blas
300 Blob<Dtype> sum_multiplier_;
301 // scale is an intermediate blob to hold temporary results.
306 template <typename Dtype>
307 class MultinomialLogisticLossLayer : public Layer<Dtype> {
309 explicit MultinomialLogisticLossLayer(const LayerParameter& param)
310 : Layer<Dtype>(param) {}
311 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
312 vector<Blob<Dtype>*>* top);
315 // The loss layer will do nothing during forward - all computation are
316 // carried out in the backward pass.
317 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
318 vector<Blob<Dtype>*>* top) { return; }
319 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
320 vector<Blob<Dtype>*>* top) { return; }
321 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
322 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
323 // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
324 // const bool propagate_down, vector<Blob<Dtype>*>* bottom);
328 // SoftmaxWithLossLayer is a layer that implements softmax and then computes
329 // the loss - it is preferred over softmax + multinomiallogisticloss in the
330 // sense that during training, this will produce more numerically stable
331 // gradients. During testing this layer could be replaced by a softmax layer
332 // to generate probability outputs.
333 template <typename Dtype>
334 class SoftmaxWithLossLayer : public Layer<Dtype> {
336 explicit SoftmaxWithLossLayer(const LayerParameter& param)
337 : Layer<Dtype>(param), softmax_layer_(new SoftmaxLayer<Dtype>(param)) {}
338 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
339 vector<Blob<Dtype>*>* top);
342 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
343 vector<Blob<Dtype>*>* top);
344 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
345 vector<Blob<Dtype>*>* top);
346 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
347 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
348 virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
349 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
351 shared_ptr<SoftmaxLayer<Dtype> > softmax_layer_;
352 // prob stores the output probability of the layer.
354 // Vector holders to call the underlying softmax layer forward and backward.
355 vector<Blob<Dtype>*> softmax_bottom_vec_;
356 vector<Blob<Dtype>*> softmax_top_vec_;
360 template <typename Dtype>
361 class EuclideanLossLayer : public Layer<Dtype> {
363 explicit EuclideanLossLayer(const LayerParameter& param)
364 : Layer<Dtype>(param), difference_() {}
365 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
366 vector<Blob<Dtype>*>* top);
369 // The loss layer will do nothing during forward - all computation are
370 // carried out in the backward pass.
371 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
372 vector<Blob<Dtype>*>* top) { return; }
373 virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
374 vector<Blob<Dtype>*>* top) { return; }
375 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
376 const bool propagate_down, vector<Blob<Dtype>*>* bottom);
377 // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
378 // const bool propagate_down, vector<Blob<Dtype>*>* bottom);
379 Blob<Dtype> difference_;
383 template <typename Dtype>
384 class AccuracyLayer : public Layer<Dtype> {
386 explicit AccuracyLayer(const LayerParameter& param)
387 : Layer<Dtype>(param) {}
388 virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
389 vector<Blob<Dtype>*>* top);
392 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
393 vector<Blob<Dtype>*>* top);
394 // The accuracy layer should not be used to compute backward operations.
395 virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
396 const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
404 #endif // CAFFE_VISION_LAYERS_HPP_