diff --git a/examples/pixel-lab/web/site/index.html b/examples/pixel-lab/web/site/index.html index 4ce8aa3a9f..c97e60821f 100644 --- a/examples/pixel-lab/web/site/index.html +++ b/examples/pixel-lab/web/site/index.html @@ -133,6 +133,7 @@

Background replacement, in your browser

+
Starting…
diff --git a/examples/pixel-lab/web/site/main.js b/examples/pixel-lab/web/site/main.js index d011c53cb6..ea2e41c716 100644 --- a/examples/pixel-lab/web/site/main.js +++ b/examples/pixel-lab/web/site/main.js @@ -1,6 +1,6 @@ const stamp = new URL(import.meta.url).searchParams.get('stamp') || Date.now(); const stamped = file => import(`./${file}?stamp=${stamp}`); -const {CAPTURES, model, capture, rvmNetworkSize, formatR, modelsFor, holdsUncertainMask} = await stamped('models.mjs'); +const {CAPTURES, model, capture, rvmNetworkSize, formatR, modelsFor, holdsUncertainMask, offersF16} = await stamped('models.mjs'); const {GpuClient} = await stamped('gpu-client.js'); const {FrameRate} = await stamped('frame-rate.mjs'); const {emptyPump, rememberFrame, startSend, finishSend} = await stamped('frame-pump.mjs'); @@ -207,11 +207,12 @@ $('hold').onchange = () => { state.client.request('hold', {enabled: $('hold').checked}).catch(error => status(error.message)); }; -const LOAD_CONTROLS = ['model', 'camera', 'capture']; +const LOAD_CONTROLS = ['model', 'camera', 'capture', 'f16']; function liveStatus(ratio, ops) { const scale = ratio !== undefined && ratio !== null ? ` (R=${formatR(Number(ratio))})` : ''; - status(`Live: ${state.modelName}, ${state.modelNote}${scale}, ${ops} GPU ops.`); + const precision = state.f16 ? ', f16' : ''; + status(`Live: ${state.modelName}, ${state.modelNote}${scale}${precision}, ${ops} GPU ops.`); } // Keep the chosen model when it still matches. Otherwise use RVM, which fits every shape. @@ -224,12 +225,19 @@ function showModels() { $('model').value = keep; showScale(); showHold(); + showF16(); } function showHold() { $('hold-label').hidden = !holdsUncertainMask(model($('model').value)); } +// f16 is offered only where it is an option, and starts off. +function showF16() { + $('f16-label').hidden = !offersF16(model($('model').value)); + $('f16').checked = false; +} + // The factor for this capture size. The long side of the inner frame stays at 512. function showScale() { const rvm = model($('model').value).kind === 'rvm'; @@ -243,7 +251,9 @@ function showScale() { $('model').onchange = () => { showScale(); showHold(); + showF16(); }; +$('f16').onchange = () => state.running && (stop(), start()); $('capture').onchange = showModels; showModels(); @@ -412,7 +422,9 @@ async function start() { } if (message.mask !== undefined && state.running) maskReady(message.mask); }); - const prepared = await state.client.request('init', {canvas, model: m.id, ratio: 'auto'}, [canvas], 180000); + const f16 = offersF16(m) && $('f16').checked; + const prepared = await state.client.request('init', {canvas, model: m.id, ratio: 'auto', f16}, [canvas], 180000); + state.f16 = prepared.f16; if (generation !== state.generation) return; await applyBackground(); bar(1); diff --git a/examples/pixel-lab/web/site/models.mjs b/examples/pixel-lab/web/site/models.mjs index 889bc4461f..7d12b79120 100644 --- a/examples/pixel-lab/web/site/models.mjs +++ b/examples/pixel-lab/web/site/models.mjs @@ -22,6 +22,7 @@ export const MODELS = [ height: 256, mean: [0, 0, 0], std: [1, 1, 1], + f16Option: true, }, { id: 'mediapipe-landscape', @@ -35,6 +36,7 @@ export const MODELS = [ height: 144, mean: [0, 0, 0], std: [1, 1, 1], + f16Option: true, }, { id: 'pp-humanseg-v2', @@ -61,6 +63,7 @@ export const MODELS = [ height: 288, mean: [0.5, 0.5, 0.5], std: [0.5, 0.5, 0.5], + f16Option: true, }, { id: 'lp-xs', @@ -180,6 +183,16 @@ export function holdsUncertainMask(entry) { return entry.kind !== 'rvm'; } +// Models that may run in f16 with f16 accumulation on request, keeping an f32 camera and mask. +export function offersF16(entry) { + return entry.f16Option === true; +} + +// Whether the network runs in f16: always for an f16Body model, on request for an f16Option one. +export function runsF16(entry, requested) { + return entry.f16Body === true || (offersF16(entry) && requested === true); +} + export function capture(id) { const found = CAPTURES.find(candidate => candidate.id === id); if (!found) throw new Error(`Unsupported capture size ${id}`); diff --git a/examples/pixel-lab/web/site/models.test.mjs b/examples/pixel-lab/web/site/models.test.mjs index 934896bd1d..41be2316f6 100644 --- a/examples/pixel-lab/web/site/models.test.mjs +++ b/examples/pixel-lab/web/site/models.test.mjs @@ -1,6 +1,6 @@ import {test} from 'node:test'; import assert from 'node:assert/strict'; -import {MODELS, CAPTURES, model, capture, modelUrl, rvmRatio, rvmNetworkSize, rvmScale, formatR, frameShape, modelsFor, holdsUncertainMask} from './models.mjs'; +import {MODELS, CAPTURES, model, capture, modelUrl, rvmRatio, rvmNetworkSize, rvmScale, formatR, frameShape, modelsFor, holdsUncertainMask, offersF16, runsF16} from './models.mjs'; test('every model is pinned and has a 16-pixel-aligned size', () => { assert.equal(new Set(MODELS.map(m => m.id)).size, MODELS.length); @@ -108,3 +108,13 @@ test('capture sizes are independent of the model size', () => { assert.equal(capture('640x360').width, 640); assert.throws(() => capture('4000x3000'), /Unsupported/); }); + +test('f16 is an option for MediaPipe and MODNet and always on for Longpipe', () => { + assert.deepEqual(MODELS.filter(offersF16).map(m => m.id), ['mediapipe', 'mediapipe-landscape', 'modnet']); + assert.equal(runsF16(model('modnet'), false), false); + assert.equal(runsF16(model('modnet'), true), true); + assert.equal(runsF16(model('mediapipe'), undefined), false); + assert.equal(runsF16(model('lp-xs'), false), true); + assert.equal(runsF16(model('pp-humanseg-v2'), true), false); + assert.equal(runsF16(model('rvm'), true), false); +}); diff --git a/examples/pixel-lab/web/site/worker.js b/examples/pixel-lab/web/site/worker.js index a4035c2284..5b5d6c1157 100644 --- a/examples/pixel-lab/web/site/worker.js +++ b/examples/pixel-lab/web/site/worker.js @@ -4,11 +4,11 @@ const stamped = file => import(`./${file}?stamp=${stamp}`); // The page posts the first command as soon as the worker is created. A top-level // await yields before onmessage exists, and Chrome drops that message, so the // canvas is transferred and never drawn. Load the modules in the handler instead. -let model, modelUrl, rvmScale, formatR, holdsUncertainMask, shouldWarp, isFirefox, createCompletionPoll; +let model, modelUrl, rvmScale, formatR, holdsUncertainMask, runsF16, shouldWarp, isFirefox, createCompletionPoll; const modulesReady = Promise.all([ stamped('models.mjs'), stamped('pairing.mjs'), stamped('firefox-poll.mjs'), ]).then(([models, pairing, firefoxPoll]) => { - ({model, modelUrl, rvmScale, formatR, holdsUncertainMask} = models); + ({model, modelUrl, rvmScale, formatR, holdsUncertainMask, runsF16} = models); shouldWarp = pairing.shouldWarp; ({isFirefox, createCompletionPoll} = firefoxPoll); }); @@ -157,7 +157,7 @@ async function command(type, payload) { post({status: `Preparing ${m.name} on WebGPU…`}); segmenter = m.kind === 'rvm' ? await Segmenter.create_rvm(bytes, width, height, ratio) - : await Segmenter.create(bytes, width, height, m.mean, m.std, m.f16Body === true); + : await Segmenter.create(bytes, width, height, m.mean, m.std, runsF16(m, payload.f16)); const ops = audit(segmenter); tractDevice = segmenter.device(); device = await (await navigator.gpu.requestAdapter({powerPreference: 'high-performance'})).requestDevice(); @@ -185,7 +185,7 @@ async function command(type, payload) { framesSeen = inferredFrame = 0; lastPresented = null; rvmSize = m.kind === 'rvm' ? {width, height} : null; - return {ops, ratio}; + return {ops, ratio, f16: runsF16(m, payload.f16)}; } if (type === 'ratio') { if (!rvmSize) throw new Error('R factor is only for RVM');