全能電路設計實戰

2019年6月14日 星期五

訓練自己的偵測物件--YOLO


  YOLO的訓練模型框架

1.安裝Darkflow
2.先使用prebuild YOLO模型
3.訓練新的YOLO模型
  • 準備訓練資料 (圖片與xml檔): 利用LabelImg 工具框選物件及標注label
  • 修改YOLO模型參數,如class數量, filter數目
  • 定義label名稱
  • 開始訓練,相關命令列參數: model , load , gpu,…
  • 儲存模型(weight .pb & metafile .meta), 相關命令列參數: --savepb
  • 載入儲存的模型(.pb & .meta)
  • 利用python 測試YOLO模型


1.安裝Darkflow

git clone https://github.com/thtrieu/darkflow.git

python setup.py build_ext --inplace

在windows上要先安裝MS visual studio才能編譯安裝

Flowing the graph using flow

python flow --help  => 若能成功執行,表示己順利安裝成功

2.先使用prebuild YOLO模型


#測試YOLO darkflow

python flow --imgdir sample_img/ --model cfg/tiny-yolo-voc.cfg --load bin/tiny-yolo-voc.weights

--imgdir: 指定要倒入YOLO模型的圖檔目錄
--model : 指定網路結構
--load: 載入權重檔; 若為-1 則從ckpt/ 從上一次記錄的權重檔繼續train

3.訓練新的YOLO模型

  • 準備訓練資料 (圖片與xml檔): 利用LabelImg 工具框選物件及標注label

    安裝LabelImg 工具:

    在Anaconda terminal 輸入: 

    git clone https://github.com/tzutalin/labelImg
    cd lableimg
    python lableimg.py    ==> 出現畫面如下
    安裝時可能會需要這些套件:
    conda install pyqt=5
    pip install resources
    pip install requests
    pip install staty
    pip install lxml

    接著就開始框選圖片中的物件並label其名稱 (名字不要有特殊符號, -,_@,#&^...)

    每一張圖片可以有標記多個物件.而每一張圖片也對應著一個和圖檔相同檔名的.xml檔,此xml記錄著此圖片中每個物件座標及其label名稱


XML檔案的內容

當你用lableimg標記完之後, 大概像下圖這樣。一個xml檔記錄著一個對應的圖檔. 若xml中有20個,則圖檔就會至少有20個被對應  
圖檔位置: train/orchid/images

XML檔位置: train/orchid/annotations

  • 修改YOLO模型參數,如class數量, filter數目

1.) 通常你會使用fine tune方式, 即使用既有的yolo network model (.cfg) 與weight (.weights)作為你訓練新模型的基本架構, 然後再修改成你想要偵測的類別項目

用既有的yolo network model (.cfg) ==> 所以你會從cfg/ copy 一份新的cfg並稍微修改一下檔名.  
例如: cp tiny-yolo-voc.cfg tiny-yolo-voc-xxxx.cfg

在複製出來的cfg檔中,修改網路的最後2層, 主要有兩個2個地方, 一個是分類數classes, 另一個是filter的數量
   
...

[convolutional]  
size=1
stride=1
pad=1
filters=40     #在倒數第2個 [convolutional], 修改filters 數量
activation=linear

[region]
anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
bias_match=1
classes=3   #偵測的物件有3類
coords=4
num=5  # anchors  box的數量 (一個anchors會有confidence、座標、在每個類別的機率)
softmax=1


...
       
filters =num*(classes+5) , 若classes=3 , num(自訂)=5, filter=40


  • 定義label名稱
修改 darkflow/labels.txt, 一行一個label,  行數要和class數量一樣 (名字不要有特殊符號, -,_@,...)
  
redline
red
white
  • 開始進行YOLO模型訓練

# Fine tuning tiny-yolo-voc from the original one

python flow  --model cfg/tiny-yolo-voc-orchid.cfg --load bin/tiny-yolo-voc.weights  --train --annotation train/orchid/annotations --dataset train/orchid/images


--train : 表示要進行training
--model : 指定網路結構
--load: 載入權重檔; 若為-1 則從ckpt/ 從上一次記錄的權重檔繼續train
--dataset : 指出training data
--annotation: 框選object 的資訊 (.xml)
--labels: labels.txt 分類名稱. 每一行代表分類名稱


說明: 

  1.) --load  tiny-yolo-voc.weights ==> 會載入tiny-yolo-voc.weights 做為你的初始權重, 因此得去找找到對應的tiny-yolo-voc.cfg 如此才能知道如何將權重載入到網路中, 而你所自訂的tiny-yolo-voc-xxxx.weights  和原本tiny-yolo-voc.cfg 基本上前面都相同, 只有後面2層不同,所以後面的2層不會載入權重. 

原文說明如下

When darkflow sees you are loading tiny-yolo-voc.weights it will look for tiny-yolo-voc.cfg in your cfg/ folder and compare that configuration file to the new one you have set with --model cfg/tiny-yolo-voc-3c.cfg. In this case, every layer will have the same exact number of weights except for the last two, so it will load the weights into all layers up to the last two because they now contain different number of weights.


2.)
在你訓練過程中, darkflow會將不斷的weight存在 ckpt/目錄中, 所以你可以隨時從最後一次記錄的權重檔繼續train下去 (--load -1 ),而不用每次都重來..(重來很花時間的!)

python flow  --model cfg/tiny-yolo-voc-orchid.cfg --load -1 --train --annotation train/orchid/annotations --dataset  train/orchid/images

  • 將最後的權重檔儲存為tensorflow格式 .pb & meta file 

#Saving the lastest checkpoint to protobuf file
python flow --model cfg/tiny-yolo-voc-orchid.cfg --load -1 --savepb

built_graph/輸出2個檔案,到時候可拿此訓練好的模型來做物件偵測與識別

tiny-yolo-voc-orchid.pb
tiny-yolo-voc-orchid.meta


  • 測試模型

1.) 用darkflw指令測試訓練好的模型
## Forward images in sample_img for predictions based on protobuf file

python flow --pbLoad built_graph/tiny-yolo-voc-orchid.pb --metaLoad built_graph/tiny-yolo-voc-orchid.meta --imgdir train/orchid/images




2.) 利用python 測試YOLO模型









References:

https://github.com/thtrieu/darkflow

Full Example Code 下載


沒有留言 :

張貼留言