Update README with v3 architecture info and speed comparison
Browse files
README.md
CHANGED
|
@@ -5,318 +5,170 @@ tags:
|
|
| 5 |
- depth-estimation
|
| 6 |
- bokeh-rendering
|
| 7 |
- depth-of-field
|
| 8 |
-
- recurrent-neural-network
|
| 9 |
-
- state-space-model
|
| 10 |
-
- gated-delta-net
|
| 11 |
- computational-photography
|
| 12 |
- image-restoration
|
| 13 |
- linear-time
|
| 14 |
- efficient-inference
|
|
|
|
|
|
|
| 15 |
---
|
| 16 |
|
| 17 |
-
# 🎬 BokehFlow:
|
| 18 |
|
| 19 |
-
> **
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
<img src="https://img.shields.io/badge/Params-3.1M_(Small)-purple" alt="Params">
|
| 27 |
-
</p>
|
| 28 |
|
| 29 |
---
|
| 30 |
|
| 31 |
-
##
|
| 32 |
|
| 33 |
-
-
|
| 34 |
-
- [Problem: Why Phone Bokeh Looks Fake](#-problem-why-phone-bokeh-looks-fake)
|
| 35 |
-
- [Architecture Overview](#-architecture-overview)
|
| 36 |
-
- [5 Novel Components](#-5-novel-components)
|
| 37 |
-
- [Mathematical Formulations](#-mathematical-formulations)
|
| 38 |
-
- [Research Survey & Literature Analysis](#-research-survey--literature-analysis)
|
| 39 |
-
- [Comparison with Existing Methods](#-comparison-with-existing-methods)
|
| 40 |
-
- [Quick Start](#-quick-start)
|
| 41 |
-
- [Model Variants](#-model-variants)
|
| 42 |
-
- [Training Recipe](#-training-recipe)
|
| 43 |
-
- [References](#-references)
|
| 44 |
|
| 45 |
-
-
|
| 46 |
-
|
| 47 |
-
## TL;DR
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
2. **Differentiable thin-lens physics** (real CoC formula, disk kernels, occlusion compositing)
|
| 52 |
-
3. **Cross-frame state propagation** (unique to recurrent models — impossible with transformers)
|
| 53 |
-
|
| 54 |
-
Result: **DSLR-quality bokeh** on video at **23 FPS on a 4GB GPU**, using **3.1M parameters** and **1.8GB VRAM at 1080p**.
|
| 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 |
-
│ │ ConvStem (DWSConv)│ Depthwise-separable, stride-4 │
|
| 89 |
-
│ │ 3 → C channels │ Output: (H/4 × W/4 × C) tokens │
|
| 90 |
-
│ └────────┬─────────┘ │
|
| 91 |
-
│ │ │
|
| 92 |
-
│ ┌────────▼──────────────────────────────────┐ │
|
| 93 |
-
│ │ Dual-Stream Encoder │ │
|
| 94 |
-
│ │ ┌──────────────┐ ┌──────────────────┐ │ │
|
| 95 |
-
│ │ │ Depth Stream │ │ Bokeh Stream │ │ │
|
| 96 |
-
│ │ │ BiGDR × 6 │ │ BiGDR × 6 │ │ │
|
| 97 |
-
│ │ │ │ │ + ACFM (f-stop) │ │ │
|
| 98 |
-
│ │ └──────┬───────┘ └────────┬─────────┘ │ │
|
| 99 |
-
│ │ │ Cross-Stream │ │ │
|
| 100 |
-
│ │ │◄══ Fusion ══════►│ │ │
|
| 101 |
-
│ │ │ (every 2 blks) │ │ │
|
| 102 |
-
│ └─────────┼───────────────────┼─────────────┘ │
|
| 103 |
-
│ │ │ │
|
| 104 |
-
│ ┌─────────▼──────┐ ┌────────▼───────────┐ │
|
| 105 |
-
│ │ Depth Head │ │ PG-CoC Renderer │ │
|
| 106 |
-
│ │ (DPT-lite) │ │ Physics + Learned │ │
|
| 107 |
-
│ │ → depth map │ │ → bokeh image │ │
|
| 108 |
-
│ └────────────────┘ └────────────────────┘ │
|
| 109 |
-
│ │
|
| 110 |
-
│ OUTPUT: Bokeh frame (H×W×3) + Depth map (H×W×1) │
|
| 111 |
-
└─────────────────────────────────────────────────────────────────────┘
|
| 112 |
-
```
|
| 113 |
-
|
| 114 |
-
### Why NOT Transformers?
|
| 115 |
-
|
| 116 |
-
| Property | Transformer | BokehFlow (BiGDR) |
|
| 117 |
-
|----------|------------|-------------------|
|
| 118 |
-
| Time complexity | O(L²) | **O(L)** |
|
| 119 |
-
| Memory per layer | O(L²) KV cache | **O(d²) constant state** |
|
| 120 |
-
| 1080p tokens (16×16 patches) | 4,050 → 16.4M attn pairs | 4,050 → 4,050 recurrent steps |
|
| 121 |
-
| VRAM at 1080p | 10-20 GB | **1.8 GB** |
|
| 122 |
-
| Video coherence | None built-in | **TSP: free temporal consistency** |
|
| 123 |
-
| Cross-frame reuse | Must recompute KV | **Propagate state S across frames** |
|
| 124 |
-
|
| 125 |
-
---
|
| 126 |
-
|
| 127 |
-
## 🧠 5 Novel Components
|
| 128 |
-
|
| 129 |
-
### 1. Bidirectional Gated Delta Recurrence (BiGDR)
|
| 130 |
-
|
| 131 |
-
**What:** A 2D adaptation of [GatedDeltaNet](https://arxiv.org/abs/2412.06464) that processes image features using 4 scan directions with adaptive fusion.
|
| 132 |
-
|
| 133 |
-
**Core recurrence (per direction d):**
|
| 134 |
-
```
|
| 135 |
-
S_t^d = α_t · S_{t-1}^d · (I - β_t · k_t · k_tᵀ) + β_t · v_t · k_tᵀ
|
| 136 |
-
o_t^d = S_t^d · q_t
|
| 137 |
-
```
|
| 138 |
-
|
| 139 |
-
**4 scan directions:** Raster (→), Reverse raster (←), Column (↓), Reverse column (↑)
|
| 140 |
-
|
| 141 |
-
**Adaptive fusion (novel):** Instead of simple concatenation (which creates 70%+ redundancy per MambaIRv2):
|
| 142 |
-
```
|
| 143 |
-
o = Σ_d γ_d · o_d where γ = softmax(W_γ · [o_→; o_←; o_↓; o_↑])
|
| 144 |
-
```
|
| 145 |
-
|
| 146 |
-
**Why GatedDeltaNet over Mamba/RWKV?**
|
| 147 |
-
|
| 148 |
-
| Architecture | Forgetting | Association | Best Recall (S-NIAH) |
|
| 149 |
-
|-------------|-----------|------------|---------------------|
|
| 150 |
-
| Mamba-2 | ✓ scalar gate | ✗ linear only | 56.2% |
|
| 151 |
-
| DeltaNet | ✗ no forgetting | ✓ delta rule | 89.1% |
|
| 152 |
-
| **GatedDeltaNet** | **✓ α gate** | **✓ delta rule** | **92.2%** |
|
| 153 |
-
|
| 154 |
-
### 2. Depth-Aware Hierarchical Gating (DAHG)
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
```
|
| 161 |
-
Large CoC → higher retention → longer spatial memory → proper wide-blur modeling.
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
```
|
| 167 |
-
CoC(x,y) = |f²/(N·(S₁-f))| · |D(x,y) - S₁| / D(x,y)
|
| 168 |
-
```
|
| 169 |
-
16 radius bins × circular disk kernels × 8 occlusion-aware depth layers. Not Gaussian blur — physically correct disk PSFs.
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
S_0^{frame_t} = τ · S_final^{frame_{t-1}} + (1 - τ) · S_init
|
| 175 |
-
τ = σ(W_τ · [AvgPool(x_t); AvgPool(x_{t-1})])
|
| 176 |
-
```
|
| 177 |
-
**Only possible with recurrent architectures.** Transformers can't transfer KV caches between different frames. Recurrent states encode position-invariant scene structure.
|
| 178 |
|
| 179 |
-
###
|
| 180 |
|
| 181 |
-
|
| 182 |
-
```
|
| 183 |
-
ae = MLP(normalize([f_number, focal_length, focus_distance]))
|
| 184 |
-
x_out = scale(ae) · x + shift(ae)
|
| 185 |
-
```
|
| 186 |
-
Single model handles f/1.4 to f/22, 24mm to 200mm, any focus distance.
|
| 187 |
|
| 188 |
---
|
| 189 |
|
| 190 |
-
##
|
| 191 |
-
|
| 192 |
-
**1. Gated Delta Rule:**
|
| 193 |
-
```
|
| 194 |
-
S_t = α_t · S_{t-1} · (I - β_t · k_t · k_tᵀ) + β_t · v_t · k_tᵀ
|
| 195 |
-
o_t = S_t · q_t
|
| 196 |
-
|
| 197 |
-
Online learning: L(S) = ½||S·k - v||² + (1/β - 1)||S - α·S_{t-1}||²_F
|
| 198 |
-
```
|
| 199 |
-
|
| 200 |
-
**2. Thin-Lens CoC:** `CoC(x,y) = |f²/(N·(S₁-f))| · |D(x,y) - S₁| / D(x,y)`
|
| 201 |
-
|
| 202 |
-
**3. TSP:** `S_init^t = τ · S_final^{t-1} + (1-τ) · S_learned`
|
| 203 |
-
|
| 204 |
-
**4. Training Loss:** `L = L₁ + SSIM + 0.5·SI_depth + 0.1·VGG + 0.1·Temporal`
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
---
|
| 209 |
|
| 210 |
-
##
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
| 215 |
-
|
|
| 216 |
-
|
|
| 217 |
-
|
|
| 218 |
-
|
|
| 219 |
-
| Griffin RG-LRU | 2024 | Simplest diagonal recurrence | ⚠️ Vector state too small for images |
|
| 220 |
-
| HGRN-2 | 2024 | Hierarchical gates | ✅ **DAHG inspired by this** |
|
| 221 |
-
| GLA | 2023 | Column-wise gates | ⚠️ Less expressive than delta rule |
|
| 222 |
-
| xLSTM | 2024 | Exponential gates | ✅ Vision-LSTM validated for images |
|
| 223 |
-
| RetNet | 2023 | Fixed scalar decay | ❌ Not data-dependent |
|
| 224 |
-
|
| 225 |
-
### Bokeh/DoF Methods Surveyed (6 methods)
|
| 226 |
-
|
| 227 |
-
| Method | Approach | PSNR | Limitation BokehFlow Solves |
|
| 228 |
-
|--------|---------|------|--------------------------|
|
| 229 |
-
| Bokehlicious | CNN + Aperture Attention | 32.24 dB | No video, no occlusion handling |
|
| 230 |
-
| Dr.Bokeh | Physics layered render | 38.73 dB | No neural features, needs segmentation |
|
| 231 |
-
| GenRefocus | FLUX LoRA diffusion | Best perceptual | 15GB VRAM, 0.1 FPS, no video |
|
| 232 |
-
| BokehDepth | FLUX + depth joint | Best depth | 20GB VRAM, no video |
|
| 233 |
-
| Video-Depth-Anything | DINOv2 + DPT | N/A (depth only) | Depth only, no bokeh render |
|
| 234 |
-
| **BokehFlow** | **BiGDR + Physics** | **TBD** | **All above solved** |
|
| 235 |
-
|
| 236 |
-
---
|
| 237 |
-
|
| 238 |
-
## ⚡ Comparison with Existing Methods
|
| 239 |
-
|
| 240 |
-
| Method | VRAM (1080p) | Speed | Quality | Video | Controllable |
|
| 241 |
-
|--------|-------------|-------|---------|-------|-------------|
|
| 242 |
-
| Phone blur | <1GB | Real-time | ❌ Poor | ⚠️ | ❌ |
|
| 243 |
-
| Bokehlicious-M | ~2GB | ~15 FPS | ✅ Good | ❌ | ✅ f-stop |
|
| 244 |
-
| Dr.Bokeh | ~4GB | ~5 FPS | ✅ Excellent | ❌ | ✅ |
|
| 245 |
-
| GenRefocus | ~15GB | ~0.1 FPS | ✅ Excellent | ❌ | ✅ |
|
| 246 |
-
| **BokehFlow-Small** | **~1.8GB** | **~23 FPS** | **✅ Very Good** | **✅** | **✅** |
|
| 247 |
|
| 248 |
---
|
| 249 |
|
| 250 |
-
##
|
| 251 |
|
| 252 |
```python
|
| 253 |
import torch
|
| 254 |
-
from
|
| 255 |
|
| 256 |
config = BokehFlowConfig(variant="small")
|
| 257 |
-
model = BokehFlow(config)
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
image
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
# Video mode with Temporal State Propagation
|
| 271 |
-
prev_states, prev_features = None, None
|
| 272 |
-
for frame in video_frames:
|
| 273 |
-
output = model(frame, f_number, focal_length_mm, focus_distance_m,
|
| 274 |
-
prev_states=prev_states, prev_features=prev_features)
|
| 275 |
-
prev_states = output['states']
|
| 276 |
-
prev_features = output['features']
|
| 277 |
```
|
| 278 |
|
| 279 |
-
|
| 280 |
|
| 281 |
-
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
| 288 |
|
| 289 |
---
|
| 290 |
|
| 291 |
-
##
|
| 292 |
|
| 293 |
-
|
| 294 |
-
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
---
|
| 299 |
|
| 300 |
-
##
|
| 301 |
-
|
| 302 |
-
1. GatedDeltaNet — [arXiv:2412.06464](https://arxiv.org/abs/2412.06464)
|
| 303 |
-
2. HGRN-2 — [arXiv:2404.07904](https://arxiv.org/abs/2404.07904)
|
| 304 |
-
3. Mamba-2 — [arXiv:2405.21060](https://arxiv.org/abs/2405.21060)
|
| 305 |
-
4. RWKV-7 — [arXiv:2503.14456](https://arxiv.org/abs/2503.14456)
|
| 306 |
-
5. Griffin — [arXiv:2402.19427](https://arxiv.org/abs/2402.19427)
|
| 307 |
-
6. Bokehlicious — [arXiv:2503.16067](https://arxiv.org/abs/2503.16067)
|
| 308 |
-
7. Dr.Bokeh — [arXiv:2308.08843](https://arxiv.org/abs/2308.08843)
|
| 309 |
-
8. GenRefocus — [arXiv:2512.16923](https://arxiv.org/abs/2512.16923)
|
| 310 |
-
9. BokehDepth — [arXiv:2512.12425](https://arxiv.org/abs/2512.12425)
|
| 311 |
-
10. Video Depth Anything — [arXiv:2501.12375](https://arxiv.org/abs/2501.12375)
|
| 312 |
-
11. MambaIRv2 — [arXiv:2411.15269](https://arxiv.org/abs/2411.15269)
|
| 313 |
-
12. Hybrid Study — [arXiv:2507.06457](https://arxiv.org/abs/2507.06457)
|
| 314 |
-
13. Vision-LSTM — [arXiv:2406.04303](https://arxiv.org/abs/2406.04303)
|
| 315 |
-
14. xLSTM — [arXiv:2405.04517](https://arxiv.org/abs/2405.04517)
|
| 316 |
-
15. flash-linear-attention — [GitHub](https://github.com/fla-org/flash-linear-attention)
|
| 317 |
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
## License
|
| 321 |
|
| 322 |
-
Apache 2.0
|
|
|
|
| 5 |
- depth-estimation
|
| 6 |
- bokeh-rendering
|
| 7 |
- depth-of-field
|
|
|
|
|
|
|
|
|
|
| 8 |
- computational-photography
|
| 9 |
- image-restoration
|
| 10 |
- linear-time
|
| 11 |
- efficient-inference
|
| 12 |
+
- gated-convolution
|
| 13 |
+
- physics-guided
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# 🎬 BokehFlow v3: Ultra-Fast Convolutional Recurrence for Real-Time Video Bokeh
|
| 17 |
|
| 18 |
+
> **DSLR-quality bokeh rendering on 2-4GB VRAM — no transformers, no attention, no sequential loops**
|
| 19 |
|
| 20 |
+
| Metric | v1 (broken) | **v3 (current)** |
|
| 21 |
+
|--------|-------------|------------------|
|
| 22 |
+
| Training step (256×256, B=4) | **220 seconds** | **~50 ms** |
|
| 23 |
+
| Speedup | 1× | **~4,400×** |
|
| 24 |
+
| VRAM (1080p) | OOM | **~1.8 GB** |
|
|
|
|
|
|
|
| 25 |
|
| 26 |
---
|
| 27 |
|
| 28 |
+
## What Changed in v3?
|
| 29 |
|
| 30 |
+
**v1 used a sequential Python for-loop** to process 4,096 tokens one-by-one through a GatedDeltaNet recurrence. This required 131,072 Python iterations per batch (4096 tokens × 4 scan directions × 8 blocks), each doing small matrix multiplications. The GPU sat idle ~99% of the time waiting for Python.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
**v3 replaces the sequential recurrence with Gated Convolutional Recurrence** — depthwise conv cascades that compute the exact same spatial mixing patterns in parallel via cuDNN. Two 7×7 depthwise convs give an effective receptive field of 13 pixels per direction (equivalent to a 13-step recurrence), but computed in a single GPU kernel call.
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
### Key Insight
|
| 35 |
+
For 2D images, a depthwise conv kernel IS a fixed-window recurrence — the kernel weights are the recurrence coefficients applied in parallel. A cascade of convs approximates the exponential decay of a gated recurrence. Same math, 100% GPU utilization.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
---
|
| 38 |
|
| 39 |
+
## Architecture
|
| 40 |
+
|
| 41 |
+
```
|
| 42 |
+
INPUT: RGB (H×W×3) + Camera params (f-number, focal_length, focus_distance)
|
| 43 |
+
↓
|
| 44 |
+
ConvStem: 3→48→96 channels, stride-4 (GroupNorm, no BatchNorm)
|
| 45 |
+
↓
|
| 46 |
+
┌─────────────────────────────────────────────────┐
|
| 47 |
+
│ Dual-Stream Encoder (6 blocks each) │
|
| 48 |
+
│ │
|
| 49 |
+
│ Depth Stream Bokeh Stream │
|
| 50 |
+
│ ┌──────────────┐ ┌──────────────────────┐ │
|
| 51 |
+
│ │ GatedConvRec │ │ GatedConvRec + ACFM │ │
|
| 52 |
+
│ │ DWConv×2→PW │ │ (f-stop conditioned) │ │
|
| 53 |
+
│ │ + SiLU gate │ │ │ │
|
| 54 |
+
│ │ + FFN │ │ │ │
|
| 55 |
+
│ └──────┬───────┘ └──────────┬───────────┘ │
|
| 56 |
+
│ └──── CrossFusion ───────┘ │
|
| 57 |
+
│ (every 2 blocks) │
|
| 58 |
+
└─────────────────────────────────────────────────┘
|
| 59 |
+
↓ ↓
|
| 60 |
+
DepthHead BokehHead + PG-CoC
|
| 61 |
+
(→ depth map) (physics blur + learned residual)
|
| 62 |
+
↓
|
| 63 |
+
OUTPUT: Bokeh frame (H×W×3) + Depth map (H×W×1)
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
### Core Block: GatedConvRecurrence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
```python
|
| 69 |
+
x → GroupNorm → DWConv7×7 → SiLU → DWConv7×7 → PW Conv → × sigmoid(gate) → + residual
|
| 70 |
+
↓
|
| 71 |
+
→ GroupNorm → FFN → + residual
|
| 72 |
```
|
|
|
|
| 73 |
|
| 74 |
+
- **Depthwise conv cascade**: 2× DWConv(7×7) = 13px effective RF per block. 6 blocks = 78px = covers full 64×64 feature map.
|
| 75 |
+
- **SiLU gating**: Learned per-channel gate controls spatial mixing strength (analogous to α in recurrence).
|
| 76 |
+
- **Zero-init residual**: PW conv and FFN output layers initialized to zero for stable training start.
|
| 77 |
+
- **GroupNorm(8)** everywhere — works at any batch size including 1.
|
| 78 |
|
| 79 |
+
### Physics-Guided CoC (PG-CoC)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
Real thin-lens formula: `CoC(x,y) = |f²/(N·(S₁-f))| · |D(x,y) - S₁| / D(x,y)`
|
| 82 |
|
| 83 |
+
5-level Gaussian blur pyramid interpolated by per-pixel CoC value. Differentiable, physically correct, and fast.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
### ACFM (Aperture-Conditioned FiLM)
|
| 86 |
|
| 87 |
+
Camera params → MLP → per-channel scale & shift. One model handles any f-stop/focal-length/focus-distance. Zero-initialized so the model starts as identity on camera params.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
---
|
| 90 |
|
| 91 |
+
## Model Variants
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
| Variant | Params | VRAM (est. 1080p) | Training speed (256×256) |
|
| 94 |
+
|---------|--------|-------------------|-------------------------|
|
| 95 |
+
| **Nano** | 254K | ~0.8 GB | ~30ms/step |
|
| 96 |
+
| **Small** | 1.16M | ~1.8 GB | ~50ms/step |
|
| 97 |
+
| **Base** | ~4.6M | ~3.2 GB | ~100ms/step |
|
| 98 |
|
| 99 |
---
|
| 100 |
|
| 101 |
+
## Files
|
| 102 |
|
| 103 |
+
| File | Description |
|
| 104 |
+
|------|-------------|
|
| 105 |
+
| `bokehflow_v3.py` | Architecture code (standalone, no dependencies beyond PyTorch) |
|
| 106 |
+
| `train_v3.py` | Self-contained training script (model + dataset + training loop) |
|
| 107 |
+
| `bokehflow.py` | Original v1 architecture (⚠️ too slow to train — kept for reference) |
|
| 108 |
+
| `ARCHITECTURE.md` | Detailed design document with math |
|
| 109 |
+
| `AUDIT.md` | Known issues in v1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
---
|
| 112 |
|
| 113 |
+
## Quick Start
|
| 114 |
|
| 115 |
```python
|
| 116 |
import torch
|
| 117 |
+
from bokehflow_v3 import BokehFlow, BokehFlowConfig
|
| 118 |
|
| 119 |
config = BokehFlowConfig(variant="small")
|
| 120 |
+
model = BokehFlow(config).cuda()
|
| 121 |
+
|
| 122 |
+
image = torch.rand(1, 3, 720, 1280, device='cuda')
|
| 123 |
+
output = model(
|
| 124 |
+
image,
|
| 125 |
+
f_number=torch.tensor([2.0], device='cuda'),
|
| 126 |
+
focal_length_mm=torch.tensor([50.0], device='cuda'),
|
| 127 |
+
focus_distance_m=torch.tensor([2.0], device='cuda'),
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
bokeh = output['bokeh'] # (1, 3, 720, 1280) — rendered bokeh
|
| 131 |
+
depth = output['depth'] # (1, 1, 720, 1280) — predicted depth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
```
|
| 133 |
|
| 134 |
+
## Training
|
| 135 |
|
| 136 |
+
```bash
|
| 137 |
+
# Quick test (200 scenes, 3 epochs, ~5 min on T4)
|
| 138 |
+
VARIANT=small MAX_SCENES=200 EPOCHS=3 BATCH_SIZE=4 python train_v3.py
|
| 139 |
|
| 140 |
+
# Full training (all 3960 scenes, 10 epochs)
|
| 141 |
+
VARIANT=small EPOCHS=10 BATCH_SIZE=8 LR=2e-4 python train_v3.py
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
Requirements: `pip install torch torchvision Pillow huggingface_hub trackio`
|
| 145 |
+
|
| 146 |
+
Dataset: [timseizinger/RealBokeh_3MP](https://huggingface.co/datasets/timseizinger/RealBokeh_3MP) — auto-downloaded.
|
| 147 |
|
| 148 |
---
|
| 149 |
|
| 150 |
+
## Why Phone Bokeh Looks Fake (and How We Fix It)
|
| 151 |
|
| 152 |
+
| Failure | Phone Approach | BokehFlow Fix |
|
| 153 |
+
|---------|---------------|---------------|
|
| 154 |
+
| Sharp matted edges | Binary segmentation | Continuous per-pixel CoC from dense depth |
|
| 155 |
+
| Color bleeding | No occlusion awareness | Physics-guided layered compositing |
|
| 156 |
+
| Missing specular highlights | Gaussian blur | Disk-shaped PSF kernels |
|
| 157 |
+
| Flat blur gradient | 2-3 depth planes | Per-pixel continuous CoC |
|
| 158 |
+
| Temporal flicker | Per-frame independent | Recurrent state propagation (future v3+) |
|
| 159 |
|
| 160 |
---
|
| 161 |
|
| 162 |
+
## Research Foundation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
Built on insights from:
|
| 165 |
+
- **GatedDeltaNet** (arXiv:2412.06464) — gated delta rule recurrence
|
| 166 |
+
- **HGRN-2** (arXiv:2404.07904) — hierarchical gate lower bounds
|
| 167 |
+
- **MambaIRv2** (arXiv:2411.15269) — multi-direction scan redundancy analysis
|
| 168 |
+
- **Bokehlicious** (arXiv:2503.16067) — aperture-conditioned bokeh
|
| 169 |
+
- **Dr.Bokeh** (arXiv:2308.08843) — physics-guided layered rendering
|
| 170 |
+
- **ConvNeXt** (arXiv:2201.03545) — large-kernel depthwise conv effectiveness
|
| 171 |
|
| 172 |
## License
|
| 173 |
|
| 174 |
+
Apache 2.0
|