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.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Master YOLOv8: End-to-End Guide to Object Detection, Training, and Deployment

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/simple

GPU 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/simple

Downloading 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=16

Other 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=cpu

Model Testing

yolo task=detect mode=predict model=runs/detect/train/weights/best.pt source=ultralytics/data/images device=cpu

Model Conversion

ONNX Export

yolo export model=yolov8s.pt format=onnx opset=12

Alternatively, 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:0

Using TensorRT’s trtexec.exe:

trtexec.exe --onnx=best.onnx --saveEngine=best.engine --fp16

Or 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:0

Full inference code and additional resources are available at the YOLOv8‑TensorRT GitHub repository and the Aliyun article links.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

object detectionTensorRTModel TrainingYOLOv8ONNX
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.