Tensorflow.js 预测结果不变
Tensorflow.js prediction result doesn't change
我用 Google Teachable Machines (Image) 训练了我的模型,并将该模型包含到我的 Ionic Angular 应用程序中。我成功加载了模型并使用相机预览来预测相机图像中显示的 class。
canvas 中显示的图片会正确更改,但 predict() 方法 returns 每次调用的结果相同。
import * as tmImage from '@teachablemachine/image';
...
async startPrediction() {
this.model = await tmImage.load(this.modelURL, this.metadataURL);
this.maxPredictions = this.model.getTotalClasses();
console.log('classes: ' + this.maxPredictions); //works properly
requestAnimationFrame(() => {
this.loop();
});
}
async loop() {
const imageAsBase64 = await this.cameraPreview.takeSnapshot({ quality: 60 });
const canvas = document.getElementById('output') as HTMLImageElement;
//image changes properly, I checked it with a canvas output
canvas.src = 'data:image/jpeg;base64,' + imageAsBase64;
const prediction = await this.model.predict(canvas);
for (let i = 0; i < this.maxPredictions; i++) {
const classPrediction =
prediction[i].className + ': ' + prediction[i].probability.toFixed(2);
//probability doesn't change, even if I hold the camera close over a trained image
}
requestAnimationFrame(() => {
this.loop();
});
}
预测结果如:class1 = 0.34,class2 = 0.66但没有变化。
我希望你能帮我找到我的错误,在此先感谢!
在您调用预测模型之前,图像可能尚未加载。已讨论 and
function load(url){
return new Promise((resolve, reject) => {
canvas.src = url
canvas.onload = () => {
resolve(canvas)
}
})
}
await load(base64Data)
// then the image can be used for prediction
我用 Google Teachable Machines (Image) 训练了我的模型,并将该模型包含到我的 Ionic Angular 应用程序中。我成功加载了模型并使用相机预览来预测相机图像中显示的 class。 canvas 中显示的图片会正确更改,但 predict() 方法 returns 每次调用的结果相同。
import * as tmImage from '@teachablemachine/image';
...
async startPrediction() {
this.model = await tmImage.load(this.modelURL, this.metadataURL);
this.maxPredictions = this.model.getTotalClasses();
console.log('classes: ' + this.maxPredictions); //works properly
requestAnimationFrame(() => {
this.loop();
});
}
async loop() {
const imageAsBase64 = await this.cameraPreview.takeSnapshot({ quality: 60 });
const canvas = document.getElementById('output') as HTMLImageElement;
//image changes properly, I checked it with a canvas output
canvas.src = 'data:image/jpeg;base64,' + imageAsBase64;
const prediction = await this.model.predict(canvas);
for (let i = 0; i < this.maxPredictions; i++) {
const classPrediction =
prediction[i].className + ': ' + prediction[i].probability.toFixed(2);
//probability doesn't change, even if I hold the camera close over a trained image
}
requestAnimationFrame(() => {
this.loop();
});
}
预测结果如:class1 = 0.34,class2 = 0.66但没有变化。 我希望你能帮我找到我的错误,在此先感谢!
在您调用预测模型之前,图像可能尚未加载。已讨论
function load(url){
return new Promise((resolve, reject) => {
canvas.src = url
canvas.onload = () => {
resolve(canvas)
}
})
}
await load(base64Data)
// then the image can be used for prediction