知乎专栏 |
yolo task=detect mode=train model=yolo11n.pt data=captcha/images/YOLODataset/dataset.yaml epochs=50 workers=1 batch=10
yolo val model=runs/detect/train/weights/best.pt data=captcha/images/YOLODataset/dataset.yaml
yolo predict model=runs/detect/train/weights/best.pt source=captcha/test/0_5902.png
(venv) neo@Neo-Mac-mini netkiller % yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg' Creating new Ultralytics Settings v0.0.6 file ✅ View Ultralytics Settings with 'yolo settings' or at '/Users/neo/Library/Application Support/Ultralytics/settings.json' Update Settings with 'yolo settings key=value', i.e. 'yolo settings runs_dir=path/to/dir'. For help see https://docs.ultralytics.com/quickstart/#ultralytics-settings. Downloading https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt to 'yolov8n.pt'... 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 6.25M/6.25M [00:02<00:00, 2.53MB/s] Ultralytics 8.3.31 🚀 Python-3.12.7 torch-2.5.1 CPU (Apple M4) YOLOv8n summary (fused): 168 layers, 3,151,904 parameters, 0 gradients, 8.7 GFLOPs Downloading https://ultralytics.com/images/bus.jpg to 'bus.jpg'... 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 134k/134k [00:00<00:00, 364kB/s] image 1/1 /Users/neo/PycharmProjects/netkiller/bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 43.4ms Speed: 3.3ms preprocess, 43.4ms inference, 6.7ms postprocess per image at shape (1, 3, 640, 480) Results saved to /Users/neo/PycharmProjects/netkiller/runs/detect/predict 💡 Learn more at https://docs.ultralytics.com/modes/predict
训练模型
yolo segment train data=coco8-seg.yaml model=yolo11n-seg.pt epochs=100 imgsz=640
检验模型
(.venv) neo@Neo-Mac-mini netkiller % yolo segment val model=yolo11n-seg.pt WARNING ⚠️ 'data' argument is missing. Using default 'data=coco8-seg.yaml'. Ultralytics 8.3.32 🚀 Python-3.12.7 torch-2.5.1 CPU (Apple M4) YOLO11n-seg summary (fused): 265 layers, 2,868,664 parameters, 0 gradients, 10.4 GFLOPs Dataset 'coco8-seg.yaml' images not found ⚠️, missing path '/Users/neo/PycharmProjects/netkiller/datasets/coco8-seg/images/val' Downloading https://ultralytics.com/assets/coco8-seg.zip to '/Users/neo/PycharmProjects/netkiller/datasets/coco8-seg.zip'... 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 439k/439k [00:00<00:00, 1.44MB/s] Unzipping /Users/neo/PycharmProjects/netkiller/datasets/coco8-seg.zip to /Users/neo/PycharmProjects/netkiller/datasets/coco8-seg...: 100%|████████ Dataset download success ✅ (2.6s), saved to /Users/neo/PycharmProjects/netkiller/datasets val: Scanning /Users/neo/PycharmProjects/netkiller/datasets/coco8-seg/labels/val... 4 images, 0 backgrounds, 0 corrupt: 100%|██████████| 4/4 [00:0 val: New cache created: /Users/neo/PycharmProjects/netkiller/datasets/coco8-seg/labels/val.cache Class Images Instances Box(P R mAP50 mAP50-95) Mask(P R mAP50 mAP50-95): 100%|███████ all 4 17 0.762 0.85 0.886 0.675 0.762 0.85 0.852 0.568 person 3 10 0.984 0.6 0.672 0.307 0.984 0.6 0.629 0.249 dog 1 1 0.738 1 0.995 0.895 0.738 1 0.995 0.895 horse 1 2 0.611 1 0.995 0.673 0.611 1 0.995 0.224 elephant 1 2 0.639 0.5 0.662 0.285 0.639 0.5 0.501 0.251 umbrella 1 1 0.672 1 0.995 0.995 0.672 1 0.995 0.895 potted plant 1 1 0.928 1 0.995 0.895 0.928 1 0.995 0.895 Speed: 1.2ms preprocess, 78.7ms inference, 0.0ms loss, 2.5ms postprocess per image Results saved to /Users/neo/PycharmProjects/netkiller/runs/segment/val 💡 Learn more at https://docs.ultralytics.com/modes/val
预测模型
(.venv) neo@Neo-Mac-mini netkiller % yolo segment predict model=yolo11n-seg.pt source='https://ultralytics.com/images/bus.jpg' Ultralytics 8.3.32 🚀 Python-3.12.7 torch-2.5.1 CPU (Apple M4) YOLO11n-seg summary (fused): 265 layers, 2,868,664 parameters, 0 gradients, 10.4 GFLOPs Downloading https://ultralytics.com/images/bus.jpg to 'bus.jpg'... ⚠️ Download failure, retrying 1/3 https://ultralytics.com/images/bus.jpg... ########################################################################################################################################### 100.0% image 1/1 /Users/neo/PycharmProjects/netkiller/bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 55.3ms Speed: 4.9ms preprocess, 55.3ms inference, 9.8ms postprocess per image at shape (1, 3, 640, 480) Results saved to /Users/neo/PycharmProjects/netkiller/runs/segment/predict 💡 Learn more at https://docs.ultralytics.com/modes/predict
使用自己刚刚训练的模型
yolo segment predict model=runs/segment/train3/weights/best.pt source='https://ultralytics.com/images/bus.jpg'
数据集
每个分类一个目录
cifar-10-/ | |-- train/ | |-- airplane/ | | |-- 10008_airplane.png | | |-- 10009_airplane.png | | |-- ... | | | |-- automobile/ | | |-- 1000_automobile.png | | |-- 1001_automobile.png | | |-- ... | | | |-- bird/ | | |-- 10014_bird.png | | |-- 10015_bird.png | | |-- ... | | | |-- ... | |-- test/ | |-- airplane/ | | |-- 10_airplane.png | | |-- 11_airplane.png | | |-- ... | | | |-- automobile/ | | |-- 100_automobile.png | | |-- 101_automobile.png | | |-- ... | | | |-- bird/ | | |-- 1000_bird.png | | |-- 1001_bird.png | | |-- ... | | | |-- ... | |-- val/ (optional) | |-- airplane/ | | |-- 105_airplane.png | | |-- 106_airplane.png | | |-- ... | | | |-- automobile/ | | |-- 102_automobile.png | | |-- 103_automobile.png | | |-- ... | | | |-- bird/ | | |-- 1045_bird.png | | |-- 1046_bird.png | | |-- ... | | | |-- ...
yolo classify train data=mnist160 model=yolo11n-cls.yaml epochs=100 imgsz=64
from ultralytics import YOLO if __name__ == '__main__': model = YOLO("yolo11n-cls.pt") # 加载预训练模型 model.train(data="data.yaml", # 数据集配置文件 epochs=100, imgsz=640, batch=16, device='0') # 使用GPU训练
默认尺寸 imgsz=224