1. 程式人生 > >caffe(2)配置檔案.prototxt的理解

caffe(2)配置檔案.prototxt的理解

首先建立一個net,net有多層構成,層有不同的型別。網路結構定義在.prototxt檔案中。下面詳細介紹:

1. 資料層即輸入層。在caffe中資料以blob的格式進行儲存和傳輸,在這一層中是實現資料其他格式與blob之間的轉換,例如從高效的資料庫lmdb或者level-db轉換為blob,也可以從低效的資料格式如hdf5或者圖片。另外資料的預處理也在本層實現,減去均值, 放大縮小, 裁剪和映象等。以Lenet_train_test.prototxt為例:

name: "LeNet"
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
  }
}
最上面name:網路名稱,可自己定義。下面是資料層layer的定義

name: 可自己取

type:層的型別。如果是Data,表示資料來源於LevelDB或LMDB。根據資料的來源不同,資料層的型別也不同。

(1)Data:資料來源於LevelDB或者LMDB,必須設定batch_size。source為包含資料庫的目錄名稱,如examples/mnist/mnist_train_lmdb

(2)MemoryData: 資料來源於記憶體,必須設定batch_size, channels, width, height.

layer {
  top: "data"
  top: "label"
  name: "memory_data"
  type: "MemoryData"
  memory_data_param{
    batch_size: 2
    height: 100
    width: 100
    channels: 1
  }
  transform_param {
    scale: 0.0078125
    mean_file: "mean.proto"
    mirror: false
  }
}

(3)HDF5Data: 資料來源於Hdf5, 必須設定batch_size和source,讀取的檔名稱
layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "examples/hdf5_classification/data/train.txt"
    batch_size: 10
  }
}
(4)ImageData: 資料來源於圖片。必須設定的引數:

  source: 每一行是給定的圖片路徑和標籤;

  batch_size

可選設定的引數為:

  rand_skip: 在開始的時候,路過某個資料的輸入。通常對非同步的SGD很有用。

  shuffle: 隨機打亂順序,預設值為false

  new_height,new_width: 如果設定,則將圖片進行resize

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
    new_width: 256
  }
}

(5)WindowData: 來源於windows

layer {
  name: "data"
  type: "WindowData"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mirror: true
    crop_size: 227
    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
  }
  window_data_param {
    source: "examples/finetune_pascal_detection/window_file_2007_trainval.txt"
    batch_size: 128
    fg_threshold: 0.5
    bg_threshold: 0.5
    fg_fraction: 0.25
    context_pad: 16
    crop_mode: "warp"
  }
}

top:本層的輸出,例子表明有兩個輸出,data和label是分類問題所必須的

bottom:本層的輸入

include:在其中規定是訓練還是測試的層。如果沒有定義則表明訓練和測試均有此層。如例,此層為訓練層,有訓練資料和標籤

transform_param:資料預處理,scale表明對資料由0-255轉換到了[0,1)。還可以進行如:mirror(1表示開啟,0表示關閉), mean_file_size(後面跟配置檔案

mean.binaryproto, 進行去均值的處理),crop_size(剪裁,訓練資料隨機剪裁,測試資料從中間剪裁)

data_param:定義資料,source是資料路徑;將全部的圖片分為不同的批次batch,batch_size是一個批次包含的圖片數目;backend表明所用的資料庫

2. 視覺層,包括convolution卷積層, pooling池化層, Local Response Normalization (LRN)區域性極大值抑制, im2col等層。

(1)層型別:Convolution,如lenet的第一個卷積層

