-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
156 lines (122 loc) · 6.53 KB
/
train.py
File metadata and controls
156 lines (122 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, random_split
from data_pipeline import download_pexels_sky, SkyEnhancementDataset
from unet import LightUNet
from tqdm import tqdm
import os
# ── Hyperparameters ──────────────────────────────────────────
BATCH_SIZE = 32 # large batch fine at 256px
EPOCHS = 200
EARLY_STOP_PATIENCE = 15 # Stop training if validation loss doesn't improve for this many epochs
LEARNING_RATE = 1e-4
NUM_IMAGES = 8000
IMAGE_SIZE = 256 # fast — upgrade to 512 only after confirming quality
IMAGE_DIR = "sky_images"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
NUM_WORKERS = 0 # Windows: spawning workers is slower than main process
# ─────────────────────────────────────────────────────────────
def total_variation_loss(img):
tv_h = torch.mean(torch.abs(img[:, :, 1:, :] - img[:, :, :-1, :]))
tv_w = torch.mean(torch.abs(img[:, :, :, 1:] - img[:, :, :, :-1]))
return tv_h + tv_w
def ssim_loss(pred, target, window_size=11):
C1, C2 = 0.01 ** 2, 0.03 ** 2
mu_p = torch.nn.functional.avg_pool2d(pred, window_size, 1, window_size // 2)
mu_t = torch.nn.functional.avg_pool2d(target, window_size, 1, window_size // 2)
mu_p2, mu_t2, mu_pt = mu_p * mu_p, mu_t * mu_t, mu_p * mu_t
sigma_p2 = torch.nn.functional.avg_pool2d(pred * pred, window_size, 1, window_size // 2) - mu_p2
sigma_t2 = torch.nn.functional.avg_pool2d(target * target, window_size, 1, window_size // 2) - mu_t2
sigma_pt = torch.nn.functional.avg_pool2d(pred * target, window_size, 1, window_size // 2) - mu_pt
ssim_map = ((2 * mu_pt + C1) * (2 * sigma_pt + C2)) / \
((mu_p2 + mu_t2 + C1) * (sigma_p2 + sigma_t2 + C2))
return 1 - ssim_map.mean()
def saturation_reg_loss(pred, target):
neutral = 0.502
pred_chroma = torch.sqrt((pred[:, 1] - neutral) ** 2 + (pred[:, 2] - neutral) ** 2 + 1e-6)
target_chroma = torch.sqrt((target[:, 1] - neutral) ** 2 + (target[:, 2] - neutral) ** 2 + 1e-6)
return torch.clamp(pred_chroma - target_chroma, min=0).mean()
def combined_loss(pred, target):
l1 = nn.L1Loss()(pred, target)
ssim = ssim_loss(pred, target)
tv = total_variation_loss(pred)
sat = saturation_reg_loss(pred, target)
# Increased TV loss for better smoothness (anti-grain) during training
return l1 + (1.0 * ssim) + (0.5 * tv) + (0.5 * sat)
def train_model():
print(f"Using device: {DEVICE}")
filepaths = download_pexels_sky(num_images=NUM_IMAGES, save_dir=IMAGE_DIR)
if len(filepaths) == 0:
raise RuntimeError("No images found! Check your Pexels API key.")
full_dataset = SkyEnhancementDataset(filepaths, image_size=IMAGE_SIZE)
val_size = max(1, int(0.1 * len(full_dataset)))
train_size = len(full_dataset) - val_size
train_dataset, val_dataset = random_split(full_dataset, [train_size, val_size])
print(f"Train: {train_size} | Val: {val_size}")
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE,
shuffle=True, num_workers=NUM_WORKERS,
pin_memory=(DEVICE == "cuda"))
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE,
shuffle=False, num_workers=NUM_WORKERS,
pin_memory=(DEVICE == "cuda"))
model = LightUNet().to(DEVICE)
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="min",
patience=4, factor=0.5)
scaler = torch.cuda.amp.GradScaler(enabled=(DEVICE == "cuda"))
if DEVICE == "cuda":
torch.backends.cudnn.benchmark = True
os.makedirs("checkpoints", exist_ok=True)
best_val_loss = float("inf")
early_stop_counter = 0
for epoch in range(EPOCHS):
# ── Train ──
model.train()
train_loss = 0
loop = tqdm(train_loader, desc=f"Epoch [{epoch+1}/{EPOCHS}]", leave=False)
for x_dull, y_perfect in loop:
x_dull = x_dull.to(DEVICE, non_blocking=True)
y_perfect = y_perfect.to(DEVICE, non_blocking=True)
with torch.cuda.amp.autocast(enabled=(DEVICE == "cuda")):
predictions = model(x_dull)
loss = combined_loss(predictions, y_perfect)
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
train_loss += loss.item()
loop.set_postfix(loss=f"{loss.item():.4f}")
avg_train = train_loss / len(train_loader)
# ── Validate ──
model.eval()
val_loss = 0
with torch.no_grad():
for x_dull, y_perfect in val_loader:
x_dull = x_dull.to(DEVICE, non_blocking=True)
y_perfect = y_perfect.to(DEVICE, non_blocking=True)
with torch.cuda.amp.autocast(enabled=(DEVICE == "cuda")):
val_loss += combined_loss(model(x_dull), y_perfect).item()
avg_val = val_loss / len(val_loader)
prev_lr = optimizer.param_groups[0]["lr"]
scheduler.step(avg_val)
new_lr = optimizer.param_groups[0]["lr"]
if new_lr != prev_lr:
print(f" ↓ LR reduced: {prev_lr:.2e} → {new_lr:.2e}")
print(f"Epoch {epoch+1:02d}/{EPOCHS} | Train: {avg_train:.4f} | Val: {avg_val:.4f}")
torch.save(model.state_dict(), f"checkpoints/sky_unet_epoch_{epoch+1}.pth")
if avg_val < best_val_loss:
best_val_loss = avg_val
early_stop_counter = 0
torch.save(model.state_dict(), "checkpoints/sky_unet_best.pth")
print(f" ✓ Best model saved (val: {best_val_loss:.4f})")
else:
early_stop_counter += 1
print(f" - No improvement in validation loss for {early_stop_counter} epoch(s)")
if early_stop_counter >= EARLY_STOP_PATIENCE:
print(f"\n[!] Early stopping triggered. Validation loss hasn't improved for {EARLY_STOP_PATIENCE} epochs.")
break
print(f"\nDone! Best val loss: {best_val_loss:.4f}")
print("Best model → checkpoints/sky_unet_best.pth")
if __name__ == "__main__":
train_model()