Merge pull request #945 from longjon/fixtypes
[platform/upstream/caffeonacl.git] / include / caffe / layer.hpp
1 #ifndef CAFFE_LAYER_H_
2 #define CAFFE_LAYER_H_
3
4 #include <algorithm>
5 #include <string>
6 #include <vector>
7
8 #include "caffe/blob.hpp"
9 #include "caffe/common.hpp"
10 #include "caffe/proto/caffe.pb.h"
11 #include "caffe/util/device_alternate.hpp"
12
13 namespace caffe {
14
15 /**
16  * @brief An interface for the units of computation which can be composed into a
17  *        Net.
18  *
19  * Layer&s must implement a Forward function, in which they take their input
20  * (bottom) Blob&s (if any) and compute their output Blob&s (if any).
21  * They may also implement a Backward function, in which they compute the error
22  * gradients with respect to their input Blob&s, given the error gradients with
23  * their output Blob&s.
24  */
25 template <typename Dtype>
26 class Layer {
27  public:
28   /**
29    * You should not implement your own constructor. Any set up code should go
30    * to SetUp(), where the dimensions of the bottom blobs are provided to the
31    * layer.
32    */
33   explicit Layer(const LayerParameter& param)
34     : layer_param_(param) {
35       // The only thing we do is to copy blobs if there are any.
36       if (layer_param_.blobs_size() > 0) {
37         blobs_.resize(layer_param_.blobs_size());
38         for (int i = 0; i < layer_param_.blobs_size(); ++i) {
39           blobs_[i].reset(new Blob<Dtype>());
40           blobs_[i]->FromProto(layer_param_.blobs(i));
41         }
42       }
43     }
44   virtual ~Layer() {}
45
46   /**
47    * @brief Implements common layer setup functionality.
48    *
49    * @param bottom the preshaped input blobs
50    * @param top
51    *     the allocated but unshaped output blobs, to be shaped by Reshape
52    *
53    * Checks that the number of bottom and top blobs is correct.
54    * Calls LayerSetUp to do special layer setup for individual layer types,
55    * followed by Reshape to set up sizes of top blobs and internal buffers.
56    * Sets up the loss weight multiplier blobs for any non-zero loss weights.
57    * This method may not be overridden.
58    */
59   void SetUp(const vector<Blob<Dtype>*>& bottom,
60       const vector<Blob<Dtype>*>& top) {
61     CheckBlobCounts(bottom, top);
62     LayerSetUp(bottom, top);
63     Reshape(bottom, top);
64     SetLossWeights(top);
65   }
66
67   /**
68    * @brief Does layer-specific setup: your layer should implement this function
69    *        as well as Reshape.
70    *
71    * @param bottom
72    *     the preshaped input blobs, whose data fields store the input data for
73    *     this layer
74    * @param top
75    *     the allocated but unshaped output blobs
76    *
77    * This method should do one-time layer specific setup. This includes reading
78    * and processing relevent parameters from the <code>layer_param_</code>.
79    * Setting up the shapes of top blobs and internal buffers should be done in
80    * <code>Reshape</code>, which will be called before the forward pass to
81    * adjust the top blob sizes.
82    */
83   virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
84       const vector<Blob<Dtype>*>& top) {}
85
86   /**
87    * @brief Adjust the shapes of top blobs and internal buffers to accomodate
88    *        the shapes of the bottom blobs.
89    *
90    * @param bottom the input blobs, with the requested input shapes
91    * @param top the top blobs, which should be reshaped as needed
92    *
93    * This method should reshape top blobs as needed according to the shapes
94    * of the bottom (input) blobs, as well as reshaping any internal buffers
95    * and making any other necessary adjustments so that the layer can
96    * accomodate the bottom blobs.
97    */
98   virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
99       const vector<Blob<Dtype>*>& top) = 0;
100
101   /**
102    * @brief Given the bottom blobs, compute the top blobs and the loss.
103    *
104    * @param bottom
105    *     the input blobs, whose data fields store the input data for this layer
106    * @param top
107    *     the preshaped output blobs, whose data fields will store this layers'
108    *     outputs
109    * \return The total loss from the layer.
110    *
111    * The Forward wrapper calls the relevant device wrapper function
112    * (Forward_cpu or Forward_gpu) to compute the top blob values given the
113    * bottom blobs.  If the layer has any non-zero loss_weights, the wrapper
114    * then computes and returns the loss.
115    *
116    * Your layer should implement Forward_cpu and (optionally) Forward_gpu.
117    */
118   inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
119       const vector<Blob<Dtype>*>& top);
120
121   /**
122    * @brief Given the top blob error gradients, compute the bottom blob error
123    *        gradients.
124    *
125    * @param top
126    *     the output blobs, whose diff fields store the gradient of the error
127    *     with respect to themselves
128    * @param propagate_down
129    *     a vector with equal length to bottom, with each index indicating
130    *     whether to propagate the error gradients down to the bottom blob at
131    *     the corresponding index
132    * @param bottom
133    *     the input blobs, whose diff fields will store the gradient of the error
134    *     with respect to themselves after Backward is run
135    *
136    * The Backward wrapper calls the relevant device wrapper function
137    * (Backward_cpu or Backward_gpu) to compute the bottom blob diffs given the
138    * top blob diffs.
139    *
140    * Your layer should implement Forward_cpu and (optionally) Forward_gpu.
141    */
142   inline void Backward(const vector<Blob<Dtype>*>& top,
143       const vector<bool>& propagate_down,
144       const vector<Blob<Dtype>*>& bottom);
145
146   /**
147    * @brief Returns the vector of learnable parameter blobs.
148    */
149   vector<shared_ptr<Blob<Dtype> > >& blobs() {
150     return blobs_;
151   }
152
153   /**
154    * @brief Returns the layer parameter.
155    */
156   const LayerParameter& layer_param() const { return layer_param_; }
157
158   /**
159    * @brief Writes the layer parameter to a protocol buffer
160    */
161   virtual void ToProto(LayerParameter* param, bool write_diff = false);
162
163   /**
164    * @brief Returns the scalar loss associated with a top blob at a given index.
165    */
166   inline Dtype loss(const int top_index) const {
167     return (loss_.size() > top_index) ? loss_[top_index] : Dtype(0);
168   }
169
170   /**
171    * @brief Sets the loss associated with a top blob at a given index.
172    */
173   inline void set_loss(const int top_index, const Dtype value) {
174     if (loss_.size() <= top_index) {
175       loss_.resize(top_index + 1, Dtype(0));
176     }
177     loss_[top_index] = value;
178   }
179
180   /**
181    * @brief Returns the layer type as an enum value.
182    */
183   virtual inline LayerParameter_LayerType type() const {
184     return LayerParameter_LayerType_NONE;
185   }
186
187   /**
188    * @brief Returns the layer type name.
189    */
190   virtual inline const string& type_name() const {
191     return LayerParameter_LayerType_Name(type());
192   }
193
194   /**
195    * @brief Returns the exact number of bottom blobs required by the layer,
196    *        or -1 if no exact number is required.
197    *
198    * This method should be overridden to return a non-negative value if your
199    * layer expects some exact number of bottom blobs.
200    */
201   virtual inline int ExactNumBottomBlobs() const { return -1; }
202   /**
203    * @brief Returns the minimum number of bottom blobs required by the layer,
204    *        or -1 if no minimum number is required.
205    *
206    * This method should be overridden to return a non-negative value if your
207    * layer expects some minimum number of bottom blobs.
208    */
209   virtual inline int MinBottomBlobs() const { return -1; }
210   /**
211    * @brief Returns the maximum number of bottom blobs required by the layer,
212    *        or -1 if no maximum number is required.
213    *
214    * This method should be overridden to return a non-negative value if your
215    * layer expects some maximum number of bottom blobs.
216    */
217   virtual inline int MaxBottomBlobs() const { return -1; }
218   /**
219    * @brief Returns the exact number of top blobs required by the layer,
220    *        or -1 if no exact number is required.
221    *
222    * This method should be overridden to return a non-negative value if your
223    * layer expects some exact number of top blobs.
224    */
225   virtual inline int ExactNumTopBlobs() const { return -1; }
226   /**
227    * @brief Returns the minimum number of top blobs required by the layer,
228    *        or -1 if no minimum number is required.
229    *
230    * This method should be overridden to return a non-negative value if your
231    * layer expects some minimum number of top blobs.
232    */
233   virtual inline int MinTopBlobs() const { return -1; }
234   /**
235    * @brief Returns the maximum number of top blobs required by the layer,
236    *        or -1 if no maximum number is required.
237    *
238    * This method should be overridden to return a non-negative value if your
239    * layer expects some maximum number of top blobs.
240    */
241   virtual inline int MaxTopBlobs() const { return -1; }
242   /**
243    * @brief Returns true if the layer requires an equal number of bottom and
244    *        top blobs.
245    *
246    * This method should be overridden to return true if your layer expects an
247    * equal number of bottom and top blobs.
248    */
249   virtual inline bool EqualNumBottomTopBlobs() const { return false; }
250
251   /**
252    * @brief Return whether "anonymous" top blobs are created automatically
253    *        by the layer.
254    *
255    * If this method returns true, Net::Init will create enough "anonymous" top
256    * blobs to fulfill the requirement specified by ExactNumTopBlobs() or
257    * MinTopBlobs().
258    */
259   virtual inline bool AutoTopBlobs() const { return false; }
260
261   /**
262    * @brief Return whether to allow force_backward for a given bottom blob
263    *        index.
264    *
265    * If AllowForceBackward(i) == false, we will ignore the force_backward
266    * setting and backpropagate to blob i only if it needs gradient information
267    * (as is done when force_backward == false).
268    */
269   virtual inline bool AllowForceBackward(const int bottom_index) const {
270     return true;
271   }
272
273   /**
274    * @brief Specifies whether the layer should compute gradients w.r.t. a
275    *        parameter at a particular index given by param_id.
276    *
277    * You can safely ignore false values and always compute gradients
278    * for all parameters, but possibly with wasteful computation.
279    */
280   inline bool param_propagate_down(const int param_id) {
281     return (param_propagate_down_.size() > param_id) ?
282         param_propagate_down_[param_id] : false;
283   }
284   /**
285    * @brief Sets whether the layer should compute gradients w.r.t. a
286    *        parameter at a particular index given by param_id.
287    */
288   inline void set_param_propagate_down(const int param_id, const bool value) {
289     if (param_propagate_down_.size() <= param_id) {
290       param_propagate_down_.resize(param_id + 1, true);
291     }
292     param_propagate_down_[param_id] = value;
293   }
294
295
296  protected:
297   /** The protobuf that stores the layer parameters */
298   LayerParameter layer_param_;
299   /** The vector that stores the learnable parameters as a set of blobs. */
300   vector<shared_ptr<Blob<Dtype> > > blobs_;
301   /** Vector indicating whether to compute the diff of each param blob. */
302   vector<bool> param_propagate_down_;
303
304   /** The vector that indicates whether each top blob has a non-zero weight in
305    *  the objective function. */
306   vector<Dtype> loss_;
307
308   /** @brief Using the CPU device, compute the layer output. */
309   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
310       const vector<Blob<Dtype>*>& top) = 0;
311   /**
312    * @brief Using the GPU device, compute the layer output.
313    *        Fall back to Forward_cpu() if unavailable.
314    */
315   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
316       const vector<Blob<Dtype>*>& top) {
317     // LOG(WARNING) << "Using CPU code as backup.";
318     return Forward_cpu(bottom, top);
319   }
320
321   /**
322    * @brief Using the CPU device, compute the gradients for any parameters and
323    *        for the bottom blobs if propagate_down is true.
324    */
325   virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
326       const vector<bool>& propagate_down,
327       const vector<Blob<Dtype>*>& bottom) = 0;
328   /**
329    * @brief Using the GPU device, compute the gradients for any parameters and
330    *        for the bottom blobs if propagate_down is true.
331    *        Fall back to Backward_cpu() if unavailable.
332    */
333   virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
334       const vector<bool>& propagate_down,
335       const vector<Blob<Dtype>*>& bottom) {
336     // LOG(WARNING) << "Using CPU code as backup.";
337     Backward_cpu(top, propagate_down, bottom);
338   }
339
340   /**
341    * Called by the parent Layer's SetUp to check that the number of bottom
342    * and top Blobs provided as input match the expected numbers specified by
343    * the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.
344    */
345   virtual void CheckBlobCounts(const vector<Blob<Dtype>*>& bottom,
346                                const vector<Blob<Dtype>*>& top) {
347     if (ExactNumBottomBlobs() >= 0) {
348       CHECK_EQ(ExactNumBottomBlobs(), bottom.size())
349           << type_name() << " Layer takes " << ExactNumBottomBlobs()
350           << " bottom blob(s) as input.";
351     }
352     if (MinBottomBlobs() >= 0) {
353       CHECK_LE(MinBottomBlobs(), bottom.size())
354           << type_name() << " Layer takes at least " << MinBottomBlobs()
355           << " bottom blob(s) as input.";
356     }
357     if (MaxBottomBlobs() >= 0) {
358       CHECK_GE(MaxBottomBlobs(), bottom.size())
359           << type_name() << " Layer takes at most " << MaxBottomBlobs()
360           << " bottom blob(s) as input.";
361     }
362     if (ExactNumTopBlobs() >= 0) {
363       CHECK_EQ(ExactNumTopBlobs(), top.size())
364           << type_name() << " Layer produces " << ExactNumTopBlobs()
365           << " top blob(s) as output.";
366     }
367     if (MinTopBlobs() >= 0) {
368       CHECK_LE(MinTopBlobs(), top.size())
369           << type_name() << " Layer produces at least " << MinTopBlobs()
370           << " top blob(s) as output.";
371     }
372     if (MaxTopBlobs() >= 0) {
373       CHECK_GE(MaxTopBlobs(), top.size())
374           << type_name() << " Layer produces at most " << MaxTopBlobs()
375           << " top blob(s) as output.";
376     }
377     if (EqualNumBottomTopBlobs()) {
378       CHECK_EQ(bottom.size(), top.size())
379           << type_name() << " Layer produces one top blob as output for each "
380           << "bottom blob input.";
381     }
382   }
383
384   /**
385    * Called by SetUp to initialize the weights associated with any top blobs in
386    * the loss function. Store non-zero loss weights in the diff blob.
387    */
388   inline void SetLossWeights(const vector<Blob<Dtype>*>& top) {
389     const int num_loss_weights = layer_param_.loss_weight_size();
390     if (num_loss_weights) {
391       CHECK_EQ(top.size(), num_loss_weights) << "loss_weight must be "
392           "unspecified or specified once per top blob.";
393       for (int top_id = 0; top_id < top.size(); ++top_id) {
394         const Dtype loss_weight = layer_param_.loss_weight(top_id);
395         if (loss_weight == Dtype(0)) { continue; }
396         this->set_loss(top_id, loss_weight);
397         const int count = top[top_id]->count();
398         Dtype* loss_multiplier = top[top_id]->mutable_cpu_diff();
399         caffe_set(count, loss_weight, loss_multiplier);
400       }
401     }
402   }
403
404   DISABLE_COPY_AND_ASSIGN(Layer);
405 };  // class Layer
406
407 // Forward and backward wrappers. You should implement the cpu and
408 // gpu specific implementations instead, and should not change these
409 // functions.
410 template <typename Dtype>
411 inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
412     const vector<Blob<Dtype>*>& top) {
413   Dtype loss = 0;
414   switch (Caffe::mode()) {
415   case Caffe::CPU:
416     Forward_cpu(bottom, top);
417     for (int top_id = 0; top_id < top.size(); ++top_id) {
418       if (!this->loss(top_id)) { continue; }
419       const int count = top[top_id]->count();
420       const Dtype* data = top[top_id]->cpu_data();
421       const Dtype* loss_weights = top[top_id]->cpu_diff();
422       loss += caffe_cpu_dot(count, data, loss_weights);
423     }
424     break;
425   case Caffe::GPU:
426     Forward_gpu(bottom, top);
427 #ifndef CPU_ONLY
428     for (int top_id = 0; top_id < top.size(); ++top_id) {
429       if (!this->loss(top_id)) { continue; }
430       const int count = top[top_id]->count();
431       const Dtype* data = top[top_id]->gpu_data();
432       const Dtype* loss_weights = top[top_id]->gpu_diff();
433       Dtype blob_loss = 0;
434       caffe_gpu_dot(count, data, loss_weights, &blob_loss);
435       loss += blob_loss;
436     }
437 #endif
438     break;
439   default:
440     LOG(FATAL) << "Unknown caffe mode.";
441   }
442   return loss;
443 }
444
445 template <typename Dtype>
446 inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
447     const vector<bool>& propagate_down,
448     const vector<Blob<Dtype>*>& bottom) {
449   switch (Caffe::mode()) {
450   case Caffe::CPU:
451     Backward_cpu(top, propagate_down, bottom);
452     break;
453   case Caffe::GPU:
454     Backward_gpu(top, propagate_down, bottom);
455     break;
456   default:
457     LOG(FATAL) << "Unknown caffe mode.";
458   }
459 }
460
461 // Serialize LayerParameter to protocol buffer
462 template <typename Dtype>
463 void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
464   param->Clear();
465   param->CopyFrom(layer_param_);
466   param->clear_blobs();
467   for (int i = 0; i < blobs_.size(); ++i) {
468     blobs_[i]->ToProto(param->add_blobs(), write_diff);
469   }
470 }
471
472 // The layer factory function
473 template <typename Dtype>
474 Layer<Dtype>* GetLayer(const LayerParameter& param);
475
476 }  // namespace caffe
477
478 #endif  // CAFFE_LAYER_H_