文章目录
darkflow实现了将darknet翻译成tensorflow,可以用tensorflow加载darknet训练好的模型,并使用tensorflow重新训练,输出tensorflow graph模型,用于移动设备.
0. 下载安装包
git clone https://github.com/thtrieu/darkflow
注:安装之前需要安装好:Python3、tensorflow 1.0、numpy, opencv 3,如果没有安装请先安装好。
1. 安装
cd darkflow
python3 setup.py build_ext --inplace
sudo pip3 install .
在最后一一步进行pip install.
的时候,如果不加入sudo
是没有权限的。
2. 测试
模型权重下载(可能需要翻墙):https://drive.google.com/drive/folders/0B1tW_VtY7onidEwyQ2FtQVplWEU
不能翻墙可以去百度云链接(密码:xf8a)
将下载好的权重放置到bin文件夹内。
使用yolo-tiny进行检测
python3 ./flow --imgdir sample_img/ --model cfg/tiny-yolo-4c.cfg --load bin/tiny-yolo-voc.weights --gpu 0 --json
使用yolo进行检测
python3 ./flow --imgdir sample_img/ --model cfg/yolo.cfg --load bin/yolo.weights --gpu 0 --json
检测结果我们可以在./darkflow/sample_img/out
文件夹中看到,文件格式为文件名+.json
JSON 输出文件格式为:
[{"label":"person", "confidence": 0.56, "topleft": {"x": 184, "y": 101}, "bottomright": {"x": 274, "y": 382}},
{"label": "dog", "confidence": 0.32, "topleft": {"x": 71, "y": 263}, "bottomright": {"x": 193, "y": 353}},
{"label": "horse", "confidence": 0.76, "topleft": {"x": 412, "y": 109}, "bottomright": {"x": 592,"y": 337}}]
label: 检测分类;
confidence: 置信度;
topleft: 检测框左上角坐标;
bottomright: 检测框右下角坐标。
3. 训练
3.1 训练新的模型
训练模型的操作是简单的,只需要将后边添加--train
从yolo-tiny中初始化yolo-new,然后100%在GPU上进行训练:
flow --model cfg/yolo-new.cfg --load bin/tiny-yolo.weights --train --gpu 1.0
完成yolo-new的初始化,使用ADAM进行训练
flow --model cfg/yolo-new.cfg --train --trainer adam
在训练期间,脚本偶尔会将中间结果保存到Tensorflow checkpoints中,并存储在ckpt/中。 要在执行训练/测试之前恢复到任何检查点,请使用–load [checkpoint_num]选项,如果checkpoint_num <0,则darkflow将通过解析ckpt/checkpoint加载最新的保存。
3.2 训练自己的数据集
- **1.**创建一个配置文件tiny-yolo-voc.cfg的副本并根据您的偏好重新命名它tiny-yolo-voc-3c.cfg。
注:当darkflow发现您正在加载tiny-yolo-voc.weights时,它会在cfg/文件夹中查找tiny-yolo-voc.cfg,并将该配置文件与您使用–model cfg/tiny-yolo-voc-3c.cfg设置的新配置文件进行比较。 在这种情况下,除了最后两个图层外,每个图层都具有相同的权重数量,所以它会将权重加载到所有图层中,直到后两个图层,因为它们现在包含不同数量的权重。
- **2.**在tiny-yolo-voc-3c.cfg中,将[region]层(最后一层)中的类更改为要训练的类的数量。 在我们的例子中,类被设置为3。
...
[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
coords=4
num=5
softmax=1
...
- **3.**在tiny-yolo-voc-3c.cfg中,将[convolutional]层(第二层到最后一层)中的滤镜更改为num (classes + 5)。 在我们的例子中,num是5并且类是3,所以5 (3 + 5)= 40,因此过滤器被设置为40。
...
[convolutional]
size=1
stride=1
pad=1
filters=40
activation=linear
[region]
anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52
...
- **4.**更改labels.txt以包含要训练的标签(标签数量应与您在tiny-yolo-voc-3c.cfg文件中设置的分类数量相同)。 在我们的例子中,labels.txt将包含3个标签。
label1
label2
label3
- **5.**训练时参考tiny-yolo-voc-3c.cfg模型。
flow --model cfg/tiny-yolo-voc-3c.cfg --load bin/tiny-yolo-voc.weights --train --annotation train/Annotations --dataset train/Images
4. 相机/视频文件演示
对于完全在CPU上运行的演示:
flow --model cfg/yolo-new.cfg --load bin/yolo-new.weights --demo videofile.avi
对于在GPU上运行100%的演示:
flow --model cfg/yolo-new.cfg --load bin/yolo-new.weights --demo videofile.avi --gpu 1.0
要使用摄像头/摄像头,只需使用关键字camera替换videofile.avi即可。
要使用预测的边界框保存视频,请添加–saveVideo选项。
# 检测摄像头版:
python3 flow --model cfg/yolo.cfg --load bin/yolo.weights --demo camera
# 检测视频版:
python3 flow --model cfg/yolo.cfg --load bin/yolo.weights --demo demo.avi
# 使用GPU版:
python3 flow --model cfg/yolo.cfg --load bin/yolo.weights --demo camera --gpu 1.0
5. python接口
注:请注意,return_predict(img)必须采用numpy.ndarray。 您的图片必须事先加载并传递给return_predict(img)。 传递文件路径不起作用。return_predict(img)的结果将是一个字典列表,它表示每个检测到的对象的值与上面列出的JSON输出格式相同。
from darkflow.net.build import TFNet
import cv2
options = {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", "threshold": 0.1}
tfnet = TFNet(options)
imgcv = cv2.imread("./sample_img/sample_dog.jpg")
result = tfnet.return_predict(imgcv)
print(result)
6.将构建的图保存到protobuf文件(.pb)
将最近的检查点保存为protobuf文件
flow --model cfg/yolo-new.cfg --load -1 --savepb
保存图和权重到protobuf文件
flow --model cfg/yolo.cfg --load bin/yolo.weights --savepb