英文:
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('imageResult')
img.onload = function(){
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);
}
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('canvas'),
ctx = canvas.getContext('2d');
// set its dimension to target size
canvas.width = width;
canvas.height = height;
// canvas with white background
ctx.fillStyle = 'white';
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("image/png");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论