From 16cbcb6d9461d8d40eb69cdafef6b35368654b66 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 22 Mar 2026 12:34:29 +0100 Subject: feat(cnn_v3): add weight assets to assets.txt, update HOW_TO_CNN export docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add WEIGHTS_CNN_V3 and WEIGHTS_CNN_V3_FILM_MLP to workspaces/main/assets.txt - Add opencv-python and pillow to export_cnn_v3_weights.py uv inline deps - Update HOW_TO_CNN.md §3 export target → workspaces/main/weights/ - Update HOW_TO_CNN.md §4 weight loading → SafeGetAsset (asset system) handoff(Gemini): cnn_v3 weight assets registered; export and C++ load path documented --- cnn_v3/docs/HOW_TO_CNN.md | 26 ++++++++++++-------------- cnn_v3/training/export_cnn_v3_weights.py | 2 ++ workspaces/main/assets.txt | 2 ++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/cnn_v3/docs/HOW_TO_CNN.md b/cnn_v3/docs/HOW_TO_CNN.md index 020f79c..458b68f 100644 --- a/cnn_v3/docs/HOW_TO_CNN.md +++ b/cnn_v3/docs/HOW_TO_CNN.md @@ -458,11 +458,14 @@ Converts a trained `.pth` checkpoint to two raw binary files for the C++ runtime ```bash cd cnn_v3/training -python3 export_cnn_v3_weights.py checkpoints/checkpoint_epoch_200.pth -# writes to export/ by default - python3 export_cnn_v3_weights.py checkpoints/checkpoint_epoch_200.pth \ - --output /path/to/assets/ + --output ../../workspaces/main/weights/ +``` + +Output files are registered in `workspaces/main/assets.txt` as: +``` +WEIGHTS_CNN_V3, BINARY, weights/cnn_v3_weights.bin, "CNN v3 conv weights (f16, 3928 bytes)" +WEIGHTS_CNN_V3_FILM_MLP, BINARY, weights/cnn_v3_film_mlp.bin, "CNN v3 FiLM MLP weights (f32, 3104 bytes)" ``` ### Output files @@ -557,20 +560,15 @@ auto cnn = std::make_shared( ### Uploading weights -Load `cnn_v3_weights.bin` once at startup, before the first `render()`: +Load `cnn_v3_weights.bin` once at startup via the asset system, before the first `render()`: ```cpp -// Read binary file -std::vector data; -{ - std::ifstream f("cnn_v3_weights.bin", std::ios::binary | std::ios::ate); - data.resize(f.tellg()); - f.seekg(0); - f.read(reinterpret_cast(data.data()), data.size()); -} +// Load via asset system +const char* data = SafeGetAsset(AssetId::ASSET_WEIGHTS_CNN_V3); +uint32_t size = GetAssetSize(AssetId::ASSET_WEIGHTS_CNN_V3); // Upload to GPU -cnn->upload_weights(ctx.queue, data.data(), (uint32_t)data.size()); +cnn->upload_weights(ctx.queue, reinterpret_cast(data), size); ``` Before `upload_weights()`: all conv weights are zero, so output is `sigmoid(0) = 0.5` gray. diff --git a/cnn_v3/training/export_cnn_v3_weights.py b/cnn_v3/training/export_cnn_v3_weights.py index d1d2893..99f3a81 100644 --- a/cnn_v3/training/export_cnn_v3_weights.py +++ b/cnn_v3/training/export_cnn_v3_weights.py @@ -3,6 +3,8 @@ # requires-python = ">=3.11" # dependencies = [ # "numpy", +# "opencv-python", +# "pillow", # "torch", # ] # /// diff --git a/workspaces/main/assets.txt b/workspaces/main/assets.txt index 4cb4f40..0af8b7b 100644 --- a/workspaces/main/assets.txt +++ b/workspaces/main/assets.txt @@ -110,6 +110,8 @@ SHADER_CNN_V3_BOTTLENECK, WGSL, ../../cnn_v3/shaders/cnn_v3_bottleneck.wgsl, "CN SHADER_CNN_V3_DEC1, WGSL, ../../cnn_v3/shaders/cnn_v3_dec1.wgsl, "CNN v3 decoder level 1" SHADER_CNN_V3_DEC0, WGSL, ../../cnn_v3/shaders/cnn_v3_dec0.wgsl, "CNN v3 decoder level 0 + sigmoid output" SHADER_DEBUG_DEBUG_PRINT, WGSL, ../../src/shaders/debug/debug_print.wgsl, "Debug print snippet" +WEIGHTS_CNN_V3, BINARY, weights/cnn_v3_weights.bin, "CNN v3 conv weights (f16, 3928 bytes)" +WEIGHTS_CNN_V3_FILM_MLP, BINARY, weights/cnn_v3_film_mlp.bin, "CNN v3 FiLM MLP weights (f32, 3104 bytes)" # --- Sequence Shaders --- SHADER_SEQUENCE_V2_UNIFORMS, WGSL, ../../src/shaders/sequence_uniforms.wgsl, "Sequence Uniforms Snippet" -- cgit v1.2.3