]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - src/caffe/layer.hpp
b82f03806f88935252993a2046d061b6ca73036f
[jacinto-ai/caffe-jacinto.git] / src / caffe / layer.hpp
1 // Copyright 2013 Yangqing Jia
3 #ifndef CAFFE_LAYER_H_
4 #define CAFFE_LAYER_H_
6 #include <vector>
7 #include "caffe/blob.hpp"
8 #include "caffe/common.hpp"
9 #include "caffe/proto/caffe.pb.h"
11 using std::vector;
13 namespace caffe {
15 template <typename Dtype>
16 class Layer {
17  public:
18   // You should not implement your own constructor. Any set up code should go
19   // to SetUp(), where the dimensions of the bottom blobs are provided to the
20   // layer.
21   explicit Layer(const LayerParameter& param)
22     : layer_param_(param) {}
23   virtual ~Layer() {}
24   // SetUp: your function should implement this.
25   virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
26       vector<Blob<Dtype>*>* top) = 0;
28   // Forward and backward wrappers. You should implement the cpu and
29   // gpu specific implementations instead, and should not change these
30   // functions.
31   inline void Forward(const vector<Blob<Dtype>*>& bottom,
32       vector<Blob<Dtype>*>* top);
33   inline Dtype Backward(const vector<Blob<Dtype>*>& top,
34       const bool propagate_down,
35       vector<Blob<Dtype>*>* bottom);
37   // Returns the vector of parameters.
38   vector<shared_ptr<Blob<Dtype> > >& params() {
39     return blobs_;
40   }
42   // Writes the layer parameter to a protocol buffer
43   void ToProto(LayerParameter* param, bool write_diff = false);
45  protected:
46   // The protobuf that stores the layer parameters
47   LayerParameter layer_param_;
48   // The vector that stores the parameters as a set of blobs.
49   vector<shared_ptr<Blob<Dtype> > > blobs_;
51   // Forward functions
52   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
53       vector<Blob<Dtype>*>* top) = 0;
54   // If no gpu code is provided, we will simply use cpu code.
55   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
56       vector<Blob<Dtype>*>* top) {
57     LOG(WARNING) << "Using CPU code as backup.";
58     Forward_cpu(bottom, top);
59   };
61   // Backward functions: the backward function will compute the gradients for
62   // any parameters and also for the bottom blobs if propagate_down is true.
63   // It will return the loss produced from this layer.
64   virtual Dtype Backward_cpu(const vector<Blob<Dtype>*>& top,
65       const bool propagate_down,
66       vector<Blob<Dtype>*>* bottom) = 0;
67   virtual Dtype Backward_gpu(const vector<Blob<Dtype>*>& top,
68       const bool propagate_down,
69       vector<Blob<Dtype>*>* bottom) {
70     LOG(WARNING) << "Using CPU code as backup.";
71     return Backward_cpu(top, propagate_down, bottom);
72   };
74   DISABLE_COPY_AND_ASSIGN(Layer);
75 };  // class Layer
77 // Forward and backward wrappers. You should implement the cpu and
78 // gpu specific implementations instead, and should not change these
79 // functions.
80 template <typename Dtype>
81 inline void Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
82     vector<Blob<Dtype>*>* top) {
83   switch (Caffe::mode()) {
84   case Caffe::CPU:
85     Forward_cpu(bottom, top);
86     break;
87   case Caffe::GPU:
88     Forward_gpu(bottom, top);
89     break;
90   default:
91     LOG(FATAL) << "Unknown caffe mode.";
92   }
93 };
95 template <typename Dtype>
96 inline Dtype Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
97     const bool propagate_down,
98     vector<Blob<Dtype>*>* bottom) {
99   switch (Caffe::mode()) {
100   case Caffe::CPU:
101     return Backward_cpu(top, propagate_down, bottom);
102   case Caffe::GPU:
103     return Backward_gpu(top, propagate_down, bottom);
104   default:
105     LOG(FATAL) << "Unknown caffe mode.";
106   }
107 };
109 template <typename Dtype>
110 void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
111   param->Clear();
112   param->CopyFrom(layer_param_);
113   param->clear_blobs();
114   for (int i = 0; i < blobs_.size(); ++i) {
115     blobs_[i]->ToProto(param->add_blobs(), write_diff);
116   }
119 }  // namespace caffe
121 #endif  // CAFFE_LAYER_H_