tensorflowjs 和 keras 在相同模型和张量上的不同结果

Ixi*_*xio 5 javascript python keras tensorflow tensorflowjs-converter

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

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

我在tensorflowjs友好的格式保存的模型下面的“替代方案:使用Python API出口直接TF.js层格式”的部分 https://js.tensorflow.org/tutorials/import-keras.html

但是,当我尝试使用 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>
      // /sf/answers/1282706911/
      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>
Run Code Online (Sandbox Code Playgroud)

这是我的python代码:

<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>
Run Code Online (Sandbox Code Playgroud)

对于完全相同的数据和相同的模型,我得到了 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
Run Code Online (Sandbox Code Playgroud)

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

edk*_*ked 3

升级到最新版本的 tfjs(当前为 0.13.3)可以解决该问题。可以在此处那里查看该问题的更多背景信息

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.3"></script>
Run Code Online (Sandbox Code Playgroud)