YOLOv8 is used here to train a custom object detector for ID cards. Start by making the dataset usable by Ultralytics YOLO, then train a pretrained model against that dataset. For a first run, YOLOv8n—the Nano variant—reduces compute and memory demands, making it useful for testing feasibility on limited hardware. section 7.3.1 section 8.1.2
Listing 7.9 Configuration training YOLOv8 with a custom dataset
path: /home/jovyan/data/ train: "train/images" val: "val/images" test: "test/images" names: 0: id_card
The dataset YAML gives YOLOv8 a root path, image locations for training, validation, and test, and the zero-indexed class name. Each split also needs a corresponding labels directory; an image and its label share a filename stem. section 7.3.1
Listing 8.3 Writing the data configuration to YAML
from ultralytics import YOLO
model = YOLO('yolov8n.pt') #1
results = model.train( #2
data='custom_data.yaml',
imgsz=640,
epochs=epochs,
batch=batch,
name='yolov8n_custom',
project=project_path) #3
Load the pretrained Nano weights with `YOLO('yolov8n.pt')`, then pass the dataset YAML, image size, epochs, batch size, run name, and artifact directory to `model.train()`. In a Kubeflow component, the project path collects weights and evaluation artifacts. section 8.1.2
Check the operational trade-offs before running training:
- Keep images and labels paired when preparing the three dataset splits; a different folder or naming convention calls for checking the relevant format documentation. section 7.3.1
- Choose model size for the deployment constraint: smaller YOLOv8 models favor speed and memory use, while larger ones can improve accuracy at greater computational cost. section 8.1.2
- Set epochs, batch size, and image size with available compute and memory in mind. Inspect saved inference samples, confusion matrices, and precision and recall curves as training progresses. section 8.1.2
