{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"gpuType":"T4","authorship_tag":"ABX9TyOCqa7FkdGyF/CF5ru7IEXm"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"code","source":["# This mounts your Google Drive to the Colab VM.\n","from google.colab import drive\n","drive.mount('/content/drive')\n","\n","%cd /content/drive/My\\ Drive/My\\ Projects/yolo_custom_training"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"fl0fMqnr1O1V","executionInfo":{"status":"ok","timestamp":1689616601309,"user_tz":240,"elapsed":23958,"user":{"displayName":"Vaishanth Ramaraj","userId":"09348595743611064915"}},"outputId":"2add0d4d-0982-47f9-8bbd-a0ead1a50a06"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\n","/content/drive/My Drive/My Projects/yolo_custom_training\n"]}]},{"cell_type":"code","source":["import os\n","HOME = os.getcwd()\n","print(HOME)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5lwQa3PijESC","executionInfo":{"status":"ok","timestamp":1689616604881,"user_tz":240,"elapsed":136,"user":{"displayName":"Vaishanth Ramaraj","userId":"09348595743611064915"}},"outputId":"964cd55b-1f1e-44f9-c853-d6d9fcee16f0"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/My Drive/My Projects/yolo_custom_training\n"]}]},{"cell_type":"markdown","source":["## Install YOLOv8"],"metadata":{"id":"I6NSNz9zbTDA"}},{"cell_type":"code","source":["# Pip install method (recommended)\n","!pip install ultralytics==8.0.20\n","\n","from IPython import display\n","display.clear_output()\n","\n","import ultralytics\n","ultralytics.checks()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NO5AyAFMbH1I","executionInfo":{"status":"ok","timestamp":1689616619862,"user_tz":240,"elapsed":13292,"user":{"displayName":"Vaishanth Ramaraj","userId":"09348595743611064915"}},"outputId":"81894275-1307-4f83-90ce-8cc31b480d4e"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stderr","text":["Ultralytics YOLOv8.0.20 🚀 Python-3.10.12 torch-2.0.1+cu118 CUDA:0 (Tesla T4, 15102MiB)\n","Setup complete ✅ (2 CPUs, 12.7 GB RAM, 24.3/78.2 GB disk)\n"]}]},{"cell_type":"markdown","source":["### Loading Custom Dataset"],"metadata":{"id":"ntvmUKx7ty-k"}},{"cell_type":"markdown","source":["1. From RoboFlow"],"metadata":{"id":"nQUe0Mect5yu"}},{"cell_type":"code","source":["!pip install roboflow --quiet\n","\n","from roboflow import Roboflow\n","rf = Roboflow(api_key=\"dNG5RONSeTdSdNyKbktT\")\n","project = rf.workspace(\"vaishanth-ramaraj-uub2b\").project(\"hand-detector-pjtzx\")\n","dataset = project.version(3).download(\"yolov8\")"],"metadata":{"id":"uAGS2ETut9Q8"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["2. Locally"],"metadata":{"id":"e2Nt70vLuHKv"}},{"cell_type":"code","source":["dataset_type = \"hand_dataset_seg_v2\" # [\"hand_dataset_seg_v1\", \"hand_dataset_seg_v2\"]\n","\n","dataset_path_abs = \"/content/drive/My\\ Drive/My\\ Projects/yolo_custom_training/datasets/\"+dataset_type+\"/data.yaml\"\n","dataset_path_rel = \"datasets/\"+dataset_type+\"/data.yaml\"\n","\n","print(\"Absolute path: \", dataset_path_abs)\n","print(\"Relative path: \", dataset_path_rel)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"s7cfNjBbuMiQ","executionInfo":{"status":"ok","timestamp":1689616623473,"user_tz":240,"elapsed":99,"user":{"displayName":"Vaishanth Ramaraj","userId":"09348595743611064915"}},"outputId":"52efe37e-f1c8-4a65-a3e8-6ed88ef250db"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Absolute path: /content/drive/My\\ Drive/My\\ Projects/yolo_custom_training/datasets/hand_dataset_seg_v2/data.yaml\n","Relative path: datasets/hand_dataset_seg_v2/data.yaml\n"]}]},{"cell_type":"markdown","source":["### Training Custom Model"],"metadata":{"id":"N90ZJOD8vXjF"}},{"cell_type":"code","source":["from ultralytics import YOLO\n","\n","# Load the model.\n","model = YOLO('yolov8m-seg.pt')\n","\n","CUSTOM_MODEL_NAME = \"yolo8m_seg_hand\"\n","\n","# Training with custom dataset\n","results = model.train(\n"," data=dataset_path_rel,\n"," imgsz=640,\n"," epochs=50,\n"," batch=8,\n"," name=CUSTOM_MODEL_NAME)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"60QljZEJvarc","executionInfo":{"status":"ok","timestamp":1689618405675,"user_tz":240,"elapsed":844714,"user":{"displayName":"Vaishanth Ramaraj","userId":"09348595743611064915"}},"outputId":"1dd83ca3-c94a-4a38-e215-c164fca7b48d"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stderr","text":["Downloading https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m-seg.pt to yolov8m-seg.pt...\n","100%|██████████| 52.4M/52.4M [00:00<00:00, 95.8MB/s]\n","\n","Ultralytics YOLOv8.0.20 🚀 Python-3.10.12 torch-2.0.1+cu118 CUDA:0 (Tesla T4, 15102MiB)\n","\u001b[34m\u001b[1myolo/engine/trainer: \u001b[0mtask=segment, mode=train, model=yolov8m-seg.yaml, data=datasets/hand_dataset_seg_v2/data.yaml, epochs=50, patience=50, batch=8, imgsz=640, save=True, cache=False, device=, workers=8, project=None, name=yolo8m_seg_hand, exist_ok=False, pretrained=False, optimizer=SGD, verbose=False, seed=0, deterministic=True, single_cls=False, image_weights=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, overlap_mask=True, mask_ratio=4, dropout=False, val=True, save_json=False, save_hybrid=False, conf=0.001, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=ultralytics/assets/, show=False, save_txt=False, save_conf=False, save_crop=False, hide_labels=False, hide_conf=False, vid_stride=1, line_thickness=3, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, boxes=True, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=17, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.001, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, fl_gamma=0.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.9, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.1, copy_paste=0.1, cfg=None, v5loader=False, save_dir=runs/segment/yolo8m_seg_hand\n","Overriding model.yaml nc=80 with nc=2\n","\n"," from n params module arguments \n"," 0 -1 1 1392 ultralytics.nn.modules.Conv [3, 48, 3, 2] \n"," 1 -1 1 41664 ultralytics.nn.modules.Conv [48, 96, 3, 2] \n"," 2 -1 2 111360 ultralytics.nn.modules.C2f [96, 96, 2, True] \n"," 3 -1 1 166272 ultralytics.nn.modules.Conv [96, 192, 3, 2] \n"," 4 -1 4 813312 ultralytics.nn.modules.C2f [192, 192, 4, True] \n"," 5 -1 1 664320 ultralytics.nn.modules.Conv [192, 384, 3, 2] \n"," 6 -1 4 3248640 ultralytics.nn.modules.C2f [384, 384, 4, True] \n"," 7 -1 1 1991808 ultralytics.nn.modules.Conv [384, 576, 3, 2] \n"," 8 -1 2 3985920 ultralytics.nn.modules.C2f [576, 576, 2, True] \n"," 9 -1 1 831168 ultralytics.nn.modules.SPPF [576, 576, 5] \n"," 10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n"," 11 [-1, 6] 1 0 ultralytics.nn.modules.Concat [1] \n"," 12 -1 2 1993728 ultralytics.nn.modules.C2f [960, 384, 2] \n"," 13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n"," 14 [-1, 4] 1 0 ultralytics.nn.modules.Concat [1] \n"," 15 -1 2 517632 ultralytics.nn.modules.C2f [576, 192, 2] \n"," 16 -1 1 332160 ultralytics.nn.modules.Conv [192, 192, 3, 2] \n"," 17 [-1, 12] 1 0 ultralytics.nn.modules.Concat [1] \n"," 18 -1 2 1846272 ultralytics.nn.modules.C2f [576, 384, 2] \n"," 19 -1 1 1327872 ultralytics.nn.modules.Conv [384, 384, 3, 2] \n"," 20 [-1, 9] 1 0 ultralytics.nn.modules.Concat [1] \n"," 21 -1 2 4207104 ultralytics.nn.modules.C2f [960, 576, 2] \n"," 22 [15, 18, 21] 1 5160182 ultralytics.nn.modules.Segment [2, 32, 192, [192, 384, 576]] \n","YOLOv8m-seg summary: 331 layers, 27240806 parameters, 27240790 gradients, 110.4 GFLOPs\n","\n","Transferred 531/537 items from pretrained weights\n","\u001b[34m\u001b[1moptimizer:\u001b[0m SGD(lr=0.01) with parameter groups 86 weight(decay=0.0), 97 weight(decay=0.001), 96 bias\n","\u001b[34m\u001b[1mtrain: \u001b[0mScanning /content/drive/My Drive/My Projects/yolo_custom_training/datasets/hand_dataset_seg_v2/train/labels.cache... 165 images, 0 backgrounds, 0 corrupt: 100%|██████████| 165/165 [00:00