Windows AB版Yolov3 一键生成训练所需配置文件

本文介绍了一种在Windows上快速生成Yolov3训练所需配置文件的方法,包括修改源码、创建脚本文件和配置文件的详细步骤,确保训练过程顺利进行。通过运行提供的Python脚本和批处理文件,可以简化训练前的准备工作。
摘要由CSDN通过智能技术生成

从一开始接触 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 &
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值