使用Darkflow+Yolov2实现水果识别模型
环境配置
本模型训练的环境为: Linux 3.10 + Python 3.5.2 + Tensorflow-gpu 1.4.0 + Darkflow + CUDA 8.0 + CUDNN 6.0 + Opencv 3.4.1
安装Opencv
1
2
3
4
5
6
7
8
9
10
11$ sudo pip3 install opencv-python
#验证安装
[squarepants@root ~]$ python3
Python 3.5.2 (default, Jun 5 2018, 03:54:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.1'
>>>安装CUDA 8.0 + CUDNN 6.0
首先先安装CUDA 8.0,去Nvidia官网下载对应系统及版本的在线deb包。例如在Ubuntu14.04下, 下载完成后:
1
2
3$ sudo dpkg -i cuda-repo-ubuntu1404_8.0.44-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get install cuda安装CUDNN,同样去Nvidia官网(这个需要注册以及填一个问卷)将runtime, developer, sample三个包都下载下来:
1
2
3$ sudo dpkg -i libcudnn6-doc_6.0.21-1+cuda8.0_amd64.deb
$ sudo dpkg -i libcudnn6-dev_6.0.21-1+cuda8.0_amd64.deb
$ sudo dpkg -i libcudnn6_6.0.21-1+cuda8.0_amd64.deb安装tensorflow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33$ sudo pip3 install tensorflow-gpu==1.4
#验证安装
[squarepants@root ~]$ python3
Python 3.5.2 (default, Jun 5 2018, 03:54:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant("Hello,world!")
>>> sess = tf.Session()
2018-07-12 15:44:48.318820: I T:\src\github\tensorflow\tensorflow\core\platform\
cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow bi
nary was not compiled to use: AVX2
2018-07-12 15:44:48.704842: I T:\src\github\tensorflow\tensorflow\core\common_ru
ntime\gpu\gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX 970M major: 5 minor: 2 memoryClockRate(GHz): 1.038
pciBusID: 0000:01:00.0
totalMemory: 3.00GiB freeMemory: 2.87GiB
2018-07-12 15:44:48.704842: I T:\src\github\tensorflow\tensorflow\core\common_ru
ntime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2018-07-12 15:44:49.009860: I T:\src\github\tensorflow\tensorflow\core\common_ru
ntime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1
edge matrix:
2018-07-12 15:44:49.010860: I T:\src\github\tensorflow\tensorflow\core\common_ru
ntime\gpu\gpu_device.cc:929] 0
2018-07-12 15:44:49.010860: I T:\src\github\tensorflow\tensorflow\core\common_ru
ntime\gpu\gpu_device.cc:942] 0: N
2018-07-12 15:44:49.010860: I T:\src\github\tensorflow\tensorflow\core\common_ru
ntime\gpu\gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:
0/task:0/device:GPU:0 with 2589 MB memory) -> physical GPU (device: 0, name: GeF
orce GTX 970M, pci bus id: 0000:01:00.0, compute capability: 5.2)
>>> print(sess.run(hello))
b'Hello,world!'安装Darkflow
1
2
3$ sudo git clone https://github.com/thtrieu/darkflow.git
$ cd darkflow
$ sudo pip3 install -e .
VOC训练集生成
-
利用Darkflow训练模型
去Yolo官网下载Yolov2的神经网络模型以及预生成的网络权重(本模型使用的是yolov2-tiny-voc)
训练之前先调整yolov2输入层的参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29$ cd darkflow-master
$ sudo vim /cfg/yolov2-tiny-voc.cfg
#拉到最下面找到region以及第一个卷积层的参数
[convolutional]
size=1
stride=1
pad=1
filters=125 #输入层与第一个卷积层连接的filters = 5*(class数量 + 5)= 5 * 13 = 65,应改成65
activation=linear
[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=20 #本模型一共八个标签,所以应修改为8
coords=4
num=5
softmax=1
jitter=.2
rescore=1
object_scale=5
noobject_scale=1
class_scale=1
coord_scale=1
absolute=1
thresh = .6
random=1执行训练
1
2$ cd darkflow-master
$ python flow --model cfg/yolov2-tiny-voc-8c.cfg --load bin/yolov2-tiny-voc.weights --train --annotation 8C_ModelData/annotations --dataset 8C_ModelData/images --gpu 0.8 --epoch 1200