-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_no_image_head.py
More file actions
368 lines (305 loc) · 12.6 KB
/
Copy pathtrain_no_image_head.py
File metadata and controls
368 lines (305 loc) · 12.6 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
Stage 5: Train the deterministic no-image segmentation head.
Usage:
python train_no_image_head.py --config config.yaml --run_id v1
python train_no_image_head.py --config config.yaml --run_id v1 --resume
python train_no_image_head.py --config config.yaml --run_id v1 --sanity_check
"""
import argparse
import json
import logging
import sys
import uuid
from pathlib import Path
import torch
import torch.optim as optim
from torch.cuda.amp import GradScaler, autocast
from torch.utils.data import DataLoader
import yaml
import pandas as pd
from modules.checkpoint import load_checkpoint, save_checkpoint
from modules.dataset import HDF5HeadDataset, NoImageHeadDataset
from modules.head import NoImageHead
from modules.losses import CombinedLoss
from modules.metrics import dice_from_logits, iou_from_logits
from modules.wandb_utils import init_wandb
def setup_logging(log_path: Path):
log_path.parent.mkdir(parents=True, exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(log_path),
logging.StreamHandler(sys.stdout),
],
)
def compute_pos_weight(manifest_train_csv: str, device: str) -> torch.Tensor:
"""
Compute pos_weight = total_bg_pixels / total_fg_pixels from the training manifest.
This corrects for background dominance in BCE loss.
"""
df = pd.read_csv(manifest_train_csv)
total_fg = df["fg_pixels"].sum()
total_bg = df["bg_pixels"].sum()
ratio = total_bg / max(total_fg, 1)
logging.info("pos_weight = bg/fg = %.1f / %.1f = %.4f", total_bg, total_fg, ratio)
return torch.tensor([ratio], dtype=torch.float32, device=device)
def build_scheduler(optimizer, tcfg: dict, num_epochs: int):
sched_type = tcfg.get("scheduler", "cosine").lower()
if sched_type == "cosine":
return optim.lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=num_epochs,
eta_min=tcfg.get("scheduler_min_lr", 1e-6),
)
elif sched_type == "plateau":
return optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
mode="max", # maximise val IoU
patience=tcfg.get("scheduler_plateau_patience", 3),
min_lr=tcfg.get("scheduler_min_lr", 1e-6),
)
elif sched_type == "none":
return None
else:
raise ValueError(f"Unknown scheduler: {sched_type}. Choose cosine | plateau | none.")
def run_sanity_check(model, train_loader, device, cfg):
"""Overfit a tiny batch to verify the head can learn."""
logging.info("=== Sanity check: overfitting 2 batches ===")
model.train()
criterion = CombinedLoss(dice_lambda=cfg["training"]["dice_lambda"])
opt = optim.AdamW(model.parameters(), lr=1e-2)
batch = next(iter(train_loader))
x = batch["input"][:4].to(device)
y = batch["target"][:4].to(device)
for step in range(200):
opt.zero_grad()
pred = model(x)
loss = criterion(pred, y)
loss.backward()
opt.step()
if step % 50 == 0:
iou = iou_from_logits(pred.detach(), y)
logging.info(" step=%d loss=%.4f iou=%.4f", step, loss.item(), iou)
final_iou = iou_from_logits(model(x).detach(), y)
if final_iou < 0.5:
logging.error("Sanity check FAILED: final IoU=%.4f (expected >0.5)", final_iou)
sys.exit(1)
logging.info("Sanity check PASSED: final IoU=%.4f", final_iou)
def train_one_epoch(model, loader, optimizer, criterion, scaler, device, use_amp):
model.train()
total_loss = 0.0
n = 0
for batch in loader:
x = batch["input"].to(device, non_blocking=True)
y = batch["target"].to(device, non_blocking=True)
optimizer.zero_grad()
with autocast(enabled=use_amp):
pred = model(x)
loss = criterion(pred, y)
if use_amp:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
optimizer.step()
total_loss += loss.item() * x.size(0)
n += x.size(0)
return total_loss / n if n > 0 else 0.0
@torch.no_grad()
def evaluate(model, loader, criterion, device, use_amp):
model.eval()
total_loss = 0.0
total_iou = 0.0
total_dice = 0.0
n_batches = 0
for batch in loader:
x = batch["input"].to(device, non_blocking=True)
y = batch["target"].to(device, non_blocking=True)
with autocast(enabled=use_amp):
pred = model(x)
loss = criterion(pred, y)
total_loss += loss.item()
total_iou += iou_from_logits(pred, y)
total_dice += dice_from_logits(pred, y)
n_batches += 1
n = max(n_batches, 1)
return total_loss / n, total_iou / n, total_dice / n
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--config", required=True)
parser.add_argument("--run_id", default=None)
parser.add_argument("--resume", action="store_true")
parser.add_argument("--sanity_check", action="store_true")
args = parser.parse_args()
with open(args.config) as f:
cfg = yaml.safe_load(f)
project_root = Path(cfg["data"]["project_root"])
run_id = args.run_id or str(uuid.uuid4())[:8]
run_dir = project_root / "checkpoints" / "no_image" / f"run_{run_id}"
run_dir.mkdir(parents=True, exist_ok=True)
log_path = project_root / "logs" / "train_no_image.log"
setup_logging(log_path)
logging.info("Run ID: %s", run_id)
# save config snapshot
(run_dir / "config.yaml").write_text(yaml.dump(cfg))
wandb_run = init_wandb(
cfg,
job_type="train",
run_name=f"train_no_image_{run_id}",
extra_config={"run_id": run_id, "resumed": args.resume},
)
tcfg = cfg["training"]
hcfg = cfg["head"]
device = cfg["sam"].get("device", "cuda") if torch.cuda.is_available() else "cpu"
data_dir = project_root / "training_data" / "no_image"
use_hdf5 = cfg.get("hdf5", {}).get("use_hdf5", True)
nw = tcfg.get("num_workers", 4)
clip = tcfg.get("logit_clip", 20.0)
pf = tcfg.get("prefetch_factor", 2) if nw > 0 else None
h5_train, h5_val = data_dir / "train.h5", data_dir / "val.h5"
if use_hdf5 and h5_train.exists() and h5_val.exists():
logging.info("Using HDF5 datasets.")
train_ds = HDF5HeadDataset(str(h5_train), logit_clip=clip)
val_ds = HDF5HeadDataset(str(h5_val), logit_clip=clip)
# HDF5 workers open their own file handle after fork — persistent_workers is safe
persistent = nw > 0
else:
if use_hdf5 and (h5_train.exists() or h5_val.exists()):
logging.warning(
"HDF5 mode requested but not all required files exist "
"(train.h5=%s, val.h5=%s). Falling back to per-file loading. "
"Run pack_to_hdf5.py for all splits first.",
h5_train.exists(), h5_val.exists(),
)
logging.info("HDF5 not found — falling back to per-file .npy loading.")
target_size = tuple(tcfg.get("target_size", [256, 256]))
train_ds = NoImageHeadDataset(str(data_dir / "train_index.csv"), logit_clip=clip, target_size=target_size)
val_ds = NoImageHeadDataset(str(data_dir / "val_index.csv"), logit_clip=clip, target_size=target_size)
persistent = False # npy workers accumulate memory
loader_kwargs = dict(num_workers=nw, pin_memory=True, persistent_workers=persistent)
if nw > 0 and pf:
loader_kwargs["prefetch_factor"] = pf
train_loader = DataLoader(train_ds, batch_size=tcfg["batch_size"], shuffle=True, **loader_kwargs)
val_loader = DataLoader(val_ds, batch_size=tcfg["batch_size"], shuffle=False, **loader_kwargs)
# model
model = NoImageHead(
channels_in=hcfg.get("channels_in", 3),
hidden_channels=hcfg.get("hidden_channels", 16),
).to(device)
n_params = sum(p.numel() for p in model.parameters())
logging.info("Model: NoImageHead (%d params)", n_params)
wandb_run.log({"model/n_params": n_params,
"model/n_params_final_conv": sum(p.numel() for p in model.final_conv.parameters())})
# sanity check before full training
if args.sanity_check:
run_sanity_check(model, train_loader, device, cfg)
logging.info("Sanity check done. Exiting.")
wandb_run.finish()
return
optimizer = optim.AdamW(model.parameters(), lr=tcfg["lr"], weight_decay=tcfg["weight_decay"])
scaler = GradScaler(enabled=tcfg.get("amp", True))
# pos_weight for class-imbalance correction
pos_weight = compute_pos_weight(
str(project_root / "manifests" / "train.csv"), device
)
criterion = CombinedLoss(dice_lambda=tcfg["dice_lambda"], pos_weight=pos_weight)
wandb_run.log({"train/pos_weight": pos_weight.item()})
num_epochs = tcfg["num_epochs"]
scheduler = build_scheduler(optimizer, tcfg, num_epochs)
start_epoch = 0
best_val_iou = 0.0
patience_counter = 0
# watch model gradients/params
try:
import wandb as _wandb
if _wandb.run is not None:
_wandb.watch(model, log="all", log_freq=50)
except Exception:
pass
if args.resume:
state = load_checkpoint(run_dir, model, optimizer, scheduler=scheduler, scaler=scaler)
start_epoch = state["epoch"] + 1
best_val_iou = state["best_val_iou"]
logging.info("Resuming from epoch %d", start_epoch)
use_amp = tcfg.get("amp", True) and device != "cpu"
patience = tcfg.get("early_stop_patience", 5)
sched_type = tcfg.get("scheduler", "cosine").lower()
for epoch in range(start_epoch, num_epochs):
train_loss = train_one_epoch(model, train_loader, optimizer, criterion, scaler, device, use_amp)
val_loss, val_iou, val_dice = evaluate(model, val_loader, criterion, device, use_amp)
# step scheduler
if scheduler is not None:
if sched_type == "plateau":
scheduler.step(val_iou)
else:
scheduler.step()
is_best = val_iou > best_val_iou
if is_best:
best_val_iou = val_iou
patience_counter = 0
else:
patience_counter += 1
current_lr = optimizer.param_groups[0]["lr"]
is_best_str = "[BEST]" if is_best else f"[patience {patience_counter}/{patience}]"
logging.info(
"Epoch %3d/%d train_loss=%.4f val_loss=%.4f val_iou=%.4f val_dice=%.4f lr=%.2e %s",
epoch + 1, num_epochs, train_loss, val_loss, val_iou, val_dice, current_lr, is_best_str,
)
wandb_run.log({
"epoch": epoch + 1,
"train/loss": train_loss,
"val/loss": val_loss,
"val/iou": val_iou,
"val/dice": val_dice,
"val/best_iou": best_val_iou,
"train/patience_counter": patience_counter,
"train/lr": current_lr,
})
save_checkpoint(
run_dir=run_dir,
model=model,
optimizer=optimizer,
scheduler=scheduler,
scaler=scaler,
epoch=epoch,
best_val_iou=best_val_iou,
is_best=is_best,
)
if patience_counter >= patience:
logging.info("Early stopping triggered after %d epochs without improvement.", patience)
break
wandb_run.log({
"summary/best_val_iou": best_val_iou,
"summary/epochs_run": epoch + 1,
})
# log best checkpoint as a wandb artifact
try:
import wandb as _wandb
if _wandb.run is not None:
artifact = _wandb.Artifact(
name=f"no_image_head_{run_id}",
type="model",
description="Best NoImageHead checkpoint",
metadata={"best_val_iou": best_val_iou, "run_id": run_id},
)
artifact.add_file(str(run_dir / "best.pt"))
_wandb.log_artifact(artifact)
except Exception as e:
logging.warning("Could not log model artifact to wandb: %s", e)
wandb_run.finish()
logging.info("Training complete. Best val IoU: %.4f", best_val_iou)
_write_training_report(project_root, run_id, best_val_iou, epoch + 1)
def _write_training_report(project_root, run_id, best_val_iou, epochs_run):
report_dir = project_root / "reports"
report_dir.mkdir(parents=True, exist_ok=True)
lines = [
"# Training Summary: No-Image Head\n",
f"- Run ID: {run_id}",
f"- Epochs run: {epochs_run}",
f"- Best validation IoU: {best_val_iou:.4f}",
]
(report_dir / "training_summary_no_image.md").write_text("\n".join(lines))
if __name__ == "__main__":
main()