Views
No views yet

model/: Contains the core UNet and PhysMamba blocks.configs/: Model configuration files.utils.py: Contains the utility logic for model inference.data/: Data processing and re-standardization utilities.checkpoints/: Directory for model weights (pytorch_model.bin)
(Please put your weights inside this directory)test_imgs/: Sample images for testing.1import torch
2import os
3from PIL import Image
4from model import FM_PhysMamba_UNET, ODESolver
5from utils import predict_large_image_vectorized, preprocess_single_image
6from data.utils import restandardize_tensor
7import matplotlib.pyplot as plt
8
9# ==========================================
10# CONFIGURATION
11# ==========================================
12# REPLACE THIS with your actual weight file path
13WEIGHT_PATH = "checkpoints/final_weights/DENSE_HAZE/pytorch_model.bin"
14
15INPUT_IMG_PATH = "test_imgs/test_images.jpeg"
16OUTPUT_DIR = "test_imgs/results"
17DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
18
19def load_dehazing_model(weight_path, device):
20 """Initializes model and loads pre-trained weights."""
21 print(f"Loading model to {device}...")
22 model = FM_PhysMamba_UNET("small").to(device)
23
24 if not os.path.exists(weight_path):
25 raise FileNotFoundError(f"Weights not found at: {weight_path}")
26
27 checkpoint = torch.load(weight_path, map_location=device, weights_only=True)
28 model.load_state_dict(checkpoint)
29 model.eval()
30 return model
31
32def run_inference(model, img_path, device, output_dir):
33 """Performs tiled inference and saves comparison result."""
34 solver = ODESolver(model)
35 raw_img = Image.open(img_path).convert("RGB")
36 input_tensor = preprocess_single_image(raw_img, device=device)
37
38 print(f"Starting tiled inference for {img_path}...")
39 with torch.no_grad():
40 restored_tensor = predict_large_image_vectorized(
41 solver=solver,
42 full_img_tensor=input_tensor,
43 device=device,
44 tile_size=256,
45 overlap_ratio=0.25
46 )
47
48 # Process for visualization
49 hazy_disp = restandardize_tensor(input_tensor.detach().squeeze(0).cpu()).permute(1, 2, 0).numpy()
50 restored_disp = restandardize_tensor(restored_tensor.detach().squeeze(0).cpu()).permute(1, 2, 0).numpy()
51
52 # Create Comparison Plot
53 os.makedirs(output_dir, exist_ok=True)
54 fig, axes = plt.subplots(1, 2, figsize=(15, 7))
55 axes[0].imshow(hazy_disp)
56 axes[0].set_title("Original Hazy Input")
57 axes[0].axis("off")
58
59 axes[1].imshow(restored_disp)
60 axes[1].set_title("FM-PhysMamba Restored")
61 axes[1].axis("off")
62
63 save_path = os.path.join(output_dir, "comparison_result.png")
64 plt.savefig(save_path, dpi=300, bbox_inches='tight')
65 print(f"Success! Result saved to: {save_path}")
66 plt.show()
67
68if __name__ == "__main__":
69 # 1. Load the model once
70 my_model = load_dehazing_model(WEIGHT_PATH, DEVICE)
71
72 # 2. Run inference
73 run_inference(my_model, INPUT_IMG_PATH, DEVICE, OUTPUT_DIR)1
2# Install dependencies into a new virtual environment
3uv sync
4
5# Activate the environment
6source .venv/bin/activate