当前位置:   article > 正文

yolov8超详细从配置环境到训练测试_yolov8环境配置

yolov8环境配置

一、用anaconda配的环境

1、Anaconda安装官网自查
2、Anaconda创建环境conda create -n 环境名 python=3.7
image.png
注意:如果出现conda命令不出来,请重新配置conda的电脑环境变量
参考文章:(35条消息) ‘conda‘不是内部或外部命令,也不是可运行的程序或批处理文件。_conda’ 不是内部或外部命令_北极的三哈的博客-CSDN博客
Anaconda安装包慢记得换源
3、Cuda安装:官网:CUDA Toolkit 11.3 Downloads | NVIDIA Developer下载11.3版本
4、pytorch安装1.11版本
在开始中找到anaconda3打开anaconda prompt并进入需要安装的环境
image.png
命令:activate 环境名
输入安装指令:

conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch
  • 1

5、下载yolov8源码:
mirrors
/ ultralytics / ultralytics · GitCode

我的下载目录是D:\workPlace\ultralytics-main\requirements.txt
cd 到下载目录(D:\workPlace\ultralytics-main\requirements.txt)
使用pip install -r requirements.txt进行配置
image.png
6、可能会出现的环境配置问题:
没有fbprophet包
image.png

conda install -c conda-forge fbprophet
  • 1

pandas存在的bug
image.png

pip install pandas==1.3.0
  • 1

7、pycharm连接anaconda环境
用pycharm打开下好的yolov8项目文件
选择file->settings->project:->python interpreter
1684993418620.png
选择已经创建好的anaconda环境下得python.exe文件
image.png
8、数据标注环境:

pip install labelImg
  • 1

二、数据集准备

1、用anaconda加载labelimg工具进行数据标注

activate pytorch#你的环境名字
labelimg#进入标注工具界面
  • 1
  • 2

选择要标注的数据目录:
image.png
更改数据的标注结果格式:通过点击这个按钮选择输出为.xml文件或者.txt文件
image.png
2、数据调整成官方给定样式
在官方代码下新建data文件目录
文件夹下创建这几个目录images中放置所有标注原图,xml中放置标注好的.xml文件
image.png

import os
import random
import argparse
 
parser = argparse.ArgumentParser()
# xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
parser.add_argument('--xml_path', default='./ultralytics/data/xml', type=str, help='input xml label path')
# 数据集的划分,地址选择自己数据下的ImageSets/Main
parser.add_argument('--txt_path', default='./ultralytics/data/dataSet', type=str, help='output txt label path')
opt = parser.parse_args()
 
trainval_percent = 1.0
train_percent = 0.9
xmlfilepath = opt.xml_path
txtsavepath = opt.txt_path
total_xml = os.listdir(xmlfilepath)
if not os.path.exists(txtsavepath):
    os.makedirs(txtsavepath)
 
num = len(total_xml)
list_index = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list_index, tv)
train = random.sample(trainval, tr)
 
file_trainval = open(txtsavepath + '/trainval.txt', 'w')
file_test = open(txtsavepath + '/test.txt', 'w')
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/val.txt', 'w')
 
for i in list_index:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        file_trainval.write(name)
        if i in train:
            file_train.write(name)
        else:
            file_val.write(name)
    else:
        file_test.write(name)
 
file_trainval.close()
file_train.close()
file_val.close()
file_test.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

在dataset文件下生成数据集划分的txt文件
image.png
xml文件转txt文件并存在labels目录中

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
 
# 数据标签
classes = ['UNK']  # 需要修改
 
 
def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    if w >= 1:
        w = 0.99
    if h >= 1:
        h = 0.99
    return (x, y, w, h)
 
 
def convert_annotation(rootpath, xmlname):
    xmlpath = rootpath + '/xml'
    xmlfile = os.path.join(xmlpath, xmlname)
    with open(xmlfile, "r", encoding='UTF-8') as in_file:
        txtname = xmlname[:-4] + '.txt'
        print(txtname)
        txtpath = rootpath + '/labels'  # 生成的.txt文件会被保存在labels目录下
        if not os.path.exists(txtpath):
            os.makedirs(txtpath)
        txtfile = os.path.join(txtpath, txtname)
        with open(txtfile, "w+", encoding='UTF-8') as out_file:
            tree = ET.parse(in_file)
            root = tree.getroot()
            size = root.find('size')
            w = int(size.find('width').text)
            h = int(size.find('height').text)
            out_file.truncate()
            for obj in root.iter('object'):
                difficult = obj.find('difficult').text
                cls = obj.find('name').text
                if cls not in classes or int(difficult) == 1:
                    continue
                cls_id = classes.index(cls)
                xmlbox = obj.find('bndbox')
                b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
                     float(xmlbox.find('ymax').text))
                bb = convert((w, h), b)
                out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
 
 
