本文主要是介绍Darknet训练分类器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Table of Contents
一:安装darknet
二:数据处理,准备train.list test.list labels.txt
三:制作数据集配置文件
四:制作网络
五:开始训练
六:测试及验证
七:中断后重新训练
一:安装darknet
git clone https://github.com/AlexeyAB/darknet/
cd darknet
make
如果有GPU并且安装了cuda,那么可以修改makefile,将cuda和cudnn修改为1.然后再make编译。
二:数据处理,准备train.list test.list labels.txt
这个不像caffe里面那样,train.txt里面每一行是图片路径名字,空格,然后是标签0 1 2. 在darknet里面,标签名字是包含在图片名字里面的,有的说图片名字是 类别_随机数.png,有的说是 随机数_类别.png,其实这两种情况都是可以的,因为我们可以从源码中看到这个函数
void fill_truth(char *path, char **labels, int k, float *truth)
{int i;memset(truth, 0, k*sizeof(float));int count = 0;for(i = 0; i < k; ++i){if(strstr(path, labels[i])){truth[i] = 1;++count;//printf("%s %s %d\n", path, labels[i], i);}}if(count != 1 && (k != 1 || count != 0)) printf("Too many or too few labels: %d, %s\n", count, path);
}
所以,我们甚至不用修改图片名字,我们可以把每一个类别的图片放到一个单独的文件夹里面,然后把每个文件夹名字用label名字对应起来,例如文件夹名字为dog cat,这里的名字要和label.txt里面的对应起来。
由于习惯了caffe里面的方式,所以我这里是把每一类都放到一个单独的文件夹里面,然后这些文件夹统一放到images文件夹里面,然后用下面的脚本生成train.list test.list labels.txt,Python脚本如下:
import os
import randombase_dir = "/data/chw/images"
f_train = open("./train.list", 'w')
#f_val = open("./val.list", 'w')
f_test = open("./test.list", 'w')
f_label = open("./labels.txt", "w")for root, dirs, files in os.walk(base_dir):i = 0for dirname in dirs:label_name = dirname#f_label.write(label_name + " " + str(i) + "\n")f_label.write(label_name + "\n")path = os.path.join(root, dirname)for jpeg_name in os.listdir(path):jpeg_path = os.path.join(path, jpeg_name)feed = random.randint(0, 10)if feed <= 9:#f_train.write(jpeg_path + " " + str(i) + "\n")f_train.write(jpeg_path + "\n")if feed == 10:#f_test.write(jpeg_path + " " + str(i) + "\n")f_test.write(jpeg_path + "\n") i=i + 1f_train.close()
#f_val.close()
f_test.close()
f_label.close()
简单附一下生成的train.list test.list labels.txt的内容(这里的aaaaaa bbbbbb cccccc就是类别名,类似于cat dog):
train.list和test.list:
/data/chw/images/gggggg/351.jpg
/data/chw/images/gggggg/621.jpg
/data/chw/images/gggggg/780.jpg
/data/chw/images/gggggg/1897.jpg
/data/chw/images/gggggg/494.jpg
/data/chw/images/gggggg/159.jpg
labels.txt:
gggggg
bbbbbb
dddddd
jjjjjj
ffffff
eeeeee
iiiiii
aaaaaa
cccccc
hhhhhh
三:制作数据集配置文件
在 darknet/cfg 目录下新建一个cspdarknet53_chw.data文件,文件内容如下
classes=10
train = /data/chw/train.list
valid = /data/chw/test.list
labels = /data/chw/labels.txt
backup = /data/chw/models
top=5
文件解释:
classes=10 #分类数量,种类数。
train = /data/chw/train.list #里面是训练图片的路径和名字
valid = /data/chw/test.list #里面是验证图片的路径和名字
labels = /data/chw/labels.txt #里面每行是一个标签,例如cat dog
backup = /data/chw/models #训练产生的权重文件保存到这里
top=5 #用来指示测试时看top几的准确率
四:制作网络
就是写一个cfg文件,类似于caffe里面的protext,下面的是将yolov4的基础网络拿出来,也就是前面的104layer拿出来,然后加上了average pooling, conv ,softmax三层得到的cfg文件。
[net]
# Testing
#batch=1
#subdivisions=1
# Training
batch=64
subdivisions=32
width=416
height=416
channels=3
momentum=0.949
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1learning_rate=0.001305
burn_in=2000
max_batches = 500000
policy=steps
steps=67200,75600
scales=.1,.1#cutmix=1
mosaic=1#:104x104 54:52x52 85:26x26 104:13x13 for 416[convolutional]
batch_normalize=1
filters=32
size=3
stride=1
pad=1
activation=mish# Downsample[convolutional]
batch_normalize=1
filters=64
size=3
stride=2
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[route]
layers = -2[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=32
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[route]
layers = -1,-7[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish# Downsample[convolutional]
batch_normalize=1
filters=128
size=3
stride=2
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[route]
layers = -2[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=64
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=mish[route]
layers = -1,-10[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish# Downsample[convolutional]
batch_normalize=1
filters=256
size=3
stride=2
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[route]
layers = -2[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=mish[route]
layers = -1,-28[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish# Downsample[convolutional]
batch_normalize=1
filters=512
size=3
stride=2
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[route]
layers = -2[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=mish[route]
layers = -1,-28[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish# Downsample[convolutional]
batch_normalize=1
filters=1024
size=3
stride=2
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish[route]
layers = -2[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish [convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=mish[shortcut]
from=-3
activation=linear[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=mish[route]
layers = -1,-16[convolutional]
batch_normalize=1
filters=1024
size=1
stride=1
pad=1
activation=mish[avgpool][convolutional]
filters=10
size=1
stride=1
pad=1
activation=linear[softmax]
groups=1
五:开始训练
./darknet classifier train cfg/cspdarknet53_chw.data cfg/cspdarknet53_chw.cfg -gpus 0,1
六:测试及验证
验证集整体准确率测试
./darknet classifier valid cfg/cspdarknet53_chw.data cfg/cspdarknet53_chw.cfg ../models/cspdarknet53_chw_34000.weights
单张图片测试
./darknet classifier predict cfg/cspdarknet53_chw.data cfg/cspdarknet53_chw.cfg ../models/cspdarknet53_chw_34000.weights ../temp0.jpg
七:中断后重新训练
./darknet classifier train cfg/cspdarknet53_chw.data cfg/cspdarknet53_chw.cfg models/cspdarknet53_chw.backup -gpus 0,1
这篇关于Darknet训练分类器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!