我喜欢在中加载ML模型。
我使用此命令将Keras模型model.h5转换为model.json和二进制权重文件。
tensorflowjs_converter --input_format=keras /tmp/model.h5 /tmp/tfjs_modelApp代码
import * as tf from "@tensorflow/tfjs"
import { bundleResourceIO } from "@tensorflow/tfjs-react-native"
const modelJson = require("model.json")
const modelWeights = require("weights.bin")
await tf.ready().then(async () => {
try {
const modelBundle = bundleResourceIO(modelJson, modelWeights)
console.log("modelBundle", modelBundle)
// Output
/**
* {"modelJson": {"convertedBy": "TensorFlow.js Converter v3.6.0", "format": "layers-model",
* "generatedBy": "keras v2.4.0", "modelTopology": {"backend": "tensorflow",
* "keras_version": "2.4.0", "model_config": [Object]}, "weightsManifest": [[Object]]},
* "modelWeightsId": 4}
*/
await tf
.loadLayersModel(bundleResourceIO(modelJson, modelWeights))
.then((layersModel) => console.log("layersModel", layersModel))
// Output
/**
* [Error: Unknown activation: swish. This may be due to one of the following reasons:
* 1. The activation is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
* 2. The custom activation is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().]
*/
await tf
.loadGraphModel(bundleResourceIO(modelJson, modelWeights))
.then((graphModel) => console.log("graphModel", graphModel))
// Output
/**
* [TypeError: undefined is not an object (evaluating 'graph.versions.producer')]
*/
} catch (error) {
console.log("error", error)
}
})我尝试将model.json中的model.json中的Functional更改为Model,结果仍然相同。
包版本:
"@tensorflow/tfjs": "3.4.0",
"@tensorflow/tfjs-react-native": "^0.5.0",https://stackoverflow.com/questions/67684738
复制相似问题