1. 程式人生 > >Caffe 深度學習框架上手教程

Caffe 深度學習框架上手教程

att per share caff 但是 fill ... post 高效

Caffe 深度學習框架上手教程

技術分享 blink 15年1月

Caffe (CNN, deep learning) 介紹

Caffe -----------Convolution Architecture For Feature Embedding (Extraction)

  1. Caffe 是什麽東東?
    • CNN (Deep Learning) 工具箱
    • C++ 語言架構
    • CPU 和GPU 無縫交換
    • Python 和matlab的封裝
    • 但是,Decaf只是CPU 版本。
  2. 為什麽要用Caffe?

    • 運算速度快。簡單 友好的架構 用到的一些庫:
    • Google Logging library (Glog): 一個C++語言的應用級日誌記錄框架,提供了C++風格的流操作和各種助手宏.
    • lebeldb(數據存儲): 是一個google實現的非常高效的kv數據庫,單進程操作。
    • CBLAS library(CPU版本的矩陣操作)
    • CUBLAS library (GPU 版本的矩陣操作)
  3. Caffe 架構

技術分享

  1. 預處理圖像的leveldb構建
    輸入:一批圖像和label (2和3)
    輸出:leveldb (4)
    指令裏包含如下信息:
    • conver_imageset (構建leveldb的可運行程序)
    • train/ (此目錄放處理的jpg或者其他格式的圖像)
    • label.txt (圖像文件名及其label信息)
    • 輸出的leveldb文件夾的名字
    • CPU/GPU (指定是在cpu上還是在gpu上運行code)
  2. CNN網絡配置文件

    • Imagenet_solver.prototxt (包含全局參數的配置的文件)
    • Imagenet.prototxt (包含訓練網絡的配置的文件)
    • Imagenet_val.prototxt (包含測試網絡的配置文件)
技術分享 blink 15年1月

Caffe深度學習之圖像分類模型AlexNet解讀

在imagenet上的圖像分類challenge上Alex提出的alexnet網絡結構模型贏得了2012屆的冠軍。要研究CNN類型DL網絡模型在圖像分類上的應用,就逃不開研究alexnet,這是CNN在圖像分類上的經典模型(DL火起來之後)。

在DL開源實現caffe的model樣例中,它也給出了alexnet的復現,具體網絡配置文件如下train_val.prototxt452

接下來本文將一步步對該網絡配置結構中各個層進行詳細的解讀(訓練階段):

  1. conv1階段DFD(data flow diagram):
    技術分享

  2. conv2階段DFD(data flow diagram):
    技術分享

  3. conv3階段DFD(data flow diagram):

    技術分享 SouthEast2455x274

  4. conv4階段DFD(data flow diagram):
    技術分享

  5. conv5階段DFD(data flow diagram):
    技術分享

  6. fc6階段DFD(data flow diagram):
    技術分享

  7. fc7階段DFD(data flow diagram):
    技術分享

  8. fc8階段DFD(data flow diagram):
    技術分享

各種layer的operation更多解釋可以參考Caffe Layer Catalogue356

從計算該模型的數據流過程中,該模型參數大概5kw+。

caffe的輸出中也有包含這塊的內容日誌,詳情如下:

