首页 > 解决方案 > 在 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 方法,而没有实际使用节点。

提前感谢您的帮助!

标签: node.jstensorflowautoml

解决方案


推荐阅读