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 const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom);
28 // The network backward should take no input and output, since it solely
29 // computes the gradient w.r.t the parameters, and the data has already
30 // been provided during the forward pass.
31 Dtype Backward();
33 Dtype ForwardBackWard(const vector<Blob<Dtype>* > & bottom) {
34 Forward(bottom);
35 return Backward();
36 }
38 // For an already initialized net, CopyTrainedLayersFrom() copies the already
39 // trained layers from another net parameter instance.
40 void CopyTrainedLayersFrom(const NetParameter& param);
41 // Writes the net to a proto.
42 void ToProto(NetParameter* param, bool write_diff = false);
44 // returns the network name.
45 inline const string& name() { return name_; }
46 // returns the layer names
47 inline const vector<string>& layer_names() { return layer_names_; }
48 // returns the blob names
49 inline const vector<string>& blob_names() { return blob_names_; }
50 // returns the blobs
51 inline const vector<shared_ptr<Blob<Dtype> > >& blobs() { return blobs_; }
52 // returns the layers
53 inline const vector<shared_ptr<Layer<Dtype> > >& layers() { return layers_; }
54 // returns the parameters
55 vector<shared_ptr<Blob<Dtype> > >& params() { return params_; };
56 // Updates the network
57 void Update();
59 protected:
60 // Individual layers in the net
61 vector<shared_ptr<Layer<Dtype> > > layers_;
62 vector<string> layer_names_;
63 // blobs stores the blobs that store intermediate results between the
64 // layers.
65 vector<shared_ptr<Blob<Dtype> > > blobs_;
66 vector<string> blob_names_;
67 // bottom_vecs stores the vectors containing the input for each layer
68 vector<vector<Blob<Dtype>*> > bottom_vecs_;
69 vector<vector<int> > bottom_id_vecs_;
70 // top_vecs stores the vectors containing the output for each layer
71 vector<vector<Blob<Dtype>*> > top_vecs_;
72 vector<vector<int> > top_id_vecs_;
73 // blob indices for the input and the output of the net.
74 vector<int> net_input_blob_indices_;
75 vector<int> net_output_blob_indices_;
76 vector<Blob<Dtype>*> net_output_blobs_;
77 string name_;
78 // The parameters in the network.
79 vector<shared_ptr<Blob<Dtype> > > params_;
81 DISABLE_COPY_AND_ASSIGN(Net);
82 };
85 } // namespace caffe
87 #endif // CAFFE_NET_HPP_