I0721 10:38:15.326920  4692 net.cpp:125] Top shape: 256 3 227 227 (39574272)
I0721 10:38:15.326971  4692 net.cpp:125] Top shape: 256 1 1 1 (256)
I0721 10:38:15.326982  4692 net.cpp:156] data does not need backward computation.
I0721 10:38:15.327003  4692 net.cpp:74] Creating Layer conv1
I0721 10:38:15.327011  4692 net.cpp:84] conv1 <- data
I0721 10:38:15.327033  4692 net.cpp:110] conv1 -> conv1
I0721 10:38:16.721956  4692 net.cpp:125] Top shape: 256 96 55 55 (74342400)
I0721 10:38:16.722030  4692 net.cpp:151] conv1 needs backward computation.
I0721 10:38:16.722059  4692 net.cpp:74] Creating Layer relu1
I0721 10:38:16.722070  4692 net.cpp:84] relu1 <- conv1
I0721 10:38:16.722082  4692 net.cpp:98] relu1 -> conv1 (in-place)
I0721 10:38:16.722096  4692 net.cpp:125] Top shape: 256 96 55 55 (74342400)
I0721 10:38:16.722105  4692 net.cpp:151] relu1 needs backward computation.
I0721 10:38:16.722116  4692 net.cpp:74] Creating Layer pool1
I0721 10:38:16.722125  4692 net.cpp:84] pool1 <- conv1
I0721 10:38:16.722133  4692 net.cpp:110] pool1 -> pool1
I0721 10:38:16.722167  4692 net.cpp:125] Top shape: 256 96 27 27 (17915904)
I0721 10:38:16.722187  4692 net.cpp:151] pool1 needs backward computation.
I0721 10:38:16.722205  4692 net.cpp:74] Creating Layer norm1
I0721 10:38:16.722221  4692 net.cpp:84] norm1 <- pool1
I0721 10:38:16.722234  4692 net.cpp:110] norm1 -> norm1
I0721 10:38:16.722251  4692 net.cpp:125] Top shape: 256 96 27 27 (17915904)
I0721 10:38:16.722260  4692 net.cpp:151] norm1 needs backward computation.
I0721 10:38:16.722272  4692 net.cpp:74] Creating Layer conv2
I0721 10:38:16.722280  4692 net.cpp:84] conv2 <- norm1
I0721 10:38:16.722290  4692 net.cpp:110] conv2 -> conv2
I0721 10:38:16.725225  4692 net.cpp:125] Top shape: 256 256 27 27 (47775744)
I0721 10:38:16.725242  4692 net.cpp:151] conv2 needs backward computation.
I0721 10:38:16.725253  4692 net.cpp:74] Creating Layer relu2
I0721 10:38:16.725261  4692 net.cpp:84] relu2 <- conv2
I0721 10:38:16.725270  4692 net.cpp:98] relu2 -> conv2 (in-place)
I0721 10:38:16.725280  4692 net.cpp:125] Top shape: 256 256 27 27 (47775744)
I0721 10:38:16.725288  4692 net.cpp:151] relu2 needs backward computation.
I0721 10:38:16.725298  4692 net.cpp:74] Creating Layer pool2
I0721 10:38:16.725307  4692 net.cpp:84] pool2 <- conv2
I0721 10:38:16.725317  4692 net.cpp:110] pool2 -> pool2
I0721 10:38:16.725329  4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.725338  4692 net.cpp:151] pool2 needs backward computation.
I0721 10:38:16.725358  4692 net.cpp:74] Creating Layer norm2
I0721 10:38:16.725368  4692 net.cpp:84] norm2 <- pool2
I0721 10:38:16.725378  4692 net.cpp:110] norm2 -> norm2
I0721 10:38:16.725389  4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.725399  4692 net.cpp:151] norm2 needs backward computation.
I0721 10:38:16.725409  4692 net.cpp:74] Creating Layer conv3
I0721 10:38:16.725419  4692 net.cpp:84] conv3 <- norm2
I0721 10:38:16.725427  4692 net.cpp:110] conv3 -> conv3
I0721 10:38:16.735193  4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.735213  4692 net.cpp:151] conv3 needs backward computation.
I0721 10:38:16.735224  4692 net.cpp:74] Creating Layer relu3
I0721 10:38:16.735234  4692 net.cpp:84] relu3 <- conv3
I0721 10:38:16.735242  4692 net.cpp:98] relu3 -> conv3 (in-place)
I0721 10:38:16.735250  4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.735258  4692 net.cpp:151] relu3 needs backward computation.
I0721 10:38:16.735302  4692 net.cpp:74] Creating Layer conv4
I0721 10:38:16.735312  4692 net.cpp:84] conv4 <- conv3
I0721 10:38:16.735321  4692 net.cpp:110] conv4 -> conv4
I0721 10:38:16.743952  4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.743988  4692 net.cpp:151] conv4 needs backward computation.
I0721 10:38:16.744000  4692 net.cpp:74] Creating Layer relu4
I0721 10:38:16.744010  4692 net.cpp:84] relu4 <- conv4
I0721 10:38:16.744020  4692 net.cpp:98] relu4 -> conv4 (in-place)
I0721 10:38:16.744030  4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.744038  4692 net.cpp:151] relu4 needs backward computation.
I0721 10:38:16.744050  4692 net.cpp:74] Creating Layer conv5
I0721 10:38:16.744057  4692 net.cpp:84] conv5 <- conv4
I0721 10:38:16.744067  4692 net.cpp:110] conv5 -> conv5
I0721 10:38:16.748935  4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.748955  4692 net.cpp:151] conv5 needs backward computation.
I0721 10:38:16.748965  4692 net.cpp:74] Creating Layer relu5
I0721 10:38:16.748975  4692 net.cpp:84] relu5 <- conv5
I0721 10:38:16.748983  4692 net.cpp:98] relu5 -> conv5 (in-place)
I0721 10:38:16.748998  4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.749011  4692 net.cpp:151] relu5 needs backward computation.
I0721 10:38:16.749022  4692 net.cpp:74] Creating Layer pool5
I0721 10:38:16.749030  4692 net.cpp:84] pool5 <- conv5
I0721 10:38:16.749039  4692 net.cpp:110] pool5 -> pool5
I0721 10:38:16.749050  4692 net.cpp:125] Top shape: 256 256 6 6 (2359296)
I0721 10:38:16.749058  4692 net.cpp:151] pool5 needs backward computation.
I0721 10:38:16.749074  4692 net.cpp:74] Creating Layer fc6
I0721 10:38:16.749083  4692 net.cpp:84] fc6 <- pool5
I0721 10:38:16.749091  4692 net.cpp:110] fc6 -> fc6
I0721 10:38:17.160079  4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.160148  4692 net.cpp:151] fc6 needs backward computation.
I0721 10:38:17.160166  4692 net.cpp:74] Creating Layer relu6
I0721 10:38:17.160177  4692 net.cpp:84] relu6 <- fc6
I0721 10:38:17.160190  4692 net.cpp:98] relu6 -> fc6 (in-place)
I0721 10:38:17.160202  4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.160212  4692 net.cpp:151] relu6 needs backward computation.
I0721 10:38:17.160222  4692 net.cpp:74] Creating Layer drop6
I0721 10:38:17.160230  4692 net.cpp:84] drop6 <- fc6
I0721 10:38:17.160238  4692 net.cpp:98] drop6 -> fc6 (in-place)
I0721 10:38:17.160258  4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.160265  4692 net.cpp:151] drop6 needs backward computation.
I0721 10:38:17.160277  4692 net.cpp:74] Creating Layer fc7
I0721 10:38:17.160286  4692 net.cpp:84] fc7 <- fc6
I0721 10:38:17.160295  4692 net.cpp:110] fc7 -> fc7
I0721 10:38:17.342094  4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.342157  4692 net.cpp:151] fc7 needs backward computation.
I0721 10:38:17.342175  4692 net.cpp:74] Creating Layer relu7
I0721 10:38:17.342185  4692 net.cpp:84] relu7 <- fc7
I0721 10:38:17.342198  4692 net.cpp:98] relu7 -> fc7 (in-place)
I0721 10:38:17.342208  4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.342217  4692 net.cpp:151] relu7 needs backward computation.
I0721 10:38:17.342228  4692 net.cpp:74] Creating Layer drop7
I0721 10:38:17.342236  4692 net.cpp:84] drop7 <- fc7
I0721 10:38:17.342245  4692 net.cpp:98] drop7 -> fc7 (in-place)
I0721 10:38:17.342254  4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.342262  4692 net.cpp:151] drop7 needs backward computation.
I0721 10:38:17.342274  4692 net.cpp:74] Creating Layer fc8
I0721 10:38:17.342283  4692 net.cpp:84] fc8 <- fc7
I0721 10:38:17.342291  4692 net.cpp:110] fc8 -> fc8
I0721 10:38:17.343199  4692 net.cpp:125] Top shape: 256 22 1 1 (5632)
I0721 10:38:17.343214  4692 net.cpp:151] fc8 needs backward computation.
I0721 10:38:17.343231  4692 net.cpp:74] Creating Layer loss
I0721 10:38:17.343240  4692 net.cpp:84] loss <- fc8
I0721 10:38:17.343250  4692 net.cpp:84] loss <- label
I0721 10:38:17.343264  4692 net.cpp:151] loss needs backward computation.
I0721 10:38:17.343305  4692 net.cpp:173] Collecting Learning Rate and Weight Decay.
I0721 10:38:17.343327  4692 net.cpp:166] Network initialization done.
I0721 10:38:17.343335  4692 net.cpp:167] Memory required for Data 1073760256
技術分享 blink 15年1月

