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
|
# CNN v2 Testing Tool
WebGPU-based browser tool for testing trained CNN v2 weights.
---
## Features
- Drag-drop PNG images and `.bin` weights
- Real-time CNN inference with WebGPU compute shaders
- View modes: CNN output, original input, difference (×10)
- Adjustable blend amount and depth
- Data-driven pipeline (supports variable layer count)
- GPU timing display
---
## Requirements
- Browser with WebGPU support:
- Chrome/Edge 113+ (enable `chrome://flags/#enable-unsafe-webgpu` if needed)
- Safari 18+ (macOS Ventura+)
- Trained CNN v2 weights in binary format (`.bin`)
- Test images (PNG format)
---
## Usage
### 1. Open Tool
```bash
open tools/cnn_v2_test/index.html
```
Or use a local server to avoid CORS:
```bash
python3 -m http.server 8000
# Open http://localhost:8000/tools/cnn_v2_test/
```
### 2. Load Data
1. **Drop PNG image** anywhere in window (shows preview immediately)
2. **Drop `.bin` weights** into header drop zone
3. CNN runs automatically when both loaded
### 3. Controls
**Sliders:**
- **Blend:** Mix between original (0.0) and CNN output (1.0)
- **Depth:** Uniform depth value for all pixels (0.0–1.0)
**Keyboard:**
- `SPACE` - Toggle original input view
- `D` - Toggle difference view (×10 amplification)
**Status Bar:**
- Shows GPU timing (ms), image dimensions, and current view mode
- Red text indicates errors
**Console Log:**
- Timestamped event log at bottom
- Tracks file loads, pipeline execution, errors
- Auto-scrolls to latest messages
---
## Preparing Test Data
### Export Weights
```bash
# From trained checkpoint
./training/export_cnn_v2_weights.py \
checkpoints/checkpoint_epoch_100.pth \
--output-weights tools/cnn_v2_test/test_weights.bin
```
Binary format: 16-byte header + 20 bytes per layer + f16 weights (~3.2 KB for 3-layer model)
### Test Images
Use training images or any PNG:
```bash
# Copy test image
cp training/input/test.png tools/cnn_v2_test/
```
**Note:** Grayscale images automatically converted to RGB.
---
## Validation
### Visual Comparison
Compare browser output with C++ tool:
```bash
# Generate C++ output
./build/cnn_test training/input/test.png /tmp/cpp_output.png
# Load same image in browser tool
# Visually compare outputs
```
### GPU Timing
Expected performance:
- 512×512: ~1-2 ms (integrated GPU)
- 1024×1024: ~3-5 ms
- 1920×1080: ~5-8 ms
Slower than expected? Check:
- WebGPU enabled in browser
- Dedicated GPU selected (if available)
- No background tabs consuming GPU
---
## Troubleshooting
### "WebGPU not supported"
- Update browser to latest version
- Enable WebGPU flag: `chrome://flags/#enable-unsafe-webgpu`
- Try Safari 18+ (native WebGPU on macOS)
### "Invalid .bin file"
- Check magic number: `hexdump -C weights.bin | head`
- Should start with: `43 4e 4e 32` ('CNN2')
- Re-export weights: `./training/export_cnn_v2_weights.py`
### Black output / incorrect colors
- Check blend slider (set to 1.0 for full CNN output)
- Verify training converged (loss < 0.01)
- Compare with C++ tool output
### Shader compilation errors
Open browser console (F12) for detailed errors. Common issues:
- Image too large (>4096×4096 not tested)
- Unsupported texture format (rare on modern GPUs)
---
## Architecture
**Pipeline:**
1. **Static Features Pass** - Generate 8D features (RGBD, UV, sin, bias)
2. **CNN Layer Passes** - Compute N layers with ping-pong textures
3. **Display Pass** - Unpack and render with view mode
**Textures:**
- Input: RGBA8 (original image)
- Depth: R32F (uniform depth)
- Static features: RGBA32Uint (8×f16 packed)
- Layer buffers: RGBA32Uint (ping-pong)
**Data-Driven Execution:**
- Layer count read from binary header
- Per-layer params (kernel size, channels, offsets) from binary
- Single CNN shader dispatched N times
---
## TODO
**Side Panel (Right):**
- Display .bin content metadata:
- Layer descriptions (kernel size, channels, weight count)
- Weight statistics (min/max/mean per layer)
- Weight heatmap visualization
- Binary format validation status
- Memory usage breakdown
**Layer Inspection Views:**
- Split R/G/B/A plane visualization
- Intermediate layer output display:
- View static features (8D packed as heatmaps)
- View layer 0 output (before activation)
- View layer 1 output
- Toggle between channels
- Activation heatmaps (where neurons fire)
---
## Extensions (v2+)
Planned enhancements:
**Variable Feature Count:**
- Binary v2: Add `num_features` to header
- Shader: Dynamic feature array or multiple textures
**Multi-Scale Input (Mip Levels):**
- Uncomment mip bindings in static shader
- No binary format change needed
**8-bit Quantized Weights:**
- Binary version bump (format field already present)
- Add quantization codepath in `get_weight()` function
- 2× size reduction (~1.6 KB)
**Pre-defined Test Images:**
- Dropdown menu with training/input/*.png
- Requires local file server
---
## Size
- HTML structure: ~1 KB
- CSS styling: ~1 KB
- JavaScript logic: ~5 KB
- Static shader: ~1 KB
- CNN shader: ~3 KB
- Display shader: ~1 KB
- **Total: ~12 KB** (single file, no dependencies)
---
## See Also
- `doc/CNN_V2.md` - Architecture and design
- `doc/HOWTO.md` - Training workflows
- `training/export_cnn_v2_weights.py` - Binary format
- `src/gpu/effects/cnn_v2_effect.cc` - C++ reference implementation
|