if __name__ == "__main__":
    rootpath = './ultralytics/data'  ##需要修改的地方改成你的路径
    xmlpath = rootpath + '/xml'
    list = os.listdir(xmlpath)
    for i in range(0, len(list)):
        path = os.path.join(xmlpath, list[i])
        if ('.xml' in path) or ('.XML' in path):
            convert_annotation(rootpath, list[i])
            print('done', i)
        else:
            print('not xml file', i)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

将所有数据转换成官方给定数据目录:
image.png


import os,shutil
rootpath="./ultralytics/data/"#待修改路径
imgtrain=rootpath+"images/train/"
imgval=rootpath+"images/val/"
labeltrain=rootpath+"labels/train/"
labelval=rootpath+"labels/val/"
if not os.path.exists(imgtrain):
    os.makedirs(imgtrain)
if not os.path.exists(imgval):
    os.makedirs(imgval)
if not os.path.exists(labeltrain):
    os.makedirs(labeltrain)
if not os.path.exists(labelval):
    os.makedirs(labelval)
 
f = open(rootpath+"dataSet/train.txt","r")
lines = f.readlines()
for i in lines:
    shutil.move(rootpath+"images/"+str(i).replace('\n','')+".jpg",imgtrain+str(i).replace('\n','')+".jpg")
    shutil.move(rootpath + "labels/" + str(i).replace('\n', '') + ".txt", labeltrain + str(i).replace('\n', '') + ".txt")
 
f = open(rootpath+"dataSet/val.txt","r")
lines = f.readlines()
for i in lines:
    shutil.move(rootpath+"images/"+str(i).replace('\n','')+".jpg",imgval+str(i).replace('\n','')+".jpg")
    shutil.move(rootpath + "labels/" + str(i).replace('\n', '') + ".txt", labelval + str(i).replace('\n', '') + ".txt")
shutil.move(rootpath+"dataSet/train.txt",rootpath+"train.txt")
shutil.move(rootpath+"dataSet/trainval.txt",rootpath+"trainval.txt")
shutil.move(rootpath+"dataSet/test.txt",rootpath+"test.txt")
shutil.move(rootpath+"dataSet/val.txt",rootpath+"val.txt")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

最后,把当前目录下所有文件放到官方代码的datasets里面
image.png
image.png

三、修改参数配置及训练

1、参数修改

训练、测试、导出参数文件目录:./ultralytics-main/ultralytics/yolo/cfg/default.yaml
如果训练时报错:

BrokenPipeError: [Errno 32] Broken pipe

解决:将参数文件中的workers改成0
image.png

训练数据参数文件,目录:./ultralytics-main/ultralytics/datasets
随便选个文件
image.png

train: images/train#改成自己放置数据的目录
val: images/val
nc: num#你的类别数量
#names中类别格式
	0:["类别名称1"]
	1:["类别名称2"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

image.png

2、训练

注:如果命令行无法运行可以装一下git在电脑上
修改类别和模型文件路径后可以直接运行
训练代码:./ultralytics-main/ultralytics/yolo/v8/detect/train.py
image.png
命令行运行:

yolo task=detect mode=train model='yolov8.yaml' data='ultralytics/datasets/自己的数据配置文件.yaml'
  • 1

训练时可能会有报错:
image.png
找到相应的settings.yaml文件
把第一行改成相应的数据目录
image.png

3、测试:

./ultralytics-main/ultralytics/yolo/v8/detect/predict.py
修改训练好的模型权重文件和被测试数据目录后可直接运行。
image.png
命令行运行:

 yolo predict model='模型文件' source='图片目录'
  • 1

yolo predict model=模型文件 source=图片目录
所有的训练和测试结果都保存在:./ultralytics-main/ultralytics/yolo/v8/detect/runs

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/373231
推荐阅读
相关标签
  

闽ICP备14008679号