tensorflow js prediction 预测过程中的问题
Problem during prediction on tensorflow js prediction
我的 tensorflow js 模型有问题,我听了一个课程 (link to the course)
在那里我学会了创建一个张量流模型并且一切正常但是课程没有展示如何使用该模型所以我自己开发了这部分但是每次我尝试预测一个数字我得到相同的结果(2),我不知道不知道为什么,我也没有解决这个问题的知识,所以我希望有人能帮我解决这个问题并提供解释。
代码的来宾部分在这里:
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);
项目的 GitHub 在这里:github
提前致谢
图像只有在完成加载后才应该被预测
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);
}
另外,初始的canvas背景是透明的,因为没有设置。当转换为张量时,透明背景变为黑色。笔触样式也是黑色的。黑底黑图。。。不管画什么,都是一样的tensor。
要么更改了笔触样式,要么填充了 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;
// 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");
}
我的 tensorflow js 模型有问题,我听了一个课程 (link to the course) 在那里我学会了创建一个张量流模型并且一切正常但是课程没有展示如何使用该模型所以我自己开发了这部分但是每次我尝试预测一个数字我得到相同的结果(2),我不知道不知道为什么,我也没有解决这个问题的知识,所以我希望有人能帮我解决这个问题并提供解释。
代码的来宾部分在这里:
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);
项目的 GitHub 在这里:github
提前致谢
图像只有在完成加载后才应该被预测
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);
}
另外,初始的canvas背景是透明的,因为没有设置。当转换为张量时,透明背景变为黑色。笔触样式也是黑色的。黑底黑图。。。不管画什么,都是一样的tensor。
要么更改了笔触样式,要么填充了 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;
// 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");
}