快捷跑通ultralytics下的yolo系列
快捷实现ultralytics下的yolo系列
1. 环境安装和配置
1.1 torch先行
根据电脑cuda版本安装适配torch,可以避免后续很多麻烦事情
nvcc -V # 查看cuda版本
torch官网
https://pytorch.org/get-started/previous-versions/
例如我的11.6cuda,即可安装
pip install torch==1.12.0+cu116 torchvision==0.13.0+cu116 torchaudio==0.12.0 --extra-index-url https://download.pytorch.org/whl/cu116
1.2 安装ultralytics
Install the ultralytics package from PyPI
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple/ # 加个清华源快到起飞
配置好两个文件,①数据集相关的描述文件
~/ultralytics/ultralytics/cfg/datasets/coco8-seg.yaml
#yoloseg_dataset/
#├── images/
#│ ├── train/
#│ │ ├── 0001.png ← 注意 file_name 中包含 train/000xxx.png
#│ └── val/
#│ ├── 0002.png← 注意 file_name 中包含 val/000xxx.png
#├── labels/
#│ ├── train/
#│ │ ├── 0001.txt
#│ └── val/
#│ ├── 0002.txt
# 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: /media/ly/AddDisk/InstanceSeg2025/yoloseg_dataset/ # dataset root dir
train: images/train # train images (relative to 'path') 4 images
val: images/val # val images (relative to 'path') 4 imagestest: # test images (optional)task: segment # 实例分割
segments: True # 启用 COCO分割nc: 1names:0: apple
②模型体积相关的描述文件
~/ultralytics/ultralytics/cfg/models/11/yolo11s-seg.yaml
# Parameters
nc: 1 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n-seg.yaml' will call yolo11-seg.yaml with scale 'n'# [depth, width, max_channels]
# n: [0.50, 0.25, 1024] # summary: 203 layers, 2876848 parameters, 2876832 gradients, 10.5 GFLOPss: [0.50, 0.50, 1024] # summary: 203 layers, 10113248 parameters, 10113232 gradients, 35.8 GFLOPs
# m: [0.50, 1.00, 512] # summary: 253 layers, 22420896 parameters, 22420880 gradients, 123.9 GFLOPs
# l: [1.00, 1.00, 512] # summary: 379 layers, 27678368 parameters, 27678352 gradients, 143.0 GFLOPs
# x: [1.00, 1.50, 512] # summary: 379 layers, 62142656 parameters, 62142640 gradients, 320.2 GFLOPs# YOLO11n backbone
backbone:# [from, repeats, module, args]- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4- [-1, 2, C3k2, [256, False, 0.25]]- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8- [-1, 2, C3k2, [512, False, 0.25]]- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16- [-1, 2, C3k2, [512, True]]- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32- [-1, 2, C3k2, [1024, True]]- [-1, 1, SPPF, [1024, 5]] # 9- [-1, 2, C2PSA, [1024]] # 10# YOLO11n head
head:- [-1, 1, nn.Upsample, [None, 2, "nearest"]]- [[-1, 6], 1, Concat, [1]] # cat backbone P4- [-1, 2, C3k2, [512, False]] # 13- [-1, 1, nn.Upsample, [None, 2, "nearest"]]- [[-1, 4], 1, Concat, [1]] # cat backbone P3- [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)- [-1, 1, Conv, [256, 3, 2]]- [[-1, 13], 1, Concat, [1]] # cat head P4- [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)- [-1, 1, Conv, [512, 3, 2]]- [[-1, 10], 1, Concat, [1]] # cat head P5- [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)- [[16, 19, 22], 1, Segment, [nc, 32, 256]] # Detect(P3, P4, P5)
开始训练(实例分割为例)
yolo task=segment mode=train \model=path/to/ultralytics/cfg/models/11/yolo11s-seg.yaml \data=path/to/ultralytics/ultralytics/cfg/datasets/coco8-seg.yaml \epochs=100 imgsz=640 batch=2