1. 程式人生 > >caffe下的網路配置檔案solver.prototxt、train_val.prototxt、deploy.portotxt

caffe下的網路配置檔案solver.prototxt、train_val.prototxt、deploy.portotxt

前言:

    前面的文章介紹瞭如何搭建caffe環境以及利用caffe進行訓練model和測試圖片,下面對caffe的配置檔案進行分析一下。

一、solver.prototxt檔案

    net:"./train_val.prototxt"                   #train_val.prototxt網路配置檔案的位置    
    test_iter:2                                          #完成一次測試需要的迭代次數 test_iter * batchsize = 測試集大小
    test_interval:50                                 #每多少次測試一次
    base_lr:0.001                                     #基礎學習率
    lr_policy:"step"                                 #lr可以變化
    gamma:0.1                                       #學習率變化的比率
    stepsize:100                                     #學習率變化的頻率
    display:20                                         #每多少次展示一次
    max_iter:50000                                #迭代次數
    momentum:0.9                                #上一次梯度更新的權重
    weight_decay:0.0005                       #權重衰減項,防止過擬合的一個引數
    snapshot:2500                                 #訓練多少次儲存映象

    snapshot_prefix:"../../data/model/alex/alex"  #儲存的路徑

    solver_mode:GPU                            #選擇GPU還是CPU

