1. 程式人生 > >Caffe入門例項與詳解 人工智慧框架

Caffe入門例項與詳解 人工智慧框架

Caffe的幾個重要檔案


用了這麼久Caffe都沒好好寫過一篇新手入門的部落格,最近應實驗室小師妹要求,打算寫一篇簡單、快熟入門的科普文。 
利用Caffe進行深度神經網路訓練第一步需要搞懂幾個重要檔案:

solver.prototxt
train_val.prototxt
train.sh


接下來我們按順序一個個說明。

solver.prototxt
solver這個檔案主要存放模型訓練所用到的一些超引數:

net := 指定待訓練模型結構檔案,即train_val.prototxt
test_interval := 測試間隔,即每隔多少次迭代進行一次測試


test_initialization := 指定是否進行初始測試,即模型未進行訓練時的測試
test_iteration := 指定測試時進行的迭代次數
base_lr := 指定基本學習率
lr_policy := 學習率變更策略,這裡有介紹,可供參考
gamma := 學習率變更策略需要用到的引數
power := 同上
stepsize := 學習率變更策略Step的變更步長(固定步長)
stepvalue := 學習率變更策略Multistep的變更步長(可變步長)
max_iter := 模型訓練的最大迭代次數
momentum := 動量,這是優化策略(Adam, SGD, … )用到的引數
momentum2 := 優化策略Adam用到的引數
weight_decay := 權重衰減率
clip_gradients := 固定梯度範圍
display := 每隔幾次迭代顯示一次結果
snapshot := 快照,每隔幾次儲存一次模型引數

snapshot_prefix := 儲存模型檔案的字首,可以是路徑
type := solver優化策略,即SGD、Adam、AdaGRAD、RMSProp、NESTROVE、ADADELTA等
solver_mode := 指定訓練模式,即GPU/CPU
debug_info := 指定是否列印除錯資訊,這裡有對啟用該功能的輸出作介紹
device_id := 指定裝置號(使用GPU模式),預設為0
使用者根據自己的情況進行相應設定,黑體引數為必須指定的,其餘引數為可選(根據情況選擇)。

train_val.prototxt


train_val檔案是用來存放模型結構的地方

 

模型的結構主要以layer為單位來構建。下面我們以LeNet為例介紹網路層的基本組成:

name: "LeNet"
layer {
  name: "mnist"                                #網路層名稱
  type: "Data"                                 #網路層型別,資料層
  top: "data"                                  #這一層的輸出,資料
  top: "label"                                 #這一層的輸出,標籤
  include {    phase: TRAIN  }                 #TRAIN:=用於訓練,TEST:=用於測試
  transform_param {    scale: 0.00390625  }    #對資料進行scale
  data_param {                                 #資料層配置 
    source: "examples/mnist/mnist_train_lmdb"  #資料存放路徑
    batch_size: 64                             #指定batch大小
    backend: LMDB                              #指定資料庫格式,LMDB/LevelDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {    phase: TEST  }
  transform_param {    scale: 0.00390625  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer{
    name:"conv1"       
    type:"Convolution" #卷積層
    bottom:"data"      #上一層的輸出作為輸入
    top:"conv1"        
    param{name:"conv1_w" lr_mult:1 decay_mult:1} #卷積層引數w的名稱,學習率和衰減率(相對於base_lr和weight_decay的倍數)
    param{name:"conv1_b" lr_mult:2 decay_mult:0} #卷積層引數b的名稱,學習率和衰減率
    convolution_param{
        num_output:20         #卷積層輸出的feature map數量 
        kernel_size:5         #卷積層的大小
        pad:0                 #卷積層的填充大小
        stride:1              #進行卷積的步長
        weight_filler{type:"xavier" }      #引數w的初始話策略
        weight_filler{type:"constant" value:0.1}     #引數b的初始化策略
    }
}
layer {        #BatchNorm層,對feature map進行批規範化處理
    name:"bn1"
    type:"BatchNorm"
    bottom:"conv1"
    top:"conv1"
    batch_norm_param{ use_global_stats:false} #訓練時為false,測試時為true
}
layer {           #池化層,即下采樣層
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX   #最大值池化,還有AVE均值池化
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {    lr_mult: 1  }
  param {    lr_mult: 2  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {      type: "xavier"    }
    bias_filler {      type: "constant"    }
  }
}
layer {
    name:"bn2"
    type:"BatchNorm"
    bottom:"conv2"
    top:"conv2"
    batch_norm_param{ use_global_stats:false}
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
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: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
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"    }
  }
}
layer {                             #損失函式層
  name: "prob"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "prob"
}

引數初始化策略可參考這裡, 啟用函式可參考這裡

網路結構和超引數都設計完了,接下來就可以進行模型訓練了。這裡我介紹最常用的模型訓練指令碼,也是Caffe官方文件給的例子。

train.sh

這個指令碼檔案可寫,可不寫。每次執行需要寫一樣的命令,所以建議寫一下。

TOOLS=/path/to/your/caffe/build/tools
GLOG_logtostderr=0 GLOG_log_dir=log/ \ #該行用於呼叫glog進行訓練日誌儲存,使用時請把該行註釋刪除,否則會出錯
$TOOLS/caffe train --solver=/path/to/your/solver.prototxt #--snapshot=/path/to/your/snapshot or --weights=/path/to/your/caffemodel ,snapshot和weights兩者只是選一,兩個引數都可以用來繼續訓練,區別在於是否儲存solver狀態

資料準備

這裡我們舉個簡單的例子,改程式碼是Caffe官方文件提供的,但只能用於單標籤的任務,多標籤得對原始碼進行修改。該指令碼是對圖片資料生成對應的lmdb檔案,博主一般使用原圖,即資料層型別用ImageData。

#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
set -e

EXAMPLE=""                            #儲存路徑
DATA=""                               #資料路徑
TOOLS=/path/to/your/caffe/build/tools #caffe所在目錄

TRAIN_DATA_ROOT=""                   #訓練資料根目錄
VAL_DATA_ROOT=""                     #測試資料根目錄
# RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=false                         #重新調整圖片大小
if $RESIZE; then
  RESIZE_HEIGHT=256
  RESIZE_WIDTH=256
else
  RESIZE_HEIGHT=0
  RESIZE_WIDTH=0
fi

#檢測路徑是否存在
if [ ! -d "$TRAIN_DATA_ROOT" ]; then
  echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
  echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet training data is stored."
  exit 1
fi

if [ ! -d "$VAL_DATA_ROOT" ]; then
  echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
  echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet validation data is stored."
  exit 1
fi

echo "Creating train lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train.txt \                #訓練圖片列表,執行時請把該行註釋刪除,否則會出錯
    $EXAMPLE/mnist_train_lmdb

echo "Creating val lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $VAL_DATA_ROOT \
    $DATA/val.txt \
    $EXAMPLE/mnist_test_lmdb

echo "Done."