“Only absolute URLs are supported” when loading Ke

2020-04-12 09:26发布

问题:

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!

回答1:

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);
    }
})();