namespace caffe {
+/**
+ * @brief An interface for the units of computation which can be composed into a
+ * Net.
+ *
+ * Layer&s must implement a Forward function, in which they take their input
+ * (bottom) Blob&s (if any) and compute their output Blob&s (if any).
+ * They may also implement a Backward function, in which they compute the error
+ * gradients with respect to their input Blob&s, given the error gradients with
+ * their output Blob&s.
+ */
template <typename Dtype>
class Layer {
public:
- // You should not implement your own constructor. Any set up code should go
- // to SetUp(), where the dimensions of the bottom blobs are provided to the
- // layer.
+ /**
+ * You should not implement your own constructor. Any set up code should go
+ * to SetUp(), where the dimensions of the bottom blobs are provided to the
+ * layer.
+ */
explicit Layer(const LayerParameter& param)
: layer_param_(param) {
// The only thing we do is to copy blobs if there are any.
}
}
virtual ~Layer() {}
- // SetUp: implements common layer setup functionality, and calls
- // LayerSetUp to do special layer setup for individual layer types.
- // This method may not be overridden.
+
+ /**
+ * @brief Implements common layer setup functionality.
+ *
+ * @param bottom the preshaped input blobs
+ * @param top
+ * the allocated but unshaped output blobs, to be shaped by LayerSetUp
+ *
+ * Checks that the number of bottom and top blobs is correct.
+ * Calls LayerSetUp to do special layer setup for individual layer types.
+ * Sets up the loss weight multiplier blobs for any non-zero loss weights.
+ * This method may not be overridden.
+ */
void SetUp(const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {
CheckBlobCounts(bottom, *top);
LayerSetUp(bottom, top);
SetLossWeights(top);
}
- // LayerSetUp: your layer should implement this.
+
+ /**
+ * @brief Does layer-specific setup: your layer should implement this.
+ *
+ * @param bottom
+ * the preshaped input blobs, whose data fields store the input data for
+ * this layer
+ * @param top
+ * the allocated but unshaped output blobs, to be initialized by LayerSetUp
+ *
+ * This method should be used to do layer-specific setup. At a minimum, this
+ * includes reshaping the empty top blobs to the shape as dictated by the
+ * shapes of the bottom blobs and any relevant parameters from the
+ * <code>layer_param_</code>.
+ */
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) { NOT_IMPLEMENTED; }
- // Forward and backward wrappers. You should implement the cpu and
- // gpu specific implementations instead, and should not change these
- // functions.
+ /**
+ * @brief Given the bottom blobs, compute the top blobs and the loss.
+ *
+ * @param bottom
+ * the input blobs, whose data fields store the input data for this layer
+ * @param top
+ * the preshaped output blobs, whose data fields will store this layers'
+ * outputs
+ * \return The total loss from the layer.
+ *
+ * The Forward wrapper calls the relevant device wrapper function
+ * (Forward_cpu or Forward_gpu) to compute the top blob values given the
+ * bottom blobs. If the layer has any non-zero loss_weights, the wrapper
+ * then computes and returns the loss.
+ *
+ * Your layer should implement Forward_cpu and (optionally) Forward_gpu.
+ */
inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);
+
+ /**
+ * @brief Given the top blob error gradients, compute the bottom blob error
+ * gradients.
+ *
+ * @param top
+ * the output blobs, whose diff fields store the gradient of the error
+ * with respect to themselves
+ * @param propagate_down
+ * a vector with equal length to bottom, with each index indicating
+ * whether to propagate the error gradients down to the bottom blob at
+ * the corresponding index
+ * @param bottom
+ * the input blobs, whose diff fields will store the gradient of the error
+ * with respect to themselves after Backward is run
+ *
+ * The Backward wrapper calls the relevant device wrapper function
+ * (Backward_cpu or Backward_gpu) to compute the bottom blob diffs given the
+ * top blob diffs.
+ *
+ * Your layer should implement Forward_cpu and (optionally) Forward_gpu.
+ */
inline void Backward(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
vector<Blob<Dtype>*>* bottom);
- // Returns the vector of blobs.
+ /**
+ * @brief Returns the vector of learnable parameter blobs.
+ */
vector<shared_ptr<Blob<Dtype> > >& blobs() {
return blobs_;
}
- // Returns the layer parameter
+ /**
+ * @brief Returns the layer parameter.
+ */
const LayerParameter& layer_param() const { return layer_param_; }
- // Writes the layer parameter to a protocol buffer
+
+ /**
+ * @brief Writes the layer parameter to a protocol buffer
+ */
virtual void ToProto(LayerParameter* param, bool write_diff = false);
+ /**
+ * @brief Returns the scalar loss associated with a top blob at a given index.
+ */
inline Dtype loss(const int top_index) const {
return (loss_.size() > top_index) ? loss_[top_index] : Dtype(0);
}
+
+ /**
+ * @brief Sets the loss associated with a top blob at a given index.
+ */
inline void set_loss(const int top_index, const Dtype value) {
if (loss_.size() <= top_index) {
loss_.resize(top_index + 1, Dtype(0));
}
loss_[top_index] = value;
}
- // Setup the weights associated with each top blob in the loss function.
- // Store non-zero loss weights in the diff blob.
- inline void SetLossWeights(vector<Blob<Dtype>*>* top) {
- const int num_loss_weights = layer_param_.loss_weight_size();
- if (num_loss_weights) {
- CHECK_EQ(top->size(), num_loss_weights) << "loss_weight must be "
- "unspecified or specified once per top blob.";
- for (int top_id = 0; top_id < top->size(); ++top_id) {
- const Dtype loss_weight = layer_param_.loss_weight(top_id);
- if (loss_weight == Dtype(0)) { continue; }
- this->set_loss(top_id, loss_weight);
- const int count = (*top)[top_id]->count();
- Dtype* loss_multiplier = (*top)[top_id]->mutable_cpu_diff();
- caffe_set(count, loss_weight, loss_multiplier);
- }
- }
- }
- // Returns the layer type as an enum value.
+ /**
+ * @brief Returns the layer type as an enum value.
+ */
virtual inline LayerParameter_LayerType type() const {
return LayerParameter_LayerType_NONE;
}
- // Returns the layer type name.
+ /**
+ * @brief Returns the layer type name.
+ */
virtual inline const string& type_name() const {
return LayerParameter_LayerType_Name(type());
}
- // These methods can be overwritten to declare that this layer type expects
- // a certain number of blobs as input and output.
- //
- // ExactNum{Bottom,Top}Blobs return a non-negative number to require an exact
- // number of bottom/top blobs; the Min/Max versions return a non-negative
- // number to require a minimum and/or maximum number of blobs.
- // If Exact is specified, neither Min nor Max should be specified, and vice
- // versa. These methods may not rely on SetUp having been called.
+ /**
+ * @brief Returns the exact number of bottom blobs required by the layer,
+ * or -1 if no exact number is required.
+ *
+ * This method should be overridden to return a non-negative value if your
+ * layer expects some exact number of bottom blobs.
+ */
virtual inline int ExactNumBottomBlobs() const { return -1; }
+ /**
+ * @brief Returns the minimum number of bottom blobs required by the layer,
+ * or -1 if no minimum number is required.
+ *
+ * This method should be overridden to return a non-negative value if your
+ * layer expects some minimum number of bottom blobs.
+ */
virtual inline int MinBottomBlobs() const { return -1; }
+ /**
+ * @brief Returns the maximum number of bottom blobs required by the layer,
+ * or -1 if no maximum number is required.
+ *
+ * This method should be overridden to return a non-negative value if your
+ * layer expects some maximum number of bottom blobs.
+ */
virtual inline int MaxBottomBlobs() const { return -1; }
+ /**
+ * @brief Returns the exact number of top blobs required by the layer,
+ * or -1 if no exact number is required.
+ *
+ * This method should be overridden to return a non-negative value if your
+ * layer expects some exact number of top blobs.
+ */
virtual inline int ExactNumTopBlobs() const { return -1; }
+ /**
+ * @brief Returns the minimum number of top blobs required by the layer,
+ * or -1 if no minimum number is required.
+ *
+ * This method should be overridden to return a non-negative value if your
+ * layer expects some minimum number of top blobs.
+ */
virtual inline int MinTopBlobs() const { return -1; }
+ /**
+ * @brief Returns the maximum number of top blobs required by the layer,
+ * or -1 if no maximum number is required.
+ *
+ * This method should be overridden to return a non-negative value if your
+ * layer expects some maximum number of top blobs.
+ */
virtual inline int MaxTopBlobs() const { return -1; }
+ /**
+ * @brief Returns true if the layer requires an equal number of bottom and
+ * top blobs.
+ *
+ * This method should be overridden to return true if your layer expects an
+ * equal number of bottom and top blobs.
+ */
+ virtual inline bool EqualNumBottomTopBlobs() const { return false; }
- // AutoTopBlobs may be overridden with a positive integer to automatically
- // create enough "anonymous" top blobs to fulfill the requirement specified
- // by ExactNumTopBlobs() or MinTopBlobs().
+ /**
+ * @brief Return whether "anonymous" top blobs are created automatically
+ * by the layer.
+ *
+ * If this method returns true, Net::Init will create enough "anonymous" top
+ * blobs to fulfill the requirement specified by ExactNumTopBlobs() or
+ * MinTopBlobs().
+ */
virtual inline bool AutoTopBlobs() const { return false; }
- // EqualNumBottomTopBlobs should return true for layers requiring an equal
- // number of bottom and top blobs.
- virtual inline bool EqualNumBottomTopBlobs() const { return false; }
-
- // Declare for each bottom blob whether to allow force_backward -- that is,
- // if AllowForceBackward(i) == false, we will ignore the force_backward
- // setting and backpropagate to blob i only if it needs gradient information
- // (as is done when force_backward == false).
+ /**
+ * @brief Return whether to allow force_backward for a given bottom blob
+ * index.
+ *
+ * If AllowForceBackward(i) == false, we will ignore the force_backward
+ * setting and backpropagate to blob i only if it needs gradient information
+ * (as is done when force_backward == false).
+ */
virtual inline bool AllowForceBackward(const int bottom_index) const {
return true;
}
- // param_propagate_down specifies whether the layer should compute gradients
- // in Backward. You can safely ignore false and always compute gradients
- // for all parameters, but possibly with wasteful computation.
+ /**
+ * @brief Specifies whether the layer should compute gradients w.r.t. a
+ * parameter at a particular index given by param_id.
+ *
+ * You can safely ignore false values and always compute gradients
+ * for all parameters, but possibly with wasteful computation.
+ */
inline bool param_propagate_down(const int param_id) {
return (param_propagate_down_.size() > param_id) ?
param_propagate_down_[param_id] : false;
}
+ /**
+ * @brief Sets whether the layer should compute gradients w.r.t. a
+ * parameter at a particular index given by param_id.
+ */
inline void set_param_propagate_down(const int param_id, const bool value) {
if (param_propagate_down_.size() <= param_id) {
param_propagate_down_.resize(param_id + 1, true);
protected:
- // The protobuf that stores the layer parameters
+ /** The protobuf that stores the layer parameters */
LayerParameter layer_param_;
- // The vector that stores the parameters as a set of blobs.
+ /** The vector that stores the learnable parameters as a set of blobs. */
vector<shared_ptr<Blob<Dtype> > > blobs_;
- // Vector indicating whether to compute the diff of each param blob.
+ /** Vector indicating whether to compute the diff of each param blob. */
vector<bool> param_propagate_down_;
- // The vector that indicates whether each top blob has a non-zero weight in
- // the objective function.
+ /** The vector that indicates whether each top blob has a non-zero weight in
+ * the objective function. */
vector<Dtype> loss_;
- // Forward functions: compute the layer output
- // (and loss layers return the loss; other layers return the dummy value 0.)
+ /** @brief Using the CPU device, compute the layer output. */
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) = 0;
- // If no gpu code is provided, we will simply use cpu code.
+ /**
+ * @brief Using the GPU device, compute the layer output.
+ * Fall back to Forward_cpu() if unavailable.
+ */
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
// LOG(WARNING) << "Using CPU code as backup.";
return Forward_cpu(bottom, top);
}
- // Backward functions: compute the gradients for any parameters and
- // for the bottom blobs if propagate_down is true.
+ /**
+ * @brief Using the CPU device, compute the gradients for any parameters and
+ * for the bottom blobs if propagate_down is true.
+ */
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
vector<Blob<Dtype>*>* bottom) = 0;
+ /**
+ * @brief Using the GPU device, compute the gradients for any parameters and
+ * for the bottom blobs if propagate_down is true.
+ * Fall back to Backward_cpu() if unavailable.
+ */
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
vector<Blob<Dtype>*>* bottom) {
Backward_cpu(top, propagate_down, bottom);
}
- // CheckBlobCounts: called by the parent Layer's SetUp to check that the
- // number of bottom and top Blobs provided as input match the expected
- // numbers specified by the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.
+ /**
+ * Called by the parent Layer's SetUp to check that the number of bottom
+ * and top Blobs provided as input match the expected numbers specified by
+ * the {ExactNum,Min,Max}{Bottom,Top}Blobs() functions.
+ */
virtual void CheckBlobCounts(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
if (ExactNumBottomBlobs() >= 0) {
}
}
+ /**
+ * Called by SetUp to initialize the weights associated with any top blobs in
+ * the loss function. Store non-zero loss weights in the diff blob.
+ */
+ inline void SetLossWeights(vector<Blob<Dtype>*>* top) {
+ const int num_loss_weights = layer_param_.loss_weight_size();
+ if (num_loss_weights) {
+ CHECK_EQ(top->size(), num_loss_weights) << "loss_weight must be "
+ "unspecified or specified once per top blob.";
+ for (int top_id = 0; top_id < top->size(); ++top_id) {
+ const Dtype loss_weight = layer_param_.loss_weight(top_id);
+ if (loss_weight == Dtype(0)) { continue; }
+ this->set_loss(top_id, loss_weight);
+ const int count = (*top)[top_id]->count();
+ Dtype* loss_multiplier = (*top)[top_id]->mutable_cpu_diff();
+ caffe_set(count, loss_weight, loss_multiplier);
+ }
+ }
+ }
+
DISABLE_COPY_AND_ASSIGN(Layer);
}; // class Layer