Skip to content

Commit 9a5acc6

Browse files
committed
Support WebGPU
1 parent 9179021 commit 9a5acc6

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

examples/DeepLabv3/web_app/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ <h3>Status</h3>
3333

3434
<div class="actions-card">
3535
<h3>Controls</h3>
36+
<div style="margin-bottom: 10px;">
37+
<label for="backend-select"
38+
style="display: block; margin-bottom: 5px; font-weight: 500;">Backend:</label>
39+
<select id="backend-select"
40+
style="width: 100%; padding: 8px; border-radius: 4px; border: 1px solid #ddd;">
41+
<option value="wasm" selected>WASM (CPU)</option>
42+
<option value="webgl">WebGL (GPU)</option>
43+
<option value="webgpu">WebGPU (GPU)</option>
44+
</select>
45+
</div>
3646
<button id="webcam-btn" class="btn primary" disabled>Start Webcam</button>
3747
<div class="file-upload">
3848
<label for="file-input" class="btn secondary">Upload Image</label>

examples/DeepLabv3/web_app/script.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Configuration
2-
const MODEL_PATH = 'document_detector.onnx';
2+
const QUANTIZED_MODEL_PATH = 'document_detector_quant.onnx';
3+
const FP32_MODEL_PATH = 'document_detector.onnx';
34
const INPUT_SIZE = 384;
45
const MEAN = [0.485, 0.456, 0.406];
56
const STD = [0.229, 0.224, 0.225];
@@ -21,27 +22,49 @@ const canvas = document.getElementById('output-canvas');
2122
const ctx = canvas.getContext('2d');
2223
const webcamVideo = document.getElementById('webcam-video');
2324
const sourceImage = document.getElementById('source-image');
25+
const backendSelect = document.getElementById('backend-select');
2426

2527
// Metrics Elements
2628
const preprocessEl = document.getElementById('preprocess-time');
2729
const inferenceEl = document.getElementById('inference-time');
2830
const postprocessEl = document.getElementById('postprocess-time');
2931
const totalEl = document.getElementById('total-time');
3032
const fpsEl = document.getElementById('fps-counter');
31-
3233
// Initialization
33-
async function init() {
34+
async function init(backend = 'wasm') {
3435
try {
35-
updateStatus('Loading ONNX Runtime...', 'loading');
36+
webcamBtn.disabled = true;
37+
updateStatus(`Initializing ${backend}...`, 'loading');
3638

3739
// Initialize ONNX Runtime
3840
const option = {
39-
executionProviders: ['wasm'],
41+
executionProviders: [backend],
4042
graphOptimizationLevel: 'all'
4143
};
4244

43-
updateStatus('Loading Model...', 'loading');
44-
session = await ort.InferenceSession.create(MODEL_PATH, option);
45+
// Optimization for WASM
46+
if (backend === 'wasm') {
47+
option.executionMode = 'parallel';
48+
option.intraOpNumThreads = navigator.hardwareConcurrency || 4;
49+
}
50+
51+
// Select model based on backend
52+
// WASM -> Quantized (INT8) for CPU speed
53+
// WebGL/WebGPU -> FP32 for GPU shader compatibility
54+
const modelPath = backend === 'wasm' ? QUANTIZED_MODEL_PATH : FP32_MODEL_PATH;
55+
56+
updateStatus(`Loading Model (${backend})...`, 'loading');
57+
58+
// Release existing session if any
59+
if (session) {
60+
session = null;
61+
}
62+
63+
session = await ort.InferenceSession.create(modelPath, option);
64+
65+
// Log the execution provider
66+
console.log('Inference Session created with provider:', session.handler.backendName);
67+
document.getElementById('backend-type').textContent = session.handler.backendName;
4568

4669
updateStatus('Ready', 'ready');
4770
webcamBtn.disabled = false;
@@ -59,7 +82,13 @@ async function init() {
5982
}
6083
}
6184

62-
// Start initialization immediately (no waiting for OpenCV)
85+
// Handle Backend Change
86+
backendSelect.addEventListener('change', (e) => {
87+
init(e.target.value);
88+
});
89+
90+
// Start initialization immediately
91+
init(backendSelect.value);
6392
init();
6493

6594
// Helper: Update Status

0 commit comments

Comments
 (0)