如何使用Node.js将转换后的经过训练的keras模型加载到Tensorflow.js中?

时间:2019-03-14 12:06:57

标签: node.js tensorflow tensorflowjs-converter

我已经使用TensorflowJs Converter转换了预训练的keras模型。我正在尝试在以下脚本中加载它们

(index.js)

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

require('@tensorflow/tfjs-node');
global.fetch = require('node-fetch')

const model = tf.loadLayersModel(
     'model/model.json');

执行node index.js

时出现以下错误
(node:28543) UnhandledPromiseRejectionWarning: Error: Request for model/decoder-model/model.json failed due to error: TypeError: Only absolute URLs are supported

(node:28543) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:28543) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我也尝试过

const model = tf.loadLayersModel(
     'https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json');

但是我得到了

(node:28772) UnhandledPromiseRejectionWarning: Error: Found more than one (2) load handlers for URL 'https://storage.googleapis.com/tfjs-models/tfjs/iris_v1/model.json'

系统信息

节点v10.15.3和 TensorflowJs v1.0.1

2 个答案:

答案 0 :(得分:1)

替换

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

使用

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

删除行

require('@tensorflow/tfjs-node');

然后,如果要从本地文件系统加载模型,请在您给loadLayersModel()的参数开头添加'file://'。

它应该可以工作

答案 1 :(得分:0)

第一个错误很明显,它需要一个绝对URL(@Query("SELECT student, (select * from db.calculate_fee (date, id) fee FROM Student student " + "WHERE student.id=:id, " + "AND student.name=:roll") public List<Student> getDetails(@Param("id") Integer id, @Param("roll") Integer roll); ),但您却给它一个相对的URL('/model/model.json')。

第二个错误也很清楚,该错误告诉您前一个抛出的错误未被捕获(因此为'model/model.json')。

关于最后一个,请参阅 https://github.com/tensorflow/tfjs/issues/779  或https://github.com/tensorflow/tfjs/issues/622

我认为这是因为将CUDA和非CUDA混合在一起。首先检查您的Unhandled