[英]Different results for tensorflowjs and keras on same model and tensor
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 .我按照https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html的示例在一些图像上训练了 CNN 模型。 My model code is identical, I just trained it on another image dataset: also for classification between two classes.
我的模型代码是相同的,我只是在另一个图像数据集上训练它:也用于两个类之间的分类。
The results are what you'd expect on the training set: images are classified correctly either 0 or 1.结果是您对训练集的期望:图像被正确分类为 0 或 1。
I saved the model in a tensorflowjs-friendly format following the "Alternative: Use the Python API to export directly to TF.js Layers format" section of https://js.tensorflow.org/tutorials/import-keras.html我在tensorflowjs友好的格式保存的模型下面的“替代方案:使用Python API出口直接TF.js层格式”的部分https://js.tensorflow.org/tutorials/import-keras.html
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.但是,当我尝试使用 javascript 访问 html 页面中的结果时,几乎每个图像(或接近它)都会得到 1:即使图像在 Keras 中给出 0。
I have even saved an image as a tensor in JSON and I get 0 in Keras and 1 in TensorflowJS.我什至在 JSON 中将图像保存为张量,在 Keras 中得到 0,在 TensorflowJS 中得到 1。 Is this a bug or have I made a mistake somewhere ?
这是一个错误还是我在某个地方犯了错误?
Here is my code in TensorflowJS to access the json:这是我在 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>
And here is my python code for the same:这是我的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])
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 ?对于完全相同的数据和相同的模型,我得到了 PREDICTION JS 1 和 PREDICTION PYTHON 0.0:有人在我的代码中看到任何问题吗?
EDIT1: I'm on Xubuntu 18.04.1 LTS and use the following software versions: 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: I opened the following issue https://github.com/tensorflow/tfjs/issues/776 and it has since been fixed. EDIT2:我打开了以下问题https://github.com/tensorflow/tfjs/issues/776 ,此后已修复。
Similar issue here on Mac 10.15, TF 2.3, Python 3.8, plain JavaScript TFJS 2.6.0, webserver: python3 -m http.server Mac 10.15、TF 2.3、Python 3.8、纯 JavaScript TFJS 2.6.0、网络服务器上的类似问题:python3 -m http.server
Results of inference would always remain around 0.5 on all units on a large, deep, CNN + RNN Keras network.在大型深度 CNN + RNN Keras 网络上的所有单元上,推理结果始终保持在 0.5 左右。
Solution: Do not use tensorflowjs_converter for .h5 to TFJS conversion解决方案:不要使用tensorflowjs_converter进行 .h5 到 TFJS 的转换
tensorflowjs_wizard lets you switch off numerical compression, providing almost identical results as Python TF2.3 (in my case up to 6 digits on final layer). tensorflowjs_wizard允许您关闭数值压缩,提供与 Python TF2.3 几乎相同的结果(在我的情况下,最后一层最多 6 位数字)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.