Tensorflow lite model is giving wrong output

好久不见. 提交于 2019-12-07 22:19:22

问题


I am developing a deep learning model with regression predicts. I created a tflite model but its predictions are different from original model and they are fully wrong.. Here is my process:

I trained my model with keras

model = Sequential()
model.add(Dense(100, input_dim=x.shape[1], activation='relu')) # Hidden 1
model.add(Dense(50, activation='relu')) # Hidden 2
model.add(Dense(1)) # Output
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x,y,verbose=0,epochs=500)

And saved my model as h5 file

model.save("keras_model.h5")

Then converted h5 file to tflile format by TocoConverter

converter = tf.contrib.lite.TocoConverter.from_keras_model_file("keras_model.h5")
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

When i test both files with same input original keras model gives reasonable output, but converted model gives unreasonable output.

# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(input_data)
print(output_data)

//Original model testing
from keras.models import load_model
model2 = load_model("keras_model.h5")
pred = model2.predict(x)
print(pred)

Output is like this:

[[10. 10. 10. 10. 10. 10.]]//input_data
[[-1.4308803]]// tflite output (meaningless)
[[335.0276]] // keras file output

Why this problem occur?


回答1:


Finally i found a solution by converting keras model to frozen graph with this code snippet. I copied this python file tensorflow Scripts folder. And copy keras model file to same folder. And create a folder called "frozen". Then run this command

py cerasconvert.py keras_model.h5 frozen/ freeze_graph

I converted newly created .pb file to tflite format

import tensorflow as tf
import numpy as np

graph_def_file = "frozen/frozen.pb"
input_arrays = ["dense_1_input_1"]
output_arrays = ["dense_3_1/BiasAdd"]

converter = tf.contrib.lite.TocoConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("frozen/converted.tflite", "wb").write(tflite_model)

now my tflite model prediction accuracy is very high.



来源:https://stackoverflow.com/questions/51557691/tensorflow-lite-model-is-giving-wrong-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!