(1) Data preparation
With the help of LabelImg, I labeled 858 pictures with the feature hand, so I got 858 annotation files. Before I started to label, I renamed all 858 pictures, and the new names are from 0.jpg to 857.jpg. Here is the rename syntax:
1. # -*- coding:utf8 -*-
2.
3. import os
4.
5. class BatchRename():
6. '''
7. 批量重命名文件夹中的图片文件
8.
9. '''
10. def __init__(self):
11. self.path = '/home/tec/下载/pictures_yolo/head' #change your directory here
12.
13. def rename(self):
14. filelist = os.listdir(self.path)
15. total_num = len(filelist)
16. i = 858 #change the first Serial number
17. for item in filelist:
18. if item.endswith('.jpg'):
19. src = os.path.join(os.path.abspath(self.path), item)
20. dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpg')
21. try:
22. os.rename(src, dst)
23. print 'converting %s to %s ...' % (src, dst)
24. i = i + 1
25. except:
26. continue
27. print 'total %d to rename & converted %d jpgs' % (total_num, i)
28.
29. if __name__ == '__main__':
30. demo = BatchRename()
31. demo.rename()
(2) A problem
After the data were all set, you can start to train your yolo. At first, no bounding box was predicted,I changed the “learning rate” to 0.0001, problem solved. I think it's because I set the max_batches=500, and yolo didn’t learn enough features.
(3) Important parameters
Ichanged the batch=64, subdivisions=8, set the max_batches=80000.
(4) Do not forget to make again
Since I already installed CUDA, OPENCV(auseful blog to help me install opencv here)and equipped with GPU, I changed three parameters in the makefile, and make again
make clean
make
(5) Train
Now, you canstart to train yolo by the syntax:
1. ./darknet detector train cfg/voc.data cfg/yolo-voc.cfg darknet19_448.conv.23
darknet19_448.conv.23 file is the pre-trained weight, you should download it first. Due to the max_batches=80000, it’s a nature thing to cost a few days. Free to pause it anytime, because you can continue anytime. When you are gonna start again, you can use the trained weight like I do by next syntax:
./darknet detector train cfg/voc.data cfg/yolo-voc.cfg backup/yolo-voc_40000.weights
Change your weight by replacing “yolo-voc_40000.weights”. If the loss are around0.3(the blue oval), and the IOU(the blue square)is bigger than 0.9,it means your model is near completion.
(6) Video test
Make sure your opencv is functional, and you can test your model by next syntax:
./darknet detector demo cfg/voc.data cfg/yolo-voc.cfg backup/yolo-voc_80000.weights