http://blog.csdn.net/kkk584520/article/details/41681085
- (1)从CAFFE_ROOT/src/caffe/proto/caffe.proto开始,了解各类数据结构,主要是内存对象和序列化磁盘文件的一一对应关系,知道如何从磁盘Load一个对象到内存,以及如何将内存对象Save到磁盘,中间的过程实现都是由Protobuf自动完成的。
- (2)看头文件,先理解整个框架。Caffe中类数目众多,但脉络十分清晰。在Testing时,最外层的类是Caffe::Net,包含了多个Caffe::Layer对象,而Layer对象派生出神经网络多种不同层的类(DataLayer,ConvolutionLayer,InnerProductionLayer,AccurancyLayer等),每层会有相应的输入输出(Blob对象)以及层的参数(可选,Blob对象);Blob中包括了SyncedMemory对象,统一了CPU和GPU存储器。自顶向下去看这些类,结合理论知识很容易掌握使用方法。
- (3)有针对性地去看cpp和cu文件。一般而言,Caffe框架不需要修改,只需要增加新的层实现即可。例如想自己实现卷积层,只需从ConvolutionLayer派生一个新类MyConvolutionLayer,然后将几个虚函数改成自己的实现即可。所以这一阶段关注点在算法上,而不是源码本身。
- (4)可以自己尝试编写各类工具,集成到Caffe内部。在CAFFE_ROOT/tools/下面有很多实用工具,可以根据需要修改。例如从训练好的模型中抽取参数进行可视化可以用Python结合matplot实现。
参考 http://blog.csdn.net/kkk584520/article/details/41046827
http://www.cnblogs.com/yymn/p/5167013.html
message NetParameter {
optional string name = 1; // consider giving the network a name
// DEPRECATED. See InputParameter. The input blobs to the network.
repeated string input = 3;
// DEPRECATED. See InputParameter. The shape of the input blobs.
repeated BlobShape input_shape = 8;
// 4D input dimensions -- deprecated. Use "input_shape" instead.
// If specified, for each input blob there should be four
// values specifying the num, channels, height and width of the input blob.
// Thus, there should be a total of (4 * #input) numbers.
repeated int32 input_dim = 4;
// Whether the network will force every layer to carry out backward operation.
// If set False, then whether to carry out backward is determined
// automatically according to the net structure and learning rates.
optional bool force_backward = 5 [default = false];
// The current "state" of the network, including the phase, level, and stage.
// Some layers may be included/excluded depending on this state and the states
// specified in the layers' include and exclude fields.
optional NetState state = 6;
// Print debugging information about results while running Net::Forward,
// Net::Backward, and Net::Update.
optional bool debug_info = 7 [default = false];
// The layers that make up the net. Each of their configurations, including
// connectivity and behavior, is specified as a LayerParameter.
repeated LayerParameter layer = 100; // ID 100 so layers are printed last.
// DEPRECATED: use 'layer' instead.
repeated V1LayerParameter layers = 2;
解析
限定修饰符① | 数据类型② | 字段名称③ | = | 字段编码值④ | [字段默认值⑤]
认识protobuf对后续学习源码很有好处,在源码里经常会出现一些参数操作,如果不了解protobuf的存在就会对这些操作无所适从不知出处。
template
class Blob {
public:
Blob(): data_(), diff_(), count_(0), capacity_(0) {}
/// @brief Deprecated; use Blob(const vector& shape)
.
explicit Blob(const int num, const int channels, const int height, const int width);
explicit Blob(const vector& shape);
/// @brief Deprecated; use Reshape(const vector& shape)
.
void Reshape(const int num, const int channels, const int height, const int width);
......
protected:
shared_ptr data_;
shared_ptr diff_;
shared_ptr shape_data_;
vector shape_;
int count_;
int capacity_;
DISABLE_COPY_AND_ASSIGN(Blob);
}; // class Blob
Layer类
五大派生类
layer类源代码
template
class Layer {
public:
explicit Layer(const LayerParameter& param) : layer_param_(param) {
// Set phase and copy blobs (if there are any).
phase_ = param.phase();
if (layer_param_.blobs_size() > 0) {
blobs_.resize(layer_param_.blobs_size());
for (int i = 0; i < layer_param_.blobs_size(); ++i) {
blobs_[i].reset(new Blob());
blobs_[i]->FromProto(layer_param_.blobs(i));
}
}
}
virtual ~Layer() {}
void SetUp(const vector*>& bottom, const vector*>& top) {
CheckBlobCounts(bottom, top);
LayerSetUp(bottom, top);
Reshape(bottom, top);
SetLossWeights(top);
}
inline Dtype Forward(const vector*>& bottom, const vector*>& top);
inline void Backward(const vector*>& top, const vector& propagate_down, const vector*>& bottom);
......
protected:
/** The protobuf that stores the layer parameters */
LayerParameter layer_param_;
/** The phase: TRAIN or TEST */
Phase phase_;
/** The vector that stores the learnable parameters as a set of blobs. */
vector > > blobs_;
/** Vector indicating whether to compute the diff of each param blob. */
vector param_propagate_down_;
/** The vector that indicates whether each top blob has a non-zero weight in the objective function. */
vector loss_;
/** @brief Using the CPU device, compute the layer output. */
virtual void Forward_cpu(const vector*>& bottom, const vector*>& top) = 0;
/**@brief Using the GPU device, compute the layer output. Fall back to Forward_cpu() if unavailable. */
virtual void Forward_gpu(const vector*>& bottom, const vector*>& top) {
// LOG(WARNING) << "Using CPU code as backup.";
return Forward_cpu(bottom, top);
}
/**@brief Using the CPU device, compute the gradients for any parameters and for the bottom blobs if propagate_down is true.*/
virtual void Backward_cpu(const vector*>& top, const vector& propagate_down, const vector*>& bottom) = 0;
/** @brief Using the GPU device, compute the gradients for any parameters and for the bottom blobs if propagate_down is true. Fall back to Backward_cpu() if unavailable. */
virtual void Backward_gpu(const vector*>& top, const vector& propagate_down, const vector*>& bottom) {
// LOG(WARNING) << "Using CPU code as backup.";
Backward_cpu(top, propagate_down, bottom);
}
......
private:
DISABLE_COPY_AND_ASSIGN(Layer);
}; // class Layer
template
class Net {
public:
explicit Net(const NetParameter& param);
explicit Net(const string& param_file, Phase phase, const int level = 0, const vector* stages = NULL);
virtual ~Net() {}
/// @brief Initialize a network with a NetParameter.
void Init(const NetParameter& param);
......
const vector*>& Forward(const vector* > & bottom, Dtype* loss = NULL);
......
void Backward();
Dtype ForwardBackward() {
Dtype loss;
Forward(&loss);
Backward();
return loss;
}
protected:
/// @brief The network name
string name_;
/// @brief The phase: TRAIN or TEST
Phase phase_;
/// @brief Individual layers in the net
vector > > layers_;
vector layer_names_;
map layer_names_index_;
vector layer_need_backward_;
/// @brief the blobs storing intermediate results between the layer.
vector > > blobs_;
......
vector*> > bottom_vecs_;
......
vector*> > top_vecs_;
......
DISABLE_COPY_AND_ASSIGN(Net);
};
} // namespace caffe
参考
http://blog.csdn.net/mounty_fsc/article/details/51085654
http://blog.csdn.net/mounty_fsc/article/details/51088173