File size: 2,283 Bytes
ee9042d
 
 
 
 
 
 
 
 
 
b1c48f0
ee9042d
 
 
 
 
 
d929c1c
ee9042d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95aca7
ee9042d
 
 
 
 
31b6324
ee9042d
 
 
 
 
8f7d85e
 
ee9042d
 
 
 
 
 
 
28cb7d5
1733ed1
ee9042d
 
31b6324
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gradio as gr
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
import tensorflow_probability as tfp
from sklearn.metrics import accuracy_score

# Load the dataset
df = pd.read_csv('./Dataset/datatraining.csv')

scaler = MinMaxScaler()

def prepare_dataset(df):
    X = df[['Temperature', 'Humidity', 'Light', 'CO2']].values
    y = df['Occupancy'].values
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)
    return X_train_scaled, X_test_scaled, y_train, y_test

X_train_scaled, X_test_scaled, y_train, y_test = prepare_dataset(df)

# Build the Bayesian Neural Network model
def build_bnn_model(input_shape):
    model = tf.keras.models.Sequential([
        tf.keras.layers.Input(shape=(input_shape,)),
        tf.keras.layers.BatchNormalization(),
        tfp.layers.DenseFlipout(64, activation='relu'),
        tf.keras.layers.BatchNormalization(),
        tfp.layers.DenseFlipout(64, activation='relu'),
        tf.keras.layers.BatchNormalization(),
        tfp.layers.DenseFlipout(1, activation='sigmoid'),
    ])

    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01))
    return model

bnn_model = build_bnn_model(input_shape=X_train_scaled.shape[1])

# Train the model
bnn_model.fit(X_train_scaled, y_train, epochs=50, verbose=0)


def predict_occupancy(Temperature, Humidity, Light, CO2):
    input_data = np.array([[Temperature, Humidity, Light, CO2]])
    # Reshape the input data to 2-dimensional array
    input_data_reshaped = input_data.reshape(1, -1)
    input_data_scaled = scaler.transform(input_data_reshaped)
    y_pred = bnn_model.predict(input_data_scaled)[0][0]
    return 'Occupied' if y_pred >= 0.5 else 'Not Occupied'


iface = gr.Interface(
    fn=predict_occupancy,
    inputs=['number', 'number', 'number', 'number'],
    outputs='text',
    title="Enhancing Building Energy Efficiency through Regularized Bayesian Neural Networks for Precise Occupancy Detection and Energy Optimization",
    description="Predict occupancy status based on sensor readings."
)
iface.launch()