10 #include "caffe/blob.hpp" 11 #include "caffe/common.hpp" 12 #include "caffe/layer.hpp" 13 #include "caffe/proto/caffe.pb.h" 23 template <
typename Dtype>
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);
32 void Init(
const NetParameter& param);
38 const vector<Blob<Dtype>*>&
Forward(Dtype* loss = NULL);
41 LOG_EVERY_N(WARNING, 1000) <<
"DEPRECATED: ForwardPrefilled() " 42 <<
"will be removed in a future version. Use Forward().";
55 Dtype ForwardFrom(
int start);
56 Dtype ForwardTo(
int end);
73 void BackwardFromTo(
int start,
int end);
74 void BackwardFrom(
int start);
75 void BackwardTo(
int end);
85 Dtype ForwardBackward() {
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;
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 {
133 inline const vector<shared_ptr<Layer<Dtype> > >&
layers()
const {
149 inline const vector<vector<Blob<Dtype>*> >&
top_vecs()
const {
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];
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];
164 inline const vector<vector<bool> >& bottom_need_backward()
const {
165 return bottom_need_backward_;
167 inline const vector<Dtype>& blob_loss_weights()
const {
170 inline const vector<bool>& layer_need_backward()
const {
171 return layer_need_backward_;
174 inline const vector<shared_ptr<Blob<Dtype> > >&
params()
const {
177 inline const vector<Blob<Dtype>*>& learnable_params()
const {
178 return learnable_params_;
182 inline const vector<bool>& has_params_lr()
const {
return has_params_lr_; }
187 inline const vector<bool>& has_params_decay()
const {
188 return has_params_decay_;
190 const map<string, int>& param_names_index()
const {
191 return param_names_index_;
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_;
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_;
203 inline const vector<Blob<Dtype>*>& output_blobs()
const {
204 return net_output_blobs_;
206 inline const vector<int>& input_blob_indices()
const {
209 inline const vector<int>& output_blob_indices()
const {
210 return net_output_blob_indices_;
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;
217 void set_debug_info(
const bool value) {
debug_info_ = value; }
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);
233 virtual void run(
int layer) = 0;
235 template <
typename T>
238 const vector<Callback*>& before_forward()
const {
return before_forward_; }
239 void add_before_forward(
Callback* value) {
240 before_forward_.push_back(value);
242 const vector<Callback*>& after_forward()
const {
return after_forward_; }
243 void add_after_forward(
Callback* value) {
244 after_forward_.push_back(value);
246 const vector<Callback*>& before_backward()
const {
return before_backward_; }
247 void add_before_backward(
Callback* value) {
248 before_backward_.push_back(value);
250 const vector<Callback*>& after_backward()
const {
return after_backward_; }
251 void add_after_backward(
Callback* value) {
252 after_backward_.push_back(value);
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,
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_;
294 vector<vector<int> > bottom_id_vecs_;
295 vector<vector<bool> > bottom_need_backward_;
298 vector<vector<int> > top_id_vecs_;
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_;
314 vector<Blob<Dtype>*> learnable_params_;
325 vector<bool> has_params_lr_;
328 vector<bool> has_params_decay_;
334 vector<Callback*> before_forward_;
335 vector<Callback*> after_forward_;
336 vector<Callback*> before_backward_;
337 vector<Callback*> after_backward_;
339 DISABLE_COPY_AND_ASSIGN(
Net);
345 #endif // CAFFE_NET_HPP_ void AppendTop(const NetParameter ¶m, 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 ¶m, const int layer_id, const int param_id)
Append a new parameter blob to the net.
Definition: net.cpp:421
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 ¶m)
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'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 ¶m, 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 ¶m, 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 ¶m)
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'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