使用YOLOV8n,在codlab上使用T4GPU训练100次,得到训练模型,可以直接加载使用。
YOLOV8格式的数据集
YOLOV8已训练的安全帽检测模型
训练代码
import os
from ultralytics import YOLO
os.environ['CUDA_VISIBLE_DEVICES'] = '0' # 选择要使用的GPU编号,0是默认编号
# # 从头开始创建一个新的YOLO模型
# model = YOLO('yolov8n.yaml')
# # 使用“safetyhelmet.yaml”数据集训练模型100个周期
results = model.train(data=r'/content/drive/MyDrive/archive/css-data/train/safetyhelmet.yaml', epochs=100, int8=True)
# 评估模型在验证集上的性能
results = model.val()
# 使用模型对图片进行目标检测
results = model('https://ultralytics.com/images/bus.jpg')
# 将模型导出为ONNX格式
success = model.export(format='onnx')
测试代码,使用本地摄像头
from ultralytics import YOLO
# Load a model
model = YOLO('helmet.pt') # load an official detection model
# Use local canmera
results = model.track(source="0", save=True, classes = [0,2], line_width = 3, show=True)
使用效果
使用图片测试
训练过程
Ultralytics YOLOv8.0.239 🚀 Python-3.10.12 torch-2.1.0+cu121 CUDA:0 (Tesla T4, 15102MiB)
engine/trainer: task=detect, mode=train, model=yolov8n.pt, data=/content/drive/MyDrive/archive/css-data/train/safetyhelmet.yaml, epochs=100, time=None, patience=50, batch=16, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=8, project=None, name=train2, 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=True, dynamic=True, 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, 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=runs/detect/train2
Overriding model.yaml nc=80 with nc=10
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 73984 ultralytics.nn.modules.conv.Conv [64, 128, 3, 2]
6 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True]
7 -1 1 295424 ultralytics.nn.modules.conv.Conv [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 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
11 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1]
12 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1]
13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
14 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1]
15 -1 1 37248 ultralytics.nn.modules.block.C2f [192, 64, 1]
16 -1 1 36992 ultralytics.nn.modules.conv.Conv [64, 64, 3, 2]
17 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1]
18 -1 1 123648 ultralytics.nn.modules.block.C2f [192, 128, 1]
19 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2]
20 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1]
21 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1]
22 [15, 18, 21] 1 753262 ultralytics.nn.modules.head.Detect [10, [64, 128, 256]]
Model summary: 225 layers, 3012798 parameters, 3012782 gradients, 8.2 GFLOPs
Transferred 319/355 items from pretrained weights
TensorBoard: Start with 'tensorboard --logdir runs/detect/train2', view at http://localhost:6006/
Freezing layer 'model.22.dfl.conv.weight'
AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n...
AMP: checks passed ✅
train: Scanning /content/drive/MyDrive/archive/css-data/train/labels.cache... 2605 images, 6 backgrounds, 0 corrupt: 100%|██████████| 2605/2605 [00:00<?, ?it/s]train: WARNING ⚠️ /content/drive/MyDrive/archive/css-data/train/images/004720_jpg.rf.afc486560a4004c7cfd67910af31a29c.jpg: 1 duplicate labels removed
train: WARNING ⚠️ /content/drive/MyDrive/archive/css-data/train/images/construction-813-_jpg.rf.b085952261fd98f2e76b8065de149b5f.jpg: 1 duplicate labels removed
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 /content/drive/MyDrive/archive/css-data/valid/labels.cache... 114 images, 10 backgrounds, 0 corrupt: 100%|██████████| 114/114 [00:00<?, ?it/s]
Plotting labels to runs/detect/train2/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.000714, momentum=0.9) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.0005), 63 bias(decay=0.0)
100 epochs...
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
1/100 3.51G 1.385 2.784 1.489 263 640: 100%|██████████| 163/163 [00:32<00:00, 5.00it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 4.24it/s]
all 114 697 0.467 0.352 0.335 0.148
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
2/100 3.98G 1.297 1.85 1.443 228 640: 100%|██████████| 163/163 [00:26<00:00, 6.20it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 4.44it/s] all 114 697 0.592 0.419 0.455 0.206
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
3/100 4.05G 1.252 1.651 1.406 256 640: 100%|██████████| 163/163 [00:26<00:00, 6.19it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 4.96it/s] all 114 697 0.663 0.445 0.5 0.228
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
4/100 3.86G 1.236 1.575 1.391 258 640: 100%|██████████| 163/163 [00:26<00:00, 6.22it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 5.13it/s] all 114 697 0.622 0.463 0.487 0.202
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
5/100 4G 1.202 1.506 1.363 386 640: 100%|██████████| 163/163 [00:26<00:00, 6.16it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 5.15it/s] all 114 697 0.651 0.508 0.54 0.237
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
6/100 4.13G 1.179 1.422 1.339 310 640: 100%|██████████| 163/163 [00:26<00:00, 6.13it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 5.10it/s] all 114 697 0.683 0.499 0.565 0.27
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
7/100 4G 1.165 1.379 1.33 342 640: 100%|██████████| 163/163 [00:26<00:00, 6.16it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 4.84it/s] all 114 697 0.674 0.518 0.565 0.266
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
8/100 4.03G 1.141 1.334 1.312 294 640: 100%|██████████| 163/163 [00:26<00:00, 6.18it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:00<00:00, 4.82it/s] all 114 697 0.769 0.473 0.565 0.268
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
9/100 4.4G 1.156 1.303 1.31