在相同模型和张量上的tensorflowjs和keras的不同结果 [英] Different results for tensorflowjs and keras on same model and tensor

查看:85
本文介绍了在相同模型和张量上的tensorflowjs和keras的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I trained a CNN model on some images following the example of https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html. My model code is identical, I just trained it on another image dataset: also for classification between two classes.

结果是您在训练集上所期望的:图像正确分类为0或1.

The results are what you'd expect on the training set: images are classified correctly either 0 or 1.

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

However when I try to access the results in an html page with javascript I get 1 for pretty much every image (or close to it): even if the image gives 0 in Keras.

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

I have even saved an image as a tensor in JSON and I get 0 in Keras and 1 in TensorflowJS. Is this a bug or have I made a mistake somewhere ?

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

Here is my code in TensorflowJS to access the 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代码:

And here is my python code for the same:

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:有人在我的代码中看到任何问题吗?

I get PREDICTION JS 1 and PREDICTION PYTHON 0.0, for exactly the same data and the same model: does anybody see any issue in my code ?

EDIT1 :我使用的是Xubuntu 18.04.1 LTS,并使用以下软件版本:

I'm on Xubuntu 18.04.1 LTS and use the following software versions:

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 ,此问题已修复.

I opened the following issue https://github.com/tensorflow/tfjs/issues/776 and it has since been fixed.

推荐答案

升级到tfjs的最新版本(当前为0.13.3)可以解决此问题. 可以在此处

Upgrading to the latest version of tfjs (currently 0.13.3) solves the issue. More context to the question can be viewed here and there

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

这篇关于在相同模型和张量上的tensorflowjs和keras的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