在TensorFlow.js预测过程中出现问题。

huangapple go评论78阅读模式
英文:

Problem during prediction on tensorflow js prediction

问题

以下是您提供的内容的中文翻译:

我在我的TensorFlow.js模型上遇到了问题,我跟随了一个课程(课程链接)学习如何创建TensorFlow模型,一切都运行正常,但是该课程没有展示如何使用模型,所以我自己开发了这部分内容,但每次尝试预测一个数字时,我都得到相同的结果(2),我不知道为什么,也没有修复它的知识,所以我希望有人能帮助我解决这个问题并提供解释。

代码的关键部分在这里:

    function guessIt(){
  let inputTensor = tf.browser.fromPixels(document.getElementById('imageResult'), 1)// imageResult是一个<img/>标签
    .reshape([1, 28, 28, 1])
    .cast('float32');
  let predictionResult =  modelJson.predict(inputTensor).dataSync();
  let recognizedDigit = predictionResult.indexOf(Math.max(...predictionResult));
  console.log(recognizedDigit);
  console.log(predictionResult);
}

var mousePressed = false;
var lastX, lastY;
var ctx;

//使用离屏画布调整图像大小
function imageToDataUri(img, width, height) {

    // 创建一个离屏画布
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d');

    // 设置其尺寸为目标尺寸
    canvas.width = width;
    canvas.height = height;

    // 将源图像绘制到离屏画布中:
    ctx.drawImage(img, 0, 0, width, height);

    // 使用压缩图像的base64版本对图像进行编码
    return canvas.toDataURL("image/png");
}

function InitThis() {
    ctx = document.getElementById('sheet').getContext("2d");

    $('#sheet').mousedown(function (e) {
        mousePressed = true;
        Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, false);
    });

    $('#sheet').mousemove(function (e) {
        if (mousePressed) {
            Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, true);
        }
    });

    $('#sheet').mouseup(function (e) {
        mousePressed = false;
        let img = imageToDataUri(document.getElementById("sheet"),28,28)//resize it
        let imgElement = document.getElementById("imageResult").setAttribute("src",img);// display it
        guessIt();
    });
	    $('#sheet').mouseleave(function (e) {
        mousePressed = false;
    });
}

function Draw(x, y, isDown) {
    if (isDown) {
        ctx.beginPath();
        ctx.strokeStyle = "000000";
        ctx.lineWidth = 9;
        ctx.lineJoin = "round";
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(x, y);
        ctx.closePath();
        ctx.stroke();
    }
    lastX = x; lastY = y;
}

function clearArea() {
    // 在清除画布时使用身份矩阵
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    document.getElementById("imageResult").setAttribute("src","");
}

// 初始化画布
document.addEventListener('DOMContentLoaded', InitThis);

项目的GitHub链接在这里:GitHub

提前感谢您的帮助。

英文:

I have a problem on my tensorflow js model, I followed a course (link to the course)
where I learned to create a tensorflow model and everything worked fine but the course doesn't show how to use the model so I developped myself this part but every time I try to predict a number I got the same result (2), I don't know why and I don't have the knowledge to fix that so I hope someone could help me fix that and provide an explenation.

The guest part of the code is here :

    function guessIt(){
  let inputTensor = tf.browser.fromPixels(document.getElementById('imageResult'), 1)// imageResult is an <img/> tag
    .reshape([1, 28, 28, 1])
    .cast('float32');
  let predictionResult =  modelJson.predict(inputTensor).dataSync();
  let recognizedDigit = predictionResult.indexOf(Math.max(...predictionResult));
  console.log(recognizedDigit);
  console.log(predictionResult);
}

var mousePressed = false;
var lastX, lastY;
var ctx;

//resize image with off-screen canvas
function imageToDataUri(img, width, height) {

    // create an off-screen canvas
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d');

    // set its dimension to target size
    canvas.width = width;
    canvas.height = height;

    // draw source image into the off-screen canvas:
    ctx.drawImage(img, 0, 0, width, height);

    // encode image to data-uri with base64 version of compressed image
    return canvas.toDataURL("image/png");
}

