ultralytics
快速上手#
参考:ultralytics
from ultralytics import YOLO
# Create a new YOLO model from scratch
model = YOLO('yolov8n.yaml')
# Load a pretrained YOLO model (recommended for training)
model = YOLO('yolov8n.pt')
# Train the model using the 'coco128.yaml' dataset for 3 epochs
results = model.train(data='coco128.yaml', epochs=3)
# Evaluate the model's performance on the validation set
results = model.val()
# Export the model to ONNX format
success = model.export(format='onnx')
================ Diagnostic Run torch.onnx.export version 2.0.0 ================
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================
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 897664 ultralytics.nn.modules.head.Detect [80, [64, 128, 256]]
YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients
Ultralytics YOLOv8.0.106 🚀 Python-3.10.11 torch-2.0.0 CUDA:0 (NVIDIA GeForce RTX 3090, 24257MiB)
yolo/engine/trainer: task=detect, mode=train, model=yolov8n.pt, data=coco128.yaml, epochs=3, patience=50, batch=16, imgsz=640, save=True, save_period=-1, cache=False, device=None, workers=8, project=None, name=None, exist_ok=False, pretrained=False, optimizer=SGD, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=0, resume=False, amp=True, 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, show=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, vid_stride=1, line_width=None, 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.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, cfg=None, v5loader=False, tracker=botsort.yaml, save_dir=runs/detect/train2
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 897664 ultralytics.nn.modules.head.Detect [80, [64, 128, 256]]
Model summary: 225 layers, 3157200 parameters, 3157184 gradients
Transferred 355/355 items from pretrained weights
AMP: running Automatic Mixed Precision (AMP) checks with YOLOv8n...
AMP: checks passed ✅
optimizer: SGD(lr=0.01) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.0005), 63 bias
train: Scanning /media/pc/data/lxw/ai/tasks/attacks/attack-tools/tools/datasets/coco128/labels/train2017.cache... 126 images, 2 backgrounds, 0 corrupt: 100%|██████████| 128/128 [00:00<?, ?it/s]
val: Scanning /media/pc/data/lxw/ai/tasks/attacks/attack-tools/tools/datasets/coco128/labels/train2017.cache... 126 images, 2 backgrounds, 0 corrupt: 100%|██████████| 128/128 [00:00<?, ?it/s]
Plotting labels to runs/detect/train2/labels.jpg...
Image sizes 640 train, 640 val
Using 8 dataloader workers
Logging results to runs/detect/train2
Starting training for 3 epochs...
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
1/3 2.51G 1.182 1.408 1.213 218 640: 100%|██████████| 8/8 [00:05<00:00, 1.51it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:02<00:00, 1.54it/s]
all 128 929 0.638 0.562 0.626 0.462
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
2/3 2.49G 1.137 1.316 1.239 205 640: 100%|██████████| 8/8 [00:00<00:00, 8.45it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:01<00:00, 2.99it/s]
all 128 929 0.677 0.566 0.649 0.482
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
3/3 2.49G 1.169 1.288 1.217 161 640: 100%|██████████| 8/8 [00:00<00:00, 9.21it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:02<00:00, 1.53it/s]
all 128 929 0.701 0.568 0.667 0.498
3 epochs completed in 0.007 hours.
Optimizer stripped from runs/detect/train2/weights/last.pt, 6.5MB
Optimizer stripped from runs/detect/train2/weights/best.pt, 6.5MB
Validating runs/detect/train2/weights/best.pt...
Ultralytics YOLOv8.0.106 🚀 Python-3.10.11 torch-2.0.0 CUDA:0 (NVIDIA GeForce RTX 3090, 24257MiB)
Model summary (fused): 168 layers, 3151904 parameters, 0 gradients
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:02<00:00, 1.81it/s]
all 128 929 0.71 0.561 0.667 0.497
person 128 254 0.86 0.65 0.767 0.55
bicycle 128 6 1 0.311 0.383 0.336
car 128 46 0.847 0.217 0.325 0.203
motorcycle 128 5 0.783 0.8 0.938 0.745
airplane 128 6 0.812 0.833 0.894 0.682
bus 128 7 0.781 0.714 0.723 0.661
train 128 3 0.711 0.843 0.913 0.83
truck 128 12 1 0.308 0.548 0.349
boat 128 6 0.32 0.167 0.379 0.232
traffic light 128 14 0.67 0.145 0.202 0.133
stop sign 128 2 0.805 1 0.995 0.647
bench 128 9 0.83 0.546 0.661 0.426
bird 128 16 0.889 0.812 0.969 0.647
cat 128 4 1 0.968 0.995 0.843
dog 128 9 0.606 0.889 0.91 0.671
horse 128 2 0.638 1 0.995 0.647
elephant 128 17 0.907 0.882 0.935 0.709
bear 128 1 0.631 1 0.995 0.995
zebra 128 4 0.876 1 0.995 0.934
giraffe 128 9 0.845 0.889 0.963 0.688
backpack 128 6 0.479 0.333 0.337 0.193
umbrella 128 18 0.874 0.556 0.692 0.467
handbag 128 19 0.958 0.105 0.245 0.143
tie 128 7 0.8 0.714 0.799 0.54
suitcase 128 4 0.51 0.75 0.788 0.543
frisbee 128 5 0.599 0.8 0.732 0.655
skis 128 1 0.83 1 0.995 0.522
snowboard 128 7 0.571 0.714 0.783 0.493
sports ball 128 6 0.681 0.362 0.573 0.308
kite 128 10 0.695 0.4 0.522 0.194
baseball bat 128 4 0.577 0.25 0.373 0.176
baseball glove 128 7 0.836 0.429 0.43 0.302
skateboard 128 5 0.817 0.6 0.6 0.427
tennis racket 128 7 0.679 0.307 0.507 0.335
bottle 128 18 0.525 0.389 0.419 0.249
wine glass 128 16 0.681 0.536 0.624 0.351
cup 128 36 0.78 0.333 0.43 0.292
fork 128 6 0.64 0.167 0.283 0.218
knife 128 16 0.668 0.562 0.672 0.393
spoon 128 22 0.685 0.182 0.353 0.205
bowl 128 28 0.717 0.571 0.635 0.495
banana 128 1 0 0 0.124 0.034
sandwich 128 2 1 0.814 0.995 0.995
orange 128 4 1 0.346 0.995 0.697
broccoli 128 11 0.67 0.185 0.303 0.257
carrot 128 24 0.63 0.625 0.756 0.483
hot dog 128 2 0.593 1 0.995 0.995
pizza 128 5 0.829 0.971 0.962 0.829
donut 128 14 0.648 1 0.934 0.827
cake 128 4 0.838 1 0.995 0.904
chair 128 35 0.415 0.406 0.421 0.267
couch 128 6 0.554 0.333 0.599 0.435
potted plant 128 14 0.573 0.714 0.709 0.487
bed 128 3 0.748 1 0.995 0.764
dining table 128 13 0.515 0.538 0.537 0.428
toilet 128 2 0.672 0.5 0.828 0.796
tv 128 2 0.444 0.5 0.745 0.696
laptop 128 3 1 0.49 0.727 0.644
mouse 128 2 1 0 0.0419 0.00838
remote 128 8 0.802 0.5 0.566 0.486
cell phone 128 8 0 0 0.0828 0.0393
microwave 128 3 0.603 0.539 0.83 0.682
oven 128 5 0.527 0.4 0.437 0.323
sink 128 6 0.396 0.167 0.394 0.202
refrigerator 128 5 0.927 0.8 0.878 0.731
book 128 29 0.491 0.138 0.346 0.177
clock 128 9 0.913 0.889 0.915 0.762
vase 128 2 0.439 1 0.995 0.895
scissors 128 1 1 0 0.497 0.108
teddy bear 128 21 0.772 0.429 0.663 0.428
toothbrush 128 5 1 0.513 0.786 0.507
Speed: 1.4ms preprocess, 0.7ms inference, 0.0ms loss, 1.5ms postprocess per image
Results saved to runs/detect/train2
Ultralytics YOLOv8.0.106 🚀 Python-3.10.11 torch-2.0.0 CUDA:0 (NVIDIA GeForce RTX 3090, 24257MiB)
Model summary (fused): 168 layers, 3151904 parameters, 0 gradients
val: Scanning /media/pc/data/lxw/ai/tasks/attacks/attack-tools/tools/datasets/coco128/labels/train2017.cache... 126 images, 2 backgrounds, 0 corrupt: 100%|██████████| 128/128 [00:00<?, ?it/s]
Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 8/8 [00:04<00:00, 1.73it/s]
all 128 929 0.706 0.563 0.665 0.496
person 128 254 0.86 0.651 0.769 0.555
bicycle 128 6 1 0.311 0.376 0.313
car 128 46 0.847 0.217 0.328 0.2
motorcycle 128 5 0.707 0.8 0.938 0.745
airplane 128 6 0.812 0.833 0.894 0.698
bus 128 7 0.839 0.714 0.722 0.661
train 128 3 0.711 0.844 0.913 0.83
truck 128 12 1 0.304 0.524 0.335
boat 128 6 0.305 0.167 0.345 0.164
traffic light 128 14 0.669 0.145 0.207 0.13
stop sign 128 2 0.805 1 0.995 0.647
bench 128 9 0.83 0.546 0.66 0.426
bird 128 16 0.9 0.812 0.969 0.647
cat 128 4 1 0.966 0.995 0.843
dog 128 9 0.606 0.889 0.91 0.671
horse 128 2 0.638 1 0.995 0.647
elephant 128 17 0.859 0.882 0.935 0.707
bear 128 1 0.63 1 0.995 0.995
zebra 128 4 0.876 1 0.995 0.965
giraffe 128 9 0.787 0.822 0.955 0.687
backpack 128 6 0.488 0.333 0.362 0.203
umbrella 128 18 0.87 0.556 0.686 0.465
handbag 128 19 0.956 0.105 0.244 0.149
tie 128 7 0.799 0.714 0.799 0.524
suitcase 128 4 0.508 0.75 0.788 0.543
frisbee 128 5 0.599 0.8 0.732 0.648
skis 128 1 0.828 1 0.995 0.522
snowboard 128 7 0.572 0.714 0.785 0.492
sports ball 128 6 0.681 0.362 0.556 0.3
kite 128 10 0.695 0.4 0.542 0.212
baseball bat 128 4 0.541 0.25 0.331 0.19
baseball glove 128 7 0.778 0.429 0.43 0.303
skateboard 128 5 0.89 0.6 0.605 0.441
tennis racket 128 7 0.68 0.309 0.507 0.348
bottle 128 18 0.53 0.389 0.441 0.275
wine glass 128 16 0.655 0.476 0.597 0.357
cup 128 36 0.733 0.333 0.453 0.306
fork 128 6 0.642 0.167 0.241 0.203
knife 128 16 0.591 0.562 0.626 0.38
spoon 128 22 0.823 0.213 0.394 0.238
bowl 128 28 0.79 0.67 0.683 0.539
banana 128 1 0 0 0.111 0.042
sandwich 128 2 0.741 0.5 0.828 0.828
orange 128 4 1 0.352 0.995 0.697
broccoli 128 11 0.55 0.182 0.284 0.244
carrot 128 24 0.674 0.775 0.75 0.46
hot dog 128 2 0.568 1 0.995 0.995
pizza 128 5 0.93 1 0.995 0.866
donut 128 14 0.647 1 0.934 0.818
cake 128 4 0.73 1 0.995 0.904
chair 128 35 0.416 0.407 0.406 0.258
couch 128 6 0.66 0.5 0.712 0.54
potted plant 128 14 0.566 0.714 0.707 0.491
bed 128 3 0.743 1 0.995 0.742
dining table 128 13 0.564 0.538 0.525 0.41
toilet 128 2 0.671 0.5 0.828 0.796
tv 128 2 0.443 0.5 0.745 0.696
laptop 128 3 1 0.492 0.706 0.629
mouse 128 2 1 0 0.0481 0.00963
remote 128 8 0.804 0.5 0.574 0.5
cell phone 128 8 0 0 0.0774 0.0377
microwave 128 3 0.601 0.536 0.83 0.65
oven 128 5 0.528 0.4 0.437 0.323
sink 128 6 0.417 0.167 0.375 0.204
refrigerator 128 5 0.979 0.8 0.878 0.706
book 128 29 0.469 0.138 0.35 0.18
clock 128 9 0.912 0.889 0.914 0.762
vase 128 2 0.436 1 0.995 0.895
scissors 128 1 1 0 0.497 0.108
teddy bear 128 21 0.771 0.429 0.655 0.429
toothbrush 128 5 1 0.589 0.846 0.513
Speed: 1.6ms preprocess, 16.3ms inference, 0.0ms loss, 6.0ms postprocess per image
Results saved to runs/detect/val2
Found https://ultralytics.com/images/bus.jpg locally at bus.jpg
image 1/1 /media/pc/data/lxw/ai/torch-book/doc/tutorial/ultralytics/bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 20.8ms
Speed: 5.0ms preprocess, 20.8ms inference, 4.5ms postprocess per image at shape (1, 3, 640, 640)
Ultralytics YOLOv8.0.106 🚀 Python-3.10.11 torch-2.0.0 CPU
PyTorch: starting from runs/detect/train2/weights/best.pt with input shape (1, 3, 640, 640) BCHW and output shape(s) (1, 84, 8400) (6.2 MB)
ONNX: starting export with onnx 1.13.1 opset 17...
ONNX: export success ✅ 1.4s, saved as runs/detect/train2/weights/best.onnx (12.2 MB)
Export complete (1.7s)
Results saved to /media/pc/data/lxw/ai/torch-book/doc/tutorial/ultralytics/runs/detect/train2/weights
Predict: yolo predict task=detect model=runs/detect/train2/weights/best.onnx imgsz=640
Validate: yolo val task=detect model=runs/detect/train2/weights/best.onnx imgsz=640 data=/media/pc/data/tmp/cache/conda/envs/tvmz/lib/python3.10/site-packages/ultralytics/datasets/coco128.yaml
Visualize: https://netron.app
model.info()
Model summary (fused): 168 layers, 3151904 parameters, 0 gradients
(3151904, 0.0)
# Perform object detection on an image using the model
results = model('https://ultralytics.com/images/bus.jpg', conf=0.25, iou=0.7)
for result in results:
boxes = result.boxes # Boxes object for bbox outputs
masks = result.masks # Masks object for segmentation masks outputs
probs = result.probs # Class probabilities for classification outputs
break
from PIL import Image
res_plotted = result[1].plot(pil=True)
Image.fromarray(res_plotted[:, :, ::-1])
result.boxes.cls
tensor([ 5., 0., 0., 0., 0., 11.], device='cuda:0')
result.boxes.conf
tensor([0.8897, 0.8854, 0.8676, 0.8225, 0.4323, 0.4203], device='cuda:0')
result.boxes.data
tensor([[2.2367e+01, 2.2733e+02, 8.0251e+02, 7.6018e+02, 8.8971e-01, 5.0000e+00],
[4.7333e+01, 3.9677e+02, 2.4670e+02, 9.0620e+02, 8.8536e-01, 0.0000e+00],
[6.7034e+02, 3.7896e+02, 8.1000e+02, 8.7867e+02, 8.6764e-01, 0.0000e+00],
[2.2060e+02, 4.0592e+02, 3.4431e+02, 8.5884e+02, 8.2246e-01, 0.0000e+00],
[0.0000e+00, 5.5279e+02, 6.4058e+01, 8.7603e+02, 4.3231e-01, 0.0000e+00],
[1.9550e-02, 2.5435e+02, 3.2364e+01, 3.2485e+02, 4.2028e-01, 1.1000e+01]], device='cuda:0')