Plz anybody To check if your TensorFlow Lite model is working correctly and making predictions

import seaborn as sns
import matplotlib.pyplot as plt
df=pd.read_csv(“DataSet.csv”)
df

Load your dataset

df = pd.read_csv(‘DataSet.csv’)

Pivot the DataFrame

sensor_data = df.pivot_table(index=‘timestamp’, columns=‘sensor_name’, values=[‘X’, ‘Y’, ‘Z’])

Flatten the multi-level column index

sensor_data.columns = [f’{col[1]}({col[0]})’ for col in sensor_data.columns]

Reset index to make timestamp a regular column

sensor_data.reset_index(inplace=True)

Save the formatted DataFrame to a new CSV file

newdf = sensor_data.to_csv(r’C:\Users\HAWLETT_PACKARD\OneDrive\Desktop\Final Year Project/NewDataSet.csv’, index=False)
newdf=pd.read_csv(“NewDataSet.csv”)
newdf
newdf.head()
newdf.tail()
newdf.info()
newdf.describe()
newdf.dtypes
newdf.isnull().sum()
newdf.fillna(0, inplace=True)
newdf.isnull().sum()
newdf
newdf.drop(“timestamp”, axis=1, inplace=True, errors=‘ignore’)
newdf
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
#newDf = scaler.fit_transform(df)
X_train_scaled = scaler.fit_transform(newdf)
X_test_scaled = scaler.transform(newdf)

Assuming newDf is a NumPy array

X_train_scaled = pd.DataFrame(X_train_scaled)
newdf.describe()
X_train_scaled.describe()
import matplotlib as mpl
import tensorflow as tf
from tensorflow.keras.models import Model
from sklearn.model_selection import train_test_split
#from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense

Split the data into training and testing sets

Adjust the test_size parameter as needed

X_train, X_test = train_test_split(X_train_scaled, test_size=0.2, random_state=42)

Define the architecture of the autoencoder

input_dim = X_train_scaled.shape[1] # Number of features
#Conventional Layer
#Rectified Linear Unit

Encoder

input_layer = Input(shape=(input_dim,))
encoder_layer = Dense(64, activation=‘relu’)(input_layer)
encoded_layer = Dense(32, activation=‘relu’)(encoder_layer)
encoded_layer = Dense(16, activation=‘relu’)(encoder_layer)

Decoder

decoder_layer = Dense(64, activation=‘relu’)(encoded_layer)
decoder_layer = Dense(32, activation=‘relu’)(encoded_layer) # for reducing the dimensionality
decoder_layer = Dense(16, activation=‘relu’)(encoded_layer)
decoded_layer = Dense(input_dim, activation=‘linear’)(decoder_layer) #produce the same output input->processing->output(same as output)

Create autoencoder model

autoencoder = Model(input_layer, decoded_layer)
autoencoder.compile(optimizer=‘adam’, loss=‘mse’) # mse=>mean_squared_error

Display the shapes of the resulting sets

print(“X_train shape:”, X_train.shape)
print(“X_test shape:”, X_test.shape)

Display the model summary to ensure the input shape is correct

autoencoder.summary()

Now, you can fit the model using the provided code

autoencoder.fit(X_train_scaled, X_train_scaled, epochs=50, batch_size=32, shuffle=True, validation_data=(X_test_scaled, X_test_scaled))
‘’‘’
X_train_scaled=>Training data
X_train_scaled=> Labels (autoencoder is trained to reconstruct input)
epochs=>Number of training epoch
batch_size=>Size of each mini-batch
shuffle=>Shuffle the training data at the beginning of each epoch
‘’’

Reconstruct the data

X_train_reconstructed = autoencoder.predict(X_train_scaled)
X_test_reconstructed = autoencoder.predict(X_test_scaled)

Calculate the reconstruction error

mse_train = np.mean(np.square(X_train_scaled - X_train_reconstructed), axis=1)
mse_test = np.mean(np.square(X_test_scaled - X_test_reconstructed), axis=1)

Set a threshold for anomaly detection

threshold = np.mean(mse_train) + 3 * np.std(mse_train)
threshold
np.mean(mse_train)

Identify anomalies

#anomalies_train = X_train_scaled[mse_train > threshold]
anomalies_test = X_test_scaled[mse_test > threshold]

Convert the model to TensorFlow Lite

converter = tf.lite.TFLiteConverter.from_keras_model(autoencoder)
tflite_model = converter.convert()

Save the TensorFlow Lite model to a file

with open(r"C:\Users\HAWLETT_PACKARD\OneDrive\Desktop\Final Year Project\tflite_model.tflite", “wb”) as f:
f.write(tflite_model)
import os

Specify the path to the TensorFlow Lite model

tflite_model_path = r"C:\Users\HAWLETT_PACKARD\OneDrive\Desktop\Final Year Project\tflite_model.tflite" # Replace with your actual path

Check if the file exists

if os.path.exists(tflite_model_path):
print(f"TensorFlow Lite model successfully saved at: {tflite_model_path}")
else:
print(“There was an issue saving the TensorFlow Lite model.”)

Hi @Muhammad_Nouman, I have tried to train a model and convert the model to tflite and tried to make predictions with the tflite model and got the expected result. Please refer to this gist.

Also let us know what is the exact issue you are facing. Thank You.