Different Results on Keras and TensorFlow lite

Viewed 204

I am using TensorFlow lite version for the model evaluation. While running on Keras, I am getting the different results on running TensorFlow lite.

Could you please help me understanding what is wrong in my work.

Code running using Keras:

data_df = pd.read_csv('core3_input_withHeader.csv')

feature_list = ['core3_SQst', 'core3_headPress', 'core3_rodPress']
channels = []
for feature in feature_list:
    channels.append(data_df.loc[:, data_df.columns.str.contains(feature)])
input_array = np.stack([channels[0].values.squeeze(), channels[1].values.squeeze(), channels[2].values.squeeze()], axis=1)
input_array_norm = input_array[np.newaxis,:,:]

autoencoder = keras.models.load_model('models/core3_ac_model.h5')
encoder = keras.models.Model(inputs= autoencoder.input, outputs = autoencoder.get_layer('bottleneck').output)
encoded_feat_full = encoder.predict(input_array_norm)
print(encoded_feat_full)

Results: [[-0.05779254 -0.13797456 0.42000926 -0.16879612 -0.26408887 -0.7427746 0.393916 -0.69853663 0.6064893 0.7713756 -0.26022637 1.2051586 0.5575749 -0.12699515 -0.6362914 -0.1943332 0.13275155 0.10403969 -0.614455 -0.5115924 0.08213274 0.38980693 -0.394821 -0.39467907 0.9681511 0.05354612 -0.4930368 -0.42177224 -0.4795676 -0.21956956 -0.04954213 -0.33019006]]

While running on tensorflow Lite: I already converted my model to tflite version using the following command:

tflite_convert --keras_model_file=/home/superuser/tico_model/core3_ac_model.h5 --output_file=/home/superuser/tico_model/tf_tico_model.tflite

Which given me the model tf_tico_model.tflite

import tensorflow as tf
import tensorflow.keras as keras
print(tf.version.VERSION)
# 2.0.0-rc2

import numpy as np
import pandas as pd
from tflite_runtime.interpreter import Interpreter

data_df = pd.read_csv('core3_input_withHeader.csv')
feature_list = ['core3_SQst', 'core3_headPress', 'core3_rodPress']

channels = []
for feature in feature_list:
    channels.append(data_df.loc[:, data_df.columns.str.contains(feature)])

input_array = np.stack([channels[0].values.squeeze(), channels[1].values.squeeze(), channels[2].values.squeeze()], axis=1)
input_array_norm = input_array[np.newaxis,:,:]
input_data = np.array(input_array_norm, dtype=np.float32)

#run the inference
autoencoder = tf.lite.Interpreter(model_path="./tf_tico_model.tflite")
autoencoder.allocate_tensors()

# at Index:1 the model input and 9 is output
autoencoder.set_tensor(1, input_data)
autoencoder.invoke()
output_data = autoencoder.get_tensor(9)
print(output_data)

INFO: Initialized TensorFlow Lite runtime. [[ 0.76454383 -0.70871294 -0.7592763 0.7502928 -0.75859773 -0.75297236 0.7334 -0.7616406 -0.7565583 0.77855337 -0.74473566 -0.73970836 0.8058165 -0.74315614 -0.7622459 0.79716206 -0.73603505 -0.72557694 0.7875527 -0.7533992 -0.7004597 0.77682453 -0.7581499 -0.6704023 0.7861713 -0.73904926 -0.6299191 0.775358 -0.7538745 -0.5752099 0.7788005 -0.747791 ]]

I am not able to find, what wrong with this?

Could you please help me.

Thanks Kind Regards Arun

0 Answers
Related