v2: Zero-download streaming notebook — 3s startup, 0 disk, images fetched on-demand via HTTP
Browse files- train_bokehflow.ipynb +211 -255
train_bokehflow.ipynb
CHANGED
|
@@ -5,15 +5,17 @@
|
|
| 5 |
"metadata": {},
|
| 6 |
"source": [
|
| 7 |
"# 🎬 BokehFlow Training Notebook\n",
|
| 8 |
-
"##
|
| 9 |
"\n",
|
| 10 |
-
"**
|
| 11 |
"\n",
|
| 12 |
-
"| Platform | GPU |
|
| 13 |
-
"|----------|-----|------
|
| 14 |
-
"| Colab Free | T4
|
| 15 |
-
"| Kaggle | 2×T4 |
|
| 16 |
-
"| Colab Pro | A100 |
|
|
|
|
|
|
|
| 17 |
]
|
| 18 |
},
|
| 19 |
{
|
|
@@ -22,10 +24,8 @@
|
|
| 22 |
"metadata": {},
|
| 23 |
"outputs": [],
|
| 24 |
"source": [
|
| 25 |
-
"#
|
| 26 |
-
"
|
| 27 |
-
"# ============================================================\n",
|
| 28 |
-
"!pip install -q torch torchvision Pillow huggingface_hub tqdm"
|
| 29 |
]
|
| 30 |
},
|
| 31 |
{
|
|
@@ -34,9 +34,7 @@
|
|
| 34 |
"metadata": {},
|
| 35 |
"outputs": [],
|
| 36 |
"source": [
|
| 37 |
-
"#
|
| 38 |
-
"# STEP 1: Download BokehFlow architecture\n",
|
| 39 |
-
"# ============================================================\n",
|
| 40 |
"from huggingface_hub import hf_hub_download\n",
|
| 41 |
"hf_hub_download(repo_id='asdf98/BokehFlow', filename='bokehflow.py', local_dir='.')\n",
|
| 42 |
"print('✓ BokehFlow downloaded')"
|
|
@@ -48,74 +46,28 @@
|
|
| 48 |
"metadata": {},
|
| 49 |
"outputs": [],
|
| 50 |
"source": [
|
| 51 |
-
"#
|
| 52 |
-
"# STEP 2: Configuration — CHANGE THESE IF YOU WANT\n",
|
| 53 |
-
"# ============================================================\n",
|
| 54 |
"CONFIG = {\n",
|
| 55 |
-
" #
|
| 56 |
-
" '
|
| 57 |
-
" \n",
|
| 58 |
-
"
|
| 59 |
-
" 'batch_size': 4, # 4 for T4 16GB, 8 for A100\n",
|
| 60 |
-
" 'crop_size': 256, # 256x256 random crops\n",
|
| 61 |
-
" 'num_epochs': 5, # 5 epochs for demo, 50+ for full training\n",
|
| 62 |
" 'lr': 3e-4,\n",
|
| 63 |
" 'weight_decay': 0.05,\n",
|
| 64 |
" 'max_grad_norm': 1.0,\n",
|
| 65 |
-
" \n",
|
| 66 |
-
" #
|
| 67 |
-
" '
|
| 68 |
-
" 'max_train_samples': 500, # Limit for quick test. Set None for full dataset.\n",
|
| 69 |
-
" \n",
|
| 70 |
-
" # Target f-stop (train on f/2.0 bokeh)\n",
|
| 71 |
-
" 'target_fstop': 2.0,\n",
|
| 72 |
-
" \n",
|
| 73 |
-
" # Save\n",
|
| 74 |
-
" 'save_every': 1, # Save checkpoint every N epochs\n",
|
| 75 |
" 'output_dir': './checkpoints',\n",
|
| 76 |
"}\n",
|
| 77 |
"\n",
|
| 78 |
-
"# Auto-detect Kaggle dual GPU\n",
|
| 79 |
"import torch\n",
|
| 80 |
"NUM_GPUS = torch.cuda.device_count()\n",
|
| 81 |
-
"
|
|
|
|
| 82 |
"if NUM_GPUS > 1:\n",
|
| 83 |
-
"
|
| 84 |
-
|
| 85 |
-
},
|
| 86 |
-
{
|
| 87 |
-
"cell_type": "code",
|
| 88 |
-
"execution_count": null,
|
| 89 |
-
"metadata": {},
|
| 90 |
-
"outputs": [],
|
| 91 |
-
"source": [
|
| 92 |
-
"# ============================================================\n",
|
| 93 |
-
"# STEP 3: Dataset — Download RealBokeh (raw images, ~19GB)\n",
|
| 94 |
-
"# For free Colab/Kaggle, we use the HF Hub API to stream\n",
|
| 95 |
-
"# ============================================================\n",
|
| 96 |
-
"import os, json, re, glob\n",
|
| 97 |
-
"from pathlib import Path\n",
|
| 98 |
-
"from huggingface_hub import snapshot_download\n",
|
| 99 |
-
"\n",
|
| 100 |
-
"# Only download the train split input images + f/2.0 GT + metadata\n",
|
| 101 |
-
"# This saves bandwidth vs full 19GB\n",
|
| 102 |
-
"DATA_DIR = './realbokeh'\n",
|
| 103 |
-
"\n",
|
| 104 |
-
"if not os.path.exists(f'{DATA_DIR}/train/in'):\n",
|
| 105 |
-
" print('Downloading RealBokeh train split (input + metadata)...')\n",
|
| 106 |
-
" print('This downloads ~5GB. On Colab it takes ~3-5 minutes.')\n",
|
| 107 |
-
" snapshot_download(\n",
|
| 108 |
-
" repo_id='timseizinger/RealBokeh_3MP',\n",
|
| 109 |
-
" repo_type='dataset',\n",
|
| 110 |
-
" local_dir=DATA_DIR,\n",
|
| 111 |
-
" allow_patterns=['train/in/*', 'train/metadata/*', 'train/gt/*/f2.0*',\n",
|
| 112 |
-
" 'train/gt/*/*_f2.0*',\n",
|
| 113 |
-
" 'validation/in/*', 'validation/metadata/*', \n",
|
| 114 |
-
" 'validation/gt/*/*_f2.0*'],\n",
|
| 115 |
-
" )\n",
|
| 116 |
-
" print('✓ Dataset downloaded')\n",
|
| 117 |
-
"else:\n",
|
| 118 |
-
" print('✓ Dataset already exists')"
|
| 119 |
]
|
| 120 |
},
|
| 121 |
{
|
|
@@ -124,128 +76,135 @@
|
|
| 124 |
"metadata": {},
|
| 125 |
"outputs": [],
|
| 126 |
"source": [
|
| 127 |
-
"#
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
-
"import torch\n",
|
| 131 |
"from torch.utils.data import Dataset, DataLoader\n",
|
| 132 |
"from torchvision import transforms\n",
|
| 133 |
-
"from
|
| 134 |
-
"import random\n",
|
| 135 |
"\n",
|
| 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 |
-
" \n",
|
| 163 |
-
" # Find target f-stop image\n",
|
| 164 |
-
" fstop_str = f'f{target_fstop}'\n",
|
| 165 |
-
" gt_path = None\n",
|
| 166 |
-
" for img, av in zip(meta['target_images'], meta['target_avs']):\n",
|
| 167 |
-
" if abs(av - target_fstop) < 0.01:\n",
|
| 168 |
-
" gt_path = self.data_dir / img\n",
|
| 169 |
-
" break\n",
|
| 170 |
-
" \n",
|
| 171 |
-
" if gt_path is None or not gt_path.exists():\n",
|
| 172 |
-
" continue\n",
|
| 173 |
-
" \n",
|
| 174 |
-
" input_path = self.data_dir / meta['source_image']\n",
|
| 175 |
-
" if not input_path.exists():\n",
|
| 176 |
" continue\n",
|
| 177 |
-
" \n",
|
| 178 |
-
"
|
| 179 |
-
" '
|
| 180 |
-
" '
|
| 181 |
-
" '
|
| 182 |
-
" '
|
| 183 |
-
" 'focus_distance_m': float(meta['focus_plane_distance']),\n",
|
| 184 |
" })\n",
|
| 185 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
" if max_samples:\n",
|
| 187 |
-
" self.
|
| 188 |
-
" \n",
|
| 189 |
-
" print(f'{split}: {len(self.samples)} paired samples found')\n",
|
| 190 |
-
" \n",
|
| 191 |
" self.to_tensor = transforms.ToTensor()\n",
|
| 192 |
-
"
|
|
|
|
| 193 |
" def __len__(self):\n",
|
| 194 |
-
" return len(self.
|
| 195 |
-
"
|
| 196 |
" def __getitem__(self, idx):\n",
|
| 197 |
-
"
|
| 198 |
-
" \n",
|
| 199 |
-
"
|
| 200 |
-
"
|
| 201 |
-
"
|
| 202 |
-
"
|
| 203 |
-
"
|
| 204 |
-
"
|
| 205 |
" cs = self.crop_size\n",
|
|
|
|
| 206 |
" if w >= cs and h >= cs:\n",
|
| 207 |
-
" x = random.randint(0, w
|
| 208 |
-
" y = random.randint(0, h - cs)\n",
|
| 209 |
" inp = inp.crop((x, y, x+cs, y+cs))\n",
|
| 210 |
-
"
|
| 211 |
" else:\n",
|
| 212 |
" inp = inp.resize((cs, cs), Image.LANCZOS)\n",
|
| 213 |
-
"
|
| 214 |
-
" \n",
|
| 215 |
-
" # Random horizontal flip\n",
|
| 216 |
" if random.random() > 0.5:\n",
|
| 217 |
" inp = inp.transpose(Image.FLIP_LEFT_RIGHT)\n",
|
| 218 |
-
"
|
| 219 |
-
"
|
| 220 |
-
" inp_t = self.to_tensor(inp) # [0,1] range\n",
|
| 221 |
-
" tgt_t = self.to_tensor(tgt)\n",
|
| 222 |
-
" \n",
|
| 223 |
" return {\n",
|
| 224 |
-
" 'input':
|
| 225 |
-
" 'target':
|
| 226 |
-
" 'f_number':
|
| 227 |
-
" 'focal_length_mm': torch.tensor(
|
| 228 |
-
" 'focus_distance_m':
|
| 229 |
" }\n",
|
| 230 |
"\n",
|
| 231 |
-
"# Create
|
| 232 |
-
"
|
| 233 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
" crop_size=CONFIG['crop_size'],\n",
|
| 235 |
" target_fstop=CONFIG['target_fstop'],\n",
|
| 236 |
-
" max_samples=CONFIG['
|
| 237 |
")\n",
|
| 238 |
"\n",
|
| 239 |
"train_loader = DataLoader(\n",
|
| 240 |
-
" train_ds,
|
| 241 |
" batch_size=CONFIG['batch_size'],\n",
|
| 242 |
" shuffle=True,\n",
|
| 243 |
" num_workers=CONFIG['num_workers'],\n",
|
| 244 |
-
"
|
|
|
|
| 245 |
" drop_last=True,\n",
|
| 246 |
")\n",
|
| 247 |
-
"\n",
|
| 248 |
-
"print(f'
|
| 249 |
]
|
| 250 |
},
|
| 251 |
{
|
|
@@ -254,24 +213,38 @@
|
|
| 254 |
"metadata": {},
|
| 255 |
"outputs": [],
|
| 256 |
"source": [
|
| 257 |
-
"#
|
| 258 |
-
"
|
| 259 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
"from bokehflow import BokehFlow, BokehFlowConfig, BokehFlowLoss, model_summary\n",
|
| 261 |
"\n",
|
| 262 |
"config = BokehFlowConfig(variant=CONFIG['variant'])\n",
|
| 263 |
"model = BokehFlow(config)\n",
|
| 264 |
"\n",
|
| 265 |
-
"# Multi-GPU support for Kaggle\n",
|
| 266 |
-
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 267 |
"if NUM_GPUS > 1:\n",
|
| 268 |
" model = torch.nn.DataParallel(model)\n",
|
| 269 |
-
" print(f'
|
| 270 |
-
"model = model.to(
|
| 271 |
"\n",
|
| 272 |
-
"
|
| 273 |
-
"print(
|
| 274 |
-
"print(f'Device: {device}')"
|
| 275 |
]
|
| 276 |
},
|
| 277 |
{
|
|
@@ -280,85 +253,53 @@
|
|
| 280 |
"metadata": {},
|
| 281 |
"outputs": [],
|
| 282 |
"source": [
|
| 283 |
-
"#
|
| 284 |
-
"# STEP 6: Training loop\n",
|
| 285 |
-
"# ============================================================\n",
|
| 286 |
-
"import torch.nn.functional as F\n",
|
| 287 |
"from tqdm.auto import tqdm\n",
|
| 288 |
-
"import
|
| 289 |
-
"\n",
|
| 290 |
-
"optimizer = torch.optim.AdamW(\n",
|
| 291 |
-
" model.parameters(), \n",
|
| 292 |
-
" lr=CONFIG['lr'], \n",
|
| 293 |
-
" weight_decay=CONFIG['weight_decay']\n",
|
| 294 |
-
")\n",
|
| 295 |
-
"\n",
|
| 296 |
-
"scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(\n",
|
| 297 |
-
" optimizer, T_max=CONFIG['num_epochs'] * len(train_loader)\n",
|
| 298 |
-
")\n",
|
| 299 |
"\n",
|
|
|
|
|
|
|
| 300 |
"criterion = BokehFlowLoss(lambda_depth=0.5)\n",
|
| 301 |
-
"\n",
|
| 302 |
"os.makedirs(CONFIG['output_dir'], exist_ok=True)\n",
|
| 303 |
"\n",
|
| 304 |
-
"
|
| 305 |
-
"print(f'\\n
|
| 306 |
-
"print(f'Starting training: {CONFIG[\"num_epochs\"]} epochs')\n",
|
| 307 |
-
"print(f'{\"=\"*60}\\n')\n",
|
| 308 |
"\n",
|
| 309 |
"for epoch in range(CONFIG['num_epochs']):\n",
|
| 310 |
" model.train()\n",
|
| 311 |
-
"
|
| 312 |
-
"
|
| 313 |
-
" \n",
|
| 314 |
" pbar = tqdm(train_loader, desc=f'Epoch {epoch+1}/{CONFIG[\"num_epochs\"]}')\n",
|
|
|
|
| 315 |
" for step, batch in enumerate(pbar):\n",
|
| 316 |
-
"
|
| 317 |
-
"
|
| 318 |
-
"
|
| 319 |
-
"
|
| 320 |
-
"
|
| 321 |
-
"
|
| 322 |
-
" \n",
|
| 323 |
-
"
|
| 324 |
-
" output = model(inp, f_num, focal, focus)\n",
|
| 325 |
-
" \n",
|
| 326 |
-
" # Loss\n",
|
| 327 |
-
" losses = criterion(\n",
|
| 328 |
-
" output if not isinstance(output, dict) else output,\n",
|
| 329 |
-
" {'bokeh_gt': tgt}\n",
|
| 330 |
-
" )\n",
|
| 331 |
" loss = losses['total']\n",
|
| 332 |
-
"
|
| 333 |
-
" # Backward\n",
|
| 334 |
" optimizer.zero_grad()\n",
|
| 335 |
" loss.backward()\n",
|
| 336 |
" torch.nn.utils.clip_grad_norm_(model.parameters(), CONFIG['max_grad_norm'])\n",
|
| 337 |
" optimizer.step()\n",
|
| 338 |
" scheduler.step()\n",
|
| 339 |
-
"
|
| 340 |
-
"
|
| 341 |
-
" pbar.set_postfix({\n",
|
| 342 |
-
"
|
| 343 |
-
"
|
| 344 |
-
"
|
| 345 |
-
" \n",
|
| 346 |
-
"
|
| 347 |
-
" elapsed = time.time() - epoch_start\n",
|
| 348 |
-
" print(f'Epoch {epoch+1}: avg_loss={avg_loss:.4f}, time={elapsed:.0f}s')\n",
|
| 349 |
-
" \n",
|
| 350 |
" # Save checkpoint\n",
|
| 351 |
-
"
|
| 352 |
-
"
|
| 353 |
-
"
|
| 354 |
-
"
|
| 355 |
-
" 'epoch': epoch + 1,\n",
|
| 356 |
-
" 'model_state_dict': state,\n",
|
| 357 |
-
" 'optimizer_state_dict': optimizer.state_dict(),\n",
|
| 358 |
-
" 'loss': avg_loss,\n",
|
| 359 |
-
" 'config': CONFIG,\n",
|
| 360 |
-
" }, ckpt_path)\n",
|
| 361 |
-
" print(f' ✓ Saved checkpoint: {ckpt_path}')\n",
|
| 362 |
"\n",
|
| 363 |
"print(f'\\n✓ Training complete!')"
|
| 364 |
]
|
|
@@ -369,34 +310,48 @@
|
|
| 369 |
"metadata": {},
|
| 370 |
"outputs": [],
|
| 371 |
"source": [
|
| 372 |
-
"#
|
| 373 |
-
"# STEP 7: Quick inference test\n",
|
| 374 |
-
"# ============================================================\n",
|
| 375 |
"import matplotlib.pyplot as plt\n",
|
| 376 |
"\n",
|
| 377 |
"model.eval()\n",
|
|
|
|
| 378 |
"with torch.no_grad():\n",
|
| 379 |
-
" sample = train_ds[0]\n",
|
| 380 |
-
" inp = sample['input'].unsqueeze(0).to(device)\n",
|
| 381 |
" out = model(\n",
|
| 382 |
-
"
|
| 383 |
-
" sample['f_number'].unsqueeze(0).to(
|
| 384 |
-
" sample['focal_length_mm'].unsqueeze(0).to(
|
| 385 |
-
" sample['focus_distance_m'].unsqueeze(0).to(
|
| 386 |
" )\n",
|
| 387 |
"\n",
|
| 388 |
"fig, axes = plt.subplots(1, 3, figsize=(15, 5))\n",
|
| 389 |
-
"axes[0].imshow(sample['input'].permute(1,2,0).numpy())\n",
|
| 390 |
-
"axes[0].set_title('Input (f/22)')\n",
|
| 391 |
-
"axes[1].imshow(out['bokeh'][0].
|
| 392 |
-
"axes[1].set_title('BokehFlow
|
| 393 |
-
"axes[2].imshow(sample['target'].permute(1,2,0).numpy())\n",
|
| 394 |
-
"axes[2].set_title('Ground
|
| 395 |
"for ax in axes: ax.axis('off')\n",
|
| 396 |
"plt.tight_layout()\n",
|
| 397 |
-
"plt.savefig('result.png', dpi=100)\n",
|
| 398 |
"plt.show()\n",
|
| 399 |
-
"print('✓
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
]
|
| 401 |
}
|
| 402 |
],
|
|
@@ -409,7 +364,8 @@
|
|
| 409 |
"language_info": {
|
| 410 |
"name": "python",
|
| 411 |
"version": "3.10.0"
|
| 412 |
-
}
|
|
|
|
| 413 |
},
|
| 414 |
"nbformat": 4,
|
| 415 |
"nbformat_minor": 4
|
|
|
|
| 5 |
"metadata": {},
|
| 6 |
"source": [
|
| 7 |
"# 🎬 BokehFlow Training Notebook\n",
|
| 8 |
+
"## Zero-download streaming — starts training in ~5 seconds\n",
|
| 9 |
"\n",
|
| 10 |
+
"**How it works:** Metadata (3960 tiny JSONs) fetched async in 3s. Images streamed on-demand via HTTP during training. **Zero disk usage, zero wait.**\n",
|
| 11 |
"\n",
|
| 12 |
+
"| Platform | GPU | Batch/s | Notes |\n",
|
| 13 |
+
"|----------|-----|---------|-------|\n",
|
| 14 |
+
"| Colab Free | T4 16GB | ~2-3s | 4 workers, prefetch hides latency |\n",
|
| 15 |
+
"| Kaggle | 2×T4 | ~1.5s | DataParallel + 8 workers |\n",
|
| 16 |
+
"| Colab Pro | A100 | ~1s | 8 workers |\n",
|
| 17 |
+
"\n",
|
| 18 |
+
"**Just run all cells. No config changes needed.**"
|
| 19 |
]
|
| 20 |
},
|
| 21 |
{
|
|
|
|
| 24 |
"metadata": {},
|
| 25 |
"outputs": [],
|
| 26 |
"source": [
|
| 27 |
+
"#@title Step 0: Install (15s)\n",
|
| 28 |
+
"!pip install -q torch torchvision Pillow huggingface_hub tqdm aiohttp"
|
|
|
|
|
|
|
| 29 |
]
|
| 30 |
},
|
| 31 |
{
|
|
|
|
| 34 |
"metadata": {},
|
| 35 |
"outputs": [],
|
| 36 |
"source": [
|
| 37 |
+
"#@title Step 1: Download BokehFlow model code (2s)\n",
|
|
|
|
|
|
|
| 38 |
"from huggingface_hub import hf_hub_download\n",
|
| 39 |
"hf_hub_download(repo_id='asdf98/BokehFlow', filename='bokehflow.py', local_dir='.')\n",
|
| 40 |
"print('✓ BokehFlow downloaded')"
|
|
|
|
| 46 |
"metadata": {},
|
| 47 |
"outputs": [],
|
| 48 |
"source": [
|
| 49 |
+
"#@title Step 2: Config\n",
|
|
|
|
|
|
|
| 50 |
"CONFIG = {\n",
|
| 51 |
+
" 'variant': 'nano', # 'nano'=583K, 'small'=3.1M, 'base'=12M\n",
|
| 52 |
+
" 'batch_size': 4, # 4 for T4, 8 for A100\n",
|
| 53 |
+
" 'crop_size': 256, # Training crop size\n",
|
| 54 |
+
" 'num_epochs': 5,\n",
|
|
|
|
|
|
|
|
|
|
| 55 |
" 'lr': 3e-4,\n",
|
| 56 |
" 'weight_decay': 0.05,\n",
|
| 57 |
" 'max_grad_norm': 1.0,\n",
|
| 58 |
+
" 'num_workers': 4, # 4 for Colab, 8 for Kaggle\n",
|
| 59 |
+
" 'target_fstop': 2.0, # Train on max bokeh (f/2.0)\n",
|
| 60 |
+
" 'max_samples': None, # None=all 3958, or set 200 for quick test\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
" 'output_dir': './checkpoints',\n",
|
| 62 |
"}\n",
|
| 63 |
"\n",
|
|
|
|
| 64 |
"import torch\n",
|
| 65 |
"NUM_GPUS = torch.cuda.device_count()\n",
|
| 66 |
+
"DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
| 67 |
+
"print(f'Device: {DEVICE}' + (f' ({torch.cuda.get_device_name(0)})' if torch.cuda.is_available() else ''))\n",
|
| 68 |
"if NUM_GPUS > 1:\n",
|
| 69 |
+
" CONFIG['num_workers'] = 8\n",
|
| 70 |
+
" print(f'Kaggle dual-GPU detected → {NUM_GPUS} GPUs, {CONFIG[\"num_workers\"]} workers')"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
]
|
| 72 |
},
|
| 73 |
{
|
|
|
|
| 76 |
"metadata": {},
|
| 77 |
"outputs": [],
|
| 78 |
"source": [
|
| 79 |
+
"#@title Step 3: Streaming Dataset — NO download, starts in ~3s\n",
|
| 80 |
+
"import asyncio, aiohttp, json, io, os, random, time, requests\n",
|
| 81 |
+
"from PIL import Image\n",
|
|
|
|
| 82 |
"from torch.utils.data import Dataset, DataLoader\n",
|
| 83 |
"from torchvision import transforms\n",
|
| 84 |
+
"from concurrent.futures import ThreadPoolExecutor\n",
|
|
|
|
| 85 |
"\n",
|
| 86 |
+
"HF_BASE = 'https://huggingface.co/datasets/timseizinger/RealBokeh_3MP/resolve/main'\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"# ---- Async metadata fetch (3960 JSONs in ~3s) ----\n",
|
| 89 |
+
"async def _fetch_all_metadata(split='train', concurrency=50):\n",
|
| 90 |
+
" split_counts = {'train': 3960, 'validation': 220, 'test': 220}\n",
|
| 91 |
+
" n = split_counts.get(split, 220)\n",
|
| 92 |
+
" async def fetch_one(session, sem, sid):\n",
|
| 93 |
+
" async with sem:\n",
|
| 94 |
+
" url = f'{HF_BASE}/{split}/metadata/{sid}.json'\n",
|
| 95 |
+
" try:\n",
|
| 96 |
+
" async with session.get(url) as r:\n",
|
| 97 |
+
" if r.status == 200:\n",
|
| 98 |
+
" return await r.json(content_type=None)\n",
|
| 99 |
+
" except:\n",
|
| 100 |
+
" pass\n",
|
| 101 |
+
" return None\n",
|
| 102 |
+
" sem = asyncio.Semaphore(concurrency)\n",
|
| 103 |
+
" conn = aiohttp.TCPConnector(limit=concurrency, force_close=False)\n",
|
| 104 |
+
" async with aiohttp.ClientSession(connector=conn) as session:\n",
|
| 105 |
+
" results = await asyncio.gather(*[fetch_one(session, sem, i) for i in range(1, n+1)])\n",
|
| 106 |
+
" return [r for r in results if r is not None]\n",
|
| 107 |
+
"\n",
|
| 108 |
+
"def _build_pairs(metas, split, target_fstop=None):\n",
|
| 109 |
+
" pairs = []\n",
|
| 110 |
+
" for m in metas:\n",
|
| 111 |
+
" for tgt_path, tgt_av in zip(m['target_images'], m['target_avs']):\n",
|
| 112 |
+
" if target_fstop is not None and abs(tgt_av - target_fstop) > 0.05:\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
" continue\n",
|
| 114 |
+
" pairs.append({\n",
|
| 115 |
+
" 'input_path': f\"{split}/{m['source_image']}\",\n",
|
| 116 |
+
" 'gt_path': f'{split}/{tgt_path}',\n",
|
| 117 |
+
" 'f_number': tgt_av,\n",
|
| 118 |
+
" 'focal_mm': float(m.get('focal_length', 50)),\n",
|
| 119 |
+
" 'focus_m': float(m.get('focus_plane_distance', 2.0)),\n",
|
|
|
|
| 120 |
" })\n",
|
| 121 |
+
" return pairs\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"def _fetch_img(path):\n",
|
| 124 |
+
" \"\"\"HTTP fetch image → PIL. No disk write.\"\"\"\n",
|
| 125 |
+
" r = requests.get(f'{HF_BASE}/{path}', timeout=30)\n",
|
| 126 |
+
" r.raise_for_status()\n",
|
| 127 |
+
" return Image.open(io.BytesIO(r.content)).convert('RGB')\n",
|
| 128 |
+
"\n",
|
| 129 |
+
"class RealBokehStream(Dataset):\n",
|
| 130 |
+
" \"\"\"Streaming dataset. Zero disk. Images fetched on-demand via HTTP.\"\"\"\n",
|
| 131 |
+
" def __init__(self, split='train', crop_size=256, target_fstop=2.0, max_samples=None):\n",
|
| 132 |
+
" t0 = time.time()\n",
|
| 133 |
+
" # Async fetch all metadata (~3s)\n",
|
| 134 |
+
" try:\n",
|
| 135 |
+
" loop = asyncio.get_event_loop()\n",
|
| 136 |
+
" if loop.is_running(): # Colab/Jupyter has running loop\n",
|
| 137 |
+
" import nest_asyncio; nest_asyncio.apply()\n",
|
| 138 |
+
" except RuntimeError:\n",
|
| 139 |
+
" pass\n",
|
| 140 |
+
" metas = asyncio.run(_fetch_all_metadata(split))\n",
|
| 141 |
+
" self.pairs = _build_pairs(metas, split, target_fstop)\n",
|
| 142 |
+
" random.shuffle(self.pairs)\n",
|
| 143 |
" if max_samples:\n",
|
| 144 |
+
" self.pairs = self.pairs[:max_samples]\n",
|
| 145 |
+
" self.crop_size = crop_size\n",
|
|
|
|
|
|
|
| 146 |
" self.to_tensor = transforms.ToTensor()\n",
|
| 147 |
+
" print(f' {split}: {len(self.pairs)} pairs ready in {time.time()-t0:.1f}s (zero disk)')\n",
|
| 148 |
+
"\n",
|
| 149 |
" def __len__(self):\n",
|
| 150 |
+
" return len(self.pairs)\n",
|
| 151 |
+
"\n",
|
| 152 |
" def __getitem__(self, idx):\n",
|
| 153 |
+
" p = self.pairs[idx]\n",
|
| 154 |
+
" # Fetch input + GT concurrently (2 threads)\n",
|
| 155 |
+
" with ThreadPoolExecutor(2) as ex:\n",
|
| 156 |
+
" f1 = ex.submit(_fetch_img, p['input_path'])\n",
|
| 157 |
+
" f2 = ex.submit(_fetch_img, p['gt_path'])\n",
|
| 158 |
+
" inp, gt = f1.result(), f2.result()\n",
|
| 159 |
+
"\n",
|
| 160 |
+
" # Synchronized random crop + flip on both images\n",
|
| 161 |
" cs = self.crop_size\n",
|
| 162 |
+
" w, h = inp.size\n",
|
| 163 |
" if w >= cs and h >= cs:\n",
|
| 164 |
+
" x, y = random.randint(0, w-cs), random.randint(0, h-cs)\n",
|
|
|
|
| 165 |
" inp = inp.crop((x, y, x+cs, y+cs))\n",
|
| 166 |
+
" gt = gt.crop((x, y, x+cs, y+cs))\n",
|
| 167 |
" else:\n",
|
| 168 |
" inp = inp.resize((cs, cs), Image.LANCZOS)\n",
|
| 169 |
+
" gt = gt.resize((cs, cs), Image.LANCZOS)\n",
|
|
|
|
|
|
|
| 170 |
" if random.random() > 0.5:\n",
|
| 171 |
" inp = inp.transpose(Image.FLIP_LEFT_RIGHT)\n",
|
| 172 |
+
" gt = gt.transpose(Image.FLIP_LEFT_RIGHT)\n",
|
| 173 |
+
"\n",
|
|
|
|
|
|
|
|
|
|
| 174 |
" return {\n",
|
| 175 |
+
" 'input': self.to_tensor(inp),\n",
|
| 176 |
+
" 'target': self.to_tensor(gt),\n",
|
| 177 |
+
" 'f_number': torch.tensor(p['f_number'], dtype=torch.float32),\n",
|
| 178 |
+
" 'focal_length_mm': torch.tensor(p['focal_mm'], dtype=torch.float32),\n",
|
| 179 |
+
" 'focus_distance_m':torch.tensor(p['focus_m'], dtype=torch.float32),\n",
|
| 180 |
" }\n",
|
| 181 |
"\n",
|
| 182 |
+
"# ---- Create dataset + loader ----\n",
|
| 183 |
+
"print('Fetching metadata (no images downloaded yet)...')\n",
|
| 184 |
+
"try:\n",
|
| 185 |
+
" import nest_asyncio; nest_asyncio.apply() # needed for Jupyter\n",
|
| 186 |
+
"except ImportError:\n",
|
| 187 |
+
" !pip install -q nest_asyncio\n",
|
| 188 |
+
" import nest_asyncio; nest_asyncio.apply()\n",
|
| 189 |
+
"\n",
|
| 190 |
+
"train_ds = RealBokehStream(\n",
|
| 191 |
+
" split='train',\n",
|
| 192 |
" crop_size=CONFIG['crop_size'],\n",
|
| 193 |
" target_fstop=CONFIG['target_fstop'],\n",
|
| 194 |
+
" max_samples=CONFIG['max_samples'],\n",
|
| 195 |
")\n",
|
| 196 |
"\n",
|
| 197 |
"train_loader = DataLoader(\n",
|
| 198 |
+
" train_ds,\n",
|
| 199 |
" batch_size=CONFIG['batch_size'],\n",
|
| 200 |
" shuffle=True,\n",
|
| 201 |
" num_workers=CONFIG['num_workers'],\n",
|
| 202 |
+
" prefetch_factor=2,\n",
|
| 203 |
+
" persistent_workers=True,\n",
|
| 204 |
" drop_last=True,\n",
|
| 205 |
")\n",
|
| 206 |
+
"print(f'✓ DataLoader: {len(train_loader)} batches/epoch, {CONFIG[\"num_workers\"]} workers')\n",
|
| 207 |
+
"print(f' Images streamed on-the-fly. Disk usage: 0 MB')"
|
| 208 |
]
|
| 209 |
},
|
| 210 |
{
|
|
|
|
| 213 |
"metadata": {},
|
| 214 |
"outputs": [],
|
| 215 |
"source": [
|
| 216 |
+
"#@title Step 4: Sanity check — fetch 1 batch\n",
|
| 217 |
+
"import time\n",
|
| 218 |
+
"t0 = time.time()\n",
|
| 219 |
+
"batch = next(iter(train_loader))\n",
|
| 220 |
+
"t1 = time.time()\n",
|
| 221 |
+
"print(f'First batch fetched in {t1-t0:.1f}s')\n",
|
| 222 |
+
"print(f' input: {batch[\"input\"].shape}')\n",
|
| 223 |
+
"print(f' target: {batch[\"target\"].shape}')\n",
|
| 224 |
+
"print(f' f_number: {batch[\"f_number\"]}')\n",
|
| 225 |
+
"print(f' focal_mm: {batch[\"focal_length_mm\"]}')\n",
|
| 226 |
+
"print(f' focus_m: {batch[\"focus_distance_m\"]}')"
|
| 227 |
+
]
|
| 228 |
+
},
|
| 229 |
+
{
|
| 230 |
+
"cell_type": "code",
|
| 231 |
+
"execution_count": null,
|
| 232 |
+
"metadata": {},
|
| 233 |
+
"outputs": [],
|
| 234 |
+
"source": [
|
| 235 |
+
"#@title Step 5: Create model\n",
|
| 236 |
"from bokehflow import BokehFlow, BokehFlowConfig, BokehFlowLoss, model_summary\n",
|
| 237 |
"\n",
|
| 238 |
"config = BokehFlowConfig(variant=CONFIG['variant'])\n",
|
| 239 |
"model = BokehFlow(config)\n",
|
| 240 |
"\n",
|
|
|
|
|
|
|
| 241 |
"if NUM_GPUS > 1:\n",
|
| 242 |
" model = torch.nn.DataParallel(model)\n",
|
| 243 |
+
" print(f'DataParallel on {NUM_GPUS} GPUs')\n",
|
| 244 |
+
"model = model.to(DEVICE)\n",
|
| 245 |
"\n",
|
| 246 |
+
"total_params = sum(p.numel() for p in model.parameters())\n",
|
| 247 |
+
"print(f'\\n✓ BokehFlow-{CONFIG[\"variant\"].capitalize()}: {total_params:,} params on {DEVICE}')"
|
|
|
|
| 248 |
]
|
| 249 |
},
|
| 250 |
{
|
|
|
|
| 253 |
"metadata": {},
|
| 254 |
"outputs": [],
|
| 255 |
"source": [
|
| 256 |
+
"#@title Step 6: Train!\n",
|
|
|
|
|
|
|
|
|
|
| 257 |
"from tqdm.auto import tqdm\n",
|
| 258 |
+
"import torch.nn.functional as F\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
"\n",
|
| 260 |
+
"optimizer = torch.optim.AdamW(model.parameters(), lr=CONFIG['lr'], weight_decay=CONFIG['weight_decay'])\n",
|
| 261 |
+
"scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=CONFIG['num_epochs'] * len(train_loader))\n",
|
| 262 |
"criterion = BokehFlowLoss(lambda_depth=0.5)\n",
|
|
|
|
| 263 |
"os.makedirs(CONFIG['output_dir'], exist_ok=True)\n",
|
| 264 |
"\n",
|
| 265 |
+
"print(f'Training: {CONFIG[\"num_epochs\"]} epochs × {len(train_loader)} batches')\n",
|
| 266 |
+
"print(f'Images streamed from HF Hub — no disk needed\\n')\n",
|
|
|
|
|
|
|
| 267 |
"\n",
|
| 268 |
"for epoch in range(CONFIG['num_epochs']):\n",
|
| 269 |
" model.train()\n",
|
| 270 |
+
" running_loss = 0.0\n",
|
| 271 |
+
" t_epoch = time.time()\n",
|
|
|
|
| 272 |
" pbar = tqdm(train_loader, desc=f'Epoch {epoch+1}/{CONFIG[\"num_epochs\"]}')\n",
|
| 273 |
+
"\n",
|
| 274 |
" for step, batch in enumerate(pbar):\n",
|
| 275 |
+
" inp = batch['input'].to(DEVICE)\n",
|
| 276 |
+
" tgt = batch['target'].to(DEVICE)\n",
|
| 277 |
+
" f_num = batch['f_number'].to(DEVICE)\n",
|
| 278 |
+
" focal = batch['focal_length_mm'].to(DEVICE)\n",
|
| 279 |
+
" focus = batch['focus_distance_m'].to(DEVICE)\n",
|
| 280 |
+
"\n",
|
| 281 |
+
" out = model(inp, f_num, focal, focus)\n",
|
| 282 |
+
" losses = criterion(out, {'bokeh_gt': tgt})\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
" loss = losses['total']\n",
|
| 284 |
+
"\n",
|
|
|
|
| 285 |
" optimizer.zero_grad()\n",
|
| 286 |
" loss.backward()\n",
|
| 287 |
" torch.nn.utils.clip_grad_norm_(model.parameters(), CONFIG['max_grad_norm'])\n",
|
| 288 |
" optimizer.step()\n",
|
| 289 |
" scheduler.step()\n",
|
| 290 |
+
"\n",
|
| 291 |
+
" running_loss += loss.item()\n",
|
| 292 |
+
" pbar.set_postfix(loss=f'{loss.item():.4f}', lr=f'{scheduler.get_last_lr()[0]:.1e}')\n",
|
| 293 |
+
"\n",
|
| 294 |
+
" avg = running_loss / len(train_loader)\n",
|
| 295 |
+
" elapsed = time.time() - t_epoch\n",
|
| 296 |
+
" print(f' → avg_loss={avg:.4f} time={elapsed:.0f}s ({elapsed/len(train_loader):.1f}s/batch)')\n",
|
| 297 |
+
"\n",
|
|
|
|
|
|
|
|
|
|
| 298 |
" # Save checkpoint\n",
|
| 299 |
+
" state = model.module.state_dict() if hasattr(model, 'module') else model.state_dict()\n",
|
| 300 |
+
" ckpt = f'{CONFIG[\"output_dir\"]}/bokehflow_{CONFIG[\"variant\"]}_ep{epoch+1}.pt'\n",
|
| 301 |
+
" torch.save({'epoch': epoch+1, 'model': state, 'loss': avg, 'config': CONFIG}, ckpt)\n",
|
| 302 |
+
" print(f' ✓ Saved {ckpt}')\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
"\n",
|
| 304 |
"print(f'\\n✓ Training complete!')"
|
| 305 |
]
|
|
|
|
| 310 |
"metadata": {},
|
| 311 |
"outputs": [],
|
| 312 |
"source": [
|
| 313 |
+
"#@title Step 7: Visualize result\n",
|
|
|
|
|
|
|
| 314 |
"import matplotlib.pyplot as plt\n",
|
| 315 |
"\n",
|
| 316 |
"model.eval()\n",
|
| 317 |
+
"sample = train_ds[0]\n",
|
| 318 |
"with torch.no_grad():\n",
|
|
|
|
|
|
|
| 319 |
" out = model(\n",
|
| 320 |
+
" sample['input'].unsqueeze(0).to(DEVICE),\n",
|
| 321 |
+
" sample['f_number'].unsqueeze(0).to(DEVICE),\n",
|
| 322 |
+
" sample['focal_length_mm'].unsqueeze(0).to(DEVICE),\n",
|
| 323 |
+
" sample['focus_distance_m'].unsqueeze(0).to(DEVICE),\n",
|
| 324 |
" )\n",
|
| 325 |
"\n",
|
| 326 |
"fig, axes = plt.subplots(1, 3, figsize=(15, 5))\n",
|
| 327 |
+
"axes[0].imshow(sample['input'].permute(1,2,0).cpu().numpy())\n",
|
| 328 |
+
"axes[0].set_title('Input (f/22 sharp)')\n",
|
| 329 |
+
"axes[1].imshow(out['bokeh'][0].permute(1,2,0).cpu().clamp(0,1).numpy())\n",
|
| 330 |
+
"axes[1].set_title('BokehFlow output')\n",
|
| 331 |
+
"axes[2].imshow(sample['target'].permute(1,2,0).cpu().numpy())\n",
|
| 332 |
+
"axes[2].set_title('Ground truth (f/2.0)')\n",
|
| 333 |
"for ax in axes: ax.axis('off')\n",
|
| 334 |
"plt.tight_layout()\n",
|
| 335 |
+
"plt.savefig('result.png', dpi=100, bbox_inches='tight')\n",
|
| 336 |
"plt.show()\n",
|
| 337 |
+
"print('✓ Done!')"
|
| 338 |
+
]
|
| 339 |
+
},
|
| 340 |
+
{
|
| 341 |
+
"cell_type": "code",
|
| 342 |
+
"execution_count": null,
|
| 343 |
+
"metadata": {},
|
| 344 |
+
"outputs": [],
|
| 345 |
+
"source": [
|
| 346 |
+
"#@title (Optional) Push trained model to HuggingFace Hub\n",
|
| 347 |
+
"# from huggingface_hub import HfApi, login\n",
|
| 348 |
+
"# login() # paste your HF token\n",
|
| 349 |
+
"# api = HfApi()\n",
|
| 350 |
+
"# api.upload_file(\n",
|
| 351 |
+
"# path_or_fileobj=f'{CONFIG[\"output_dir\"]}/bokehflow_{CONFIG[\"variant\"]}_ep{CONFIG[\"num_epochs\"]}.pt',\n",
|
| 352 |
+
"# path_in_repo=f'checkpoints/bokehflow_{CONFIG[\"variant\"]}.pt',\n",
|
| 353 |
+
"# repo_id='YOUR_USERNAME/BokehFlow-trained',\n",
|
| 354 |
+
"# )"
|
| 355 |
]
|
| 356 |
}
|
| 357 |
],
|
|
|
|
| 364 |
"language_info": {
|
| 365 |
"name": "python",
|
| 366 |
"version": "3.10.0"
|
| 367 |
+
},
|
| 368 |
+
"accelerator": "GPU"
|
| 369 |
},
|
| 370 |
"nbformat": 4,
|
| 371 |
"nbformat_minor": 4
|