javascript - tensorflow 和 keras 在同一模型和张量上的不同结果

标签 javascript python tensorflow keras tensorflowjs-converter

我按照 https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html 的示例在一些图像上训练了 CNN 模型.我的模型代码是相同的,我只是在另一个图像数据集上训练它:也用于两个类之间的分类。

结果是您对训练集的预期:图像被正确分类为 0 或 1。

我按照 https://js.tensorflow.org/tutorials/import-keras.html 的“备选方案:使用 Python API 直接导出为 TF.js 层格式”部分以 te​​nsorflowjs 友好的格式保存了模型。

但是,当我尝试使用 javascript 访问 html 页面中的结果时,几乎每张图像(或接近它的图像)都得到 1:即使图像在 Keras 中给出 0。

我什至将图像保存为 JSON 中的张量,我在 Keras 中得到 0,在 TensorflowJS 中得到 1。这是错误还是我在某处犯了错误?

这是我在 TensorflowJS 中访问 json 的代码:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.0"></script>

    <script>
      // https://stackoverflow.com/a/18324384/2730032
      function callAjax(url, callback){
        var xmlhttp;
        // compatible with IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                callback(xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
      }

      tf.loadModel('/model.json').then(model => {
        callAjax('/tensor.json', res => {
          arr = JSON.parse(res);
          const example = tf.tensor(arr).reshape([1, 150, 150, 3]);
          const prediction = model.predict(example);
          prediction.data().then(res => {
            console.log('PREDICTION JS', res[0]);
          })
        });
      })
    </script>
  </head>
  <body>
  </body>
</html>

这是我的 python 代码:

import json
import numpy as np
import tensorflowjs as tfjs

model = tfjs.converters.load_keras_model('model.json')

with open('tensor.json', 'r') as f:
    r = json.load(f)
arr = np.array([np.array([np.array(v) for v in l]) for l in r])
print('PREDICTION PYTHON', model.predict(arr[np.newaxis,...])[0][0])

对于完全相同的数据和相同的模型,我得到了 PREDICTION JS 1 和 PREDICTION PYTHON 0.0:有人在我的代码中看到任何问题吗?

EDIT1:我在 Xubuntu 18.04.1 LTS 上并使用以下软件版本:

Python 3.6.6
Keras 2.2.4
tensorflow 1.11.0
tensorflowjs 0.6.2
numpy 1.15.2

EDIT2: 我打开了以下问题 https://github.com/tensorflow/tfjs/issues/776并且此问题已得到修复。

最佳答案

升级到最新版本的 tfjs(目前是 0.13.3)解决了这个问题。 可以查看问题的更多上下文 herethere

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.3"></script>

关于javascript - tensorflow 和 keras 在同一模型和张量上的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52683723/

相关文章:

javascript - 如何使用 NodeJS 在本地测试 AWS Lambda 处理程序?

javascript - 原型(prototype)的目的是什么?

javascript - Ghost.py 0.2.3 超时错误 : Unable to load requested page

python - 最好先按 keys_only=True 然后按 get_multi 查询还是只按完整查询?

python - destroy() tkinter toplevel from queue 默默地失败(竞争条件?)

javascript - React Router 4、LINK 和 ROUTE 在不同的布局上

python - 子类构造函数调用错误的 __init__

用于读取稀疏数据的 TensorFlow 输入函数(采用 libsvm 格式)

python - 在 tensorflow 中与许多输入数据文件很好地混合

tensorflow - Layer.add_loss() 的目的和直觉是什么?它是如何工作的?它是否只计算当前层的损失?