function InitThis() {
    ctx = document.getElementById('sheet').getContext("2d");

    $('#sheet').mousedown(function (e) {
        mousePressed = true;
        Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, false);
    });

    $('#sheet').mousemove(function (e) {
        if (mousePressed) {
            Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, true);
        }
    });

    $('#sheet').mouseup(function (e) {
        mousePressed = false;
        let img = imageToDataUri(document.getElementById("sheet"),28,28)//resize it
        let imgElement = document.getElementById("imageResult").setAttribute("src",img);// display it
        guessIt();
    });
	    $('#sheet').mouseleave(function (e) {
        mousePressed = false;
    });
}

function Draw(x, y, isDown) {
    if (isDown) {
        ctx.beginPath();
        ctx.strokeStyle = "000000";
        ctx.lineWidth = 9;
        ctx.lineJoin = "round";
        ctx.moveTo(lastX, lastY);
        ctx.lineTo(x, y);
        ctx.closePath();
        ctx.stroke();
    }
    lastX = x; lastY = y;
}

function clearArea() {
    // Use the identity matrix while clearing the canvas
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    document.getElementById("imageResult").setAttribute("src","");
}

// init the cancas
document.addEventListener('DOMContentLoaded', InitThis);

The GitHub of the project is here : github

thanks in advance

答案1

得分: 1

以下是您要翻译的代码部分:

当图像加载完成后才应该进行预测

const img = document.getElementById('imageResult')
img.onload = function(){
    let inputTensor = tf.browser.fromPixels(document.getElementById('imageResult'), 1) // imageResult是一个<img/>标签
        .reshape([1, 28, 28, 1])
        .cast('float32');
    let predictionResult = modelJson.predict(inputTensor).dataSync();
    let recognizedDigit = predictionResult.indexOf(Math.max(...predictionResult));
    console.log(recognizedDigit);
    console.log(predictionResult);
}

此外初始画布背景是透明的因为尚未设置转换为张量时透明背景变为黑色描边样式也是黑色黑色的图像在黑色背景上... 无论绘制什么都会导致相同的张量

要么更改描边样式要么填充画布背景或者两者都更改但两者不应具有相同的颜色原因如上所述)。

function imageToDataUri(img, width, height) {

    // 创建一个离屏画布
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d');
    

    // 将其尺寸设置为目标大小
    canvas.width = width;
    canvas.height = height;
    
    // 画布具有白色背景
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);


    // 将源图像绘制到离屏画布中:
    ctx.drawImage(img, 0, 0, width, height);

    // 使用压缩图像的base64版本将图像编码为数据URI
    return canvas.toDataURL("image/png");
}
英文:

The image should be predicted only when it has completed to load

const img = document.getElementById(&#39;imageResult&#39;)
img.onload = function(){
let inputTensor = tf.browser.fromPixels(document.getElementById(&#39;imageResult&#39;), 1)// imageResult is an &lt;img/&gt; tag
.reshape([1, 28, 28, 1])
.cast(&#39;float32&#39;);
let predictionResult =  modelJson.predict(inputTensor).dataSync();
let recognizedDigit = predictionResult.indexOf(Math.max(...predictionResult));
console.log(recognizedDigit);
console.log(predictionResult);
} 

Additionnaly, the initial canvas background is transparent because it has not been set. When converted to tensor the transparent background becomes black. The stroke style is also black. A black image on a black background... No matter what is drawn, it leads to the same tensor.

Either the strokestyle is changed, or the canvas background is filled or both (but both should not have the same color for the same reason explained above).

function imageToDataUri(img, width, height) {
// create an off-screen canvas
var canvas = document.createElement(&#39;canvas&#39;),
ctx = canvas.getContext(&#39;2d&#39;);
// set its dimension to target size
canvas.width = width;
canvas.height = height;
// canvas with white background
ctx.fillStyle = &#39;white&#39;;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// draw source image into the off-screen canvas:
ctx.drawImage(img, 0, 0, width, height);
// encode image to data-uri with base64 version of compressed image
return canvas.toDataURL(&quot;image/png&quot;);
}

huangapple
  • 本文由 发表于 2020年1月6日 22:36:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定