Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Only absolute URLs are supported" when loading Keras model in Tensorflow.js with loadLayersModel

I want to load a Keras model in Tensorflow.js from a local file, inside a NodeJS server, but I get the following error: "Only absolute URLs are supported".

let tf = require("@tensorflow/tfjs");

(async () => {
    try
    {
        const model = await tf.loadLayersModel("/path/to/model.json");
    }
    catch(error)
    {
        console.error(error);
    }
})();

Are local files not supported yet with loadLayersModel?

Thank you!

like image 609
anegru Avatar asked May 08 '19 22:05

anegru


1 Answers

The Tensorflow documentation indicates that you should use direct to your filesystem using the file:// keyword, so something like

tf.loadLayersModel("file://path/to/model.json");

The path to the model is relative to the folder you are currently calling the function from. For example, if the above function is within a file in /a/b/c and the model is in /a/d/model.json the correct path is "file://../../d/model.json".

Moreover, a require('@tensorflow/tfjs-node') is needed,otherwise the following error is thrown: "Only HTTP(s) protocols are supported".

Full working example:

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

(async () => {
    try
    {
        const model = await tf.loadLayersModel('file://relative/path/to/model.json');
    }
    catch(error)
    {
        console.error(error);
    }
})();
like image 171
Abhineet Gupta Avatar answered Nov 15 '22 12:11

Abhineet Gupta