从YOLOv5到YOLOv8(0.52)

该教程适用于已有YOLOv5基础的读者,介绍了如何在YOLOv8环境中直接使用YOLOv5的设置,包括代码文件的集成、超参数的修改,特别是超参数集中在default.yaml文件中,并提供了在训练时覆盖超参数的方法。虽然结构略有不同,但适应后能提升开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每次代码更新版本都要费眼睛去适应,写个教程方便他人


前言

这里是写给有YOLOv5基础的,因为v5的新手教程已经很多了,v8和v5的源码形式上差不多,只是调用的时候有点区别而已。


YOLOv8 github源码

一、直接用v5的环境

v8的环境和v5没什么主要区别,待会儿跑一遍,缺什么补什么就好。

二、代码文件

下载源文件,不用全部解压,只需要将子文件ultralytics到你的项目文件夹下。例如:
文件示例
这里的main_train.py文件是我自己创的,为的就是运行训练,而且代码也相当简单:

from ultralytics import YOLO

if __name__ == '__main__':
    # 加载模型
    # model = YOLO("yolov8n.yaml")
    model = YOLO("./yolov8n.pt")  # 用初始化权重
    results = model.train(data="./coco128.yaml")  # 载入数据集
    # results = model.val()  # 在验证集上评估模型性能

这里的data.yaml跟YOLOv5是一模一样的,不用改

二、超参数修改

1.yaml文件

v8把所有的参数放在ultralytics\yolo\cfg\default.yaml文件下了,相当于把v5的cfg.yaml和paser堆到了一起,同时还包含着detect、export和tracing(这个v5没有)的参数。总之就是弄到一块去了:

# Ultralytics YOLO 🚀, GPL-3.0 license
# Default training settings and hyperparameters for medium-augmentation COCO training

task: detect  # inference task, i.e. detect, segment, classify
mode: train  # YOLO mode, i.e. train, val, predict, export

# Train settings -------------------------------------------------------------------------------------------------------
model:  # path to model file, i.e. yolov8n.pt, yolov8n.yaml
data:  # path to data file, i.e. coco128.yaml
epochs: 100  # number of epochs to train for
patience: 50  # epochs to wait for no observable improvement for early stopping of training
batch: 16  # number of images per batch (-1 for AutoBatch)
imgsz: 640  # size of input images as integer or w,h
save: True  # save train checkpoints and predict results
save_period: -1 # Save checkpoint every x epochs (disabled if < 1)
cache: False  # True/ram, disk or False. Use cache for data loading
device: # device to run on, i.e. cuda device=0 or device=0,1,2,3 or device=cpu
workers:  # number of worker threads for data loading (per RANK if DDP)
project: runs # project name
name: FT # experiment name, results saved to 'project/name' directory
exist_ok: False  # whether to overwrite existing experiment
pretrained: False  # whether to use a pretrained model
optimizer: SGD  # optimizer to use, choices=['SGD', 'Adam', 'AdamW', 'RMSProp']
verbose: True  # whether to print verbose output
seed: 0  # random seed for reproducibility
deterministic: True  # whether to enable deterministic mode
single_cls: False  # train multi-class data as single-class
image_weights: False  # use weighted image selection for training
rect: False  # support rectangular training if mode='train', support rectangular evaluation if mode='val'
cos_lr: False  # use cosine learning rate scheduler
close_mosaic: 10  # disable mosaic augmentation for final 10 epochs
resume: False  # resume training from last checkpoint
# Segmentation
overlap_mask: True  # masks should overlap during training (segment train only)
mask_ratio: 4  # mask downsample ratio (segment train only)
# Classification
dropout: 0.0  # use dropout regularization (classify train only)

# Val/Test settings ----------------------------------------------------------------------------------------------------
val: True  # validate/test during training
split: val  # dataset split to use for validation, i.e. 'val', 'test' or 'train'
save_json: False  # save results to JSON file
save_hybrid: False  # save hybrid version of labels (labels + additional predictions)
conf: 0.001 # object confidence threshold for detection (default 0.25 predict, 0.001 val)
iou: 0.7  # intersection over union (IoU) threshold for NMS
max_det: 300  # maximum number of detections per image
half: False  # use half precision (FP16)
dnn: False  # use OpenCV DNN for ONNX inference
plots: True  # save plots during train/val

# Prediction settings --------------------------------------------------------------------------------------------------
source:  # source directory for images or videos
show: False  # show results if possible
save_txt: False  # save results as .txt file
save_conf: False  # save results with confidence scores
save_crop: False  # save cropped images with results
hide_labels: False  # hide labels
hide_conf: False  # hide confidence scores
vid_stride: 1  # video frame-rate stride
line_thickness: 3  # bounding box thickness (pixels)
visualize: False  # visualize model features
augment: False  # apply image augmentation to prediction sources
agnostic_nms: False  # class-agnostic NMS
classes:  # filter results by class, i.e. class=0, or class=[0,2,3]
retina_masks: False  # use high-resolution segmentation masks
boxes: True  # Show boxes in segmentation predictions

# Export settings ------------------------------------------------------------------------------------------------------
format: onnx  # format to export to
keras: False  # use Keras
optimize: False  # TorchScript: optimize for mobile
int8: False  # CoreML/TF INT8 quantization
dynamic: False  # ONNX/TF/TensorRT: dynamic axes
simplify: False  # ONNX: simplify model
opset:  # ONNX: opset version (optional)
workspace: 4  # TensorRT: workspace size (GB)
nms: False  # CoreML: add NMS

# Hyperparameters ------------------------------------------------------------------------------------------------------
lr0: 0.01  # initial learning rate (i.e. SGD=1E-2, Adam=1E-3)
lrf: 0.01  # final learning rate (lr0 * lrf)
momentum: 0.937  # SGD momentum/Adam beta1
weight_decay: 0.0005  # optimizer weight decay 5e-4
warmup_epochs: 3.0  # warmup epochs (fractions ok)
warmup_momentum: 0.8  # warmup initial momentum
warmup_bias_lr: 0.1  # warmup initial bias lr
box: 7.5  # box loss gain
cls: 0.5  # cls loss gain (scale with pixels)
dfl: 1.5  # dfl loss gain
fl_gamma: 0.0  # focal loss gamma (efficientDet default gamma=1.5)
label_smoothing: 0.0  # label smoothing (fraction)
nbs: 64  # nominal batch size
hsv_h: 0.015  # image HSV-Hue augmentation (fraction)
hsv_s: 0.7  # image HSV-Saturation augmentation (fraction)
hsv_v: 0.4  # image HSV-Value augmentation (fraction)
degrees: 0.0  # image rotation (+/- deg)
translate: 0.1  # image translation (+/- fraction)
scale: 0.5  # image scale (+/- gain)
shear: 0.0  # image shear (+/- deg)
perspective: 0.0  # image perspective (+/- fraction), range 0-0.001
flipud: 0.0  # image flip up-down (probability)
fliplr: 0.5  # image flip left-right (probability)
mosaic: 1.0  # image mosaic (probability)
mixup: 0.0  # image mixup (probability)
copy_paste: 0.0  # segment copy-paste (probability)

# Custom config.yaml ---------------------------------------------------------------------------------------------------
cfg:  # for overriding defaults.yaml

# Debug, do not modify -------------------------------------------------------------------------------------------------
v5loader: False  # use legacy YOLOv5 dataloader

# Tracker settings ------------------------------------------------------------------------------------------------------
tracker: botsort.yaml  # tracker type, ['botsort.yaml', 'bytetrack.yaml']

默认参数就能跑,但是我用的pytorch版本比较高,跑的时候会出现warning(具体是啥我也忘了),通过把workers输入0就可以了

2.覆盖超参

可以在model.train的时候覆盖超参数,具体为输入同名参数就行。代码如下(示例):

from ultralytics import YOLO

if __name__ == '__main__':
    model = YOLO("./yolov8n.pt")
    results = model.train(data="./coco128.yaml", epoch=100, batch=8)

看上去是简单了,但是用惯了v5就会感觉怪怪的


总结

这样用了之后是能跑的,而且这代码集合了v3、5、8,习惯了之后也是挺方便。就是那个default.yaml一开始让我有点懵,找不到选择的地方(比如我想用不同的yaml)。但是经过初步查看,应该是在YOLO类中的self.overrides参数,改改这个类的初始化函数应该就能用了。

### YOLOv8 Segmentation TXT 文件格式 对于YOLOv8中的分割任务,`txt`文件用于存储每张图片对应的标注信息。这些文件通常位于特定的数据集目录下,并遵循严格的格式以便于模型读取和解析。 #### 基本结构 每个`.txt`文件对应一张图像,其内部记录了该图中所有目标对象的信息。每一行代表一个单独的对象实例,具体格式如下: ``` class_id center_x center_y width height point_1_x point_1_y ... point_n_x point_n_y ``` 其中: - `class_id`: 对象类别编号(整数),依据`classes.txt`定义; - `center_x`, `center_y`: 边框中心坐标相对于整个图像宽度高度的比例值 (0~1之间的小数); - `width`, `height`: 边框宽高同样按比例表示; - `point_i_x`, `point_i_y`: 多边形顶点序列,依次给出各点相对全图尺寸的比例坐标(0~1),用来描述物体轮廓; 注意:多边形的最后一个顶点应与第一个闭合连接形成封闭区域[^2]。 例如,假设有一个包含两个不同类别的数据集——汽车(`car`)和行人(`person`),并且已经创建了一个名为`classes.txt`的文件来指定这两个标签的位置,则相应的`*.txt`文件可能看起来像这样: ```text # Example content of a .txt file for an image with one car and one person 0 0.45 0.37 0.29 0.18 0.41 0.34 0.52 0.36 ... 1 0.78 0.56 0.12 0.23 0.75 0.53 0.82 0.58 ... ``` 这里的第一列指明了所属分类索引号,后面几列表达边界框参数以及一系列构成实例掩码边缘的关键节点座标。 为了确保训练过程顺利进行,在准备阶段还需要确认所有的路径设置正确无误,并且验证所使用的配置文件是否匹配当前版本的要求[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值