File size: 11,256 Bytes
bf88397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""Whole-volume refinement, frozen teacher features, and geometric queries."""

import numpy as np
import torch
from torch import nn
from torch.nn import functional as F

from src.inference.density import PLAIN
from src.models.unet import probability_median


def teacher_components(net, cfg):
    if cfg.get("tta_horizontal", False):
        raise ValueError("Volume teacher must disable horizontal TTA")
    if cfg["architecture"] in PLAIN:
        if cfg.get("decode_method") != "posterior_median":
            raise ValueError("Volume teacher needs posterior-median decoding")
        return [(net, cfg, 1.0)]
    if (
        cfg["architecture"] == "boundary_ensemble_v1"
        and cfg.get("reduce") == "probability_mean"
        and not cfg.get("density_native_columns", False)
    ):
        components = list(zip(net.nets, cfg["components"]))
        if any(
            c["config"]["architecture"] not in PLAIN
            or c["config"].get("tta_horizontal", False)
            or c["config"].get("prediction_policy")
            or c.get("post_policy")
            for _, c in components
        ):
            raise ValueError("Unsupported volume probability component")
        return [(m, c["config"], c["weight"]) for m, c in components]
    raise ValueError("Unsupported frozen volume teacher")


def feature_channels(cfg, family):
    components = (
        cfg["components"]
        if cfg["architecture"] == "boundary_ensemble_v1"
        else [{"config": cfg}]
    )
    return 26 * sum(c["config"]["base"] for c in components) + 4 * len(
        cfg["boundary_keys"][family]
    )


def normalized_positive(probability):
    positive = probability[:, :, 1:, :]
    mass = positive.sum(2, keepdim=True)
    return torch.where(
        mass > 1e-12,
        positive / mass.clamp_min(1e-12),
        torch.full_like(positive, 1 / positive.shape[2]),
    )


@torch.inference_mode()
def teacher_forward(
    net, cfg, x, family, feature_width=32, output_width=512, return_probability=False
):
    """Read image activations and predictions; no supplied label values enter."""
    radius = cfg.get("context_radius", 0)
    hidden = []
    mixture = None
    if x.shape[1] != 2 * radius + 1:
        raise ValueError("Teacher image context mismatch")
    for model, child, weight in teacher_components(net, cfg):
        r = child.get("context_radius", 0)
        part = x[:, radius - r : radius + r + 1]
        captures = {}
        base = child["base"]

        def deep_hook(module, inputs):
            captures["deep"] = inputs[0][:, : 12 * base]

        def fine_hook(module, inputs):
            captures["fine"] = inputs[0]

        handles = [
            model.decoder[0].register_forward_pre_hook(deep_hook),
            model.heads[family].register_forward_pre_hook(fine_hook),
        ]
        try:
            logits = model(part, family)
        finally:
            for handle in handles:
                handle.remove()
        if captures["deep"].shape[1] != 12 * base or captures["fine"].shape[1] != base:
            raise ValueError("Unexpected teacher activation geometry")
        fields = []
        for key in ["fine", "deep"]:
            value = captures[key].float()
            stats = torch.cat([value.mean(2), value.amax(2)], dim=1)
            fields.append(
                F.interpolate(
                    stats, size=feature_width, mode="linear", align_corners=True
                )
            )
        hidden.append(torch.cat(fields, dim=1))
        p = logits.float().softmax(2) * float(weight)
        mixture = p if mixture is None else mixture + p
    full = probability_median(mixture)
    positive = probability_median(normalized_positive(mixture)) + 1
    p0 = mixture[:, :, 0, :]
    endpoint = torch.logit(p0.clamp(1e-6, 1 - 1e-6))
    entropy = -(mixture * mixture.clamp_min(1e-12).log()).sum(2) / np.log(
        max(mixture.shape[2], 2)
    )
    resize = lambda v: F.interpolate(
        v, size=feature_width, mode="linear", align_corners=True
    )
    stats = [
        resize(full) / (cfg["height"] - 1),
        resize(positive) / (cfg["height"] - 1),
        resize(p0),
        resize(entropy),
    ]
    features = torch.cat(hidden + stats, dim=1).to(torch.float16)
    if (
        features.shape[1] != feature_channels(cfg, family)
        or not torch.isfinite(features).all()
    ):
        raise ValueError("Invalid image-derived volume features")
    return (
        features,
        F.interpolate(positive, size=output_width, mode="linear", align_corners=True),
        F.interpolate(endpoint, size=output_width, mode="linear", align_corners=True),
        mixture if return_probability else None,
    )


def canonical_features(features, frame_fraction, frames=16):
    """Interpolate sampled encoded B locations onto a fixed geometric grid."""
    values = np.asarray(features, dtype="float32")
    fraction = np.asarray(frame_fraction, dtype="float64")
    if (
        values.ndim != 3
        or len(values) != len(fraction)
        or not len(fraction)
        or not np.isfinite(values).all()
        or not np.isfinite(fraction).all()
        or np.any(np.diff(fraction) <= 0)
    ):
        raise ValueError("Invalid sampled feature geometry")
    if len(fraction) == 1:
        result = np.repeat(values, frames, axis=0)
    else:
        query = np.linspace(0, 1, frames).clip(fraction[0], fraction[-1])
        right = np.searchsorted(fraction, query, side="right").clip(
            1, len(fraction) - 1
        )
        left = right - 1
        weight = ((query - fraction[left]) / (fraction[right] - fraction[left])).astype(
            "float32"
        )[:, None, None]
        result = values[left] * (1 - weight) + values[right] * weight
    return result.transpose(1, 0, 2).astype("float16")


def load_cached_images(paths):
    """The image cache may also contain labels; only its image field is loaded."""
    images = []
    for path in paths:
        with np.load(path, allow_pickle=False) as z:
            images.append(z["image"])
    result = np.stack(images).astype("float32")
    if not np.isfinite(result).all():
        raise ValueError("Nonfinite cached OCT pixels")
    return result


def sample_field(field, frame_fraction, width):
    n, _, _, _ = field.shape
    fractions = frame_fraction.to(device=field.device, dtype=torch.float32)
    columns = torch.linspace(-1, 1, width, device=field.device)
    grid = torch.stack(
        [
            columns[None, None, :].expand(n, fractions.shape[1], width),
            (fractions[:, :, None] * 2 - 1).expand(n, fractions.shape[1], width),
        ],
        dim=-1,
    )
    return F.grid_sample(
        field.float(), grid, mode="bilinear", padding_mode="border", align_corners=True
    )


def calibrate_positions(probability, positive_delta, endpoint_delta):
    """Anchored endpoint-mass calibration; zero corrections preserve the teacher."""
    original = probability[:, :, 0, :]
    bounded = original.clamp(1e-6, 1 - 1e-6)
    odds = endpoint_delta.exp()
    calibrated = bounded * odds / (1 + bounded * (odds - 1))
    mass = (original + (calibrated - bounded)).clamp(0, 1)
    positive = normalized_positive(probability)
    anchor = torch.cat(
        [original[:, :, None, :], positive * (1 - original[:, :, None, :])], dim=2
    )
    updated = torch.cat(
        [mass[:, :, None, :], positive * (1 - mass[:, :, None, :])], dim=2
    )
    result = (
        probability_median(probability)
        + (probability_median(updated) - probability_median(anchor))
        + torch.where(mass < 0.5, positive_delta, torch.zeros_like(positive_delta))
    )
    return result.clamp(0, probability.shape[2] - 1)


class ContextBlock(nn.Module):
    def __init__(self, width, dilation):
        super().__init__()
        self.net = nn.Sequential(
            nn.Conv2d(width, width, 3, padding=dilation, dilation=dilation, bias=False),
            nn.GroupNorm(8, width),
            nn.SiLU(),
            nn.Conv2d(width, width, 3, padding=dilation, dilation=dilation, bias=False),
            nn.GroupNorm(8, width),
        )

    def forward(self, x):
        return F.silu(x + self.net(x))


class VolumeRefiner(nn.Module):
    def __init__(self, cfg):
        super().__init__()
        from src.models import make_model

        self.coarse = make_model(cfg["coarse_config"])
        self.coarse.requires_grad_(False)
        self.coarse.eval()
        width = cfg.get("volume_width", 64)
        keys = cfg["boundary_keys"]
        self.projections = nn.ModuleDict(
            {
                d: nn.Conv2d(
                    feature_channels(cfg["coarse_config"], d), width, 1, bias=False
                )
                for d in keys
            }
        )
        self.context = nn.Sequential(
            nn.GroupNorm(8, width),
            nn.SiLU(),
            *[ContextBlock(width, 2**i) for i in range(cfg.get("volume_blocks", 3))],
        )
        self.heads = nn.ModuleDict(
            {d: nn.Conv2d(width, 2 * len(k), 1) for d, k in keys.items()}
        )
        self.config = cfg
        for head in self.heads.values():
            nn.init.zeros_(head.weight)
            nn.init.zeros_(head.bias)

    def train(self, mode=True):
        super().train(mode)
        self.coarse.eval()
        return self

    def forward(self, features, family):
        return self.heads[family](self.context(self.projections[family](features)))

    def corrections(self, features, family, frame_fraction, width):
        raw = self(features, family).float()
        k = raw.shape[1] // 2
        positive = self.config.get("volume_radius", 16.0) * raw[:, :k].tanh()
        radius = self.config.get("endpoint_radius", 30.0)
        endpoint = radius * (raw[:, k:] / radius).tanh()
        sampled = sample_field(
            torch.cat([positive, endpoint], dim=1), frame_fraction, self.config["width"]
        )
        if width != self.config["width"]:
            n, c, b, a = sampled.shape
            sampled = F.interpolate(
                sampled.reshape(n, c * b, a),
                size=width,
                mode="linear",
                align_corners=True,
            ).reshape(n, c, b, width)
        return sampled[:, :k], sampled[:, k:]


def volume_loss(
    positive_delta,
    endpoint_delta,
    base_positive,
    base_endpoint,
    target_positive,
    zero_fraction,
    valid,
    height,
    coordinate_weight=0.01,
    regularization=0.0001,
):
    positive = (base_positive + positive_delta).clamp(0, height - 1)
    logit = base_endpoint + endpoint_delta
    mass = zero_fraction.float()
    q = logit.sigmoid()
    distance = (positive - target_positive).abs()
    point = (
        F.binary_cross_entropy_with_logits(logit, mass, reduction="none")
        + (1 - mass) * distance
    )
    risk = q * (1 - mass) * target_positive + (1 - q) * (
        mass * positive + (1 - mass) * distance
    )
    point = (
        point
        + coordinate_weight * risk
        + regularization * (positive_delta.square() + endpoint_delta.square())
    )
    counts = valid.sum((-1, -2))
    term = (point * valid).sum((-1, -2)) / counts.clamp_min(1)
    return (term * (counts > 0)).sum() / (counts > 0).sum().clamp_min(1)