node.js - 在 Node 中使用 AutoML 模型进行图像分类
问题描述
我正在尝试在节点中为 Tenserflow.js 使用 AutoML 边缘模型导出,并根据文件系统上的图像进行预测。Google 仅提供了一个与浏览器相关的 html-images: 方法示例https://cloud.google.com/vision/automl/docs/tensorflow-js-tutorial
。
因此,由于tfjs-automl
您可以像这样加载模型:tf.automl.loadImageClassification('file://./model.json');
. 当您出于某种原因在节点中尝试此操作时,这是不可能的,因为您收到以下错误:
TypeError: Only HTTP(S) protocols are supported
. 因此,tfjs-node
对于这个用例,确实可以让您对tf.loadLayersModel('file://./model.json');
. 但后来我明白了Error: layer: Improper config format:
。所以我尝试tf.loadGraphModel('file://./model.json');
了哪个有效,但你不能使用model.classify(image)
. 你必须使用model.predict(image)
. 因此,经过大量尝试,我得到了以下代码:
const model = await tf.loadLayersModel('file://./model.json');
let data = fs.readFileSync('image.jpg');
let tfimage = tf.node.decodeImage(data, 3);
tfimage = tfimage.resizeBilinear([224,224]);
tfimage = tfimage.reshape([1, 224, 224, 3]);
let result = model.predict(tfimage);
console.log(result);
这给了我以下输出:
Tensor {
kept: false,
isDisposedInternal: false,
shape: [ 2 ],
dtype: 'float32',
size: 2,
strides: [],
dataId: {},
id: 285,
rankType: '1',
scopeId: 290
}
我不知道我应该怎么做,因为我只对我的图像在我的两个班级之一中出现的概率感兴趣。让我大吃一惊的是,这个用例没有示例。每个示例都使用这种浏览器/html 方法,而没有实际使用节点。
提前感谢您的帮助!
解决方案
推荐阅读
- python - 将具有许多二进制数据特征的大量观察结果提供给 TensorFlow
- ios - 快速突出字体的粗体
- node.js - 结果已定义但 result.data 在 .then() 中未定义
- sql - SQL Server:将一行拆分为多行(值)
- css - 在 ReactJS 中逐个淡入节元素
- typescript - 在这个例子的类型声明中这个赋值的意义和目的是什么?
- jquery - 如何在 jquery 中使用 % 而不是 px 来调整大小
- c# - 调用方法作为包装对象
- docker - 更改 Ubuntu docker 容器上的 fs.protectected_hardlinks
- docker - 在 dockerfile 中运行 go build main.go