File size: 6,472 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
176
177
178
179
180
181
182
183
"""Fresh-process, label-free OCT boundary inference. Network access is unnecessary."""

from pathlib import Path
import argparse
import json
import time
import numpy as np
import torch
from torch.nn import functional as F
from src.inference.decoding import positions_from_logits
from src.models import build_model, output_indices
from src.inference.runtime import configure_inference, autocast_options
from src.io import load_manifest, volume_headers
from src.inference.context import iter_normalized_contexts
from src.inference.combination import finish_positions


def predict_case(
    row, data_root, model, config, device, batch_size=8, tta=False, precision="fp32"
):
    headers, (b, h, a) = volume_headers(data_root, row["oct_paths"])
    keys = config["device_keys"][row["device"]]
    indices = output_indices(config, row["device"])
    values = np.empty((len(keys), b, a), np.float32)
    shape = (config["height"], config["width"])
    batch = []
    completed = 0

    def consume(images):
        nonlocal completed
        x = torch.from_numpy(np.stack(images)).to(device)
        with torch.inference_mode(), torch.autocast(
            **autocast_options(device, precision)
        ):
            pred = positions_from_logits(model(x)[:, indices], config, row["device"])
            if tta:
                pred = (
                    pred
                    + positions_from_logits(
                        model(x.flip(-1))[:, indices], config, row["device"]
                    ).flip(-1)
                ) * 0.5
            pred = F.interpolate(pred, size=a, mode="linear", align_corners=False)
            pred = (pred + 0.5) * h / shape[0] - 0.5
            pred = pred.clamp(0, h - 1)
        arr = pred.float().cpu().numpy().transpose(1, 0, 2)
        values[:, completed : completed + len(images)] = arr
        completed += len(images)

    for images in iter_normalized_contexts(
        headers, [(shape[0], shape[1], config.get("input_channels", 1))]
    ):
        batch.append(images[0])
        if len(batch) == batch_size:
            consume(batch)
            batch = []
    if batch:
        consume(batch)
    if completed != b or not np.isfinite(values).all():
        raise ValueError("Incomplete or nonfinite prediction")
    return finish_positions(
        values, row["device"], h, config.get("inference", {}).get("postprocess", {})
    ), np.asarray(keys)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--input-manifest", required=True)
    ap.add_argument("--data-root", required=True)
    ap.add_argument("--checkpoint", required=True)
    ap.add_argument("--output-dir", required=True)
    ap.add_argument("--batch-size", type=int, default=32)
    ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
    ap.add_argument("--tta", action="store_true")
    ap.add_argument("--workers", type=int, default=4)
    ap.add_argument("--precision", choices=["fp32", "bf16", "tf32"], default=None)
    ap.add_argument("--shard-index", type=int, default=0)
    ap.add_argument("--num-shards", type=int, default=1)
    args = ap.parse_args()
    # Checkpoint contains model tensors and simple configuration; never labels,
    # participant IDs, paths or a lookup table indexed by acquisition identifiers.
    ck = torch.load(args.checkpoint, map_location="cpu", weights_only=True)
    config = ck["config"]
    args.precision = args.precision or config.get("inference", {}).get(
        "precision", "fp32"
    )
    configure_inference(args.precision)
    if config.get("architecture") == "BoundaryEnsemble":
        from src.inference.ensemble import stream_ensemble

        rows = [
            {k: r[k] for k in ["case_id", "device", "oct_paths"]}
            for r in load_manifest(args.input_manifest)
        ]
        selected = [
            r for i, r in enumerate(rows) if i % args.num_shards == args.shard_index
        ]
        out = Path(args.output_dir)
        out.mkdir(parents=True, exist_ok=True)
        if args.tta:
            raise ValueError("TTA must be explicitly validated before ensemble use")
        stream_ensemble(
            selected,
            args.data_root,
            ck,
            out,
            args.device,
            args.batch_size,
            args.workers,
            args.precision,
        )
        return
    model = build_model(config)
    model.load_state_dict(ck["model"])
    model.to(args.device).eval()
    rows = load_manifest(args.input_manifest)
    # Drop every field except the label-free contract, even if a caller passes more.
    rows = [{k: r[k] for k in ["case_id", "device", "oct_paths"]} for r in rows]
    out = Path(args.output_dir)
    out.mkdir(parents=True, exist_ok=True)
    t = time.monotonic()
    count = 0
    if args.workers:
        from src.inference.stream import stream_predictions

        selected = [
            r for i, r in enumerate(rows) if i % args.num_shards == args.shard_index
        ]
        stream_predictions(
            selected,
            args.data_root,
            [{"model": model, "config": config, "destination": out}],
            args.device,
            args.batch_size,
            args.workers,
            args.tta,
            args.precision,
        )
        return
    for i, row in enumerate(rows):
        if i % args.num_shards != args.shard_index:
            continue
        target = out / (row["case_id"] + ".npz")
        positions, keys = predict_case(
            row,
            args.data_root,
            model,
            config,
            args.device,
            args.batch_size,
            args.tta,
            args.precision,
        )
        tmp = target.with_suffix(".tmp.npz")
        np.savez_compressed(tmp, positions=positions, boundary_keys=keys)
        tmp.replace(target)
        count += 1
        if count % 10 == 0:
            print(
                json.dumps(
                    {
                        "completed_cases": count,
                        "elapsed_seconds": time.monotonic() - t,
                        "shard": args.shard_index,
                    }
                ),
                flush=True,
            )
    print(
        json.dumps(
            {
                "completed_cases": count,
                "elapsed_seconds": time.monotonic() - t,
                "shard": args.shard_index,
            }
        ),
        flush=True,
    )


if __name__ == "__main__":
    main()