c27442b8243da29d3f2adadf0211d1b7651a6350
[platform/upstream/caffeonacl.git] / src / caffe / net.hpp
1 // Copyright 2013 Yangqing Jia
2
3 #ifndef CAFFE_NET_HPP_
4 #define CAFFE_NET_HPP_
5
6 #include <map>
7 #include <string>
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 using std::map;
16 using std::vector;
17 using std::string;
18
19 namespace caffe {
20
21
22 template <typename Dtype>
23 class Net {
24  public:
25   Net(const NetParameter& param,
26       const vector<Blob<Dtype>* >& bottom);
27   ~Net() {}
28   const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom);
29   // The network backward should take no input and output, since it solely
30   // computes the gradient w.r.t the parameters, and the data has already
31   // been provided during the forward pass.
32   Dtype Backward();
33
34   Dtype ForwardBackward(const vector<Blob<Dtype>* > & bottom) {
35     Forward(bottom);
36     return Backward();
37   }
38
39   // For an already initialized net, CopyTrainedLayersFrom() copies the already
40   // trained layers from another net parameter instance.
41   void CopyTrainedLayersFrom(const NetParameter& param);
42   // Writes the net to a proto.
43   void ToProto(NetParameter* param, bool write_diff = false);
44
45   // returns the network name.
46   inline const string& name() { return name_; }
47   // returns the layer names
48   inline const vector<string>& layer_names() { return layer_names_; }
49   // returns the blob names
50   inline const vector<string>& blob_names() { return blob_names_; }
51   // returns the blobs
52   inline const vector<shared_ptr<Blob<Dtype> > >& blobs() { return blobs_; }
53   // returns the layers
54   inline const vector<shared_ptr<Layer<Dtype> > >& layers() { return layers_; }
55   // returns the bottom and top vecs for each layer - usually you won't need
56   // this unless you do per-layer checks such as gradients.
57   inline vector<vector<Blob<Dtype>*> >& bottom_vecs() { return bottom_vecs_; }
58   inline vector<vector<Blob<Dtype>*> >& top_vecs() { return top_vecs_; }
59   // returns the parameters
60   inline vector<shared_ptr<Blob<Dtype> > >& params() { return params_; }
61   // returns the parameter learning rate multipliers
62   inline vector<float>& params_lr() {return params_lr_; }
63   // Updates the network
64   void Update();
65
66  protected:
67   // Individual layers in the net
68   vector<shared_ptr<Layer<Dtype> > > layers_;
69   vector<string> layer_names_;
70   // blobs stores the blobs that store intermediate results between the
71   // layers.
72   vector<shared_ptr<Blob<Dtype> > > blobs_;
73   vector<string> blob_names_;
74   // bottom_vecs stores the vectors containing the input for each layer
75   vector<vector<Blob<Dtype>*> > bottom_vecs_;
76   vector<vector<int> > bottom_id_vecs_;
77   // top_vecs stores the vectors containing the output for each layer
78   vector<vector<Blob<Dtype>*> > top_vecs_;
79   vector<vector<int> > top_id_vecs_;
80   // blob indices for the input and the output of the net.
81   vector<int> net_input_blob_indices_;
82   vector<int> net_output_blob_indices_;
83   vector<Blob<Dtype>*> net_output_blobs_;
84   string name_;
85   // The parameters in the network.
86   vector<shared_ptr<Blob<Dtype> > > params_;
87   // the learning rate multipliers
88   vector<float> params_lr_;
89   DISABLE_COPY_AND_ASSIGN(Net);
90 };
91
92
93 }  // namespace caffe
94
95 #endif  // CAFFE_NET_HPP_