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);
27  explicit Net(const string& param_file, Phase phase,
28  const int level = 0, const vector<string>* stages = NULL);
29  virtual ~Net() {}
30 
32  void Init(const NetParameter& param);
33 
38  const vector<Blob<Dtype>*>& Forward(Dtype* loss = NULL);
40  const vector<Blob<Dtype>*>& ForwardPrefilled(Dtype* loss = NULL) {
41  LOG_EVERY_N(WARNING, 1000) << "DEPRECATED: ForwardPrefilled() "
42  << "will be removed in a future version. Use Forward().";
43  return Forward(loss);
44  }
45 
54  Dtype ForwardFromTo(int start, int end);
55  Dtype ForwardFrom(int start);
56  Dtype ForwardTo(int end);
58  const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom,
59  Dtype* loss = NULL);
60 
65  void ClearParamDiffs();
66 
72  void Backward();
73  void BackwardFromTo(int start, int end);
74  void BackwardFrom(int start);
75  void BackwardTo(int end);
76 
83  void Reshape();
84 
85  Dtype ForwardBackward() {
86  Dtype loss;
87  Forward(&loss);
88  Backward();
89  return loss;
90  }
91 
93  void Update();
100  void ShareWeights();
101 
106  void ShareTrainedLayersWith(const Net* other);
107  // For an already initialized net, CopyTrainedLayersFrom() copies the already
108  // trained layers from another net parameter instance.
113  void CopyTrainedLayersFrom(const NetParameter& param);
114  void CopyTrainedLayersFrom(const string trained_filename);
115  void CopyTrainedLayersFromBinaryProto(const string trained_filename);
116  void CopyTrainedLayersFromHDF5(const string trained_filename);
118  void ToProto(NetParameter* param, bool write_diff = false) const;
120  void ToHDF5(const string& filename, bool write_diff = false) const;
121 
123  inline const string& name() const { return name_; }
125  inline const vector<string>& layer_names() const { return layer_names_; }
127  inline const vector<string>& blob_names() const { return blob_names_; }
129  inline const vector<shared_ptr<Blob<Dtype> > >& blobs() const {
130  return blobs_;
131  }
133  inline const vector<shared_ptr<Layer<Dtype> > >& layers() const {
134  return layers_;
135  }
137  inline Phase phase() const { return phase_; }
142  inline const vector<vector<Blob<Dtype>*> >& bottom_vecs() const {
143  return bottom_vecs_;
144  }
149  inline const vector<vector<Blob<Dtype>*> >& top_vecs() const {
150  return top_vecs_;
151  }
153  inline const vector<int> & top_ids(int i) const {
154  CHECK_GE(i, 0) << "Invalid layer id";
155  CHECK_LT(i, top_id_vecs_.size()) << "Invalid layer id";
156  return top_id_vecs_[i];
157  }
159  inline const vector<int> & bottom_ids(int i) const {
160  CHECK_GE(i, 0) << "Invalid layer id";
161  CHECK_LT(i, bottom_id_vecs_.size()) << "Invalid layer id";
162  return bottom_id_vecs_[i];
163  }
164  inline const vector<vector<bool> >& bottom_need_backward() const {
165  return bottom_need_backward_;
166  }
167  inline const vector<Dtype>& blob_loss_weights() const {
168  return blob_loss_weights_;
169  }
170  inline const vector<bool>& layer_need_backward() const {
171  return layer_need_backward_;
172  }
174  inline const vector<shared_ptr<Blob<Dtype> > >& params() const {
175  return params_;
176  }
177  inline const vector<Blob<Dtype>*>& learnable_params() const {
178  return learnable_params_;
179  }
181  inline const vector<float>& params_lr() const { return params_lr_; }
182  inline const vector<bool>& has_params_lr() const { return has_params_lr_; }
184  inline const vector<float>& params_weight_decay() const {
185  return params_weight_decay_;
186  }
187  inline const vector<bool>& has_params_decay() const {
188  return has_params_decay_;
189  }
190  const map<string, int>& param_names_index() const {
191  return param_names_index_;
192  }
193  inline const vector<int>& param_owners() const { return param_owners_; }
194  inline const vector<string>& param_display_names() const {
195  return param_display_names_;
196  }
198  inline int num_inputs() const { return net_input_blobs_.size(); }
199  inline int num_outputs() const { return net_output_blobs_.size(); }
200  inline const vector<Blob<Dtype>*>& input_blobs() const {
201  return net_input_blobs_;
202  }
203  inline const vector<Blob<Dtype>*>& output_blobs() const {
204  return net_output_blobs_;
205  }
206  inline const vector<int>& input_blob_indices() const {
208  }
209  inline const vector<int>& output_blob_indices() const {
210  return net_output_blob_indices_;
211  }
212  bool has_blob(const string& blob_name) const;
213  const shared_ptr<Blob<Dtype> > blob_by_name(const string& blob_name) const;
214  bool has_layer(const string& layer_name) const;
215  const shared_ptr<Layer<Dtype> > layer_by_name(const string& layer_name) const;
216 
217  void set_debug_info(const bool value) { debug_info_ = value; }
218 
219  // Helpers for Init.
224  static void FilterNet(const NetParameter& param,
225  NetParameter* param_filtered);
227  static bool StateMeetsRule(const NetState& state, const NetStateRule& rule,
228  const string& layer_name);
229 
230  // Invoked at specific points during an iteration
231  class Callback {
232  protected:
233  virtual void run(int layer) = 0;
234 
235  template <typename T>
236  friend class Net;
237  };
238  const vector<Callback*>& before_forward() const { return before_forward_; }
239  void add_before_forward(Callback* value) {
240  before_forward_.push_back(value);
241  }
242  const vector<Callback*>& after_forward() const { return after_forward_; }
243  void add_after_forward(Callback* value) {
244  after_forward_.push_back(value);
245  }
246  const vector<Callback*>& before_backward() const { return before_backward_; }
247  void add_before_backward(Callback* value) {
248  before_backward_.push_back(value);
249  }
250  const vector<Callback*>& after_backward() const { return after_backward_; }
251  void add_after_backward(Callback* value) {
252  after_backward_.push_back(value);
253  }
254 
255  protected:
256  // Helpers for Init.
258  void AppendTop(const NetParameter& param, const int layer_id,
259  const int top_id, set<string>* available_blobs,
260  map<string, int>* blob_name_to_idx);
262  int AppendBottom(const NetParameter& param, const int layer_id,
263  const int bottom_id, set<string>* available_blobs,
264  map<string, int>* blob_name_to_idx);
266  void AppendParam(const NetParameter& param, const int layer_id,
267  const int param_id);
268 
270  void ForwardDebugInfo(const int layer_id);
272  void BackwardDebugInfo(const int layer_id);
274  void UpdateDebugInfo(const int param_id);
275 
277  string name_;
279  Phase phase_;
281  vector<shared_ptr<Layer<Dtype> > > layers_;
282  vector<string> layer_names_;
283  map<string, int> layer_names_index_;
284  vector<bool> layer_need_backward_;
286  vector<shared_ptr<Blob<Dtype> > > blobs_;
287  vector<string> blob_names_;
288  map<string, int> blob_names_index_;
289  vector<bool> blob_need_backward_;
293  vector<vector<Blob<Dtype>*> > bottom_vecs_;
294  vector<vector<int> > bottom_id_vecs_;
295  vector<vector<bool> > bottom_need_backward_;
297  vector<vector<Blob<Dtype>*> > top_vecs_;
298  vector<vector<int> > top_id_vecs_;
301  vector<Dtype> blob_loss_weights_;
302  vector<vector<int> > param_id_vecs_;
303  vector<int> param_owners_;
304  vector<string> param_display_names_;
305  vector<pair<int, int> > param_layer_indices_;
306  map<string, int> param_names_index_;
309  vector<int> net_output_blob_indices_;
310  vector<Blob<Dtype>*> net_input_blobs_;
311  vector<Blob<Dtype>*> net_output_blobs_;
313  vector<shared_ptr<Blob<Dtype> > > params_;
314  vector<Blob<Dtype>*> learnable_params_;
322  vector<int> learnable_param_ids_;
324  vector<float> params_lr_;
325  vector<bool> has_params_lr_;
327  vector<float> params_weight_decay_;
328  vector<bool> has_params_decay_;
330  size_t memory_used_;
333  // Callbacks
334  vector<Callback*> before_forward_;
335  vector<Callback*> after_forward_;
336  vector<Callback*> before_backward_;
337  vector<Callback*> after_backward_;
338 
339 DISABLE_COPY_AND_ASSIGN(Net);
340 };
341 
342 
343 } // namespace caffe
344 
345 #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:356
const vector< Blob< Dtype > * > & ForwardPrefilled(Dtype *loss=NULL)
DEPRECATED; use Forward() instead.
Definition: net.hpp:40
vector< shared_ptr< Layer< Dtype > > > layers_
Individual layers in the net.
Definition: net.hpp:281
int num_inputs() const
Input and output blob numbers.
Definition: net.hpp:198
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:127
vector< Dtype > blob_loss_weights_
Definition: net.hpp:301
void AppendParam(const NetParameter &param, const int layer_id, const int param_id)
Append a new parameter blob to the net.
Definition: net.cpp:421
Definition: net.hpp:231
void UpdateDebugInfo(const int param_id)
Helper for displaying debug info in Update.
Definition: net.cpp:639
void Reshape()
Reshape all layers from bottom to top.
Definition: net.cpp:726
Dtype ForwardFromTo(int start, int end)
Definition: net.cpp:516
const vector< shared_ptr< Layer< Dtype > > > & layers() const
returns the layers
Definition: net.hpp:133
size_t memory_used_
The bytes of memory used by this net.
Definition: net.hpp:330
void Update()
Updates the network weights based on the diff values computed.
Definition: net.cpp:907
bool debug_info_
Whether to compute and display debug info for the net.
Definition: net.hpp:332
const vector< shared_ptr< Blob< Dtype > > > & blobs() const
returns the blobs
Definition: net.hpp:129
vector< vector< Blob< Dtype > * > > top_vecs_
top_vecs stores the vectors containing the output for each layer
Definition: net.hpp:297
const string & name() const
returns the network name.
Definition: net.hpp:123
void ClearParamDiffs()
Zeroes out the diffs of all net parameters. Should be run before Backward.
Definition: net.cpp:914
void BackwardDebugInfo(const int layer_id)
Helper for displaying debug info in Backward.
Definition: net.cpp:612
void ShareWeights()
Shares weight data of owner blobs with shared blobs.
Definition: net.cpp:935
void Init(const NetParameter &param)
Initialize a network with a NetParameter.
Definition: net.cpp:44
void Backward()
Definition: net.cpp:707
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:149
void ShareTrainedLayersWith(const Net *other)
For an already initialized net, implicitly copies (i.e., using no additional memory) the pre-trained ...
Definition: net.cpp:665
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:396
const vector< float > & params_weight_decay() const
returns the learnable parameter decay multipliers
Definition: net.hpp:184
vector< float > params_weight_decay_
the weight decay multipliers for learnable_params_
Definition: net.hpp:327
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:259
const vector< int > & top_ids(int i) const
returns the ids of the top blobs of layer i
Definition: net.hpp:153
const vector< int > & bottom_ids(int i) const
returns the ids of the bottom blobs of layer i
Definition: net.hpp:159
void ToProto(NetParameter *param, bool write_diff=false) const
Writes the net to a proto.
Definition: net.cpp:838
void ToHDF5(const string &filename, bool write_diff=false) const
Writes the net to an HDF5 file.
Definition: net.cpp:850
vector< int > learnable_param_ids_
Definition: net.hpp:322
vector< shared_ptr< Blob< Dtype > > > params_
The parameters in the network.
Definition: net.hpp:313
Phase phase_
The phase: TRAIN or TEST.
Definition: net.hpp:279
void CopyTrainedLayersFrom(const NetParameter &param)
For an already initialized net, copies the pre-trained layers from another Net.
Definition: net.cpp:733
static bool StateMeetsRule(const NetState &state, const NetStateRule &rule, const string &layer_name)
return whether NetState state meets NetStateRule rule
Definition: net.cpp:289
const vector< Blob< Dtype > * > & Forward(Dtype *loss=NULL)
Run Forward and return the result.
Definition: net.cpp:545
const vector< shared_ptr< Blob< Dtype > > > & params() const
returns the parameters
Definition: net.hpp:174
void ForwardDebugInfo(const int layer_id)
Helper for displaying debug info in Forward.
Definition: net.cpp:586
vector< float > params_lr_
the learning rate multipliers for learnable_params_
Definition: net.hpp:324
vector< int > net_input_blob_indices_
blob indices for the input and the output of the net
Definition: net.hpp:308
const vector< string > & layer_names() const
returns the layer names
Definition: net.hpp:125
const vector< float > & params_lr() const
returns the learnable parameter learning rate multipliers
Definition: net.hpp:181
Phase phase() const
returns the phase: TRAIN or TEST
Definition: net.hpp:137
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:286
vector< vector< Blob< Dtype > * > > bottom_vecs_
Definition: net.hpp:293
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:142
string name_
The network name.
Definition: net.hpp:277
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24