Creating Datasets for YOLOv8 +
Guide: Labeling Images & Training YOLOv8 for Object Detection
1. Image Collection
Before labeling, you need a dataset of images containing the objects you want to detect.
Ensure a diverse dataset (different backgrounds, angles, lighting).
Use at least 5000–1000 images for decent results.
Resize images (if necessary) to a standard resolution (e.g., 640x640).
2. Annotation (Labeling)
To train a YOLOv8 model, you need to annotate your images with bounding boxes.
Tools for Labeling
You can use various tools to label your dataset:
LabelImg (for manual labeling)
Labeling Process
Open the labeling tool and load your images.
Draw bounding boxes around each object.
Assign a class label to each object.
Save the annotations in YOLO format.
YOLO Annotation Format
Each image should have a corresponding .txt
file with the same name, containing lines in this format:
<class_id> <x_center> <y_center> <width> <height>
class_id: Index of the object class (starting from 0).
x_center, y_center: Bounding box center (normalized, 0 to 1).
width, height: Bounding box dimensions (normalized).
Example for a cat (class 0) and dog (class 1):
0 0.45 0.60 0.30 0.40
1 0.70 0.50 0.25 0.35
💡 Tip: Always normalize bounding box coordinates relative to image size.
3. Organizing Dataset
Your dataset should be structured as follows:
/dataset
├── images
│ ├── train (80% of images)
│ ├── val (10% of images)
│ ├── test (10% of images)
├── labels
│ ├── train (corresponding .txt files)
│ ├── val
│ ├── test
├── data.yaml (dataset configuration file)
data.yaml File
Create a data.yaml
file that defines your dataset:
train: /path/to/dataset/images/train
val: /path/to/dataset/images/val
test: /path/to/dataset/images/test
nc: 2 # Number of classes
names: ["cat", "dog"]
Replace "cat", "dog"
with your actual class names.
4. Training the YOLOv8 Model
Make sure you have installed Ultralytics YOLOv8:
pip install ultralytics
Start Training
Run the following command to train your model:
from ultralytics import YOLO
model = YOLO("yolov8n.pt") # Load pre-trained model
results = model.train(data="data.yaml", epochs=50, imgsz=640, batch=16, device="cuda")
Training Parameters Explained
"yolov8n.pt"
→ Start with YOLOv8 nano; switch toyolov8s.pt
,yolov8m.pt
, oryolov8l.pt
for better accuracy.epochs=50
→ Number of training cycles (increase for better results).imgsz=640
→ Image size (should match dataset).batch=16
→ Number of images per batch (adjust based on GPU memory).device="cuda"
→ Use GPU (change to"cpu"
if needed).
5. Evaluating the Model
After training, you can evaluate the model:
metrics = model.val()
mAP (Mean Average Precision) is the key metric for object detection.
Precision & Recall help measure how well the model detects objects.
Testing on Images
Run inference on a test image:
results = model("test_image.jpg", show=True, save=True)
This will display and save the output with detected objects.
6. Exporting the Model
You can export the trained model to different formats:
model.export(format="onnx", imgsz=320) # Convert to ONNX for optimized inference
Supported formats: ONNX
Final Tips
✔ More Data = Better Model
✔ Augment Data = Improve Generalization (flip
, rotate
, color jitter
)
✔ Higher Epochs = Better Accuracy (but watch for overfitting)
✔ Monitor Loss & mAP = Track Performance (tensorboard --logdir=runs
)
Last updated