File size: 7,113 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
"""Label-free mixed-resolution/context ensemble inference, one OCT decode pass."""

import json
import time
import numpy as np
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
from src.models import build_model, output_indices
from src.inference.stream import OCTBatches
from src.inference.runtime import autocast_options
from src.inference.decoding import positions_from_logits
from src.inference.combination import finish_positions, combine_named_heads
from src.inference.writer import PredictionWriter


def stream_ensemble(
    rows, root, checkpoint, output_dir, device, batch_size, workers, precision
):
    config = checkpoint["config"]
    members = []
    for saved in checkpoint["members"]:
        cfg = saved["config"]
        model = build_model(cfg)
        model.load_state_dict(saved["model"])
        model.to(device).eval().requires_grad_(False)
        members.append({"name": saved["name"], "config": cfg, "model": model})
    if not members or len({m["name"] for m in members}) != len(members):
        raise ValueError("Invalid ensemble members")
    head_routes = config["combination"].get("head_source_by_device")
    active_by_device = {}
    if head_routes is not None:
        if set(head_routes) != set(config["device_keys"]):
            raise ValueError("Head-source device families differ")
        available = {member["name"] for member in members}
        for family, keys in config["device_keys"].items():
            if set(head_routes[family]) != set(keys):
                raise ValueError("Head-source anatomical identities differ")
            active_by_device[family] = set(head_routes[family].values())
            if not active_by_device[family] <= available:
                raise ValueError("Unknown selected head source")
    shapes = sorted(
        {
            (
                m["config"]["height"],
                m["config"]["width"],
                m["config"].get("input_channels", 1),
            )
            for m in members
        }
    )
    loader = DataLoader(
        OCTBatches(rows, root, shapes, batch_size),
        batch_size=None,
        num_workers=workers,
        pin_memory=device.startswith("cuda"),
        **({"prefetch_factor": 2} if workers else {})
    )
    writer = PredictionWriter(output_dir, workers=2, maximum_pending_cases=2)
    # PredictionWriter uses named subdirectories; final files belong at root.
    pending = {}
    done = 0
    start = time.monotonic()
    evaluated_frames = {}
    for batch in loader:
        index, offset = int(batch["index"]), int(batch["offset"])
        b, h, a = map(int, batch["geometry"])
        row = rows[index]
        n = len(batch["images"][0])
        keys = config["device_keys"][row["device"]]
        if index not in pending:
            buffers = []
            for member in members:
                if (
                    head_routes is not None
                    and member["name"] not in active_by_device[row["device"]]
                ):
                    continue
                cfg = member["config"]
                own_keys = cfg["device_keys"][row["device"]]
                if set(own_keys) != set(keys) or len(own_keys) != len(keys):
                    raise ValueError("Ensemble boundary vocabulary differs")
                shape = (cfg["height"], cfg["width"], cfg.get("input_channels", 1))
                buffers.append(
                    {
                        "member": member,
                        "shape": shape,
                        "indices": output_indices(cfg, row["device"]),
                        "order": [own_keys.index(k) for k in keys],
                        "positions": np.empty((len(keys), b, a), np.float32),
                    }
                )
            pending[index] = {"seen": np.zeros(b, bool), "buffers": buffers}
        state = pending[index]
        if offset + n > b or state["seen"][offset : offset + n].any():
            raise ValueError("Invalid frame coverage")
        tensors = [image.to(device, non_blocking=True) for image in batch["images"]]
        with torch.inference_mode(), torch.autocast(
            **autocast_options(device, precision)
        ):
            for buffer in state["buffers"]:
                member = buffer["member"]
                cfg = member["config"]
                family_counts = evaluated_frames.setdefault(row["device"], {})
                family_counts[member["name"]] = family_counts.get(member["name"], 0) + n
                position = positions_from_logits(
                    member["model"](tensors[shapes.index(buffer["shape"])])[
                        :, buffer["indices"]
                    ],
                    cfg,
                    row["device"],
                )
                position = F.interpolate(
                    position, size=a, mode="linear", align_corners=False
                )
                position = ((position + 0.5) * h / cfg["height"] - 0.5).clamp(0, h - 1)
                values = position.float().cpu().numpy().transpose(1, 0, 2)
                buffer["positions"][:, offset : offset + n] = values[buffer["order"]]
        state["seen"][offset : offset + n] = True
        if state["seen"].all():
            combination = config["combination"]
            components = [
                finish_positions(
                    item["positions"],
                    row["device"],
                    h,
                    item["member"]["config"]
                    .get("inference", {})
                    .get("postprocess", {}),
                )
                for item in state["buffers"]
            ]
            values = combine_named_heads(
                components,
                [item["member"]["name"] for item in state["buffers"]],
                keys,
                row["device"],
                combination,
            )
            values = finish_positions(
                values,
                row["device"],
                h,
                config.get("inference", {}).get("postprocess", {}),
            )
            if not np.isfinite(values).all():
                raise ValueError("Nonfinite ensemble output")
            writer.submit(row["case_id"], [("", values, keys)])
            del pending[index]
            done += 1
            if done % 10 == 0:
                print(
                    json.dumps(
                        {
                            "completed_cases": done,
                            "elapsed_seconds": time.monotonic() - start,
                        }
                    ),
                    flush=True,
                )
    writer.close()
    if pending or done != len(rows):
        raise ValueError("Incomplete ensemble input manifest")
    summary = {
        "completed_cases": done,
        "members": len(members),
        "elapsed_seconds": time.monotonic() - start,
        "evaluated_member_frames_by_device": evaluated_frames,
        "static_inactive_networks_skipped": head_routes is not None,
    }
    print(json.dumps(summary), flush=True)
    return summary