YOLOv5: How to Train a Object Detection Model on Custom Dataset

4 min read 1 month ago
Published on Aug 03, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial guides you through the process of training a custom object detection model using YOLOv5 with your own dataset. By the end of this tutorial, you'll be able to create a model that can detect specific objects in images, utilizing tools like Roboflow and Google Colab. This is particularly useful for developers and researchers looking to implement object detection in their applications.

Chapter 1: Setting Up Your Environment

  1. Access YOLOv5 Repository

    • Visit the YOLOv5 GitHub repository provided by Ultralytics.
    • Familiarize yourself with the documentation and available models.
  2. Install Required Libraries

    • Use the following command to install YOLOv5:
      pip install -r requirements.txt
      
    • Ensure you have Python and PyTorch installed.
  3. Join the Community

    • Consider joining relevant Discord servers or online communities for support and discussion on computer vision and deep learning.

Chapter 2: Creating a Custom Dataset

  1. Sign Up for Roboflow

    • Create an account on Roboflow to manage your dataset.
  2. Create a New Project

    • Start a new project in Roboflow and upload your images.
  3. Annotate Images

    • Use the annotation tools in Roboflow to draw bounding boxes around the objects you want to detect.
    • Save the class names for your objects (e.g., "mock").
  4. Split Dataset

    • Divide your dataset into training, validation, and test sets.
    • For instance, use a split of 70 images for training and 20 for validation.
  5. Apply Data Augmentation

    • Use Roboflow's data augmentation features to enhance your dataset. Options include:
      • Horizontal and vertical flips
      • Rotations (e.g., between -30 and +30 degrees)
  6. Generate the Dataset

    • Click on the Generate button to create a new version of your dataset. Choose a suitable size for augmented images.
  7. Export the Dataset

    • Download the dataset in YOLOv5 PyTorch format or use the provided code snippet to import it directly into Google Colab.

Chapter 3: Setting Up Google Colab

  1. Open Google Colab

    • Start a new notebook in Google Colab.
  2. Clone the YOLOv5 Repository

    • Use the following command to clone the repository:
      !git clone https://github.com/ultralytics/yolov5.git
      
  3. Install Dependencies

    • Run the command to install the necessary libraries:
      %cd yolov5
      !pip install -r requirements.txt
      
  4. Set Runtime for GPU

    • Navigate to Runtime > Change runtime type and select GPU as the hardware accelerator.
  5. Download Dataset

    • Paste the Roboflow code snippet to download your dataset directly from Roboflow.

Chapter 4: Training the YOLOv5 Model

  1. Prepare Training Script

    • Run the training script with the following command:
      !python train.py --img 416 --batch 16 --epochs 100 --data dataset.yaml --weights yolov5s.pt --cache
      
    • Adjust parameters as necessary:
      • img: Image dimensions (e.g., 416)
      • batch: Batch size (e.g., 16)
      • epochs: Number of training epochs (e.g., 100)
  2. Monitor Training Progress

    • Observe the output for training metrics, including precision and loss values.
  3. Use TensorBoard

    • Load TensorBoard to visualize training results:
      %load_ext tensorboard
      %tensorboard --logdir runs/train
      

Chapter 5: Running Inference

  1. Perform Predictions

    • Use the detection script to run predictions on your validation images:
      !python detect.py --weights runs/train/exp/weights/best.pt --img 416 --conf 0.2 --source val/images
      
  2. Review Results

    • Check the output images saved in the detection folder to see the bounding boxes drawn around detected objects.
  3. Download Your Model

    • Save the best model weights for future use:
      !cp runs/train/exp/weights/best.pt /content/
      

Conclusion

You have successfully created a custom object detection model using YOLOv5 and your own dataset. You learned how to set up your environment, annotate images, train your model, and run inference on new images. This foundational knowledge enables you to explore and expand your projects further, including deploying your model in applications with live detection capabilities. Keep experimenting with different datasets and augmentation techniques to improve your model's performance!