当keras模型加载到tensorjs中时,它变得完全不准确
When keras model is loaded into tensorjs, it becomes completely inaccurate
我正在尝试为我在 kaggle 上找到的 keras 模型构建一个图像识别网络应用程序。我是这方面的初学者。这是我第一次从事 ML 项目。该模型在 keras/tensorflow 中运行良好(如果我的术语不准确,请原谅我),但是当我通过 tensorjs 将模型加载到我的 webapp 中并进行预测时,它非常不准确,即使使用训练数据也是如此。我不知道到底发生了什么,但我有一种预感,它涉及我的图像在网络应用程序中的处理方式。我只是不知道我必须改变什么。
这是我的流程图片代码
function processImage(image)
{
let tensor = tf.browser.fromPixels(image)
const resized = tf.image.resizeBilinear(tensor, [256, 256]).toFloat()
const offset = tf.scalar(255.0);
const normalized = tf.scalar(1.0).sub(resized.div(offset));
const batched = normalized.expandDims(0);
return batched;
}
async function start()
{
model=await tf.loadLayersModel('http://localhost:8013/pokemonClassifier/model/model.json');
console.log(classNames.length)
console.log($('#custom-text').text());
if(model==undefined)
{
alert('No model present');
}
if($.trim($('#custom-text').text())=='No file chosen, yet.')
{
alert('please load an image before starting model');
}
let image=document.getElementById("preview");
console.log(image);
let tensor=processImage(image);
let predictions= await model.predict(tensor).data();
console.log(predictions);
let results = Array.from(predictions)
.map(function (p, i) {
return {
probability: p,
className: classNames[i]
};
}).sort(function (a, b) {
return b.probability - a.probability;
}).slice(0, 5);
alert(results[0].className);
console.log(results);
}
最后,我用来在 python 中加载测试图像的代码。这就是为我的模型设置图像格式的方式。
def load_image(img_path, show=False):
img = image.load_img(img_path, target_size=(256, 256))
img_tensor = image.img_to_array(img) # (height, width, channels)
img_tensor = np.expand_dims(img_tensor, axis=0) # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
img_tensor /= 255. # imshow expects values in the range [0, 1]
if show:
plt.imshow(img_tensor[0])
plt.axis('off')
plt.show()
return img_tensor
我真的只需要有人告诉我我用于模型的 load_image 格式与我在 javascript 中使用的 processImage 代码之间的差异。我需要在 javascript 代码中添加或删除什么才能正确处理我的图像?
js和python应用的预处理不同。
在python
normalized = data / 255
在 js 中
normalized = 1 - (data / 255)
要在js中有相同的归一化,归一化应该是:
const normalized = resized.div(offset)
我正在尝试为我在 kaggle 上找到的 keras 模型构建一个图像识别网络应用程序。我是这方面的初学者。这是我第一次从事 ML 项目。该模型在 keras/tensorflow 中运行良好(如果我的术语不准确,请原谅我),但是当我通过 tensorjs 将模型加载到我的 webapp 中并进行预测时,它非常不准确,即使使用训练数据也是如此。我不知道到底发生了什么,但我有一种预感,它涉及我的图像在网络应用程序中的处理方式。我只是不知道我必须改变什么。
这是我的流程图片代码
function processImage(image)
{
let tensor = tf.browser.fromPixels(image)
const resized = tf.image.resizeBilinear(tensor, [256, 256]).toFloat()
const offset = tf.scalar(255.0);
const normalized = tf.scalar(1.0).sub(resized.div(offset));
const batched = normalized.expandDims(0);
return batched;
}
async function start()
{
model=await tf.loadLayersModel('http://localhost:8013/pokemonClassifier/model/model.json');
console.log(classNames.length)
console.log($('#custom-text').text());
if(model==undefined)
{
alert('No model present');
}
if($.trim($('#custom-text').text())=='No file chosen, yet.')
{
alert('please load an image before starting model');
}
let image=document.getElementById("preview");
console.log(image);
let tensor=processImage(image);
let predictions= await model.predict(tensor).data();
console.log(predictions);
let results = Array.from(predictions)
.map(function (p, i) {
return {
probability: p,
className: classNames[i]
};
}).sort(function (a, b) {
return b.probability - a.probability;
}).slice(0, 5);
alert(results[0].className);
console.log(results);
}
最后,我用来在 python 中加载测试图像的代码。这就是为我的模型设置图像格式的方式。
def load_image(img_path, show=False):
img = image.load_img(img_path, target_size=(256, 256))
img_tensor = image.img_to_array(img) # (height, width, channels)
img_tensor = np.expand_dims(img_tensor, axis=0) # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
img_tensor /= 255. # imshow expects values in the range [0, 1]
if show:
plt.imshow(img_tensor[0])
plt.axis('off')
plt.show()
return img_tensor
我真的只需要有人告诉我我用于模型的 load_image 格式与我在 javascript 中使用的 processImage 代码之间的差异。我需要在 javascript 代码中添加或删除什么才能正确处理我的图像?
js和python应用的预处理不同。
在python
normalized = data / 255
在 js 中
normalized = 1 - (data / 255)
要在js中有相同的归一化,归一化应该是:
const normalized = resized.div(offset)