首先获得darknet的官方源码并做配置修改并编译:
git clone https://github.com/pjreddie/darknet.git
cd darknet
vi Makefile
修改如下项目:
GPU=1
CUDNN=1
OPENCV=1
OPENMP=0
DEBUG=0
ARCH= -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=[sm_50,compute_50] \
-gencode arch=compute_52,code=[sm_52,compute_52] \
-gencode arch=compute_70,code=[sm_70,compute_70]
算力根据你所使用的GPU卡的类型定,我使用的是Tesla v100,各种GPU的具体算力值参考https://developer.nvidia.com/cuda-gpus
然后执行:
make
如果编译过程中报错说: No package 'opencv' found,安装libopencv-dev即可:
sudo apt install libopencv-dev
编译完后darknet二进制文件就生成了,下面下载训练所需weights文件和根据数据集格式(例如我使用VOC2007格式)做配置:
1. YOLOv3
1)wget https://pjreddie.com/media/files/darknet53.conv.74
2)vi cfg/yolov3-voc.cfg
将Testing部分注释掉,将Trainning部分打开:
[net]
# Testing
# batch=1
# subdivisions=1
# Training
batch=64
subdivisions=16
将所有[yolo]里面classes修改为你的class的数量,例如我的是11;
对所有[yolo]的前一个[convolutional]中的filters进行修改, 其取值为filters = 3 * ( classes + 5 ),由于上一步中classes=11所以这里filters取48. 另外酌情将所有.ignore_threshold的由0.7改成需要的值,例如 0.5(optional).
3)获取voc_label.py并做修改:
cd data
wget https://pjreddie.com/media/files/voc_label.py
vi voc_label.py
#sets=[('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
sets=[('2007', 'train'), ('2007', 'val')]
#classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
classes = ['person','wm','dr', ...] #总共有11个class
if cls not in classes: #or int(difficult) == 1:
4) 确认数据集在darknet/data/VOCdevkit/VOC2007下,voc_label.py在darknet/data/下,在 darknet/data/下执行
python voc_label.py #生成2007_train.txt, 2007_val.txt以及VOCdevkit/VOC2007/labels/下多个文件
5)然后修改data/voc.names和cfg/voc.data的内容:
cd ..
vi data/voc.names
person
wm
dr
...
vi cfg/voc.data
classes= 11
train = data/2007_train.txt
valid = data/2007_val.txt
names = data/voc.names
backup = backup #设置保存weights文件的目录
6)执行训练:
nohup ./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 &
#测试训练结果
./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-tvoc_20000.weights data/VOCdevkit/VOC2007/JPEGImages/v521-20-05-00400.jpg
此外,如果直接使用yolov3的预训练weights文件做测试:
wget https://pjreddie.com/media/files/yolov3.weights
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/dog.jpg
2. YOLOv3 Tiny
对于Tiny版本, 首先需要用weights文件生成一个tiny版网络的预训练模型:
wget https://pjreddie.com/media/files/yolov3-tiny.weights
./darknet partial ./cfg/yolov3-tiny.cfg ./yolov3-tiny.weights ./yolov3-tiny.conv.15 15
对于yolov3 tiny配置文件的修改:
cp yolov3-tiny.cfg yolov3-tiny-voc.cfg
然后做和上面YOLOv3的yolov3-voc.cfg里一样的修改,同样对data/voc.names和cfg/voc.data做同样的修改,也需要执行python voc_label.py生成list文件,然后训练:
nohup ./darknet detector train cfg/voc.data cfg/yolov3-tiny-voc.cfg yolov3-tiny.conv.15 &
#测试训练结果
./darknet detector test cfg/voc.data cfg/yolov3-tiny-voc.cfg backup/yolov3-tiny-voc_1000.weights data/VOCdevkit/VOC2007/JPEGImages/v521-20-05-00400.jpg
训练输出信息解释:
网络结构里三个yolo layer