当将keras模型加载到tensorjs中时,它变得完全不准确 [英] When keras model is loaded into tensorjs, it becomes completely inaccurate
问题描述
我正在尝试为我在kaggle上找到的keras模型构建图像识别Web应用程序.我是这个东西的完整入门者.这是我第一次从事ML项目.该模型在keras/tensorflow中工作得很好(如果我的术语不准确,请原谅我),但是当我通过tensorjs将模型加载到我的web应用程序中并进行预测时,即使使用训练数据,它也很不准确.我不知道到底发生了什么,但我有一种直觉,那就是它涉及到如何在Web应用程序中处理我的图像.我只是不知道我到底要改变什么.
I am trying to build an image recognition web app for a keras model that i found on kaggle. I'm a complete beginner to this stuff. This is my first time working on a ML project. The model works fairly well in keras/tensorflow (forgive me if my terminologies are inaccurate), but when i load the model into my webapp via tensorjs and make predictions, its hilariously inaccurate, even with training data. I dont know exactly what's going on, but I have a hunch that it involves how my image is being processed in the web app. I just dont know exactly what i have to change.
这是我的processImage代码
This is my processImage code
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中加载测试图像的代码.这是为我的模型格式化图像的方式.
and finally, the code I use to load test images in python. This is how images are formatted for my model.
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代码中添加或删除哪些内容,以便我的图片得到正确处理?
I really just need someone to tell me the discrepancies between the load_image formatting which I use for my model and the processImage code I used in javascript. What do I need to add or remove from the javascript code so that my image will be processed properly?
推荐答案
所应用的预处理在js和python中是不同的.
The preprocessing applied is different in js and python.
在python中
normalized = data / 255
和js
normalized = 1 - (data / 255)
要在js中进行相同的规范化,规范化应为:
To have the same normalization in js, the normalization should be:
const normalized = resized.div(offset)
这篇关于当将keras模型加载到tensorjs中时,它变得完全不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!