node.js - 隐式形状不能是小数

标签 node.js tensorflow tensorflow.js

我正在尝试在 TensorFlow 中创建标签。 这是image在下面的代码中。

async function main(){
    const model = await mobilenet.load();
    const classifier = await knnClassifier.create();
    const buf = fs.readFileSync('./41.png');
    const decode = tfnode.node.decodeImage(buf);
    console.log(decode)
    return new Promise(async resolve => {
        const embedding = model.infer(
            decode,
            false
        );
        classifier.addExample(
            embedding,
            'rong'
        )
    })
}

我得到了这个错误:

(node:17992) UnhandledPromiseRejectionWarning: Error: The implicit shape can't be a fractional number. Got 200704 / 150528

这是我 console.log() 解码时的情况:

Tensor {
  kept: false,
  isDisposedInternal: false,
  shape: [ 475, 475, 4 ],
  dtype: 'int32',
  size: 902500,
  strides: [ 1900, 4 ],
  dataId: {},
  id: 264,
  rankType: '3',
  scopeId: 311
}

最佳答案

我昨晚也遇到了和你一样的问题。我想知道为什么它不适用于 PNG 图像,但 JPG 图像却可以正常工作。事实证明,它与number of channels有关。从缓冲区解码图像时使用。

当我们使用 decodeImage 函数 ( reference ) 时,它对 JPG 使用 3 个 channel ,对 PNG 使用 4 个 channel 。然而,该模型预计有 3 个 channel 。解码图像后,我们可以明确告诉 decodeImage 使用 3 个 channel 来解码所有类型的图像。

修改后的代码如下所示:

async function main() {
    const model = await mobilenet.load();
    const classifier = await knnClassifier.create();
    const buf = fs.readFileSync('./41.png');
    const decode = tfnode.node.decodeImage(buf, 3);

    return new Promise(async resolve => {
        const embedding = model.infer(decode, false);
        classifier.addExample(embedding, 'rong');
    })
}

其中 decodeImage 现在接受第二个参数,指示图像解码期间要使用的 channel 数。

关于node.js - 隐式形状不能是小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63421470/

相关文章:

javascript - curl获取的文件如何清除

node.js catch block 在等待时未按预期触发

python - 急切执行 : Could not find valid device for node. {{node Conv2D}}

python - 为什么 keras2onnx.convert_keras() 函数不断出现错误 "' KerasTensor 的对象没有属性 'graph'“

javascript - 在 tensorflow.js 中设置权重的函数初始值设定项

node.js - 如何使用 Electron 运行tensorflow.js?

node.js - BodyPix - 在 node.js 中运行 toMask() 和 toColoredPartMask() 会引发错误 : ImageData is not defined

node.js - ExpressJS 不在浏览器上渲染计算出的 html 字符串

node.js - 尝试使用 node.js 和 mysql 连接到每个用户的不同数据库 - 最好使用池

ios - Keras和CoreML的预测结果非常不同