TFJS predict vs Python predict

Viewed 200

I trained my model using Keras in Python and I converted my model to a tfjs model to use it in my webapp. I also wrote a small prediction script in python to validate my model on unseen data. In python it works perfectly, but when I'm trying to predict in my webapp it goes wrong.

This is the code I use in Python to create tensors and predict based on these created tensors:

input_dict = {name: tf.convert_to_tensor([value]) for name, value in sample_v.items()}
predictions = model.predict(input_dict)
classes = predictions.argmax(axis=-1)

In TFJS however it seems I can't pass a dict (or object) to the predict function, but if I write code to convert it to a tensor array (like I found on some places online), it still doesn't seem to work.

  Object.keys(input).forEach((k) => {
    input[k] = tensor1d([input[k]]);
  });
  console.log(Object.values(input));

  const prediction = await model.executeAsync(Object.values(input));
  console.log(prediction);

If I do the above, I get the following error: The shape of dict['key_1'] provided in model.execute(dict) must be [-1,1], but was [1]

If I then convert it to this code:

  const input = { ...track.audioFeatures };
  Object.keys(input).forEach((k) => {
    input[k] = tensor2d([input[k]], [1, 1]);
  });
  console.log(Object.values(input));

I get the error that some dtypes have to be int32 but are float32. No problem, I can set the dtype manually:

  const input = { ...track.audioFeatures };
  Object.keys(input).forEach((k) => {
    if (k === 'int_key') {
      input[k] = tensor2d([input[k]], [1, 1], 'int32');
    } else {
      input[k] = tensor2d([input[k]], [1, 1]);
    }
  });
  console.log(Object.values(input));

I still get the same error, but if I print it, I can see the datatype is set to int32.

I'm really confused as to why this is and why I can't just do like python and just put a dict (or object) in TFJS, and how to fix the issues I'm having.

Edit 1: Complete Prediction Snippet

  const model = await loadModel();  
  const input = { ...track.audioFeatures };
  Object.keys(input).forEach((k) => {
    if (k === 'time_signature') {
      input[k] = tensor2d([parseInt(input[k], 10)], [1, 1], 'int32');
    } else {
      input[k] = tensor2d([input[k]], [1, 1]);
    }
  });

  console.log(Object.values(input));
  const prediction = model.predict(Object.values(input));
  console.log(prediction);

Edit 2: added full errormessage full console.log

0 Answers
Related