This project implements a roadblock detection system using the YOLOv8 model to identify roadblocks (e.g., cones, barriers) in images. The model is trained on a custom dataset and can be used for real-time detection in traffic scenarios.
- Hardware: CPU or NVIDIA GPU (e.g., RTX 3060 with CUDA 11.8)
- OS: Windows/Linux
- Software:
- Python 3.8+
- PyTorch 2.4.1+cu118
- torchvision 0.19.1+cu118
- ultralytics 8.3.x
- opencv-python, numpy
- Set up Python environment:
python -m venv yolo_env source yolo_env/bin/activate # Linux/Mac yolo_env\Scripts\activate # Windows
- Install dependencies:
pip install torch==2.4.1 torchvision==0.19.1 torchaudio==2.4.1 --index-url https://download.pytorch.org/whl/cu118 pip install ultralytics opencv-python numpy
- Location:
block_data/
- Structure:
block_data/ ├── images/ │ ├── train/ │ ├── val/ │ ├── test/ ├── labels/ │ ├── train/ │ ├── val/ │ ├── test/ ├── data.yaml
- Format:
- Images:
.jpg
- Labels:
.txt
(YOLO format:class_id x_center y_center width height
, normalized to 0-1) data.yaml
:train: ./images/train val: ./images/val test: ./images/test nc: 1 names: ['block']
- Images:
- Train the model (in Jupyter Notebook or Python script):
from ultralytics import YOLO model = YOLO("yolov8n.pt") model.train( data="block_data/data.yaml", epochs=50, imgsz=416, batch=8, device=0, # Use GPU (or 'cpu' if no GPU) amp=False, name='block_train', project='runs/train', exist_ok=True )
- Output:
- Trained weights:
runs/train/block_train/weights/best.pt
- Training logs:
runs/train/block_train/results.png
- Trained weights:
- Test on a single image:
from ultralytics import YOLO model = YOLO("runs/train/block_train/weights/best.pt") results = model("block_data/images/test/test_image.jpg", device=0) results.show() results.save(save_dir='runs/detect/test')
- Evaluate on validation set:
model.val(data="block_data/data.yaml", device=0)
- Local:
- Run training/testing in Jupyter Notebook or Python scripts.
- Remote (Company Server):
- Upload
block_data
and scripts to~/yolo_project
via FileZilla (IP: 10.31.15.53, user: zhusongyu, password: zhusongyu123, port: 22). - Run on server:
ssh zhusongyu@10.31.15.53 -p 22 cd ~/yolo_project source yolo_env/bin/activate python train_yolov8.py
- Upload
- GPU Issues: If
NotImplementedError
occurs, ensuretorchvision
is CUDA-enabled (0.19.1+cu118
). Usedevice='cpu'
as fallback. - Dataset: Ensure each image has a corresponding label file. Split dataset into train/val/test (80:10:10) using
split_dataset.py
. - Performance: Adjust
epochs
,imgsz
, orbatch
based on dataset size and hardware.