Download runtime/code/whisper_synth/src/direction2/cascade2_det3.py from edschen/eagle-u-pair-2c: direct link, hf CLI and curl.
- Browser
- Download file 7.52 kB
-
https://huggingface.co/edschen/eagle-u-pair-2c/resolve/main/runtime/code/whisper_synth/src/direction2/cascade2_det3.py
- Command line
-
hf download hf://edschen/eagle-u-pair-2c/runtime/code/whisper_synth/src/direction2/cascade2_det3.py
-
curl -L -o cascade2_det3.py https://huggingface.co/edschen/eagle-u-pair-2c/resolve/main/runtime/code/whisper_synth/src/direction2/cascade2_det3.py
7.52 kB
| """cascade2_det3 (detector-training agent, 2026-11-18): stage-1 RENDERER-ONLY detector recipe + real-data dose-response. | |
| New file. Imports cascade2_nnunet / cascade2_det2 read-only; nothing there is modified. Exposed to nnU-Net via | |
| src/nnUNet/nnunetv2/training/nnUNetTrainer/nnUNetTrainer_D2Det3.py (names nnUNetTrainer_C2D3*). | |
| Base recipe = detA (D331 warm start, ResEnc-L, FLAIR only with T1 zeroed inside the network, tolerant targets, CE + per-lesion | |
| blob Dice, component oversampling alpha 0.5 / floor 1 mL, 150 epochs at full LR, decoy-ignore via env C2_DECOY_IGNORE=1, | |
| v1 renderer 'confirmgen' online in the DA workers, GIN p 0.6 on renderer samples). Family filter = cascade2_det2._C2D2Base | |
| (GROUPS_OVERRIDE; the fold's training keys are filtered to those families, so every arm sees the same patients). | |
| REAL-DATA DOSE-RESPONSE (diagnostic for the user's question "how much real data is needed, if any"; G1 excluded throughout, | |
| fold 0 = patients of c2x_iii_f0 / c2x_iv_f0, identical 702 renderer patients): | |
| renderer only = existing nnUNetTrainer_C2DetA_iv on c2x_iv_f0 (tag detA_iv_x) | |
| C2D3_r10 = renderer 0.9 + real CONTRA 0.1 on c2x_iii_f0 (tag det3_r10_x) [this file] | |
| renderer 0.5 + real 0.5 = existing nnUNetTrainer_C2DetA_iii on c2x_iii_f0 (tag detA_iii_x, resumed) | |
| RENDERER-ONLY RECIPE (target: tiny / low-contrast lesions = the LAB gap; candidate recall ceiling is a PEAK-heat metric): | |
| C2D3_ivT renderer only + tiny-lesion supervision bundle: | |
| * MIL lesion-max term (w 0.2): mean_k -log max_{i in C_k} p_i -> pushes every lesion's PEAK heat up, which is | |
| exactly what the candidate ceiling counts (touched by a heat component >= t); | |
| * lesion-max deep-supervision targets (a coarse voxel is lesion if any fine voxel is): 10-50-voxel lesions are | |
| no longer erased at the 4x/8x heads (stage1x code audit); | |
| * component sampling floor 0.1 mL instead of 1 mL (tiny components drawn more often). | |
| C2D3_ivS renderer only + sharp targets (tolerant band outside the lesion 1 voxel instead of 3). | |
| C2D3_ivG renderer only + extreme-breadth FLAIR input (GIN p 0.9 on renders + slice-thickness simulation p 0.35, x2-6). | |
| """ | |
| from __future__ import annotations | |
| import sys as _sys | |
| from pathlib import Path | |
| _D2 = str(Path(__file__).resolve().parent) | |
| if _D2 not in _sys.path: | |
| _sys.path.insert(0, _D2) | |
| import numpy as np # noqa: E402 | |
| import torch # noqa: E402 | |
| from cascade2_det2 import _C2D2Base, _C2D2Robust, _C2Smoke, _mk_smoke_init # noqa: E402 | |
| class _C2D3Breadth(_C2D2Robust): | |
| """renderer only + EXTREME-BREADTH FLAIR input (robustness / generalisation screen): GIN on renderer samples with p GIN_CG | |
| (base 0.6) + slice-thickness / partial-volume simulation on the FLAIR channel with p ANISO_P along a random axis, factor | |
| U(ANISO_K) (cascade2_det2._C2D2Robust; label and geometry untouched).""" | |
| DATA_OPTION = "iv" | |
| GROUPS_OVERRIDE = {"cg": 1.0} | |
| GIN_CG = 0.9 | |
| ANISO_P = 0.35 | |
| ANISO_K = (2.0, 6.0) | |
| def __init__(self, plans, configuration, fold, dataset_json, device=torch.device("cuda")): | |
| super().__init__(plans, configuration, fold, dataset_json, device) | |
| self.GIN_P = dict(self.GIN_P, cg=self.GIN_CG) | |
| class _IvTMixin: | |
| """the ivT tiny-lesion bundle as a MIXIN, so it can be put on ANY stage-1 detector trainer (incl. the renderer designer's | |
| Render3 trainers) for the 2x2 renderer-dose x loss screens: MIL lesion-max term (w W_MIL) via cascade2_det2.C2D2Loss, | |
| lesion-max deep-supervision targets, component sampling floor 0.1 mL. Put it FIRST in the bases.""" | |
| W_MIL = 0.2 | |
| DS_LESION_MAX = True | |
| VOL_FLOOR = 100 | |
| def _build_loss(self): | |
| from cascade2_det2 import C2D2Loss | |
| w = None | |
| if self.enable_deep_supervision: | |
| n = len(self._get_deep_supervision_scales()) | |
| w = np.array([1 / (2 ** i) for i in range(n)]); w[-1] = 0; w = (w / w.sum()).tolist() | |
| return C2D2Loss(w, self.W_CE, self.W_BLOB, self.BLOB_MODE, self.BLOB_MARGIN, w_mil=self.W_MIL) | |
| def with_ivT(base, name, doc=""): | |
| return type(name, (_IvTMixin, base), {"__module__": __name__, "__doc__": doc or f"{base.__name__} + ivT tiny-lesion bundle"}) | |
| def _mk(name, **attrs): | |
| doc = attrs.pop("doc", "") | |
| return type(name, (_C2D2Base,), dict(attrs, __module__=__name__, __doc__=doc)) | |
| _GEN = { | |
| "nnUNetTrainer_C2D3_r10": _mk( | |
| "nnUNetTrainer_C2D3_r10", DATA_OPTION="iii", GROUPS_OVERRIDE={"cg": 0.9, "real": 0.1}, | |
| doc="dose-response: renderer 0.9 + real CONTRA 0.1 (no G1); train on c2x_iii_f0"), | |
| "nnUNetTrainer_C2D3_ivT": _mk( | |
| "nnUNetTrainer_C2D3_ivT", DATA_OPTION="iv", GROUPS_OVERRIDE={"cg": 1.0}, | |
| W_MIL=0.2, DS_LESION_MAX=True, VOL_FLOOR=100, | |
| doc="renderer only + tiny-lesion bundle (MIL 0.2, lesion-max DS, sampling floor 0.1 mL); train on c2x_iv_f0"), | |
| "nnUNetTrainer_C2D3_ivTD": _mk( | |
| "nnUNetTrainer_C2D3_ivTD", DATA_OPTION="iv", GROUPS_OVERRIDE={"cg": 1.0}, WARM="scratch", | |
| W_MIL=0.2, DS_LESION_MAX=True, VOL_FLOOR=100, | |
| doc="ivT recipe from RANDOM initialisation (no D331 warm start; isolates the init effect on top of ivT); train on c2x_iv_f0"), | |
| "nnUNetTrainer_C2D3_ivS": _mk( | |
| "nnUNetTrainer_C2D3_ivS", DATA_OPTION="iv", GROUPS_OVERRIDE={"cg": 1.0}, R_OUT=1, | |
| doc="renderer only + SHARP targets: ignore band 1 voxel outside the lesion instead of 3 (lesion-sized heat, fewer halo " | |
| "merges / offset peaks; integrator TO_AGENTS items 1 and 3); train on c2x_iv_f0"), | |
| "nnUNetTrainer_C2D3_ivTG": type("nnUNetTrainer_C2D3_ivTG", (_C2D3Breadth,), dict( | |
| W_MIL=0.2, DS_LESION_MAX=True, VOL_FLOOR=100, __module__=__name__, | |
| __doc__="ivT recipe (MIL 0.2, lesion-max DS, floor 0.1 mL) + extreme-breadth FLAIR input (GIN p 0.9, slice-thickness sim " | |
| "p 0.35 x2-6); targets the ivT robustness weak points (LAB thick slices / blur, heavy noise); train on c2x_iv_f0")), | |
| "nnUNetTrainer_C2D3_ivG": type("nnUNetTrainer_C2D3_ivG", (_C2D3Breadth,), dict( | |
| __module__=__name__, __doc__="renderer only + extreme-breadth FLAIR input (GIN p 0.9, slice-thickness sim p 0.35 x2-6)")), | |
| } | |
| # ---- 2x2 renderer-dose x loss screens at 40 epochs (fold 0), with the renderer designer (render3/DESIGNS.md T-tiny / D3-2x2) | |
| try: | |
| import cascade2_render2_nnunet as _R2N | |
| _GEN["nnUNetTrainer_C2D3_ivT_e40"] = with_ivT(_R2N._GEN["nnUNetTrainer_C2R2V1DetA_iv_e40"], "nnUNetTrainer_C2D3_ivT_e40", | |
| "v1 renderer only, 40 epochs + ivT bundle (2x2 cell: v1 renderer x ivT loss)") | |
| except Exception as _e: # pragma: no cover | |
| print(f"[cascade2_det3] render2 e40 base unavailable: {_e!r}") | |
| try: | |
| import cascade2_render3_nnunet as _R3N | |
| for _b in [k for k in _R3N._GEN if k.endswith("DetA_iv_e40") and "Smoke" not in k]: | |
| _nm = _b.replace("nnUNetTrainer_", "nnUNetTrainer_C2D3_") + "_ivT" | |
| _GEN[_nm] = with_ivT(_R3N._GEN[_b], _nm, f"{_b} (renderer designer) + ivT bundle (2x2 cell)") | |
| except Exception as _e: # pragma: no cover | |
| print(f"[cascade2_det3] render3 bases unavailable: {_e!r}") | |
| for _n, _c in list(_GEN.items()): | |
| _GEN[_n + "_Smoke"] = type(_n + "_Smoke", (_C2Smoke, _c), {"__init__": _mk_smoke_init(_c), "__module__": __name__}) | |
| globals().update(_GEN) | |