darkflow测试和训练yolo

来源:互联网 发布:relex软件 破解版 编辑:程序博客网 时间:2024/05/17 22:59

参考自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 clonehttps://github.com/thtrieu/darkflow

进入darkflow目录,并安装:

cd darkflow

python3 setup.py build_ext --inplace
pip3 install .

安装成功:

训练与测试

labels.txt文件位于darkflow/目录下.文件中为需要分类的类别明,例如如果你只想要检测三类,分别为tvmonitor,person,pottedplant,那么编辑labels.txt为:

tvmonitorpersonpottedplant

也可以通过参数--labels例如--labels myOtherLabelsFile.txt加载其他类别文件.如果不设置,darkflow默认加载label.txt文件.需要注意的是,如果使用设计好的COCO,VOC数据集的模型配置文件.cfg,则也会忽略label.txt,而是去加载对应的COCO,VOC标记文件.

网络设计

与darknet相同,模型文件为.cfg文件,位于/home/qinghua/program/darkflow/cfg/下面,用户也可以设计网络结构,例如:

...[convolutional]batch_normalize = 1size = 3stride = 1pad = 1activation = leaky[maxpool][connected]output = 4096activation = linear...

可以通过参数选项--load,加载已经训练好的模型,用于模型初始化:

# 1. Load yolo-tiny.weightspython3 ./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 optionpython3 ./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 optimizerpython3 ./flow --model cfg/yolo-new.cfg --train --trainer adam

训练的时候,会周期性地将模型保存到darkflow/ckpt/目录下,可以通过设置--load -1,加载最近的checkpoint,

# Resume the most recent checkpoint for trainingpython3 ./flow --train --model cfg/yolo-new.cfg --load -1

使用step=1500的checkpoint预测:

# Test with checkpoint at step 1500python3 ./flow --model cfg/yolo-new.cfg --load 1500

使用已经训练的模型训练:

# Fine tuning yolo-tiny from the original onepython3 ./flow --train --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights

保存graph

保存graph到protobuf文件(.pb文件):

## Saving the lastest checkpoint to protobuf filepython3 ./flow --model cfg/yolo-new.cfg --load -1 --savepb## Saving graph and weights to protobuf filepython3 ./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/