Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当keras模型加载到tensorjs中时,它变得完全不准确_Python_Tensorflow_Keras_Tensorflow.js - Fatal编程技术网

Python 当keras模型加载到tensorjs中时,它变得完全不准确

Python 当keras模型加载到tensorjs中时,它变得完全不准确,python,tensorflow,keras,tensorflow.js,Python,Tensorflow,Keras,Tensorflow.js,我正在尝试为我在kaggle上找到的keras模型构建一个图像识别web应用程序。我对这东西完全是个初学者。这是我第一次从事ML项目。该模型在keras/tensorflow中运行得相当好(如果我的术语不准确,请原谅我),但是当我通过tensorjs将该模型加载到我的webapp中并进行预测时,即使有训练数据,它也非常不准确。我不知道到底发生了什么,但我有一种预感,它涉及到我的图像是如何在网络应用程序中处理的。我只是不知道我到底要改变什么 这是我的processImage代码 function

我正在尝试为我在kaggle上找到的keras模型构建一个图像识别web应用程序。我对这东西完全是个初学者。这是我第一次从事ML项目。该模型在keras/tensorflow中运行得相当好(如果我的术语不准确,请原谅我),但是当我通过tensorjs将该模型加载到我的webapp中并进行预测时,即使有训练数据,它也非常不准确。我不知道到底发生了什么,但我有一种预感,它涉及到我的图像是如何在网络应用程序中处理的。我只是不知道我到底要改变什么

这是我的processImage代码

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_图像格式和我在javascript中使用的processImage代码之间的差异。我需要从javascript代码中添加或删除什么才能正确处理我的图像?

在js和python中应用的预处理是不同的

用python

normalized = data / 255
还有js

normalized = 1 - (data / 255)
要在js中具有相同的规范化,规范化应为:

const normalized = resized.div(offset)

您可以尝试更改
const normalized=tf.scalar(1.0).sub(resized.div(offset))
to
const normalized=resized.div(255.0)。看起来你的图像处理在python和js之间是不同的!太谢谢你了,我真的为这件事伤了脑筋。成功了!