File size: 2,335 Bytes
fa3ff47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "**YOLOv8 model training using ultralytics**"
      ],
      "metadata": {
        "id": "7PihFE53D92W"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "toxMDptj5Psd"
      },
      "outputs": [],
      "source": [
        "!pip install ultralytics"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from ultralytics import YOLO\n",
        "import os"
      ],
      "metadata": {
        "id": "0Jw1Ow2A5_Sf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Path to the root directory that contains config.yaml file and the dataset\n",
        "# annotated in the YOLOv8 format\n",
        "ROOT_DIR = \"Path\"\n",
        "\n",
        "# Path to the pretrained model if exists\n",
        "PRETRAINED_MODEL_PATH = \"Path\"\n",
        "\n",
        "# Number of iterations (forward pass) to train the model\n",
        "NUM_EPOCHS = 100"
      ],
      "metadata": {
        "id": "NDclcuR05QZP"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def train_model(load_model=False):\n",
        "  if load_model:\n",
        "    model = YOLO(PRETRAINED_MODEL_PATH)\n",
        "    results = model.train(data=os.path.join(ROOT_DIR, \"config.yaml\"),\n",
        "                          epochs=NUM_EPOCHS)\n",
        "  else:\n",
        "    model = YOLO(\"yolov8n.yaml\")\n",
        "    results = model.train(data=os.path.join(ROOT_DIR, \"config.yaml\"),\n",
        "                          epochs=NUM_EPOCHS)\n",
        ""
      ],
      "metadata": {
        "id": "PAYhaotC5QcB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "train_model()"
      ],
      "metadata": {
        "id": "mCxI3Xlr7Ryu"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}