YOLOV8

YOLOV8

一、环境准备

1、创建conda环境

conda create -n yolov8 python=3.10

2、安装pytorch

​ 地址:PyTorch

①CPU
windows-pip:
pip3 install torch torchvision torchaudio
windows-conda:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
②GPU
windows-pip:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118  #CUDA11.8
windows-conda:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia #CUDA11.8
注:
注意安装的pytorch版本和torchvision版本要对应
torch              2.3.0+cu118
torchaudio         2.3.0+cu118
torchvision        0.18.0+cu118
下载地址:https://download.pytorch.org/whl/torch_stable.html

查找对应版本的torchvision wheel文件:在页面上,浏览或使用Ctrl+F搜索“torchvision”,寻找适合你配置的版本。特别是寻找一个版本号匹配(例如torchvision-0.18.0),同时标有cp310(代表Python 3.10)和cu118(代表CUDA 11.8)的wheel文件。

安装:
pip install <whl文件路径>

3、下载yolov8

​ 地址:ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite (github.com)

进入yolov8的环境中:
conda activate yolov8
安装依赖:
pip install ultralytics
测试环境:
yolo predict model=yolov8n.pt source='<测试图片的地址>'
# 默认图片的路径:ultralytics-main\ultralytics\assets
输出路径为:ultralytics-main\runs

二、数据集准备

1、 创建文件夹用来存放数据集

mydata
    ├─dataset #用来存放读取到的图片的名称的文本
    ├─images  #存放图片
    ├─labels  #存放txt标签
    └─xml     #存放xml标签

2、获取所有xml文件中的类名

import os
import xml.etree.ElementTree as ET

def extract_names_from_xml(file_path):
    """从单个XML文件中提取所有name字段"""
    tree = ET.parse(file_path)
    root = tree.getroot()
    names = set()  # 使用集合去重
    for obj in root.findall('object'):
        name = obj.find('name').text
        names.add(name)  # 添加到集合中自动去重
    return names

def extract_names_from_folder(folder_path):
    """从文件夹中所有XML文件提取所有name字段"""
    all_names = set()  # 存储存所有文件的name集合
    for filename in os.listdir(folder_path):
        if filename.endswith('.xml'):
            file_path = os.path.join(folder_path, filename)
            names = extract_names_from_xml(file_path)
            all_names.update(names)  # 更新集合
    return all_names

if __name__ == "__main__":
    folder_path = '<xml>'  # 替换为你的XML文件夹路径
    all_unique_names = extract_names_from_folder(folder_path)
    print("Unique names across all XML files:")
    for name in sorted(all_unique_names):
        print(name)

3、分割训练集、测试集

'''运行后会在dataset中生成四个文本'''

#-----------------------------分割线----------------------------------#

# coding:utf-8

import os
import random
import argparse

parser = argparse.ArgumentParser()
# xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
parser.add_argument('--xml_path', default='xml文件夹', type=str, help='input xml label path')
# 数据集的划分,地址选择自己数据下的ImageSets/Main
parser.add_argument('--txt_path', default='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()

4、转换xml到txt(yolo系列使用的标签都是txt格式[<类索引><锚框中心点坐标><锚框相对于图片的尺寸比例>])

'''运行后会在labels生成文件同时会在mydata中生成三个文本文件(train,test,val)'''

#-----------------------------分割线----------------------------------#

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
from os import getcwd
 
sets = ['train', 'val', 'test']
classes = ["a", "b"]   # 改成自己的类别
abs_path = os.getcwd()
print(abs_path)
 
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
    return x, y, w, h
 
def convert_annotation(image_id):
    in_file = open('data/mydata/xml/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open('data/mydata/labels/%s.txt' % (image_id), 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
    for obj in root.iter('object'):
        # difficult = obj.find('difficult').text
        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))
        b1, b2, b3, b4 = b
        # 标注越界修正
        if b2 > w:
            b2 = w
        if b4 > h:
            b4 = h
        b = (b1, b2, b3, b4)
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
 
