Hi Omer,
Thanks for the prompt reply.
I will attach the files, here is the main.py -
main.py
import tensorflow as tf
import pandas as pd
import numpy as np
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from hailo_sdk_client import ClientRunner
Path
save_path = “./model”
Hailo Hardware Architecture
chosen_hw_arch = “hailo8”
def load_data():
melb_data = ‘melb_data.csv’
base_frame = pd.read_csv(melb_data)
df = base_frame[['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude', 'Price']].copy()
df = df.dropna()
return df
def split_data(data):
x = data[[‘Rooms’, ‘Bathroom’, ‘Landsize’, ‘Lattitude’, ‘Longtitude’]].copy()
y = data[‘Price’].copy()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
return x_train, x_test, y_train, y_test
def create_model(normalizer, adam_optimizer):
model = tf.keras.Sequential([
normalizer,
tf.keras.layers.Dense(64, activation=‘relu’),
tf.keras.layers.Dense(128, activation=‘relu’),
tf.keras.layers.Dense(32, activation=‘relu’),
tf.keras.layers.Dense(1)
])
model.compile(loss=‘mean_squared_error’, optimizer=adam_optimizer)
return model
def reset_weights(model):
for layer in model.layers:
if hasattr(layer, ‘kernel_initializer’) and hasattr(layer, ‘bias_initializer’):
# Reinitialize weights and biases
layer.kernel.assign(layer.kernel_initializer(tf.shape(layer.kernel)))
layer.bias.assign(layer.bias_initializer(tf.shape(layer.bias)))
def main():
# Load the data from csv
data = load_data()
# Split the Data into Training and Testing
x_train, x_test, y_train, y_test = split_data(data)
# Normalize the Data and Create Optimizer
normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(tf.convert_to_tensor(x_train))
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
# Create the Model
model = create_model(normalizer, optimizer)
# Fit the Model
model.fit(x_train, y_train, epochs=10)
# Predict Model and Evaluate Results
y_pred = model.predict(x_test)
print(r2_score(y_test, y_pred))
# Summary
print("------------------")
model.summary()
print("------------------")
# Save the Model
model.save(save_path+"/saved_model.keras")
# Converting the Model to tflite
model_name = "action_forecast"
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
tf.lite.OpsSet.SELECT_TF_OPS, # enable TensorFlow ops.
]
tflite_model = converter.convert() # may cause warnings in jupyter notebook, don't worry.
tflite_model_path = "./model/action_forecast.tflite"
with tf.io.gfile.GFile(tflite_model_path, "wb") as f:
f.write(tflite_model)
# Parsing the model to Hailo format
runner = ClientRunner(hw_arch=chosen_hw_arch)
hn, npz = runner.translate_tf_model(tflite_model_path, model_name)
# Reset the weights of the model
# reset_weights(model)
if name == “main”:
main()