Spaces:
Running
Running
File size: 5,192 Bytes
f93c1b8 | 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 | #!/usr/bin/env python3
"""Train on a real, openly-available histopathology dataset (no manual download).
Uses MedMNIST's PathMNIST (100k+ real colorectal H&E image tiles, 9 tissue
classes, auto-downloaded from Zenodo). We binarise it to the task the app uses:
tumour = colorectal adenocarcinoma epithelium (class 8)
normal = everything else
It trains a ResNet18, reports honest held-out AUROC + calibration (ECE), and
saves a checkpoint the app loads directly:
PATHQA_MODEL=model.pt ./pathqa-app (or set it in your host)
Run on a GPU (Colab is free). CPU works but is slow.
pip install medmnist
python train_open.py --epochs 3 --out model.pt
# quick test: python train_open.py --epochs 1 --subset 4000 --out model.pt
For the closer-to-real PatchCamelyon set instead, use train_pcam.py.
"""
from __future__ import annotations
import argparse
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset, Subset
from torchvision import transforms
from pathqa.data import IMAGENET_MEAN, IMAGENET_STD
from pathqa.engine import (evaluate, expected_calibration_error, fit_temperature,
train_one_epoch)
from pathqa.model import TemperatureScaler, build_model
TUMOUR_CLASS = 8 # PathMNIST: colorectal adenocarcinoma epithelium (TUM)
class BinaryPathMNIST(Dataset):
"""PathMNIST wrapped as binary tumour(=1)/normal(=0), resized to 96px."""
def __init__(self, split: str, train: bool):
from medmnist import PathMNIST
aug = [transforms.RandomHorizontalFlip(), transforms.RandomVerticalFlip()] if train else []
self.tf = transforms.Compose([transforms.Resize(96), *aug,
transforms.ToTensor(),
transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
self.ds = PathMNIST(split=split, download=True, as_rgb=True, size=28)
def __len__(self):
return len(self.ds)
def __getitem__(self, i):
img, y = self.ds[i] # PIL image, label array shape (1,)
label = 1 if int(y[0]) == TUMOUR_CLASS else 0
return self.tf(img), label
def loaders(batch_size, subset):
tr, va, te = (BinaryPathMNIST("train", True), BinaryPathMNIST("val", False),
BinaryPathMNIST("test", False))
if subset:
tr = Subset(tr, range(min(subset, len(tr))))
va = Subset(va, range(min(subset // 4 or 1, len(va))))
te = Subset(te, range(min(subset // 4 or 1, len(te))))
return (DataLoader(tr, batch_size, shuffle=True, num_workers=2),
DataLoader(va, batch_size, num_workers=2),
DataLoader(te, batch_size, num_workers=2))
@torch.no_grad()
def _probs_labels(model, loader, device, scaler=None):
model.eval(); ps, ys = [], []
for x, y in loader:
lo = model(x.to(device))
if scaler is not None:
lo = scaler(lo)
ps.append(F.softmax(lo, 1)[:, 1].cpu()); ys.append(y)
return torch.cat(ps), torch.cat(ys)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--epochs", type=int, default=3)
ap.add_argument("--lr", type=float, default=1e-4)
ap.add_argument("--batch-size", type=int, default=128)
ap.add_argument("--subset", type=int, default=None, help="cap samples per split for a quick run")
ap.add_argument("--out", default="model.pt")
args = ap.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"[train_open] device={device} dataset=PathMNIST (binarised tumour vs rest)")
train_dl, val_dl, test_dl = loaders(args.batch_size, args.subset)
model = build_model("resnet18", pretrained=True).to(device)
optim = torch.optim.Adam(model.parameters(), lr=args.lr)
best, best_state = -1.0, None
for ep in range(args.epochs):
loss = train_one_epoch(model, train_dl, optim, device)
res = evaluate(model, val_dl, device)
print(f"[epoch {ep+1}/{args.epochs}] loss={loss:.3f} val_acc={res.accuracy:.3f} val_auroc={res.auroc:.3f}")
if res.auroc >= best:
best, best_state = res.auroc, {k: v.cpu().clone() for k, v in model.state_dict().items()}
if best_state:
model.load_state_dict(best_state)
T = fit_temperature(model, val_dl, device)
scaler = TemperatureScaler(); scaler.set_temperature(T)
p, y = _probs_labels(model, test_dl, device, scaler)
test = evaluate(model, test_dl, device, scaler=scaler)
ece = expected_calibration_error(p, y)
print("\n=== Held-out test metrics (real data) ===")
print(f" accuracy {test.accuracy:.3f} | AUROC {test.auroc:.3f} | ECE {ece:.3f} | T {T:.3f} | n {test.n}")
torch.save({"state_dict": model.state_dict(), "backbone": "resnet18", "temperature": T,
"labels": {0: "normal", 1: "tumor"},
"metrics": {"test_auroc": test.auroc, "test_acc": test.accuracy, "ece": ece},
"note": "Pathify model trained on PathMNIST (open data). QA aid, not a diagnosis."},
args.out)
print(f"[saved] {args.out} -> run with PATHQA_MODEL={args.out}")
if __name__ == "__main__":
main()
|