轉自 https://blog.csdn.net/u011961856/article/details/76582669
參考自github:https://github.com/thtrieu/darkflowjava
darkflow實現了將darknet翻譯成tensorflow,能夠用tensorflow加載darknet訓練好的模型,並使用tensorflow從新訓練,輸出tensorflow graph模型,用於移動設備.python
darkflow須要的依賴庫:android
Python3, tensorflow 1.0, numpy, opencv 3.git
下載與安裝darkfolw:
首先須要安轉cython for python3,安裝命令爲:github
sudo pip3 install Cython --install-option="--no-cython-compile"json
下載darkflow:網絡
git clone https://github.com/thtrieu/darkflowide
進入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 filepython3 ./flow --pbLoad built_graph/yolo.pb --metaLoad built_graph/yolo.meta --imgdir sample_img/--------------------- 做者:imperfect00 來源:CSDN 原文:https://blog.csdn.net/u011961856/article/details/76582669 版權聲明:本文爲博主原創文章,轉載請附上博文連接!