隐式形状不能是小数

The implicit shape can't be a fractional number

我正在尝试在 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 图像却可以正常工作。事实证明,它与从缓冲区解码图像时使用的有关。

当我们使用 decodeImage 函数 (reference) 时,它为 JPG 使用 3 个通道,为 PNG 使用 4 个通道。但是,该模型需要 3 个通道。解码图像后,我们可以明确地告诉 decodeImage 对要解码的所有类型的图像使用 3 个通道。

修改后的代码如下所示:

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 现在接受第二个参数,指示图像解码期间要使用的通道数。