-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.js
501 lines (465 loc) · 17.9 KB
/
utils.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
'use strict';
import {numpy} from './libs/numpy.js';
import {addAlert} from './ui.js';
export function weightsOrigin() {
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
return 'https://d3i5xkfad89fac.cloudfront.net';
} else {
return '..';
}
}
export function sizeOfShape(shape) {
return shape.reduce((a, b) => {
return a * b;
});
}
// This function is used for reading buffer from a given url,
// which will be exported to node.js environment as well,
// so we use 'fs' module for examples ran in node.js and
// fetch() method for examples ran in browser.
export async function getBufferFromUrl(url) {
let arrayBuffer;
if (globalThis.fetch) {
const response = await fetch(url);
arrayBuffer = await response.arrayBuffer();
} else {
const fs = await import('fs');
const uint8Array = await fs.promises.readFile(url);
arrayBuffer = uint8Array.buffer;
}
return arrayBuffer;
}
export async function buildConstantByNpy(builder, url) {
const dataTypeMap = new Map([
['f2', {type: 'float16', array: Uint16Array}],
['f4', {type: 'float32', array: Float32Array}],
['f8', {type: 'float64', array: Float64Array}],
['i1', {type: 'int8', array: Int8Array}],
['i2', {type: 'int16', array: Int16Array}],
['i4', {type: 'int32', array: Int32Array}],
['i8', {type: 'int64', array: BigInt64Array}],
['u1', {type: 'uint8', array: Uint8Array}],
['u2', {type: 'uint16', array: Uint16Array}],
['u4', {type: 'uint32', array: Uint32Array}],
['u8', {type: 'uint64', array: BigUint64Array}],
]);
const response = await fetch(url);
const buffer = await response.arrayBuffer();
const npArray = new numpy.Array(new Uint8Array(buffer));
if (!dataTypeMap.has(npArray.dataType)) {
throw new Error(`Data type ${npArray.dataType} is not supported.`);
}
const dimensions = npArray.shape;
const type = dataTypeMap.get(npArray.dataType).type;
const TypedArrayConstructor = dataTypeMap.get(npArray.dataType).array;
const dataView = new Uint8Array(npArray.data.buffer);
const dataView2 = dataView.slice();
const typedArray = new TypedArrayConstructor(dataView2.buffer);
return builder.constant({dataType: type, type, dimensions}, typedArray);
}
// Convert video frame to a canvas element
export function getVideoFrame(videoElement) {
const canvasElement = document.createElement('canvas');
canvasElement.width = videoElement.videoWidth;
canvasElement.height = videoElement.videoHeight;
const canvasContext = canvasElement.getContext('2d');
canvasContext.drawImage(videoElement, 0, 0, canvasElement.width,
canvasElement.height);
return canvasElement;
}
// Get media stream from camera
export async function getMediaStream() {
// Support 'user' facing mode at present
const constraints = {audio: false, video: {facingMode: 'user'}};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
return stream;
}
// Stop camera stream and cancel animation frame
export function stopCameraStream(id, stream) {
cancelAnimationFrame(id);
if (stream) {
stream.getTracks().forEach((track) => {
if (track.readyState === 'live' && track.kind === 'video') {
track.stop();
}
});
}
}
/**
* This method is used to covert input element to tensor data.
* @param {Object} inputElement, an object of HTML [<img> | <video>] element.
* @param {!Object<string, *>} inputOptions, an object of options to process
* input element.
* inputOptions = {
* inputLayout {String}, // input layout of tensor.
* inputDimensions: {!Array<number>}, // dimensions of input tensor.
* mean: {Array<number>}, // optional, mean values for processing the input
* element. If not specified, it will be set to [0, 0, 0, 0].
* std: {Array<number>}, // optional, std values for processing the input
* element. If not specified, it will be set to [1, 1, 1, 1].
* norm: {Boolean}, // optional, normlization flag. If not specified,
* it will be set to false.
* scaledFlag: {boolean}, // optional, scaling flag. If specified,
* scale the width and height of the input element.
* drawOptions: { // optional, drawOptions is used for
* CanvasRenderingContext2D.drawImage() method.
* sx: {number}, // the x-axis coordinate of the top left corner of
* sub-retangle of the source image.
* sy: {number}, // the y-axis coordinate of the top left corner of
* sub-retangle of the source image.
* sWidth: {number}, // the width of the sub-retangle of the
* source image.
* sHeight: {number}, // the height of the sub-retangle of the
* source image.
* dWidth: {number}, // the width to draw the image in the detination
* canvas.
* dHeight: {number}, // the height to draw the image in the detination
* canvas.
* },
* };
* @return {Object} tensor, an object of input tensor.
*/
export function getInputTensor(inputElement, inputOptions) {
const inputDimensions = inputOptions.inputDimensions;
const tensor = new Float32Array(
inputDimensions.slice(1).reduce((a, b) => a * b));
inputElement.width = inputElement.videoWidth ||
inputElement.naturalWidth;
inputElement.height = inputElement.videoHeight ||
inputElement.naturalHeight;
let [channels, height, width] = inputDimensions.slice(1);
const mean = inputOptions.mean || [0, 0, 0, 0];
const std = inputOptions.std || [1, 1, 1, 1];
const normlizationFlag = inputOptions.norm || false;
const channelScheme = inputOptions.channelScheme || 'RGB';
const scaledFlag = inputOptions.scaledFlag || false;
const inputLayout = inputOptions.inputLayout;
const imageChannels = 4; // RGBA
const drawOptions = inputOptions.drawOptions;
if (inputLayout === 'nhwc') {
[height, width, channels] = inputDimensions.slice(1);
}
const canvasElement = document.createElement('canvas');
canvasElement.width = width;
canvasElement.height = height;
const canvasContext = canvasElement.getContext('2d');
if (drawOptions) {
canvasContext.drawImage(inputElement, drawOptions.sx, drawOptions.sy,
drawOptions.sWidth, drawOptions.sHeight, 0, 0, drawOptions.dWidth,
drawOptions.dHeight);
} else {
if (scaledFlag) {
const resizeRatio = Math.max(Math.max(
inputElement.width / width, inputElement.height / height), 1);
const scaledWidth = Math.floor(inputElement.width / resizeRatio);
const scaledHeight = Math.floor(inputElement.height / resizeRatio);
canvasContext.drawImage(inputElement, 0, 0, scaledWidth, scaledHeight);
} else {
canvasContext.drawImage(inputElement, 0, 0, width, height);
}
}
let pixels = canvasContext.getImageData(0, 0, width, height).data;
if (normlizationFlag) {
pixels = new Float32Array(pixels).map((p) => p / 255);
}
for (let c = 0; c < channels; ++c) {
for (let h = 0; h < height; ++h) {
for (let w = 0; w < width; ++w) {
let value;
if (channelScheme === 'BGR') {
value = pixels[h * width * imageChannels + w * imageChannels +
(channels - c - 1)];
} else {
value = pixels[h * width * imageChannels + w * imageChannels + c];
}
if (inputLayout === 'nchw') {
tensor[c * width * height + h * width + w] =
(value - mean[c]) / std[c];
} else {
tensor[h * width * channels + w * channels + c] =
(value - mean[c]) / std[c];
}
}
}
}
return tensor;
}
// Get median value from an array of Number
export function getMedianValue(array) {
array = array.sort((a, b) => a - b);
return array.length % 2 !== 0 ? array[Math.floor(array.length / 2)] :
(array[array.length / 2 - 1] + array[array.length / 2]) / 2;
}
// Set tf.js backend based WebNN's 'MLDeviceType' option
export async function setPolyfillBackend(device) {
// Simulate WebNN's device selection using various tf.js backends.
// MLDeviceType: ['default', 'gpu', 'cpu']
// 'default' or 'gpu': tfjs-backend-webgl, 'cpu': tfjs-backend-wasm
if (!device) device = 'gpu';
// Use 'webgl' by default for better performance.
// Note: 'wasm' backend may run failed on some samples since
// some ops aren't supported on 'wasm' backend at present
const backend = device === 'cpu' ? 'wasm' : 'webgl';
const context = await navigator.ml.createContext();
const tf = context.tf;
if (tf) {
if (backend == 'wasm') {
const wasm = context.wasm;
// Force to use Wasm SIMD only
wasm.setWasmPath(`https://unpkg.com/@tensorflow/tfjs-backend-wasm@${tf.version_core}/dist/tfjs-backend-wasm-simd.wasm`);
}
if (!(await tf.setBackend(backend))) {
throw new Error(`Failed to set tf.js backend ${backend}.`);
}
await tf.ready();
let backendInfo = backend == 'wasm' ? 'WASM' : 'WebGL';
if (backendInfo == 'WASM') {
const hasSimd = tf.env().features['WASM_HAS_SIMD_SUPPORT'];
const hasThreads = tf.env().features['WASM_HAS_MULTITHREAD_SUPPORT'];
if (hasThreads && hasSimd) {
backendInfo += ' (SIMD + threads)';
} else if (hasThreads && !hasSimd) {
backendInfo += ' (threads)';
} else if (!hasThreads && hasSimd) {
backendInfo += ' (SIMD)';
}
}
addAlert(
`This sample is running on ` +
`<a href='https://github.com/webmachinelearning/webnn-polyfill'>` +
`WebNN-polyfill</a> with tf.js ${tf.version_core} ` +
`<b>${backendInfo}</b> backend.`, 'info');
}
}
// Get url params
export function getUrlParams() {
const params = new URLSearchParams(location.search);
// Get 'numRuns' param to run inference multiple times
let numRuns = params.get('numRuns');
numRuns = numRuns === null ? 1 : parseInt(numRuns);
if (numRuns < 1) {
addAlert(`Ignore the url param: 'numRuns', its value must be >= 1.`);
numRuns = 1;
}
// Get 'powerPreference' param to set WebNN's 'MLPowerPreference' option
let powerPreference = params.get('powerPreference');
const powerPreferences = ['default', 'high-performance', 'low-power'];
if (powerPreference && !powerPreferences.includes(powerPreference)) {
addAlert(`Ignore the url param: 'powerPreference', its value must be ` +
`one of {'default', 'high-performance', 'low-power'}.`);
powerPreference = null;
}
// Get 'numThreads' param to set WebNN's 'numThreads' option
let numThreads = params.get('numThreads');
if (numThreads != null) {
numThreads = parseInt(numThreads);
if (!Number.isInteger(numThreads) || numThreads < 0) {
addAlert(`Ignore the url param: 'numThreads', its value must be ` +
`an integer and not less than 0.`);
numThreads = null;
}
}
return [numRuns, powerPreference, numThreads];
}
// Set backend for using WebNN-polyfill or WebNN
export async function setBackend(backend, device) {
const webnnPolyfillId = 'webnn_polyfill';
const webnnNodeId = 'webnn_node';
const webnnPolyfillElem = document.getElementById(webnnPolyfillId);
const webnnNodeElem = document.getElementById(webnnNodeId);
if (backend === 'polyfill') {
if (webnnNodeElem) {
document.body.removeChild(webnnNodeElem);
// Unset global objects defined in node_setup.js
global.navigator.ml = undefined;
global.MLContext = undefined;
global.MLGraphBuilder = undefined;
global.MLGraph = undefined;
global.MLOperand = undefined;
}
if (!webnnPolyfillElem) {
const webnnPolyfillUrl =
'https://webmachinelearning.github.io/webnn-polyfill/dist/webnn-polyfill.js';
if (typeof(tf) != 'undefined') {
// Reset tf.ENV to avoid environments from tf.min.js
// affect webnn-polyfill.js
tf.engine().reset();
}
// Create WebNN-polyfill script
await loadScript(webnnPolyfillUrl, webnnPolyfillId);
}
await setPolyfillBackend(device);
} else if (backend === 'webnn') {
// For Electron
if (isElectron()) {
if (webnnPolyfillElem) {
document.body.removeChild(webnnPolyfillElem);
}
if (!webnnNodeElem) {
// Create WebNN node script, node_setup.js is located at
// https://github.com/webmachinelearning/webnn-native/tree/main/node/examples/electron/webnn-samples
// Specific for running samples with WebNN node addon on Electron.js
await loadScript('../../node_setup.js', webnnNodeId);
}
addAlert(
`This sample is running on WebNN-native with <b>${device}</b>` +
` backend.`, 'info');
} else {
// For Browser
if (!await isWebNN()) {
addAlert(`WebNN is not supported!`, 'warning');
}
}
} else {
addAlert(`Unknow backend: ${backend}`, 'warning');
}
}
// Promise to load script with url and id
async function loadScript(url, id) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.onload = resolve;
script.onerror = reject;
script.src = url;
script.id = id;
if (url.startsWith('http')) {
script.crossOrigin = 'anonymous';
}
document.body.appendChild(script);
});
}
export function isElectron() {
const userAgent = navigator.userAgent.toLowerCase();
return userAgent.indexOf(' electron/') > -1;
}
export async function isWebNN() {
// This would be used in
// https://github.com/webmachinelearning/webnn-native/tree/main/node/examples/electron/webnn-samples,
// where WebNN is enabled by default.
if (isElectron()) {
return true;
} else {
if (typeof MLGraphBuilder !== 'undefined') {
const context = await navigator.ml.createContext();
return !context.tf;
} else {
return false;
}
}
}
// Derive from
// https://github.com/webmachinelearning/webnn-baseline/blob/main/src/lib/compute-padding.js
/**
* Compute the beginning and ending pad given input, filter and stride sizes.
* @param {String} autoPad
* @param {Number} inputSize
* @param {Number} effectiveFilterSize
* @param {Number} stride
* @param {Number} outputPadding
* @return {Array} [paddingBegin, paddingEnd]
*/
function computePadding1DForAutoPad(
autoPad, inputSize, effectiveFilterSize, stride, outputPadding) {
let totalPadding;
if (outputPadding === undefined) {
// for conv2d
const outSize = Math.ceil(inputSize / stride);
const neededInput = (outSize - 1) * stride + effectiveFilterSize;
totalPadding = neededInput > inputSize ? neededInput - inputSize : 0;
} else {
// for convTranspose2d
// totalPadding = beginning padding + ending padding
// SAME_UPPER or SAME_LOWER mean pad the input so that
// output size = input size * strides
// output size = (input size - 1) * stride + effectiveFilterSize
// - beginning padding - ending padding + output padding
totalPadding = (inputSize - 1) * stride + effectiveFilterSize +
outputPadding - inputSize * stride;
}
let paddingBegin;
let paddingEnd;
switch (autoPad) {
case 'same-upper':
paddingBegin = Math.floor(totalPadding / 2);
paddingEnd = Math.floor((totalPadding + 1) / 2);
break;
case 'same-lower':
paddingBegin = Math.floor((totalPadding + 1) / 2);
paddingEnd = Math.floor(totalPadding / 2);
break;
default:
throw new Error('The autoPad is invalid.');
}
return [paddingBegin, paddingEnd];
}
// Compute explicit padding given input sizes, filter sizes, strides, dilations
// and auto pad mode 'same-upper' or 'same-lower'.
export function computePadding2DForAutoPad(
inputSizes, filterSizes, strides, dilations, autoPad) {
const [inputHeight, inputWidth] = inputSizes;
const [filterHeight, filterWidth] = filterSizes;
const [strideHeight, strideWidth] = strides ? strides : [1, 1];
const [dilationHeight, dilationWidth] = dilations ? dilations: [1, 1];
const effectiveFilterHeight = (filterHeight - 1) * dilationHeight + 1;
const effectiveFilterWidth = (filterWidth - 1) * dilationWidth + 1;
const [beginningPaddingHeight, endingPaddingHeight] =
computePadding1DForAutoPad(
autoPad, inputHeight, effectiveFilterHeight, strideHeight);
const [beginningPaddingWidth, endingPaddingWidth] =
computePadding1DForAutoPad(
autoPad, inputWidth, effectiveFilterWidth, strideWidth);
return [beginningPaddingHeight, endingPaddingHeight,
beginningPaddingWidth, endingPaddingWidth];
}
// This function derives from Transformer.js `permute_data()` function:
// https://github.com/xenova/transformers.js/blob/main/src/utils/maths.js#L98
// which is in Apache License 2.0
// https://github.com/xenova/transformers.js/blob/main/LICENSE
/**
* Helper method to permute a `AnyTypedArray` directly
* @template {AnyTypedArray} T
* @param {T} array
* @param {number[]} dims
* @param {number[]} axes
* @return {[T, number[]]} The permuted array and the new shape.
*/
export function permuteData(array, dims, axes) {
// Calculate the new shape of the permuted array
// and the stride of the original array
const shape = new Array(axes.length);
const stride = new Array(axes.length);
for (let i = axes.length - 1, s = 1; i >= 0; --i) {
stride[i] = s;
shape[i] = dims[axes[i]];
s *= shape[i];
}
// Precompute inverse mapping of stride
const invStride = axes.map((_, i) => stride[axes.indexOf(i)]);
// Create the permuted array with the new shape
// @ts-ignore
const permutedData = new array.constructor(array.length);
// Permute the original array to the new array
for (let i = 0; i < array.length; ++i) {
let newIndex = 0;
for (let j = dims.length - 1, k = i; j >= 0; --j) {
newIndex += (k % dims[j]) * invStride[j];
k = Math.floor(k / dims[j]);
}
permutedData[newIndex] = array[i];
}
return [permutedData, shape];
}
export function getDefaultLayout(deviceType) {
const userAgent = navigator.userAgent;
if (userAgent.indexOf('Linux') != -1 || userAgent.indexOf('Android') != -1 ||
userAgent.indexOf('CrOS') != -1) {
return 'nhwc';
} else {
// Windows or Mac platform.
if (deviceType.indexOf('cpu') != -1) {
return 'nhwc';
} else if (deviceType.indexOf('gpu') != -1) {
return 'nchw';
}
}
}