Saracasm commited on
Commit
38e2cf4
Β·
1 Parent(s): 2f8b9c8

Add files via upload

Browse files
Files changed (1) hide show
  1. notebooks/00_smoke_test.ipynb +405 -0
notebooks/00_smoke_test.ipynb ADDED
@@ -0,0 +1,405 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# 00 β€” Smoke Test\n",
8
+ "\n",
9
+ "**Purpose:** verify that everything in our environment works before we touch any real data.\n",
10
+ "\n",
11
+ "Run every cell top-to-bottom. If all green checkmarks βœ… appear at the end, Phase 1 is complete.\n",
12
+ "\n",
13
+ "**What this notebook does:**\n",
14
+ "1. Verifies GPU is available\n",
15
+ "2. Mounts Google Drive and creates folder structure\n",
16
+ "3. Installs pinned dependencies\n",
17
+ "4. Loads Wav2Vec 2.0 from HuggingFace\n",
18
+ "5. Runs one forward pass on random audio\n",
19
+ "6. Connects to Weights & Biases\n",
20
+ "7. Prints environment summary for documentation\n",
21
+ "\n",
22
+ "**Estimated time:** 5-10 minutes the first time (mostly pip install)."
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "markdown",
27
+ "metadata": {},
28
+ "source": [
29
+ "## Step 1 β€” GPU check\n",
30
+ "\n",
31
+ "Before anything else: are we on a GPU? If this cell shows \"No GPU\", go to **Runtime β†’ Change runtime type β†’ Hardware accelerator β†’ GPU** and re-run.\n",
32
+ "\n",
33
+ "**What you want to see:** Tesla T4, V100, or A100. With Colab Pro you'll usually get T4 (most common) or sometimes V100. A100 is rare on Pro."
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": null,
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "import subprocess\n",
43
+ "\n",
44
+ "result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)\n",
45
+ "if result.returncode == 0:\n",
46
+ " print(result.stdout)\n",
47
+ "else:\n",
48
+ " print('❌ No GPU detected. Go to Runtime β†’ Change runtime type β†’ GPU')"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "markdown",
53
+ "metadata": {},
54
+ "source": [
55
+ "## Step 2 β€” Mount Google Drive\n",
56
+ "\n",
57
+ "Colab gives you a temporary disk that wipes between sessions. We need persistent storage for the ~25 GB of ASVspoof data and our model checkpoints. Drive is the answer.\n",
58
+ "\n",
59
+ "**You'll be prompted** to authorize access. Click the link, choose your Google account, copy the verification code, and paste it back. This happens once per session."
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "metadata": {},
66
+ "outputs": [],
67
+ "source": [
68
+ "from google.colab import drive\n",
69
+ "drive.mount('/content/drive')"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "markdown",
74
+ "metadata": {},
75
+ "source": [
76
+ "Now create the folder structure on Drive. We do this once; it'll persist across sessions."
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": null,
82
+ "metadata": {},
83
+ "outputs": [],
84
+ "source": [
85
+ "import os\n",
86
+ "\n",
87
+ "DRIVE_ROOT = '/content/drive/MyDrive/deepfake_audio'\n",
88
+ "\n",
89
+ "subdirs = [\n",
90
+ " 'data/raw/asvspoof_2019',\n",
91
+ " 'data/raw/asvspoof_2021',\n",
92
+ " 'data/raw/wavefake',\n",
93
+ " 'data/processed',\n",
94
+ " 'checkpoints',\n",
95
+ " 'logs',\n",
96
+ "]\n",
97
+ "\n",
98
+ "for sub in subdirs:\n",
99
+ " path = os.path.join(DRIVE_ROOT, sub)\n",
100
+ " os.makedirs(path, exist_ok=True)\n",
101
+ " print(f' βœ… {path}')\n",
102
+ "\n",
103
+ "print('\\nβœ… Drive folder structure created.')"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "markdown",
108
+ "metadata": {},
109
+ "source": [
110
+ "## Step 3 β€” Clone the GitHub repo\n",
111
+ "\n",
112
+ "Pull our project code into Colab's local disk. We work out of `/content/deepfake-audio-detection/` for code, and read/write data via `/content/drive/MyDrive/deepfake_audio/`.\n",
113
+ "\n",
114
+ "**⚠️ Replace `YOUR_USERNAME` below with your actual GitHub username before running.**"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": null,
120
+ "metadata": {},
121
+ "outputs": [],
122
+ "source": [
123
+ "# --- EDIT THIS LINE ---\n",
124
+ "REPO_URL = 'https://github.com/YOUR_USERNAME/deepfake-audio-detection.git'\n",
125
+ "# ----------------------\n",
126
+ "\n",
127
+ "%cd /content\n",
128
+ "!if [ ! -d 'deepfake-audio-detection' ]; then git clone {REPO_URL}; else echo 'Repo already cloned'; fi\n",
129
+ "%cd /content/deepfake-audio-detection\n",
130
+ "!ls -la"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "markdown",
135
+ "metadata": {},
136
+ "source": [
137
+ "## Step 4 β€” Install dependencies\n",
138
+ "\n",
139
+ "We pin exact versions in `requirements.txt`. This protects us from \"it worked yesterday\" disasters when libraries silently update.\n",
140
+ "\n",
141
+ "**Note:** after install, Colab may suggest restarting the runtime. Restart **once**, then continue from Step 5. Don't restart again later β€” that wipes the loaded model."
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": null,
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "!pip install -q -r requirements.txt\n",
151
+ "print('\\nβœ… Dependencies installed. If Colab shows a restart prompt, restart NOW and resume from Step 5.')"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "markdown",
156
+ "metadata": {},
157
+ "source": [
158
+ "## Step 5 β€” Verify imports & versions\n",
159
+ "\n",
160
+ "Confirm the libraries actually loaded with the versions we expect."
161
+ ]
162
+ },
163
+ {
164
+ "cell_type": "code",
165
+ "execution_count": null,
166
+ "metadata": {},
167
+ "outputs": [],
168
+ "source": [
169
+ "import sys\n",
170
+ "import torch\n",
171
+ "import torchaudio\n",
172
+ "import transformers\n",
173
+ "import numpy as np\n",
174
+ "import pandas as pd\n",
175
+ "import librosa\n",
176
+ "import sklearn\n",
177
+ "\n",
178
+ "print(f'Python: {sys.version.split()[0]}')\n",
179
+ "print(f'PyTorch: {torch.__version__}')\n",
180
+ "print(f'torchaudio: {torchaudio.__version__}')\n",
181
+ "print(f'transformers: {transformers.__version__}')\n",
182
+ "print(f'numpy: {np.__version__}')\n",
183
+ "print(f'pandas: {pd.__version__}')\n",
184
+ "print(f'librosa: {librosa.__version__}')\n",
185
+ "print(f'sklearn: {sklearn.__version__}')\n",
186
+ "\n",
187
+ "print(f'\\nCUDA available: {torch.cuda.is_available()}')\n",
188
+ "if torch.cuda.is_available():\n",
189
+ " print(f'CUDA version: {torch.version.cuda}')\n",
190
+ " print(f'GPU name: {torch.cuda.get_device_name(0)}')\n",
191
+ " props = torch.cuda.get_device_properties(0)\n",
192
+ " print(f'GPU memory: {props.total_memory / 1e9:.2f} GB')\n",
193
+ " print(f'Compute capability: {props.major}.{props.minor}')\n",
194
+ "\n",
195
+ "print('\\nβœ… All imports succeeded.')"
196
+ ]
197
+ },
198
+ {
199
+ "cell_type": "markdown",
200
+ "metadata": {},
201
+ "source": [
202
+ "## Step 6 β€” Load Wav2Vec 2.0\n",
203
+ "\n",
204
+ "This is the core test. We load the pretrained Wav2Vec 2.0 Base model from HuggingFace. The first run downloads ~360 MB; subsequent runs use a cached copy.\n",
205
+ "\n",
206
+ "**What's happening under the hood:** HuggingFace fetches the model weights and configuration, instantiates a PyTorch model, and we move it to the GPU."
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": null,
212
+ "metadata": {},
213
+ "outputs": [],
214
+ "source": [
215
+ "from transformers import Wav2Vec2Model, Wav2Vec2FeatureExtractor\n",
216
+ "\n",
217
+ "MODEL_NAME = 'facebook/wav2vec2-base'\n",
218
+ "\n",
219
+ "print(f'Loading {MODEL_NAME}...')\n",
220
+ "feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_NAME)\n",
221
+ "model = Wav2Vec2Model.from_pretrained(MODEL_NAME)\n",
222
+ "\n",
223
+ "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
224
+ "model = model.to(device)\n",
225
+ "model.eval()\n",
226
+ "\n",
227
+ "n_params = sum(p.numel() for p in model.parameters())\n",
228
+ "print(f'\\nβœ… Model loaded on {device}.')\n",
229
+ "print(f' Total parameters: {n_params:,} (~{n_params/1e6:.1f}M)')\n",
230
+ "print(f' Number of transformer layers: {model.config.num_hidden_layers}')\n",
231
+ "print(f' Hidden size: {model.config.hidden_size}')"
232
+ ]
233
+ },
234
+ {
235
+ "cell_type": "markdown",
236
+ "metadata": {},
237
+ "source": [
238
+ "## Step 7 β€” Forward pass on random audio\n",
239
+ "\n",
240
+ "Generate 4 seconds of random audio (just noise β€” we're testing plumbing, not real prediction). Push it through the model. Check the output shape makes sense.\n",
241
+ "\n",
242
+ "**What you want to see:**\n",
243
+ "- Input shape: `(1, 64000)` β€” 1 batch Γ— 4 seconds Γ— 16,000 samples/sec\n",
244
+ "- Output shape: `(1, ~199, 768)` β€” 1 batch Γ— ~199 time frames Γ— 768 hidden dimensions\n",
245
+ "- Wav2Vec 2.0 downsamples by ~320, so 64000 / 320 β‰ˆ 200 frames"
246
+ ]
247
+ },
248
+ {
249
+ "cell_type": "code",
250
+ "execution_count": null,
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": [
254
+ "SAMPLE_RATE = 16000\n",
255
+ "DURATION_SEC = 4.0\n",
256
+ "\n",
257
+ "# Random audio β€” Gaussian noise, just for shape testing\n",
258
+ "torch.manual_seed(42)\n",
259
+ "fake_audio = torch.randn(1, int(SAMPLE_RATE * DURATION_SEC)).to(device)\n",
260
+ "print(f'Input shape: {tuple(fake_audio.shape)}')\n",
261
+ "\n",
262
+ "with torch.no_grad():\n",
263
+ " output = model(fake_audio)\n",
264
+ "\n",
265
+ "hidden_states = output.last_hidden_state\n",
266
+ "print(f'Output shape: {tuple(hidden_states.shape)}')\n",
267
+ "print(f' ↳ batch size: {hidden_states.shape[0]}')\n",
268
+ "print(f' ↳ time frames: {hidden_states.shape[1]} (one frame β‰ˆ 20ms of audio)')\n",
269
+ "print(f' ↳ feature dim: {hidden_states.shape[2]} (Wav2Vec 2.0 Base uses 768)')\n",
270
+ "\n",
271
+ "# Mean-pool over time to get one vector per clip β€” this is what our classifier head will use\n",
272
+ "pooled = hidden_states.mean(dim=1)\n",
273
+ "print(f'\\nMean-pooled shape: {tuple(pooled.shape)}')\n",
274
+ "print(f' ↳ This is what the classification head (added in Phase 3) will see.')\n",
275
+ "\n",
276
+ "print('\\nβœ… Forward pass successful. The model is talking to the GPU correctly.')"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "markdown",
281
+ "metadata": {},
282
+ "source": [
283
+ "## Step 8 β€” Connect to Weights & Biases\n",
284
+ "\n",
285
+ "Wandb is our experiment tracking service. Without it, every Colab disconnect erases your training history. With it, every loss curve, every metric, every config β€” saved to the cloud and viewable in a browser.\n",
286
+ "\n",
287
+ "**Before running this cell:**\n",
288
+ "1. Sign up at https://wandb.ai (use GitHub for one-click auth)\n",
289
+ "2. Go to https://wandb.ai/authorize and copy your API key\n",
290
+ "3. When prompted by the cell below, paste the key\n",
291
+ "\n",
292
+ "After this works once, wandb caches your credentials in `/root/.netrc` for the session. You won't be prompted again unless you start fresh."
293
+ ]
294
+ },
295
+ {
296
+ "cell_type": "code",
297
+ "execution_count": null,
298
+ "metadata": {},
299
+ "outputs": [],
300
+ "source": [
301
+ "import wandb\n",
302
+ "\n",
303
+ "wandb.login() # will prompt for your API key on first run\n",
304
+ "\n",
305
+ "# Start a tiny test run to confirm everything connects end-to-end\n",
306
+ "run = wandb.init(\n",
307
+ " project='deepfake-audio-detection',\n",
308
+ " name='smoke-test',\n",
309
+ " job_type='setup-test',\n",
310
+ " config={\n",
311
+ " 'phase': 'phase-1-setup',\n",
312
+ " 'model': MODEL_NAME,\n",
313
+ " 'gpu': torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'cpu',\n",
314
+ " 'pytorch_version': torch.__version__,\n",
315
+ " }\n",
316
+ ")\n",
317
+ "\n",
318
+ "# Log a fake metric just to verify the pipe works\n",
319
+ "wandb.log({'smoke_test_metric': 1.0, 'environment_ok': True})\n",
320
+ "\n",
321
+ "print(f'\\nβœ… Wandb run started.')\n",
322
+ "print(f' View it at: {run.url}')\n",
323
+ "\n",
324
+ "wandb.finish()\n",
325
+ "print('\\nβœ… Wandb test run finished. Visit the URL above to confirm it logged correctly.')"
326
+ ]
327
+ },
328
+ {
329
+ "cell_type": "markdown",
330
+ "metadata": {},
331
+ "source": [
332
+ "## Step 9 β€” Environment summary\n",
333
+ "\n",
334
+ "Print the full environment fingerprint. **Copy this output into `docs/environment.md`** and commit it. Future-you (and your coauthor) will thank you."
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "execution_count": null,
340
+ "metadata": {},
341
+ "outputs": [],
342
+ "source": [
343
+ "import platform\n",
344
+ "from datetime import datetime\n",
345
+ "\n",
346
+ "print('='*70)\n",
347
+ "print(f' ENVIRONMENT FINGERPRINT β€” {datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")}')\n",
348
+ "print('='*70)\n",
349
+ "print(f'Platform: Google Colab Pro')\n",
350
+ "print(f'OS: {platform.platform()}')\n",
351
+ "print(f'Python: {sys.version.split()[0]}')\n",
352
+ "print(f'PyTorch: {torch.__version__}')\n",
353
+ "print(f'torchaudio: {torchaudio.__version__}')\n",
354
+ "print(f'transformers: {transformers.__version__}')\n",
355
+ "if torch.cuda.is_available():\n",
356
+ " print(f'GPU: {torch.cuda.get_device_name(0)}')\n",
357
+ " print(f'CUDA: {torch.version.cuda}')\n",
358
+ " print(f'GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB')\n",
359
+ "print(f'Drive root: {DRIVE_ROOT}')\n",
360
+ "print(f'Wandb project: deepfake-audio-detection')\n",
361
+ "print('='*70)\n",
362
+ "\n",
363
+ "print('\\nπŸŽ‰ Phase 1 smoke test complete. You are ready for Phase 2 (data acquisition).')"
364
+ ]
365
+ },
366
+ {
367
+ "cell_type": "markdown",
368
+ "metadata": {},
369
+ "source": [
370
+ "## Phase 1 Checklist\n",
371
+ "\n",
372
+ "Before moving to Phase 2, confirm all of these are done:\n",
373
+ "\n",
374
+ "- [ ] All cells above ran without errors\n",
375
+ "- [ ] GPU detected (T4, V100, or A100)\n",
376
+ "- [ ] Drive mounted, `deepfake_audio/` folder structure created\n",
377
+ "- [ ] GitHub repo cloned into Colab\n",
378
+ "- [ ] Wav2Vec 2.0 loaded and forward pass succeeded\n",
379
+ "- [ ] Wandb run visible at the URL printed in Step 8\n",
380
+ "- [ ] Environment fingerprint copied into `docs/environment.md`\n",
381
+ "- [ ] Repo committed and pushed to GitHub with all Phase 1 files\n",
382
+ "\n",
383
+ "**If anything failed:** scroll up, find the failing cell, read the error message, fix the issue, re-run. Don't proceed until every cell shows βœ…."
384
+ ]
385
+ }
386
+ ],
387
+ "metadata": {
388
+ "accelerator": "GPU",
389
+ "colab": {
390
+ "gpuType": "T4",
391
+ "provenance": []
392
+ },
393
+ "kernelspec": {
394
+ "display_name": "Python 3",
395
+ "language": "python",
396
+ "name": "python3"
397
+ },
398
+ "language_info": {
399
+ "name": "python",
400
+ "version": "3.11.x"
401
+ }
402
+ },
403
+ "nbformat": 4,
404
+ "nbformat_minor": 4
405
+ }