简体   繁体   English

tensorflow.js模型无法正确预测

[英]tensorflow.js model is not predicting correctly

hello I'm new to this topic of ml. 你好,我是毫升这个话题的新手。 I trained a model in python which classifies the images in 2 classes as 'santa' and 'not santa' and it is predicting correctly. 我使用python训练了一个模型,该模型将图像分为2类,分别为“圣诞老人”和“非圣诞老人”,并且可以正确预测。 I converted this model to tensorflow.js as I need to use it in my website for classifying uploaded images but it is not classifying images correctly as in the python model. 我将此模型转换为tensorflow.js,因为我需要在我的网站中使用它来对上传的图像进行分类,但是它无法像python模型中那样对图像进行正确分类。 model.predict() returns 2 probabilities which of one is higher returns that class. model.predict()返回2个概率,其中一个较高者返回该类。 I feel the problem lies in the preprocessing part in the javascript for testing from the model. 我觉得问题出在javascript的预处理部分,用于从模型进行测试。 I have attached the code below. 我已经附上了下面的代码。

below is the code snippet for testing in python 以下是在python中进行测试的代码段

image = cv2.imread(args["image"])
orig = image.copy()
image = cv2.resize(image, (28, 28))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
print("[INFO] loading network...")
model = load_model(args["model"])
x= model.predict(image)
print(x)

below is javascript code for testing 以下是用于测试的javascript代码

function preprocess(img)
{


let tensor = tf.browser.fromPixels(img)

const resized = tf.image.resizeBilinear(tensor, [28, 28]).toFloat()

const offset = tf.scalar(255.0);
const normalized = tf.scalar(1.0).sub(resized.div(offset));

const batched = normalized.expandDims(0)
return batched

}

function predict(imgData) {

    var class_names = ['santa','not santa']

    var pred = model.predict(preprocess(imgData)).dataSync()
    console.log(pred)            
    const idx = tf.argMax(pred);



    var indices = findIndicesOfMax(pred, 1)
    console.log(indices)
    var probs = findTopValues(pred, 1)
    var names = getClassNames(indices) 


    document.getElementById("Result").innerHTML = names

    console.log(names);

    console.log(document.getElementById("Result"));

  }

please help how can I solve this problem. 请帮助我如何解决这个问题。 for a sample images the python model returns the value as follows [[0.9940202 0.00597982]] [python output] 1 对于样本图像,python模型返回的值如下[[0.9940202 0.00597982]] [python输出] 1

and for the same image tensorflowjs model returns value as follows Float32Array [ 0.24975205957889557, 0.7502480149269104 ] [tensorflowjs] 2 对于同一张图像tensorflowjs模型,其返回值如下所示Float32Array [0.24975205957889557,0.7502480149269104] [tensorflowjs] 2

There is a slight difference in the way the image is preprocessed in Python and in JavaScript. 在Python和JavaScript中对图像进行预处理的方式略有不同。 With this line 用这条线

image = image.astype("float") / 255.0 

You're only dividing the image pixel values by 255. A common processing is to substract 127 before the division operation: 您仅将图像像素值除以255。通常的处理是在除法运算之前减去127:

image = (image.astype("float") -127) / 127 

In js 在js中

const normalized  = resized.sub(offset).div(offset));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM