yolov10训练
- 1 yolov10 COCO训练
- 1.1 环境配置
- 1.2 模型训练
- 2 yolov10 训练自己的数据集
- 2.1 使用源码训练
- 2.1.1 数据集的准备
- 2.1.2 yolov10的预训练权重
- 2.1.3 模型训练
- 2.1.3.1 YOLO
- 2.1.3.2 YOLOv10
- 2.1.4 模型验证
- 3 参考链接
论文代码:https://github.com/THU-MIG/yolov10
论文链接:https://arxiv.org/abs/2405.14458
1 yolov10 COCO训练
1.1 环境配置
conda create -n yolov10 python=3.10.12
activate yolov10
conda install cudatoolkit==11.8.0
conda install pytorch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 pytorch-cuda=11.8 -c pytorch -c nvidia
git clone https://github.com/THU-MIG/yolov10
cd yolov10
在安装requirements.txt中的python包之前需要删除其中的torch
和torchvision
,如果不删除掉,使用pip install -r requirments.txt
会卸载掉之前安装的torch-gpu
,而安装上torch-cpu
版本
requirements.txt
# torch==2.0.1
# torchvision==0.15.2
onnx==1.14.0
onnxruntime==1.15.1
pycocotools==2.0.7
PyYAML==6.0.1
scipy==1.13.0
onnxsim==0.4.36
onnxruntime-gpu==1.18.0
安装清华大学版本的ultralytics
cd yolov10
pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
1.2 模型训练
使用ultralytics
不要github源码,直接进入yolov10
conda环境中就可以训练
conda activate yolov10
多gpu(没尝试过)
yolo detect train data=coco.yaml model=yolov10s.yaml epochs=500 batch=256 imgsz=640 device=0,1
单gpu
yolo detect train data=coco.yaml model=yolov10s.yaml epochs=500 batch=32 imgsz=640 device=0
结果展示
(yolov10) C:\Users\Administrator\Desktop\yolov10>yolo detect train data=coco.yaml model=yolov10n.yaml epochs=1 batch=64 imgsz=640 device=0
New https://pypi.org/project/ultralytics/8.2.26 available 😃 Update with 'pip install -U ultralytics'
Ultralytics YOLOv8.1.34 🚀 Python-3.10.12 torch-2.1.1 CUDA:0 (NVIDIA GeForce RTX 4090, 24564MiB)
engine\trainer: task=detect, mode=train, model=yolov10n.yaml, data=coco.yaml, epochs=1, time=None, patience=100, batch=64, imgsz=640, save=True, save_period=-1, val_period=1, cache=False, device=0, workers=8, project=None, name=train3, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, freeze=None, multi_scale=False, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, vid_stride=1, stream_buffer=False, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, embed=None, show=False, save_frames=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, show_boxes=True, line_width=None, 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.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.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, bgr=0.0, mosaic=1.0, mixup=0.0, copy_paste=0.0, auto_augment=randaugment, erasing=0.4, crop_fraction=1.0, cfg=None, tracker=botsort.yaml, save_dir=c:\Users\Administrator\Desktop\yolov10\runs\detect\train3
from n params module arguments
0 -1 1 464 ultralytics.nn.modules.conv.Conv [3, 16, 3, 2]
1 -1 1 4672 ultralytics.nn.modules.conv.Conv [16, 32, 3, 2]
2 -1 1 7360 ultralytics.nn.modules.block.C2f [32, 32, 1, True]
3 -1 1 18560 ultralytics.nn.modules.conv.Conv [32, 64, 3, 2]
4 -1 2 49664 ultralytics.nn.modules.block.C2f [64, 64, 2, True]
5 -1 1 9856 ultralytics.nn.modules.block.SCDown [64, 128, 3, 2]
6 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True]
7 -1 1 36096 ultralytics.nn.modules.block.SCDown [128, 256, 3, 2]
8 -1 1 460288 ultralytics.nn.modules.block.C2f [256, 256, 1, True]
9 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5]
10 -1 1 249728 ultralytics.nn.modules.block.PSA [256, 256]
11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
12 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1]
13 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1]
14 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
15 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1]
16 -1 1 37248 ultralytics.nn.modules.block.C2f [192, 64, 1]
17 -1 1 36992 ultralytics.nn.modules.conv.Conv [64, 64, 3, 2]
18 [-1, 13] 1 0 ultralytics.nn.modules.conv.Concat [1]
19 -1 1 123648 ultralytics.nn.modules.block.C2f [192, 128, 1]
20 -1 1 18048 ultralytics.nn.modules.block.SCDown [128, 128, 3, 2]
21 [-1, 10] 1 0 ultralytics.nn.modules.conv.Concat [1]
22 -1 1 282624 ultralytics.nn.modules.block.C2fCIB [384, 256, 1, True, True]
23 [16, 19, 22] 1 929808 ultralytics.nn.modules.head.v10Detect [80, [64, 128, 256]]
YOLOv10n summary: 385 layers, 2775520 parameters, 2775504 gradients, 8.7 GFLOPs
Freezing layer 'model.23.dfl.conv.weight'
AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n...
AMP: checks passed ✅
train: Scanning C:\Users\Administrator\Desktop\datasets\coco\labels\train2017.cache... 117266 images, 1021 backgrounds, 0 corrupt: 100%|██████████| 118287/
val: Scanning C:\Users\Administrator\Desktop\datasets\coco\labels\val2017.cache... 4952 images, 48 backgrounds, 0 corrupt: 100%|██████████| 5000/5000 [00:0
Plotting labels to c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\labels.jpg...
optimizer: 'optimizer=auto' found, ignoring 'lr0=0.01' and 'momentum=0.937' and determining best 'optimizer', 'lr0' and 'momentum' automatically...
optimizer: AdamW(lr=0.000119, momentum=0.9) with parameter groups 95 weight(decay=0.0), 108 weight(decay=0.0005), 107 bias(decay=0.0)
Image sizes 640 train, 640 val
Using 8 dataloader workers
Logging results to c:\Users\Administrator\Desktop\yolov10\runs\detect\train3
Starting training for 1 epochs...
Epoch GPU_mem box_om cls_om dfl_om box_oo cls_oo dfl_oo Instances Size
1/1 15.8G 3.331 5.348 3.731 3.137 6.743 3.685 234 640: 100%|██████████| 1849/1849 [07:24<00:00, 4
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 40/40 [00:23<00:00, 1.69it/s]
Saving c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\predictions.json...
Evaluating pycocotools mAP using c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\predictions.json and C:\Users\Administrator\Desktop\datasets\coco\annotations\instances_val2017.json...
loading annotations into memory...
Done (t=0.31s)
creating index...
index created!
Loading and preparing results...
DONE (t=5.73s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=17.53s).
Accumulating evaluation results...
DONE (t=4.14s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.001
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.003
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.002
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.006
1 epochs completed in 0.142 hours.
Optimizer stripped from c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\weights\last.pt, 5.9MB
Optimizer stripped from c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\weights\best.pt, 5.9MB
Validating c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\weights\best.pt...
Ultralytics YOLOv8.1.34 🚀 Python-3.10.12 torch-2.1.1 CUDA:0 (NVIDIA GeForce RTX 4090, 24564MiB)
YOLOv10n summary (fused): 285 layers, 2762608 parameters, 0 gradients, 8.6 GFLOPs
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 40/40 [00:16<00:00, 2.42it/s]
Speed: 0.1ms preprocess, 0.7ms inference, 0.0ms loss, 0.0ms postprocess per image
Saving c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\predictions.json...
Evaluating pycocotools mAP using c:\Users\Administrator\Desktop\yolov10\runs\detect\train3\predictions.json and C:\Users\Administrator\Desktop\datasets\coco\annotations\instances_val2017.json...
loading annotations into memory...
Done (t=0.31s)
creating index...
index created!
Loading and preparing results...
DONE (t=5.48s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=17.37s).
Accumulating evaluation results...
DONE (t=4.34s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.000
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.001
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.003
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.002
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.006
Results saved to c:\Users\Administrator\Desktop\yolov10\runs\detect\train3
💡 Learn more at https://docs.ultralytics.com/modes/train
这里与官网github上展示的结果不一样,Params
为2.7M
,FLOPs
为8.6G
,而github上给出的yolov10nParams
为2.3M
,FLOPs
为6.7G
。
2 yolov10 训练自己的数据集
环境配置直接按照1.1
中的配置即可,yolov10的训练方法和yolov8的训练方式一样
2.1 使用源码训练
2.1.1 数据集的准备
custom.yaml添加yolov10/ultralytics/cfg/datasets/custom.yaml
# 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: ../ultralytics/datasets # dataset root dir
train: images/train # train images (relative to 'path') 128 images
val: images/val # val images (relative to 'path') 128 images
test: # test images (optional)
# Classes
names:
0: flame
1: smoke
datasets添加在yolov10
目录下
2.1.2 yolov10的预训练权重
yolov10的预训练权重不会自动下载,需要到github上下载yolov10预训练权重,然后将其存放在yolov10
目录下。
2.1.3 模型训练
2.1.3.1 YOLO
在yolov10
目录下创建一个main.py
from ultralytics import YOLO
if __name__ == '__main__':
# Load a model
# model = YOLO('yolov9c.yaml') # build a new model from YAML
# model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
model = YOLO('yolov10n.yaml').load('yolov10n.pt') # build from YAML and transfer weights
# Train the model
results = model.train(data='custom.yaml', epochs=300, imgsz=640)
然后直接运行就可以,如果想要改一些超参,可以直接在yolov10/ultralytics/cfg/default.yaml
文件中修改。注意使用上面的这个方法时,YOLO('yolov10n.yaml')
中必须包含yolov10
,否则加载的训练模式中就是YOLO的训练代码,不是YOLOv10的训练代码。如果传入的是路径的话,如runs/detect/yolov10n/weights/best.pt
也是不行的,文件名是best.pt
,文件名必须包含yolov10
。
结果展示
PS C:\Users\Administrator\Desktop\yolov10> & C:/ProgramData/anaconda3/envs/yolov10/python.exe c:/Users/Administrator/Desktop/yolov10/main.py
Transferred 619/619 items from pretrained weights
New https://pypi.org/project/ultralytics/8.2.26 available 😃 Update with 'pip install -U ultralytics'
Ultralytics YOLOv8.1.34 🚀 Python-3.10.12 torch-2.1.1 CUDA:0 (NVIDIA GeForce RTX 4090, 24564MiB)
engine\trainer: task=detect, mode=train, model=yolov10s.yaml, data=custom.yaml, epochs=300, time=None, patience=100, batch=32, imgsz=640, save=True, save_period=-1, val_period=1, cache=False, device=None, workers=8, project=None, name=yolov10s, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, freeze=None, multi_scale=False, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, vid_stride=1, stream_buffer=False, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, embed=None, show=False, save_frames=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, show_boxes=True, line_width=None, 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.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.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, bgr=0.0, mosaic=1.0, mixup=0.0, copy_paste=0.0, auto_augment=randaugment, erasing=0.4, crop_fraction=1.0, cfg=None, tracker=botsort.yaml, save_dir=c:\Users\Administrator\Desktop\yolov10\runs\detect\yolov10s
Overriding model.yaml nc=80 with nc=2
from n params module arguments
1 -1 1 18560 ultralytics.nn.modules.conv.Conv [32, 64, 3, 2]
2 -1 1 29056 ultralytics.nn.modules.block.C2f [64, 64, 1, True]
3 -1 1 73984 ultralytics.nn.modules.conv.Conv [64, 128, 3, 2]
4 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True]
5 -1 1 36096 ultralytics.nn.modules.block.SCDown [128, 256, 3, 2]
6 -1 2 788480 ultralytics.nn.modules.block.C2f [256, 256, 2, True]
7 -1 1 137728 ultralytics.nn.modules.block.SCDown [256, 512, 3, 2]
8 -1 1 958464 ultralytics.nn.modules.block.C2fCIB [512, 512, 1, True, True]
9 -1 1 656896 ultralytics.nn.modules.block.SPPF [512, 512, 5]
10 -1 1 990976 ultralytics.nn.modules.block.PSA [512, 512]
11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
12 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1]
13 -1 1 591360 ultralytics.nn.modules.block.C2f [768, 256, 1]
14 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
15 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1]
16 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1]
17 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2]
18 [-1, 13] 1 0 ultralytics.nn.modules.conv.Concat [1]
19 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1]
20 -1 1 68864 ultralytics.nn.modules.block.SCDown [256, 256, 3, 2]
21 [-1, 10] 1 0 ultralytics.nn.modules.conv.Concat [1]
22 -1 1 1089536 ultralytics.nn.modules.block.C2fCIB [768, 512, 1, True, True]
23 [16, 19, 22] 1 1640348 ultralytics.nn.modules.head.v10Detect [2, [128, 256, 512]]
YOLOv10s summary: 402 layers, 8067900 parameters, 8067884 gradients, 24.8 GFLOPs
Transferred 607/619 items from pretrained weights
Freezing layer 'model.23.dfl.conv.weight'
AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n...
AMP: checks passed ✅
train: Scanning C:\Users\Administrator\Desktop\ultralytics\datasets\labels\train.cache... 4672 images, 0 backgrounds, 0 corrupt: 100%|██████████| 4672/4672 [00:00<?
val: Scanning C:\Users\Administrator\Desktop\ultralytics\datasets\labels\val.cache... 1168 images, 0 backgrounds, 0 corrupt: 100%|██████████| 1168/1168 [00:00<?, ?i
Plotting labels to c:\Users\Administrator\Desktop\yolov10\runs\detect\yolov10s\labels.jpg...
optimizer: 'optimizer=auto' found, ignoring 'lr0=0.01' and 'momentum=0.937' and determining best 'optimizer', 'lr0' and 'momentum' automatically...
optimizer: SGD(lr=0.01, momentum=0.9) with parameter groups 99 weight(decay=0.0), 112 weight(decay=0.0005), 111 bias(decay=0.0)
Image sizes 640 train, 640 val
Using 8 dataloader workers
Logging results to c:\Users\Administrator\Desktop\yolov10\runs\detect\yolov10s
Starting training for 300 epochs...
Epoch GPU_mem box_om cls_om dfl_om box_oo cls_oo dfl_oo Instances Size
1/300 11.2G 1.173 1.774 1.44 1.233 3.024 1.377 176 640: 100%|██████████| 146/146 [00:22<00:00, 6.46it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 19/19 [00:05<00:00, 3.76it/s]
all 1168 3052 0.642 0.569 0.615 0.341
所有文件存在目录如下图
2.1.3.2 YOLOv10
研究了下代码,为了方便,也可以直接使用YOLOv10
from ultralytics import YOLOv10
if __name__ == '__main__':
# Load a model
# model = YOLO('yolov9c.yaml') # build a new model from YAML
# model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
model = YOLOv10('yolov10n.yaml').load('yolov10n.pt') # build from YAML and transfer weights
# Train the model
results = model.train(data='custom.yaml', epochs=300, imgsz=640)
2.1.4 模型验证
因为保存的文件名不包含yolov10
,所以只能使用2.1.3.2
的方法进行验证,当然,也可以将文件名best.pt
改为yolov10n.pt
,这样也可以使用2.1.3.1
的方法进行验证,为了方便,就采用2.1.3.2
的方法,使用from ultralytics import Y0LOv10
from ultralytics import YOLO, YOLOv10
if __name__ == '__main__':
# model = YOLO('yolov10m.yaml').load('yolov10m.pt') # build from YAML and transfer weights
# Train the model
# results = model.train(data='custom.yaml', epochs=300, imgsz=640)
# val
model = YOLOv10('runs/detect/yolov10n/weights/best.pt')
results = model.val(data='custom.yaml',imgsz=640,name='val')
结果展示
Ultralytics YOLOv8.1.34 🚀 Python-3.10.12 torch-2.1.1 CUDA:0 (NVIDIA GeForce RTX 4090, 24564MiB)
YOLOv10n summary (fused): 285 layers, 2695196 parameters, 0 gradients, 8.2 GFLOPs
val: Scanning C:\Users\Administrator\Desktop\ultralytics\datasets\labels\val.cache... 1168 images, 0 backgrounds, 0 corrupt: 100%|██████████| 1168/
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 37/37 [00:03<00:00, 9.44it/s]
all 1168 3052 0.848 0.777 0.858 0.61
flame 1168 1906 0.8 0.726 0.82 0.541
smoke 1168 1146 0.895 0.828 0.897 0.679
Speed: 0.1ms preprocess, 1.2ms inference, 0.0ms loss, 0.0ms postprocess per image
Results saved to c:\Users\Administrator\Desktop\yolov10\runs\detect\val
3 参考链接
- YOLOv10最全使用教程(含ONNX和TensorRT推理)
- YOLOv10 正式发布!原理、部署、应用一站式齐全
- 【论文阅读】 YOLOv10: Real-Time End-to-End Object Detection
- YOLOv8训练、验证、测试官方提供的方法