简体   繁体   English

webworkers中的tensorflow.js

[英]tensorflow.js in webworkers

I want to import 2 scripts in webWorker by importScripts() as follows,but it failed to import. 我想通过importScripts()在webWorker中导入2个脚本,如下所示,但无法导入。 How to deal with it? 怎么处理呢?

self.importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs');
self.importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-converter');

error figure 错误数字

Currently, it is not possible to use the webgl implementation on web-worker, the offlineCanvas being an experimental features. 目前,无法在web-worker上使用webgl实现,offlineCanvas是一个实验性功能。 However, it is possible to use the CPU backend. 但是,可以使用CPU后端。

Here is an example of delegation to the web-worker to perform a computation 以下是委派给Web工作者执行计算的示例

 <head> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.2/dist/tf.min.js"></script> <script> const worker_function = () => { onmessage = () => { console.log('from web worker') this.window = this importScripts('https://cdn.jsdelivr.net/npm/setimmediate@1.0.5/setImmediate.min.js') importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3') tf.setBackend('cpu') const res = tf.zeros([1, 2]).add(tf.ones([1, 2])) res.print() postMessage({res: res.dataSync(), shape: res.shape}) }; } if (window != self) worker_function(); </script> <script> const worker = new Worker(URL.createObjectURL(new Blob(["(" + worker_function.toString() + ")()"], { type: 'text/javascript' }))); worker.postMessage({}); worker.onmessage = (message) => { console.log('from main thread') const {data} = message tf.tensor(data.res, data.shape).print() } </script> </head> 

With tensors the data shared between the main thread and the web worker can be big. 使用张量,主线程和Web工作者之间共享的数据可能很大。 This data is either cloned or transferred. 克隆或传输此数据。

The difference is that if the data is cloned, the web worker will still keep a copy of the data for further processing. 不同之处在于,如果克隆数据,Web工作者仍将保留数据副本以供进一步处理。 When transfer, the ownership of the data is transferred as well. 转移时,也会传输数据的所有权。 Its advantage compare to cloning is the fastness of the transfer, actually it can be viewed as a passing to reference (if ones come from a background of language with pointer) 与克隆相比,它的优势在于转移的牢固性,实际上它可以被视为传递给参考(如果它来自带指针的语言背景)

Let's discuss the performance with these two snippets 让我们用这两个片段讨论性能

 <head> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.2/dist/tf.min.js"></script> <script> const worker_function = () => { onmessage = () => { console.log('from web worker') this.window = this importScripts('https://cdn.jsdelivr.net/npm/setimmediate@1.0.5/setImmediate.min.js') importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3') tf.setBackend('cpu') const res = tf.randomNormal([2000, 2000, 3]) const t0 = performance.now() postMessage({res: res.dataSync().buffer, shape: res.shape}, [res.dataSync().buffer]) console.log(`Prediction took ${(performance.now() - t0).toFixed(1)} ms`) }; } if (window != self) worker_function(); </script> <script> const worker = new Worker(URL.createObjectURL(new Blob(["(" + worker_function.toString() + ")()"], { type: 'text/javascript' }))); worker.postMessage({}); worker.onmessage = (message) => { console.log('from main thread') const {data} = message tf.tensor(new Float32Array(message.data.res), message.data.shape) } </script> </head> 

 <head> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.2/dist/tf.min.js"></script> <script> const worker_function = () => { onmessage = () => { console.log('from web worker') this.window = this importScripts('https://cdn.jsdelivr.net/npm/setimmediate@1.0.5/setImmediate.min.js') importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3') tf.setBackend('cpu') const res = tf.randomNormal([2000, 2000, 3]) const t0 = performance.now() postMessage({res: res.dataSync(), shape: res.shape}) console.log(`Prediction took ${(performance.now() - t0).toFixed(1)} ms`) }; } if (window != self) worker_function(); </script> <script> const worker = new Worker(URL.createObjectURL(new Blob(["(" + worker_function.toString() + ")()"], { type: 'text/javascript' }))); worker.postMessage({}); worker.onmessage = (message) => { console.log('from main thread') const {data} = message tf.tensor(message.data.res, message.data.shape) } </script> </head> 

We can see a difference of around 10ms between the two snippets. 我们可以看到两个片段之间的差异大约为10毫秒。 When performance is at cost, one needs to take into account how the data is shared if it has to be cloned or transferred. 当性能成本时,如果必须克隆或传输数据,则需要考虑如何共享数据。

TensorflowJS needs the canvas to do it's GPU computation and a worker currently doesn't have a canvas. TensorflowJS需要画布来进行GPU计算,而工作者当前没有画布。

OffscreenCanvas is a feature that's being worked on, but before TFJS uses it, it probably needs wide enough browser support. OffscreenCanvas是一项正在使用的功能,但在TFJS使用之前,它可能需要足够广泛的浏览器支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM