5

I am trying to load a local tensorflowjs model on my Node server but am getting the error:

UnhandledPromiseRejectionWarning: TypeError: Only HTTP(S) protocols are supported.

This is how I am importing the Tensorflowjs packages:

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');

global.fetch = require('node-fetch')

and loading the model like this:

const ssd_model_path =
    'file://models/ssd_model/model.json'

this.model = await tf.loadLayersModel(ssd_model_path);

My dependencies are as follows:

"dependencies": {
    "@tensorflow/tfjs": "^1.5.2",
    "@tensorflow/tfjs-core": "^1.2.11",
    "@tensorflow/tfjs-node": "^1.2.3",
    "electron-reload": "^1.5.0",
    "esm": "^3.2.25",
    "jimp": "^0.9.3",
    "node-fetch": "^2.6.0"
  }

2 Answers 2

5

I think tfjs-node is for NodeJS, and tfjs is for the web browser. You shouldn't use both at the same time, because tfjs-node has every thing as dependencies for NodeJS.

If you look at a NodeJS example, then you'll see doesn't use tfjs

https://github.com/tensorflow/tfjs-examples/tree/master/mnist-node

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure this exactly answers your question, but it might be slightly related. For us the issue, was that we had a library, say LIB, which invoked tf.loadLayersModel. We were using LIB in the BROWSER (browser code) and SERVER (back-end server). We basically wanted to use both tfjs and tfjs-node at the "same time", meaning we wanted to make a common library for both front and back end that used tfjs.

Note that we were only calling the tf.loadLayersModel code in LIB only through the browser.

To make the LIB --- more specifically tfjs --- compatible with the BROWSER and the SERVER, we solved it by adding the following conditional import:

// Only use tfjs-node env in node env
// Note, due to webpack? checks we
// put the string outside of the import
// to take advantage of lazy loading.
if (typeof window === 'undefined') {
    const tfjsNode = '@tensorflow/tfjs-node'
    await import(tfjsNode)
}

This allowed us to in the LIB have the load layers code

this.model = await tf.loadLayersModel('file://modelsPath/model.json');

And then call it in the SERVER to load from the file system.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.