问题描述
我正在尝试在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,dataId: {},id: 264,rankType: '3',scopeId: 311
}
解决方法
昨晚我和你有同样的问题。我想知道为什么它不能用于PNG图像,而JPG图像却能正常工作。事实证明,它需要做一些number of channels的工作,用于从缓冲区解码图像。
当我们使用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
现在接受第二个参数,指示在图像解码期间要使用的通道数。