如何将 y (const y= await tf.toPixels(image)) 传输给 webworker use webworker.postMessage?
How to transfer the y (const y= await tf.toPixels(image)) to the webworker use webworker.postMessage?
我想用webworker来处理一些任务。
主线程:
首先,我使用 tf.loadFrozenModel() 加载预训练 model.Secondly,我使用 model.predict() 来预测图像(大小:512*512*4)。当我使用 const data = await tf.toPixels(image)
获取图像像素,需要大量时间,导致UI操作造成卡顿。所以想用webworker来处理这个问题
const y=tf.tidy(() => {
......
var output=model.predict(
{[INPUT_NODE_NAME]: imageConcat}, OUTPUT_NODE_NAME);
......
return output
})
webworker.postMessage({headpackage:y});//y is the predicted image
在网络工作者中:
importScripts('https://cdn.jsdelivr.net/npm/setimmediate@1.0.5/setImmediate.min.js')
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3')
var dataMessage;
self.addEventListener('message', function (e) {
dataMessage = e.data;
a();
}, false);
async function a() {
const data = await tf.toPixels(dataMessage["headpackage"]);
//Change the value of image data
var image={
data:new Uint8Array(data),
width:512,
height:512
};
tfoutputtexture.image=image;
tfoutputtexture.flipY=true;
tfoutputtexture.needsUpdate = true;
}
但是失败了。
您可以发送类型化数组,而不是将张量对象发送给网络工作者。
从版本 15 开始,类型化数组与使用 tensor.array
的张量具有相同的形状。
webworker.postMessage({headpackage:await y.array()})
// Webworker
tf.toPixels(tf.tensor(dataMessage["headpackage"]));
如果您使用的是 15 之前的版本,则需要传入类型数组及其形状。
webworker.postMessage({headpackage:y.dataSync(), shape: y.shape})
// Webworker
tf.toPixels(tf.tensor(dataMessage["headpackage"], dataMessage["shape"]));
我想用webworker来处理一些任务。
主线程:
首先,我使用 tf.loadFrozenModel() 加载预训练 model.Secondly,我使用 model.predict() 来预测图像(大小:512*512*4)。当我使用 const data = await tf.toPixels(image)
获取图像像素,需要大量时间,导致UI操作造成卡顿。所以想用webworker来处理这个问题
const y=tf.tidy(() => {
......
var output=model.predict(
{[INPUT_NODE_NAME]: imageConcat}, OUTPUT_NODE_NAME);
......
return output
})
webworker.postMessage({headpackage:y});//y is the predicted image
在网络工作者中:
importScripts('https://cdn.jsdelivr.net/npm/setimmediate@1.0.5/setImmediate.min.js')
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3')
var dataMessage;
self.addEventListener('message', function (e) {
dataMessage = e.data;
a();
}, false);
async function a() {
const data = await tf.toPixels(dataMessage["headpackage"]);
//Change the value of image data
var image={
data:new Uint8Array(data),
width:512,
height:512
};
tfoutputtexture.image=image;
tfoutputtexture.flipY=true;
tfoutputtexture.needsUpdate = true;
}
但是失败了。
您可以发送类型化数组,而不是将张量对象发送给网络工作者。
从版本 15 开始,类型化数组与使用 tensor.array
的张量具有相同的形状。
webworker.postMessage({headpackage:await y.array()})
// Webworker
tf.toPixels(tf.tensor(dataMessage["headpackage"]));
如果您使用的是 15 之前的版本,则需要传入类型数组及其形状。
webworker.postMessage({headpackage:y.dataSync(), shape: y.shape})
// Webworker
tf.toPixels(tf.tensor(dataMessage["headpackage"], dataMessage["shape"]));