bksahu
bksahu

Reputation: 220

How to load a converted pre-trained keras model to Tensorflow.js using Node.js?

I have pre-trained keras models that I have conveter using TensorflowJs Converter. I'm trying to load them in this following script

(index.js)

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

require('@tensorflow/tfjs-node');
global.fetch = require('node-fetch')

const model = tf.loadLayersModel(
     'model/model.json');

I'm getting the following error when I execute node index.js

(node:28543) UnhandledPromiseRejectionWarning: Error: Request for model/decoder-model/model.json failed due to error: TypeError: Only absolute URLs are supported

and

(node:28543) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:28543) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I have also tried this

const model = tf.loadLayersModel(
     'https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json');

But here I get

(node:28772) UnhandledPromiseRejectionWarning: Error: Found more than one (2) load handlers for URL 'https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json'

System Information

Node v10.15.3 and TensorflowJs v1.0.1

Upvotes: 3

Views: 2529

Answers (2)

Shanqing Cai
Shanqing Cai

Reputation: 3876

Replace

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

With

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

And remove the line

require('@tensorflow/tfjs-node');

Then, if you are loading a model from the local file system, add 'file://' to the beginning of the argument you give to loadLayersModel().

And it should work

Upvotes: 3

Daniel W.
Daniel W.

Reputation: 32260

The first error is clear, it wants an absolute URL ('/model/model.json') but you feed it a relative one ( 'model/model.json').

The second error is also rather clear, the error tells you that the former thrown error did not get catched (it's therefore Unhandled).

For the last one, please see https://github.com/tensorflow/tfjs/issues/779 or https://github.com/tensorflow/tfjs/issues/622

I think this is because mixing CUDA and non-CUDA things. Check your packages.json first.

Upvotes: 0

Related Questions