wd = getcwd()
for image_set in sets:
    if not os.path.exists('data/mydata/labels/'):
        os.makedirs('data/mydata/labels/')
    image_ids = open('data/mydata/dataset/%s.txt' % (image_set)).read().strip().split()
    list_file = open('mydata/%s.txt' % (image_set), 'w')
    for image_id in image_ids:
        list_file.write(abs_path + '/mydata/images/%s.jpg\n' % (image_id))
        convert_annotation(image_id)
    list_file.close()

三、配置文件

1、复制coco.yaml改名为mydata.yaml

路径:ultralytics-main\ultralytics\cfg\datasets

2、修改内容

# Ultralytics YOLO 🚀, AGPL-3.0 license
# COCO 2017 dataset https://cocodataset.org by Microsoft
# Documentation: https://docs.ultralytics.com/datasets/detect/coco/
# Example usage: yolo train data=coco.yaml
# parent
# ├── ultralytics
# └── datasets
#     └── coco  ← downloads here (20.1 GB)

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco # dataset root dir
train: train2017.txt # train images (relative to 'path') 118287 images
val: val2017.txt # val images (relative to 'path') 5000 images
test: test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

# Classes
names:
  0: person
  1: bicycle
  2: car
  3: motorcycle
  4: airplane
  5: bus
  6: train
  7: truck
  8: boat
  9: traffic light
  10: fire hydrant
  11: stop sign
  12: parking meter
  13: bench
  14: bird
  15: cat
  16: dog
  17: horse
  18: sheep
  19: cow
  20: elephant
  21: bear
  22: zebra
  23: giraffe
  24: backpack
  25: umbrella
  26: handbag
  27: tie
  28: suitcase
  29: frisbee
  30: skis
  31: snowboard
  32: sports ball
  33: kite
  34: baseball bat
  35: baseball glove
  36: skateboard
  37: surfboard
  38: tennis racket
  39: bottle
  40: wine glass
  41: cup
  42: fork
  43: knife
  44: spoon
  45: bowl
  46: banana
  47: apple
  48: sandwich
  49: orange
  50: broccoli
  51: carrot
  52: hot dog
  53: pizza
  54: donut
  55: cake
  56: chair
  57: couch
  58: potted plant
  59: bed
  60: dining table
  61: toilet
  62: tv
  63: laptop
  64: mouse
  65: remote
  66: keyboard
  67: cell phone
  68: microwave
  69: oven
  70: toaster
  71: sink
  72: refrigerator
  73: book
  74: clock
  75: vase
  76: scissors
  77: teddy bear
  78: hair drier
  79: toothbrush

# Download script/URL (optional)
download: |
  from ultralytics.utils.downloads import download
  from pathlib import Path

  # Download labels
  segments = True  # segment or box labels
  dir = Path(yaml['path'])  # dataset root dir
  url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/'
  urls = [url + ('coco2017labels-segments.zip' if segments else 'coco2017labels.zip')]  # labels
  download(urls, dir=dir.parent)
  # Download data
  urls = ['http://images.cocodataset.org/zips/train2017.zip',  # 19G, 118k images
          'http://images.cocodataset.org/zips/val2017.zip',  # 1G, 5k images
          'http://images.cocodataset.org/zips/test2017.zip']  # 7G, 41k images (optional)
  download(urls, dir=dir / 'images', threads=3)
  
  #———————————————————————————————————分割线—————————————————————————————————————————————#
修改其中的
path: .../mydata 
train: ./train.txt 
val: ./val.txt 
test: test-dev2017.txt # 在我的项目中注释了

#————————————#
class更改为你的类名,顺序要和上面二、4中的代码中的class顺序相同

3、准备预训练模型

官网地址:ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite (github.com)

在这里插入图片描述

四、开始训练

yolo task=detect mode=train model=<yolov8n.pt路径> data=<mydata.yaml路径> epochs=3 batch=16
参考文章:
数据处理部分参考文章:原作者:Ai追随者  原文链接:https://blog.csdn.net/qq_40716944/article/details/128648001
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值