darkflow結合darknet與tensorflow,除了可以使用yolo已經訓練好的模型外,也可以自己訓練模型,輸入圖片與標記檔(xml),讀取labels.txt就可以透過darkflow訓練自己的模型。
Step1)安裝darkflow,請參考使用darkflow結合yolo影像辨識。
如果虛擬環境已安裝,新增一個訓練資料夾,在資料夾下可以從新安裝darkflow,在執行python .\flow時才不會出現錯誤,執行以下步驟。
建立一個專案資料夾,例如:df,在資料夾df下,重新下載與安裝darkflow。
#cd df
#git clone https://github.com/thtrieu/darkflow
#cd darkflow
#pip uninstall darkflow
#pip install -e .
Step2)下載訓練用圖檔與XML標記檔,本範例使用世界級的計算機視覺比賽Pattern Analysis, Statical Modeling and Computational(PASCAL) Learning(http://host.robots.ox.ac.uk/pascal/VOC/voc2007/)的訓練資料VOCtrainval_06-Nov-2007.tar(壓縮後約400MB)。
解壓縮後將資料夾VOC2007拷貝到darkflow的上一層資料夾df下。圖片檔在JPEGImages資料夾下,XML標記檔在Annotations,資料夾下結構如下。
Step3)修改以下檔案
(1)設定檔:https://github.com/pjreddie/darknet/blob/master/cfg/yolov2-tiny-voc.cfg,放置於VOC2007資料夾下的cfg資料夾下。
修改第124行為classes=3,表示辨識三種物件
修改第118行為filters=40,因為num*(classes+5)=5*(3+5),num在yolov2-tiny-voc.cfg的第126行。
(2)權重檔:https://pjreddie.com/media/files/yolov2-tiny-voc.weights,放置於VOC2007資料夾下的bin資料夾下。
(3)在VOC2007資料夾下,新增labels.txt,內含要辨識的物件,本範例為
tvmonitor
person
pottedplant
在VOC2007內有20種物件,分別為aeroplane、bicycle、bird、boat、bottle、bus、car、cat、chair、cow、diningtable、dog、horse、motorbike、person、pottedplant、sheep、sofa、train、tvmonitor,可以選以上20種的3種。
Step4)開始訓練,訓練時發生以下錯誤
File "C:\darkflow\darkflow\darkflow\utils\loader.py", line 77, in load
walker.offset, walker.size)
AssertionError: expect 63123056 bytes, found 63471556
修改darkflow\utils\loader.py的第121行,self.offset = 16+(63471556-63123056)
Step5)進行訓練
切換資料夾到VOC2007
#cd VOC2007
flow指令說明
python ..\darkflow\flow
--model 設定檔
--load 權重檔
--train
--annotation 標記XML資料夾
--dataset 圖片資料夾
--labels 標記名稱檔(labels.txt)
#python ..\darkflow\flow --model cfg\yolov2-tiny-voc.cfg --load bin\yolov2-tiny-voc.weights --train --annotation Annotations --dataset JPEGImages --labels labels.txt
出現以下訊息表示正在訓練,目前已經執行922次,損失為88.33761596679688。
step 921 - loss 89.11116027832031 - moving ave loss 89.31534514771508
step 922 - loss 88.33761596679688 - moving ave loss 89.21757222962327
經過10個小時後,損失降低到 5.9371
step 5012 - loss 6.518990516662598 - moving ave loss 6.504753824650983
step 5013 - loss 5.937129974365234 - moving ave loss 6.447991439622409
Step6)訓練結果
訓練過程的權重檔在VOC2007資料夾下的資料夾ckpt,儲存模型參數的暫存檔,資料夾內容如下。
Step7)如果訓練到一半中斷後,想從最後紀錄點開始訓練,使用「--load -1」,就會使用最後紀錄點權重進行訓練。
#python ..\darkflow\flow --model cfg\yolov2-tiny-voc.cfg --load -1 --train --annotation Annotations --dataset JPEGImages --labels labels.txt
Step8)將要辨識的圖片複製到sample_img資料夾下,以下指令使用最後紀錄點進行圖片辨識
#python ..\darkflow\flow --model cfg\yolov2-tiny-voc.cfg --load -1 --labels labels.txt --imgdir sample_img\