Back to Blog
Computer Vision March 12, 2025 8 min read

YOLOv8 Custom Training: From Dataset to Production API

End-to-end guide to training YOLOv8 on a custom dataset — annotation, training, evaluation, and deploying as a FastAPI endpoint with ONNX export.

Dataset Preparation

  1. Annotate with Roboflow or LabelImg
  2. Export in YOLOv8 format
  3. Data split: 80/10/10 train/val/test

Training

from ultralytics import YOLO

model = YOLO('yolov8n.pt')  # nano for fast prototyping
results = model.train(
    data='dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    augment=True,
)

ONNX Export for Production

model.export(format='onnx', dynamic=True, simplify=True)

FastAPI Endpoint

@app.post('/detect')
async def detect(file: UploadFile):
    img = Image.open(file.file)
    results = model(img)
    return {'detections': results[0].boxes.json()}
YOLOv8Object DetectionFastAPIONNXProduction
O

Ossama Elhakki

AI Engineer & ML Systems Builder — Morocco