{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

YOLO11 Training

\n", "

Matthias Bartolo

\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Package Imports

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "aFbKjvakDnZE", "outputId": "dbe28130-1112-4141-e265-65d3f6b06acc" }, "outputs": [], "source": [ "# !pip install --upgrade roboflow ultralytics" ] }, { "cell_type": "markdown", "metadata": { "id": "dWZ2DvvEDnZF" }, "source": [ "**

Required libraries.

**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "9Zbf3zFkDnZG" }, "outputs": [], "source": [ "import torch\n", "import os\n", "import ultralytics\n", "import locale\n", "import glob\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "\n", "from IPython import display\n", "from ultralytics import YOLO\n", "from IPython.display import display, Image\n", "from roboflow import Roboflow\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": { "id": "FyRdDYkqAKN4" }, "source": [ "**

Using GPU if one is available.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!nvidia-smi" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dgNdkO48DnZG", "outputId": "11ca77d8-7b09-4d02-a899-0dfdc5aaac2d" }, "outputs": [], "source": [ "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n", "print(device)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CjpPg4mGKc1v", "outputId": "7035eeee-b2d1-438c-bb57-188bd34ea8eb" }, "outputs": [], "source": [ "# Retrieving the current working directory\n", "HOME = os.getcwd()\n", "print(HOME)" ] }, { "cell_type": "markdown", "metadata": { "id": "3C3EO_2zNChu" }, "source": [ "**

Downloading the Roboflow dataset.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BSd93ZJzZZKt", "outputId": "cd51037c-4df9-415e-ecd8-540168fb544a" }, "outputs": [], "source": [ "if not os.path.isdir(os.path.join(HOME, 'datasets')):\n", " os.mkdir(os.path.join(HOME, 'datasets'))\n", "os.chdir(os.path.join(HOME, 'datasets'))\n", "\n", "\n", "rf = Roboflow(api_key=\"\")\n", "project = rf.workspace(\"\").project(\"\")\n", "version = project.version(2)\n", "dataset = version.download(\"yolov11\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**

Training the YOLO11 model.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_WRhnBXjDnZH", "outputId": "09bcb11a-5656-4c53-8564-51b29cc08d6c" }, "outputs": [], "source": [ "# Specifying the paths\n", "model_path = 'yolo11m.pt'\n", "\n", "yaml_path = dataset.location+\"/data.yaml\"\n", "\n", "# Creating YOLO object\n", "model = YOLO(model_path)\n", "\n", "# Specifying training parameters\n", "num_epochs = 100 # Number of epochs\n", "batch_size = 16 #8 # Adjust based on GPU memory\n", "image_size = 640 # Decrease for faster training\n", "\n", "# Training configuration\n", "train_config = {\n", " 'data': yaml_path,\n", " 'imgsz': image_size,\n", " 'batch': batch_size,\n", " 'epochs': num_epochs,\n", " 'device': 0, # Use GPU 0\n", " # 'workers': 1, # Number of data loading workers\n", " 'optimizer': 'Adam', # Use Adam optimizer\n", " 'cache': True,#'disk', # Cache images for faster training\n", " 'patience': 10, # epochs to wait before decreasing LR\n", " 'val': True, # Run validation during training\n", " 'plots': True, # Run plots during training\n", "}\n", "\n", "# Train the model\n", "model.train(**train_config)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "6ODk1VTlevxn" }, "source": [ "**

Validating the YOLO11 model on the Validation subset.

**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IkrxsRHoV67H", "outputId": "ecb3eac0-3a4f-4df6-fdb0-9db829ad7a76" }, "outputs": [], "source": [ "locale.getpreferredencoding = lambda: \"UTF-8\"\n", "# !pip install aspose-words" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YpyuwrNlXc1P", "outputId": "f4718557-cd29-4208-d5fa-ad0776a893b7" }, "outputs": [], "source": [ "model.val() #This will output a train file however it will be on the validation data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**

Validating the YOLO11 model on the Testing subset.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.val(split='test') #This will output a train file however it will be on the test data" ] }, { "cell_type": "markdown", "metadata": { "id": "i4eASbcWkQBq" }, "source": [ "**

Testing the YOLO11 model on the Testing subset.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Wjc1ctZykYuf", "outputId": "38b730a6-7d15-4f9f-b812-e814fc861557" }, "outputs": [], "source": [ "!yolo task=detect \\\n", "mode=predict \\\n", "model={HOME}/datasets/runs/detect/train1/weights/best.pt \\\n", "source={dataset.location}/test/images \\\n", "save=True" ] }, { "cell_type": "markdown", "metadata": { "id": "uBkrV5y5X9CH" }, "source": [ "**

Training Results.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 447 }, "id": "mWCxLBpMbKoQ", "outputId": "722f7b87-4d71-4e44-85aa-0c6a33f08362" }, "outputs": [], "source": [ "from IPython.display import Image as IPyImage\n", "\n", "IPyImage(filename=f'{HOME}/datasets/runs/detect/train/results.png', width=600)" ] }, { "cell_type": "markdown", "metadata": { "id": "t6EZwLBNfjKP" }, "source": [ "**

Testing Resultant Images.

**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "mzkcnDekgUWf" }, "outputs": [], "source": [ "counter =0\n", "limit = 10\n", "for image_path in glob.glob(f'{HOME}/datasets/runs/detect/predict/*.jpg'):\n", " display(Image(filename=image_path))\n", " print(\"\\n\")\n", " counter += 1\n", " if counter == limit:\n", " break" ] } ], "metadata": { "accelerator": "GPU", "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }