Master YOLOv8: End-to-End Guide to Object Detection, Training, and Deployment
This comprehensive tutorial walks you through YOLOv8 object detection—from environment setup and dataset preparation to model training, validation, testing, and conversion to ONNX and TensorRT—providing clear commands, code snippets, and visual results for each step.
Object Detection Overview
Object detection is a core computer‑vision task that identifies and localizes specific targets in images or videos using deep‑learning models such as convolutional neural networks (CNN). It powers applications like face detection, pedestrian detection, and vehicle detection in security, autonomous driving, and smart retail.
YOLOv8 Introduction
YOLOv8, released by Ultralytics on January 10, 2023, is the next major update after YOLOv5. It supports image classification, object detection, and instance segmentation, offering new SOTA architectures (P5 640, P6 1280) and a range of model sizes (N/S/M/L/X). Unlike earlier anchor‑based versions, YOLOv8 adopts a novel design.
Source Code Acquisition
The official repository is https://github.com/ultralytics/ultralytics .
Environment Setup
CPU environment
conda create -n YOLOv8 python==3.8.1</code>
<code>pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simpleGPU environment (CUDA, cuDNN, PyTorch, etc.) – see Aliyun guide .
# Install CUDA, cuDNN, Python, PyTorch, Torchvision (compatible versions)</code>
<code>pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simpleDownloading Model and Test Image
Download yolov8n.pt and bus.jpg from the official assets:
yolov8n.pt
bus.jpg
Run a quick prediction:
yolo predict model=yolov8n.pt source='ultralytics/data/images/bus.jpg'Dataset Preparation
Prepare a dataset in YOLO format (TXT label files alongside original images). Refer to the YOLOv5 tutorial for details: dataset guide .
Model Training
Method 1 – Python API
from ultralytics import YOLO</code>
<code># Load a pretrained model</code>
<code>model = YOLO("yolov8n.pt")</code>
<code>model.train(data="ultralytics/cfg/mask.yaml", epochs=3)</code>
<code>metrics = model.val()</code>
<code>results = model("ultralytics/data/images/bus.jpg")</code>
<code>path = model.export(format="onnx")Method 2 – CLI
yolo task=detect mode=train model=yolov8n.pt data=ultralytics/cfg/mask.yaml epochs=3 batch=16Other Tasks (detect, segment, classify, pose) can be run by loading the appropriate YAML config and specifying the dataset via the data argument.
Model Validation
yolo task=detect mode=val model=runs/detect/train/weights/best.pt data=ultralytics/cfg/mask.yaml device=cpuModel Testing
yolo task=detect mode=predict model=runs/detect/train/weights/best.pt source=ultralytics/data/images device=cpuModel Conversion
ONNX Export
yolo export model=yolov8s.pt format=onnx opset=12Alternatively, use the Ultralytics API to embed post‑processing (bbox decoder, NMS) into the ONNX model.
TensorRT Conversion
python export-det.py \</code>
<code>--weights yolov8s.pt \</code>
<code>--iou-thres 0.65 \</code>
<code>--conf-thres 0.25 \</code>
<code>--topk 100 \</code>
<code>--opset 11 \</code>
<code>--sim \</code>
<code>--input-shape 1 3 640 640 \</code>
<code>--device cuda:0Using TensorRT’s trtexec.exe:
trtexec.exe --onnx=best.onnx --saveEngine=best.engine --fp16Or build with the provided Python script:
python3 build.py \</code>
<code>--weights yolov8s.onnx \</code>
<code>--iou-thres 0.65 \</code>
<code>--conf-thres 0.25 \</code>
<code>--topk 100 \</code>
<code>--fp16 \</code>
<code>--device cuda:0Full inference code and additional resources are available at the YOLOv8‑TensorRT GitHub repository and the Aliyun article links.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
