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?