CIFAR-10在caffe上進行訓練與學習

使用數據庫:CIFAR-10

60000張 32X32 彩色圖像 10類,50000張訓練,10000張測試

技術分享

準備

在終端運行以下指令:

cd $CAFFE_ROOT/data/cifar10
./get_cifar10.sh
cd $CAFFE_ROOT/examples/cifar10
./create_cifar10.sh

其中CAFFE_ROOT是caffe-master在你機子的地址

運行之後,將會在examples中出現數據庫文件./cifar10-leveldb和數據庫圖像均值二進制文件./mean.binaryproto

技術分享

模型

該CNN由卷積層,POOLing層,非線性變換層,在頂端的局部對比歸一化線性分類器組成。該模型的定義在CAFFE_ROOT/examples/cifar10 directory’s cifar10_quick_train.prototxt中,可以進行修改。其實後綴為prototxt很多都是用來修改配置的。

技術分享

訓練和測試

訓練這個模型非常簡單,當我們寫好參數設置的文件cifar10_quick_solver.prototxt和定義的文件cifar10_quick_train.prototxt和cifar10_quick_test.prototxt後,運行train_quick.sh或者在終端輸入下面的命令:

cd $CAFFE_ROOT/examples/cifar10
./train_quick.sh

即可,train_quick.sh是一個簡單的腳本,會把執行的信息顯示出來,培訓的工具是train_net.bin,cifar10_quick_solver.prototxt作為參數。

