"""Model definitions, class labels and weight loading for LeukoAI v1. The three models are DenseNet-121 classifiers recovered from the original desktop application (toolv9, 2023). Weights in ``weights/`` are the original checkpoints with training state removed; predictions are unchanged. """ from __future__ import annotations import hashlib import os from dataclasses import dataclass import torch from torchvision.models import densenet121 # Class order exactly as in the original application. DX1 = ["NO LEUKEMIA", "LEUKEMIA"] DX2 = ["ACUTE LEUKEMIA", "CHRONIC LEUKEMIA", "NO LEUKEMIA", "REACTIVE CELLS"] DX3 = ["ALL", "AML", "CLL", "CML", "NO LEUKEMIA"] @dataclass(frozen=True) class ModelSpec: key: str file: str original_file: str labels: tuple sha256: str SPECS = ( ModelSpec("m1", "m1_leukemia_binary.pt", "del_new_bin_flip_jit_scale.ckpt", tuple(DX1), "e5b65eb8b31a187908faf77867949cfa8f70a08b29c956458a0f59d42f14c873"), ModelSpec("m2", "m2_acute_chronic_normal_reactive.pt", "del_new_acnr_flip_scale_jit.ckpt", tuple(DX2), "304f436254a084d3921daaca41bbeb0ed9c8a712c6b7389ded779c49075a3a05"), ModelSpec("m3", "m3_subtype_5class.pt", "new_five.ckpt", tuple(DX3), "78b520597a19d03647815b0ae332630f4fdc790b0c0475afacce5d804cdd21a9"), ) DEFAULT_WEIGHTS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "weights") def _sha256(path: str) -> str: h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def _load_state_dict(spec: ModelSpec, weights_dir: str, verify: bool) -> dict: slim = os.path.join(weights_dir, spec.file) if os.path.exists(slim): if verify and _sha256(slim) != spec.sha256: raise ValueError(f"Checksum mismatch for {slim}; the weight file is not the released LeukoAI model.") return torch.load(slim, map_location="cpu", weights_only=True) original = os.path.join(weights_dir, spec.original_file) if os.path.exists(original): # original toolv9 Lightning checkpoint sd = torch.load(original, map_location="cpu", weights_only=True)["state_dict"] return {k.replace("net.", "", 1): v for k, v in sd.items()} raise FileNotFoundError(f"Neither {spec.file} nor {spec.original_file} found in {weights_dir}") def load_models(weights_dir: str | None = None, device: str | None = None, verify: bool = True): """Return ``{"m1": model, "m2": model, "m3": model}`` in eval mode.""" weights_dir = weights_dir or DEFAULT_WEIGHTS_DIR device = device or ("cuda" if torch.cuda.is_available() else "cpu") models = {} for spec in SPECS: m = densenet121(num_classes=len(spec.labels)) m.load_state_dict(_load_state_dict(spec, weights_dir, verify), strict=True) models[spec.key] = m.eval().to(device) return models