1 // Copyright 2013 Yangqing Jia
3 #ifndef CAFFE_NET_HPP_
4 #define CAFFE_NET_HPP_
6 #include <map>
7 #include <string>
8 #include <vector>
10 #include "caffe/blob.hpp"
11 #include "caffe/layer.hpp"
12 #include "caffe/common.hpp"
13 #include "caffe/proto/caffe.pb.h"
15 using std::map;
16 using std::vector;
17 using std::string;
19 namespace caffe {
21 template <typename Dtype>
22 class Net {
23 public:
24 Net(const NetParameter& param,
25 const vector<Blob<Dtype>* >& bottom);
26 ~Net() {}
27 void Forward(const vector<Blob<Dtype>* > & bottom,
28 vector<Blob<Dtype>*>* top);
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();
34 Dtype ForwardBackWard(const vector<Blob<Dtype>* > & bottom,
35 vector<Blob<Dtype>*>* top) {
36 Forward(bottom, top);
37 return Backward();
38 }
40 // For an already initialized net, CopyTrainedLayersFrom() copies the already
41 // trained layers from another net parameter instance.
42 void CopyTrainedLayersFrom(const NetParameter& param);
43 // Writes the net to a proto.
44 void ToProto(NetParameter* param, bool write_diff = false);
46 // returns the network name.
47 inline const string& name() { return name_; }
48 // returns the layer names
49 inline const vector<string>& layer_names() { return layer_names_; }
50 // returns the blob names
51 inline const vector<string>& blob_names() { return blob_names_; }
52 // returns the blobs
53 inline const vector<shared_ptr<Blob<Dtype> > >& blobs() { return blobs_; }
54 // returns the layers
55 inline const vector<shared_ptr<Layer<Dtype> > >& layers() { return layers_; }
56 // returns the parameters
57 vector<shared_ptr<Blob<Dtype> > >& params() { return params_; };
58 // Updates the network
59 void Update();
61 protected:
62 // Individual layers in the net
63 vector<shared_ptr<Layer<Dtype> > > layers_;
64 vector<string> layer_names_;
65 // blobs stores the blobs that store intermediate results between the
66 // layers.
67 vector<shared_ptr<Blob<Dtype> > > blobs_;
68 vector<string> blob_names_;
69 // bottom_vecs stores the vectors containing the input for each layer, except
70 // for the first layer whose bottom vec is provided by the network's input.
71 vector<vector<Blob<Dtype>*> > bottom_vecs_;
72 vector<vector<int> > bottom_id_vecs_;
73 // top_vecs stores the vectors containing the output for each layer, except
74 // for the last layer (likewise)
75 vector<vector<Blob<Dtype>*> > top_vecs_;
76 vector<vector<int> > top_id_vecs_;
77 // blob indices for the input and the output of the net.
78 vector<int> net_input_blob_indices_;
79 vector<int> net_output_blob_indices_;
80 string name_;
81 // The parameters in the network.
82 vector<shared_ptr<Blob<Dtype> > > params_;
84 DISABLE_COPY_AND_ASSIGN(Net);
85 };
88 } // namespace caffe
90 #endif // CAFFE_NET_HPP_