Hrishi-2003 commited on
Commit
b956a44
·
verified ·
1 Parent(s): cd9aac0

Upload 10 files

Browse files
Files changed (10) hide show
  1. LICENSE +21 -0
  2. Model Architecture +15 -0
  3. README.md +43 -3
  4. __init__.py +1 -0
  5. data_loader.py +27 -0
  6. evaluate.py +24 -0
  7. model_builder.py +33 -0
  8. predict.py +23 -0
  9. setup.py +18 -0
  10. train.py +45 -0
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Hrishikesh Shahane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
Model Architecture ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ConvLSTM Layer
2
+ The model leverages ConvLSTM2D, a combination of convolutional layers and LSTM units, to predict cloud cover:
3
+
4
+ ConvLSTM2D Layer: Captures spatiotemporal features by applying convolutional operations on the input sequence of images.
5
+ Batch Normalization: Normalizes the output of each convolutional layer to improve training speed and stability.
6
+ Residual Connections: Introduced in the model to allow for deeper layers without vanishing gradients, ensuring that important features are passed forward.
7
+ TimeDistributed Layer: Allows the model to apply 2D convolution to each frame in a sequence independently.
8
+ Sigmoid Activation: The final output layer uses the sigmoid activation function to output predicted cloud cover values in the range [0, 1].
9
+
10
+ Model Evaluation
11
+ SSIM (Structural Similarity Index)
12
+ SSIM is a metric used to measure the similarity between two images. It evaluates luminance, contrast, and structure. The closer the SSIM score is to 1, the more similar the two images are.
13
+ MSE (Mean Squared Error)
14
+ MSE calculates the average squared differences between the predicted and actual values, providing a quantitative measure of the prediction error. Lower MSE indicates better predictions.
15
+
README.md CHANGED
@@ -1,3 +1,43 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Cloud Cover Nowcasting
2
+ This project aims to predict cloud cover using a sequence-to-sequence ConvLSTM (Convolutional Long Short-Term Memory) model. The goal is to predict future cloud cover based on past satellite images. The model uses satellite .tif images taken at regular intervals to forecast cloud patterns, aiding in weather prediction and climate monitoring.
3
+
4
+ ## Features
5
+ Satellite Image Processing: The model processes `.tif` images, which are commonly used in satellite data, providing high-resolution, geospatial information.
6
+ ConvLSTM for Sequential Prediction: The ConvLSTM model is used to learn and predict the temporal dynamics of cloud cover. ConvLSTM combines the power of convolutional layers (for spatial feature extraction) with `LSTM (Long Short-Term Memory)` layers (for sequence learning), making it ideal for spatiotemporal data like satellite imagery.
7
+ Model Evaluation: The model's performance is evaluated using metrics like Structural Similarity Index (SSIM) and Mean Squared Error (MSE). SSIM helps in evaluating the similarity between the predicted and actual cloud cover images, while MSE gives a quantitative measure of prediction error.
8
+ Project Overview
9
+ ## What is ConvLSTM?
10
+ ConvLSTM is an advanced deep learning architecture that is particularly suitable for processing spatiotemporal data. Unlike regular LSTMs, which operate on sequences of scalar values, ConvLSTMs apply convolution operations within the LSTM structure, allowing the model to capture both spatial and temporal dependencies in the data.
11
+
12
+ Convolutional Layers: These layers capture spatial patterns in images, such as cloud structures, edges, and textures.
13
+ LSTM Layers: These layers allow the model to learn temporal dependencies, meaning the model can learn how cloud patterns evolve over time.
14
+ Sequence-to-Sequence Learning: The model is trained to predict the next frames in a sequence based on the previous ones. This is particularly useful for tasks like weather forecasting, where the past data influences future predictions.
15
+ Time Series Forecasting with ConvLSTM
16
+ The dataset consists of a series of satellite images (each representing cloud cover at different time intervals). Time series forecasting using ConvLSTM allows the model to predict the next frames in the sequence, making it an ideal tool for nowcasting cloud cover. Each sequence of images provides context to help the model make accurate predictions about future cloud cover, taking into account both spatial and temporal factors.
17
+
18
+ ## Dataset
19
+ The dataset consists of .tif images (grayscale cloud cover images) that are loaded into memory and preprocessed for model training. The images are organized into sequences of frames to enable the model to learn the temporal evolution of cloud cover.
20
+
21
+
22
+
23
+ ## Features in short
24
+ - Processes satellite `.tif` images.
25
+ - Uses ConvLSTM for sequential prediction.
26
+ - Evaluates model accuracy using SSIM and MSE.
27
+
28
+ ## Setup
29
+ 1. Clone the repository.
30
+ 2. Install dependencies: `pip install -r requirements.txt`.
31
+ 3. Add `.tif` images to the `data/` directory.
32
+
33
+ ## Run Training
34
+ The model uses ConvLSTM layers to process the sequences of images. It learns spatial features from each frame and temporal patterns from the sequence, enabling it to predict future cloud cover images. The model is trained using mean absolute error (MAE) as the loss function, and it is optimized using the Adam optimizer.
35
+ `python /train.py`
36
+
37
+ ## Run Evaluate
38
+ Once the model is trained, you can evaluate its performance on a validation set using the following command:
39
+ `python /evaluate.py`
40
+
41
+ ## Run Predict
42
+ To make predictions on a new set of satellite images, use the following command:
43
+ `python /predict.py`
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # This file marks the `src` directory as a Python package.
data_loader.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+
5
+ def load_images_from_folder(folder_path, img_size=(200, 200)):
6
+ images = []
7
+ for filename in sorted(os.listdir(folder_path)):
8
+ if filename.endswith('.tif'):
9
+ img_path = os.path.join(folder_path, filename)
10
+ img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
11
+ if img is not None:
12
+ img = cv2.resize(img, img_size)
13
+ img = img.astype('float32') / np.max(img) # Normalize
14
+ images.append(img)
15
+ else:
16
+ print(f"Warning: {img_path} could not be read.")
17
+ print(f"Total images loaded from {folder_path}: {len(images)}")
18
+ return np.array(images)
19
+
20
+ def create_sequences(data, sequence_length):
21
+ num_sequences = data.shape[0] - sequence_length + 1
22
+ return np.array([data[i:i + sequence_length] for i in range(num_sequences)])
23
+
24
+ def create_shifted_frames(data):
25
+ x = data[:, :-1, :, :, :] # Frames 0 to n-1
26
+ y = data[:, 1:, :, :, :] # Frames 1 to n
27
+ return x, y
evaluate.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ from sklearn.metrics import mean_squared_error, structural_similarity as ssim
3
+
4
+ def evaluate_model(model, x_test, y_test):
5
+ predictions = model.predict(x_test)
6
+ mse = mean_squared_error(y_test.flatten(), predictions.flatten())
7
+ ssim_score = ssim(y_test[0, -1, :, :, 0], predictions[0, -1, :, :, 0], data_range=1.0)
8
+ print(f"Mean Squared Error: {mse}")
9
+ print(f"Structural Similarity Index: {ssim_score}")
10
+
11
+ return predictions
12
+
13
+ def visualize_predictions(x_test, y_test, predictions, idx=0):
14
+ fig, axes = plt.subplots(1, 3, figsize=(15, 5))
15
+ axes[0].imshow(x_test[idx, -1, :, :, 0], cmap='gray')
16
+ axes[0].set_title("Input Frame")
17
+
18
+ axes[1].imshow(y_test[idx, -1, :, :, 0], cmap='gray')
19
+ axes[1].set_title("True Frame")
20
+
21
+ axes[2].imshow(predictions[idx, -1, :, :, 0], cmap='gray')
22
+ axes[2].set_title("Predicted Frame")
23
+
24
+ plt.show()
model_builder.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.layers import (
3
+ ConvLSTM2D, Input, Conv2D, BatchNormalization, Add, ReLU, TimeDistributed
4
+ )
5
+
6
+ def build_residual_convlstm_model_seq2seq(input_shape):
7
+ input_layer = Input(shape=input_shape)
8
+
9
+ # First ConvLSTM layer with residual connection
10
+ x = ConvLSTM2D(filters=128, kernel_size=(3, 3), padding='same', return_sequences=True)(input_layer)
11
+ x = BatchNormalization()(x)
12
+ res = x # Save the residual
13
+
14
+ # Second ConvLSTM layer
15
+ x = ConvLSTM2D(filters=128, kernel_size=(3, 3), padding='same', return_sequences=True)(x)
16
+ x = BatchNormalization()(x)
17
+
18
+ # Residual connection
19
+ x = Add()([x, res])
20
+
21
+ # Third ConvLSTM layer with residual connection, returning the entire sequence
22
+ x = ConvLSTM2D(filters=128, kernel_size=(3, 3), padding='same', return_sequences=True)(x)
23
+ x = BatchNormalization()(x)
24
+
25
+ # Apply Conv2D and ReLU to each frame in the sequence using TimeDistributed
26
+ x = TimeDistributed(Conv2D(128, (3, 3), padding='same'))(x)
27
+ x = TimeDistributed(ReLU())(x)
28
+
29
+ # Final Conv2D layer to predict the sequence of frames
30
+ output_layer = TimeDistributed(Conv2D(1, (3, 3), activation='sigmoid', padding='same'))(x)
31
+
32
+ model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
33
+ return model
predict.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from tensorflow.keras.models import load_model
3
+ from data_loader import load_images_from_folder, create_sequences
4
+
5
+ def predict_next_frame(model_path, input_sequence):
6
+ model = load_model(model_path)
7
+ predictions = model.predict(input_sequence)
8
+ return predictions
9
+
10
+ if __name__ == "__main__":
11
+ folder_path = "/path/to/new/data"
12
+ img_size = (200, 200)
13
+ sequence_length = 5
14
+
15
+ # Load and preprocess data
16
+ dataset = load_images_from_folder(folder_path, img_size=img_size)
17
+ dataset = np.expand_dims(dataset, axis=-1)
18
+ sequences = create_sequences(dataset, sequence_length)
19
+
20
+ # Load trained model and predict
21
+ model_path = "best_model.keras"
22
+ predictions = predict_next_frame(model_path, sequences)
23
+ print("Predictions generated for the input sequence.")
setup.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="cloud_cover_nowcasting",
5
+ version="0.1.0",
6
+ author="Hrishikesh Shahane",
7
+ description="A package for cloud cover nowcasting using ConvLSTM.",
8
+ packages=find_packages(where="src"),
9
+ package_dir={"": "src"},
10
+ install_requires=[
11
+ "tensorflow>=2.9.0",
12
+ "numpy",
13
+ "opencv-python",
14
+ "matplotlib",
15
+ "scikit-image",
16
+ "scikit-learn",
17
+ ],
18
+ )
train.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from sklearn.model_selection import train_test_split
3
+ from tensorflow.keras.optimizers import Adam
4
+ from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
5
+ from data_loader import load_images_from_folder, create_sequences, create_shifted_frames
6
+ from model_builder import build_residual_convlstm_model_seq2seq
7
+
8
+ # Parameters
9
+ folder_path = "/home/hrishikesh2003/Data/DataNewApproch"
10
+ image_size = (200, 200)
11
+ sequence_length = 5
12
+ batch_size = 4
13
+ epochs = 20
14
+
15
+ # Load and preprocess data
16
+ dataset = load_images_from_folder(folder_path, img_size=image_size)
17
+ dataset = np.expand_dims(dataset, axis=-1)
18
+ sequences = create_sequences(dataset, sequence_length)
19
+
20
+ # Split data
21
+ train_sequences, val_sequences = train_test_split(sequences, test_size=0.1, shuffle=False)
22
+ x_train, y_train = create_shifted_frames(train_sequences)
23
+ x_val, y_val = create_shifted_frames(val_sequences)
24
+
25
+ # Build model
26
+ model = build_residual_convlstm_model_seq2seq(input_shape=(sequence_length - 1, *image_size, 1))
27
+ model.compile(optimizer=Adam(learning_rate=0.001), loss='mean_absolute_error')
28
+
29
+ # Callbacks
30
+ checkpoint = ModelCheckpoint(
31
+ 'best_model.keras', monitor='val_loss', save_best_only=True, mode='min', verbose=1
32
+ )
33
+ early_stopping = EarlyStopping(monitor='val_loss', patience=20, mode='min', verbose=1)
34
+
35
+ # Train model
36
+ if x_train.shape[0] > 0 and y_train.shape[0] > 0:
37
+ model.fit(
38
+ x_train, y_train,
39
+ batch_size=batch_size,
40
+ epochs=epochs,
41
+ validation_data=(x_val, y_val),
42
+ callbacks=[checkpoint, early_stopping]
43
+ )
44
+ else:
45
+ print("Not enough training data to train the model.")