layer.hpp
#ifndef CAFFE_LAYER_H_
#define CAFFE_LAYER_H_
#include <algorithm>
#include <string>
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer_factory.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/math_functions.hpp"
namespace boost { class mutex; }
namespace caffe {
template <typename Dtype>
class Layer {
public:
explicit Layer(const LayerParameter& param)
: layer_param_(param) {
// Set phase and copy blobs (if there are any).
phase_ = param.phase();
if (layer_param_.blobs_size() > 0) {
blobs_.resize(layer_param_.blobs_size());
for (int i = 0; i < layer_param_.blobs_size(); ++i) {
blobs_[i].reset(new Blob<Dtype>());
blobs_[i]->FromProto(layer_param_.blobs(i));
}
}
}
virtual ~Layer() {}
void SetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
CheckBlobCounts(bottom, top);
LayerSetUp(bottom, top);
Reshape(bottom, top);
SetLossWeights(top);
}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {}
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) = 0;
inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
inline void Backward(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom);
vector<shared_ptr<Blob<Dtype> > >& blobs() {
return blobs_;
}
const LayerParameter& layer_param() const { return layer_param_; }
virtual void ToProto(LayerParameter* param, bool write_diff = false);
inline Dtype loss(const int top_index) const {
return (loss_.size() > top_index) ? loss_[top_index] : Dtype(0);
}
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;
}
virtual inline const char* type() const { return ""; }
virtual inline int ExactNumBottomBlobs() const { return -1; }
virtual inline int MinBottomBlobs() const { return -1; }
virtual inline int MaxBottomBlobs() const { return -1; }
virtual inline int ExactNumTopBlobs() const { return -1; }
virtual inline int MinTopBlobs() const { return -1; }
virtual inline int MaxTopBlobs() const { return -1; }
virtual inline bool EqualNumBottomTopBlobs() const { return false; }
virtual inline bool AutoTopBlobs() const { return false; }
virtual inline bool AllowForceBackward(const int bottom_index) const {
return true;
}
inline bool param_propagate_down(const int param_id) {
return (param_propagate_down_.size() > param_id) ?
param_propagate_down_[param_id] : false;
}
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);
}
param_propagate_down_[param_id] = value;
}
protected:
LayerParameter layer_param_;
Phase phase_;
vector<shared_ptr<Blob<Dtype> > > blobs_;
vector<bool> param_propagate_down_;
vector<Dtype> loss_;
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) = 0;
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
// LOG(WARNING) << "Using CPU code as backup.";
return Forward_cpu(bottom, top);
}
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) = 0;
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
// LOG(WARNING) << "Using CPU code as backup.";
Backward_cpu(top, propagate_down, bottom);
}
virtual void CheckBlobCounts(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
if (ExactNumBottomBlobs() >= 0) {
CHECK_EQ(ExactNumBottomBlobs(), bottom.size())
<< type() << " Layer takes " << ExactNumBottomBlobs()
<< " bottom blob(s) as input.";
}
if (MinBottomBlobs() >= 0) {
CHECK_LE(MinBottomBlobs(), bottom.size())
<< type() << " Layer takes at least " << MinBottomBlobs()
<< " bottom blob(s) as input.";
}
if (MaxBottomBlobs() >= 0) {
CHECK_GE(MaxBottomBlobs(), bottom.size())
<< type() << " Layer takes at most " << MaxBottomBlobs()
<< " bottom blob(s) as input.";
}
if (ExactNumTopBlobs() >= 0) {
CHECK_EQ(ExactNumTopBlobs(), top.size())
<< type() << " Layer produces " << ExactNumTopBlobs()
<< " top blob(s) as output.";
}
if (MinTopBlobs() >= 0) {
CHECK_LE(MinTopBlobs(), top.size())
<< type() << " Layer produces at least " << MinTopBlobs()
<< " top blob(s) as output.";
}
if (MaxTopBlobs() >= 0) {
CHECK_GE(MaxTopBlobs(), top.size())
<< type() << " Layer produces at most " << MaxTopBlobs()
<< " top blob(s) as output.";
}
if (EqualNumBottomTopBlobs()) {
CHECK_EQ(bottom.size(), top.size())
<< type() << " Layer produces one top blob as output for each "
<< "bottom blob input.";
}
}
inline void SetLossWeights(const 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);
}
}
}
private:
DISABLE_COPY_AND_ASSIGN(Layer);
}; // class Layer
// Forward and backward wrappers. You should implement the cpu and
// gpu specific implementations instead, and should not change these
// functions.
template <typename Dtype>
inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
Dtype loss = 0;
Reshape(bottom, top);
switch (Caffe::mode()) {
case Caffe::CPU:
Forward_cpu(bottom, top);
for (int top_id = 0; top_id < top.size(); ++top_id) {
if (!this->loss(top_id)) { continue; }
const int count = top[top_id]->count();
const Dtype* data = top[top_id]->cpu_data();
const Dtype* loss_weights = top[top_id]->cpu_diff();
loss += caffe_cpu_dot(count, data, loss_weights);
}
break;
case Caffe::GPU:
Forward_gpu(bottom, top);
#ifndef CPU_ONLY
for (int top_id = 0; top_id < top.size(); ++top_id) {
if (!this->loss(top_id)) { continue; }
const int count = top[top_id]->count();
const Dtype* data = top[top_id]->gpu_data();
const Dtype* loss_weights = top[top_id]->gpu_diff();
Dtype blob_loss = 0;
caffe_gpu_dot(count, data, loss_weights, &blob_loss);
loss += blob_loss;
}
#endif
break;
default:
LOG(FATAL) << "Unknown caffe mode.";
}
return loss;
}
template <typename Dtype>
inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
switch (Caffe::mode()) {
case Caffe::CPU:
Backward_cpu(top, propagate_down, bottom);
break;
case Caffe::GPU:
Backward_gpu(top, propagate_down, bottom);
break;
default:
LOG(FATAL) << "Unknown caffe mode.";
}
}
// Serialize LayerParameter to protocol buffer
template <typename Dtype>
void Layer<Dtype>::ToProto(LayerParameter* param, bool write_diff) {
param->Clear();
param->CopyFrom(layer_param_);
param->clear_blobs();
for (int i = 0; i < blobs_.size(); ++i) {
blobs_[i]->ToProto(param->add_blobs(), write_diff);
}
}
} // namespace caffe
#endif // CAFFE_LAYER_H_
caffe::Layer::param_propagate_down_
vector< bool > param_propagate_down_
Definition: layer.hpp:303
caffe::Layer::Reshape
virtual void Reshape(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)=0
Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs.
caffe::Layer::CheckBlobCounts
virtual void CheckBlobCounts(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Definition: layer.hpp:346
caffe::Layer::AutoTopBlobs
virtual bool AutoTopBlobs() const
Return whether "anonymous" top blobs are created automatically by the layer.
Definition: layer.hpp:258
caffe::Layer::Backward_cpu
virtual void Backward_cpu(const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)=0
Using the CPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
boost
Definition: internal_thread.hpp:10
caffe::Layer::EqualNumBottomTopBlobs
virtual bool EqualNumBottomTopBlobs() const
Returns true if the layer requires an equal number of bottom and top blobs.
Definition: layer.hpp:248
caffe::Layer::MinTopBlobs
virtual int MinTopBlobs() const
Returns the minimum number of top blobs required by the layer, or -1 if no minimum number is required...
Definition: layer.hpp:232
caffe::Layer::Forward
Dtype Forward(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Given the bottom blobs, compute the top blobs and the loss.
Definition: layer.hpp:413
caffe::Layer::ExactNumTopBlobs
virtual int ExactNumTopBlobs() const
Returns the exact number of top blobs required by the layer, or -1 if no exact number is required.
Definition: layer.hpp:224
caffe::Layer::Backward
void Backward(const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)
Given the top blob error gradients, compute the bottom blob error gradients.
Definition: layer.hpp:449
caffe::Layer::blobs
vector< shared_ptr< Blob< Dtype > > > & blobs()
Returns the vector of learnable parameter blobs.
Definition: layer.hpp:157
caffe::Layer::blobs_
vector< shared_ptr< Blob< Dtype > > > blobs_
Definition: layer.hpp:301
caffe::Layer::AllowForceBackward
virtual bool AllowForceBackward(const int bottom_index) const
Return whether to allow force_backward for a given bottom blob index.
Definition: layer.hpp:268
caffe::Layer::set_param_propagate_down
void set_param_propagate_down(const int param_id, const bool value)
Sets whether the layer should compute gradients w.r.t. a parameter at a particular index given by par...
Definition: layer.hpp:287
caffe::Layer::loss_
vector< Dtype > loss_
Definition: layer.hpp:307
caffe::Blob
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers,...
Definition: blob.hpp:24
caffe::Layer::LayerSetUp
virtual void LayerSetUp(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Does layer-specific setup: your layer should implement this function as well as Reshape.
Definition: layer.hpp:91
caffe::Layer::MaxBottomBlobs
virtual int MaxBottomBlobs() const
Returns the maximum number of bottom blobs required by the layer, or -1 if no maximum number is requi...
Definition: layer.hpp:216
caffe::Layer::SetUp
void SetUp(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Implements common layer setup functionality.
Definition: layer.hpp:67
caffe::Layer
An interface for the units of computation which can be composed into a Net.
Definition: layer.hpp:33
caffe::Layer::Backward_gpu
virtual void Backward_gpu(const vector< Blob< Dtype > * > &top, const vector< bool > &propagate_down, const vector< Blob< Dtype > * > &bottom)
Using the GPU device, compute the gradients for any parameters and for the bottom blobs if propagate_...
Definition: layer.hpp:334
caffe::Layer::MaxTopBlobs
virtual int MaxTopBlobs() const
Returns the maximum number of top blobs required by the layer, or -1 if no maximum number is required...
Definition: layer.hpp:240
caffe::Layer::MinBottomBlobs
virtual int MinBottomBlobs() const
Returns the minimum number of bottom blobs required by the layer, or -1 if no minimum number is requi...
Definition: layer.hpp:208
caffe::Layer::set_loss
void set_loss(const int top_index, const Dtype value)
Sets the loss associated with a top blob at a given index.
Definition: layer.hpp:181
caffe::Layer::layer_param_
LayerParameter layer_param_
Definition: layer.hpp:297
caffe::Layer::param_propagate_down
bool param_propagate_down(const int param_id)
Specifies whether the layer should compute gradients w.r.t. a parameter at a particular index given b...
Definition: layer.hpp:279
caffe::Layer::Forward_gpu
virtual void Forward_gpu(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)
Using the GPU device, compute the layer output. Fall back to Forward_cpu() if unavailable.
Definition: layer.hpp:316
caffe::Layer::ToProto
virtual void ToProto(LayerParameter *param, bool write_diff=false)
Writes the layer parameter to a protocol buffer.
Definition: layer.hpp:466
caffe::Layer::SetLossWeights
void SetLossWeights(const vector< Blob< Dtype > * > &top)
Definition: layer.hpp:389
caffe::Layer::Forward_cpu
virtual void Forward_cpu(const vector< Blob< Dtype > * > &bottom, const vector< Blob< Dtype > * > &top)=0
Using the CPU device, compute the layer output.
caffe
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
caffe::Layer::phase_
Phase phase_
Definition: layer.hpp:299
caffe::Layer::layer_param
const LayerParameter & layer_param() const
Returns the layer parameter.
Definition: layer.hpp:164
caffe::Layer::Layer
Layer(const LayerParameter &param)
Definition: layer.hpp:40
caffe::Layer::ExactNumBottomBlobs
virtual int ExactNumBottomBlobs() const
Returns the exact number of bottom blobs required by the layer, or -1 if no exact number is required.
Definition: layer.hpp:200
caffe::Layer::loss
Dtype loss(const int top_index) const
Returns the scalar loss associated with a top blob at a given index.
Definition: layer.hpp:174
caffe::Layer::type
virtual const char * type() const
Returns the layer type.
Definition: layer.hpp:191