Problem with TensorFlow load model

That technique and the reasons why it’s needed are described on this old post here:

Basically you have this following asynchronous race condition:
You need variable model to be properly initialized before the code reaches this statement:
let results = model.predict(xs);

Most likely b/c the statement above happens much further into the code, your load() function should have completed your JSON file loading by then.

But you shouldn’t count on that for all machines that code might also run.

If the loading takes too much time, there’s a possibility for your code to crash at:
let results = model.predict(xs);.

I’ve refactored your posted code to postpone p5js initialization until the JSON file is fully loaded:

/**
 * TensorFlow JSON Model Loading (v1.0.0)
 * by Omri Bergman (2022-Apr-17)
 * modded by GoToLoop
 *
 * https://Discourse.Processing.org/t/problem-with-tensorflow-load-model/36390/4
 */

'use strict';

const JSON_FILENAME = 'my-model.json';
var model, bg = [ 15, 43, 55 ]; // background rgb array

loadLayersModelJSON();

function loadLayersModelJSON() { // assuming tf is a global variable
  tf.loadLayersModel(JSON_FILENAME).then(gotLayersModel);
}

function gotLayersModel(layersModel) {
  model = layersModel;
  startP5(); // now we can safely start the p5js sketch
}

function startP5() {
  globalThis.setup = setup; // place callback setup() into global context
  globalThis.draw  = draw;  // place callback draw()  into global context

  new p5; // manually instantiate the p5js library
}

const setup = function () { // setup() callback is hidden from p5js for now
  createCanvas(800, 600);
  bg = color(bg); // convert rgb array to p5.Color object
}

const draw = function () { // draw() callback is hidden from p5js for now
  background(bg);
  tf.tidy(predictModel);
}

function predictModel() {
  const
    tensors = tf.tensor2d([ bg._array ]), // p5.Color's normalized rgba
    results = model.predict(tensors),
    [ index ] = results.argMax(1).dataSync(),
    label = labelList[index]; // unknown array variable (at least for me)

  print(label);
}

Obviously not tested b/c I don’t have any TensorFlow library.

1 Like