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