轉自 https://blog.csdn.net/u011961856/article/details/76582669
參考自github:https://github.com/thtrieu/darkflow
darkflow實現了將darknet翻譯成tensorflow,可以用tensorflow加載darknet訓練好的模型,並使用tensorflow重新訓練,輸出tensorflow graph模型,用於移動設備.
darkflow需要的依賴庫:
Python3, tensorflow 1.0, numpy, opencv 3.
下載與安裝darkfolw:
首先需要安轉cython for python3,安裝命令為:
sudo pip3 install Cython --install-option="--no-cython-compile"
下載darkflow:
git clone https://github.com/thtrieu/darkflow
進入darkflow目錄,並安裝:
cd darkflow
python3 setup.py build_ext --inplace
pip3 install .
安裝成功:
訓練與測試
labels.txt文件位於darkflow/目錄下.文件中為需要分類的類別明,例如如果你只想要檢測三類,分別為tvmonitor, person, pottedplant,那么編輯labels.txt為:
tvmonitor
person
pottedplant
也可以通過參數--labels例如--labels myOtherLabelsFile.txt加載其他類別文件.如果不設置,darkflow默認加載label.txt文件.需要注意的是,如果使用設計好的COCO,VOC數據集的模型配置文件.cfg,則也會忽略label.txt,而是去加載對應的COCO,VOC標記文件.
網絡設計
與darknet相同,模型文件為.cfg文件,位於/home/qinghua/program/darkflow/cfg/下面,用戶也可以設計網絡結構,例如:
...
[convolutional]
batch_normalize = 1
size = 3
stride = 1
pad = 1
activation = leaky
[maxpool]
[connected]
output = 4096
activation = linear
...
可以通過參數選項--load,加載已經訓練好的模型,用於模型初始化:
# 1. Load yolo-tiny.weights
python3 ./flow --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights
模型下載鏈接:https://drive.google.com/drive/folders/0B1tW_VtY7onidEwyQ2FtQVplWEU
yolo-tiny.cfg為模型配置文件.
模型預測
如不需要用已有模型初始化,即隨機初始化模型,則代碼為:
# 2. To completely initialize a model, leave the --load option
python3 ./flow --model cfg/yolo-new.cfg
同時,darkflow還支持用訓練好的模型,初始化另一個模型中的相同的網絡層:
# 3. It is useful to reuse the first identical layers of tiny for `yolo-new`
python3 ./flow --model cfg/yolo-new.cfg --load bin/yolo-tiny.weights
# this will print out which layers are reused, which are initialized
輸入圖像位於目錄/darkflow/sample_img/下,預測結構存入目錄/darkflow/sample_img/out/下.
測試,如要將輸出保存為json格式,則加上--json選項:
python3 ./flow --imgdir sample_img/ --model cfg/tiny-yolo-4c.cfg --load bin/tiny-yolo-4c.weights --gpu 0 --json
運行結果如下圖:
模型訓練:
模型訓練參數選項為--train,訓練代碼為:
# Initialize yolo-new from yolo-tiny, then train the net on 100% GPU:
python3 ./flow --model cfg/yolo-new.cfg --load bin/yolo-tiny.weights --train --gpu 0
可以通過參數--trainer設置梯度更新函數:
# Completely initialize yolo-new and train it with ADAM optimizer
python3 ./flow --model cfg/yolo-new.cfg --train --trainer adam
訓練的時候,會周期性地將模型保存到darkflow/ckpt/目錄下,可以通過設置--load -1,加載最近的checkpoint,
# Resume the most recent checkpoint for training
python3 ./flow --train --model cfg/yolo-new.cfg --load -1
使用step=1500的checkpoint預測:
# Test with checkpoint at step 1500
python3 ./flow --model cfg/yolo-new.cfg --load 1500
使用已經訓練的模型訓練:
# Fine tuning yolo-tiny from the original one
python3 ./flow --train --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights
保存graph
保存graph到protobuf文件(.pb文件):
## Saving the lastest checkpoint to protobuf file
python3 ./flow --model cfg/yolo-new.cfg --load -1 --savepb
## Saving graph and weights to protobuf file
python3 ./flow --model cfg/yolo.cfg --load bin/yolo.weights --savepb
android demo:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowYoloDetector.java
darkflow也支持從.pb文件加載模型,
## Forward images in sample_img for predictions based on protobuf file
python3 ./flow --pbLoad built_graph/yolo.pb --metaLoad built_graph/yolo.meta --imgdir sample_img/
---------------------
作者:imperfect00
來源:CSDN
原文:https://blog.csdn.net/u011961856/article/details/76582669
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!