net.hpp
#ifndef CAFFE_NET_HPP_
#define CAFFE_NET_HPP_
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
template <typename Dtype>
class Net {
public:
explicit Net(const NetParameter& param);
explicit Net(const string& param_file, Phase phase,
const int level = 0, const vector<string>* stages = NULL);
virtual ~Net() {}
void Init(const NetParameter& param);
const vector<Blob<Dtype>*>& Forward(Dtype* loss = NULL);
const vector<Blob<Dtype>*>& ForwardPrefilled(Dtype* loss = NULL) {
LOG_EVERY_N(WARNING, 1000) << "DEPRECATED: ForwardPrefilled() "
<< "will be removed in a future version. Use Forward().";
return Forward(loss);
}
Dtype ForwardFromTo(int start, int end);
Dtype ForwardFrom(int start);
Dtype ForwardTo(int end);
const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom,
Dtype* loss = NULL);
void ClearParamDiffs();
void Backward();
void BackwardFromTo(int start, int end);
void BackwardFrom(int start);
void BackwardTo(int end);
void Reshape();
Dtype ForwardBackward() {
Dtype loss;
Forward(&loss);
Backward();
return loss;
}
void Update();
void ShareWeights();
void ShareTrainedLayersWith(const Net* other);
// For an already initialized net, CopyTrainedLayersFrom() copies the already
// trained layers from another net parameter instance.
void CopyTrainedLayersFrom(const NetParameter& param);
void CopyTrainedLayersFrom(const string& trained_filename);
void CopyTrainedLayersFromBinaryProto(const string& trained_filename);
void CopyTrainedLayersFromHDF5(const string& trained_filename);
void ToProto(NetParameter* param, bool write_diff = false) const;
void ToHDF5(const string& filename, bool write_diff = false) const;
inline const string& name() const { return name_; }
inline const vector<string>& layer_names() const { return layer_names_; }
inline const vector<string>& blob_names() const { return blob_names_; }
inline const vector<shared_ptr<Blob<Dtype> > >& blobs() const {
return blobs_;
}
inline const vector<shared_ptr<Layer<Dtype> > >& layers() const {
return layers_;
}
inline Phase phase() const { return phase_; }
inline const vector<vector<Blob<Dtype>*> >& bottom_vecs() const {
return bottom_vecs_;
}
inline const vector<vector<Blob<Dtype>*> >& top_vecs() const {
return top_vecs_;
}
inline const vector<int> & top_ids(int i) const {
CHECK_GE(i, 0) << "Invalid layer id";
CHECK_LT(i, top_id_vecs_.size()) << "Invalid layer id";
return top_id_vecs_[i];
}
inline const vector<int> & bottom_ids(int i) const {
CHECK_GE(i, 0) << "Invalid layer id";
CHECK_LT(i, bottom_id_vecs_.size()) << "Invalid layer id";
return bottom_id_vecs_[i];
}
inline const vector<vector<bool> >& bottom_need_backward() const {
return bottom_need_backward_;
}
inline const vector<Dtype>& blob_loss_weights() const {
return blob_loss_weights_;
}
inline const vector<bool>& layer_need_backward() const {
return layer_need_backward_;
}
inline const vector<shared_ptr<Blob<Dtype> > >& params() const {
return params_;
}
inline const vector<Blob<Dtype>*>& learnable_params() const {
return learnable_params_;
}
inline const vector<float>& params_lr() const { return params_lr_; }
inline const vector<bool>& has_params_lr() const { return has_params_lr_; }
inline const vector<float>& params_weight_decay() const {
return params_weight_decay_;
}
inline const vector<bool>& has_params_decay() const {
return has_params_decay_;
}
const map<string, int>& param_names_index() const {
return param_names_index_;
}
inline const vector<int>& param_owners() const { return param_owners_; }
inline const vector<string>& param_display_names() const {
return param_display_names_;
}
inline int num_inputs() const { return net_input_blobs_.size(); }
inline int num_outputs() const { return net_output_blobs_.size(); }
inline const vector<Blob<Dtype>*>& input_blobs() const {
return net_input_blobs_;
}
inline const vector<Blob<Dtype>*>& output_blobs() const {
return net_output_blobs_;
}
inline const vector<int>& input_blob_indices() const {
return net_input_blob_indices_;
}
inline const vector<int>& output_blob_indices() const {
return net_output_blob_indices_;
}
bool has_blob(const string& blob_name) const;
const shared_ptr<Blob<Dtype> > blob_by_name(const string& blob_name) const;
bool has_layer(const string& layer_name) const;
const shared_ptr<Layer<Dtype> > layer_by_name(const string& layer_name) const;
void set_debug_info(const bool value) { debug_info_ = value; }
// Helpers for Init.
static void FilterNet(const NetParameter& param,
NetParameter* param_filtered);
static bool StateMeetsRule(const NetState& state, const NetStateRule& rule,
const string& layer_name);
// Invoked at specific points during an iteration
class Callback {
protected:
virtual void run(int layer) = 0;
template <typename T>
friend class Net;
};
const vector<Callback*>& before_forward() const { return before_forward_; }
void add_before_forward(Callback* value) {
before_forward_.push_back(value);
}
const vector<Callback*>& after_forward() const { return after_forward_; }
void add_after_forward(Callback* value) {
after_forward_.push_back(value);
}
const vector<Callback*>& before_backward() const { return before_backward_; }
void add_before_backward(Callback* value) {
before_backward_.push_back(value);
}
const vector<Callback*>& after_backward() const { return after_backward_; }
void add_after_backward(Callback* value) {
after_backward_.push_back(value);
}
protected:
// Helpers for Init.
void AppendTop(const NetParameter& param, const int layer_id,
const int top_id, set<string>* available_blobs,
map<string, int>* blob_name_to_idx);
int AppendBottom(const NetParameter& param, const int layer_id,
const int bottom_id, set<string>* available_blobs,
map<string, int>* blob_name_to_idx);
void AppendParam(const NetParameter& param, const int layer_id,
const int param_id);
void ForwardDebugInfo(const int layer_id);
void BackwardDebugInfo(const int layer_id);
void UpdateDebugInfo(const int param_id);
string name_;
Phase phase_;
vector<shared_ptr<Layer<Dtype> > > layers_;
vector<string> layer_names_;
map<string, int> layer_names_index_;
vector<bool> layer_need_backward_;
vector<shared_ptr<Blob<Dtype> > > blobs_;
vector<string> blob_names_;
map<string, int> blob_names_index_;
vector<bool> blob_need_backward_;
vector<vector<Blob<Dtype>*> > bottom_vecs_;
vector<vector<int> > bottom_id_vecs_;
vector<vector<bool> > bottom_need_backward_;
vector<vector<Blob<Dtype>*> > top_vecs_;
vector<vector<int> > top_id_vecs_;
vector<Dtype> blob_loss_weights_;
vector<vector<int> > param_id_vecs_;
vector<int> param_owners_;
vector<string> param_display_names_;
vector<pair<int, int> > param_layer_indices_;
map<string, int> param_names_index_;
vector<int> net_input_blob_indices_;
vector<int> net_output_blob_indices_;
vector<Blob<Dtype>*> net_input_blobs_;
vector<Blob<Dtype>*> net_output_blobs_;
vector<shared_ptr<Blob<Dtype> > > params_;
vector<Blob<Dtype>*> learnable_params_;
vector<int> learnable_param_ids_;
vector<float> params_lr_;
vector<bool> has_params_lr_;
vector<float> params_weight_decay_;
vector<bool> has_params_decay_;
size_t memory_used_;
bool debug_info_;
// Callbacks
vector<Callback*> before_forward_;
vector<Callback*> after_forward_;
vector<Callback*> before_backward_;
vector<Callback*> after_backward_;
DISABLE_COPY_AND_ASSIGN(Net);
};
} // namespace caffe
#endif // CAFFE_NET_HPP_
caffe::Net::UpdateDebugInfo
void UpdateDebugInfo(const int param_id)
Helper for displaying debug info in Update.
Definition: net.cpp:641
caffe::Net::bottom_ids
const vector< int > & bottom_ids(int i) const
returns the ids of the bottom blobs of layer i
Definition: net.hpp:159
caffe::Net::params_weight_decay_
vector< float > params_weight_decay_
the weight decay multipliers for learnable_params_
Definition: net.hpp:327
caffe::Net::top_ids
const vector< int > & top_ids(int i) const
returns the ids of the top blobs of layer i
Definition: net.hpp:153
caffe::Net::bottom_vecs
const vector< vector< Blob< Dtype > * > > & bottom_vecs() const
returns the bottom vecs for each layer – usually you won't need this unless you do per-layer checks s...
Definition: net.hpp:142
caffe::Net::Update
void Update()
Updates the network weights based on the diff values computed.
Definition: net.cpp:920
caffe::Net::learnable_param_ids_
vector< int > learnable_param_ids_
Definition: net.hpp:322
caffe::Net::phase
Phase phase() const
returns the phase: TRAIN or TEST
Definition: net.hpp:137
caffe::Net::AppendTop
void AppendTop(const NetParameter &param, const int layer_id, const int top_id, set< string > *available_blobs, map< string, int > *blob_name_to_idx)
Append a new top blob to the net.
Definition: net.cpp:358
caffe::Net::name
const string & name() const
returns the network name.
Definition: net.hpp:123
caffe::Net::top_vecs_
vector< vector< Blob< Dtype > * > > top_vecs_
top_vecs stores the vectors containing the output for each layer
Definition: net.hpp:297
caffe::Net::ForwardPrefilled
const vector< Blob< Dtype > * > & ForwardPrefilled(Dtype *loss=NULL)
DEPRECATED; use Forward() instead.
Definition: net.hpp:40
caffe::Net::Callback
Definition: net.hpp:231
caffe::Net::ToHDF5
void ToHDF5(const string &filename, bool write_diff=false) const
Writes the net to an HDF5 file.
Definition: net.cpp:857
caffe::Net::layer_names
const vector< string > & layer_names() const
returns the layer names
Definition: net.hpp:125
caffe::Net::num_inputs
int num_inputs() const
Input and output blob numbers.
Definition: net.hpp:198
caffe::Net::memory_used_
size_t memory_used_
The bytes of memory used by this net.
Definition: net.hpp:330
caffe::Net::name_
string name_
The network name.
Definition: net.hpp:277
caffe::Net::bottom_vecs_
vector< vector< Blob< Dtype > * > > bottom_vecs_
Definition: net.hpp:293
caffe::Net::params
const vector< shared_ptr< Blob< Dtype > > > & params() const
returns the parameters
Definition: net.hpp:174
caffe::Blob
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers,...
Definition: blob.hpp:24
caffe::Net::Backward
void Backward()
Definition: net.cpp:709
caffe::Net::ForwardDebugInfo
void ForwardDebugInfo(const int layer_id)
Helper for displaying debug info in Forward.
Definition: net.cpp:588
caffe::Net::net_input_blob_indices_
vector< int > net_input_blob_indices_
blob indices for the input and the output of the net
Definition: net.hpp:308
caffe::Net::StateMeetsRule
static bool StateMeetsRule(const NetState &state, const NetStateRule &rule, const string &layer_name)
return whether NetState state meets NetStateRule rule
Definition: net.cpp:291
caffe::Net::ShareWeights
void ShareWeights()
Shares weight data of owner blobs with shared blobs.
Definition: net.cpp:948
caffe::Net::params_lr_
vector< float > params_lr_
the learning rate multipliers for learnable_params_
Definition: net.hpp:324
caffe::Net::blob_loss_weights_
vector< Dtype > blob_loss_weights_
Definition: net.hpp:301
caffe::Net::ClearParamDiffs
void ClearParamDiffs()
Zeroes out the diffs of all net parameters. Should be run before Backward.
Definition: net.cpp:927
caffe::Net::blob_names
const vector< string > & blob_names() const
returns the blob names
Definition: net.hpp:127
caffe::Net::layers
const vector< shared_ptr< Layer< Dtype > > > & layers() const
returns the layers
Definition: net.hpp:133
caffe::Net::ToProto
void ToProto(NetParameter *param, bool write_diff=false) const
Writes the net to a proto.
Definition: net.cpp:845
caffe::Net::params_
vector< shared_ptr< Blob< Dtype > > > params_
The parameters in the network.
Definition: net.hpp:313
caffe::Net::debug_info_
bool debug_info_
Whether to compute and display debug info for the net.
Definition: net.hpp:332
caffe::Net::blobs_
vector< shared_ptr< Blob< Dtype > > > blobs_
the blobs storing intermediate results between the layer.
Definition: net.hpp:286
caffe::Net::AppendBottom
int AppendBottom(const NetParameter &param, const int layer_id, const int bottom_id, set< string > *available_blobs, map< string, int > *blob_name_to_idx)
Append a new bottom blob to the net.
Definition: net.cpp:398
caffe::Net::Init
void Init(const NetParameter &param)
Initialize a network with a NetParameter.
Definition: net.cpp:46
caffe::Net::params_weight_decay
const vector< float > & params_weight_decay() const
returns the learnable parameter decay multipliers
Definition: net.hpp:184
caffe::Net::AppendParam
void AppendParam(const NetParameter &param, const int layer_id, const int param_id)
Append a new parameter blob to the net.
Definition: net.cpp:423
caffe::Net::layers_
vector< shared_ptr< Layer< Dtype > > > layers_
Individual layers in the net.
Definition: net.hpp:281
caffe::Net
Connects Layers together into a directed acyclic graph (DAG) specified by a NetParameter.
Definition: net.hpp:24
caffe::Net::Reshape
void Reshape()
Reshape all layers from bottom to top.
Definition: net.cpp:728
caffe::Net::blobs
const vector< shared_ptr< Blob< Dtype > > > & blobs() const
returns the blobs
Definition: net.hpp:129
caffe::Net::ForwardFromTo
Dtype ForwardFromTo(int start, int end)
Definition: net.cpp:518
caffe::Net::CopyTrainedLayersFrom
void CopyTrainedLayersFrom(const NetParameter &param)
For an already initialized net, copies the pre-trained layers from another Net.
Definition: net.cpp:735
caffe::Net::top_vecs
const vector< vector< Blob< Dtype > * > > & top_vecs() const
returns the top vecs for each layer – usually you won't need this unless you do per-layer checks such...
Definition: net.hpp:149
caffe::Net::FilterNet
static void FilterNet(const NetParameter &param, NetParameter *param_filtered)
Remove layers that the user specified should be excluded given the current phase, level,...
Definition: net.cpp:261
caffe
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
caffe::Net::Forward
const vector< Blob< Dtype > * > & Forward(Dtype *loss=NULL)
Run Forward and return the result.
Definition: net.cpp:547
caffe::Net::params_lr
const vector< float > & params_lr() const
returns the learnable parameter learning rate multipliers
Definition: net.hpp:181
caffe::Net::BackwardDebugInfo
void BackwardDebugInfo(const int layer_id)
Helper for displaying debug info in Backward.
Definition: net.cpp:614
caffe::Net::ShareTrainedLayersWith
void ShareTrainedLayersWith(const Net *other)
For an already initialized net, implicitly copies (i.e., using no additional memory) the pre-trained ...
Definition: net.cpp:667
caffe::Net::phase_
Phase phase_
The phase: TRAIN or TEST.
Definition: net.hpp:279