layer {

  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
lr_mult:第一個表示權重w的學習率的係數,學習率=base_lr(定義在solver.prototxt)×lr_mult, 第二個表示偏重bias的學習率係數

num_output: 卷積核kernel的個數

kernel_size: kernel的大小,如果卷積核的長和寬不等,需要用kernel_h和kernel_w分別設定

stride: 卷積運算的步長,預設為1。也可以用stride_h和stride_w來設定。

pad: 填充邊緣的大小。如設定,可是得到的特徵圖與原圖大小相等,pad_h和pad_w來分別設定

weight_filter: 權值初始化,若設定為constant, 則預設為0。也可使用"xavier"或者”gaussian"進行初始化

bias_filler: 偏置項的初始化,與weight_filter類似

bias_term: 是否開啟偏置項(0或1)

group: 分組,預設為1組。如果大於1,我們限制卷積的連線操作在一個子集內。如果我們根據影象的通道來分組,那麼第i個輸出分組只能與第i個輸入分組進行連線。

(2)層型別:pooling。
layer {

  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
必須設定的引數:  
kernel_size: 池化的核大小。也可以用kernel_h和kernel_w分別設定。

其它引數:
  pool: 池化方法,預設為MAX。目前可用的方法有MAX, AVE, 或STOCHASTIC
   pad: 和卷積層的pad的一樣,進行邊緣擴充。預設為0
   stride: 池化的步長,預設為1。一般我們設定為2,即不重疊。也可以用stride_h和stride_w來設定。
(3)層型別LRN

layers {
  name: "norm1"
  type: LRN
  bottom: "pool1"
  top: "norm1"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
local_size: 預設為5。如果是跨通道LRN,則表示求和的通道數;如果是在通道內LRN,則表示求和的正方形區域長度。

alpha: 預設為1,歸一化公式中的引數。

beta: 預設為5,歸一化公式中的引數。

norm_region: 預設為ACROSS_CHANNELS。有兩個選擇,ACROSS_CHANNELS表示在相鄰的通道間求和歸一化。

WITHIN_CHANNEL表示在一個通道內部特定的區域內進行求和歸一化。與前面的local_size引數對應。

歸一化公式為:除以
(4)層型別:img2col, 將一個大矩陣,重疊地劃分為多個子矩陣,對每個子矩陣序列化成向量,最後得到另外一個矩陣。

在caffe中,卷積運算就是先對資料進行im2col操作,再進行內積運算(inner product)。這樣做,比原始的卷積操作速度更快。

看看兩種卷積操作的異同:
3. 啟用層, 對輸入資料進行啟用操作,常用的啟用函式有:SigmoidTanHAbsVal(Absolute Value)RELU(ReLU / Rectified-Linear and Leaky-ReLU)收斂速度最快。
layer {

  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}

RELU函式為:max(x, 0)

可選引數:

  negative_slope:預設為0. 對標準的ReLU函式進行變化,如果設定了這個值,那麼資料為負數時,就不再設定為0,而是用原始資料乘以negative_slope
Power:f(x)= (shift + scale * x) ^ power
layer {
  name: "layer"
  bottom: "in"
  top: "out"
  type: "Power"
  power_param {
    power: 2
    scale: 1
    shift: 0
  }
}

BNLL: binomial normal log likelihood
layer {
  name: "layer"
  bottom: "in"
  top: "out"
  type: “BNLL”
}
4. 其他層:softmax_loss層,Inner Product層,accuracy層,reshape層和dropout層
(1)softmax_loss層
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

(2)全連線層,把輸入當作成一個向量,輸出也是一個簡單向量(把輸入資料blobs的width和height全變為1)。

輸入: n*c0*h*w

輸出: n*c1*1*1

全連線層實際上也是一種卷積層,只是它的卷積核大小和原資料大小一致。因此它的引數基本和卷積層的引數一樣。
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
(3)accuracy,只有測試階段才有
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}

(4)Reshape層,改變資料維度
layer {
    name: "reshape"
    type: "Reshape"
    bottom: "input"
    top: "output"
    reshape_param {
      shape {
        dim: 0  # copy the dimension from below
        dim: 2
        dim: 3
        dim: -1 # infer it from the other dimensions
      }
    }
  }

(5)Dropout層, 防止過擬合,可以隨機讓網路某些隱含層節點的權重不工作。

layer {
  name: "drop7"
  type: "Dropout"
  bottom: "fc7-conv"
  top: "fc7-conv"
  dropout_param {
    dropout_ratio: 0.5
  }