簡體   English   中英

如何使用 Node.js 將轉換后的預訓練 keras 模型加載到 Tensorflow.js?

[英]How to load a converted pre-trained keras model to Tensorflow.js using Node.js?

我有使用 TensorflowJs 轉換器進行轉換的預訓練 keras 模型。 我正在嘗試在以下腳本中加載它們

(索引.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'

系統信息

Node v10.15.3 和 TensorflowJs v1.0.1

代替

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

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

並刪除該行

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

然后,如果您從本地文件系統加載模型,請將“file://”添加到您提供給 loadLayersModel() 的參數的開頭。

它應該工作

第一個錯誤很明顯,它需要一個絕對 URL ( '/model/model.json' ),但您提供了一個相對 URL ( 'model/model.json' )。

第二個錯誤也很清楚,錯誤告訴你前一個拋出的錯誤沒有被捕獲(因此它是Unhandled )。

最后一個請參見https://github.com/tensorflow/tfjs/issues/779https://github.com/tensorflow/tfjs/issues/622

我認為這是因為混合了 CUDA 和非 CUDA 的東西。 首先檢查你的packages.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM