深度學習之Caffe框架及製作數據源

caffe框架:
結構:
Blob:stores data and derivatives
Layer: transform bottom blobs to top blobs
Net:Many layers;computes gradients via forward/backward
Solver:Uses gradients to updata weights
流程:
no need to write code
1.convert data (run a script) 數據轉換:create LMDB using convert_imageset
2.define net (edit prototxt) 定義網絡
3.define solver(edit prototxt)) 定義配置參數
4.train (run a script)
學習資源:Model Zoo
#定義層 例

name:""
layer{
   name:""
    trandform_param{
    scale:0.03}
}

layer{
  name:"conv1"
  type:"Convolution"
  bottom:"data"
  top:"conv1"
  param{
      lr_mult:1
  }
  convolution_param{
      num_output:20
      kernel_size:5
      stride:1  
      weight_filler{
          type:"xavier"   #權重初始化方式
      }
      bias_filler{
          type:"constant"
      }
     }
}
layer{
    name:"pool1"
    type:"Pooling"
    bottom:"conv1"
    top:"pool1"
    pooling_param{
        pool:MAX
        kernel_size:2
        stride:2
        }
}

#訓練 例

./bulid/tools/caffe train \
  -gpu 0 \
  -model path/to/trainval.prototxt \
  -solver
  -weights

#卷積神經網絡各層示例

#數據層
layer{
    name:" "   #網絡名稱
    type="Data"
    top="data" #用bottom表示輸入,top表示輸出,多個top代表多個輸出
    top="label"
    include{
        phase:TRAIN #訓練網絡分爲訓練階段和自測試階段,如果沒寫include則表示該層既在測試層又在訓練層
    }
    transform_param{
        mean_file:"examples/cifar10/mean.binaryproto" #用一個配置文件進行均值的操作 R,G,B三通道
        transform_param{
        scale:0.0039     #1/255   (0-255)壓縮到(0,1)
        mirror:1   #1表示開啓鏡像 0表示關閉  圖像翻轉
        #裁剪一個227*224的圖塊,在訓練階段隨機裁剪,在測試階段從中間裁剪
        crop_size : 227
    }    
    }
    data_param{
        source:"examples/cifar10/train_lmb"   #數據庫來源
        batch_size:100 #每次批處理的個數
        backend:LMDB #選用數據的名稱
    }
}
#卷積層
layer{
    name:"conv1"
    type:"Convolution"
    bottom:"data"
    top:"conv1"
    param{
        lr_mult:1
    }
    param{
        lr_mult:2        
    }
    convolution_param{
        num_output:20 #卷積核(filter)的個數
        kernael_size:5#卷積核的大小
        stride:1 #卷積核的步長,默認爲1
        pad:0 #擴充邊緣,默認爲0,不擴充
        weight_filler{
            type:"xavier"#權值初始化。 默認爲"constant", 值全爲0.
        }
        bias_filler{
            type:"constant" #偏置項的初始化。一般設置爲"constant",值全爲0
        }
    }
}
輸入: n*c0*w0*h0
輸出: n*c1*w1*h1

#池化層
layer{
    name:"pool1"
    type:"Pooling"
    bottom:"conv1"
    top:"pool1"
    pooling_param{
        pool:MAX #池化方法 目前可用的方法有max,ave
        kernel_size:3#池化的核大小
        stride:2 #池化的步長,默認爲1。
    }
}

#激活函數
layer{
    name:"test"
    bottom:"conv"
    top:"test"
    type:"Sigmoid"
}
#Relu是目前使用最多的激活函數,主要因爲其收斂快,並能保持同樣效果
f(x)=max(x,0)
layer{
    name:"relu1"
    type:"ReLU"
    bottom:"pool1"
    top:"pool1"
}
#全連接層
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_fiiler{
             type:"constant"
         }
    }    
}
#測試準確率
layer{
    name:"accuracy"
    type:"Accuracy"
    bottom:"ip2"
    bottom:"label"
    top:"accuracy"
    include{
        phase:TEST
    }
}
#輸出loss值
layer{
    name:"loss"
    type:"SoftmaxWithLoss"
    bottom:"ip1"
    bottom:"label"
    top:"loss"
}
#輸出似然值
layer{
    bottom:"cls3_fc"
    top:"prob"
    name:"prob"
    type:"Softmax"
}
#改變數據維度
layer{
    name:"reshape"
    type:"Reshape"
    bottom:"input"
    top:"output"
    reshape_param{
        shape{
            dim:0        #維度不變
            dim:x         #維度變爲x
            dim:-1       #維度機器去猜
         }
    }
}
#dropout層
layer{
    name:"drop7"
    type:"Dropout"
    bottom:"fc7-conv"
    top:""
    dropout_param{
        dropout_ratio:0.5
    }
}

#往往loss function是非凸的
#caffe提供了六種優化算法來求解最優參數
Stochastic Gradient Descent(type: “SGD”)
AdaDelta(type:“AdaDelta”)
Adaptive Gradient(type:“AdaGrad”)
Adam(type:“Adam”)
Nesterov’s Accelerated Gradient(type:“Nesterov”)
RMSprop(type:“RMSProp”)

net:" .prototxt"   #網絡位置
test_iter:100   #一次迭代多少樣本 batchsize * test_iter = 測試總量
test_interval:500   #測試間隔
base_lr:0.01    #設置基礎學習率
lr_policy:"inv"   #學習率調整的策略
       -fixed:保持base_lr不變
       -step: 如果設置爲step,則還需要設置一個stepsize
       -exp: 
       -inv:
momentum:0.9  #動量
display:100 #每訓練100次,在屏幕上顯示一次。
max_iter:20000   #最大迭代次數
snapshot:5000 #快照。將訓練出來的model和solver狀態進行保存.
solver_mode:CPU  #設置運行模式,默認爲gpu

製作數據源

#製作LMDB數據源
EXAMPLE =     #新建文件夾位置
DATA=
TOOLS=        #caffe安裝的路徑
TRAIN_DATA_ROOT = 
VAL_DATA_ROOT =
train.txt                             #從子文件夾開始寫
val.txt                         #直接寫文件名稱加label
製作:  sh face-lmdb.sh(cmd窗口下找到當前文件夾)
訓練:  sh train.sh

label      #迴歸任務 找關鍵點 座標(),(),()
##使用HDF5數據源  多label情況
layer{
    name:"data"
    type:"HDF5Data"
    top:"data"
    top:"label"
    hdf5_data_param{
        source:"examples/hdf5_classification/data/train.txt"
        batch_size:10
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章