Spaces:
Runtime error
Runtime error
| # Import libraries | |
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate | |
| import matplotlib.pyplot as plt | |
| import rasterio | |
| import os | |
| from huggingface_hub import HfApi, HfFolder | |
| from huggingface_hub import create_repo, upload_file | |
| # Define the U-Net model | |
| def build_unet(input_shape=(128, 128, 1)): | |
| inputs = Input(input_shape) | |
| # Encoder | |
| c1 = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs) | |
| p1 = MaxPooling2D((2, 2))(c1) | |
| c2 = Conv2D(128, (3, 3), activation='relu', padding='same')(p1) | |
| p2 = MaxPooling2D((2, 2))(c2) | |
| c3 = Conv2D(256, (3, 3), activation='relu', padding='same')(p2) | |
| # Decoder | |
| u1 = UpSampling2D((2, 2))(c3) | |
| m1 = concatenate([u1, c2]) | |
| c4 = Conv2D(128, (3, 3), activation='relu', padding='same')(m1) | |
| u2 = UpSampling2D((2, 2))(c4) | |
| m2 = concatenate([u2, c1]) | |
| c5 = Conv2D(64, (3, 3), activation='relu', padding='same')(m2) | |
| outputs = Conv2D(1, (1, 1), activation='sigmoid')(c5) | |
| model = tf.keras.Model(inputs, outputs) | |
| return model | |
| # Prepare the model | |
| input_shape = (128, 128, 1) | |
| unet_model = build_unet(input_shape) | |
| unet_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) | |
| unet_model.summary() | |
| # Load sample terrain data (replace with your own dataset) | |
| # For demonstration, we'll create synthetic data | |
| def generate_synthetic_data(num_samples=100, img_size=128): | |
| data = [] | |
| labels = [] | |
| for _ in range(num_samples): | |
| image = np.random.rand(img_size, img_size, 1) | |
| label = (image > 0.5).astype(float) | |
| data.append(image) | |
| labels.append(label) | |
| return np.array(data), np.array(labels) | |
| # Generate synthetic data | |
| X_train, y_train = generate_synthetic_data() | |
| # Train the model | |
| history = unet_model.fit(X_train, y_train, epochs=5, batch_size=8, validation_split=0.2) | |
| # Save the model | |
| unet_model.save("unet_flood_model.h5") | |
| # Visualize results | |
| def plot_sample(image, mask, prediction=None): | |
| plt.figure(figsize=(12, 4)) | |
| plt.subplot(1, 3, 1) | |
| plt.title("Input") | |
| plt.imshow(image.squeeze(), cmap='gray') | |
| plt.subplot(1, 3, 2) | |
| plt.title("Ground Truth") | |
| plt.imshow(mask.squeeze(), cmap='gray') | |
| if prediction is not None: | |
| plt.subplot(1, 3, 3) | |
| plt.title("Prediction") | |
| plt.imshow(prediction.squeeze(), cmap='gray') | |
| plt.show() | |
| # Test the model | |
| sample_image = X_train[0:1] | |
| sample_mask = y_train[0:1] | |
| prediction = unet_model.predict(sample_image) | |
| plot_sample(sample_image, sample_mask, prediction) | |
| # Define repository name and token | |
| repo_name = "flood-risk-prediction" | |
| hf_token = "colab api" # Replace with your token | |
| # Create the repository | |
| create_repo(repo_name, token=hf_token, exist_ok=True) | |
| # Upload the model file | |
| upload_file( | |
| path_or_fileobj="unet_flood_model.h5", # Path to your model file | |
| path_in_repo="unet_flood_model.h5", # Target path in the repository | |
| repo_id=f"Asrar990/{repo_name}", # Replace 'your_username' with your Hugging Face username | |
| token=hf_token # Use your token here | |
| ) | |