diff --git a/ai/classifier/colab/train_baseline.ipynb b/ai/classifier/colab/train_baseline.ipynb new file mode 100644 index 0000000..73011ce --- /dev/null +++ b/ai/classifier/colab/train_baseline.ipynb @@ -0,0 +1,141 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# GateGuard Classifier — Baseline Training (Colab)\n", + "\n", + "EfficientNet-B0 영상 분류 베이스라인을 Colab GPU(T4) 에서 학습한다.\n", + "\n", + "**전제**: Google Drive 에 데이터셋이 다음 구조로 있다고 가정.\n", + "```\n", + "/\n", + " jump/ *.mp4\n", + " crawling/ *.mp4\n", + " tailgating/ *.mp4\n", + " unpaid/ *.mp4\n", + "```\n", + "구조가 다르면 4번 셀에서 정리 한 번 해주거나 코드를 맞게 수정한다.\n", + "\n", + "**런타임**: 상단 메뉴 → Runtime → Change runtime type → **GPU(T4)** 선택." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. GPU 확인\n", + "!nvidia-smi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 2. repo clone + 의존성\n", + "!git clone --depth 1 https://github.com/CHOSOOGEUN/GateGuard.git\n", + "%cd GateGuard\n", + "!pip install -q -r ai/classifier/requirements.txt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Google Drive 마운트\n", + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 4. 데이터 경로 설정 + 검증\n", + "# ↓ 본인 드라이브 구조에 맞게 한 줄만 수정.\n", + "# 예) '/content/drive/MyDrive/GateGuard_dataset'\n", + "# 공유 드라이브면 '/content/drive/Shareddrives/...'\n", + "DATA_ROOT = '/content/drive/MyDrive/GateGuard_dataset'\n", + "\n", + "import os, glob\n", + "assert os.path.isdir(DATA_ROOT), f'경로 없음: {DATA_ROOT}'\n", + "for cls in ('jump', 'crawling', 'tailgating', 'unpaid'):\n", + " p = os.path.join(DATA_ROOT, cls)\n", + " n = len(glob.glob(os.path.join(p, '*.mp4'))) if os.path.isdir(p) else 0\n", + " flag = 'OK ' if n > 0 else '!! '\n", + " print(f'{flag}{cls:12s} {n:3d} clips ({p})')\n", + "\n", + "os.environ['DATA_ROOT'] = DATA_ROOT" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 5. 학습 실행\n", + "# - epoch 50, batch 8, AdamW + CE(class_weight balanced), early stop patience 7\n", + "# - val_loss 기준 best 저장 → ai/classifier/runs/baseline/best_model.pth\n", + "# - 종료 후 confusion matrix + classification report 출력\n", + "!python -m ai.classifier.train \\\n", + " --data-root \"$DATA_ROOT\" \\\n", + " --output-dir ai/classifier/runs/baseline \\\n", + " --epochs 50 --batch-size 8" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 6. (선택) 학습 결과를 드라이브로 백업\n", + "import datetime, shutil, os\n", + "stamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')\n", + "dst = f'/content/drive/MyDrive/GateGuard_runs/baseline_{stamp}'\n", + "os.makedirs(os.path.dirname(dst), exist_ok=True)\n", + "shutil.copytree('ai/classifier/runs/baseline', dst)\n", + "print(f'saved: {dst}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 7. (선택) 단일 영상 추론 데모\n", + "# SAMPLE_VIDEO 에 본인 클립 하나 경로 넣기\n", + "SAMPLE_VIDEO = '/content/drive/MyDrive/GateGuard_dataset/jump/clip001.mp4'\n", + "!python -m ai.classifier.inference_video \\\n", + " --checkpoint ai/classifier/runs/baseline/best_model.pth \\\n", + " --video \"$SAMPLE_VIDEO\"" + ] + } + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}