【Caffe篇】--Caffe從入門到初始及各層介紹


一、前述

Caffe,全稱Convolutional Architecture for Fast Feature Embedding。是一種常用的深度學習框架,主要應用在視頻、圖像處理方面的應用上。caffe是一個清晰,可讀性高,快速的深度學習框架。作者是賈揚清,加州大學伯克利的ph.D,現就職於Facebook。caffe的官網是http://caffe.berkeleyvision.org/。

 二、具體

1、輸入層

layer { name: "cifar" type: "Data" top: "data"  #一般用bottom表示輸入,top表示輸出,多個top代表有多個輸出
  top: "label" include { phase: TRAIN #訓練網絡分為訓練階段和自測試階段,如果沒寫include則表示該層即在測試中,又在訓練中
 } transform_param { mean_file: "examples/cifar10/mean.binaryproto" #用一個配置文件來進行均值的操作
 transform_param { scale: 0.00390625 mirror: 1  # 1表示開啟鏡像,0表示關閉,也可用ture和false來表示
    # 剪裁一個 227*227的圖塊,在訓練階段隨機剪裁,在測試階段從中間裁剪
    crop_size: 227 } } data_param { source: "examples/cifar10/cifar10_train_lmdb" #數據庫來源
    batch_size: 64 #每次批處理的個數
    backend: LMDB #選用數據的名稱
 } } ### 使用LMDB源
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 } } ###使用HDF5數據源
layer { name: "data" type: "HDF5Data" top: "data" top: "label" hdf5_data_param { source: "examples/hdf5_classification/data/train.txt" batch_size: 10 } } ###數據直接來源與圖片 #/path/to/images/img3423.jpg 2  #/path/to/images/img3424.jpg 13  #/path/to/images/img3425.jpg 8
 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 #如果設置就對圖片進行resize操作
    new_width: 256 } }

 2、卷積層

 

layer { name: "conv1" #定義一個名字 必須指定的 type: "Convolution" bottom: "data"#前面連接的層 data層 top: "conv1"#輸出是卷積層 param { lr_mult: 1  #lr_mult: #當前層的學習率 學習率的系數,最終的學習率是這個數乘以solver.prototxt配置文件中的base_lr。如果有兩個lr_mult, 則第一個表示權值的學習率,第二個表示偏置項的學習率。一般偏置項的學習率是權值學習率的兩倍
 } param { lr_mult: 2 } convolution_param { num_output: 20 #卷積核(filter)的個數等於特征圖的個數
    kernel_size: 5 #卷積核的大小 5*5*d 中的d是上一層的深度 
    stride: 1 #卷積核的步長,默認為1 
    pad: 0 #擴充邊緣,默認為0,不擴充
 weight_filler { type: "xavier" #權值初始化。 默認為“constant",值全為0,很多時候我們用"xavier"算法來進行初始化,也可以設置為”gaussian"
 } bias_filler { type: "constant" #偏置項的初始化。一般設置為"constant",值全為0
 } } } 輸入:n*c0*w0*h0 輸出:n*c1*w1*h1 其中,c1就是參數中的num_output,生成的特征圖個數 w1=(w0+2*pad-kernel_size)/stride+1; h1=(h0+2*pad-kernel_size)/stride+1;

結論:

假設輸入時h*w k是kernel_size p 是padding s是stride則
特征圖 的輸出的h是多大的 (h-k+2p)/s+1
w是(w-k+2p)/s+1

3、池化層

layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX #池化方法,默認為MAX。目前可用的方法有MAX, AVE
    kernel_size: 3 #池化的核大小
    stride: 2 #池化的步長,默認為1。一般我們設置為2,即不重疊。
 } } #pooling層的運算方法基本是和卷積層是一樣的。

 4、激活函數層

#在激活層中,對輸入數據進行激活操作,是逐元素進行運算的,在運算過程中,沒有改變數據的大小,即輸入和輸出的數據大小是相等的。

###Sigmoid
 layer { name: "test" bottom: "conv" top: "test" type: "Sigmoid" } #ReLU是目前使用最多的激活函數,主要因為其收斂更快,並且能保持同樣效果。標准的ReLU函數為max(x, 0),當x>0時,輸出x; 當x<=0時,輸出0
f(x)=max(x,0) layer { name: "relu1" type: "ReLU" bottom: "pool1" top: "pool1" }

 5、全連接層

#全連接層,輸出的是一個簡單向量 參數跟卷積層一樣
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: "accuracy" type: "Accuracy" bottom: "ip2"#兩個輸入一個輸入是分類結果 bottom: "label"#另一個輸入是label top: "accuracy" include { phase: TEST } }

 6、softmax_layer

#softmax-loss layer:輸出loss值 對於softmax 得到損失函數 -logp p為正確的分類的概率
layer { name: "loss" type: "SoftmaxWithLoss" bottom: "ip1" bottom: "label" top: "loss" } #softmax layer: 輸出似然值 得到每一個類別的概率值
layers { bottom: "cls3_fc" top: "prob" name: "prob" type: “Softmax" }

 7、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
 } } } 有一個可選的參數組shape, 用於指定blob數據的各維的值(blob是一個四維的數據:n*c*w*h)。 dim:0 表示維度不變,即輸入和輸出是相同的維度。 dim:2 或 dim:3 將原來的維度變成2或3 dim:-1 表示由系統自動計算維度。數據的總量不變,系統會根據blob數據的其它三維來自動計算當前維的維度值 。 假設原數據為:32*3*28*28, 表示32張3通道的28*28的彩色圖片 shape { dim: 0 #表示不變 dim: 0 dim: 14 dim: -1 #表示自動推斷 } 輸出數據為:32*3*14*56

#Dropout是一個防止過擬合的層 #只需要設置一個dropout_ratio就可以了。
layer { name: "drop7" type: "Dropout" bottom: "fc7-conv" top: "fc7-conv" dropout_param { dropout_ratio: 0.5 } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM