Precipitation Nowcasting

repository·master·Indexed 20 days ago

https://github.com/hzzone/precipitation-nowcasting

A PyTorch implementation of encoder-forecaster models, including TrajGRU and ConvLSTM, designed for precipitation nowcasting and benchmarked on the HKO-7 dataset. The repository includes tools for training with balanced MSE/MAE or WeightedCrossEntropyLoss, a VarFlow Python wrapper with a C++ core, and utilities for downloading and managing the HKO-7 dataset.

Tokens
6K
Snippets
17
Records
23
Agent score
70%

What's inside hzzone-precipitation-nowcasting

  1. Overview of Precipitation Nowcasting Models

    master

    This repository provides PyTorch-based encoder-forecaster models for precipitation nowcasting using Recurrent Neural Networks (RNNs). The primary implementations include:

    • TrajGRU: A trajectory-based GRU model.
    • ConvLSTM: A convolutional LSTM model.

    All models expect input data in the shape S*B*C*H*W (Sequence, Batch, Channel, Height, Width) and have been validated on the HKO-7 dataset.

  2. Review VarFlow implementation and examples

    master

    The VarFlow distribution includes the following files for implementation and testing:

    • VarFlow.cpp: The implementation of the VarFlow class.
    • VarFlow.h: The header definition of the VarFlow class.
    • example.cpp: A complete usage example demonstrating how to instantiate and use the class.
    • Data\yos_img_08.jpg & Data\yos_img_09.jpg: Sample images from the Yosemite flyby sequence for testing the algorithm.
  3. Understand the HKO-7 dataset file structure

    master

    Text Files (Frame Names)

    • hko7_rainy_train_days.txt: Frame names for the training set.
    • hko7_rainy_valid_days.txt: Frame names for the validation set.
    • hko7_rainy_test_days.txt: Frame names for the testing set.

    Intensity Data

    • intensity_day.pkl: The daily intensity of the HKO-7 data.

    Pandas DataFrames (located in the pd directory)

    These files contain datetime information and should be loaded using pandas.read_pickle():

    • hko7_all.pkl: Datetimes from 2009 to 2015.
    • hko7_all_09_14.pkl: Datetimes from 2009 to 2014.
    • hko7_all_15.pkl: Datetimes in the year 2015.
    • hko7_rainy_train.pkl: Datetimes for the training set.
    • hko7_rainy_valid.pkl: Datetimes for the validation set.
    • hko7_rainy_test.pkl: Datetimes for the test set.
  4. Use the VarFlow class for variational optical flow

    master

    The VarFlow class implements the variational optical flow algorithm described by Bruhn et al. To use it, include VarFlow.h in your C++ project and link against the OpenCV libraries.

    Note: The class was originally developed with OpenCV 1.1, but it is compatible with OpenCV 2.0. Ensure your compiler environment is correctly configured to link with your specific OpenCV version.

    // See example.cpp for a complete usage implementation
    #include "VarFlow.h"
  5. Install the VarFlow Python Wrapper

    master

    The VarFlow Python wrapper requires a two-step installation process: first, compiling the underlying C++ core using cmake, and second, installing the Python package using setup.py.

    1. Compile the C++ Core

    Create a build directory and run cmake. Depending on your operating system, use the appropriate commands below.

    Windows

    Use the Visual Studio generator:

    mkdir build
    cmake -G "Visual Studio 14 2015 Win64" ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DCMAKE_CONFIGURATION_TYPES="Release" ^
    ..

    Linux

    mkdir build
    cd build
    cmake ..

    Handling OpenCV Errors

    If cmake fails to find OpenCV, manually specify the path using the -DOpenCV_DIR flag:

    mkdir build
    cd build
    cmake -DOpenCV_DIR=/usr/local/software/opencv/share/OpenCV ..
    make

    2. Install the Python Package

    Once the build is successful, validate the installation by running the example script located at varflow/varflow.py. If it runs without error, install the package in development mode:

    python setup.py develop
    mkdir build
    cd build
    cmake -DOpenCV_DIR=/usr/local/software/opencv/share/OpenCV ..
    make
    python setup.py develop
  6. Train the Models

    master

    Training follows a two-step process using the HKO-7 dataset:

    1. Prepare Dataset: Apply for the HKO-7 Dataset and modify the paths in config.py to point to your local dataset location.
    2. Initial Training: Run the balanced MSE/MAE training script.
    3. Fine-tuning: Run the frame-weighted MSE script to fine-tune the model based on the weights from the previous step.
    # Step 1: Initial training
    python3 experiments/trajGRU_balanced_mse_mae/main.py
    
    # Step 2: Fine-tuning
    python3 experiments/trajGRU_frame_weighted_mse/main.py
  7. Run TrajGRU training experiments with balanced MSE/MAE loss

    master

    This entrypoint script configures and executes a training and testing loop for the TrajGRU model architecture using a balanced Weighted_mse_mae loss function. It integrates an Encoder and a Forecaster into an EF (Encoder-Forecaster) wrapper. The training process uses the Adam optimizer and a MultiStepLR learning rate scheduler.

    import torch
    from nowcasting.config import cfg
    from nowcasting.models.forecaster import Forecaster
    from nowcasting.models.encoder import Encoder
    from nowcasting.models.model import EF
    from nowcasting.models.loss import Weighted_mse_mae
    from nowcasting.models.trajGRU import TrajGRU
    from nowcasting.train_and_test import train_and_test
    from experiments.net_params import encoder_params, forecaster_params
    
    # Configuration
    batch_size = cfg.GLOBAL.BATCH_SZIE
    max_iterations = 100000
    test_iteration_interval = 1000
    test_and_save_checkpoint_iterations = 1000
    LR = 1e-4
    
    # Model Setup
    criterion = Weighted_mse_mae().to(cfg.GLOBAL.DEVICE)
    encoder = Encoder(encoder_params[0], encoder_params[1]).to(cfg.GLOBAL.DEVICE)
    forecaster = Forecaster(forecaster_params[0], forecaster_params[1]).to(cfg.GLOBAL.DEVICE)
    encoder_forecaster = EF(encoder, forecaster).to(cfg.GLOBAL.DEVICE)
    
    # Optimization
    optimizer = torch.optim.Adam(encoder_forecaster.parameters(), lr=LR)
    mult_step_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[30000, 60000], gamma=0.1)
    
    # Execution
    train_and_test(
        encoder_forecaster, 
        optimizer, 
        criterion, 
        mult_step_scheduler, 
        batch_size, 
        max_iterations, 
        test_iteration_interval, 
        test_and_save_checkpoint_iterations, 
        folder_name
    )
  8. Run Conv2D training experiments with train_and_test

    master

    To execute a Conv2D-based precipitation nowcasting training experiment, use the train_and_test function. This function orchestrates the training loop, evaluation, and checkpoint saving. It requires a model, an optimizer, a loss criterion, a learning rate scheduler, and several hyperparameter/interval settings.

    Key parameters for train_and_test:

    • model: The neural network instance (e.g., a Predictor configured with conv2d_params).
    • optimizer: The optimization algorithm (e.g., torch.optim.Adam).
    • criterion: The loss function (e.g., Weighted_mse_mae).
    • exp_lr_scheduler: The learning rate scheduler (e.g., torch.optim.lr_scheduler.StepLR).
    • batch_size: Number of samples per training batch.
    • max_iterations: Total number of training iterations.
    • test_iteration_interval: Frequency of evaluation iterations.
    • test_and_save_checkpoint_iterations: Frequency of evaluation and checkpoint saving.
    • folder_name: The directory name used for organizing experiment outputs.
    from nowcasting.train_and_test import train_and_test
    
    # Example setup
    train_and_test(
        model, 
        optimizer, 
        criterion, 
        exp_lr_scheduler, 
        batch_size=4, 
        max_iterations=80000, 
        test_iteration_interval=10000, 
        test_and_save_checkpoint_iterations=10000, 
        folder_name='conv2d'
    )