跳到正文
DEV Community· Mango Developer·· 1 小時前AI 評分40

YOLOv8 自訂電腦視覺模型訓練指南:從數據集到部署

Training a Custom Computer Vision Model with YOLOv8: From Dataset to Deployment

AI 導讀

YOLOv8 自訂電腦視覺模型訓練指南涵蓋從數據集準備到部署的完整流程,並比較 YOLOv8n、YOLOv8s、YOLOv8m、YOLOv8l 和 YOLOv8x 的參數量、COCO mAP 及推理速度。指南指出,訓練所需時間和成本視硬件而異,數據集及標籤質素是模型效果的重要因素。

正文

The first time I trained a YOLO model, it was 2018 and I was trying to detect solar panels in satellite imagery. The setup took a week. Converting annotations, fighting Darknet's C build system, debugging CUDA mismatches, hand-writing a data loader in C. By the time it actually trained, I'd forgotten why I started.

YOLOv8 is a different universe. Ultralytics rewrote the experience from scratch-Python-first, pip-installable, sane defaults, one-command training. You can go from a folder of labeled images to a deployed model in an afternoon. But the ease hides a lot of decisions that matter for production. This guide walks through the full lifecycle, with the parts that actually bite.

$32

YOLOv8 Variants: Picking the Right Size

Ultralytics ships five sizes. The trade-off is speed vs. accuracy.

Variant Params mAP (COCO) Inference (V100, ms) Use case
YOLOv8n 3.2M 37.3 ~1.2 Edge/mobile, CPU inference
YOLOv8s 11.2M 44.9 ~2.1 Balanced default for most projects
YOLOv8m 25.9M 50.2 ~4.7 Higher accuracy, still fast
YOLOv8l 43.7M 52.9 ~7.2 When accuracy matters more than speed
YOLOv8x 68.2M 53.9 ~10.1 Max accuracy, needs serious hardware

My defaults: yolov8s for prototyping, yolov8m for production if I have a GPU, yolov8n for edge/CPU. The jump from n to s is the best ROI-accuracy goes up significantly for a small speed cost. The jump from l to x is rarely worth it.

Prerequisites

  • A GPU is strongly recommended. YOLOv8 will train on CPU but it's 50-100x slower. A single RTX 3060 (12GB) handles YOLOv8m comfortably.
  • Python 3.8-3.11.
  • 10GB+ free disk for datasets and weights.
  • For data: 100+ images per class minimum, 500+ for production quality.

If you don't have a local GPU, Google Colab free tier (T4) trains YOLOv8s on a small dataset in under an hour.

Installation

python -m venv .venv
source .venv/bin/activate
pip install ultralytics
# Verify
yolo version
python -c "from ultralytics import YOLO; print('ok')"

The ultralytics package bundles PyTorch, OpenCV, and everything else. If you need a specific PyTorch version (for CUDA compatibility), install PyTorch first with the right index URL, then ultralytics.

# CUDA 12.1
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
pip install ultralytics

Verify GPU is visible:

import torch
print(torch.cuda.is_available()) # True
print(torch.cuda.get_device_name(0)) # e.g., 'NVIDIA GeForce RTX 3060'

$33

$34

$35

$36

$37

$38

$39

$3a

$3b

$3c

Cost Considerations

Training. A typical YOLOv8s training run on 1000 images, 100 epochs: ~1-2 hours on an RTX 3060 (~$0.50 on RunPod), 30-60 minutes on an A100 (~$1.50).

Inference. A single A10 ($0.6/hr on RunPod) serves ~200 FPS at 640x640. That's enough for most real-time use cases.

Edge deployment. Jetson Nano 4GB (~$150) runs YOLOv8n at ~15-30 FPS. Raspberry Pi 5 with Hailo-8L accelerator (~$130) hits similar. For always-on edge, total cost is dominated by hardware.

Setup Use case One-time Monthly
RTX 3060 desktop Dev/training $600 ~$10 power
RunPod A10 Occasional training + serving $0 $0.6/hr
Jetson Nano Edge inference $150 ~$2 power
Cloud GPU endpoint (A10) Prod serving $0 $50-200
Roboflow Pro Annotation + hosting $0 $50

Real-World Use Cases

  • Retail inventory. Detect out-of-stock items on shelves from phone photos. YOLOv8s trained on 2000 store images, deployed on edge devices.
  • Manufacturing defect detection. YOLOv8m on a production line, 60 FPS, catches defects with 95%+ precision. Replaces manual inspection.
  • Parking management. YOLOv8n + ByteTrack on RTSP streams, counts available spaces in real time.
  • Agriculture. Drone imagery + YOLOv8 detects crop diseases. Edge deployment on the drone itself.
  • Security. Person/vehicle detection on CCTV feeds with privacy filters (no face recognition, just presence).
  • Medical imaging. YOLOv8 for lesion detection on X-rays (with clinical validation-don't ship without it).

Wrapping Up

YOLOv8 is the first object detector that I can hand to a junior engineer and trust they'll have a working model in a day. The framework gets out of your way. The hard parts-data prep, label quality, deployment-are the same as ever, but at least the training itself isn't a fight.

If you take one thing from this: your dataset matters more than your model. A YOLOv8n with 2000 carefully labeled images beats a YOLOv8x with 200 sloppy ones. Spend your time on data, not on chasing the latest architecture.


Originally published on Mango Developer.

來源:DEV Community · dev.to