Tensorflow.js中predict的输出始终显示第一个示例

Tensorflow.js中predict的输出始终显示第一个示例,tensorflow,tensorflow.js,Tensorflow,Tensorflow.js,我最近在浏览器中学习了一个使用TensorFlow js的图像分类器,我做了一个简单的图像分类,可以识别Giorno和Jotaro的图像,但是当预测一个新图像时,结果总是显示我添加的第一个示例(Jotaro),我试图检查添加第二个示例(Giorno)的第二个函数,它在控制台中运行良好,这是我的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&qu

我最近在浏览器中学习了一个使用TensorFlow js的图像分类器,我做了一个简单的图像分类,可以识别Giorno和Jotaro的图像,但是当预测一个新图像时,结果总是显示我添加的第一个示例(Jotaro),我试图检查添加第二个示例(Giorno)的第二个函数,它在控制台中运行良好,这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Giorno and Jotaro Classifier</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-classifier"></script>
</head>
<body>
<h1>Test Image</h1>
<img id="predict" src="anime/test_giorno.jpg" alt="" width="300">
</body>
</html>
<script>

const initScript = async function(){
const ClassifierKNN = knnClassifier.create();
const mobileneModule = await mobilenet.load();


for(let i=1; i<=3;i++){
    const im = new Image(300,300);
    im.src = 'anime/jotaro/'+i+'.jpg';
    im.onload = ()=>{
        let trainingImageJotaro = tf.browser.fromPixels(im);
        let predJotaro = mobileneModule.infer(trainingImageJotaro,'conv_preds');
        if(i)
        ClassifierKNN.addExample(predJotaro,"Jotaro");
    }
    im.onload();
}

for(let i=1; i<=3;i++){
    const im2 = new Image(300,300);
    im2.src = 'anime/giorno/'+i+'.jpg';
    im2.onload = ()=>{
        let trainingImageGiorno = tf.browser.fromPixels(im2);
        let predGiorno = mobileneModule.infer(trainingImageGiorno,'conv_preds');
        ClassifierKNN.addExample(predGiorno,"Giorno");
    }
    im2.onload();
}


let imgX = document.getElementById('predict');
const tensorX = tf.browser.fromPixels(imgX);
const logitsX = mobileneModule.infer(tensorX,'conv_preds');
let result = await ClassifierKNN.predictClass(logitsX);
console.log('outout:');
console.log(result);

}
initScript();

</script>

吉奥尔诺和佐塔罗分级机
测试图像
const initScript=异步函数(){
const ClassifierKNN=knnsclassifier.create();
const mobileneModule=等待mobilenet.load();
for(设i=1;i{
让trainingImageJotaro=tf.browser.fromPixels(im);
让predJotaro=mobileneModule.infere(trainingImageJotaro,'conv_preds');
如果(i)
addExample(predJotaro,“Jotaro”);
}
im.onload();
}
for(设i=1;i{
让trainingImageGiorno=tf.browser.fromPixels(im2);
让predGiorno=mobileneModule.infere(trainingImageGiorno,'conv_preds');
添加示例(predGiorno,“Giorno”);
}
im2.onload();
}
让imgX=document.getElementById('predict');
常量tensorX=tf.browser.fromPixels(imgX);
const logitsX=mobileneModule.infere(tensorX,'conv_preds');
让结果=等待分类器kn.predictClass(logitsX);
console.log('outout:');
控制台日志(结果);
}
initScript();
脚本的结果总是jotaro,但当我像这样切换循环位置时:

for(let i=1; i<=3;i++){
const im2 = new Image(300,300);
im2.src = 'anime/giorno/'+i+'.jpg';
im2.onload = ()=>{
    let trainingImageGiorno = tf.browser.fromPixels(im2);
    let predGiorno = mobileneModule.infer(trainingImageGiorno,'conv_preds');
    ClassifierKNN.addExample(predGiorno,"Giorno");
}
im2.onload();
}

for(let i=1; i<=3;i++){
    const im = new Image(300,300);
    im.src = 'anime/jotaro/'+i+'.jpg';
    im.onload = ()=>{
        let trainingImageJotaro = tf.browser.fromPixels(im);
        let predJotaro = mobileneModule.infer(trainingImageJotaro,'conv_preds');
        if(i)
        ClassifierKNN.addExample(predJotaro,"Jotaro");
    }
    im.onload();
   }
for(设i=1;i{
让trainingImageGiorno=tf.browser.fromPixels(im2);
让predGiorno=mobileneModule.infere(trainingImageGiorno,'conv_preds');
添加示例(predGiorno,“Giorno”);
}
im2.onload();
}
for(设i=1;i{
让trainingImageJotaro=tf.browser.fromPixels(im);
让predJotaro=mobileneModule.infere(trainingImageJotaro,'conv_preds');
如果(i)
addExample(predJotaro,“Jotaro”);
}
im.onload();
}

结果总是Giorno,有人能帮我解决这个问题吗?

感谢@zer0sign在评论中分享解决方案。为了社区的利益,我在这里提供解决方案(答案部分)


此问题已解决,出现上述问题的原因是,第二个训练数据在测试数据之后执行,因此上面的代码仅获得1个标签,因为第二个标签尚未执行,因此预测图像的输出标签始终显示第一个训练数据的标签。

感谢@zer0sign,谢谢您的支持在评论中列出解决方案。为了社区的利益,我在这里提供解决方案(答案部分)


此问题已修复,出现上述问题的原因是,第二个训练数据在测试数据之后执行,因此上述代码仅获得1个标签,因为第二个标签尚未执行,因此预测图像的输出标签始终显示第一个训练数据的标签。

此问题已修复,出现上述问题的原因是,第二个训练数据是在测试数据之后执行的,因此上面的代码只得到1个标签,因为第二个标签没有执行。因此,预测图像的输出标签始终显示第一个训练数据的标签。此问题已得到解决,问题在出现上述问题的原因是,第二个训练数据是在测试数据之后执行的,因此上述代码只得到1个标签,因为第二个标签没有执行。因此预测图像的输出标签始终显示第一个训练数据的标签