{ "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": [] } ] }