了解 caffe 之前必須先懂得 這四個東西之間的關係

四個物件是一層包一層的

Blob,Layer,Net,Solve

 

Blob:是基礎的數據結構,是用來保存學習到的參數以及網絡傳輸過程中產生數據 的類

Layer:是網絡的基本單元,由此派生出了各種層類。修改這部分的人主要是研究特徵表達方向的。

Net:是網絡的搭建,將 Layer 所派生出層類組合成網絡

Solver:是 Net 的求解, 修改這部分人主要會是研究 DL 求解方向的。

 

通常您有數據 如圖片等資料時

必須把它從 RGB 矩陣模式 轉成Blob 的數據結構

然後再輸進去 layer中做訓練

而很多layer  可以組成 一個網路 例如一個autoencoder

最後我們會寫一個solver 來訓練這網路 告訴它 weight 該怎麼改....bla bla 的~

 

相信大家還是很模糊  那就跟著小編一步一步的操作吧~

以下所講的都是來自官網資訊  :   http://caffe.berkeleyvision.org/

大家有不懂的可以查詢

 

 

首先我們先來學如何創造數據吧

 

1.創造數據庫

移動到caffe 目錄下 執行以下指令

cd $CAFFE_ROOT

執行完後會出現如下圖四個檔案為mnist 壓縮過後的圖檔 以及laybel
./data/mnist/get_mnist.sh

 

再執行已下指令將 image data 轉成 blob 型式

執行完會出現兩個資料夾 分別是訓練資料以及測試資料


./examples/mnist/create_mnist.sh

 

 

2.訓練數據

cd $CAFFE_ROOT
./examples/mnist/train_lenet.sh

跑完產生檔案:     lenet_iter_10000.caffemodel      lenet_iter_10000.solverstate

第一個檔案為caffe 訓練完的model 包含 weight ,bias 等資訊

 

第二個檔案可以為後續訓練之用 假如以後想再訓練到20000 iteration 的話 就可以使用此檔

範例如下:

# resume training from the half-way point snapshot
caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solverstate

 

 

3.測試數據

當所有數據都訓練好之後,接下來就是如何將模型應用到實際數據了:

cd $CAFFE_ROOT

實際上就只是運行caffe.bin 這個檔案 後面只是運行的參數

橘色代表運行模式 綠色輸入要解決的網路 紅色輸入剛剛訓練出來的weight

./build/tools/caffe test -model=examples/mnist/lenet.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel -gpu=0

如果沒有GPU則使用

./build/tools/caffe test -model=examples/mnist/lenet.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel

4.調用數據

將train 的 網路改成可以輸出的網路 mnist_autoencoder_half

因為我們不需要訓練網路了 所以需要把和data 相關的 layer 都刪掉

做成一個適合當 deploy 的網路

詳細方法可參考官網:

https://github.com/BVLC/caffe/wiki/Using-a-Trained-Network:-Deploy

或者我自己做的教學

 

 

底下是用python 寫出調用數據的範例:

 

import caffe
import numpy as np

np.set_printoptions(threshold='nan')

#將剛剛改好的網路丟入
MODEL_FILE = '
mnist_autoencoder_half.prototxt'

#將訓練好的weights 丟入
PRETRAIN_FILE = 'mnist_autoencoder_iter_65000.caffemodel'

