Tensor flow keras model returns incorrect values after converting to tensor flow lite.
I am trying to create tensor flow model to run on Android device. First I decided to test it on sample fashion_mnist. Everything worked correctly with standard keras model (expected results were correct for my sample images). But as soon as I converted it to tflite model it.. (and used Interpreter to predict answer) it returns the same answer for all the images.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
print(tf.__version__)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
def prepare_keras_model():
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'), ## warstwa neuronow
tf.keras.layers.Dense(10) # bo 10 rodzajow ubran
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10) # increaase to 10 after tests
# test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
# print('\nTest accuracy:', test_acc)
return tf.keras.Sequential([model, tf.keras.layers.Softmax()])
def calculate_prediction(model, test_data, index):
predictions = model.predict(test_data)
value = predictions[index][np.argmax(predictions[index])]
print("PRED RESULTS " + str(class_names[np.argmax(predictions[index])]) + " VALUE " + str(value))
plt.imshow(test_data[index], cmap=plt.cm.binary)
def load_image_from_disk(img_name):
test_data = []
test_path = "."
IMG_SIZE = 28
test_img = os.path.join(test_path, img_name + ".png")
img_array = cv2.bitwise_not(cv2.imread(test_img, cv2.IMREAD_GRAYSCALE)) # loading the image
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize the image
float_array = (new_array / 255.0).astype(np.float32)
return float_array
def convert_keras_to_tflite(model):
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
return tflite_model
def save_model_to_file(model, file_name):
with open(file_name + '.tflite', 'wb') as f:
f.write(model)
def load_model_from_file(file_name):
with open(file_name + '.tflite', 'rb') as f:
byte_data = f.read()
return byte_data
def run_tflite_pred(model, image):
interpreter = tf.lite.Interpreter(model_content=model)
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
interpreter.set_tensor(input_index, [image])
interpreter.invoke()
result = interpreter.get_tensor(output_index)
best_index = np.argmax(result)
print("test " + class_names[best_index] + " " + str(result[0][best_index]))
# -------------- START --------------
# probability_model = prepare_keras_model()
# tflite_model = convert_keras_to_tflite(probability_model)
tflite_model = load_model_from_file("2")
# calculate_prediction(probability_model, test_images, 1)
image = load_image_from_disk("shoe")
plt.imshow(image)
run_tflite_pred(tflite_model, image)
# save_model_to_file(tflite_model, "2")