This project demonstrates the training and deployment of YOLOv8 models for detecting potholes in road images. The goal is to provide a robust solution for identifying road imperfections, which can be crucial for road maintenance and safety applications.
- Features
- Getting Started
- Dataset
- Training
- Inference
- Exported Model (ONNX)
- Acknowledgements
- Screenshots of Detection Outputs
- Pothole detection using YOLOv8 (nano, small, and medium variants).
- Training on a custom dataset (
potholes.v1i.yolov8). - Performance evaluation with mAP50, mAP50-95, Precision, and Recall metrics.
- Export of the best-performing model to ONNX format for optimized deployment.
- Demonstration of inference with both
.ptand ONNX models. - Discussion of real-time inference considerations and deployment strategies.
- Python 3.8+
- Git
- Google Drive (for dataset storage, if following this notebook exactly)
- A GPU (recommended for faster training and inference)
- Clone the repository:
git clone https://github.com/MikeTx56/yolov8-pothole-detection.git cd yolov8-pothole-detection - Create a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install dependencies:
pip install -r requirements.txt
The project utilizes the potholes.v1i.yolov8 dataset. This dataset is expected to be stored in your Google Drive at /content/drive/MyDrive/potholes.v1i.yolov8.
Dataset link:
Ensure the dataset contains data.yaml, train/, valid/, and test/ directories with images and corresponding labels.
To use your own dataset, update the DATASET_PATH variable in the notebook and modify data.yaml as needed.
The YOLOv8 models (nano, small, medium) were trained for 10 epochs with an image size of 640x640 and a batch size of 16.
from ultralytics import YOLO
DATA_YAML = "/content/drive/MyDrive/potholes.v1i.yolov8/data.yaml"
# YOLOv8n Training
model_n = YOLO("yolov8n.pt")
results_n = model_n.train(data=DATA_YAML, epochs=10, imgsz=640, batch=16, name="yolov8n_custom")
# YOLOv8s Training
model_s = YOLO("yolov8s.pt")
results_s = model_s.train(data=DATA_YAML, epochs=10, imgsz=640, batch=16, name="yolov8s_custom")
# YOLOv8m Training
model_m = YOLO("yolov8m.pt")
results_m = model_m.train(data=DATA_YAML, epochs=10, imgsz=640, batch=16, name="yolov8m_custom")Training results, including plots and best weights, are saved in the runs/detect/ directory.
Here's a comparison of the trained models based on validation metrics after training:
| Model | mAP50 | mAP50-95 | Precision | Recall | Training Time (Epochs) | Inference Speed (ms/img) | Model Size (MB) |
|---|---|---|---|---|---|---|---|
| YOLOv8n | 0.845 | 0.463 | 0.840 | 0.732 | 10 (partial logs) | 4.6 | ~12 |
| YOLOv8s | 0.897 | 0.526 | 0.850 | 0.851 | 10 (partial logs) | 10.1 | ~44 |
| YOLOv8m | 0.671 | 0.338 | 0.617 | 0.606 | 10 (partial logs) | 23.3 | ~103 |
| YOLOv8s (ONNX) | N/A | N/A | N/A | N/A | N/A | 21.8 | 42.7 |
The yolov8s_custom model showed the best overall performance among the .pt models after training.
To run inference on an image using the trained .pt model:
from ultralytics import YOLO
import matplotlib.pyplot as plt
import cv2
import os
TEST_IMAGES = "/content/drive/MyDrive/potholes.v1i.yolov8/test/images"
img_path = os.path.join(TEST_IMAGES, "sample_image.jpg") # Replace with your image path
trained_model_s = YOLO("runs/detect/yolov8s_custom/weights/best.pt")
results = trained_model_s(img_path)
for r in results:
im_array = r.plot() # draw boxes
plt.imshow(cv2.cvtColor(im_array, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.show()For real-time applications, you would typically integrate the ONNX model with a video stream (webcam or file). The process involves:
- Capturing frames using
cv2.VideoCapture. - Preprocessing frames to match model input.
- Running inference with the ONNX model.
- Post-processing predictions (boxes and confidence).
- Displaying detections in a loop.
The yolov8s_custom model, identified as the best performer, was exported to ONNX format for deployment flexibility and optimized inference. The exported model is located at runs/detect/yolov8s_custom/weights/best.onnx.
from ultralytics import YOLO
trained_model_s = YOLO("runs/detect/yolov8s_custom/weights/best.pt")
trained_model_s.export(format="onnx")
onnx_model = YOLO("runs/detect/yolov8s_custom/weights/best.onnx")
print(f"ONNX model loaded successfully from: {onnx_model.model_name}")- Ultralytics for the YOLOv8 framework.
- Roboflow for dataset management and annotation tools.
- Google Colab for the development environment.
