使用TensorflowJS 2.0.1从本机文件系统加载tensorfow模型



我正试图将一个预先训练好的Keras模型加载到我的小型react应用程序中。由于使用了TensorFlow的2.0版本,所以添加和更改的内容很少。我想知道应该如何从本机文件系统加载模型。

  1. 首先导入tensorflowJS
import * as tf from "@tensorflow/tfjs";
  1. 我的目录结构model.json和所有必需的.bin文件都位于我的App.js所在的同一目录中。

  2. 加载预先训练的模型-文档

model = await tf.loadLayersModel("file://model.json");

不幸的是,我获取错误失败。有人能解释一下我缺了什么吗?

您必须使用require((设置模型权重和JSON的路径

const modelJSON = require("../model/model.json");
const modelWeights = require("../model/group1-shard1of1.bin");
const model = await tf.loadLayersModel(bundleResourceIO(modelJSON, modelWeights))

编辑在你有多个bin文件的情况下,你只能使用一个bin文件和一个型号.json

下面的代码将您的keras模型转换为单个bin文件和model.json

tensorflowjs_converter --input_format keras --weight_shard_size_bytes 60000000 path_to_model.h5 path_to_save

expo项目不支持这样的结构。

  1. 将模型文件夹保存在资源中
  2. 导入liabrary";世博资产;来自世博会文件
  3. 然后将代码调用为

const modelJson=Asset.fromModule(require('./assets/model/model.json((

const modelWeights=Asset.fromModule(require('./assets/model/wights.bin'((

最新更新