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,
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);
33 void Init(
const NetParameter& param);
39 const vector<Blob<Dtype>*>&
Forward(Dtype* loss = NULL);
42 LOG_EVERY_N(WARNING, 1000) <<
"DEPRECATED: ForwardPrefilled() " 43 <<
"will be removed in a future version. Use Forward().";
56 Dtype ForwardFrom(
int start);
57 Dtype ForwardTo(
int end);
74 void BackwardFromTo(
int start,
int end);
75 void BackwardFrom(
int start);
76 void BackwardTo(
int end);
86 Dtype ForwardBackward() {
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;
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 {
134 inline const vector<shared_ptr<Layer<Dtype> > >&
layers()
const {
150 inline const vector<vector<Blob<Dtype>*> >&
top_vecs()
const {
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];
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];
165 inline const vector<vector<bool> >& bottom_need_backward()
const {
166 return bottom_need_backward_;
168 inline const vector<Dtype>& blob_loss_weights()
const {
171 inline const vector<bool>& layer_need_backward()
const {
172 return layer_need_backward_;
175 inline const vector<shared_ptr<Blob<Dtype> > >&
params()
const {
178 inline const vector<Blob<Dtype>*>& learnable_params()
const {
179 return learnable_params_;
183 inline const vector<bool>& has_params_lr()
const {
return has_params_lr_; }
188 inline const vector<bool>& has_params_decay()
const {
189 return has_params_decay_;
191 const map<string, int>& param_names_index()
const {
192 return param_names_index_;
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_;
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_;
204 inline const vector<Blob<Dtype>*>& output_blobs()
const {
205 return net_output_blobs_;
207 inline const vector<int>& input_blob_indices()
const {
210 inline const vector<int>& output_blob_indices()
const {
211 return net_output_blob_indices_;
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;
218 void set_debug_info(
const bool value) {
debug_info_ = value; }
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);
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,
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_;
270 vector<vector<int> > bottom_id_vecs_;
271 vector<vector<bool> > bottom_need_backward_;
274 vector<vector<int> > top_id_vecs_;
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_;
290 vector<Blob<Dtype>*> learnable_params_;
301 vector<bool> has_params_lr_;
304 vector<bool> has_params_decay_;
311 DISABLE_COPY_AND_ASSIGN(
Net);
317 #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: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 ¶m, 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 ¶m)
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'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 ¶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: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 ¶m, 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 ¶m)
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'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