caffe使用过程+digits在windows下的安装和运行
一。模型基本組成
想要訓練一個caffe模型,需要配置兩個文件,包含兩個部分:網絡模型,參數配置,分別對應*.prototxt , ****_solver.prototxt文件。
Caffe模型文件解析:
預處理圖像的leveldb構建
輸入:一批圖像和label (2和3)?
輸出:leveldb (4)?
指令里包含如下信息:
CNN網絡配置文件
網絡模型:
DATA:一般包括訓練數據和測試數據層兩種類型。 一般指輸入層,包含source:數據路徑,批處理數據大小batch_size,scale表示數據表示在[0,1],0.00390625即 1/255
訓練數據層:
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: 64backend: LMDB} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
測試數據層:
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: 100backend: LMDB} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
CONVOLUATION:卷積層,blobs_lr:1 , blobs_lr:2分別表示weight 及bias更新時的學習率,這里權重的學習率為solver.prototxt文件中定義的學習率真,bias的學習率真是權重學習率的2倍,這樣一般會得到很好的收斂速度。
num_output表示濾波的個數,kernelsize表示濾波的大小,stride表示步長,weight_filter表示濾波的類型
layer {name: "conv1"type: "Convolution"bottom: "data"top: "conv1"param {lr_mult: 1 //weight學習率}param {lr_mult: 2 //bias學習率,一般為weight的兩倍}convolution_param {num_output: 20 //濾波器個數kernel_size: 5stride: 1 //步長weight_filler {type: "xavier"}bias_filler {type: "constant"}} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
POOLING: 池化層
layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 2 stride: 2} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
INNER_PRODUCT: 其實表示全連接,不要被名字誤導
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"}} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
RELU:激活函數,非線性變化層 max( 0 ,x ),一般與CONVOLUTION層成對出現
layer {name: "relu1"type: "ReLU"bottom: "ip1"top: "ip1" }- 1
- 2
- 3
- 4
- 5
- 6
SOFTMAX:
layer {name: "loss"type: "SoftmaxWithLoss"bottom: "ip2"bottom: "label"top: "loss" }- 1
- 2
- 3
- 4
- 5
- 6
- 7
參數配置文件:
***_solver.prototxt文件定義一些模型訓練過程中需要到的參數,比較學習率,權重衰減系數,迭代次數,使用GPU還是CPU等等.
# The train/test net protocol buffer definition net: "examples/mnist/lenet_train_test.prototxt"# test_iter specifies how many forward passes the test should carry out. # In the case of MNIST, we have test batch size 100 and 100 test iterations, # covering the full 10,000 testing images. test_iter: 100# Carry out testing every 500 training iterations. test_interval: 500# The base learning rate, momentum and the weight decay of the network. base_lr: 0.01 momentum: 0.9 weight_decay: 0.0005# The learning rate policy lr_policy: "inv" gamma: 0.0001 power: 0.75# Display every 100 iterations display: 100# The maximum number of iterations max_iter: 10000# snapshot intermediate results snapshot: 5000 snapshot_prefix: "examples/mnist/lenet"# solver mode: CPU or GPU solver_mode: GPU device_id: 0 #在cmdcaffe接口下,GPU序號從0開始,如果有一個GPU,則device_id:0- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
訓練出的模型被存為*.caffemodel,可供以后使用。?
一個完整的網絡應該是:?
步驟
- 數據準備?
準備三組數據:?
- Training Set:用于訓練網絡
- Validation Set:用于訓練時測試網絡準確率
- Test Set:用于測試網絡訓練完成后的最終正確率
- 構建lmdb/leveldb文件,caffe支持三種數據格式輸入:images, levelda, lmdb
雖然lmdb的內存消耗是leveldb的1.1倍,但是lmdb的速度比leveldb快10%至15%,更重要的是lmdb允許多種訓練模型同時讀取同一組數據集。?
因此lmdb取代了leveldb成為Caffe默認的數據集生成格式。
- 定義name.prototxt , name_solver.prototxt文件
-
訓練模型
在windows下訓練巨麻煩,要在win下使用.sh文件才行。
在windows使用.sh
安裝一波?
cygwin?
在軟件下可以安裝,如果出現package不存在的情況可以重新打開setup執行包下載,一般沒問題碰到什么問題解決什么問題。
用.bat來測試
在caffe根目錄下,新建一個create_mnist.bat,里面寫入如下的腳本。此處可能出錯,因為train-images.idx3-ubyte?在解壓的時候可能是train-images-idx3-ubyte要注意修改。
.\Build\x64\Release\convert_mnist_data.exe .\data\mnist\mnist_train_lmdb\train-images.idx3-ubyte .\data\mnist\mnist_train_lmdb\train-labels.idx1-ubyte .\examples\mnist\mnist_train_lmdb?
echo.?
.\Build\x64\Release\convert_mnist_data.exe .\data\mnist\mnist_test_lmdb\t10k-images.idx3-ubyte .\data\mnist\mnist_test_lmdb\t10k-labels.idx1-ubyte .\examples\mnist\mnist_test_lmdb?
pause?
`
然后雙擊該腳本運行,即可在E:\caffe\examples\mnist下面生成相應的lmdb數據文件。
.\Build\x64\Release\caffe.exe train –solver=.\examples\mnist\lenet_solver.prototxt?
pause
然后雙擊運行,就會開始訓練,訓練完畢后會得到相應的準確率和損失率。?
、
接下來安裝digits:
按照這里裝就好了
https://github.com/NVIDIA/DIGITS/blob/digits-5.0/docs/BuildDigitsWindows.md
最后在digits目錄下 執行python -m digits就可以了?
出現了不少bug
bug1
出現找不到pycaffe的情況,?
這種情況一般是因為python沒有導入caffe的包只需要將CAFFE_ROOT\Build\x64\Release\pycaffe\caffe文件夾復制到anaconda的sitepackages中就可以了。
bug2
出現pkg_resources._vendor.packaging.version.InvalidVersion: Invalid version: 'CAFFE_VERSION'?
找到\DIGITS-master\digits\config下的caffe.py?
按照下面的中文部分修改。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
再次運行:?
訓練:?
官方教程
完美~
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的caffe使用过程+digits在windows下的安装和运行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装Windows digits问题列表
- 下一篇: CS231n课程笔记翻译系列之目录汇总