Update app.py
Browse files
app.py
CHANGED
|
@@ -7,82 +7,98 @@ import pandas as pd
|
|
| 7 |
from joblib import load
|
| 8 |
from tensorflow.keras.models import load_model
|
| 9 |
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
| 10 |
import os
|
| 11 |
-
import sklearn
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
print(f"Scikit-learn version: {sklearn.__version__}")
|
| 17 |
-
print(f"Joblib version: {joblib.__version__}")
|
| 18 |
-
print(f"TensorFlow version: {tf.__version__}")
|
| 19 |
-
print(f"Pandas version: {pd.__version__}")
|
| 20 |
-
|
| 21 |
-
# Directory paths for the saved models
|
| 22 |
-
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
-
scaler_path = os.path.join(script_dir, 'toolkit', 'scaler_X.json')
|
| 24 |
-
rf_model_path = os.path.join(script_dir, 'toolkit', 'rf_model.joblib')
|
| 25 |
-
mlp_model_path = os.path.join(script_dir, 'toolkit', 'mlp_model.keras')
|
| 26 |
-
meta_model_path = os.path.join(script_dir, 'toolkit', 'meta_model.joblib')
|
| 27 |
-
image_path = os.path.join(script_dir, 'toolkit', 'car.png')
|
| 28 |
-
|
| 29 |
-
# Load the scaler and models
|
| 30 |
-
try:
|
| 31 |
-
# Load the scaler
|
| 32 |
-
with open(scaler_path, 'r') as f:
|
| 33 |
-
scaler_params = json.load(f)
|
| 34 |
-
scaler_X = MinMaxScaler()
|
| 35 |
-
scaler_X.scale_ = np.array(scaler_params["scale_"])
|
| 36 |
-
scaler_X.min_ = np.array(scaler_params["min_"])
|
| 37 |
-
scaler_X.data_min_ = np.array(scaler_params["data_min_"])
|
| 38 |
-
scaler_X.data_max_ = np.array(scaler_params["data_max_"])
|
| 39 |
-
scaler_X.data_range_ = np.array(scaler_params["data_range_"])
|
| 40 |
-
scaler_X.n_features_in_ = scaler_params["n_features_in_"]
|
| 41 |
-
scaler_X.feature_names_in_ = np.array(scaler_params["feature_names_in_"])
|
| 42 |
-
|
| 43 |
-
# Load the models
|
| 44 |
-
loaded_rf_model = load(rf_model_path)
|
| 45 |
-
print("Random Forest model loaded successfully.")
|
| 46 |
-
loaded_mlp_model = load_model(mlp_model_path)
|
| 47 |
-
print("MLP model loaded successfully.")
|
| 48 |
-
loaded_meta_model = load(meta_model_path)
|
| 49 |
-
print("Meta model loaded successfully.")
|
| 50 |
-
except Exception as e:
|
| 51 |
-
print(f"Error loading models or scaler: {e}")
|
| 52 |
-
|
| 53 |
-
def predict_new_values(new_input_data):
|
| 54 |
try:
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Error in Gradio interface: {e}")
|
| 85 |
-
return ["Error"] *
|
| 86 |
|
| 87 |
inputs = [
|
| 88 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
|
@@ -100,18 +116,18 @@ contamination_outputs = [
|
|
| 100 |
gr.Textbox(label="Rear Contamination")
|
| 101 |
]
|
| 102 |
|
| 103 |
-
|
| 104 |
-
gr.Textbox(label="Front Left
|
| 105 |
-
gr.Textbox(label="Front Right
|
| 106 |
-
gr.Textbox(label="Left
|
| 107 |
-
gr.Textbox(label="Right
|
| 108 |
-
gr.Textbox(label="Roof
|
| 109 |
-
gr.Textbox(label="Rear
|
| 110 |
]
|
| 111 |
|
| 112 |
with gr.Blocks() as demo:
|
| 113 |
-
gr.Markdown("<h1 style='text-align: center;'>Environmental Factor-Based Contamination &
|
| 114 |
-
gr.Markdown("This application predicts the contamination levels
|
| 115 |
|
| 116 |
with gr.Row():
|
| 117 |
with gr.Column():
|
|
@@ -124,7 +140,11 @@ with gr.Blocks() as demo:
|
|
| 124 |
with gr.Column(scale=1, min_width=0):
|
| 125 |
gr.Image(image_path) # Ensure the image is centered
|
| 126 |
|
| 127 |
-
gr.Button(value="Submit", variant="primary").click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
gr.Button(value="Clear").click(fn=lambda: None)
|
| 129 |
|
| 130 |
with gr.Column():
|
|
@@ -133,8 +153,8 @@ with gr.Blocks() as demo:
|
|
| 133 |
out.render()
|
| 134 |
|
| 135 |
with gr.Column():
|
| 136 |
-
gr.Markdown("###
|
| 137 |
-
for out in
|
| 138 |
out.render()
|
| 139 |
|
| 140 |
demo.launch()
|
|
|
|
| 7 |
from joblib import load
|
| 8 |
from tensorflow.keras.models import load_model
|
| 9 |
from sklearn.preprocessing import MinMaxScaler
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
import os
|
| 12 |
+
import sklearn
|
| 13 |
+
|
| 14 |
+
# Assuming the previous setup and imports have been done
|
| 15 |
+
|
| 16 |
+
def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
+
# Prepare the example data
|
| 19 |
+
example_data = pd.DataFrame({
|
| 20 |
+
'Velocity(mph)': [velocity],
|
| 21 |
+
'Temperature': [temperature],
|
| 22 |
+
'Precipitation': [precipitation],
|
| 23 |
+
'Humidity': [humidity]
|
| 24 |
+
})
|
| 25 |
|
| 26 |
+
# Scale the example data
|
| 27 |
+
example_data_scaled = scaler_X.transform(example_data)
|
| 28 |
|
| 29 |
+
# Function to predict contamination levels
|
| 30 |
+
def predict_contamination(example_data_scaled):
|
| 31 |
+
# Predict using MLP model
|
| 32 |
+
mlp_predictions_contamination, mlp_predictions_gradients = loaded_mlp_model.predict(example_data_scaled)
|
| 33 |
|
| 34 |
+
# Predict using RF model
|
| 35 |
+
rf_predictions = loaded_rf_model.predict(example_data_scaled)
|
| 36 |
+
|
| 37 |
+
# Combine predictions for meta model
|
| 38 |
+
combined_features = np.concatenate([np.concatenate([mlp_predictions_contamination, mlp_predictions_gradients], axis=1), rf_predictions], axis=1)
|
| 39 |
+
|
| 40 |
+
# Predict using meta model
|
| 41 |
+
meta_predictions = loaded_meta_model.predict(combined_features)
|
| 42 |
+
|
| 43 |
+
return meta_predictions[:, :6] # Assuming the first 6 columns are contamination predictions
|
| 44 |
+
|
| 45 |
+
# Predict contamination levels for the single example
|
| 46 |
+
contamination_levels = predict_contamination(example_data_scaled)
|
| 47 |
+
|
| 48 |
+
# Simulate contamination levels at multiple time intervals
|
| 49 |
+
time_intervals = np.arange(0, 601, 60) # Simulating time intervals from 0 to 600 seconds
|
| 50 |
+
|
| 51 |
+
# Generate simulated contamination levels (linear interpolation between predicted values)
|
| 52 |
+
simulated_contamination_levels = np.array([
|
| 53 |
+
np.linspace(contamination_levels[0][i], contamination_levels[0][i] * 2, len(time_intervals))
|
| 54 |
+
for i in range(contamination_levels.shape[1])
|
| 55 |
+
]).T
|
| 56 |
+
|
| 57 |
+
# Function to calculate cleaning time using linear interpolation
|
| 58 |
+
def calculate_cleaning_time(time_intervals, contamination_levels, threshold=0.4):
|
| 59 |
+
cleaning_times = []
|
| 60 |
+
for i in range(contamination_levels.shape[1]):
|
| 61 |
+
levels = contamination_levels[:, i]
|
| 62 |
+
for j in range(1, len(levels)):
|
| 63 |
+
if levels[j-1] <= threshold <= levels[j]:
|
| 64 |
+
# Linear interpolation
|
| 65 |
+
t1, t2 = time_intervals[j-1], time_intervals[j]
|
| 66 |
+
c1, c2 = levels[j-1], levels[j]
|
| 67 |
+
cleaning_time = t1 + (threshold - c1) * (t2 - t1) / (c2 - c1)
|
| 68 |
+
cleaning_times.append(cleaning_time)
|
| 69 |
+
break
|
| 70 |
+
return cleaning_times
|
| 71 |
+
|
| 72 |
+
# Calculate cleaning times for all 6 lidars
|
| 73 |
+
cleaning_times = calculate_cleaning_time(time_intervals, simulated_contamination_levels)
|
| 74 |
+
|
| 75 |
+
# Lidar names
|
| 76 |
+
lidar_names = ['F/L', 'F/R', 'Left', 'Right', 'Roof', 'Rear']
|
| 77 |
+
|
| 78 |
+
# Plot the graph
|
| 79 |
+
plt.figure(figsize=(12, 8))
|
| 80 |
+
|
| 81 |
+
for i in range(simulated_contamination_levels.shape[1]):
|
| 82 |
+
plt.plot(time_intervals, simulated_contamination_levels[:, i], label=f'{lidar_names[i]}')
|
| 83 |
+
plt.axhline(y=0.4, color='r', linestyle='--', label='Contamination Threshold' if i == 0 else "")
|
| 84 |
+
if i < len(cleaning_times):
|
| 85 |
+
plt.scatter(cleaning_times[i], 0.4, color='k') # Mark the cleaning time point
|
| 86 |
+
|
| 87 |
+
plt.title('Contamination Levels Over Time for Each Lidar')
|
| 88 |
+
plt.xlabel('Time (seconds)')
|
| 89 |
+
plt.ylabel('Contamination Level')
|
| 90 |
+
plt.legend()
|
| 91 |
+
plt.grid(True)
|
| 92 |
|
| 93 |
+
# Save the plot to a file
|
| 94 |
+
plt.savefig('plot.png')
|
| 95 |
+
|
| 96 |
+
# Return the plot and predictions
|
| 97 |
+
return plt, [f"{val * 100:.2f}%" for val in contamination_levels[0]], [f"{val:.2f}" for val in cleaning_times]
|
| 98 |
+
|
| 99 |
except Exception as e:
|
| 100 |
print(f"Error in Gradio interface: {e}")
|
| 101 |
+
return plt.figure(), ["Error"] * 6, ["Error"] * 6
|
| 102 |
|
| 103 |
inputs = [
|
| 104 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
|
|
|
| 116 |
gr.Textbox(label="Rear Contamination")
|
| 117 |
]
|
| 118 |
|
| 119 |
+
cleaning_time_outputs = [
|
| 120 |
+
gr.Textbox(label="Front Left Cleaning Time"),
|
| 121 |
+
gr.Textbox(label="Front Right Cleaning Time"),
|
| 122 |
+
gr.Textbox(label="Left Cleaning Time"),
|
| 123 |
+
gr.Textbox(label="Right Cleaning Time"),
|
| 124 |
+
gr.Textbox(label="Roof Cleaning Time"),
|
| 125 |
+
gr.Textbox(label="Rear Cleaning Time")
|
| 126 |
]
|
| 127 |
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
+
gr.Markdown("<h1 style='text-align: center;'>Environmental Factor-Based Contamination & Cleaning Time Prediction</h1>")
|
| 130 |
+
gr.Markdown("This application predicts the contamination levels, corresponding gradients, and cleaning times for different parts of a car's LiDAR system based on environmental factors such as velocity, temperature, precipitation, and humidity.")
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
with gr.Column():
|
|
|
|
| 140 |
with gr.Column(scale=1, min_width=0):
|
| 141 |
gr.Image(image_path) # Ensure the image is centered
|
| 142 |
|
| 143 |
+
gr.Button(value="Submit", variant="primary").click(
|
| 144 |
+
fn=predict_and_plot,
|
| 145 |
+
inputs=inputs,
|
| 146 |
+
outputs=[gr.Plot(label="Contamination Levels Over Time")] + contamination_outputs + cleaning_time_outputs
|
| 147 |
+
)
|
| 148 |
gr.Button(value="Clear").click(fn=lambda: None)
|
| 149 |
|
| 150 |
with gr.Column():
|
|
|
|
| 153 |
out.render()
|
| 154 |
|
| 155 |
with gr.Column():
|
| 156 |
+
gr.Markdown("### Cleaning Time Predictions")
|
| 157 |
+
for out in cleaning_time_outputs:
|
| 158 |
out.render()
|
| 159 |
|
| 160 |
demo.launch()
|