0

I have a Tensorflow.js model making predictions and I want to send a certain websocket message each time the predicted label changes.

I have tried to store the latest label in a variable and compare that to the newest one. However, my "cat" websocket message is still activated each second even if the predicted label stays the same. Problem is probably some newbie mistake regarding the use of async function or storing the values - I would really appreciate any directions! Thank you so much.

Code (cleaned to include only relevant bits):


let newLabel;
let oldLabel;


async function app() {

  const model = await tf.loadGraphModel(MODEL_URL);
  console.log('Successfully loaded model');
  const labels = ["cat", "dog", "human"];
  console.log(labels);

  const webcam = await tf.data.webcam(video);


  var myPredict;


  async function predictFunction () {
    myPredict = setInterval (predictLoop, 1000); 

  }

  async function predictLoop () { 

   ...

    const result = await model.predict(t4d); 
    result.print(); 
    result.as1D().argMax().print(); 
    const labelIndex = result.as1D().argMax(); 
    const predictionLabel = result.as1D().argMax().dataSync()[0];
    newLabel = labels[predictionLabel];
    const predict = await result.data();
    const value = predict[predictionLabel]; 


    document.getElementById("demo").innerHTML = `
    I can see a: ${newLabel}\n
    Confidence is: ${value}\n
    `;

    if (newLabel== "cat" && newLabel !==oldLabel){ 
      var data = {
        speed: 100
      }
      socket.emit("cat", data);
      sendParam ("/sound", 1);
  }

    else {
      socket.emit("other"); 
      sendParam ("/sound", 2);
    }

      img.dispose();
      result.dispose();
      smalImg.dispose();
      resized.dispose();
      t4d.dispose();
      tf.engine().endScope();
      await tf.nextFrame();

  } 

  oldLabel = newLabel; 
  predictFunction(); 

} 

app(); 


vjuss
  • 199
  • 1
  • 10
  • Where is "result" defined? Is there an "await" there as well? – Colin G Aug 01 '20 at 17:11
  • @ColinG yes! I added that bit to my question. – vjuss Aug 01 '20 at 17:18
  • You need to declare the `oldLabel` outside of the `predictLoop`. Your current code creates a new variable with each call, it will be `undefined` what you are comparing against. – Bergi Aug 01 '20 at 18:12
  • @Bergi thank you! Makes sense. However, just putting declaring `oldLabel` outside the `predictLoop` did not help. I realized I did not provide the main "app" function which probably makes difference. Would you have a chance to check my updated code in the question? Thanks a lot! – vjuss Aug 01 '20 at 18:59
  • 1
    Nah, the `oldLabel = newLabel;` assignment needs to stay where it was, so that it is executed every time the function runs. Only the `var oldLabel = undefined` declaration should be moved outside, next to the `var myPredict;` preferably. – Bergi Aug 01 '20 at 19:01
  • This did the trick! Thank you so much :) – vjuss Aug 01 '20 at 19:08

0 Answers0