""" Training protocol shared by the baseline and ablation runs, and the variants. phase 1: weighted MSE + soft Dice (lambda_rec 100), lr 1e-4 phase 2: RME (lambda_rec 10) + traction-stress consistency (lambda_force 100), lr 1e-5, starting from the best phase-1 generator """ import copy PROTOCOL = dict( train_manifest="alltune_train", val_manifest="alltune_val", phases=[ dict(name="phase1", epochs=100, rec_loss="wmd", lambda_rec=100.0, lambda_force=0.0, g_lr=1e-4, d_lr=1e-4), dict(name="phase2", epochs=40, rec_loss="rme", lambda_rec=10.0, lambda_force="from_variant", g_lr=1e-5, d_lr=1e-5), ], batch_size=2, img_size=1024, augment=True, beta1=0.5, beta2=0.999, warmup_frac=0.1, lambda_gan=1.0, lambda_force=100.0, arch="s2f", in_channels=3, use_multi_scale_input=True, bridge="cbam", attention_gates=True, substrate_mode="legacy", input_mode="bf", seed=0, ) VARIANTS = { # reference "s2f_full": dict(), # baselines "unet": dict(arch="unet", lambda_gan=0.0), "unet_gan": dict(arch="unet"), "mask_only": dict(input_mode="mask"), # loss ablations "s2f_no_gan": dict(lambda_gan=0.0), "s2f_no_force": dict(lambda_force=0.0, share_phase1_with="s2f_full"), "s2f_gan_0.1": dict(lambda_gan=0.1), "s2f_gan_5": dict(lambda_gan=5.0), # input ablations "s2f_no_substrate": dict(in_channels=1, substrate_mode="none"), "s2f_nominal_substrate": dict(substrate_mode="nominal"), # architecture ablations "s2f_no_multiscale": dict(use_multi_scale_input=False), "s2f_bridge_no_cbam": dict(bridge="cnn"), "s2f_bridge_plain": dict(bridge="plain"), "s2f_no_attention_gates": dict(attention_gates=False), # bright-field only "unet_no_substrate": dict(arch="unet", lambda_gan=0.0, in_channels=1, substrate_mode="none"), "unet_gan_no_substrate": dict(arch="unet", in_channels=1, substrate_mode="none"), "s2f_no_substrate_no_gan": dict(in_channels=1, substrate_mode="none", lambda_gan=0.0), "s2f_no_substrate_no_multiscale": dict(in_channels=1, substrate_mode="none", use_multi_scale_input=False), "s2f_no_substrate_bridge_plain": dict(in_channels=1, substrate_mode="none", bridge="plain"), "s2f_no_substrate_no_attention_gates": dict(in_channels=1, substrate_mode="none", attention_gates=False), # substrate channels added in phase 2 (phase 1 shared with s2f_no_substrate) "s2f_staged": dict(share_phase1_with="s2f_no_substrate", expand_from_image_only=True), "s2f_staged_no_force": dict(share_phase1_with="s2f_no_substrate", expand_from_image_only=True, lambda_force=0.0), } PRIORITY = ["s2f_full", "unet", "unet_gan", "s2f_no_gan", "s2f_no_substrate", "s2f_no_force", "s2f_no_attention_gates", "s2f_no_multiscale", "s2f_bridge_plain", "s2f_bridge_no_cbam", "s2f_nominal_substrate", "s2f_gan_0.1", "s2f_gan_5"] def get_config(variant, epochs=None, g_lr=None, d_lr=None, **overrides): """epochs / g_lr / d_lr override every phase (smoke tests); other overrides are top-level keys.""" if variant not in VARIANTS: raise KeyError(f"unknown variant '{variant}'. Options: {', '.join(VARIANTS)}") cfg = copy.deepcopy(PROTOCOL) cfg.update(VARIANTS[variant]) cfg.update({k: v for k, v in overrides.items() if v is not None}) cfg["variant"] = variant if cfg["substrate_mode"] == "none": cfg["in_channels"] = 1 for ph in cfg["phases"]: if ph["lambda_force"] == "from_variant": ph["lambda_force"] = cfg["lambda_force"] for k, v in (("epochs", epochs), ("g_lr", g_lr), ("d_lr", d_lr)): if v is not None: ph[k] = v cfg["epochs"] = sum(ph["epochs"] for ph in cfg["phases"]) # total, used to detect finished runs return cfg