I am new to TensorflowJS and Javascript. I trained a mobilenet with images of diamonds using Tensorflow Object Detection so that it can detect Diamonds. The model is saved in SavedModel format. I converted the SavedModel format into TFJSgraphmodel format using the tensorflowjs_convertor.py and it created a model.json and 29 Shard file. I am using this [https://github.com/dkreider/tensorflowjs-cat-vs-dog] tutorial which is very similar in functionality of uploading a image and predicting after a button is pressed. I changed the await tf.loadLayersModel to await tf.loadGraphModel in its index.js file, as converting from SAvedModel to LayerModel is not supported, I have to use loadGraphModel.

I tried the below code to predict on Click

async function initialize() {
        model = await tf.loadGraphModel('trained-model/model.json');
    }catch(e) {
       console.log("The model is not loaded") 
    classifierElement.style.display = 'block';
    loaderElement.style.display = 'none';

    document.getElementById('predict').addEventListener('click', () => predict());    

async function predict () {

  const imageElement = document.getElementById('img');
  let tensorImg = tf.browser.fromPixels(imageElement).cast('int32').resizeNearestNeighbor([640, 640]);
  const t4d=tensorImg.expandDims(0);

  data = await t4d.data()

  const prediction = model.executeAsync(t4d).then(prediction=> { 
    const data = prediction.dataSync() // you can also use arraySync or their equivalents async methods
    console.log('Predictions: ', data);

But when I run it, I am getting this error:

Uncaught (in promise) TypeError: Cannot read property 'backend' of undefined
    at e.t.moveData (tfjs:17)
    at e.t.get (tfjs:17)
    at Object.dG [as kernelFunc] (tf.min.js:17)
    at n (tfjs:17)
    at tfjs:17
    at e.t.scopedRun (tfjs:17)
    at e.t.runKernelFunc (tfjs:17)
    at e.t.runKernel (tfjs:17)
    at reshape_ (tf.min.js:17)
    at reshape__op (tf.min.js:17)

Kindly help, I am stuck on this for many days. Thanks


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.