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 // For an already initialized net, CopyTrainedLayersFrom() copies the already
35 // trained layers from another net parameter instance.
36 void CopyTrainedLayersFrom(const NetParameter& param);
37 // Writes the net to a proto.
38 void ToProto(NetParameter* param, bool write_diff = false);
40 // returns the network name.
41 inline const string& name() { return name_; }
42 // returns the layer names
43 inline const vector<string>& layer_names() { return layer_names_; }
44 // returns the blob names
45 inline const vector<string>& blob_names() { return blob_names_; }
46 // returns the blobs
47 inline const vector<shared_ptr<Blob<Dtype> > >& blobs() { return blobs_; }
48 // rethrns the layers
49 inline const vector<shared_ptr<Layer<Dtype> > >& layers() { return layers_; }
51 protected:
52 // Individual layers in the net
53 vector<shared_ptr<Layer<Dtype> > > layers_;
54 vector<string> layer_names_;
55 // blobs stores the blobs that store intermediate results between the
56 // layers.
57 vector<shared_ptr<Blob<Dtype> > > blobs_;
58 vector<string> blob_names_;
59 // bottom_vecs stores the vectors containing the input for each layer, except
60 // for the first layer whose bottom vec is provided by the network's input.
61 vector<vector<Blob<Dtype>*> > bottom_vecs_;
62 // top_vecs stores the vectors containing the output for each layer, except
63 // for the last layer (likewise)
64 vector<vector<Blob<Dtype>*> > top_vecs_;
65 // blob indices for the input and the output of the net.
66 vector<int> net_input_blob_indices_;
67 vector<int> net_output_blob_indices_;
68 string name_;
69 };
72 } // namespace caffe
74 #endif // CAFFE_NET_HPP_