环境:环境 ubantu16.04+cudnn7.0+cuda_9.0.176
1、安装darknet
$ git clone https://github.com/pjreddie/darknet
$ cd darknet
2、修改Makefile
GPU=1 #0或1 使用GPU为1,不使用为0。
CUDNN=1 #0或1
OPENCV=1 #0或1
OPENMP=0
DEBUG=0
3、编译
$ make
4、下载预训练模型
$ wget https://pjreddie.com/media/files/yolov3.weights
5、用预训练模型进行简单的测试
- 一张图片测试:
$ ./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
2)视频检测:
$ ./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights person.mp4
6、训练VOC风格的数据
所用数据集为COCO中抽取的person类转化为VOC风格。
darknet不需要xml文件,需要.txt文件,用voc_label.py生成
修改voc_label.py文件
修改sets为训练样本集的名称
sets=[(‘2007’, ‘train’),(‘2007’,‘val’),(‘2007’,‘test’)]
修改classes为训练样本集的类标签
classes=[“person”]
在voc_label.py文件中加上os.system(“cat 2007_train.txt 2007_val.txt > train.txt”)
或直接在voc_label.py所在文件夹中打开终端执行cat 2007_train.txt 2007_val.txt > train.txt命令。
7、下载Imagenet上预先训练的权重
$ wget https://pjreddie.com/media/files/darknet53.conv.74
8、修改cfg/voc.data
classes= 1 #classes为训练样本集的类别总数
train = /home/user/darknet/train.txt #train的路径为训练样本集所在的路径
valid = /home/user/darknet/2007_test.txt #valid的路径为验证样本集所在的路径
names = data/voc.names #names的路径为data/voc.names文件所在的路径
backup = backup
9、修改data/voc.name为样本集的标签名
在文件中写入person
10、修改cfg/yolov3-voc.cfg
[net]
# Testing ### 测试模式
# batch=1
# subdivisions=1
# Training ### 训练模式,每次前向的图片数目 = batch/subdivisions
batch=64
subdivisions=16
width=416 ### 网络的输入宽、高、通道数
height=416
channels=3
momentum=0.9 ### 动量
decay=0.0005 ### 权重衰减
angle=0
saturation = 1.5 ### 饱和度
exposure = 1.5 ### 曝光度
hue=.1 ### 色调
learning_rate=0.001 ### 学习率
burn_in=1000 ### 学习率控制的参数
max_batches = 50200 ### 迭代次数 我设置的是10000
policy=steps ### 学习率策略
steps=40000,45000 ### 学习率变动步长 我设置的是8500,9500
scales=.1,.1 ### 学习率变动因子
[convolutional]
batch_normalize=1 ### BN
filters=32 ### 卷积核数目
size=3 ### 卷积核尺寸
stride=1 ### 卷积核步长
pad=1 ### pad
activation=leaky ### 激活函数
......
[convolutional]
size=1
stride=1
pad=1
filters=18 #3*(1+4+1)
activation=linear
[yolo]
mask = 6,7,8
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=1 #类别
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1 #1,如果显存很小,将random设置为0,关闭多尺度训练;
......
[convolutional]
size=1
stride=1
pad=1
filters=18 #3*(1+4+1)
activation=linear
[yolo]
mask = 3,4,5
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=1 #类别
num=9
jitter=.3
ignore_thresh = .5
truth_thresh = 1
random=1 #1,如果显存很小,将random设置为0,关闭多尺度训练;
......
[convolutional]
size=1
stride=1
pad=1
filters=18 #3*(1+4+1)
activation=linear
[yolo]
mask = 0,1,2
anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
classes=1 #类别
num=9
jitter=.3 # 数据扩充的抖动操作
ignore_thresh = .5 #文章中的阈值1
truth_thresh = 1 #文章中的阈值2
random=1 #1,如果显存很小,将random设置为0,关闭多尺度训练;
11、开始训练
1)运行命令:
$ ./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74
训练过程产生的文件保存在backup文件夹中。
yolov3-voc.backup可以继续训练
$ ./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc.backup
2)训练日志可视化
前提是训练过程中保存了训练日志xxx.log.
①运行测试时的命令改为:
$ ./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg weights/darknet53.conv.74 -gpu 0 | tee train_yolov3.log
训练结束后会生成train_yolov3.log脚本
② 在使用脚本绘制变化曲线之前,需要先使用extract_log.py脚本,格式化log,用生成的新的log文件供可视化工具绘图,格式化log的extract_log.py脚本如下(和生成的log文件同一目录)参考:(https://blog.csdn.net/qq_34806812/article/details/81459982)
extract_log.py:
# coding=utf-8
# 该文件用来提取训练log,去除不可解析的log后使log文件格式化,生成新的log文件供可视化工具绘图
import inspect
import os
import random
import sys
def extract_log(log_file,new_log_file,key_word):
with open(log_file, 'r') as f:
with open(new_log_file, 'w') as train_log:
#f = open(log_file)
#train_log = open(new_log_file, 'w')
for line in f:
# 去除多gpu的同步log
if 'Syncing' in line:
continue
# 去除除零错误的log
if 'nan' in line:
continue
if key_word in line:
train_log.write(line)
f.close()
train_log.close()
extract_log('train_yolov3.log','train_log_loss.txt','images')
extract_log('train_yolov3.log','train_log_iou.txt','IOU')
train_log_loss.txt内容:
train_log_iou.txt内容:
③在运行下面脚本:参考(https://blog.csdn.net/csdn_zhishui/article/details/85397380)
# -*- coding: utf-8 -*-
# @Time : 2018/12/30 16:26
# @Author : lazerliu
# @File : vis_yolov3_log.py
# @Func :yolov3 训练日志可视化,把该脚本和日志文件放在同一目录下运行。
import pandas as pd
import matplotlib.pyplot as plt
import os
# ==================可能需要修改的地方=====================================#
g_log_path = "train_yolov3.log" # 此处修改为你的训练日志文件名
# ==========================================================================#
def extract_log(log_file, new_log_file, key_word):
'''
:param log_file:日志文件
:param new_log_file:挑选出可用信息的日志文件
:param key_word:根据关键词提取日志信息
:return:
'''
with open(log_file, "r") as f:
with open(new_log_file, "w") as train_log:
for line in f:
# 去除多gpu的同步log
if "Syncing" in line:
continue
# 去除nan log
if "nan" in line:
continue
if key_word in line:
train_log.write(line)
f.close()
train_log.close()
def drawAvgLoss(loss_log_path):
'''
:param loss_log_path: 提取到的loss日志信息文件
:return: 画loss曲线图
'''
line_cnt = 0
for count, line in enumerate(open(loss_log_path, "rU")):
line_cnt += 1
result = pd.read_csv(loss_log_path, skiprows=[iter_num for iter_num in range(line_cnt) if ((iter_num < 500))],
error_bad_lines=False,
names=["loss", "avg", "rate", "seconds", "images"])
result["avg"] = result["avg"].str.split(" ").str.get(1)
result["avg"] = pd.to_numeric(result["avg"])
fig = plt.figure(1, figsize=(6, 4))
ax = fig.add_subplot(1, 1, 1)
ax.plot(result["avg"].values, label="Avg Loss", color="#ff7043")
ax.legend(loc="best")
ax.set_title("Avg Loss Curve")
ax.set_xlabel("Batches")
ax.set_ylabel("Avg Loss")
def drawIOU(iou_log_path):
'''
:param iou_log_path: 提取到的iou日志信息文件
:return: 画iou曲线图
'''
line_cnt = 0
for count, line in enumerate(open(iou_log_path, "rU")):
line_cnt += 1
result = pd.read_csv(iou_log_path, skiprows=[x for x in range(line_cnt) if (x % 39 != 0 | (x < 5000))],
error_bad_lines=False,
names=["Region Avg IOU", "Class", "Obj", "No Obj", "Avg Recall", "count"])
result["Region Avg IOU"] = result["Region Avg IOU"].str.split(": ").str.get(1)
result["Region Avg IOU"] = pd.to_numeric(result["Region Avg IOU"])
result_iou = result["Region Avg IOU"].values
# 平滑iou曲线
for i in range(len(result_iou) - 1):
iou = result_iou[i]
iou_next = result_iou[i + 1]
if abs(iou - iou_next) > 0.2:
result_iou[i] = (iou + iou_next) / 2
fig = plt.figure(2, figsize=(6, 4))
ax = fig.add_subplot(1, 1, 1)
ax.plot(result_iou, label="Region Avg IOU", color="#ff7043")
ax.legend(loc="best")
ax.set_title("Avg IOU Curve")
ax.set_xlabel("Batches")
ax.set_ylabel("Avg IOU")
if __name__ == "__main__":
loss_log_path = "train_log_loss.txt"
iou_log_path = "train_log_iou.txt"
if os.path.exists(g_log_path) is False:
exit(-1)
if os.path.exists(loss_log_path) is False:
extract_log(g_log_path, loss_log_path, "images")
if os.path.exists(iou_log_path) is False:
extract_log(g_log_path, iou_log_path, "IOU")
drawAvgLoss(loss_log_path)
drawIOU(iou_log_path)
plt.show()
12、测试单张图片:
测试时的cfg文件中的batch和subdivisions必须为1。
运行命令:
$ ./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights timg.jpeg
13、批量测试:
参考:https://blog.csdn.net/wangzy9766/article/details/88749696
测试时:Makefile中的opencv调为0。
1)用下面代码替换detector.c文件(example文件夹下)的void test_detector函数(注意有3处要改成自己的路径)
全部复制并代替,三处修改路径写对
此段代码来自https://blog.csdn.net/mieleizhi0522/article/details/79989754
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{
list *options = read_data_cfg(datacfg);
char *name_list = option_find_str(options, "names", "data/names.list");
char **names = get_labels(name_list);
image **alphabet = load_alphabet();
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
srand(2222222);
double time;
char buff[256];
char *input = buff;
float nms=.45;
int i=0;
while(1){
if(filename){
strncpy(input, filename, 256);
image im = load_image_color(input,0,0);
image sized = letterbox_image(im, net->w, net->h);
//image sized = resize_image(im, net->w, net->h);
//image sized2 = resize_max(im, net->w);
//image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
//resize_network(net, sized.w, sized.h);
layer l = net->layers[net->n-1];
float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
//printf("%d\n", nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
free_detections(dets, nboxes);
if(outfile)
{
save_image(im, outfile);
}
else{
save_image(im, "predictions");
#ifdef OPENCV
cvNamedWindow("predictions", CV_WINDOW_NORMAL);
if(fullscreen){
cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
}
show_image(im, "predictions");
cvWaitKey(0);
cvDestroyAllWindows();
#endif
}
free_image(im);
free_image(sized);
if (filename) break;
}
else {
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
list *plist = get_paths(input);
char **paths = (char **)list_to_array(plist);
printf("Start Testing!\n");
int m = plist->size;
if(access("/home/FENGsl/darknet/data/out",0)==-1)//"/home/FENGsl/darknet/data"修改成自己的路径
{
if (mkdir("/home/FENGsl/darknet/data/out",0777))//"/home/FENGsl/darknet/data"修改成自己的路径
{
printf("creat file bag failed!!!");
}
}
for(i = 0; i < m; ++i){
char *path = paths[i];
image im = load_image_color(path,0,0);
image sized = letterbox_image(im, net->w, net->h);
//image sized = resize_image(im, net->w, net->h);
//image sized2 = resize_max(im, net->w);
//image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
//resize_network(net, sized.w, sized.h);
layer l = net->layers[net->n-1];
float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("Try Very Hard:");
printf("%s: Predicted in %f seconds.\n", path, what_time_is_it_now()-time);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
//printf("%d\n", nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
free_detections(dets, nboxes);
if(outfile){
save_image(im, outfile);
}
else{
char b[2048];
sprintf(b,"/home/FENGsl/darknet/data/out/%s",GetFilename(path));//"/home/FENGsl/darknet/data"修改成自己的路径
save_image(im, b);
printf("save %s successfully!\n",GetFilename(path));
#ifdef OPENCV
cvNamedWindow("predictions", CV_WINDOW_NORMAL);
if(fullscreen){
cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
}
show_image(im, "predictions");
cvWaitKey(0);
cvDestroyAllWindows();
#endif
}
free_image(im);
free_image(sized);
if (filename) break;
}
}
}
}
2).在前面添加GetFilename(char p)函数(注意后面的注释)
全部复制(包括头文件)
此段代码来自https://blog.csdn.net/mieleizhi0522/article/details/79989754
#include "darknet.h"
#include <sys/stat.h>
#include<stdio.h>
#include<time.h>
#include<sys/types.h>
static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};
char *GetFilename(char *p)
{
static char name[20]={""};
char *q = strrchr(p,'/') + 1;
strncpy(name,q,6);//注意后面的6,如果你的测试集的图片的名字字符(不包括后缀)是其他长度,请改为你需要的长度(官方的默认的长度是6)
return name;
}
3)重新进行编译
$ make clean
$ make
4)读取测试图片的准备
如果要另找图片进行测试,不用2007_test.txt进行测试需要:
①新建要测试图像的文件夹
②制作自己的txt:
dataset.py程序:
import os
path = ['/home/yuxin/darknet']
f = open('input_data.txt','w')
for path in path:
p = os.path.abspath(path) + '/pictures_person'
filenames = os.listdir(p)
for filename in filenames:
im_path = p + '/' + filename
print(im_path)
f.write(im_path + '\n')
f.close()
input_data.txt中的内容如下:
5)执行批量测试命令
./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights
6)输入Image Path:
14、生成预测结果:
执行命令:
$ ./darknet detector valid cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights -out“”
结果生成在<data_cfg>的指定的目录下以<out_file>开头的若干文件中,若<data_cfg>没有指定results,那么默认为<darknet_root>/results
默认情况下,结果会得到在results下的各类别的txt检测结果文件
每行代表一个预测框,分别为:不带路径与后缀的图片名,置信度分数,四个绝对坐标值
此处的-out后面直接" "即可,因为不论你写什么,他都会根据voc_names中配置的类名来生成对应txt,有几个类就生成几个txt,并且会将类名自动写入txt文件名中。
person.txt内容:
15、计算mAP
采用第三方工具计算mAP
下载第三方库:
$ git clone https://github.com/LianjiLi/yolo-compute-map.git
voc_eval.py程序如下(使用faster RCNN的voc_eval进行计算,只是把最后的返回值改成return ap):
voc_eval.py程序如下(使用faster RCNN的voc_eval进行计算,只是把最后的返回值改成return ap):
compute_mAP.py程序如下:
计算单类时:
from voc_eval import voc_eval
print voc_eval('/home/yuxin/darknet/results/{}.txt', '/home/yuxin/darknet/VOCdevkit/VOC2007/Annotations/{}.xml', '/home/yuxin/darknet/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'person', '.')
计算多类时:
from voc_eval import voc_eval
map_ = 0
classnames = ['person'] #填写自己的类别
for classname in classnames:
ap = voc_eval('/home/yuxin/darknet/results/{}.txt', '/home/yuxin/darknet/VOCdevkit/VOC2007/Annotations/{}.xml', '/home/yuxin/darknet/VOCdevkit/VOC2007/ImageSets/Main/test.txt', classname, '.')
map_ += ap
print ('%-20s' % (classname + '_ap:')+'%s' % ap)
map = map_/len(classnames)
print ('%-20s' % 'map:' + '%s' % map)
或者:( https://blog.csdn.net/m0_37857151/article/details/86605087)
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/yuxin/darknet/results/{}.txt', '/home/yuxin/darknet/VOCdevkit/VOC2007/Annotations/{}.xml', '/home/yuxin/darknet/VOCdevkit/VOC2007/ImageSets/Main/test.txt', class_name, '.')
print("{} :\t {} ".format(class_name, ap))
mAP.append(ap)
mAP = tuple(mAP)
print("***************************")
print("mAP :\t {}".format( float( sum(mAP)/len(mAP)) ))
上述程序在python2下运行:
16、训练之后计算召回率:
1) 修改detector.c文件中validate_detector_recall函数
list *plist = get_paths(“data/coco_val_5k.list”);
换成:
list *plist=get_paths("/home/…/darknet/train.txt");
2)for(k = 0; k < l.wl.hl.n; ++k){
换成:
for(k = 0; k < nboxes; ++k){
重新 make
3)执行命令:
$ ./darknet detector recall cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_final.weights
参考博客:
https://www.cnblogs.com/pprp/p/9525508.html
https://blog.csdn.net/wangzy9766/article/details/88749696
https://www.cnblogs.com/xieqi/p/9818056.html
https://blog.csdn.net/m0_37857151/article/details/86605087
https://blog.csdn.net/csdn_zhishui/article/details/85397380