#將上面兩組資料丟入Net
net = caffe.Net(MODEL_FILE, PRETRAIN_FILE, caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_raw_scale('data', 255)
net.blobs['data'].reshape(1, 1, 28, 28)

#將要測試的圖片丟入 出來的資料為28,28,1三維矩陣
img = caffe.io.load_image('cat_28_28.jpg', color=False)

print "Show the size of image:\n",img.shape,"\n"

#將矩陣資料轉為Blob 型式
net.blobs['data'].data[...] = transformer.preprocess('data', img)

#讓網路執行向前傳遞法 出來的資料型態為blob
out = net.forward()

#底下示範取出資料方法
print "The type of the data come from net is Blob:\n",out,"\n"
print "We need to convert it to matrix that we can use:\n",
out[net.outputs[0]],"\n"

#藍色部分可以改為要取出的layer 名稱
print "This is second method that can extract any data from any layer:\n",
net.blobs['encode4neuron'].data,"\n"

 

5.Code 架構理解:

每個layer 內的大概架構說明

layer {
  name: "conv1" # 名稱:conv1
  type: "Convolution" # 類型:卷積層
  bottom: "data" # 輸入層:數據層
  top: "conv1" # 輸出層:卷積層1
  # 濾波器(filters)的學習速率因子和衰減因子
  param { lr_mult: 1 decay_mult: 1 }
  # 偏置項(biases)的學習速率因子和衰減因子
  param { lr_mult: 2 decay_mult: 0 }
  convolution_param {
    num_output: 96 # 96個濾波器(filters)
    kernel_size: 11 # 每個濾波器(filters)大小為11*11
    stride: 4 # 每次濾波間隔為4個像素
    weight_filler {
      type: "gaussian" # 初始化高斯濾波器(Gaussian)
      std: 0.01 # 標準差為0.01, 均值默認為0
    }
    bias_filler {
      type: "constant" # 初始化偏置項(bias)為零
      value: 0
    }
  }
}

 

 

分析上面的訓練步驟 共有三個檔案:

一個為:lenet.prototxt    

描述網路結構

 

一個為:lenet_solver.prototxt          

描述用何方法求解上述網路結構 檔案內必須包含 lenet_train_test.prototxt

 

一個為:train_lenet.sh                      

調用caffe 訓練網路   檔案內必須包含 lenet_solver.prototxt

 

仔細分析完之後是一個檔案包一個, 先寫網路檔出來

然後再寫網路檔的solver

最後再調用caffe 其實我覺得不用最後一個檔案也可以

因為裡面就只有一句話

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt

就是調用caffe 做訓練模式阿@@  炯

 

 

 

底下列出三個檔案的內容:

1.lenet.prototxt 檔

name: "LeNet"

******************************************************1

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

 }

}

*******************************************************2

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

 }

}

*********************************************************3

layer {

 name: "conv1"

 type: "Convolution"

 bottom: "data"

 top: "conv1"

 param {

   lr_mult: 1

 }

 param {

   lr_mult: 2

 }

 convolution_param {

   num_output: 20

   kernel_size: 5

   stride: 1

   weight_filler {

     type: "xavier"

   }

   bias_filler {

     type: "constant"

   }

 }

}

**********************************************************4

layer {

 name: "pool1"

 type: "Pooling"

 bottom: "conv1"

 top: "pool1"

 pooling_param {

   pool: MAX

   kernel_size: 2

   stride: 2

 }

}

**********************************************************5

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"

   }

 }

}

***********************************************************6

layer {

 name: "pool2"

 type: "Pooling"

 bottom: "conv2"

 top: "pool2"

 pooling_param {

   pool: MAX

   kernel_size: 2

   stride: 2

 }

}

***********************************************************7

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"

   }

 }

}

************************************************************8

layer {

 name: "relu1"

 type: "ReLU"

 bottom: "ip1"

 top: "ip1"

}

************************************************************9

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"

   }

 }

}

***********************************************************10

layer {

 name: "accuracy"

 type: "Accuracy"

 bottom: "ip2"

 bottom: "label"

 top: "accuracy"

 include {

   phase: TEST

 }

}

************************************************************11

layer {

 name: "loss"

 type: "SoftmaxWithLoss"

 bottom: "ip2"

 bottom: "label"

 top: "loss"

}

 

 

 

2.lenet_solver.prototxt 檔

# The train/test net protocol buffer definition

net: "examples/mnist/lenet.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: CPU

3.train_lenet.sh

作用:用裡面的內建程式 caffe 執行 lenet_solver.prototxt 檔案

 

內容:

#!/usr/bin/env sh

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt



 

 

 

 

 

arrow
arrow

    Darwin的AI天地 發表在 痞客邦 留言(2) 人氣()