繁体   English   中英

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

[英]Different results for tensorflowjs and keras on same model and tensor

我按照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>
      // 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)解决了这个问题。 可以在这里那里查看问题的更多背景

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

Mac 10.15、TF 2.3、Python 3.8、纯 JavaScript TFJS 2.6.0、网络服务器上的类似问题:python3 -m http.server

在大型深度 CNN + RNN Keras 网络上的所有单元上,推理结果始终保持在 0.5 左右。

解决方案:不要使用tensorflowjs_converter进行 .h5 到 TFJS 的转换

tensorflowjs_wizard允许您关闭数值压缩,提供与 Python TF2.3 几乎相同的结果(在我的情况下,最后一层最多 6 位数字)。

暂无
暂无

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

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