二、train_val.prototxt訓練網路配置檔案

    name: "AlexNet"                      #框架的名字
    layer {
        name: "data"                        #該層的名字
        type: "Data"                         #該層的型別
        top: "data"                           #top/bottom botton用來輸入資料,top用來輸出資料
        top: "label"                          #多個top/botto表示多個數據的輸入或輸出
        include {                              #具有include引數,該層會表示是訓練或者測試,如果沒有表示該層即在測試又在訓練模型中
            phase: TRAIN                  #表示是訓練
         }
        transform_param {
            mirror: true                     #true表示開啟映象,false表示關閉映象
            crop_size: 227                 #剪裁一個227*227的圖塊
            mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"   #訓練的均值檔案
        }
        data_param {
            source: "examples/imagenet/ilsvrc12_train_lmdb"        #訓練的圖片轉換成的lmdb資料格式
            batch_size: 256                        #每次處理資料的個數
            backend: LMDB                       #資料的格式
       }
    }
    layer {
        name: "data"                                          
        type: "Data"
        top: "data"                         #如果有兩個top一般一個是data一個是label
        top: "label"
        include {
            phase: TEST                   #表示測試
         }
        transform_param {
            mirror: false                   #測試不生成映象
            crop_size: 227                #裁剪的圖片大小
            mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"   #測試的均值檔案
        }
        data_param {
            source: "examples/imagenet/ilsvrc12_val_lmdb"          #測試的圖片轉換成的lmdb資料格式
            batch_size: 50           #每次測試的圖片個數 如果顯示卡或記憶體不是很強 可以將每次測試的圖片數減小,測試的次數增多,測試的次數在solver.prototxt中
            backend: LMDB          #資料的格式
         }
    }
    layer {
        name: "conv1"                   #第一卷積層
        type: "Convolution"           #卷積
        bottom: "data"                  #輸入的資料是data層
        top: "conv1"                      #輸出是conv1
        param {
            lr_mult: 1                       #權值的學習率係數,學習率是solver.prototxt中的base_lr * lr_mult
            decay_mult: 1                #權值的衰減
        }
        param {
            lr_mult: 2                       #偏置的學習率係數
            decay_mult: 0#權值的衰減
        }
        convolution_param {        #卷積的引數
            num_output: 96           #卷積核的個數
            kernel_size: 11              #卷積核大小 11*11  如果卷積核的長和寬不等,需要用 kernel_h 和 kernel_w 分別設定
            stride: 4                         #卷積核的步長
            weight_filler {                #權值的初始化
                type: "gaussian"        #很多時候用xavier演算法來初始化,也可以用Gaussian 高斯分佈來初始化
                std: 0.01                     #標準差
            }
            bias_filler {                      #偏置的初始化
                type: "constant"          #預設constant固定值
                value: 0                        #固定值多少
            }
      }
}
layer {
  name: "relu1"                            #啟用層
  type: "ReLU"                              #啟用函式relu
  bottom: "conv1"                        #輸入是conv1
  top: "conv1"                               #輸出是conv1
}
layer {
  name: "norm1"                          #對輸入的區域性區域進行歸一化,達到側抑制的效果
  type: "LRN"
  bottom: "conv1"                        #輸入conv1
  top: "norm1"                              #輸出norml
  lrn_param {
    local_size: 5         #預設5 如果是跨通道LRN,則表示求和的通道數;如果是在通道內LRN,則表示求和的正方形區域長度。 
    alpha: 0.0001                            #預設1 歸一化公式中的引數
    beta: 0.75                                  #歸一化公式中的引數
  }
}
layer {                                                 
    name: "pool1"                   #第一池化層pool1
    type: "Pooling"                  #池化操作
    bottom: "norm1"               #該層的輸入是norml
    top: "pool1"                       #該層的輸出是pool1
    pooling_param {                #池化層引數
        pool: MAX                     #池化方法 預設是MAX即池化核內的最大值。目前可用的方法有MAX, AVE, 或STOCHASTIC
        kernel_size: 3                 #池化核的大小
        stride: 2                          #池化的步長
#pad: 2                           #池化的邊緣擴充和卷積層一樣
      }
}
layer {
    name: "conv2"                   #第二卷積層
    type: "Convolution"
    bottom: "pool1"
    top: "conv2"
    param {
        lr_mult: 1
        decay_mult: 1
    }
    param {
        lr_mult: 2
        decay_mult: 0
    }
    convolution_param {
        num_output: 256                       #卷積核的個數變成256
        pad: 2                                         #邊沿擴充是2 也就是長寬都增加4個畫素點
        kernel_size: 5                              #卷積核5*5
        group: 2                                      #分組 預設為1 卷積的分組可以減少網路的引數
        weight_filler {                              #權值引數設定
            type: "gaussian"                      #高斯分佈
            std: 0.01                                   #標準差是0.01
        }
        bias_filler {                                    #偏置引數設定
            type: "constant"                        #固定值
            value: 0.1                                   #值0.1
       }
   }
}
layer {
    name: "relu2"                                    #啟用層
    type: "ReLU"
    bottom: "conv2"
    top: "conv2"
}
layer {
    name: "norm2"                                 #歸一化
    type: "LRN"
    bottom: "conv2"
    top: "norm2"
    lrn_param {
        local_size: 5
        alpha: 0.0001
        beta: 0.75
    }
}
layer {                                                 
    name: "pool2"                                 #第二池化層
    type: "Pooling"
    bottom: "norm2"
    top: "pool2"
    pooling_param {
        pool: MAX
        kernel_size: 3                                #卷積核大小 3*3
        stride: 2                                         #步長 2
    }
}
layer {
    name: "conv3"
    type: "Convolution"
    bottom: "pool2"
    top: "conv3"
    param {
        lr_mult: 1
        decay_mult: 1
    }
    param {
        lr_mult: 2
        decay_mult: 0
    }
    convolution_param {
        num_output: 384
        pad: 1
        kernel_size: 3
        weight_filler {
            type: "gaussian"
            std: 0.01
        }
        bias_filler {
            type: "constant"
            value: 0
        }
    }
}
layer {
    name: "relu3"
    type: "ReLU"
    bottom: "conv3"
    top: "conv3"*
}
layer {
    name: "conv4"
    type: "Convolution"
    bottom: "conv3"
    top: "conv4"
    param {
        lr_mult: 1
        decay_mult: 1
    }
    param {
        lr_mult: 2
        decay_mult: 0
    }
   convolution_param {
       num_output: 384
       pad: 1
       kernel_size: 3
       group: 2
       weight_filler {
           type: "gaussian"
           std: 0.01
       }
       bias_filler {
           type: "constant"
           value: 0.1
       }
    }
}
layer {
    name: "relu4"
    type: "ReLU"
    bottom: "conv4"
    top: "conv4"
}
layer {
    name: "conv5"
    type: "Convolution"
    bottom: "conv4"
    top: "conv5"
    param {
        lr_mult: 1
        decay_mult: 1
    }
    param {
        lr_mult: 2
        decay_mult: 0
    }
    convolution_param {
        num_output: 256
        pad: 1
        kernel_size: 3
        group: 2
        weight_filler {
            type: "gaussian"
            std: 0.01
         }
         bias_filler {
             type: "constant"
            value: 0.1
        }
    }
}
layer {
    name: "relu5"
    type: "ReLU"
    bottom: "conv5"
    top: "conv5"
}
layer {
    name: "pool5"
    type: "Pooling"
    bottom: "conv5"
    top: "pool5"
    pooling_param {
        pool: MAX
        kernel_size: 3
        stride: 2
    }
}
layer {
    name: "fc6"                              #全連線層
    type: "InnerProduct"              #全連線層實際上也是一種卷積層,只是它的卷積核大小和原資料大小一致。因此它的引數基本和卷積層的引數一樣
    bottom: "pool5"                      #輸入是pool5
    top: "fc6"                                 #輸出是fc6
    param { 
        lr_mult: 1                             #權值學習率係數
        decay_mult: 1                      #權值衰減
    }
    param {
        lr_mult: 2                             #偏置的學習率係數
        decay_mult: 0                      #偏置衰減
    }
    inner_product_param {
        num_output: 4096               #輸出的個數
        weight_filler {
            type: "gaussian"              #高斯分佈
            std: 0.005                         #標準差
        }
        bias_filler { 
            type: "constant"
            value: 0.1                         #偏置固定值0.1
        }
    }
}
layer {
    name: "relu6"
    type: "ReLU"
    bottom: "fc6"
    top: "fc6"
}
layer {
    name: "drop6"                             #對於神經網路單元,按照一定的概率將其暫時從網路中丟棄
    type: "Dropout"                           #防止過擬合
    bottom: "fc6"                               #該層輸入fc6
    top: "fc6"                                     #該層輸出fc6
    dropout_param {
        dropout_ratio: 0.5                    #dropout 的概率
    }
}
layer {
    name: "fc7"
    type: "InnerProduct"
    bottom: "fc6"
    top: "fc7"
    param {
        lr_mult: 1
        decay_mult: 1
    }
    param {
        lr_mult: 2
        decay_mult: 0
    }
    inner_product_param {
        num_output: 4096
        weight_filler {
            type: "gaussian"
            std: 0.005
        }
        bias_filler {
            type: "constant"
            value: 0.1
        }
    }
}
layer {
    name: "relu7"
    type: "ReLU"
    bottom: "fc7"
    top: "fc7"
}
layer {
    name: "drop7"
    type: "Dropout"
    bottom: "fc7"
    top: "fc7"
    dropout_param {
        dropout_ratio: 0.5
    }
}
layer {
    name: "fc8"
    type: "InnerProduct"
    bottom: "fc7"
    top: "fc8"
    param {
        lr_mult: 1
        decay_mult: 1
    }
    param {
        lr_mult: 2
        decay_mult: 0
    }
    inner_product_param {
        num_output: 1000
        weight_filler {
            type: "gaussian"
            std: 0.01
        }
        bias_filler {
            type: "constant"
            value: 0
        }
    }
}
layer {             #caffe中計算Accuracy時,是通過比較最後一個全連線層(神經元個數=類別數、但沒有加入activation function)的輸出和資料集的labels來得到的,計算過程在AccuracyLayer中實現
    name: "accuracy"                    #正確率層
    type: "Accuracy"                     #利用fc8的輸出得到資料集的預測labels
    bottom: "fc8"#最後一層全連線作為輸入
    bottom: "label"                       #資料層的lable作為另一個輸入
    top: "accuracy"                       #輸出正確率
    include {
        phase: TEST                         #測試的正確率
    }
}
layer {
    name: "loss"                            #丟失率
    type: "SoftmaxWithLoss"
    bottom: "fc8"
    bottom: "label"
    top: "loss"

}

三、deploy.prototxt測試網路配置檔案

name: "AlexNet"
layer {
    name: "data"
    type: "Input"
    top: "data"
    #輸入資料的batch size, channel, width,height
    input_param { shape: { dim: 10 dim: 3 dim: 227 dim: 227 } }

}

...

...

layer {
    name: "prob"
    #定義的分類器
    type: "Softmax"
    bottom: "fc8"
    top: "prob"

}

注意:

首先deploy.prototxt檔案都是在train_val.prototxt檔案的基礎上刪除了一些東西所形成的。
由於兩個檔案的性質,train_val.prototxt檔案裡面訓練的部分都會在deploy.prototxt檔案中刪除。