3b09459bbf11c31c9ab3a8cbf675e35bfada8709
[platform/upstream/caffeonacl.git] / include / caffe / vision_layers.hpp
1 // Copyright 2013 Yangqing Jia
2
3 #ifndef CAFFE_VISION_LAYERS_HPP_
4 #define CAFFE_VISION_LAYERS_HPP_
5
6 #include <vector>
7
8 #include "leveldb/db.h"
9 #include "pthread.h"
10 #include "boost/scoped_ptr.hpp"
11 #include "hdf5.h"
12
13 #include "caffe/layer.hpp"
14 #include "caffe/proto/caffe.pb.h"
15
16 namespace caffe {
17
18
19 // The neuron layer is a specific type of layers that just works on single
20 // celements.
21 template <typename Dtype>
22 class NeuronLayer : public Layer<Dtype> {
23  public:
24   explicit NeuronLayer(const LayerParameter& param)
25      : Layer<Dtype>(param) {}
26   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
27       vector<Blob<Dtype>*>* top);
28 };
29
30
31 template <typename Dtype>
32 class ReLULayer : public NeuronLayer<Dtype> {
33  public:
34   explicit ReLULayer(const LayerParameter& param)
35       : NeuronLayer<Dtype>(param) {}
36
37  protected:
38   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
39       vector<Blob<Dtype>*>* top);
40   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
41       vector<Blob<Dtype>*>* top);
42
43   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
44       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
45   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
46       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
47 };
48
49 template <typename Dtype>
50 class TanHLayer : public NeuronLayer<Dtype> {
51  public:
52   explicit TanHLayer(const LayerParameter& param)
53       : NeuronLayer<Dtype>(param) {}
54
55  protected:
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);
60
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 };
66
67 template <typename Dtype>
68 class SigmoidLayer : public NeuronLayer<Dtype> {
69  public:
70   explicit SigmoidLayer(const LayerParameter& param)
71       : NeuronLayer<Dtype>(param) {}
72
73  protected:
74   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
75       vector<Blob<Dtype>*>* top);
76   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
77       vector<Blob<Dtype>*>* top);
78
79   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
80       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
81   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
82       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
83 };
84
85
86 template <typename Dtype>
87 class BNLLLayer : public NeuronLayer<Dtype> {
88  public:
89   explicit BNLLLayer(const LayerParameter& param)
90       : NeuronLayer<Dtype>(param) {}
91
92  protected:
93   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
94       vector<Blob<Dtype>*>* top);
95   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
96       vector<Blob<Dtype>*>* top);
97
98   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
99       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
100   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
101       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
102 };
103
104
105 template <typename Dtype>
106 class DropoutLayer : public NeuronLayer<Dtype> {
107  public:
108   explicit DropoutLayer(const LayerParameter& param)
109       : NeuronLayer<Dtype>(param) {}
110   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
111       vector<Blob<Dtype>*>* top);
112
113  protected:
114   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
115       vector<Blob<Dtype>*>* top);
116   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
117       vector<Blob<Dtype>*>* top);
118
119   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
120       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
121   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
122       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
123   shared_ptr<SyncedMemory> rand_vec_;
124   float threshold_;
125   float scale_;
126   unsigned int uint_thres_;
127 };
128
129
130 template <typename Dtype>
131 class SplitLayer : public Layer<Dtype> {
132  public:
133   explicit SplitLayer(const LayerParameter& param)
134       : Layer<Dtype>(param) {}
135   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
136       vector<Blob<Dtype>*>* top);
137
138  protected:
139   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
140       vector<Blob<Dtype>*>* top);
141   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
142       vector<Blob<Dtype>*>* top);
143   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
144       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
145   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
146       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
147   int count_;
148 };
149
150
151 template <typename Dtype>
152 class FlattenLayer : public Layer<Dtype> {
153  public:
154   explicit FlattenLayer(const LayerParameter& param)
155       : Layer<Dtype>(param) {}
156   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
157       vector<Blob<Dtype>*>* top);
158
159  protected:
160   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
161       vector<Blob<Dtype>*>* top);
162   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
163       vector<Blob<Dtype>*>* top);
164   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
165       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
166   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
167       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
168   int count_;
169 };
170
171
172 template <typename Dtype>
173 class InnerProductLayer : public Layer<Dtype> {
174  public:
175   explicit InnerProductLayer(const LayerParameter& param)
176       : Layer<Dtype>(param) {}
177   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
178       vector<Blob<Dtype>*>* top);
179
180  protected:
181   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
182       vector<Blob<Dtype>*>* top);
183   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
184       vector<Blob<Dtype>*>* top);
185
186   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
187       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
188   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
189       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
190   int M_;
191   int K_;
192   int N_;
193   bool biasterm_;
194   shared_ptr<SyncedMemory> bias_multiplier_;
195 };
196
197 template <typename Dtype>
198 class LRNLayer : public Layer<Dtype> {
199  public:
200   explicit LRNLayer(const LayerParameter& param)
201       : Layer<Dtype>(param) {}
202   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
203       vector<Blob<Dtype>*>* top);
204
205  protected:
206   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
207       vector<Blob<Dtype>*>* top);
208   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
209       vector<Blob<Dtype>*>* top);
210   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
211       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
212   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
213       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
214   // scale_ stores the intermediate summing results
215   Blob<Dtype> scale_;
216   int size_;
217   int pre_pad_;
218   Dtype alpha_;
219   Dtype beta_;
220   int num_;
221   int channels_;
222   int height_;
223   int width_;
224 };
225
226
227 template <typename Dtype>
228 class Im2colLayer : public Layer<Dtype> {
229  public:
230   explicit Im2colLayer(const LayerParameter& param)
231       : Layer<Dtype>(param) {}
232   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
233       vector<Blob<Dtype>*>* top);
234
235  protected:
236   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
237       vector<Blob<Dtype>*>* top);
238   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
239       vector<Blob<Dtype>*>* top);
240   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
241       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
242   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
243       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
244   int KSIZE_;
245   int STRIDE_;
246   int CHANNELS_;
247   int HEIGHT_;
248   int WIDTH_;
249   int PAD_;
250 };
251
252 template <typename Dtype>
253 class PoolingLayer : public Layer<Dtype> {
254  public:
255   explicit PoolingLayer(const LayerParameter& param)
256       : Layer<Dtype>(param) {}
257   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
258       vector<Blob<Dtype>*>* top);
259
260  protected:
261   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
262       vector<Blob<Dtype>*>* top);
263   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
264       vector<Blob<Dtype>*>* top);
265   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
266       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
267   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
268       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
269   int KSIZE_;
270   int STRIDE_;
271   int CHANNELS_;
272   int HEIGHT_;
273   int WIDTH_;
274   int POOLED_HEIGHT_;
275   int POOLED_WIDTH_;
276   Blob<float> rand_idx_;
277 };
278
279
280 template <typename Dtype>
281 class ConvolutionLayer : public Layer<Dtype> {
282  public:
283   explicit ConvolutionLayer(const LayerParameter& param)
284       : Layer<Dtype>(param) {}
285   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
286       vector<Blob<Dtype>*>* top);
287
288  protected:
289   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
290       vector<Blob<Dtype>*>* top);
291   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
292       vector<Blob<Dtype>*>* top);
293   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
294       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
295   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
296       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
297   Blob<Dtype> col_bob_;
298
299   int KSIZE_;
300   int STRIDE_;
301   int NUM_;
302   int CHANNELS_;
303   int PAD_;
304   int HEIGHT_;
305   int WIDTH_;
306   int NUM_OUTPUT_;
307   int GROUP_;
308   Blob<Dtype> col_buffer_;
309   shared_ptr<SyncedMemory> bias_multiplier_;
310   bool biasterm_;
311   int M_;
312   int K_;
313   int N_;
314 };
315
316 template <typename Dtype>
317 class ConcatLayer : public Layer<Dtype> {
318  public:
319   explicit ConcatLayer(const LayerParameter& param)
320       : Layer<Dtype>(param) {}
321   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
322       vector<Blob<Dtype>*>* top);
323
324  protected:
325   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
326       vector<Blob<Dtype>*>* top);
327   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
328       vector<Blob<Dtype>*>* top);
329   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
330       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
331   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
332       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
333   Blob<Dtype> col_bob_;
334
335   int COUNT_;
336   int NUM_;
337   int CHANNELS_;
338   int HEIGHT_;
339   int WIDTH_;
340   int concat_dim_;
341 };
342
343 // This function is used to create a pthread that prefetches the data.
344 template <typename Dtype>
345 void* DataLayerPrefetch(void* layer_pointer);
346
347 template <typename Dtype>
348 class DataLayer : public Layer<Dtype> {
349   // The function used to perform prefetching.
350   friend void* DataLayerPrefetch<Dtype>(void* layer_pointer);
351
352  public:
353   explicit DataLayer(const LayerParameter& param)
354       : Layer<Dtype>(param) {}
355   virtual ~DataLayer();
356   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
357       vector<Blob<Dtype>*>* top);
358
359  protected:
360   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
361       vector<Blob<Dtype>*>* top);
362   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
363       vector<Blob<Dtype>*>* top);
364   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
365       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
366   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
367       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
368
369   shared_ptr<leveldb::DB> db_;
370   shared_ptr<leveldb::Iterator> iter_;
371   int datum_channels_;
372   int datum_height_;
373   int datum_width_;
374   int datum_size_;
375   pthread_t thread_;
376   shared_ptr<Blob<Dtype> > prefetch_data_;
377   shared_ptr<Blob<Dtype> > prefetch_label_;
378   Blob<Dtype> data_mean_;
379 };
380
381
382 template <typename Dtype>
383 class HDF5DataLayer : public Layer<Dtype> {
384  public:
385   explicit HDF5DataLayer(const LayerParameter& param)
386       : Layer<Dtype>(param) {}
387   virtual ~HDF5DataLayer();
388   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
389       vector<Blob<Dtype>*>* top);
390
391  protected:
392   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
393       vector<Blob<Dtype>*>* top);
394   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
395       vector<Blob<Dtype>*>* top);
396   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
397       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
398   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
399       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
400
401   boost::scoped_ptr<Dtype> data;
402   boost::scoped_ptr<Dtype> label;
403   hsize_t data_dims[2];
404   hsize_t label_dims[2];
405   hsize_t current_row;
406 };
407
408
409 template <typename Dtype>
410 class SoftmaxLayer : public Layer<Dtype> {
411  public:
412   explicit SoftmaxLayer(const LayerParameter& param)
413       : Layer<Dtype>(param) {}
414   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
415       vector<Blob<Dtype>*>* top);
416
417  protected:
418   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
419       vector<Blob<Dtype>*>* top);
420   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
421       vector<Blob<Dtype>*>* top);
422   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
423       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
424   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
425      const bool propagate_down, vector<Blob<Dtype>*>* bottom);
426
427   // sum_multiplier is just used to carry out sum using blas
428   Blob<Dtype> sum_multiplier_;
429   // scale is an intermediate blob to hold temporary results.
430   Blob<Dtype> scale_;
431 };
432
433
434 template <typename Dtype>
435 class MultinomialLogisticLossLayer : public Layer<Dtype> {
436  public:
437   explicit MultinomialLogisticLossLayer(const LayerParameter& param)
438       : Layer<Dtype>(param) {}
439   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
440       vector<Blob<Dtype>*>* top);
441
442  protected:
443   // The loss layer will do nothing during forward - all computation are
444   // carried out in the backward pass.
445   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
446       vector<Blob<Dtype>*>* top) { return; }
447   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
448       vector<Blob<Dtype>*>* top) { return; }
449   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
450       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
451   // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
452   //     const bool propagate_down, vector<Blob<Dtype>*>* bottom);
453 };
454
455 template <typename Dtype>
456 class InfogainLossLayer : public Layer<Dtype> {
457  public:
458   explicit InfogainLossLayer(const LayerParameter& param)
459       : Layer<Dtype>(param), infogain_() {}
460   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
461       vector<Blob<Dtype>*>* top);
462
463  protected:
464   // The loss layer will do nothing during forward - all computation are
465   // carried out in the backward pass.
466   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
467       vector<Blob<Dtype>*>* top) { return; }
468   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
469       vector<Blob<Dtype>*>* top) { return; }
470   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
471       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
472   // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
473   //     const bool propagate_down, vector<Blob<Dtype>*>* bottom);
474
475   Blob<Dtype> infogain_;
476 };
477
478
479 // SoftmaxWithLossLayer is a layer that implements softmax and then computes
480 // the loss - it is preferred over softmax + multinomiallogisticloss in the
481 // sense that during training, this will produce more numerically stable
482 // gradients. During testing this layer could be replaced by a softmax layer
483 // to generate probability outputs.
484 template <typename Dtype>
485 class SoftmaxWithLossLayer : public Layer<Dtype> {
486  public:
487   explicit SoftmaxWithLossLayer(const LayerParameter& param)
488       : Layer<Dtype>(param), softmax_layer_(new SoftmaxLayer<Dtype>(param)) {}
489   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
490       vector<Blob<Dtype>*>* top);
491
492  protected:
493   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
494       vector<Blob<Dtype>*>* top);
495   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
496       vector<Blob<Dtype>*>* top);
497   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
498       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
499   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
500      const bool propagate_down, vector<Blob<Dtype>*>* bottom);
501
502   shared_ptr<SoftmaxLayer<Dtype> > softmax_layer_;
503   // prob stores the output probability of the layer.
504   Blob<Dtype> prob_;
505   // Vector holders to call the underlying softmax layer forward and backward.
506   vector<Blob<Dtype>*> softmax_bottom_vec_;
507   vector<Blob<Dtype>*> softmax_top_vec_;
508 };
509
510
511 template <typename Dtype>
512 class EuclideanLossLayer : public Layer<Dtype> {
513  public:
514   explicit EuclideanLossLayer(const LayerParameter& param)
515       : Layer<Dtype>(param), difference_() {}
516   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
517       vector<Blob<Dtype>*>* top);
518
519  protected:
520   // The loss layer will do nothing during forward - all computation are
521   // carried out in the backward pass.
522   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
523       vector<Blob<Dtype>*>* top) { return; }
524   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
525       vector<Blob<Dtype>*>* top) { return; }
526   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
527       const bool propagate_down, vector<Blob<Dtype>*>* bottom);
528   // virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
529   //     const bool propagate_down, vector<Blob<Dtype>*>* bottom);
530   Blob<Dtype> difference_;
531 };
532
533
534 template <typename Dtype>
535 class AccuracyLayer : public Layer<Dtype> {
536  public:
537   explicit AccuracyLayer(const LayerParameter& param)
538       : Layer<Dtype>(param) {}
539   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
540       vector<Blob<Dtype>*>* top);
541
542  protected:
543   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
544       vector<Blob<Dtype>*>* top);
545   // The accuracy layer should not be used to compute backward operations.
546   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
547       const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
548     NOT_IMPLEMENTED;
549     return Dtype(0.);
550   }
551 };
552
553 }  // namespace caffe
554
555 #endif  // CAFFE_VISION_LAYERS_HPP_