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/math_functions.hpp" 18 namespace boost {
class mutex; }
32 template <
typename Dtype>
40 explicit Layer(
const LayerParameter& param)
41 : layer_param_(param), is_shared_(false) {
43 phase_ = param.phase();
44 if (layer_param_.blobs_size() > 0) {
45 blobs_.resize(layer_param_.blobs_size());
46 for (
int i = 0; i < layer_param_.blobs_size(); ++i) {
48 blobs_[i]->FromProto(layer_param_.blobs(i));
70 CheckBlobCounts(bottom, top);
71 LayerSetUp(bottom, top);
107 inline bool IsShared()
const {
return is_shared_; }
114 CHECK(ShareInParallel() || !is_shared)
115 << type() <<
"Layer does not support sharing.";
116 is_shared_ = is_shared;
131 virtual void Reshape(
const vector<
Blob<Dtype>*>& bottom,
151 inline Dtype Forward(
const vector<
Blob<Dtype>*>& bottom,
175 inline void Backward(
const vector<
Blob<Dtype>*>& top,
176 const vector<bool>& propagate_down,
182 vector<shared_ptr<Blob<Dtype> > >&
blobs() {
189 const LayerParameter&
layer_param()
const {
return layer_param_; }
194 virtual void ToProto(LayerParameter* param,
bool write_diff =
false);
199 inline Dtype
loss(
const int top_index)
const {
200 return (loss_.size() > top_index) ? loss_[top_index] : Dtype(0);
206 inline void set_loss(
const int top_index,
const Dtype value) {
207 if (loss_.size() <= top_index) {
208 loss_.resize(top_index + 1, Dtype(0));
210 loss_[top_index] = value;
216 virtual inline const char*
type()
const {
return ""; }
305 return (param_propagate_down_.size() > param_id) ?
306 param_propagate_down_[param_id] :
false;
313 if (param_propagate_down_.size() <= param_id) {
314 param_propagate_down_.resize(param_id + 1,
true);
316 param_propagate_down_[param_id] = value;
326 vector<shared_ptr<Blob<Dtype> > >
blobs_;
335 virtual void Forward_cpu(
const vector<
Blob<Dtype>*>& bottom,
344 return Forward_cpu(bottom, top);
351 virtual void Backward_cpu(
const vector<
Blob<Dtype>*>& top,
352 const vector<bool>& propagate_down,
360 const vector<bool>& propagate_down,
363 Backward_cpu(top, propagate_down, bottom);
373 if (ExactNumBottomBlobs() >= 0) {
374 CHECK_EQ(ExactNumBottomBlobs(), bottom.size())
375 << type() <<
" Layer takes " << ExactNumBottomBlobs()
376 <<
" bottom blob(s) as input.";
378 if (MinBottomBlobs() >= 0) {
379 CHECK_LE(MinBottomBlobs(), bottom.size())
380 << type() <<
" Layer takes at least " << MinBottomBlobs()
381 <<
" bottom blob(s) as input.";
383 if (MaxBottomBlobs() >= 0) {
384 CHECK_GE(MaxBottomBlobs(), bottom.size())
385 << type() <<
" Layer takes at most " << MaxBottomBlobs()
386 <<
" bottom blob(s) as input.";
388 if (ExactNumTopBlobs() >= 0) {
389 CHECK_EQ(ExactNumTopBlobs(), top.size())
390 << type() <<
" Layer produces " << ExactNumTopBlobs()
391 <<
" top blob(s) as output.";
393 if (MinTopBlobs() >= 0) {
394 CHECK_LE(MinTopBlobs(), top.size())
395 << type() <<
" Layer produces at least " << MinTopBlobs()
396 <<
" top blob(s) as output.";
398 if (MaxTopBlobs() >= 0) {
399 CHECK_GE(MaxTopBlobs(), top.size())
400 << type() <<
" Layer produces at most " << MaxTopBlobs()
401 <<
" top blob(s) as output.";
403 if (EqualNumBottomTopBlobs()) {
404 CHECK_EQ(bottom.size(), top.size())
405 << type() <<
" Layer produces one top blob as output for each " 406 <<
"bottom blob input.";
415 const int num_loss_weights = layer_param_.loss_weight_size();
416 if (num_loss_weights) {
417 CHECK_EQ(top.size(), num_loss_weights) <<
"loss_weight must be " 418 "unspecified or specified once per top blob.";
419 for (
int top_id = 0; top_id < top.size(); ++top_id) {
420 const Dtype loss_weight = layer_param_.loss_weight(top_id);
421 if (loss_weight == Dtype(0)) {
continue; }
422 this->set_loss(top_id, loss_weight);
423 const int count = top[top_id]->count();
424 Dtype* loss_multiplier = top[top_id]->mutable_cpu_diff();
425 caffe_set(count, loss_weight, loss_multiplier);
435 shared_ptr<boost::mutex> forward_mutex_;
444 DISABLE_COPY_AND_ASSIGN(
Layer);
450 template <
typename Dtype>
456 Reshape(bottom, top);
457 switch (Caffe::mode()) {
459 Forward_cpu(bottom, top);
460 for (
int top_id = 0; top_id < top.size(); ++top_id) {
461 if (!this->loss(top_id)) {
continue; }
462 const int count = top[top_id]->count();
463 const Dtype* data = top[top_id]->cpu_data();
464 const Dtype* loss_weights = top[top_id]->cpu_diff();
465 loss += caffe_cpu_dot(count, data, loss_weights);
469 Forward_gpu(bottom, top);
471 for (
int top_id = 0; top_id < top.size(); ++top_id) {
472 if (!this->loss(top_id)) {
continue; }
473 const int count = top[top_id]->count();
474 const Dtype* data = top[top_id]->gpu_data();
475 const Dtype* loss_weights = top[top_id]->gpu_diff();
477 caffe_gpu_dot(count, data, loss_weights, &blob_loss);
483 LOG(FATAL) <<
"Unknown caffe mode.";
489 template <
typename Dtype>
491 const vector<bool>& propagate_down,
493 switch (Caffe::mode()) {
495 Backward_cpu(top, propagate_down, bottom);
498 Backward_gpu(top, propagate_down, bottom);
501 LOG(FATAL) <<
"Unknown caffe mode.";
506 template <
typename Dtype>
509 param->CopyFrom(layer_param_);
510 param->clear_blobs();
511 for (
int i = 0; i < blobs_.size(); ++i) {
512 blobs_[i]->
ToProto(param->add_blobs(), write_diff);
518 #endif // CAFFE_LAYER_H_ 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:359
vector< Dtype > loss_
Definition: layer.hpp:332
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:341
An interface for the units of computation which can be composed into a Net.
Definition: layer.hpp:33
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
Definition: internal_thread.hpp:10
vector< shared_ptr< Blob< Dtype > > > blobs_
Definition: layer.hpp:326
virtual const char * type() const
Returns the layer type.
Definition: layer.hpp:216
vector< shared_ptr< Blob< Dtype > > > & blobs()
Returns the vector of learnable parameter blobs.
Definition: layer.hpp:182
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:265
void SetShared(bool is_shared)
Set whether this layer is actually shared by other nets If ShareInParallel() is true and using more t...
Definition: layer.hpp:113
void SetLossWeights(const vector< Blob< Dtype > *> &top)
Definition: layer.hpp:414
vector< bool > param_propagate_down_
Definition: layer.hpp:328
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:225
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:241
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:312
virtual bool ShareInParallel() const
Whether a layer should be shared by multiple nets during data parallelism. By default, all layers except for data layers should not be shared. data layers should be shared to ensure each worker solver access data sequentially during data parallelism.
Definition: layer.hpp:101
const LayerParameter & layer_param() const
Returns the layer parameter.
Definition: layer.hpp:189
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:249
void SetUp(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Implements common layer setup functionality.
Definition: layer.hpp:67
Layer(const LayerParameter ¶m)
Definition: layer.hpp:40
virtual bool AllowForceBackward(const int bottom_index) const
Return whether to allow force_backward for a given bottom blob index.
Definition: layer.hpp:293
virtual bool AutoTopBlobs() const
Return whether "anonymous" top blobs are created automatically by the layer.
Definition: layer.hpp:283
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:257
virtual void ToProto(LayerParameter *param, bool write_diff=false)
Writes the layer parameter to a protocol buffer.
Definition: layer.hpp:507
Phase phase_
Definition: layer.hpp:324
LayerParameter layer_param_
Definition: layer.hpp:322
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:92
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:206
Dtype loss(const int top_index) const
Returns the scalar loss associated with a top blob at a given index.
Definition: layer.hpp:199
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:233
bool IsShared() const
Return whether this layer is actually shared by other nets. If ShareInParallel() is true and using mo...
Definition: layer.hpp:107
virtual void CheckBlobCounts(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Definition: layer.hpp:371
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:304
virtual bool EqualNumBottomTopBlobs() const
Returns true if the layer requires an equal number of bottom and top blobs.
Definition: layer.hpp:273
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24