forked from microsoft/onnxruntime-inference-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
415 lines (369 loc) · 11.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
// An example how to run segment-anything with webgpu in onnxruntime-web.
//
import ort from 'onnxruntime-web/webgpu';
// the image size on canvas
const MAX_WIDTH = 500;
const MAX_HEIGHT = 500;
// the image size supported by the model
const MODEL_WIDTH = 1024;
const MODEL_HEIGHT = 1024;
const MODELS = {
sam_b: [
{
name: "sam-b-encoder",
url: "https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.encoder-fp16.onnx",
size: 180,
},
{
name: "sam-b-decoder",
url: "https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b_01ec64.decoder.onnx",
size: 17,
},
],
sam_b_int8: [
{
name: "sam-b-encoder-int8",
url: "https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b-encoder-int8.onnx",
size: 108,
},
{
name: "sam-b-decoder-int8",
url: "https://huggingface.co/schmuell/sam-b-fp16/resolve/main/sam_vit_b-decoder-int8.onnx",
size: 5,
},
],
};
const config = getConfig();
ort.env.wasm.wasmPaths = 'dist/';
ort.env.wasm.numThreads = config.threads;
// ort.env.wasm.proxy = config.provider == "wasm";
let canvas;
let filein;
let decoder_latency;
var image_embeddings;
var points = [];
var labels = [];
var imageImageData;
var isClicked = false;
var maskImageData;
function log(i) {
document.getElementById('status').innerText += `\n${i}`;
}
/**
* create config from url
*/
function getConfig() {
const query = window.location.search.substring(1);
var config = {
model: "sam_b",
provider: "webgpu",
device: "gpu",
threads: "1",
};
let vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
let pair = vars[i].split("=");
if (pair[0] in config) {
config[pair[0]] = decodeURIComponent(pair[1]);
} else if (pair[0].length > 0) {
throw new Error("unknown argument: " + pair[0]);
}
}
config.threads = parseInt(config.threads);
config.local = parseInt(config.local);
return config;
}
/**
* clone tensor
*/
function cloneTensor(t) {
return new ort.Tensor(t.type, Float32Array.from(t.data), t.dims);
}
/*
* create feed for the original facebook model
*/
function feedForSam(emb, points, labels) {
const maskInput = new ort.Tensor(new Float32Array(256 * 256), [1, 1, 256, 256]);
const hasMask = new ort.Tensor(new Float32Array([0]), [1,]);
const origianlImageSize = new ort.Tensor(new Float32Array([MODEL_HEIGHT, MODEL_WIDTH]), [2,]);
const pointCoords = new ort.Tensor(new Float32Array(points), [1, points.length / 2, 2]);
const pointLabels = new ort.Tensor(new Float32Array(labels), [1, labels.length]);
return {
"image_embeddings": cloneTensor(emb.image_embeddings),
"point_coords": pointCoords,
"point_labels": pointLabels,
"mask_input": maskInput,
"has_mask_input": hasMask,
"orig_im_size": origianlImageSize
}
}
/*
* Handle cut-out event
*/
async function handleCut(event) {
if (points.length == 0) {
return;
}
const [w, h] = [canvas.width, canvas.height];
// canvas for cut-out
const cutCanvas = new OffscreenCanvas(w, h);
const cutContext = cutCanvas.getContext('2d');
const cutPixelData = cutContext.getImageData(0, 0, w, h);
// need to rescale mask to image size
const maskCanvas = new OffscreenCanvas(w, h);
const maskContext = maskCanvas.getContext('2d');
maskContext.drawImage(await createImageBitmap(maskImageData), 0, 0);
const maskPixelData = maskContext.getImageData(0, 0, w, h);
// copy masked pixels to cut-out
for (let i = 0; i < maskPixelData.data.length; i += 4) {
if (maskPixelData.data[i] > 0) {
for (let j = 0; j < 4; ++j) {
const offset = i + j;
cutPixelData.data[offset] = imageImageData.data[offset];
}
}
}
cutContext.putImageData(cutPixelData, 0, 0);
// Download image
const link = document.createElement('a');
link.download = 'image.png';
link.href = URL.createObjectURL(await cutCanvas.convertToBlob());
link.click();
link.remove();
}
async function decoder(points, labels) {
let ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = imageImageData.width;
canvas.height = imageImageData.height;
ctx.putImageData(imageImageData, 0, 0);
if (points.length > 0) {
// need to wait for encoder to be ready
if (image_embeddings === undefined) {
await MODELS[config.model][0].sess;
}
// wait for encoder to deliver embeddings
const emb = await image_embeddings;
// the decoder
const session = MODELS[config.model][1].sess;
const feed = feedForSam(emb, points, labels);
const start = performance.now();
const res = await session.run(feed);
decoder_latency.innerText = `${(performance.now() - start).toFixed(1)}ms`;
for (let i = 0; i < points.length; i += 2) {
ctx.fillStyle = 'blue';
ctx.fillRect(points[i], points[i + 1], 10, 10);
}
const mask = res.masks;
maskImageData = mask.toImageData();
ctx.globalAlpha = 0.3;
ctx.drawImage(await createImageBitmap(maskImageData), 0, 0);
}
}
function getPoint(event) {
const rect = canvas.getBoundingClientRect();
const x = Math.trunc(event.clientX - rect.left);
const y = Math.trunc(event.clientY - rect.top);
return [x, y];
}
/**
* handler mouse move event
*/
async function handleMouseMove(event) {
if (isClicked) {
return;
}
try {
isClicked = true;
canvas.style.cursor = "wait";
const point = getPoint(event);
await decoder([...points, point[0], point[1]], [...labels, 1]);
}
finally {
canvas.style.cursor = "default";
isClicked = false;
}
}
/**
* handler to handle click event on canvas
*/
async function handleClick(event) {
if (isClicked) {
return;
}
try {
isClicked = true;
canvas.style.cursor = "wait";
const point = getPoint(event);
const label = 1;
points.push(point[0]);
points.push(point[1]);
labels.push(label);
await decoder(points, labels);
}
finally {
canvas.style.cursor = "default";
isClicked = false;
}
}
/**
* handler called when image available
*/
async function handleImage(img) {
const encoder_latency = document.getElementById("encoder_latency");
encoder_latency.innerText = "";
points = [];
labels = [];
filein.disabled = true;
decoder_latency.innerText = "";
canvas.style.cursor = "wait";
image_embeddings = undefined;
let width = img.width;
let height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height = height * (MAX_WIDTH / width);
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width = width * (MAX_HEIGHT / height);
height = MAX_HEIGHT;
}
}
width = Math.round(width);
height = Math.round(height);
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
imageImageData = ctx.getImageData(0, 0, width, height);
const t = await ort.Tensor.fromImage(imageImageData, { resizedWidth: MODEL_WIDTH, resizedHeight: MODEL_HEIGHT });
const feed = (config.isSlimSam) ? { "pixel_values": t } : { "input_image": t };
const session = await MODELS[config.model][0].sess;
const start = performance.now();
image_embeddings = session.run(feed);
image_embeddings.then(() => {
encoder_latency.innerText = `${(performance.now() - start).toFixed(1)}ms`;
canvas.style.cursor = "default";
});
filein.disabled = false;
}
/*
* fetch and cache url
*/
async function fetchAndCache(url, name) {
try {
const cache = await caches.open("onnx");
let cachedResponse = await cache.match(url);
if (cachedResponse == undefined) {
await cache.add(url);
cachedResponse = await cache.match(url);
log(`${name} (network)`);
} else {
log(`${name} (cached)`);
}
const data = await cachedResponse.arrayBuffer();
return data;
} catch (error) {
log(`${name} (network)`);
return await fetch(url).then(response => response.arrayBuffer());
}
}
/*
* load models one at a time
*/
async function load_models(models) {
const cache = await caches.open("onnx");
let missing = 0;
for (const [name, model] of Object.entries(models)) {
let cachedResponse = await cache.match(model.url);
if (cachedResponse === undefined) {
missing += model.size;
}
}
if (missing > 0) {
log(`downloading ${missing} MB from network ... it might take a while`);
} else {
log("loading...");
}
const start = performance.now();
for (const [name, model] of Object.entries(models)) {
try {
const opt = {
executionProviders: [config.provider],
enableMemPattern: false,
enableCpuMemArena: false,
extra: {
session: {
disable_prepacking: "1",
use_device_allocator_for_initializers: "1",
use_ort_model_bytes_directly: "1",
use_ort_model_bytes_for_initializers: "1"
}
},
};
const model_bytes = await fetchAndCache(model.url, model.name);
const extra_opt = model.opt || {};
const sess_opt = { ...opt, ...extra_opt };
model.sess = await ort.InferenceSession.create(model_bytes, sess_opt);
} catch (e) {
log(`${model.url} failed, ${e}`);
}
}
const stop = performance.now();
log(`ready, ${(stop - start).toFixed(1)}ms`);
}
async function main() {
const model = MODELS[config.model];
canvas = document.getElementById("img_canvas");
canvas.style.cursor = "wait";
filein = document.getElementById("file-in");
decoder_latency = document.getElementById("decoder_latency");
document.getElementById("clear-button").addEventListener("click", () => {
points = [];
labels = [];
decoder(points, labels);
});
let img = document.getElementById("original-image");
await load_models(MODELS[config.model]).then(() => {
canvas.addEventListener("click", handleClick);
canvas.addEventListener("mousemove", handleMouseMove);
document.getElementById("cut-button").addEventListener("click", handleCut);
// image upload
filein.onchange = function (evt) {
let target = evt.target || window.event.src, files = target.files;
if (FileReader && files && files.length) {
let fileReader = new FileReader();
fileReader.onload = () => {
img.onload = () => handleImage(img);
img.src = fileReader.result;
}
fileReader.readAsDataURL(files[0]);
}
};
handleImage(img);
}, (e) => {
log(e);
});
}
async function hasFp16() {
try {
const adapter = await navigator.gpu.requestAdapter()
return adapter.features.has('shader-f16')
} catch (e) {
return false
}
}
document.addEventListener("DOMContentLoaded", () => {
hasFp16().then((fp16) => {
if (fp16) {
main();
} else {
log("Your GPU or Browser doesn't support webgpu/f16");
}
});
});