File size: 7,318 Bytes
8fca640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""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