从一开始接触 yolov3 到现在,最折腾的就是训练时的配置文件了,没什么技术含量,懒得折腾,就写了一个脚本,记录一下
还没有搭建好 Yolov3 的朋友可以参考这篇博客 : https://blog.csdn.net/weixin_39954922/article/details/105785460?fps=1&locationNum=2
首先找到 detector.c 文件,我的是在 darknet\src 目录下,然后找到 calc_anchors 函数,将最后一行注释掉,我的是在第 1547 行处
再生成一下就 OK (为什么要改这里,后面会说)
将生成好的 darknet.exe 文件放在 (your_yolov3_path)\darknet\build\darknet\x64 文件夹下
再创建 demo_cmd 和 test_photo 这两个文件夹 (后续可以根据代码自行修改)
进入 demo_cmd 文件夹下 分别创建 make_cfg.py make_file.py Once.py 这三个文件
代码分别如下:
make_cfg.py
import shutil
import re
class Base:
name_cfg = ''
test_name_cfg = ''
name_anchors = ''
eight_batches = ''
nine_batches = ''
fif = ''
classnum = ''
max_batches = ''
def inif(name, max_batches, classnum):
Base.name_cfg = './' + name + '/' + name + '.cfg'
Base.test_name_cfg = './' + name + '/' + name + '_test.cfg'
Base.name_anchors = './' + name + '/' + 'anchors.txt'
Base.max_batches = max_batches
Base.eight_batches = int(max_batches) * 0.8
Base.nine_batches = int(max_batches) * 0.9
Base.classnum = str(classnum)
Base.fif = str((int(classnum) + 5) * 3)
# 替换文件参数
def Replace(cfg, old, new):
f = open(cfg, 'r')
alllines = f.readlines()
f.close()
f = open(cfg, 'w+')
for eachline in alllines:
a = re.sub(old, new, eachline)
f.writelines(a)
f.close()
def start_cfg():
# 复制文件
shutil.copyfile('yolov3-giou.cfg', Base.name_cfg)
with open(Base.name_anchors) as f: # 需要重新打开文本进行读取
for line in f:
anchors = line.rstrip() # 删除字符串末尾的空白
Replace(Base.name_cfg, 'max_batches = 100400', 'max_batches = ' + Base.max_batches)
Replace(Base.name_cfg, 'steps=40000,45000', 'steps=' + str(int(Base.eight_batches)) + ',' + str(int(Base.nine_batches)))
Replace(Base.name_cfg, 'classes=20', 'classes=' + Base.classnum)
Replace(Base.name_cfg, 'filters=75', 'filters=' + Base.fif)
Replace(Base.name_cfg, 'anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326',
'anchors=' + anchors)
# print(anchors)
shutil.copyfile(Base.name_cfg, Base.test_name_cfg)
Replace(Base.test_name_cfg, '# batch=1', 'batch=1')
Replace(Base.test_name_cfg, '# subdivisions=1', 'subdivisions=1')
Replace(Base.test_name_cfg, 'batch=64', '# batch=64')
Replace(Base.test_name_cfg, 'subdivisions=16', '# subdivisions=16')
print("OK")
make_file.py
import os
import shutil
class Path:
mkpath = ''
data_path = ''
cfg_path = ''
test_cfg_path = ''
names_path = ''
backup_path = ''
calc_path = ''
copy_path = ''
train_path = ''
test_path = ''
train_txt_path = ''
test_txt_path = ''
def init(name):
Path.mkpath = "\\" + name + "\\"
Path.data_path = Path.mkpath + name + ".data"
Path.cfg_path &