问题描述
我想从NodeJS服务器内部的本地文件在Tensorflow.js中加载Keras模型,但出现以下错误:仅支持绝对URL".
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);
}
})();
loadLayersModel是否还不支持本地文件?
Are local files not supported yet with loadLayersModel?
谢谢!
推荐答案
Tensorflow 文档表示您应该使用file://
关键字直接使用文件系统,类似
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");
模型的路径相对于您当前从中调用函数的文件夹.例如,如果上述函数位于/a/b/c中的文件中,而模型位于/a/d/model.json中,则正确的路径为"file://../../d/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".
此外,需要require('@ tensorflow/tfjs-node'),否则将引发以下错误:仅支持HTTP(s)协议".
Moreover, a require('@tensorflow/tfjs-node') is needed,otherwise the following error is thrown: "Only HTTP(s) protocols are supported".
完整的示例:
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);
}
})();
这篇关于“仅支持绝对URL".使用loadLayersModel在Tensorflow.js中加载Keras模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!