【问题标题】:Unable to load python trained model in tensorflow.js无法在 tensorflow.js 中加载 python 训练的模型
【发布时间】:2018-12-14 05:02:56
【问题描述】:

当我使用loadModel() 函数 tensorflow.js 尝试加载我在 Python 中训练的模型时遇到以下错误:

加载资源失败:net::ERR_NAME_NOT_RESOLVED

Uncaught (in promise) TypeError: Failed to fetch

下面是 predict.js 文件

console.log ("hello");

let model;
(async function () {
    model = await tf.loadModel("http://keras_model/model.json");
    $(".progress-bar").hide();
    console.log("it works");
})();

目录结构:

main
  -dataset  (contains images for training the model) 
  -training_scripts (python scripts to train the model)
  -user_interface
     --server.js (server made using node.js(and express))
     --static (this folder contains the trained keras model)
         --index.html (html file to be served)
         --predict.js 
         --keras_model(this folder contains the model.json file)

任何帮助将不胜感激!

【问题讨论】:

  • 如果您使用http:// 作为文件类型,您的网址必须是绝对网址
  • 或者如果你使用node并且想要加载一个本地文件你必须使用文件类型file:///
  • 我正在使用节点,但出现此错误。 Fetch API cannot load file:///keras_model/model.json. URL scheme must be "http" or "https" for CORS request.我这里也必须使用绝对路径吗??
  • 您需要tfjs-node吗?
  • 嘿,谢谢你的帮助。它现在正在工作。

标签: javascript python tensorflow tensorflow.js


【解决方案1】:

如果你想在 tfjs 中加载本地文件,你需要使用文件类型file:///,并且为此你需要 tfjs 的节点扩展名。您可以通过在程序中安装并要求 node-fetch 来加载它。

【讨论】:

    【解决方案2】:

    您也可以像这样使用 tfjs-node 中公开的fileSystem 处理程序:

    const tf = require("@tensorflow/tfjs");
    const tfn = require("@tensorflow/tfjs-node");
    const handler = tfn.io.fileSystem("./path/to/your/model.json");
    const model = await tf.loadModel(handler);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2020-03-16
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多