Caffe
net.hpp
1 #ifndef CAFFE_NET_HPP_
2 #define CAFFE_NET_HPP_
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include "caffe/blob.hpp"
11 #include "caffe/common.hpp"
12 #include "caffe/layer.hpp"
13 #include "caffe/proto/caffe.pb.h"
14 
15 namespace caffe {
16 
23 template <typename Dtype>
24 class Net {
25  public:
26  explicit Net(const NetParameter& param, const Net* root_net = NULL);
27  explicit Net(const string& param_file, Phase phase,
28  const int level = 0, const vector<string>* stages = NULL,
29  const Net* root_net = NULL);
30  virtual ~Net() {}
31 
33  void Init(const NetParameter& param);
34 
39  const vector<Blob<Dtype>*>& Forward(Dtype* loss = NULL);
41  const vector<Blob<Dtype>*>& ForwardPrefilled(Dtype* loss = NULL) {
42  LOG_EVERY_N(WARNING, 1000) << "DEPRECATED: ForwardPrefilled() "
43  << "will be removed in a future version. Use Forward().";
44  return Forward(loss);
45  }
46 
55  Dtype ForwardFromTo(int start, int end);
56  Dtype ForwardFrom(int start);
57  Dtype ForwardTo(int end);
59  const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom,
60  Dtype* loss = NULL);
61 
66  void ClearParamDiffs();
67 
73  void Backward();
74  void BackwardFromTo(int start, int end);
75  void BackwardFrom(int start);
76  void BackwardTo(int end);
77 
84  void Reshape();
85 
86  Dtype ForwardBackward() {
87  Dtype loss;
88  Forward(&loss);
89  Backward();
90  return loss;
91  }
92 
94  void Update();
101  void ShareWeights();
102 
107  void ShareTrainedLayersWith(const Net* other);
108  // For an already initialized net, CopyTrainedLayersFrom() copies the already
109  // trained layers from another net parameter instance.
114  void CopyTrainedLayersFrom(const NetParameter& param);
115  void CopyTrainedLayersFrom(const string trained_filename);
116  void CopyTrainedLayersFromBinaryProto(const string trained_filename);
117  void CopyTrainedLayersFromHDF5(const string trained_filename);
119  void ToProto(NetParameter* param, bool write_diff = false) const;
121  void ToHDF5(const string& filename, bool write_diff = false) const;
122 
124  inline const string& name() const { return name_; }
126  inline const vector<string>& layer_names() const { return layer_names_; }
128  inline const vector<string>& blob_names() const { return blob_names_; }
130  inline const vector<shared_ptr<Blob<Dtype> > >& blobs() const {
131  return blobs_;
132  }
134  inline const vector<shared_ptr<Layer<Dtype> > >& layers() const {
135  return layers_;
136  }
138  inline Phase phase() const { return phase_; }
143  inline const vector<vector<Blob<Dtype>*> >& bottom_vecs() const {
144  return bottom_vecs_;
145  }
150  inline const vector<vector<Blob<Dtype>*> >& top_vecs() const {
151  return top_vecs_;
152  }
154  inline const vector<int> & top_ids(int i) const {
155  CHECK_GE(i, 0) << "Invalid layer id";
156  CHECK_LT(i, top_id_vecs_.size()) << "Invalid layer id";
157  return top_id_vecs_[i];
158  }
160  inline const vector<int> & bottom_ids(int i) const {
161  CHECK_GE(i, 0) << "Invalid layer id";
162  CHECK_LT(i, bottom_id_vecs_.size()) << "Invalid layer id";
163  return bottom_id_vecs_[i];
164  }
165  inline const vector<vector<bool> >& bottom_need_backward() const {
166  return bottom_need_backward_;
167  }
168  inline const vector<Dtype>& blob_loss_weights() const {
169  return blob_loss_weights_;
170  }
171  inline const vector<bool>& layer_need_backward() const {
172  return layer_need_backward_;
173  }
175  inline const vector<shared_ptr<Blob<Dtype> > >& params() const {
176  return params_;
177  }
178  inline const vector<Blob<Dtype>*>& learnable_params() const {
179  return learnable_params_;
180  }
182  inline const vector<float>& params_lr() const { return params_lr_; }
183  inline const vector<bool>& has_params_lr() const { return has_params_lr_; }
185  inline const vector<float>& params_weight_decay() const {
186  return params_weight_decay_;
187  }
188  inline const vector<bool>& has_params_decay() const {
189  return has_params_decay_;
190  }
191  const map<string, int>& param_names_index() const {
192  return param_names_index_;
193  }
194  inline const vector<int>& param_owners() const { return param_owners_; }
195  inline const vector<string>& param_display_names() const {
196  return param_display_names_;
197  }
199  inline int num_inputs() const { return net_input_blobs_.size(); }
200  inline int num_outputs() const { return net_output_blobs_.size(); }
201  inline const vector<Blob<Dtype>*>& input_blobs() const {
202  return net_input_blobs_;
203  }
204  inline const vector<Blob<Dtype>*>& output_blobs() const {
205  return net_output_blobs_;
206  }
207  inline const vector<int>& input_blob_indices() const {
209  }
210  inline const vector<int>& output_blob_indices() const {
211  return net_output_blob_indices_;
212  }
213  bool has_blob(const string& blob_name) const;
214  const shared_ptr<Blob<Dtype> > blob_by_name(const string& blob_name) const;
215  bool has_layer(const string& layer_name) const;
216  const shared_ptr<Layer<Dtype> > layer_by_name(const string& layer_name) const;
217 
218  void set_debug_info(const bool value) { debug_info_ = value; }
219 
220  // Helpers for Init.
225  static void FilterNet(const NetParameter& param,
226  NetParameter* param_filtered);
228  static bool StateMeetsRule(const NetState& state, const NetStateRule& rule,
229  const string& layer_name);
230 
231  protected:
232  // Helpers for Init.
234  void AppendTop(const NetParameter& param, const int layer_id,
235  const int top_id, set<string>* available_blobs,
236  map<string, int>* blob_name_to_idx);
238  int AppendBottom(const NetParameter& param, const int layer_id,
239  const int bottom_id, set<string>* available_blobs,
240  map<string, int>* blob_name_to_idx);
242  void AppendParam(const NetParameter& param, const int layer_id,
243  const int param_id);
244 
246  void ForwardDebugInfo(const int layer_id);
248  void BackwardDebugInfo(const int layer_id);
250  void UpdateDebugInfo(const int param_id);
251 
253  string name_;
255  Phase phase_;
257  vector<shared_ptr<Layer<Dtype> > > layers_;
258  vector<string> layer_names_;
259  map<string, int> layer_names_index_;
260  vector<bool> layer_need_backward_;
262  vector<shared_ptr<Blob<Dtype> > > blobs_;
263  vector<string> blob_names_;
264  map<string, int> blob_names_index_;
265  vector<bool> blob_need_backward_;
269  vector<vector<Blob<Dtype>*> > bottom_vecs_;
270  vector<vector<int> > bottom_id_vecs_;
271  vector<vector<bool> > bottom_need_backward_;
273  vector<vector<Blob<Dtype>*> > top_vecs_;
274  vector<vector<int> > top_id_vecs_;
277  vector<Dtype> blob_loss_weights_;
278  vector<vector<int> > param_id_vecs_;
279  vector<int> param_owners_;
280  vector<string> param_display_names_;
281  vector<pair<int, int> > param_layer_indices_;
282  map<string, int> param_names_index_;
285  vector<int> net_output_blob_indices_;
286  vector<Blob<Dtype>*> net_input_blobs_;
287  vector<Blob<Dtype>*> net_output_blobs_;
289  vector<shared_ptr<Blob<Dtype> > > params_;
290  vector<Blob<Dtype>*> learnable_params_;
298  vector<int> learnable_param_ids_;
300  vector<float> params_lr_;
301  vector<bool> has_params_lr_;
303  vector<float> params_weight_decay_;
304  vector<bool> has_params_decay_;
306  size_t memory_used_;
310  const Net* const root_net_;
311  DISABLE_COPY_AND_ASSIGN(Net);
312 };
313 
314 
315 } // namespace caffe
316 
317 #endif // CAFFE_NET_HPP_
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:384
const vector< Blob< Dtype > * > & ForwardPrefilled(Dtype *loss=NULL)
DEPRECATED; use Forward() instead.
Definition: net.hpp:41
vector< shared_ptr< Layer< Dtype > > > layers_
Individual layers in the net.
Definition: net.hpp:257
int num_inputs() const
Input and output blob numbers.
Definition: net.hpp:199
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
const vector< string > & blob_names() const
returns the blob names
Definition: net.hpp:128
vector< Dtype > blob_loss_weights_
Definition: net.hpp:277
void AppendParam(const NetParameter &param, const int layer_id, const int param_id)
Append a new parameter blob to the net.
Definition: net.cpp:449
void UpdateDebugInfo(const int param_id)
Helper for displaying debug info in Update.
Definition: net.cpp:656
void Reshape()
Reshape all layers from bottom to top.
Definition: net.cpp:743
Dtype ForwardFromTo(int start, int end)
Definition: net.cpp:544
const vector< shared_ptr< Layer< Dtype > > > & layers() const
returns the layers
Definition: net.hpp:134
size_t memory_used_
The bytes of memory used by this net.
Definition: net.hpp:306
void Update()
Updates the network weights based on the diff values computed.
Definition: net.cpp:925
bool debug_info_
Whether to compute and display debug info for the net.
Definition: net.hpp:308
const vector< shared_ptr< Blob< Dtype > > > & blobs() const
returns the blobs
Definition: net.hpp:130
vector< vector< Blob< Dtype > * > > top_vecs_
top_vecs stores the vectors containing the output for each layer
Definition: net.hpp:273
const string & name() const
returns the network name.
Definition: net.hpp:124
void ClearParamDiffs()
Zeroes out the diffs of all net parameters. Should be run before Backward.
Definition: net.cpp:932
void BackwardDebugInfo(const int layer_id)
Helper for displaying debug info in Backward.
Definition: net.cpp:629
void ShareWeights()
Shares weight data of owner blobs with shared blobs.
Definition: net.cpp:953
void Init(const NetParameter &param)
Initialize a network with a NetParameter.
Definition: net.cpp:49
void Backward()
Definition: net.cpp:724
const vector< vector< Blob< Dtype > * > > & top_vecs() const
returns the top vecs for each layer – usually you won&#39;t need this unless you do per-layer checks suc...
Definition: net.hpp:150
void ShareTrainedLayersWith(const Net *other)
For an already initialized net, implicitly copies (i.e., using no additional memory) the pre-trained ...
Definition: net.cpp:682
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:424
const vector< float > & params_weight_decay() const
returns the learnable parameter decay multipliers
Definition: net.hpp:185
vector< float > params_weight_decay_
the weight decay multipliers for learnable_params_
Definition: net.hpp:303
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:287
const vector< int > & top_ids(int i) const
returns the ids of the top blobs of layer i
Definition: net.hpp:154
const vector< int > & bottom_ids(int i) const
returns the ids of the bottom blobs of layer i
Definition: net.hpp:160
void ToProto(NetParameter *param, bool write_diff=false) const
Writes the net to a proto.
Definition: net.cpp:856
void ToHDF5(const string &filename, bool write_diff=false) const
Writes the net to an HDF5 file.
Definition: net.cpp:868
vector< int > learnable_param_ids_
Definition: net.hpp:298
vector< shared_ptr< Blob< Dtype > > > params_
The parameters in the network.
Definition: net.hpp:289
Phase phase_
The phase: TRAIN or TEST.
Definition: net.hpp:255
void CopyTrainedLayersFrom(const NetParameter &param)
For an already initialized net, copies the pre-trained layers from another Net.
Definition: net.cpp:750
static bool StateMeetsRule(const NetState &state, const NetStateRule &rule, const string &layer_name)
return whether NetState state meets NetStateRule rule
Definition: net.cpp:317
const vector< Blob< Dtype > * > & Forward(Dtype *loss=NULL)
Run Forward and return the result.
Definition: net.cpp:568
const vector< shared_ptr< Blob< Dtype > > > & params() const
returns the parameters
Definition: net.hpp:175
void ForwardDebugInfo(const int layer_id)
Helper for displaying debug info in Forward.
Definition: net.cpp:603
vector< float > params_lr_
the learning rate multipliers for learnable_params_
Definition: net.hpp:300
const Net *const root_net_
The root net that actually holds the shared layers in data parallelism.
Definition: net.hpp:310
vector< int > net_input_blob_indices_
blob indices for the input and the output of the net
Definition: net.hpp:284
const vector< string > & layer_names() const
returns the layer names
Definition: net.hpp:126
const vector< float > & params_lr() const
returns the learnable parameter learning rate multipliers
Definition: net.hpp:182
Phase phase() const
returns the phase: TRAIN or TEST
Definition: net.hpp:138
Connects Layers together into a directed acyclic graph (DAG) specified by a NetParameter.
Definition: net.hpp:24
vector< shared_ptr< Blob< Dtype > > > blobs_
the blobs storing intermediate results between the layer.
Definition: net.hpp:262
vector< vector< Blob< Dtype > * > > bottom_vecs_
Definition: net.hpp:269
const vector< vector< Blob< Dtype > * > > & bottom_vecs() const
returns the bottom vecs for each layer – usually you won&#39;t need this unless you do per-layer checks ...
Definition: net.hpp:143
string name_
The network name.
Definition: net.hpp:253
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24