File size: 11,510 Bytes
6cc8ae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# \ud83d\udc04 Cattle Breed Classifier \u2014 Colab Runner\n",
                "\n",
                "This notebook sets up the environment on **Google Colab** and runs all training notebooks sequentially.\n",
                "\n",
                "### What this does\n",
                "1. Verifies GPU availability\n",
                "2. Clones the repo (dataset included)\n",
                "3. Installs missing dependencies\n",
                "4. Runs each notebook in order:\n",
                "   - `00_data_audit` \u2192 `01_mlp_baseline` \u2192 `02_cnn_from_scratch` \u2192 `03_resnet_transfer_learning` \u2192 `04_vit_transfer_learning` \u2192 `05_model_comparison`\n",
                "\n",
                "> \u26a0\ufe0f **Runtime**: Select **GPU** runtime before running: *Runtime \u2192 Change runtime type \u2192 T4 GPU*"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 0. Check GPU"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import torch\n",
                "print(f'PyTorch version: {torch.__version__}')\n",
                "print(f'CUDA available:  {torch.cuda.is_available()}')\n",
                "if torch.cuda.is_available():\n",
                "    print(f'GPU:             {torch.cuda.get_device_name(0)}')\n",
                "    print(f'Memory:          {torch.cuda.get_device_properties(0).total_mem / 1024**3:.1f} GB')\n",
                "else:\n",
                "    print('\u26a0\ufe0f  No GPU detected. Go to Runtime \u2192 Change runtime type \u2192 T4 GPU')"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 1. Clone Repository"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import os\n",
                "\n",
                "REPO_URL = 'https://github.com/ajitsingh98/cattle-breed-classifier-webapp.git'\n",
                "REPO_DIR = '/content/cattle-breed-classifier-webapp'\n",
                "\n",
                "if not os.path.exists(REPO_DIR):\n",
                "    !git clone {REPO_URL} {REPO_DIR}\n",
                "else:\n",
                "    print(f'Repo already cloned at {REPO_DIR}')\n",
                "    !cd {REPO_DIR} && git pull\n",
                "\n",
                "os.chdir(REPO_DIR)\n",
                "print(f'\\nWorking directory: {os.getcwd()}')"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 2. Install Dependencies"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Colab already has PyTorch, torchvision, numpy, pandas, matplotlib, seaborn, PIL, scikit-learn.\n",
                "# Install only the missing packages.\n",
                "!pip install -q PyYAML gdown aiofiles aiohttp nest_asyncio"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 3. Set Up Python Path"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import sys\n",
                "from pathlib import Path\n",
                "\n",
                "PROJECT_ROOT = Path(REPO_DIR).resolve()\n",
                "if str(PROJECT_ROOT) not in sys.path:\n",
                "    sys.path.insert(0, str(PROJECT_ROOT))\n",
                "\n",
                "# Create artifact directories\n",
                "for d in ['ml/artifacts/manifests', 'ml/artifacts/checkpoints',\n",
                "          'ml/artifacts/figures', 'ml/artifacts/logs', 'ml/artifacts/reports']:\n",
                "    (PROJECT_ROOT / d).mkdir(parents=True, exist_ok=True)\n",
                "\n",
                "# Verify dataset exists\n",
                "data_dir = PROJECT_ROOT / 'Cattle_Resized'\n",
                "class_dirs = sorted([d for d in data_dir.iterdir() if d.is_dir()])\n",
                "total_images = sum(len(list(d.glob('*'))) for d in class_dirs)\n",
                "print(f'Dataset: {len(class_dirs)} breeds, {total_images} images')\n",
                "print(f'Project root: {PROJECT_ROOT}')"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 4. Run All Notebooks\n",
                "\n",
                "Each notebook is executed in order using `nbconvert`. Output is displayed inline.\n",
                "\n",
                "You can also **skip this cell** and open each notebook individually from the file browser on the left:\n",
                "> `cattle-breed-classifier-webapp/ml/notebooks/`"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "!pip install -q papermill jupyter ipykernel"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import subprocess, time\n",
                "\n",
                "NOTEBOOKS = [\n",
                "    '00_data_audit.ipynb',\n",
                "    '01_mlp_baseline.ipynb',\n",
                "    '02_cnn_from_scratch.ipynb',\n",
                "    '03_resnet_transfer_learning.ipynb',\n",
                "    '04_vit_transfer_learning.ipynb',\n",
                "    '05_model_comparison.ipynb',\n",
                "]\n",
                "\n",
                "NOTEBOOK_DIR = PROJECT_ROOT / 'ml' / 'notebooks'\n",
                "results = {}\n",
                "\n",
                "for nb_name in NOTEBOOKS:\n",
                "    nb_path = NOTEBOOK_DIR / nb_name\n",
                "    print(f'\\n{\"=\" * 60}')\n",
                "    print(f'\u25b6 Running: {nb_name}')\n",
                "    print(f'{\"=\" * 60}')\n",
                "\n",
                "    start = time.time()\n",
                "    # We use papermill because it supports streaming cell outputs (--log-output)\n",
                "    result = subprocess.run(\n",
                "        [\n",
                "            \"papermill\",\n",
                "            str(nb_path),\n",
                "            str(nb_path), # overwrite inplace so output is saved in the notebook\n",
                "            \"--log-output\",\n",
                "            \"--kernel\", \"python3\"\n",
                "        ],\n",
                "        cwd=str(PROJECT_ROOT),\n",
                "        env={**os.environ, \"PYTHONPATH\": str(PROJECT_ROOT)},\n",
                "    )\n",
                "    elapsed = time.time() - start\n",
                "\n",
                "    if result.returncode == 0:\n",
                "        status = '\u2705 PASSED'\n",
                "    else:\n",
                "        status = '\u274c FAILED'\n",
                "\n",
                "    results[nb_name] = {'status': status, 'time': elapsed}\n",
                "    print(f'{status} ({elapsed:.1f}s)')\n",
                "\n",
                "# Summary\n",
                "print(f'\\n{\"=\" * 60}')\n",
                "print('Summary')\n",
                "print(f'{\"=\" * 60}')\n",
                "for nb, info in results.items():\n",
                "    print(f\"  {info['status']}  {nb:45s}  {info['time']:6.1f}s\")\n",
                "total_time = sum(r['time'] for r in results.values())\n",
                "print(f'\\nTotal time: {total_time/60:.1f} minutes')"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## 5. Download Artifacts (Optional)\n",
                "\n",
                "After training completes, download the best model checkpoint and reports."
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# List generated artifacts\n",
                "import glob\n",
                "\n",
                "print('=== Checkpoints ===')\n",
                "for f in sorted(glob.glob(str(PROJECT_ROOT / 'ml/artifacts/checkpoints/*.pth'))):\n",
                "    size_mb = os.path.getsize(f) / 1024 / 1024\n",
                "    print(f'  {Path(f).name:40s} {size_mb:8.1f} MB')\n",
                "\n",
                "print('\\n=== Figures ===')\n",
                "for f in sorted(glob.glob(str(PROJECT_ROOT / 'ml/artifacts/figures/*.png'))):\n",
                "    print(f'  {Path(f).name}')\n",
                "\n",
                "print('\\n=== Reports ===')\n",
                "for f in sorted(glob.glob(str(PROJECT_ROOT / 'ml/artifacts/reports/*.json'))):\n",
                "    print(f'  {Path(f).name}')"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# Zip and download artifacts\n",
                "!cd {REPO_DIR} && zip -r /content/artifacts.zip ml/artifacts/\n",
                "\n",
                "from google.colab import files\n",
                "files.download('/content/artifacts.zip')\n",
                "print('\\n\u2705 Download started! Check your browser downloads.')"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "---\n",
                "\n",
                "### \ud83d\udca1 Running Notebooks Individually\n",
                "\n",
                "Instead of the automated runner above, you can open each notebook directly:\n",
                "\n",
                "1. In the **Colab file browser** (left panel), navigate to:  \n",
                "   `cattle-breed-classifier-webapp/ml/notebooks/`\n",
                "2. Double-click any `.ipynb` file to open it in a new tab\n",
                "3. Run cells with `Shift+Enter`\n",
                "\n",
                "**Important**: Each notebook auto-detects the project root, so they work both from the automated runner and when opened individually."
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "name": "python",
            "version": "3.10.0"
        },
        "colab": {
            "provenance": [],
            "gpuType": "T4"
        },
        "accelerator": "GPU"
    },
    "nbformat": 4,
    "nbformat_minor": 4
}