### Install Dependencies via Conda Source: https://github.com/jeya-maria-jose/transweather/blob/main/README.md Create and activate the environment using the provided configuration file. ```bash conda env create -f environment.yml conda activate transweather ``` -------------------------------- ### Install Dependencies via Pip Source: https://github.com/jeya-maria-jose/transweather/blob/main/README.md Manual installation of required package versions if not using Conda. ```text timm==0.3.2 mmcv-full==1.2.7 torch==1.7.1 torchvision==0.8.2 opencv-python==4.5.1.48 ``` -------------------------------- ### Clone the Repository Source: https://github.com/jeya-maria-jose/transweather/blob/main/README.md Initial steps to download the project source code. ```bash git clone https://github.com/jeya-maria-jose/TransWeather cd TransWeather ``` -------------------------------- ### Initialize Validation Dataset and DataLoader Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Sets up the validation dataset and loader for testing, with a recommended batch size of 1. ```python # Initialize validation dataset val_dataset = ValData(val_data_dir, val_filename) # Create DataLoader (batch_size=1 recommended for testing) val_loader = DataLoader( val_dataset, batch_size=1, shuffle=False, num_workers=8 ) # Process validation samples for input_im, gt, input_name in val_loader: # Images are automatically resized to multiple of 16 # input_im: normalized [-1, 1], gt: normalized [0, 1] print(f"Image: {input_name[0]}, Shape: {input_im.shape}") break ``` -------------------------------- ### Train All-Weather Model Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Commands for setting up the environment and training the full TransWeather model on the All-Weather dataset. ```bash # Clone and setup environment git clone https://github.com/jeya-maria-jose/TransWeather cd TransWeather conda env create -f environment.yml conda activate transweather # Download All-Weather training dataset and place in ./data/train/ # Download validation datasets and place in ./data/test/ # Train with default parameters python train.py -exp_name TransWeather_allweather # Train with custom parameters python train.py \ -learning_rate 2e-4 \ -crop_size 256 256 \ -train_batch_size 18 \ -val_batch_size 1 \ -epoch_start 0 \ -num_epochs 200 \ -lambda_loss 0.04 \ -seed 19 \ -exp_name TransWeather_experiment # Output: # Seed: 19 # --- Hyper-parameters for training --- # learning_rate: 0.0002 # crop_size: [256, 256] # train_batch_size: 18 # ... # Epoch: 0, Iteration: 0 # Rain Drop old_val_psnr: XX.XX, old_val_ssim: X.XXXX ``` -------------------------------- ### Initialize and Use Transweather Model Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Initializes the TransWeather model, loads pretrained weights, moves it to the appropriate device (GPU or CPU), and performs a forward pass on a single degraded image. Ensure input images are normalized to the [-1, 1] range. ```python import torch from transweather_model import Transweather # Initialize the TransWeather model model = Transweather() # Load pretrained weights from checkpoint model = Transweather(path='./checkpoints/best') # Move model to GPU device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) # Process a single degraded image (batch_size=1, channels=3, height=256, width=256) # Input should be normalized to [-1, 1] range input_image = torch.randn(1, 3, 256, 256).to(device) # Forward pass - returns clean image with same dimensions with torch.no_grad(): model.eval() clean_image = model(input_image) # Output: torch.Size([1, 3, 256, 256]), values in [-1, 1] range (tanh activation) print(f"Output shape: {clean_image.shape}") ``` -------------------------------- ### Initialize and Run Convolutional Projection Head Source: https://context7.com/jeya-maria-jose/transweather/llms.txt The projection head performs progressive upsampling using skip connections from the encoder to reconstruct the image. ```python import torch from transweather_model import convprojection # Initialize convolutional projection head conv_proj = convprojection() conv_proj = conv_proj.cuda() # Encoder features (skip connections) encoder_features = [ torch.randn(1, 64, 64, 64).cuda(), torch.randn(1, 128, 32, 32).cuda(), torch.randn(1, 320, 16, 16).cuda(), torch.randn(1, 512, 8, 8).cuda() ] # Decoder output decoder_output = [torch.randn(1, 512, 4, 4).cuda()] # Progressive upsampling with skip connections with torch.no_grad(): output = conv_proj(encoder_features, decoder_output) # Output shape before final convolution: [B, 8, 512, 512] print(f"Projection output: {output.shape}") ``` -------------------------------- ### Dataset Directory Structure Source: https://github.com/jeya-maria-jose/transweather/blob/main/README.md Required file system layout for training and testing data. ```text TransWeather ├── data | ├── train # Training | | ├── | | | ├── input # rain images | | | └── gt # clean images | | └── dataset_filename.txt | └── test # Testing | | ├── | | | ├── input # rain images | | | └── gt # clean images | | └── dataset_filename.txt ``` -------------------------------- ### Initialize and Use Transweather_base Model Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Initializes the Transweather_base model, an encoder-only variant. Optionally loads weights from a checkpoint and moves the model to the selected device. It then processes a degraded image. ```python import torch from transweather_model import Transweather_base # Initialize the base model (encoder-only architecture) model = Transweather_base() # Optional: load from checkpoint model = Transweather_base(path='./checkpoints/rain800_best') device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) # Process degraded image input_image = torch.randn(1, 3, 256, 256).to(device) with torch.no_grad(): model.eval() clean_image = model(input_image) print(f"Restored image shape: {clean_image.shape}") ``` -------------------------------- ### Initialize and Run Transweather Encoder Source: https://context7.com/jeya-maria-jose/transweather/llms.txt The encoder processes RGB images into multi-scale feature maps. Input dimensions must be divisible by 32. ```python # embed_dims=[64, 128, 320, 512], depths=[2, 2, 2, 2] encoder = Tenc() encoder = encoder.cuda() # Input: RGB image tensor [B, 3, H, W] # H, W should be divisible by 32 input_image = torch.randn(1, 3, 256, 256).cuda() # Forward pass returns multi-scale features with torch.no_grad(): features = encoder(input_image) # Returns list of 4 feature maps at different scales: # features[0]: [B, 64, H/4, W/4] - Stage 1 output # features[1]: [B, 128, H/8, W/8] - Stage 2 output # features[2]: [B, 320, H/16, W/16] - Stage 3 output # features[3]: [B, 512, H/32, W/32] - Stage 4 output for i, feat in enumerate(features): print(f"Stage {i+1}: {feat.shape}") ``` -------------------------------- ### Initialize and Use TrainData Dataset and DataLoader Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Sets up the TrainData PyTorch Dataset for loading paired degraded and ground truth images, including random cropping for augmentation. It then creates a DataLoader for batch processing with multi-worker loading. The dataset expects a specific directory structure and a file listing image paths. ```python from torch.utils.data import DataLoader from train_data_functions import TrainData # Dataset directory structure: # ./data/train/ # ├── allweather.txt # List of input image paths # ├── / # │ ├── input/ # Degraded images # │ └── gt/ # Ground truth clean images crop_size = [256, 256] # Height, Width for random crops train_data_dir = './data/train/' train_filename = 'allweather.txt' # File containing list of image paths # Initialize dataset train_dataset = TrainData(crop_size, train_data_dir, train_filename) # Create DataLoader with multi-worker loading train_loader = DataLoader( train_dataset, batch_size=18, shuffle=True, num_workers=8 ) # Iterate through batches for batch_id, (input_image, gt_image, img_id) in enumerate(train_loader): # input_image: normalized to [-1, 1], shape [B, 3, 256, 256] # gt_image: normalized to [0, 1], shape [B, 3, 256, 256] # img_id: list of image identifiers print(f"Batch {batch_id}: input={input_image.shape}, gt={gt_image.shape}") break ``` -------------------------------- ### Initialize ValData Dataset Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Initializes the ValData PyTorch Dataset for validation and test data. This dataset automatically resizes images to be divisible by 16 while maintaining aspect ratio and capping resolution at 1024 pixels, which is necessary for the transformer architecture. It requires a specific directory structure and a file listing image paths. ```python from torch.utils.data import DataLoader from val_data_functions import ValData # Dataset directory structure: # ./data/test/ # ├── raindroptesta.txt # List of test image paths # ├── / # │ ├── input/ # Degraded test images # │ └── gt/ # Ground truth images val_data_dir = './data/test/' val_filename = 'raindroptesta.txt' # Initialize validation dataset (example with specific parameters) # val_dataset = ValData(val_data_dir, val_filename) # Create DataLoader for validation # val_loader = DataLoader( # val_dataset, # batch_size=1, # shuffle=False, # num_workers=0 # ) ``` -------------------------------- ### Train Individual Weather Removal Model Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Commands for training the base encoder-only model on specific datasets for task-specific weather removal. ```bash # Train on Rain-800 dataset for deraining only python train-individual.py \ -train_batch_size 32 \ -exp_name Transweather-rain800 \ -epoch_start 0 \ -num_epochs 250 # Modify train-individual.py for different datasets: # Change labeled_name = 'rain800.txt' to your dataset file # Change val_filename1 = 'rain800_test.txt' to matching test file # Output: # Seed: 19 # --- Hyper-parameters for training --- # Rain 800 old_val_psnr: XX.XX, old_val_ssim: X.XXXX # Epoch: 0, Iteration: 0 # ... # model saved ``` -------------------------------- ### Test on Snow100K Dataset Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Evaluation script for the Snow100K-L test dataset. ```bash # Test on Snow100K-L dataset python test_snow100k.py \ -exp_name TransWeather_allweather \ -val_batch_size 1 \ -seed 19 # Ensure snowtest100k_L.txt exists in ./data/test/ # Results saved to ./results/snowtest100k/{exp_name}/ # Output: # Seed: 19 # cuda # --- Testing starts! --- # val_psnr: XX.XX, val_ssim: X.XXXX # validation time is XXX.XXXX ``` -------------------------------- ### Execute Complete Inference Pipeline Source: https://context7.com/jeya-maria-jose/transweather/llms.txt End-to-end restoration pipeline including image preprocessing, model inference, and post-processing. ```python import torch import torch.nn as nn from PIL import Image import numpy as np from torchvision.transforms import Compose, ToTensor, Normalize from transweather_model import Transweather def restore_image(model, image_path, device): """Complete inference pipeline for weather image restoration.""" # Load and preprocess image input_img = Image.open(image_path).convert('RGB') # Resize to multiple of 16 (required by transformer) w, h = input_img.size new_w = int(16 * np.ceil(w / 16.0)) new_h = int(16 * np.ceil(h / 16.0)) input_img = input_img.resize((new_w, new_h), Image.LANCZOS) # Transform to tensor and normalize to [-1, 1] transform = Compose([ ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) input_tensor = transform(input_img).unsqueeze(0).to(device) # Model inference with torch.no_grad(): output = model(input_tensor) # Post-process: convert from [-1, 1] to [0, 255] output = output.squeeze(0).cpu() output = (output + 1) / 2 # [-1, 1] -> [0, 1] output = output.clamp(0, 1) output = (output.permute(1, 2, 0).numpy() * 255).astype(np.uint8) return Image.fromarray(output) # Usage device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = Transweather(path='./checkpoints/best').to(device) model = nn.DataParallel(model) model.eval() restored = restore_image(model, './data/test/raindrop/input/test_001.png', device) restored.save('./results/restored_001.png') ``` -------------------------------- ### Initialize and Run Transformer Decoder Source: https://context7.com/jeya-maria-jose/transweather/llms.txt The transformer decoder processes encoder features using learnable weather-type query embeddings. ```python import torch from transweather_model import Tdec # Initialize transformer decoder decoder = Tdec() decoder = decoder.cuda() # Input: list of encoder features (output from Tenc) # Simulating encoder output encoder_features = [ torch.randn(1, 64, 64, 64).cuda(), # Stage 1 torch.randn(1, 128, 32, 32).cuda(), # Stage 2 torch.randn(1, 320, 16, 16).cuda(), # Stage 3 torch.randn(1, 512, 8, 8).cuda() # Stage 4 ] # Decoder processes the deepest features with weather queries with torch.no_grad(): decoder_output = decoder(encoder_features) # Returns list with decoded features # decoder_output[0]: [B, 512, 4, 4] print(f"Decoder output: {decoder_output[0].shape}") ``` -------------------------------- ### Implement VGG16 Perceptual Loss Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Configures a VGG16-based perceptual loss network to compute content-aware loss between predicted and ground truth images. ```python import torch from torchvision.models import vgg16 from perceptual import LossNetwork device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Load pretrained VGG16 and extract first 16 layers vgg_model = vgg16(pretrained=True).features[:16] vgg_model = vgg_model.to(device) # Freeze VGG parameters for param in vgg_model.parameters(): param.requires_grad = False # Initialize perceptual loss network # Uses relu1_2, relu2_2, relu3_3 features loss_network = LossNetwork(vgg_model) loss_network.eval() # Compute perceptual loss between prediction and ground truth pred_image = torch.randn(4, 3, 256, 256).to(device) gt_image = torch.randn(4, 3, 256, 256).to(device) perceptual_loss = loss_network(pred_image, gt_image) print(f"Perceptual loss: {perceptual_loss.item():.4f}") ``` -------------------------------- ### Test on Raindrop Dataset Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Evaluation script for the Raindrop dataset test set A. ```bash # Test on Raindrop test set A python test_raindrop.py \ -exp_name TransWeather_allweather \ -val_batch_size 1 \ -seed 19 # Ensure raindroptest1a.txt exists in ./data/test/ # Results directory structure: # ./{category}_results/{exp_name}/ # └── rain/ # Output: # Seed: 19 # --- Testing starts! --- # val_psnr: XX.XX, val_ssim: X.XXXX # validation time is XXX.XXXX ``` -------------------------------- ### Train Individual Restoration Task Source: https://github.com/jeya-maria-jose/transweather/blob/main/README.md Command to train the TransWeather encoder on specific datasets without the weather decoder. ```bash python train-individual.py -train_batch_size 32 -exp_name Transweather-finetune -epoch_start 0 -num_epochs 250 ``` -------------------------------- ### Initialize Transformer Encoder Source: https://context7.com/jeya-maria-jose/transweather/llms.txt Initializes the hierarchical transformer encoder (Tenc) component. ```python import torch from transweather_model import Tenc # Initialize transformer encoder with default parameters ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.