YOLOv8: Evaluating Models on Test Data

Oliver Lövström
Internet of Technology
2 min readMar 23, 2024
Photo by Mariia Shalabaieva on Unsplash

Ultralytics YOLO doesn’t provide a separate mode for evaluating test data. However, there’s an easy workaround.

Training a Model

Assuming you already have a trained model, for a comprehensive guide on YOLOv8 training, visit this page:

Now, you should have .pt model.

Setting Up the Test Directory

Each image file in the test directory should have a corresponding annotation .txt file, the files shall have matching names:

path/to/
├── empty/
├── train/
│ ├── img_000696.png
│ ├── img_000696.txt
│ ├── ...
│ ├── img_000708.png
│ ├── img_000708.txt

Creating Data YAML

To perform validation on YOLOv8, we need a data YAML file. This file provides the path to the training and validation data and specifies the number of classes nc, and the names. Let the train path point to the location of the empty directory, while the val path point to the test directory:

# my_data.yaml
train: /path/to/empty
val: /path/to/test
nc: 1
names:
0: Hands

Code

Load the model and perform validation:

from ultralytics import YOLO

# Load model
model = YOLO("best.pt")

# Perform validation on data
model.val(data="my_data.yaml")

Output

Similar to the training process, the results are written to /runs/<task>:

runs
└── detect
├── val
│ ├── F1_curve.png
│ ├── PR_curve.png
│ ├── P_curve.png
│ ├── R_curve.png
│ ├── confusion_matrix.png
│ ├── confusion_matrix_normalized.png
│ ├── val_batch0_labels.jpg
│ ├── val_batch0_pred.jpg
│ ├── val_batch1_labels.jpg
│ ├── val_batch1_pred.jpg
│ ├── val_batch2_labels.jpg
│ └── val_batch2_pred.jpg

Further Reading

If you want to learn more about programming and, specifically, machine learning, see the following course:

Note: If you use my links to order, I’ll get a small kickback.

--

--