This project demonstrates how to train and use a YOLOv8 (You Only Look Once v8) model for custom object detection using the Ultralytics YOLO library.
The model is trained on a custom dataset and can perform real-time object detection on:
- Images 🖼️
- Videos 🎥
- Webcam streams 📷
YOLOv8 is a state-of-the-art, one-stage object detection architecture that provides high accuracy and real-time performance for edge and embedded devices.
Ensure your dataset is organized as follows before training:
If you’re not familiar with how to create your own dataset — including image labeling and annotation generation — you can refer to the following guide:
dataset/
├── images/
│ ├── train/
│ └── val/
├── labels/
│ ├── train/
│ └── val/
└── data.yaml
You can use any IDE or code editor such as:
- Visual Studio Code (VS Code)
- Eclipse
- PyCharm
# Create virtual environment
python -m venv venvvenv\Scripts\activatesource venv/bin/activateMake sure your virtual environment is active, then install the required libraries:
pip install ultralytics torch torchvision opencv-python pyyamlAlternatively, create a requirements.txt file and run:
pip install -r requirements.txtYou can train your YOLOv8 model using the modeltraining.py script or directly from the terminal.
from ultralytics import YOLO
# Load a pre-trained YOLOv8 model (e.g., small version)
model = YOLO("yolov8s.pt")
# Train the model on your custom dataset
model.train(data="dataset/data.yaml", epochs=100, imgsz=640, batch=16, name="custom_yolo_train")yolo detect train data=dataset/data.yaml model=yolov8s.pt epochs=100 imgsz=640 batch=16 name=custom_yolo_trainFor this project, I trained the YOLOv8 model to detect three custom object classes. Below are the results obtained after training, and I’ve also included my training outputs and evaluation results under the runs/ directory.
After training, YOLOv8 will automatically create a directory:
runs/train/custom_yolo_train/
Inside it, you’ll find:
weights/best.pt→ Best performing modelweights/last.pt→ Model state from last epochresults.csv→ Training logsresults.png→ Training graphs (loss, mAP, precision, recall)
Once you have your trained weights (best.pt), use the following script to detect objects on images, videos, or webcam:
from ultralytics import YOLO
# Load trained model
model = YOLO("runs/train/custom_yolo_train/weights/best.pt")
# Run inference on an image
results = model.predict(source="test_images/image1.jpg", conf=0.5, show=True)- Images:
source="path/to/image.jpg" - Videos:
source="path/to/video.mp4" - Webcam:
source=0
Detected results will be saved in:
runs/detect/predict/
After training, YOLOv8 automatically computes performance metrics such as:
- mAP (Mean Average Precision)
- Precision
- Recall
- F1 Score
You can visualize these metrics and training curves inside:
runs/train/custom_yolo_train/
Best weights will be saved at:
runs/train/custom_yolo_train/weights/best.pt
Use this file for inference, evaluation, or deployment.
YOLOv8 supports exporting to multiple formats for deployment:
yolo export model=runs/train/custom_yolo_train/weights/best.pt format=onnxAvailable formats: onnx, torchscript, coreml, tflite, openvino, engine (TensorRT).
| Problem | Possible Cause | Solution |
|---|---|---|
| No labels found | Label files missing or not matching image names | Ensure each image has a .txt label with the same name |
| CUDA not available | PyTorch not installed with GPU support | Reinstall PyTorch with CUDA enabled |
| Low mAP / accuracy | Poor labeling or small dataset | Increase dataset size or improve label quality |
| Out of memory (OOM) | Batch size too large for GPU | Reduce batch size or use smaller image size |
SAI CHAITANYA KANCHARANA 📧kancharanasaichaitanya@gmail.com 🌐https://github.com/saichaitanya-c/
