1. 程式人生 > >caffe protobuf詳解

caffe protobuf詳解

方法 type source protobuf 輸出 filter out cat hdf

1.數據層

layer {
  name: "cifar"
  type: "Data"
  top: "data" #一般用bottom表示輸入,top表示輸出,多個top代表有多個輸出
  top: "label"
  include {
  phase: TRAIN #訓練網絡分為訓練階段和自測試階段,如果沒寫include則表示該層即在測試中,又在訓練中
  }
  transform_param {
  mean_file: "examples/cifar10/mean.binaryproto" #用一個配置文件來進行均值的操作
  transform_param {
  scale: 0.00390625
  mirror: 1 # 1表示開啟鏡像,0表示關閉,也可用ture和false來表示
  # 剪裁一個 227*227的圖塊,在訓練階段隨機剪裁,在測試階段從中間裁剪
  crop_size: 227
  }
  }
  data_param {
  source: "examples/cifar10/cifar10_train_lmdb" #數據庫來源
  batch_size: 64 #每次批處理的個數
  backend: LMDB #選用數據的名稱
  }
}

### 使用LMDB源
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
  phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
  source: "examples/mnist/mnist_train_lmdb"
  batch_size: 64
  backend: LMDB
}
}

###使用HDF5數據源
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
  source: "examples/hdf5_classification/data/train.txt"
  batch_size: 10
  }
}

###數據直接來源與圖片
#/path/to/images/img3423.jpg 2
#/path/to/images/img3424.jpg 13
#/path/to/images/img3425.jpg 8

layer {
  name: "data"
  type: "ImageData" #類型
  top: "data"
  top: "label"
  transform_param {
  mirror: false
  crop_size: 227
  mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
image_data_param {
  source: "examples/_temp/file_list.txt"
  batch_size: 50
  new_height: 256 #如果設置就對圖片進行resize操作
  new_width: 256
}
}

2.卷積層

layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
  lr_mult: 1 #lr_mult: 學習率的系數,最終的學習率是這個數乘以solver.prototxt配置文件中的base_lr。如果有兩個lr_mult, 則第一個表示權值的學習率,第二個表示偏置項的學習率。一般偏置項的學習率是權值學習率的兩倍。
  }
  param {
  lr_mult: 2
  }
convolution_param {
  num_output: 20 #卷積核(filter)的個數
  kernel_size: 5 #卷積核的大小
  stride: 1 #卷積核的步長,默認為1
  pad: 0 #擴充邊緣,默認為0,不擴充
  weight_filler {
  type: "xavier" #權值初始化。 默認為“constant",值全為0,很多時候我們用"xavier"算法來進行初始化,也可以設置為”gaussian"
  }
  bias_filler {
  type: "constant" #偏置項的初始化。一般設置為"constant",值全為0
  }
}
}

輸入:n*c0*w0*h0
輸出:n*c1*w1*h1
其中,c1就是參數中的num_output,生成的特征圖個數
w1=(w0+2*pad-kernel_size)/stride+1;
h1=(h0+2*pad-kernel_size)/stride+1;

3.池化層

layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
  pool: MAX #池化方法,默認為MAX。目前可用的方法有MAX, AVE
  kernel_size: 3 #池化的核大小
  stride: 2 #池化的步長,默認為1。一般我們設置為2,即不重疊。
  }
}

#pooling層的運算方法基本是和卷積層是一樣的。

4.激活函數

#在激活層中,對輸入數據進行激活操作,是逐元素進行運算的,在運算過程中,沒有改變數據的大小,即輸入和輸出的數據大小是相等的。

#ReLU是目前使用最多的激活函數,主要因為其收斂更快,並且能保持同樣效果。標準的ReLU函數為max(x, 0),當x>0時,輸出x; 當x<=0時,輸出0
f(x)=max(x,0)

layer {
  name: "relu1"
  type: "ReLU"
  bottom: "pool1"
  top: "pool1"
}

5.全連接層

#全連接層,輸出的是一個簡單向量 參數跟卷積層一樣
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
  lr_mult: 1
  }
  param {
  lr_mult: 2
  }
  inner_product_param {
  num_output: 500
  weight_filler {
  type: "xavier"
  }
  bias_filler {
  type: "constant"
  }
  }
}
#測試的時候輸入準確率
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
  phase: TEST
  }
}

caffe protobuf詳解