然後出現類似以下的信息:這是搭建模型的相關信息

I0317 21:52:48.945710 2008298256 net.cpp:74] Creating Layer conv1
I0317 21:52:48.945716 2008298256 net.cpp:84] conv1 <- data
I0317 21:52:48.945725 2008298256 net.cpp:110] conv1 -> conv1
I0317 21:52:49.298691 2008298256 net.cpp:125] Top shape: 100 32 32 32 (3276800)
I0317 21:52:49.298719 2008298256 net.cpp:151] conv1 needs backward computation.

接著:

0317 21:52:49.309370 2008298256 net.cpp:166] Network initialization done.
I0317 21:52:49.309376 2008298256 net.cpp:167] Memory required for Data 23790808
I0317 21:52:49.309422 2008298256 solver.cpp:36] Solver scaffolding done.
I0317 21:52:49.309447 2008298256 solver.cpp:47] Solving CIFAR10_quick_train

之後,訓練開始

I0317 21:53:12.179772 2008298256 solver.cpp:208] Iteration 100, lr = 0.001
I0317 21:53:12.185698 2008298256 solver.cpp:65] Iteration 100, loss = 1.73643
...
I0317 21:54:41.150030 2008298256 solver.cpp:87] Iteration 500, Testing net
I0317 21:54:47.129461 2008298256 solver.cpp:114] Test score #0: 0.5504
I0317 21:54:47.129500 2008298256 solver.cpp:114] Test score #1: 1.27805

其中每100次叠代次數顯示一次訓練時lr(learningrate),和loss(訓練損失函數),每500次測試一次,輸出score 0(準確率)和score 1(測試損失函數)

當5000次叠代之後,正確率約為75%,模型的參數存儲在二進制protobuf格式在cifar10_quick_iter_5000

然後,這個模型就可以用來運行在新數據上了。

其他

另外,更改cifar*solver.prototxt文件可以使用CPU訓練,

# solver mode: CPU or GPU
solver_mode: CPU

可以看看CPU和GPU訓練的差別。

主要資料來源:caffe官網教程

Caffe 深度學習框架上手教程