我正在尝试在 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/