Search code examples
web-workertensorflow.js

How to transfer the y (const y= await tf.toPixels(image)) to the webworker use webworker.postMessage?


I want to use the webworker to deal with some tasks.

Main Thread: Firstly,I use tf.loadFrozenModel() to load pre-train model.Secondly,I use model.predict() to predict a image(size:512*512*4).When I use const data = await tf.toPixels(image) to get the image pixels, it takes a lot of time, causing the UI operation to cause a jam. So I want to use webworker to deal with this problem.

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

In webworker:

    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;



}

But it failed. enter image description here


Solution

  • Instead of sending the tensor object to the webworker, you can send a typed array.

    From version 15 onward, the typed array has the same shape as the tensor using tensor.array.

    webworker.postMessage({headpackage:await y.array()})
    
     // Webworker
    
      tf.toPixels(tf.tensor(dataMessage["headpackage"]));
    

    If you're using a version prior to 15, you will need to pass in both the typed array and its shape.

     webworker.postMessage({headpackage:y.dataSync(), shape: y.shape})
    
     // Webworker
    
      tf.toPixels(tf.tensor(dataMessage["headpackage"], dataMessage["shape"]));