Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/pixel-lab/web/site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ <h1>Background replacement, in your browser</h1>
<label class="check"><input id="refine" type="checkbox" checked>Smooth mask edges<span class="info"><button type="button" class="info-i" aria-label="About smooth mask edges">i</button><span class="info-text" role="tooltip">Softens the mask and pulls its edge onto the camera image. Works whether or not the mask is moved.</span></span></label>
<label class="check" id="hold-label"><input id="hold" type="checkbox" checked>Hold uncertain mask<span class="info"><button type="button" class="info-i" aria-label="About holding the mask">i</button><span class="info-text" role="tooltip">For the single-frame models. An unsure pixel keeps most of the previous mask. A sure pixel updates. The picture shows background below 0.1 and solid person from 0.7. Hidden for RVM.</span></span></label>
<label class="check"><input id="motion" type="checkbox">Move the mask with the camera<span class="info"><button type="button" class="info-i" aria-label="About moving the mask">i</button><span class="info-text" role="tooltip">Off, the composited picture stays on the camera frame the mask was measured from. On, that pair is shown while the mask keeps up with the camera. When the camera gets ahead, the last mask is moved onto those extra frames, so the output stays on the camera clock. The preview keeps running live.</span></span></label>
<label class="check" id="f16-label" hidden><input id="f16" type="checkbox">Run the model in f16<span class="info"><button type="button" class="info-i" aria-label="About f16">i</button><span class="info-text" role="tooltip">Runs the network in 16-bit floats with 16-bit accumulation. The camera input and the mask stay 32-bit. Faster on most GPUs; the mask can differ slightly from the 32-bit one. Offered for MediaPipe and MODNet. Changing it restarts the camera.</span></span></label>
<button id="go" disabled>Loading…</button>
<div class="bar"><div id="bar"></div></div>
<div id="status">Starting…</div>
Expand Down
20 changes: 16 additions & 4 deletions examples/pixel-lab/web/site/main.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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.
Expand All @@ -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';
Expand All @@ -243,7 +251,9 @@ function showScale() {
$('model').onchange = () => {
showScale();
showHold();
showF16();
};
$('f16').onchange = () => state.running && (stop(), start());
$('capture').onchange = showModels;
showModels();

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions examples/pixel-lab/web/site/models.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const MODELS = [
height: 256,
mean: [0, 0, 0],
std: [1, 1, 1],
f16Option: true,
},
{
id: 'mediapipe-landscape',
Expand All @@ -35,6 +36,7 @@ export const MODELS = [
height: 144,
mean: [0, 0, 0],
std: [1, 1, 1],
f16Option: true,
},
{
id: 'pp-humanseg-v2',
Expand All @@ -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',
Expand Down Expand Up @@ -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}`);
Expand Down
12 changes: 11 additions & 1 deletion examples/pixel-lab/web/site/models.test.mjs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
});
8 changes: 4 additions & 4 deletions examples/pixel-lab/web/site/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down