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

℡╲_俬逩灬. 提交于 2019-12-02 00:48:48

问题


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


来源:https://stackoverflow.com/questions/56049893/only-absolute-urls-are-supported-when-loading-keras-model-in-tensorflow-js-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!