"""Model construction and device-specific output selection. Device is used only to select supervised/output heads. The convolutional network receives OCT pixels, and its external boundary vocabulary stays anatomical. Legacy checkpoints use one shared head per anatomical code. """ from src.models.unet import BoundaryNet import torch from torch.nn import functional as F def output_indices(config, device): keys = config["device_keys"][device] if "device_output_indices" in config: indices = config["device_output_indices"][device] labels = config["model_head_keys"] if len(indices) != len(keys) or len(set(indices)) != len(indices): raise ValueError("Invalid device output index mapping") if any(labels[i] != device + "::" + key for i, key in zip(indices, keys)): raise ValueError("Device heads do not match the external vocabulary") return indices return [config["boundary_keys"].index(k) for k in keys] def configure_heads(config, mode="shared"): config["head_mode"] = mode if mode == "device": labels, indices = [], {} for device, keys in sorted(config["device_keys"].items()): indices[device] = list(range(len(labels), len(labels) + len(keys))) labels.extend(device + "::" + key for key in keys) config["model_head_keys"] = labels config["device_output_indices"] = indices elif mode != "shared": raise ValueError("Unknown output head mode") for device in config["device_keys"]: output_indices(config, device) return config def build_model(config): n = len(config.get("model_head_keys", config["boundary_keys"])) if config.get("architecture") == "BoundaryNetWidePrior": from src.models.wide import WideBoundaryNet return WideBoundaryNet(n, config["base"], config["height"]) if config.get("architecture") == "BoundaryNetGlobalPrior": from src.models.global_context import GlobalBoundaryNet return GlobalBoundaryNet(n, config["base"], config["height"]) if config.get("architecture") == "BoundaryNetAuxiliaryPrior": from src.models.auxiliary import AuxiliaryBoundaryNet return AuxiliaryBoundaryNet( n, config["base"], config["height"], len(config["auxiliary_region_names"]) ) if config.get("architecture") in ["BoundaryNetContext", "BoundaryNetContextPrior"]: from src.models.context import ContextBoundaryNet return ContextBoundaryNet( n, config["base"], ( config["height"] if config["architecture"] == "BoundaryNetContextPrior" else None ), ) if config.get("architecture") == "BoundaryNetAxialPrior": from src.models.axial_prior import AxialPriorNet return AxialPriorNet(n, config["base"], config["height"]) if config.get("architecture", "BoundaryNet") != "BoundaryNet": raise ValueError("Unknown architecture") return BoundaryNet(n, config["base"]) def remap_state(state, source_config, target_config): """Duplicate this researcher's shared trained heads for a device-head run. Every backbone tensor is retained. No weights from external assets are used. Returning a remapping flag lets training explicitly reset optimizer state. """ old = source_config.get("model_head_keys", source_config["boundary_keys"]) new = target_config.get("model_head_keys", target_config["boundary_keys"]) result = dict(state) changed = False indices = None if old != new: if "model_head_keys" in source_config or "model_head_keys" not in target_config: raise ValueError( "Only shared-to-device trained-head remapping is supported" ) indices = [old.index(label.split("::", 1)[1]) for label in new] for name in ["head.weight", "head.bias"]: result[name] = state[name][indices].clone() changed = True target_prior = target_config.get("architecture") in [ "BoundaryNetAxialPrior", "BoundaryNetContextPrior", "BoundaryNetAuxiliaryPrior", "BoundaryNetGlobalPrior", "BoundaryNetWidePrior", ] if target_prior: if "axial_prior" not in state: result["axial_prior"] = state["head.weight"].new_zeros( 1, len(new), target_config["height"], 1 ) changed = True else: prior = state["axial_prior"] if indices is not None: prior = prior[:, indices].clone() if prior.shape[-2] != target_config["height"]: prior = F.interpolate( prior, size=(target_config["height"], 1), mode="bilinear", align_corners=False, ) changed = True result["axial_prior"] = prior elif "axial_prior" in state: raise ValueError( "Dropping a trained axial prior is not an implicit resume operation" ) channels = ( 3 if target_config.get("architecture") in ["BoundaryNetGlobalPrior", "BoundaryNetWidePrior"] else target_config.get("input_channels", 1) ) weight = result["down.0.net.0.weight"] if weight.shape[1] != channels: if weight.shape[1] != 1 or channels != 3: raise ValueError("Unsupported input-channel resume mapping") result["down.0.net.0.weight"] = weight.repeat(1, 3, 1, 1) / 3 changed = True if target_config.get("architecture") == "BoundaryNetAuxiliaryPrior": regions = len(target_config["auxiliary_region_names"]) if "auxiliary_head.weight" not in state: result["auxiliary_head.weight"] = state["head.weight"].new_zeros( regions, target_config["base"], 1, 1 ) result["auxiliary_head.bias"] = state["head.weight"].new_zeros(regions) changed = True elif "auxiliary_head.weight" in state: raise ValueError( "Dropping an auxiliary head is not an implicit resume operation" ) if target_config.get("architecture") == "BoundaryNetGlobalPrior": with torch.random.fork_rng(devices=[]): torch.manual_seed(target_config["global_context"]["initialization_seed"]) initial = build_model(target_config).state_dict() for name, value in initial.items(): if ( name.startswith(("global_encoder.", "global_film.")) and name not in state ): result[name] = value changed = True elif any(name.startswith(("global_encoder.", "global_film.")) for name in state): raise ValueError("Dropping trained global context is not implicit") if target_config.get("architecture") == "BoundaryNetWidePrior": if "outer_conv.weight" not in state: result["outer_conv.weight"] = state["head.weight"].new_zeros( target_config["base"], 2, 3, 3 ) changed = True elif "outer_conv.weight" in state: raise ValueError("Dropping trained distant-view inputs is not implicit") return result, changed