要看caffe源碼垒酬,我認(rèn)為首先應(yīng)該看的就是caffe.proto。
它位于src/caffe/proto目錄下,在這個(gè)文件夾下還有一個(gè).pb.cc和一個(gè).pb.h文件罪治,這兩個(gè)文件都是由caffe.proto編譯而來(lái)的娜遵。
什么是protocol buffer
Google Protocol Buffer( 簡(jiǎn)稱 Protobuf) 是 Google 公司內(nèi)部的混合語(yǔ)言數(shù)據(jù)標(biāo)準(zhǔn)蜕衡,目前已經(jīng)正在使用的有超過(guò) 48,162 種報(bào)文格式定義和超過(guò) 12,183 個(gè) .proto 文件。更多關(guān)于Protobuf的介紹可以參考相關(guān)官網(wǎng)內(nèi)容.
caffe.proto
caffe.proto文件是一個(gè)消息格式文件设拟,后綴名為proto. proto文件即消息協(xié)議原型定義文件慨仿,在該文件中可以通過(guò)使用描述性語(yǔ)言來(lái)定義程序中需要用到的數(shù)據(jù)格式。
caffe.proto文件在caffe中的作用:
- 定義了很多結(jié)構(gòu)化數(shù)據(jù)纳胧,用于構(gòu)建Caffe網(wǎng)絡(luò)镰吆,即*.prototxt文件中的每個(gè)字段名要在caffe.proto中存在;
- 負(fù)責(zé)*.caffemodel數(shù)據(jù)文件的存儲(chǔ)和讀扰苣健万皿;
- 每次向Caffe中增加新的層,相應(yīng)的caffe.proto文件也需調(diào)整相赁,并需重新生成caffe.pb.h/caffe.pb.cc文件的止;
- 注意選擇Protobuf的版本要與Caffe中的一致,否則會(huì)產(chǎn)生Protobuf版本不一致的error围苫。
caffe.proto中的幾個(gè)重要數(shù)據(jù)類型
BlobProto
message BlobProto {//blob的屬性以及blob中的數(shù)據(jù)(data\diff)
optional int32 num = 1 [default = 0];
optional int32 channels = 2 [default = 0];
optional int32 height = 3 [default = 0];
optional int32 width = 4 [default = 0];
repeated float data = 5 [packed = true];
repeated float diff = 6 [packed = true];
}
Datum
message Datum {
optional int32 channels = 1;
optional int32 height = 2;
optional int32 width = 3;
optional bytes data = 4;//真實(shí)的圖像數(shù)據(jù)奄喂,以字節(jié)存儲(chǔ)(bytes)
optional int32 label = 5;
repeated float float_data = 6;//datum也能存float類型的數(shù)據(jù)(float)
}
LayerParameter
message LayerParameter {
repeated string bottom = 2; //輸入的blob的名字(string)
repeated string top = 3; //輸出的blob的名字(string)
optional string name = 4; //層的名字
enum LayerType { //層的枚舉(enum,和c++中的enum一樣)
NONE = 0;
ACCURACY = 1;
BNLL = 2;
CONCAT = 3;
CONVOLUTION = 4;
DATA = 5;
DROPOUT = 6;
EUCLIDEAN_LOSS = 7;
ELTWISE_PRODUCT = 25;
FLATTEN = 8;
HDF5_DATA = 9;
HDF5_OUTPUT = 10;
HINGE_LOSS = 28;
IM2COL = 11;
IMAGE_DATA = 12;
INFOGAIN_LOSS = 13;
INNER_PRODUCT = 14;
LRN = 15;
MEMORY_DATA = 29;
MULTINOMIAL_LOGISTIC_LOSS = 16;
POOLING = 17;
POWER = 26;
RELU = 18;
SIGMOID = 19;
SIGMOID_CROSS_ENTROPY_LOSS = 27;
SOFTMAX = 20;
SOFTMAX_LOSS = 21;
SPLIT = 22;
TANH = 23;
WINDOW_DATA = 24;
}
optional LayerType type = 5; // 層的類型
repeated BlobProto blobs = 6; //blobs的數(shù)值參數(shù)
repeated float blobs_lr = 7; //學(xué)習(xí)速率(repeated)绵脯,如果你想那個(gè)設(shè)置一個(gè)blob的學(xué)習(xí)速率佳励,你需要設(shè)置所有blob的學(xué)習(xí)速率。
repeated float weight_decay = 8; //權(quán)值衰減(repeated)
// 相對(duì)于某一特定層的參數(shù)(optional)
optional ConcatParameter concat_param = 9;
optional ConvolutionParameter convolution_param = 10;
optional DataParameter data_param = 11;
optional DropoutParameter dropout_param = 12;
optional HDF5DataParameter hdf5_data_param = 13;
optional HDF5OutputParameter hdf5_output_param = 14;
optional ImageDataParameter image_data_param = 15;
optional InfogainLossParameter infogain_loss_param = 16;
optional InnerProductParameter inner_product_param = 17;
optional LRNParameter lrn_param = 18;
optional MemoryDataParameter memory_data_param = 22;
optional PoolingParameter pooling_param = 19;
optional PowerParameter power_param = 21;
optional WindowDataParameter window_data_param = 20;
optional V0LayerParameter layer = 1;
}
NetParameter
message NetParameter {
optional string name = 1;//網(wǎng)絡(luò)的名字
repeated LayerParameter layers = 2; //repeated類似于數(shù)組
repeated string input = 3;//輸入層blob的名字
repeated int32 input_dim = 4;//輸入層blob的維度蛆挫,應(yīng)該等于(4*#input)
optional bool force_backward = 5 [default = false];//網(wǎng)絡(luò)是否進(jìn)行反向傳播赃承。如果設(shè)置為否,則由網(wǎng)絡(luò)的結(jié)構(gòu)和學(xué)習(xí)速率來(lái)決定是否進(jìn)行反向傳播悴侵。
}
SolverParameter
message SolverParameter {
optional string train_net = 1; // 訓(xùn)練網(wǎng)絡(luò)的proto file
optional string test_net = 2; // 測(cè)試網(wǎng)絡(luò)的proto file
optional int32 test_iter = 3 [default = 0]; // 每次測(cè)試時(shí)的迭代次數(shù)
optional int32 test_interval = 4 [default = 0]; // 兩次測(cè)試的間隔迭代次數(shù)
optional bool test_compute_loss = 19 [default = false];
optional float base_lr = 5; // 基本學(xué)習(xí)率
optional int32 display = 6; // 兩次顯示的間隔迭代次數(shù)
optional int32 max_iter = 7; // 最大迭代次數(shù)
optional string lr_policy = 8; // 學(xué)習(xí)速率衰減方式
optional float gamma = 9; // 關(guān)于梯度下降的一個(gè)參數(shù)
optional float power = 10; // 計(jì)算學(xué)習(xí)率的一個(gè)參數(shù)
optional float momentum = 11; // 動(dòng)量
optional float weight_decay = 12; // 權(quán)值衰減
optional int32 stepsize = 13; // 學(xué)習(xí)速率的衰減步長(zhǎng)
optional int32 snapshot = 14 [default = 0]; // snapshot的間隔
optional string snapshot_prefix = 15; // snapshot的前綴
optional bool snapshot_diff = 16 [default = false]; // 是否對(duì)于 diff 進(jìn)行 snapshot
enum SolverMode {
CPU = 0;
GPU = 1;
}
optional SolverMode solver_mode = 17 [default = GPU]; // solver的模式瞧剖,默認(rèn)為GPU
optional int32 device_id = 18 [default = 0]; // GPU的ID
optional int64 random_seed = 20 [default = -1]; // 隨機(jī)數(shù)種子
}