来源:https://blog.csdn.net/xiao_lxl/article/details/85047103
git clone https://github.com/pjreddie/darknet
cd darknet
make
wget https://pjreddie.com/media/files/yolov3.weights
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
see the detect:
105 conv 255 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 255 0.754 BFLOPs
106 yolo
Loading weights from yolov3.weights...Done!
data/dog.jpg: Predicted in 0.043505 seconds.
dog: 100%
truck: 92%
bicycle: 99%
使用detector test 模式检测图片
./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/dog.jpg
see the detect:
105 conv 255 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 255 0.754 BFLOPs
106 yolo
Loading weights from yolov3.weights...Done!
data/dog.jpg: Predicted in 0.043032 seconds.
dog: 100%
truck: 92%
bicycle: 99%
将阈值设为0
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -thresh 0
yolov3检测视频文件
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights /media/****/****/video/aa.mp4
wget https://pjreddie.com/media/files/yolov3-tiny.weights
./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
首先软连接本地电脑上的VOC数据库
cd data
ln -s /media/****/****/data/VOCdevkit .
若本机没有VOC数据库,可联网下载
wget https://pjreddie.com/media/files/VOCtrainval_11-May-2012.tar
wget https://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tar
wget https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar
tar xf VOCtrainval_11-May-2012.tar
tar xf VOCtrainval_06-Nov-2007.tar
tar xf VOCtest_06-Nov-2007.tar
将scripts文件夹下的voc_label.py文件复制到data路径下
cp ../scripts/voc_label.py .
生成标签文件
python voc_label.py
cat 2007_train.txt 2007_val.txt 2012_*.txt > train.txt
Modify Cfg for Pascal VOC Data cfg/voc.data
classes= 20
train = /media/****/****/darknet-yolov3/darknet/data/train.txt
valid = /media/****/****/darknet-yolov3/darknet/data/2007_test.txt
names = data/voc.names
backup = backup
注意修改cfg/yolov3-voc.cfg的参数
[net]
# Testing
# batch=1
# subdivisions=1
# Training
batch=64
subdivisions=16
训练时,batch=64, subdivisions=16,
测试时 batch=1,subdivisions=1
wget https://pjreddie.com/media/files/darknet53.conv.74
./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74
修改cfg/yolov3-voc.cfg的参数,否则会出现检测不到目标的现象,没有画出检测框
[net]
# Testing
batch=1
subdivisions=1
# Training
# batch=64
#subdivisions=16
./darknet detector demo cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights /media/****/****/video/aa.mp4
./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights data/dog.jpg -thresh 0.01
检测视频30~47fps,检测单张图片是0.022s
./darknet detector valid cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup -out ""
在终端返回用时,在./result下测试结果。
将py-faster-rcnn/lib/datasets/voc_eval.py拷贝到darknet根目录
voc_eval.py下载:https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/datasets/voc_eval.py
py-faster-rcnn中的voc_eval.py解读
https://blog.csdn.net/qq_34806812/article/details/82018072
from voc_eval import voc_eval
print(voc_eval('/home/****/****/darknet/results/{}.txt', '/home/****/****/darknet/voc/VOCdevkit/VOC2012/Annotations/{}.xml', '/home/****/****/darknet/voc/2012_val.txt', 'egret', '.'))
参数第一个为上一步的txt路径,
第二个为验证集对应的xml标签路径,
第三个为验证集txt文本路径,
第四个为待验证的类别名,
第五个为pkl文件保存的路径
注意:验证机txt内容必须是无路径无后缀的图片名。
重复执行,检测其他类别需要删除 ./darknet/annots.pkl ,或者改变compute_mAP.py中pkl文件保存的路径
voc_eval.py中返回的最后一个值即AP,检测单类就是mAP
修改examples/detector.c下的validate_detector_recall函数
//void validate_detector_recall(char *cfgfile, char *weightfile)
void validate_detector_recall(char *datacfg, char *cfgfile, char *weightfile)
//else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_detector_recall(datacfg,cfg, weights);
//list *plist = get_paths("data/coco_val_5k.list");
//char **paths = (char **)list_to_array(plist);
list options = read_data_cfg(datacfg);
char valid_images = option_find_str(options, “valid”, "/media/**/801328a5-39c6-4e08-b070-19fc662a5236/darknet-yolov3/darknet/data/2007_val.txt");
list *plist = get_paths(valid_images);
char **paths = (char **)list_to_array(plist);
//for(k = 0; k < l.w*l.h*l.n; ++k){
for(k = 0; k < nboxes; ++k){
注意以上四处修改,否则可能会出现IOU为NAN的情况。
修改后examples/detector.c之后需要重新make.
./darknet detector recall cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup
结果:最后一列为recall值。
新建computer_all_mAP.py
from voc_eval import voc_eval
import os
current_path = os.getcwd()
results_path = current_path+"/results"
sub_files = os.listdir(results_path)
mAP = []
for i in range(len(sub_files)):
class_name = sub_files[i].split(".txt")[0] rec, prec, ap = voc_eval('/home/peidashun/projects/darknet/results/{}.txt', '/home/peidashun/projects/darknet/voc/VOCdevkit/VOC2018/Annotations/{}.xml', '/home/peidashun/projects/darknet/voc/2018_test.txt', class_name, '/home/peidashun/projects/darknet/voc/VOCdevkit/VOC2018/mAP')
print("{} :\t {} ".format(class_name, ap))
mAP.append(ap)
mAP = tuple(mAP)
print("***************************")
print("mAP :\t {}".format( float( sum(mAP)/len(mAP)) ))
注意: results文件夹只能有 '类名.txt’文件,2018_test.txt文件包含所有results中txt文件的图片名(保证是共同的验证集),保证最后的输出路径下无pkl文件。
上述代码中的ap就是针对输入单一类别计算出的AP。
cp scripts/get_coco_dataset.sh data
cd data
bash get_coco_dataset.sh
其中,get_coco_dataset.sh为
#!/bin/bash
git clone https://github.com/pdollar/coco
cd coco
mkdir images
cd images
wget -c https://pjreddie.com/media/files/train2014.zip
wget -c https://pjreddie.com/media/files/val2014.zip
unzip -q train2014.zip
unzip -q val2014.zip
cd …
wget -c https://pjreddie.com/media/files/instances_train-val2014.zip
wget -c https://pjreddie.com/media/files/coco/5k.part
wget -c https://pjreddie.com/media/files/coco/trainvalno5k.part
wget -c https://pjreddie.com/media/files/coco/labels.tgz
tar xzf labels.tgz
unzip -q instances_train-val2014.zip
paste <(awk “{print “KaTeX parse error: Can't use function '\"' in math mode at position 4: PWD\̲"̲}" <5k.part)…PWD”}”
classes= 80
train = /media/****/****/data/darkbet-coco/coco/trainvalno5k.txt
valid = /media/****/****/data/darkbet-coco/coco/5k.txt
#valid = data/coco_val_5k.list
names = data/coco.names
backup = backup-coco
eval=coco
将yolov3.cfg拷贝一份,重命名为yolov3-coco.cfg,并修改
[net]
# Testing
# batch=1
# subdivisions=1
# Training
batch=64
subdivisions=63
使用两个GPU进行训练
./darknet detector train cfg/coco.data cfg/yolov3-coco.cfg darknet53.conv.74 -gpus 1,2
[1] https://pjreddie.com/darknet/yolo/
[2] https://github.com/pjreddie/darknet