WebNN Code Generator
The WebNN Code Generator is a browser-based tool that converts machine learning models from ONNX, TensorFlow Lite, and other formats into vanilla JavaScript code using the WebNN API. It operates entirely within your browser to protect your model intellectual property.
Privacy and Security
🔒 Client-Side Processing Only
All model conversion and code generation operations run exclusively in your browser. No model data, weights, or intellectual property is transmitted to or stored on any external servers. This makes the tool safe for:
- Proprietary models
- Sensitive intellectual property
- Enterprise environments with strict data governance
- Any scenario requiring complete data privacy
The WebNN Code Generator , along with WebNN Netron , ensures your models never leave your local environment during the conversion process.
Overview
The WebNN Code Generator provides an alternative to command-line tool ONNX2WebNN , offering a user-friendly web interface for generating WebNN-compatible JavaScript code. The tool supports various model formats and generates optimized code for browser-based machine learning inference.
Workflow
The conversion process involves two complementary tools:
- WebNN Netron - Extracts model structure and weights
- WebNN Code Generator - Generates WebNN JavaScript code
Step-by-Step Guide
Step 1: Extract Model Components
- Navigate to WebNN Netron
- Click the “Open Model…” button
- Select your model file (
.onnx
,.tflite
, or other supported formats) - Once loaded, download the required files:
- Click “Graph” → Download
graph.json
- Click “Weights” → Download
weights.json
andweights.bin
- Click “Graph” → Download
The graph file contains the model architecture, while the weights files contain the trained parameters.
Step 2: Generate WebNN Code
- Open WebNN Code Generator
- Upload the extracted files:
- Click “Choose Graph” → Select
graph.json
- Click “Choose Weights” → Select
weights.json
- Click “Choose Graph” → Select
Step 3: Configure Dynamic Dimensions (if needed)
If your model contains symbolic dimensions, you’ll see a “Set free dimension overrides” section:
- Enter specific values for each dynamic dimension
- This resolves variables like batch size or input dimensions to concrete values
- See symbolic dimensions documentation for details
Step 4: Generate and Download
- Click “Generate WebNN Code”
- Click “Download Code Files” to receive:
- Generated JavaScript file with WebNN implementation
webnn.html
test file for validation
- Put the
weights.bin
downloaded from WebNN Netron together with.js
andwebnn.html
in the same folder
Testing Your Generated Code
Security Requirements
WebNN requires a secure context to function. Valid environments include:
https://
URLshttp://localhost
orhttp://127.0.0.1
- Local development servers
Local Testing
Start a local HTTP server to test your generated code:
# Install http-server if needed
npm install -g http-server
# Start server in your project directory
http-server
# Navigate to http://localhost:8080 in your browser
Open the generated webnn.html
file in your browser to validate the conversion.
Use Cases
The WebNN Code Generator is ideal for:
- Rapid Prototyping: Quickly convert models for web-based testing
- Educational Purposes: Understanding WebNN API structure and usage
- IP-Sensitive Projects: Converting proprietary models without cloud exposure
- Cross-Platform Development: Generating code that runs across WebNN-supported browsers
Best Practices
- Model Preparation: Ensure your model is optimized and has fixed input dimensions when possible
- Dimension Overrides: Carefully set symbolic dimensions based on your expected input data
- Testing: Always validate the generated code with sample inputs before production use
- Performance: Consider model quantization or optimization before conversion for better inference speed
Related Resources
- WebNN Netron - Model visualization tool with enhanced features for WebNN API
- GitHub Issues - WebNN Code Generator - Report bugs and request features
- ONNX2WebNN - Command-line alternative
- WebNN API Specification - Official API documentation