YOLOv10 on Custom Dataset
Tags: ai, machinelearning, deeplearning, python
Published: 2024-06-09 12:33:41

## What is YOLO?
**You Only Look Once (YOLO)** is a state-of-the-art, real-time object detection algorithm .

![YOLOv10 Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22c4x05mrv5pu3ijjo26.jpeg)

## What makes YOLO popular?
- Speed 
- Detection accuracy 
- Good generalization 
- Open-source

Google Colab is an excellent platform for running deep learning models due to its free access to GPUs and ease of use. This guide will walk you through the process of running the latest version, YOLOv10, on Google Colab.

### Before You Start
To make sure that you have access to GPU. You can use `nvidia-smi` command to do that. In case of any problems navigate to `Edit` -> `Notebook settings` -> `Hardware accelerator`, set it to `GPU`, and then click `Save`.
```
!nvidia-smi
```

### Install Required Packages
Clone the GitHub repository.
```
!git clone https://github.com/THU-MIG/yolov10.git
```
```
cd yolov10
```
```
!pip install .
```

## Upload Data To Colab

### Step 1: Mount Google Drive
```
from google.colab import drive
drive.mount('/content/drive')
```

### Step 2: Upload Files Directly
```
from google.colab import files
uploaded = files.upload()
```

### Step 3: Organize Data for YOLOv10

**Images:**
- The images directory contains subdirectories for train and val (validation) sets.
- Each subdirectory contains the corresponding images for training and validation.

**Labels:**
- The labels directory mirrors the images directory structure.
- Each text file in the labels/train and labels/val subdirectories contains the annotations for the corresponding images.

**Annotations Format:**
```
/my_dataset
  /images
    /train
      image1.jpg
      image2.jpg
      ...
    /val
      image1.jpg
      image2.jpg
      ...
  /labels
    /train
      image1.txt
      image2.txt
      ...
    /val
      image1.txt
      image2.txt
      ...
  data.yaml
```

**Data Configuration File (data.yaml):**
```
train: /content/my_dataset/images/train
val: /content/my_dataset/images/val

nc: N     # N for number of classes
names: ['class1', 'class2', ..., 'classN']
```

## Download Pre-trained Weights
```
import os
import urllib.request

# Create a directory for the weights in the current working directory
weights_dir = os.path.join(os.getcwd(), "weights")
os.makedirs(weights_dir, exist_ok=True)

# URLs of the weight files
urls = [
    "https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10n.pt",
    "https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10s.pt",
    "https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10m.pt",
    "https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10b.pt",
    "https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10x.pt",
    "https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10l.pt"
]

# Download each file
for url in urls:
    file_name = os.path.join(weights_dir, os.path.basename(url))
    urllib.request.urlretrieve(url, file_name)
    print(f"Downloaded {file_name}")
```

## Train Custom Model
```
!yolo task=detect mode=train epochs=100 batch=4 plots=True model=weights/yolov10n.pt data=data.yaml
```

## Inference on Image
```
!yolo task=detect mode=predict conf=0.25 save=True model=runs/detect/train/weights/best.pt source=img.jpg
```

## Inference on Video
```
!yolo task=detect mode=predict conf=0.25 save=True model=runs/detect/train/weights/best.pt source=video.mp4

```

## Summary
This guide covers running YOLOv10 on Google Colab by setting up the environment, installing necessary libraries, and running inference with pre-trained weights. It also explains how to upload and organize data in Colab for YOLOv8, including the required directory structure and configuration files. These steps enable efficient training and inference for object detection models using Colab's resources.