| | import numpy as np |
| | import matplotlib.pyplot as plt |
| | import os |
| | from sklearn.model_selection import train_test_split |
| |
|
| | |
| | def generate_data(num_samples=100, img_size=128): |
| | X = [] |
| | Y = [] |
| | for _ in range(num_samples): |
| | |
| | terrain = np.random.rand(img_size, img_size) |
| |
|
| | |
| | rainfall = np.random.rand(img_size, img_size) * 0.5 |
| |
|
| | |
| | flood_risk = np.clip(terrain + rainfall, 0, 1) |
| |
|
| | X.append(np.dstack([terrain, rainfall])) |
| | Y.append(flood_risk) |
| |
|
| | X = np.array(X) |
| | Y = np.array(Y) |
| | return X, Y |
| |
|
| | |
| | X, Y = generate_data(200) |
| |
|
| | |
| | X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42) |
| |
|
| |
|
| | import tensorflow as tf |
| | from tensorflow.keras import layers, Model |
| |
|
| | |
| | def unet_model(input_shape=(128, 128, 2)): |
| | inputs = layers.Input(shape=input_shape) |
| |
|
| | |
| | c1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(inputs) |
| | c1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c1) |
| | p1 = layers.MaxPooling2D((2, 2))(c1) |
| |
|
| | c2 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(p1) |
| | c2 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c2) |
| | p2 = layers.MaxPooling2D((2, 2))(c2) |
| |
|
| | |
| | b = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(p2) |
| | b = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(b) |
| |
|
| | |
| | u2 = layers.UpSampling2D((2, 2))(b) |
| | c3 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(u2) |
| | c3 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c3) |
| |
|
| | u1 = layers.UpSampling2D((2, 2))(c3) |
| | c4 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(u1) |
| | c4 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c4) |
| |
|
| | outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(c4) |
| | return Model(inputs, outputs) |
| |
|
| | |
| | model = unet_model() |
| | model.compile(optimizer='adam', loss='mse', metrics=['accuracy']) |
| | model.summary() |
| |
|
| |
|
| | |
| | history = model.fit(X_train, Y_train, epochs=10, batch_size=16, validation_data=(X_test, Y_test)) |
| |
|
| | import numpy as np |
| | import gradio as gr |
| | import tensorflow as tf |
| | from tensorflow.keras import layers, Model |
| |
|
| | |
| | def unet_model(input_shape=(128, 128, 2)): |
| | inputs = layers.Input(shape=input_shape) |
| |
|
| | |
| | c1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(inputs) |
| | c1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c1) |
| | p1 = layers.MaxPooling2D((2, 2))(c1) |
| |
|
| | c2 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(p1) |
| | c2 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c2) |
| | p2 = layers.MaxPooling2D((2, 2))(c2) |
| |
|
| | |
| | b = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(p2) |
| | b = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(b) |
| |
|
| | |
| | u2 = layers.UpSampling2D((2, 2))(b) |
| | c3 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(u2) |
| | c3 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c3) |
| |
|
| | u1 = layers.UpSampling2D((2, 2))(c3) |
| | c4 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(u1) |
| | c4 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c4) |
| |
|
| | outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(c4) |
| | return Model(inputs, outputs) |
| |
|
| | |
| | model = unet_model() |
| | model.compile(optimizer='adam', loss='mse', metrics=['accuracy']) |
| |
|
| | |
| | def predict_flood(terrain, rainfall): |
| | try: |
| | |
| | if not isinstance(terrain, np.ndarray) or not isinstance(rainfall, np.ndarray): |
| | raise ValueError("Both terrain and rainfall must be NumPy arrays.") |
| |
|
| | |
| | if terrain.shape != (128, 128) or rainfall.shape != (128, 128): |
| | raise ValueError("Both terrain and rainfall images must be of shape (128, 128).") |
| |
|
| | |
| | terrain = terrain.astype(np.float32) / 255.0 |
| | rainfall = rainfall.astype(np.float32) / 255.0 |
| |
|
| | |
| | input_data = np.dstack([terrain, rainfall]) |
| | input_data = input_data.reshape(1, 128, 128, 2) |
| |
|
| | |
| | print(f"Input data shape: {input_data.shape}") |
| | print(f"Terrain min/max: {terrain.min()}/{terrain.max()}") |
| | print(f"Rainfall min/max: {rainfall.min()}/{rainfall.max()}") |
| |
|
| | |
| | prediction = model.predict(input_data)[0].squeeze() |
| |
|
| | |
| | print(f"Prediction shape: {prediction.shape}") |
| | print(f"Prediction min/max: {prediction.min()}/{prediction.max()}") |
| |
|
| | |
| | if prediction.shape != (128, 128): |
| | raise ValueError("Model output is not of expected shape (128, 128).") |
| |
|
| | |
| | prediction = (prediction * 255).astype(np.uint8) |
| |
|
| | return prediction |
| |
|
| | except Exception as e: |
| | |
| | print(f"Error during prediction: {e}") |
| | |
| | return np.zeros((128, 128), dtype=np.uint8) |
| |
|
| | |
| | iface = gr.Interface( |
| | fn=predict_flood, |
| | inputs=[ |
| | gr.Image(type="numpy", label="Terrain Map", image_mode='L'), |
| | gr.Image(type="numpy", label="Rainfall Map", image_mode='L') |
| | ], |
| | outputs=gr.Image(label="Predicted Flood Risk Map", type="numpy"), |
| | title="Flood Risk Prediction", |
| | description="Upload terrain and rainfall maps to predict flood risk." |
| | ) |
| |
|
| | iface.launch() |
| |
|