我在 tf.js 的 GitHub 上同样提出了 issue,模型文件有点大,就不在论坛上上传了,可以从这里获取
opened 01:34AM - 12 Oct 18 UTC
closed 01:53AM - 23 Oct 18 UTC
comp:layers
To get help from the community, check out our [Google group](https://groups.goog… le.com/a/tensorflow.org/forum/#!forum/tfjs).
#### TensorFlow.js version
TensorFlow version: 1.10.1
tensorflowjs_converters version: 0.6.1
TensorFlow.js version: script(src='https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest')
#### Browser version
Chrome 69.0.3497.100
#### Describe the problem or feature request
I trained a Keras Sequential model in python, and transofrm it to tf.js model by using tensorflowjs_converters. But I found the infer result are completely wrong in python and tf.js. There is the infer code.
## Python infer
```
from keras.models import load_model
model = load_model('keras.h5')
test = np.genfromtxt('voice.txt', delimiter=',', dtype=np.float32)
test = test.reshape(1, 100, 1024, 1)
test -= 128
test /= 128.0
pred = model.predict(test)
print(pred)
```
## tf.js infer
```
async function start() {
model = await tf.loadModel('model2/model.json')
model.summary()
const pred = model.predict(tf.zeros([1, 100, 1024, 1]))
}
start()
let input_tensor = tf.tensor(values=intArray);
var tensorValue = input_tensor.dataSync()
for(var i = 0; i < intArray; i++){
if(intArray != tensorValue){
console.log('Wrong!')
}
}
const reshaped_tensor = input_tensor.reshape([1, 100, 1024, 1]).toFloat();
const divided_tensor = reshaped_tensor.sub(tf.scalar(128)).div(tf.scalar(128))
divided_tensor.print(true)
var pred = model.predict(divided_tensor).dataSync()
console.log(pred)
```
var "intArray" are the input data and it's shape is [100.1024], dtype is int32(I transformed it to float32 in data process)
And there is the model:
![image](https://user-images.githubusercontent.com/11308780/46842873-c549ba80-ce02-11e8-80de-8a37d1efbba4.png)
I use the same input data, and here are the infer result
## Python infer
![image](https://user-images.githubusercontent.com/11308780/46842722-cf1eee00-ce01-11e8-9b96-39bcb4e55e7d.png)
## tf.js infer
![image](https://user-images.githubusercontent.com/11308780/46842730-da721980-ce01-11e8-8663-3582454606f1.png)
### Models
The model files are more than 10MB, so I use SFX. If you want to use it, just change the name, such as
“model2.002.zip” to "model2.zip.002"
### tf.js model
[model2.002.zip](https://github.com/tensorflow/tfjs/files/2471257/model2.002.zip)
[model2.003.zip](https://github.com/tensorflow/tfjs/files/2471258/model2.003.zip)
[model2.004.zip](https://github.com/tensorflow/tfjs/files/2471259/model2.004.zip)
[model2.005.zip](https://github.com/tensorflow/tfjs/files/2471260/model2.005.zip)
[model2.001.zip](https://github.com/tensorflow/tfjs/files/2471261/model2.001.zip)
### keras model
[keras.001.zip](https://github.com/tensorflow/tfjs/files/2471274/keras.001.zip)
[keras.002.zip](https://github.com/tensorflow/tfjs/files/2471275/keras.002.zip)
[keras.003.zip](https://github.com/tensorflow/tfjs/files/2471271/keras.003.zip)
[keras.004.zip](https://github.com/tensorflow/tfjs/files/2471272/keras.004.zip)
[keras.005.zip](https://github.com/tensorflow/tfjs/files/2471273/keras.005.zip)
### input file
[voice.txt](https://github.com/tensorflow/tfjs/files/2471280/voice.txt)
#### Code to reproduce the bug / link to feature request
N/A
我在 python 中训练了 Keras Sequential 模型,并使用 tensorflowjs_converters 将其转换为 tf.js 模型。但我发现 python 和 tf.js 中的推断结果是完全错误的。
Python infer
from keras.models import load_model
model = load_model ('keras.h5')
test = np.genfromtxt ('voice.txt', delimiter=',', dtype=np.float32)
test = test.reshape (1, 100, 1024, 1)
test -= 128
test /= 128.0
pred = model.predict (test)
print (pred)
tf.js infer
async function start () {
model = await tf.loadModel ('model2/model.json')
model.summary ()
const pred = model.predict (tf.zeros ([1, 100, 1024, 1]))
}
start ()
let input_tensor = tf.tensor (values=intArray);
var tensorValue = input_tensor.dataSync ()
for (var i = 0; i < intArray; i++){
if (intArray != tensorValue){
console.log ('Wrong!')
}
}
const reshaped_tensor = input_tensor.reshape ([1, 100, 1024, 1]).toFloat ();
const divided_tensor = reshaped_tensor.sub (tf.scalar (128)).div (tf.scalar (128))
divided_tensor.print (true)
var pred = model.predict (divided_tensor).dataSync ()
console.log (pred)
var “intArray” 是输入数据,它的形状是 [100.1024],dtype 是 int32(我在数据处理中将它转换为 float32)
我使用了相同的输入数据,这是推断结果:
提问人:AngelZheng,发表在 2018-10-12 10:05:35
建议每一层跑跑 拿 weights 看输出 具体定位问题
另外看一下输入输出的预处理
舟 3332 发表于 2018-10-15 18:25:46