0% found this document useful (0 votes)
8 views

Annex e Gui

Uploaded by

Moti Ram giri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Annex e Gui

Uploaded by

Moti Ram giri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

APPENDIX E: SAMPLE CODES FOR GUI WITH

VISUZILATION

import tkinter as tk

from tkinter import messagebox

import numpy as np

import joblib

from tensorflow.keras.models import load_model

import os

import tensorflow_addons as tfa

from tensorflow.keras.utils import custom_object_scope

# Directory containing all model files

model_directory = r'F:\Thesis at Byas\Best Files for Prediction\OBC Model'

# Function to load Keras models

def load_keras_model(model_path):

return load_model(model_path)

# Function to load joblib models

def load_joblib_model(model_path):

return joblib.load(model_path)

# Initialize dictionaries to hold models and scalers

keras_models = {}

rf_models = {}

svr_models = {}
scalers = {}

'''

# Load Keras models and corresponding scalers

keras_model_files = {

"ANN OBC": ('model_OBC_tanh_Nol_21_NoN_126.h5', 'OBC_scaler.pkl')

for key, (model_file, scaler_file) in keras_model_files.items():

model_path = os.path.join(model_directory, model_file)

scaler_path = os.path.join(model_directory, scaler_file)

keras_models[key] = load_keras_model(model_path)

scalers[key] = load_joblib_model(scaler_path)'''

# Load Keras models and corresponding scalers

keras_model_files = {

"ANN OBC": ('model_OBC_tanh_Nol_24_NoN_127.h5', 'OBC_scaler.pkl')

for key, (model_file, scaler_file) in keras_model_files.items():

model_path = os.path.join(model_directory, model_file)

scaler_path = os.path.join(model_directory, scaler_file)

keras_models[key] = load_keras_model(model_path)

scalers[key] = load_joblib_model(scaler_path)

# Load Random Forest models

rf_model_files = {

"RF OBC": 'best_rf_model_OBC.joblib'


}

for key, file_name in rf_model_files.items():

model_path = os.path.join(model_directory, file_name)

rf_models[key] = load_joblib_model(model_path)

# Load SVR models

svr_model_files = {

"SVM OBC": 'best_svr_model_OBC_257.joblib'

for key, file_name in svr_model_files.items():

model_path = os.path.join(model_directory, file_name)

svr_models[key] = load_joblib_model(model_path)

# GUI creation

root = tk.Tk()

root.title("Marshall OBC Predictor")

# List of feature names

feature_names = [

"Percentage 26/20/19mm Down", "Percentage 16mm Down", "Percentage 13/10/9.5mm


Down",

"Percentage 4.75mm Down", "Specific Gravity of 26/20/19mm Down Aggregate",

"Specific Gravity of 16mm Down Aggregate", "Specific Gravity of 13/10/9.5mm Down


Aggregate",

"Specific Gravity of 4.75mm Down Aggregate", "Specific Gravity of Bitumen",

"Percentage Passing through 26mm Sieve", "Percentage Passing through 19mm Sieve",
"Percentage Passing through 13.2mm Sieve", "Percentage Passing through 9.5mm Sieve",

"Percentage Passing through 4.75mm Sieve", "Percentage Passing through 2.36mm


Sieve",

"Percentage Passing through 1.18mm Sieve", "Percentage Passing through 0.6mm Sieve",

"Percentage Passing through 0.3mm Sieve", "Percentage Passing through 0.15mm Sieve",

"Percentage Passing through 0.075mm Sieve"

# Create input fields dynamically

entries = []

for i, feature in enumerate(feature_names):

tk.Label(root, text=f"{feature}:").grid(row=i, column=0)

entry = tk.Entry(root)

entry.grid(row=i, column=1)

entries.append(entry)

# Function to handle prediction

def predict():

try:

# Read input values from the entries and standardize them

features = [entry.get() for entry in entries]

# Convert to float and check for non-numeric entries

numeric_features = []

non_numeric_indices = []

for i, value in enumerate(features):


try:

numeric_value = float(value)

numeric_features.append(numeric_value)

except ValueError:

non_numeric_indices.append(i)

# Highlight non-numeric entries in red

for index in non_numeric_indices:

entries[index].config(bg="red")

if non_numeric_indices:

messagebox.showerror("Input Error", "Please enter valid numbers for all features.")

return

# Convert to numpy array and reshape

features_array = np.array(numeric_features).reshape(1, -1)

# Predictions from each model

predictions = {

'Keras': [],

'RF': [],

'SVR': []

# Keras model predictions with their respective scalers

for key, model in keras_models.items():

if model:

scaler = scalers[key]

scaled_features = scaler.transform(features_array)
keras_prediction = model.predict(scaled_features)

predictions['Keras'].append(f"{key}: {keras_prediction[0][0]:.2f}")

# Random Forest model predictions

for key, model in rf_models.items():

if model:

rf_prediction = model.predict(features_array)

predictions['RF'].append(f"{key}: {rf_prediction[0]:.2f}")

# SVR model predictions

for key, model in svr_models.items():

if model:

svr_prediction = model.predict(features_array)

predictions['SVR'].append(f"{key}: {svr_prediction[0]:.2f}")

# Display predictions

keras_output.delete(1.0, tk.END)

keras_output.insert(tk.END, "\n".join(predictions['Keras']))

rf_output.delete(1.0, tk.END)

rf_output.insert(tk.END, "\n".join(predictions['RF']))

svr_output.delete(1.0, tk.END)

svr_output.insert(tk.END, "\n".join(predictions['SVR']))

except Exception as e:

messagebox.showerror("Prediction Error", f"An error occurred during prediction: {e}")

# Create the Predict button

predict_button = tk.Button(root, text="Predict", command=predict)

predict_button.grid(row=len(feature_names), column=0, columnspan=2)


# Labels to display the prediction results

keras_label = tk.Label(root, text="Keras Predictions:")

keras_label.grid(row=0, column=2, padx=10, sticky='w')

keras_output = tk.Text(root, width=25, height=20)

keras_output.grid(row=1, column=2, rowspan=len(feature_names))

rf_label = tk.Label(root, text="Random Forest Predictions:")

rf_label.grid(row=0, column=3, padx=10, sticky='w')

rf_output = tk.Text(root, width=25, height=20)

rf_output.grid(row=1, column=3, rowspan=len(feature_names))

svr_label = tk.Label(root, text="SVR Predictions:")

svr_label.grid(row=0, column=4, padx=10, sticky='w')

svr_output = tk.Text(root, width=25, height=20)

svr_output.grid(row=1, column=4, rowspan=len(feature_names))

# Run the application

root.mainloop()
GUI Interface for prediction of OBC

Figure 1 GUI Interface for Prediction of OBC based on 20 Independent Variables

You might also like