- 本文是个人使用YOLOv8训练自己的YOLO格式数据集的应用案例,由于水平有限,难免出现错漏,敬请批评改正。
- 虽然YOLOv8与YOLOv5都是同一个团队Ultralytics发布的,但是YOLOv8的代码封装性比YOLOv5更好。
- YOLOv8要求的数据集格式与YOLOv5、YOLOv7一致。
- YOLOv8最大的改变就是抛弃了以往的anchor-base,使用了anchor-free的思想。
- 更多精彩内容,可点击进入YOLO系列专栏或我的个人主页查看
- 熟悉Python
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.6.0
Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1
torch>=1.7.0
torchvision>=0.8.1
tqdm>=4.64.0
tensorboard>=2.4.1
pandas>=1.1.4
seaborn>=0.11.0
pip install ultralytics
官方YOLOv8源代码地址:https://github.com/ultralytics/ultralytics.git
本文章项目地址:https://gitcode.net/FriendshipTang/yolov8.git
注:本文之所以不直接克隆官方YOLOv8源代码地址,是因为:
- 我在源代码基础上,下载好并添加了
yolov8s.pt
权重文件和新建并编辑好了关于足球数据集信息的football.yaml
文件,便于后续使用。- 如果直接克隆官方YOLOv8源代码地址,你会发现会出现一个这样的路径
"/ultralytics/ultralytics"
,这可能会导致from ultralytics import YOLO
或import ultralytics
报错。
git clone https://gitcode.net/FriendshipTang/yolov8.git
Cloning into 'yolov8'...
remote: Enumerating objects: 4583, done.
remote: Counting objects: 100% (4583/4583), done.
remote: Compressing objects: 100% (1270/1270), done.
remote: Total 4583 (delta 2981), reused 4576 (delta 2979), pack-reused 0
Receiving objects: 100% (4583/4583), 23.95 MiB | 1.55 MiB/s, done.
Resolving deltas: 100% (2981/2981), done.
请到
https://gitcode.net/FriendshipTang/yolov8.git
网站下载源代码zip压缩包。
详见YOLOv7训练自己的数据集(口罩检测)
地址:https://blog.csdn.net/FriendshipTang/article/details/126513426
以
football.yaml
文件内容为例,大家可以根据自己的数据集信息进行修改。
# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ./yolov8/football_yolodataset/trainset
val: ./yolov8/football_yolodataset/testset
# number of classes
nc: 1
# class names
names: ["football"]
yolo detect train data=football.yaml model=yolov8s.pt epochs=20 imgsz=640 device=0,1 batch=128
Ultralytics YOLOv8.0.37 Python-3.7.12 torch-1.11.0 CUDA:0 (Tesla T4, 15110MiB)
CUDA:1 (Tesla T4, 15110MiB)
yolo/engine/trainer: task=detect, mode=train, model=yolov8s.pt, data=football.yaml, epochs=20, patience=50, batch=128, imgsz=640, save=True, save_period=-1, cache=False, device=(0, 1), workers=8, project=None, name=None, exist_ok=False, pretrained=False, optimizer=SGD, verbose=True, seed=0, deterministic=True, single_cls=False, image_weights=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, min_memory=False, overlap_mask=True, mask_ratio=4, dropout=False, val=True, split=val, save_json=False, save_hybrid=False, conf=0.001, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=ultralytics/assets/, show=False, save_txt=False, save_conf=False, save_crop=False, hide_labels=False, hide_conf=False, vid_stride=1, line_thickness=3, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, boxes=True, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.001, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, fl_gamma=0.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, cfg=None, v5loader=False, save_dir=runs/detect/train
Overriding model.yaml nc=80 with nc=1
from n params module arguments
0 -1 1 928 ultralytics.nn.modules.Conv [3, 32, 3, 2]
1 -1 1 18560 ultralytics.nn.modules.Conv [32, 64, 3, 2]
2 -1 1 29056 ultralytics.nn.modules.C2f [64, 64, 1, True]
3 -1 1 73984 ultralytics.nn.modules.Conv [64, 128, 3, 2]
4 -1 2 197632 ultralytics.nn.modules.C2f [128, 128, 2, True]
5 -1 1 295424 ultralytics.nn.modules.Conv [128, 256, 3, 2]
6 -1 2 788480 ultralytics.nn.modules.C2f [256, 256, 2, True]
7 -1 1 1180672 ultralytics.nn.modules.Conv [256, 512, 3, 2]
8 -1 1 1838080 ultralytics.nn.modules.C2f [512, 512, 1, True]
9 -1 1 656896 ultralytics.nn.modules.SPPF [512, 512, 5]
10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
11 [-1, 6] 1 0 ultralytics.nn.modules.Concat [1]
12 -1 1 591360 ultralytics.nn.modules.C2f [768, 256, 1]
13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
14 [-1, 4] 1 0 ultralytics.nn.modules.Concat [1]
15 -1 1 148224 ultralytics.nn.modules.C2f [384, 128, 1]
16 -1 1 147712 ultralytics.nn.modules.Conv [128, 128, 3, 2]
17 [-1, 12] 1 0 ultralytics.nn.modules.Concat [1]
18 -1 1 493056 ultralytics.nn.modules.C2f [384, 256, 1]
19 -1 1 590336 ultralytics.nn.modules.Conv [256, 256, 3, 2]
20 [-1, 9] 1 0 ultralytics.nn.modules.Concat [1]
21 -1 1 1969152 ultralytics.nn.modules.C2f [768, 512, 1]
22 [15, 18, 21] 1 2116435 ultralytics.nn.modules.Detect [1, [128, 256, 512]]
Model summary: 225 layers, 11135987 parameters, 11135971 gradients, 28.6 GFLOPs
Transferred 349/355 items from pretrained weights
DDP settings: RANK 0, WORLD_SIZE 2, DEVICE cuda:0
Overriding model.yaml nc=80 with nc=1
from n params module arguments
0 -1 1 928 ultralytics.nn.modules.Conv [3, 32, 3, 2]
1 -1 1 18560 ultralytics.nn.modules.Conv [32, 64, 3, 2]
2 -1 1 29056 ultralytics.nn.modules.C2f [64, 64, 1, True]
3 -1 1 73984 ultralytics.nn.modules.Conv [64, 128, 3, 2]
4 -1 2 197632 ultralytics.nn.modules.C2f [128, 128, 2, True]
5 -1 1 295424 ultralytics.nn.modules.Conv [128, 256, 3, 2]
6 -1 2 788480 ultralytics.nn.modules.C2f [256, 256, 2, True]
7 -1 1 1180672 ultralytics.nn.modules.Conv [256, 512, 3, 2]
8 -1 1 1838080 ultralytics.nn.modules.C2f [512, 512, 1, True]
9 -1 1 656896 ultralytics.nn.modules.SPPF [512, 512, 5]
10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
11 [-1, 6] 1 0 ultralytics.nn.modules.Concat [1]
12 -1 1 591360 ultralytics.nn.modules.C2f [768, 256, 1]
13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
14 [-1, 4] 1 0 ultralytics.nn.modules.Concat [1]
15 -1 1 148224 ultralytics.nn.modules.C2f [384, 128, 1]
16 -1 1 147712 ultralytics.nn.modules.Conv [128, 128, 3, 2]
17 [-1, 12] 1 0 ultralytics.nn.modules.Concat [1]
18 -1 1 493056 ultralytics.nn.modules.C2f [384, 256, 1]
19 -1 1 590336 ultralytics.nn.modules.Conv [256, 256, 3, 2]
20 [-1, 9] 1 0 ultralytics.nn.modules.Concat [1]
21 -1 1 1969152 ultralytics.nn.modules.C2f [768, 512, 1]
22 [15, 18, 21] 1 2116435 ultralytics.nn.modules.Detect [1, [128, 256, 512]]
Model summary: 225 layers, 11135987 parameters, 11135971 gradients, 28.6 GFLOPs
Transferred 349/355 items from pretrained weights
optimizer: SGD(lr=0.01) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.002), 63 bias
train: Scanning /kaggle/working/yolov8/football_yolodataset/trainset/labels.cach
albumentations: Blur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8))
val: Scanning /kaggle/working/yolov8/football_yolodataset/testset/labels.cache..
Image sizes 640 train, 640 val
Using 2 dataloader workers
Logging results to runs/detect/train
Starting training for 20 epochs...
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
1/20 13.7G 1.241 7.611 1.058 50 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.728 0.46 0.496 0.282
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
2/20 13.7G 1.061 1.076 0.979 46 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.791 0.497 0.566 0.338
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
3/20 13.7G 1.043 0.8968 0.9592 56 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.722 0.506 0.581 0.344
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
4/20 13.7G 1.082 0.8714 0.9542 57 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.804 0.503 0.589 0.311
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
5/20 13.7G 1.134 0.891 0.9604 44 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.652 0.469 0.485 0.236
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
6/20 13.7G 1.134 0.8498 0.9638 44 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.701 0.489 0.509 0.242
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
7/20 13.7G 1.152 0.8197 0.9519 49 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.759 0.485 0.549 0.254
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
8/20 13.7G 1.111 0.7813 0.9402 36 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.705 0.499 0.551 0.307
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
9/20 13.7G 1.106 0.7623 0.9441 44 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.722 0.521 0.569 0.308
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
10/20 13.7G 1.073 0.7442 0.9144 50 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.725 0.512 0.57 0.314
Closing dataloader mosaic
albumentations: Blur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8))
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
11/20 13.7G 1.083 0.7146 0.9452 24 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.646 0.465 0.497 0.286
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
12/20 13.7G 1.083 0.7343 0.9433 27 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.771 0.486 0.567 0.32
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
13/20 13.7G 1.071 0.6758 0.9452 26 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.76 0.504 0.585 0.339
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
14/20 13.7G 1.04 0.6566 0.9366 26 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.747 0.545 0.6 0.343
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
15/20 13.7G 1.047 0.6338 0.9396 25 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.782 0.569 0.661 0.369
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
16/20 13.7G 1.008 0.6253 0.9315 26 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.777 0.605 0.669 0.39
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
17/20 13.7G 0.9794 0.5733 0.9216 27 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.747 0.606 0.67 0.394
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
18/20 13.7G 0.9408 0.5384 0.9087 25 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.798 0.619 0.708 0.416
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
19/20 13.7G 0.9406 0.5241 0.8998 26 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.841 0.647 0.719 0.43
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
20/20 13.7G 0.8838 0.5055 0.8898 29 640: 1
Class Images Instances Box(P R mAP50 m
all 683 693 0.864 0.659 0.756 0.454
20 epochs completed in 0.980 hours.
Optimizer stripped from runs/detect/train/weights/last.pt, 22.5MB
Optimizer stripped from runs/detect/train/weights/best.pt, 22.5MB
Validating runs/detect/train/weights/best.pt...
Model summary (fused): 168 layers, 11125971 parameters, 0 gradients, 28.4 GFLOPs
Class Images Instances Box(P R mAP50 m
all 683 693 0.864 0.659 0.756 0.454
Speed: 0.1ms pre-process, 3.6ms inference, 0.0ms loss, 1.0ms post-process per image
Results saved to runs/detect/train
训练完成,会在./runs/detect/train文件夹生成best.pt和last.pt权重。
yolo detect val model=./runs/detect/train/weights/best.pt
yolo detect predict model=./runs/detect/train/weights/best.pt source="football.png" # predict with custom model
足球数据集
- 地址:https://download.csdn.net/download/FriendshipTang/87354858
[1] YOLOv8 源代码地址. https://github.com/ultralytics/ultralytics.git.
[2] YOLOv8 Docs. https://docs.ultralytics.com/
- 更多精彩内容,可点击进入YOLO系列专栏或我的个人主页查看