-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.js
400 lines (370 loc) · 12 KB
/
main.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
'use strict';
import {ResNet50V1FP16Nchw} from './resnet50v1_fp16_nchw.js';
import {EfficientNetFP16Nchw} from './efficientnet_fp16_nchw.js';
import {MobileNetV2Nchw} from './mobilenet_nchw.js';
import {MobileNetV2Nhwc} from './mobilenet_nhwc.js';
import {SqueezeNetNchw} from './squeezenet_nchw.js';
import {SqueezeNetNhwc} from './squeezenet_nhwc.js';
import {ResNet50V2Nchw} from './resnet50v2_nchw.js';
import {ResNet50V2Nhwc} from './resnet50v2_nhwc.js';
import * as ui from '../common/ui.js';
import * as utils from '../common/utils.js';
const maxWidth = 380;
const maxHeight = 380;
const imgElement = document.getElementById('feedElement');
imgElement.src = './images/test.jpg';
const camElement = document.getElementById('feedMediaElement');
let modelName = '';
let modelId = '';
let layout = 'nhwc';
let dataType = 'float32';
let instanceType = modelName + layout;
let rafReq;
let isFirstTimeLoad = true;
let inputType = 'image';
let netInstance = null;
let labels = null;
let stream = null;
let loadTime = 0;
let buildTime = 0;
let computeTime = 0;
let inputOptions;
let deviceType = '';
let lastdeviceType = '';
let backend = '';
let lastBackend = '';
let stopRender = true;
let isRendering = false;
const disabledSelectors = ['#tabs > li', '.btn'];
const modelIds = [
'mobilenet',
'squeezenet',
'resnet50v2',
'resnet50v1',
'efficientnet',
];
const modelList = {
'cpu': {
'float32': [
'mobilenet',
'squeezenet',
'resnet50v2',
],
},
'gpu': {
'float32': [
'mobilenet',
'squeezenet',
'resnet50v2',
],
'float16': [
'efficientnet',
'mobilenet',
'resnet50v1',
],
},
'npu': {
'float16': [
'efficientnet',
'mobilenet',
'resnet50v1',
],
},
};
async function fetchLabels(url) {
const response = await fetch(url);
const data = await response.text();
return data.split('\n');
}
$(document).ready(async () => {
$('.icdisplay').hide();
if (await utils.isWebNN()) {
$('#webnn_cpu').click();
} else {
console.log(utils.webNNNotSupportMessage());
ui.addAlert(utils.webNNNotSupportMessageHTML());
}
});
$('#backendBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
const backendId = $(e.target).attr('id');
layout = utils.getDefaultLayout(backendId);
[backend, deviceType] = backendId.split('_');
// Only show the supported models for each deviceType. Now fp16 nchw models
// are only supported on gpu/npu.
if (backendId == 'webnn_gpu') {
ui.handleBtnUI('#float16Label', false);
ui.handleBtnUI('#float32Label', false);
$('#float32').click();
utils.displayAvailableModels(modelList, modelIds, deviceType, dataType);
} else if (backendId == 'webnn_npu') {
ui.handleBtnUI('#float16Label', false);
ui.handleBtnUI('#float32Label', true);
$('#float16').click();
utils.displayAvailableModels(modelList, modelIds, deviceType, 'float16');
} else {
ui.handleBtnUI('#float16Label', true);
ui.handleBtnUI('#float32Label', false);
$('#float32').click();
utils.displayAvailableModels(modelList, modelIds, deviceType, 'float32');
}
// Uncheck selected model
if (modelId != '') {
$(`#${modelId}`).parent().removeClass('active');
}
});
$('#modelBtns .btn').on('change', async (e) => {
if (inputType === 'camera') {
await stopCamRender();
}
modelId = $(e.target).attr('id');
modelName = modelId;
if (dataType == 'float16') {
modelName += 'fp16';
}
await main();
});
// $('#layoutBtns .btn').on('change', async (e) => {
// if (inputType === 'camera') {
// await stopCamRender();
// }
// layout = $(e.target).attr('id');
// await main();
// });
$('#dataTypeBtns .btn').on('change', async (e) => {
dataType = $(e.target).attr('id');
utils.displayAvailableModels(modelList, modelIds, deviceType, dataType);
// Uncheck selected model
if (modelId != '') {
$(`#${modelId}`).parent().removeClass('active');
}
});
// Click trigger to do inference with <img> element
$('#img').click(async () => {
if (inputType === 'camera') {
await stopCamRender();
} else {
return;
}
inputType = 'image';
$('.shoulddisplay').hide();
await main();
});
$('#imageFile').change((e) => {
const files = e.target.files;
if (files.length > 0) {
$('#feedElement').removeAttr('height');
$('#feedElement').removeAttr('width');
imgElement.src = URL.createObjectURL(files[0]);
}
});
$('#feedElement').on('load', async () => {
await main();
});
// Click trigger to do inference with <video> media element
$('#cam').click(async () => {
if (inputType == 'camera') return;
inputType = 'camera';
$('.shoulddisplay').hide();
await main();
});
function stopCamRender() {
stopRender = true;
utils.stopCameraStream(rafReq, stream);
return new Promise((resolve) => {
// if the rendering is not stopped, check it every 100ms
setInterval(() => {
// resolve when the rendering is stopped
if (!isRendering) {
resolve();
}
}, 100);
});
}
/**
* This method is used to render live camera tab.
*/
async function renderCamStream() {
if (!stream.active || stopRender) return;
// If the video element's readyState is 0, the video's width and height are 0.
// So check the readState here to make sure it is greater than 0.
if (camElement.readyState === 0) {
rafReq = requestAnimationFrame(renderCamStream);
return;
}
isRendering = true;
const inputBuffer = utils.getInputTensor(camElement, inputOptions);
const inputCanvas = utils.getVideoFrame(camElement);
console.log('- Computing... ');
const start = performance.now();
const outputBuffer = await netInstance.compute(inputBuffer);
computeTime = (performance.now() - start).toFixed(2);
console.log(` done in ${computeTime} ms.`);
drawInput(inputCanvas, 'camInCanvas');
showPerfResult();
await drawOutput(outputBuffer, labels);
$('#fps').text(`${(1000/computeTime).toFixed(0)} FPS`);
isRendering = false;
if (!stopRender) {
rafReq = requestAnimationFrame(renderCamStream);
}
}
// Get top 3 classes of labels from output buffer
function getTopClasses(buffer, labels) {
const probs = Array.from(buffer);
const indexes = probs.map((prob, index) => [prob, index]);
const sorted = indexes.sort((a, b) => {
if (a[0] === b[0]) {
return 0;
}
return a[0] < b[0] ? -1 : 1;
});
sorted.reverse();
const classes = [];
for (let i = 0; i < 3; ++i) {
const prob = sorted[i][0];
const index = sorted[i][1];
const c = {
label: labels[index],
prob: (prob * 100).toFixed(2),
};
classes.push(c);
}
return classes;
}
function drawInput(srcElement, canvasId) {
const inputCanvas = document.getElementById(canvasId);
const resizeRatio = Math.max(
Math.max(srcElement.width / maxWidth, srcElement.height / maxHeight), 1);
const scaledWidth = Math.floor(srcElement.width / resizeRatio);
const scaledHeight = Math.floor(srcElement.height / resizeRatio);
inputCanvas.height = scaledHeight;
inputCanvas.width = scaledWidth;
const ctx = inputCanvas.getContext('2d');
ctx.drawImage(srcElement, 0, 0, scaledWidth, scaledHeight);
}
async function drawOutput(outputBuffer, labels) {
const labelClasses = getTopClasses(outputBuffer, labels);
$('#inferenceresult').show();
labelClasses.forEach((c, i) => {
const labelElement = document.getElementById(`label${i}`);
const probElement = document.getElementById(`prob${i}`);
labelElement.innerHTML = `${c.label}`;
probElement.innerHTML = `${c.prob}%`;
});
}
function showPerfResult(medianComputeTime = undefined) {
$('#loadTime').html(`${loadTime} ms`);
$('#buildTime').html(`${buildTime} ms`);
if (medianComputeTime !== undefined) {
$('#computeLabel').html('Median inference time:');
$('#computeTime').html(`${medianComputeTime} ms`);
} else {
$('#computeLabel').html('Inference time:');
$('#computeTime').html(`${computeTime} ms`);
}
}
function constructNetObject(type) {
const netObject = {
'mobilenetfp16nchw': new MobileNetV2Nchw('float16'),
'resnet50v1fp16nchw': new ResNet50V1FP16Nchw(),
'efficientnetfp16nchw': new EfficientNetFP16Nchw(),
'mobilenetnchw': new MobileNetV2Nchw(),
'mobilenetnhwc': new MobileNetV2Nhwc(),
'squeezenetnchw': new SqueezeNetNchw(),
'squeezenetnhwc': new SqueezeNetNhwc(),
'resnet50v2nchw': new ResNet50V2Nchw(),
'resnet50v2nhwc': new ResNet50V2Nhwc(),
};
return netObject[type];
}
async function main() {
try {
if (modelName === '') return;
ui.handleClick(disabledSelectors, true);
if (isFirstTimeLoad) $('#hint').hide();
let start;
const [numRuns, powerPreference, numThreads] = utils.getUrlParams();
// Only do load() and build() when model first time loads,
// there's new model choosed, backend changed or device changed
if (isFirstTimeLoad || instanceType !== modelName + layout ||
lastdeviceType != deviceType || lastBackend != backend) {
if (lastdeviceType != deviceType || lastBackend != backend) {
// Set backend and device
lastdeviceType = lastdeviceType != deviceType ?
deviceType : lastdeviceType;
lastBackend = lastBackend != backend ? backend : lastBackend;
}
instanceType = modelName + layout;
netInstance = constructNetObject(instanceType);
inputOptions = netInstance.inputOptions;
labels = await fetchLabels(inputOptions.labelUrl);
isFirstTimeLoad = false;
console.log(`- Model name: ${modelName}, Model layout: ${layout} -`);
// UI shows model loading progress
await ui.showProgressComponent('current', 'pending', 'pending');
console.log('- Loading weights... ');
const contextOptions = {deviceType};
if (powerPreference) {
contextOptions['powerPreference'] = powerPreference;
}
if (numThreads) {
contextOptions['numThreads'] = numThreads;
}
start = performance.now();
const outputOperand = await netInstance.load(contextOptions);
loadTime = (performance.now() - start).toFixed(2);
console.log(` done in ${loadTime} ms.`);
// UI shows model building progress
await ui.showProgressComponent('done', 'current', 'pending');
console.log('- Building... ');
start = performance.now();
await netInstance.build(outputOperand);
buildTime = (performance.now() - start).toFixed(2);
console.log(` done in ${buildTime} ms.`);
}
// UI shows inferencing progress
await ui.showProgressComponent('done', 'done', 'current');
if (inputType === 'image') {
const inputBuffer = utils.getInputTensor(imgElement, inputOptions);
console.log('- Computing... ');
const computeTimeArray = [];
let medianComputeTime;
// Do warm up
let outputBuffer = await netInstance.compute(inputBuffer);
for (let i = 0; i < numRuns; i++) {
start = performance.now();
outputBuffer = await netInstance.compute(inputBuffer);
computeTime = (performance.now() - start).toFixed(2);
console.log(` compute time ${i+1}: ${computeTime} ms`);
computeTimeArray.push(Number(computeTime));
}
if (numRuns > 1) {
medianComputeTime = utils.getMedianValue(computeTimeArray);
medianComputeTime = medianComputeTime.toFixed(2);
console.log(` median compute time: ${medianComputeTime} ms`);
}
console.log('outputBuffer: ', outputBuffer);
await ui.showProgressComponent('done', 'done', 'done');
ui.readyShowResultComponents();
drawInput(imgElement, 'inputCanvas');
await drawOutput(outputBuffer, labels);
showPerfResult(medianComputeTime);
} else if (inputType === 'camera') {
stream = await utils.getMediaStream();
camElement.srcObject = stream;
stopRender = false;
camElement.onloadeddata = await renderCamStream();
await ui.showProgressComponent('done', 'done', 'done');
ui.readyShowResultComponents();
} else {
throw Error(`Unknown inputType ${inputType}`);
}
} catch (error) {
console.log(error);
ui.addAlert(error.message);
}
ui.handleClick(disabledSelectors, false);
}