farrell236 commited on
Commit
8fca640
·
verified ·
1 Parent(s): 7778bc0

Deploy native Gradio OCT inference app

Browse files

Publish the app source, configuration, and original trained checkpoint with explicit user approval. Configure Python 3.12 and CPU inference.

README.md CHANGED
@@ -5,11 +5,41 @@ colorFrom: green
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.31.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
12
  short_description: MYXOS OCT Segmentation AI-READI (agent1)
13
  ---
14
 
15
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.31.0
8
+ python_version: "3.12"
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
12
  short_description: MYXOS OCT Segmentation AI-READI (agent1)
13
  ---
14
 
15
+ # MYXOS OCT Segmentation · Agent 1
16
+
17
+ Visualize retinal OCT boundaries using a model trained on AI-READI.
18
+
19
+ 1. Upload one structural OCT DICOM volume, or its contiguous split parts.
20
+ 2. Choose the scanner family and processor. Spectralis is selected initially;
21
+ **Detect from DICOM** is also available.
22
+ 3. Run **Full volume** inference, or choose a fast **Selected B-scan preview**.
23
+ 4. Browse the boundary-overlay and original-OCT tabs. Use the toolbar to zoom,
24
+ pan, fit, or view fullscreen; adjust visible boundaries and display settings.
25
+ 5. Choose NPZ predictions or the current overlay PNG, then download the result.
26
+
27
+ Supported scanners: Heidelberg Spectralis, Topcon Maestro2, Topcon Triton, and
28
+ Zeiss Cirrus. This Space runs on CPU with batch size 1 initially; full-volume
29
+ inference can take longer than a slice preview. The model uses its saved FP32
30
+ inference configuration. Preview mode omits smoothing between B-scans.
31
+
32
+ NPZ files contain `positions[boundary, B-scan, A-scan]` in original zero-based
33
+ axial pixels and `boundary_keys`. Slice previews also include `frame_indices`.
34
+
35
+ Inference runs on this Space's server. Uploads and exports are temporary;
36
+ **Clear volume** removes the staged volume and its results. Idle viewer sessions
37
+ expire after two hours, and Gradio clears cached files on a two-hour schedule.
38
+
39
+ This model is specialized to AI-READI structural OCT exports. The boundaries
40
+ retain supplied vendor label distinctions and are research predictions.
41
+
42
+ The retained checkpoint achieved 1.427501 native axial pixels MAE (3.733009 µm)
43
+ on 2,392 frozen validation volumes. Those historical results measure agreement
44
+ with supplied automatic annotations; they do not establish clinical performance
45
+ or numerical equivalence on this hosting hardware.
SHA256SUMS ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ e3ebb3772d3e81ce38adfb24faa6c3bbb228428bbad1e26ba8d6914ff7868d88 README.md
2
+ 174cc76b331c66ccd66212ba9affc9f1bdddc5d4cf0b29748d5cdc7ead406e71 app.py
3
+ 3885e357513ee8bb5d016edfdf1a32aa2693a4b3957159edd0a258cff5af82f6 artifacts/checkpoint.pt
4
+ 27ec4256c8e819455477c01ac58b55e56f0e431d3159e60dea71ed240179efe7 artifacts/config.json
5
+ 488d9c3cb32685b93d71f65392ca721b8880dfaf9b47e62916fdf8c075562554 deployment-manifest.json
6
+ 4462d9add0ce6b4da2719a8ddb766c741adc4629189dc9a2121f5c1751bc27d2 requirements.txt
7
+ e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 src/__init__.py
8
+ e173e85d01aec05b90439ad0ad8901e71168114046568fba3a93629b73b053cb src/annotations.py
9
+ 16d2a26af365572a05d3bdd489c49bb3e5f2b20e3cde91b35e799d578fe38373 src/app.py
10
+ e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 src/inference/__init__.py
11
+ 5db770acac3968725ddc792d2ed7dfea0696cc2afaf0755a7d7020b6236d81f0 src/inference/combination.py
12
+ bcc926d0bf9815ea7c4240d7d81ccd80d6f147d4da9d09df5474bf23bbf89afd src/inference/context.py
13
+ bb61a0844faaf35b907921e2d2ff0654dfd5973a740d8ae4d55d98e3f85ec260 src/inference/decoding.py
14
+ 8fb30517646603138f3ae59853865725fcf3b9f33e852e69f17e8fc63c0867ea src/inference/ensemble.py
15
+ bec5f06c903b609cb5a81407c5ff330ee92303e6d0097fd5afce6b6f47c2963b src/inference/postprocess.py
16
+ 77a4df00a035877c81fe50666eab85fe39e184a32b53998f25cf47ce58a66e10 src/inference/predict.py
17
+ be8083fef3073fec66d7597bb2b6162a71374250280be03a1a9def1423ddb011 src/inference/runtime.py
18
+ 532e5fc012975b26a2c878afdabc6f9a3460d15400f971994b98798de823b853 src/inference/stream.py
19
+ dc20c2fc824c4e139be7325f31e5c478f6fa0b1649f4be7f4854c95285ab0980 src/inference/writer.py
20
+ f98a90699cd60d434bce6ec4a972f7e6cc61443c4c793478d73ddf5c4710ff6e src/io.py
21
+ 9492b95e91a4f5763bd2051b9cb701aa446f8bddee0c44ab4464090a526e4820 src/models/__init__.py
22
+ c27c7cec00f7ec8793a4a60099ea6b5d58fd50a550ef17a3318f60349852d232 src/models/auxiliary.py
23
+ 5a4f94f4367a1d64068bc7d9e659bc9c73f88dd710f813b95e09aa371c87e29d src/models/axial_prior.py
24
+ a8c3545793eb5f9641347b37926992ae9cee87f27e5d75631a45d56f2dbc570c src/models/context.py
25
+ 04e9d96e400e8e3a810bd1715eaf108556efd2f353a8e9387debeef136561c62 src/models/global_context.py
26
+ b32fe6dfdaa14315168cfcdbb089caea2b657bd750d00ee6934cd17a999413cc src/models/unet.py
27
+ ed6afbab0b1b5be688e9ee8d215a0d6d7d21bf5c4b5dd8b475aab6025710210a src/models/wide.py
28
+ ae1086d80632488af53810ffd3174a4310ed7ff08629403fead0eaecf0d153a6 src/paths.py
29
+ 7daf3444431e52d28547ca11d213cd0670dbe4283f4e46eec6fcd668ab1fba0b src/protocol.py
30
+ e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 src/training/__init__.py
31
+ 494a07c49ae2a3d50ac42d62aa7a8d980814d014544d663c32248819094290ae src/training/guard.py
32
+ d8a1be7675de472522fb23b355c8106c5b4392bcee3b27ce3b44c24b7fd77117 src/training/unet.py
33
+ a10d79e8681dbe05a9c36896850587a6e8984ff500b0c1475edf41a830b61580 src/viewer.js
34
+ 1b3bebd10774adfb115d884dbb8eb549a08b0a39f569117946e8acf161c8f32f src/viewer.py
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Launch OCT boundary inference locally or on Hugging Face Spaces."""
2
+
3
+ import argparse
4
+ import os
5
+
6
+
7
+ def parse_args(argv=None):
8
+ parser = argparse.ArgumentParser(description=__doc__)
9
+ parser.add_argument(
10
+ "--port", type=int, default=os.environ.get("GRADIO_SERVER_PORT", "7860")
11
+ )
12
+ parser.add_argument(
13
+ "--host",
14
+ default=os.environ.get(
15
+ "GRADIO_SERVER_NAME",
16
+ "0.0.0.0" if os.environ.get("SPACE_ID") else "127.0.0.1",
17
+ ),
18
+ )
19
+ parser.add_argument(
20
+ "--open", action="store_true", help="Open the viewer in your browser"
21
+ )
22
+ args = parser.parse_args(argv)
23
+ if not 1 <= args.port <= 65535:
24
+ parser.error("Port must be between 1 and 65535")
25
+ return args
26
+
27
+
28
+ def main():
29
+ args = parse_args()
30
+ os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
31
+
32
+ from src.app import build_app
33
+ from src.viewer import CHECKPOINT
34
+
35
+ if not CHECKPOINT.is_file():
36
+ raise SystemExit(f"Checkpoint does not exist: {CHECKPOINT}")
37
+ demo = build_app()
38
+ demo.queue(default_concurrency_limit=1, max_size=12).launch(
39
+ server_name=args.host,
40
+ server_port=args.port,
41
+ inbrowser=args.open and not bool(os.environ.get("SPACE_ID")),
42
+ share=False,
43
+ show_error=False,
44
+ blocked_paths=[str(CHECKPOINT)],
45
+ )
46
+
47
+
48
+ if __name__ == "__main__":
49
+ main()
artifacts/checkpoint.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3885e357513ee8bb5d016edfdf1a32aa2693a4b3957159edd0a258cff5af82f6
3
+ size 44047582
artifacts/config.json ADDED
@@ -0,0 +1,495 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architecture": "BoundaryEnsemble",
3
+ "boundary_keys": [
4
+ "DCM_128289",
5
+ "DCM_128290",
6
+ "DCM_128291",
7
+ "DCM_128292",
8
+ "DCM_128293",
9
+ "DCM_128297",
10
+ "DCM_128298",
11
+ "DCM_128299",
12
+ "DCM_128300",
13
+ "DCM_128301",
14
+ "DCM_128320",
15
+ "DCM_128321",
16
+ "DCM_128323",
17
+ "SCT_280677004",
18
+ "SCT_76710003"
19
+ ],
20
+ "device_keys": {
21
+ "zeiss_cirrus": [
22
+ "SCT_280677004",
23
+ "DCM_128298"
24
+ ],
25
+ "heidelberg_spectralis": [
26
+ "SCT_280677004",
27
+ "DCM_128300",
28
+ "DCM_128289",
29
+ "DCM_128290",
30
+ "DCM_128291",
31
+ "DCM_128292",
32
+ "DCM_128293",
33
+ "SCT_76710003",
34
+ "DCM_128321",
35
+ "DCM_128323",
36
+ "DCM_128297"
37
+ ],
38
+ "topcon_triton": [
39
+ "SCT_280677004",
40
+ "DCM_128289",
41
+ "DCM_128290",
42
+ "DCM_128291",
43
+ "DCM_128320",
44
+ "DCM_128297",
45
+ "DCM_128299",
46
+ "DCM_128293",
47
+ "SCT_76710003",
48
+ "DCM_128301"
49
+ ],
50
+ "topcon_maestro2": [
51
+ "SCT_280677004",
52
+ "DCM_128289",
53
+ "DCM_128290",
54
+ "DCM_128291",
55
+ "DCM_128320",
56
+ "DCM_128297",
57
+ "DCM_128299",
58
+ "DCM_128293",
59
+ "SCT_76710003"
60
+ ]
61
+ },
62
+ "combination": {
63
+ "method": "weighted_heads",
64
+ "head_weights_by_device": {
65
+ "zeiss_cirrus": {
66
+ "SCT_280677004": {
67
+ "global_l1_10000": 1.0
68
+ },
69
+ "DCM_128298": {
70
+ "global_l1_10000": 1.0
71
+ }
72
+ },
73
+ "heidelberg_spectralis": {
74
+ "SCT_280677004": {
75
+ "highres_context150": 0.75,
76
+ "global_l1_10000": 0.25
77
+ },
78
+ "DCM_128300": {
79
+ "highres_context150": 0.75,
80
+ "global_l1_10000": 0.25
81
+ },
82
+ "DCM_128289": {
83
+ "highres_context150": 0.5,
84
+ "global_l1_10000": 0.5
85
+ },
86
+ "DCM_128290": {
87
+ "highres_context150": 0.5,
88
+ "global_l1_10000": 0.5
89
+ },
90
+ "DCM_128291": {
91
+ "highres_context150": 0.5,
92
+ "global150": 0.5
93
+ },
94
+ "DCM_128292": {
95
+ "highres_context150": 0.5,
96
+ "global150": 0.5
97
+ },
98
+ "DCM_128293": {
99
+ "highres_context150": 0.75,
100
+ "global150": 0.25
101
+ },
102
+ "SCT_76710003": {
103
+ "highres_context150": 0.75,
104
+ "global150": 0.25
105
+ },
106
+ "DCM_128321": {
107
+ "highres_context150": 0.75,
108
+ "global150": 0.25
109
+ },
110
+ "DCM_128323": {
111
+ "highres_context150": 0.75,
112
+ "global150": 0.25
113
+ },
114
+ "DCM_128297": {
115
+ "highres_context150": 0.75,
116
+ "global150": 0.25
117
+ }
118
+ },
119
+ "topcon_triton": {
120
+ "SCT_280677004": {
121
+ "global_l1_10000": 1.0
122
+ },
123
+ "DCM_128289": {
124
+ "highres_context150": 0.5,
125
+ "global_l1_10000": 0.5
126
+ },
127
+ "DCM_128290": {
128
+ "highres_context150": 0.5,
129
+ "global_l1_10000": 0.5
130
+ },
131
+ "DCM_128291": {
132
+ "highres_context150": 0.5,
133
+ "global_l1_10000": 0.5
134
+ },
135
+ "DCM_128320": {
136
+ "highres_context150": 0.5,
137
+ "global_l1_10000": 0.5
138
+ },
139
+ "DCM_128297": {
140
+ "highres_context150": 0.5,
141
+ "global_l1_10000": 0.5
142
+ },
143
+ "DCM_128299": {
144
+ "highres_context150": 0.75,
145
+ "global_l1_10000": 0.25
146
+ },
147
+ "DCM_128293": {
148
+ "highres_context150": 0.5,
149
+ "global_l1_10000": 0.5
150
+ },
151
+ "SCT_76710003": {
152
+ "highres_context150": 0.75,
153
+ "global_l1_10000": 0.25
154
+ },
155
+ "DCM_128301": {
156
+ "highres_context150": 0.75,
157
+ "global_l1_10000": 0.25
158
+ }
159
+ },
160
+ "topcon_maestro2": {
161
+ "SCT_280677004": {
162
+ "highres_context150": 0.5,
163
+ "global_l1_10000": 0.5
164
+ },
165
+ "DCM_128289": {
166
+ "highres_context150": 0.25,
167
+ "global_l1_10000": 0.75
168
+ },
169
+ "DCM_128290": {
170
+ "global_l1_10000": 1.0
171
+ },
172
+ "DCM_128291": {
173
+ "global_l1_10000": 1.0
174
+ },
175
+ "DCM_128320": {
176
+ "global_l1_10000": 1.0
177
+ },
178
+ "DCM_128297": {
179
+ "global_l1_10000": 1.0
180
+ },
181
+ "DCM_128299": {
182
+ "global_l1_10000": 1.0
183
+ },
184
+ "DCM_128293": {
185
+ "global_l1_10000": 1.0
186
+ },
187
+ "SCT_76710003": {
188
+ "global_l1_10000": 1.0
189
+ }
190
+ }
191
+ }
192
+ },
193
+ "inference": {
194
+ "precision": "fp32",
195
+ "batch_size": 32,
196
+ "workers": 4,
197
+ "postprocess": {
198
+ "by_device": {
199
+ "zeiss_cirrus": {
200
+ "boundary_filters": [
201
+ {
202
+ "filter": "gaussian",
203
+ "round_devices": [
204
+ "topcon_maestro2",
205
+ "topcon_triton",
206
+ "zeiss_cirrus"
207
+ ],
208
+ "sigma_b": 0.75
209
+ },
210
+ {
211
+ "filter": "gaussian",
212
+ "round_devices": [
213
+ "topcon_maestro2",
214
+ "topcon_triton",
215
+ "zeiss_cirrus"
216
+ ],
217
+ "sigma_b": 0.75
218
+ }
219
+ ]
220
+ },
221
+ "heidelberg_spectralis": {
222
+ "boundary_filters": [
223
+ {
224
+ "filter": "gaussian",
225
+ "round_devices": [
226
+ "topcon_maestro2",
227
+ "topcon_triton",
228
+ "zeiss_cirrus"
229
+ ],
230
+ "sigma_b": 6.0
231
+ },
232
+ {
233
+ "filter": "gaussian",
234
+ "round_devices": [
235
+ "topcon_maestro2",
236
+ "topcon_triton",
237
+ "zeiss_cirrus"
238
+ ],
239
+ "sigma_b": 9.0
240
+ },
241
+ {
242
+ "filter": "gaussian",
243
+ "round_devices": [
244
+ "topcon_maestro2",
245
+ "topcon_triton",
246
+ "zeiss_cirrus"
247
+ ],
248
+ "sigma_b": 6.0
249
+ },
250
+ {
251
+ "filter": "gaussian",
252
+ "round_devices": [
253
+ "topcon_maestro2",
254
+ "topcon_triton",
255
+ "zeiss_cirrus"
256
+ ],
257
+ "sigma_b": 6.0
258
+ },
259
+ {
260
+ "filter": "gaussian",
261
+ "round_devices": [
262
+ "topcon_maestro2",
263
+ "topcon_triton",
264
+ "zeiss_cirrus"
265
+ ],
266
+ "sigma_b": 6.0
267
+ },
268
+ {
269
+ "filter": "gaussian",
270
+ "round_devices": [
271
+ "topcon_maestro2",
272
+ "topcon_triton",
273
+ "zeiss_cirrus"
274
+ ],
275
+ "sigma_b": 6.0
276
+ },
277
+ {
278
+ "filter": "gaussian",
279
+ "round_devices": [
280
+ "topcon_maestro2",
281
+ "topcon_triton",
282
+ "zeiss_cirrus"
283
+ ],
284
+ "sigma_b": 6.0
285
+ },
286
+ {
287
+ "filter": "gaussian",
288
+ "round_devices": [
289
+ "topcon_maestro2",
290
+ "topcon_triton",
291
+ "zeiss_cirrus"
292
+ ],
293
+ "sigma_b": 9.0
294
+ },
295
+ {
296
+ "filter": "gaussian",
297
+ "round_devices": [
298
+ "topcon_maestro2",
299
+ "topcon_triton",
300
+ "zeiss_cirrus"
301
+ ],
302
+ "sigma_b": 9.0
303
+ },
304
+ {
305
+ "filter": "gaussian",
306
+ "round_devices": [
307
+ "topcon_maestro2",
308
+ "topcon_triton",
309
+ "zeiss_cirrus"
310
+ ],
311
+ "sigma_b": 6.0
312
+ },
313
+ {
314
+ "filter": "gaussian",
315
+ "round_devices": [
316
+ "topcon_maestro2",
317
+ "topcon_triton",
318
+ "zeiss_cirrus"
319
+ ],
320
+ "sigma_b": 6.0
321
+ }
322
+ ]
323
+ },
324
+ "topcon_triton": {
325
+ "boundary_filters": [
326
+ {
327
+ "round_devices": [
328
+ "topcon_maestro2",
329
+ "topcon_triton",
330
+ "zeiss_cirrus"
331
+ ]
332
+ },
333
+ {
334
+ "filter": "gaussian",
335
+ "round_devices": [
336
+ "topcon_maestro2",
337
+ "topcon_triton",
338
+ "zeiss_cirrus"
339
+ ],
340
+ "sigma_b": 0.375
341
+ },
342
+ {
343
+ "filter": "gaussian",
344
+ "round_devices": [
345
+ "topcon_maestro2",
346
+ "topcon_triton",
347
+ "zeiss_cirrus"
348
+ ],
349
+ "sigma_b": 0.375
350
+ },
351
+ {
352
+ "filter": "gaussian",
353
+ "round_devices": [
354
+ "topcon_maestro2",
355
+ "topcon_triton",
356
+ "zeiss_cirrus"
357
+ ],
358
+ "sigma_b": 0.375
359
+ },
360
+ {
361
+ "filter": "gaussian",
362
+ "round_devices": [
363
+ "topcon_maestro2",
364
+ "topcon_triton",
365
+ "zeiss_cirrus"
366
+ ],
367
+ "sigma_b": 0.25
368
+ },
369
+ {
370
+ "filter": "gaussian",
371
+ "round_devices": [
372
+ "topcon_maestro2",
373
+ "topcon_triton",
374
+ "zeiss_cirrus"
375
+ ],
376
+ "sigma_b": 0.25
377
+ },
378
+ {
379
+ "filter": "gaussian",
380
+ "round_devices": [
381
+ "topcon_maestro2",
382
+ "topcon_triton",
383
+ "zeiss_cirrus"
384
+ ],
385
+ "sigma_b": 0.25
386
+ },
387
+ {
388
+ "filter": "gaussian",
389
+ "round_devices": [
390
+ "topcon_maestro2",
391
+ "topcon_triton",
392
+ "zeiss_cirrus"
393
+ ],
394
+ "sigma_b": 0.375
395
+ },
396
+ {
397
+ "filter": "gaussian",
398
+ "round_devices": [
399
+ "topcon_maestro2",
400
+ "topcon_triton",
401
+ "zeiss_cirrus"
402
+ ],
403
+ "sigma_b": 0.375
404
+ },
405
+ {
406
+ "filter": "gaussian",
407
+ "round_devices": [
408
+ "topcon_maestro2",
409
+ "topcon_triton",
410
+ "zeiss_cirrus"
411
+ ],
412
+ "sigma_b": 0.375
413
+ }
414
+ ]
415
+ },
416
+ "topcon_maestro2": {
417
+ "boundary_filters": [
418
+ {
419
+ "filter": "gaussian",
420
+ "round_devices": [
421
+ "topcon_maestro2",
422
+ "topcon_triton",
423
+ "zeiss_cirrus"
424
+ ],
425
+ "sigma_b": 0.25
426
+ },
427
+ {
428
+ "filter": "gaussian",
429
+ "round_devices": [
430
+ "topcon_maestro2",
431
+ "topcon_triton",
432
+ "zeiss_cirrus"
433
+ ],
434
+ "sigma_b": 0.375
435
+ },
436
+ {
437
+ "filter": "gaussian",
438
+ "round_devices": [
439
+ "topcon_maestro2",
440
+ "topcon_triton",
441
+ "zeiss_cirrus"
442
+ ],
443
+ "sigma_b": 0.375
444
+ },
445
+ {
446
+ "round_devices": [
447
+ "topcon_maestro2",
448
+ "topcon_triton",
449
+ "zeiss_cirrus"
450
+ ]
451
+ },
452
+ {
453
+ "round_devices": [
454
+ "topcon_maestro2",
455
+ "topcon_triton",
456
+ "zeiss_cirrus"
457
+ ]
458
+ },
459
+ {
460
+ "round_devices": [
461
+ "topcon_maestro2",
462
+ "topcon_triton",
463
+ "zeiss_cirrus"
464
+ ]
465
+ },
466
+ {
467
+ "round_devices": [
468
+ "topcon_maestro2",
469
+ "topcon_triton",
470
+ "zeiss_cirrus"
471
+ ]
472
+ },
473
+ {
474
+ "round_devices": [
475
+ "topcon_maestro2",
476
+ "topcon_triton",
477
+ "zeiss_cirrus"
478
+ ]
479
+ },
480
+ {
481
+ "round_devices": [
482
+ "topcon_maestro2",
483
+ "topcon_triton",
484
+ "zeiss_cirrus"
485
+ ]
486
+ }
487
+ ]
488
+ }
489
+ }
490
+ }
491
+ },
492
+ "initialization": "own random-root supervised CNNs with fixed validation-selected anatomical mixtures",
493
+ "validation_manifest_sha256": "d7e10d32e377b098245f9f675849903998a1c60df7834b6f2bc09436494b7cfb",
494
+ "test_payloads_used": false
495
+ }
deployment-manifest.json ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "space": "farrell236/OCT_Segmentation_a1",
3
+ "python_version": "3.12",
4
+ "gradio_version": "6.31.0",
5
+ "hardware": "cpu-basic",
6
+ "files": {
7
+ "README.md": {
8
+ "source": "deploy/huggingface/README.md",
9
+ "sha256": "e3ebb3772d3e81ce38adfb24faa6c3bbb228428bbad1e26ba8d6914ff7868d88",
10
+ "bytes": 2012
11
+ },
12
+ "app.py": {
13
+ "source": "app.py",
14
+ "sha256": "174cc76b331c66ccd66212ba9affc9f1bdddc5d4cf0b29748d5cdc7ead406e71",
15
+ "bytes": 1377
16
+ },
17
+ "artifacts/checkpoint.pt": {
18
+ "source": "artifacts/checkpoint.pt",
19
+ "sha256": "3885e357513ee8bb5d016edfdf1a32aa2693a4b3957159edd0a258cff5af82f6",
20
+ "bytes": 44047582
21
+ },
22
+ "artifacts/config.json": {
23
+ "source": "artifacts/config.json",
24
+ "sha256": "27ec4256c8e819455477c01ac58b55e56f0e431d3159e60dea71ed240179efe7",
25
+ "bytes": 12416
26
+ },
27
+ "requirements.txt": {
28
+ "source": "deploy/huggingface/requirements.txt",
29
+ "sha256": "4462d9add0ce6b4da2719a8ddb766c741adc4629189dc9a2121f5c1751bc27d2",
30
+ "bytes": 320
31
+ },
32
+ "src/__init__.py": {
33
+ "source": "src/__init__.py",
34
+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
35
+ "bytes": 0
36
+ },
37
+ "src/annotations.py": {
38
+ "source": "src/annotations.py",
39
+ "sha256": "e173e85d01aec05b90439ad0ad8901e71168114046568fba3a93629b73b053cb",
40
+ "bytes": 5865
41
+ },
42
+ "src/app.py": {
43
+ "source": "src/app.py",
44
+ "sha256": "16d2a26af365572a05d3bdd489c49bb3e5f2b20e3cde91b35e799d578fe38373",
45
+ "bytes": 19812
46
+ },
47
+ "src/inference/__init__.py": {
48
+ "source": "src/inference/__init__.py",
49
+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
50
+ "bytes": 0
51
+ },
52
+ "src/inference/combination.py": {
53
+ "source": "src/inference/combination.py",
54
+ "sha256": "5db770acac3968725ddc792d2ed7dfea0696cc2afaf0755a7d7020b6236d81f0",
55
+ "bytes": 5484
56
+ },
57
+ "src/inference/context.py": {
58
+ "source": "src/inference/context.py",
59
+ "sha256": "bcc926d0bf9815ea7c4240d7d81ccd80d6f147d4da9d09df5474bf23bbf89afd",
60
+ "bytes": 2152
61
+ },
62
+ "src/inference/decoding.py": {
63
+ "source": "src/inference/decoding.py",
64
+ "sha256": "bb61a0844faaf35b907921e2d2ff0654dfd5973a740d8ae4d55d98e3f85ec260",
65
+ "bytes": 2729
66
+ },
67
+ "src/inference/ensemble.py": {
68
+ "source": "src/inference/ensemble.py",
69
+ "sha256": "8fb30517646603138f3ae59853865725fcf3b9f33e852e69f17e8fc63c0867ea",
70
+ "bytes": 7113
71
+ },
72
+ "src/inference/postprocess.py": {
73
+ "source": "src/inference/postprocess.py",
74
+ "sha256": "bec5f06c903b609cb5a81407c5ff330ee92303e6d0097fd5afce6b6f47c2963b",
75
+ "bytes": 2862
76
+ },
77
+ "src/inference/predict.py": {
78
+ "source": "src/inference/predict.py",
79
+ "sha256": "77a4df00a035877c81fe50666eab85fe39e184a32b53998f25cf47ce58a66e10",
80
+ "bytes": 6472
81
+ },
82
+ "src/inference/runtime.py": {
83
+ "source": "src/inference/runtime.py",
84
+ "sha256": "be8083fef3073fec66d7597bb2b6162a71374250280be03a1a9def1423ddb011",
85
+ "bytes": 947
86
+ },
87
+ "src/inference/stream.py": {
88
+ "source": "src/inference/stream.py",
89
+ "sha256": "532e5fc012975b26a2c878afdabc6f9a3460d15400f971994b98798de823b853",
90
+ "bytes": 7186
91
+ },
92
+ "src/inference/writer.py": {
93
+ "source": "src/inference/writer.py",
94
+ "sha256": "dc20c2fc824c4e139be7325f31e5c478f6fa0b1649f4be7f4854c95285ab0980",
95
+ "bytes": 1773
96
+ },
97
+ "src/io.py": {
98
+ "source": "src/io.py",
99
+ "sha256": "f98a90699cd60d434bce6ec4a972f7e6cc61443c4c793478d73ddf5c4710ff6e",
100
+ "bytes": 3591
101
+ },
102
+ "src/models/__init__.py": {
103
+ "source": "src/models/__init__.py",
104
+ "sha256": "9492b95e91a4f5763bd2051b9cb701aa446f8bddee0c44ab4464090a526e4820",
105
+ "bytes": 7318
106
+ },
107
+ "src/models/auxiliary.py": {
108
+ "source": "src/models/auxiliary.py",
109
+ "sha256": "c27c7cec00f7ec8793a4a60099ea6b5d58fd50a550ef17a3318f60349852d232",
110
+ "bytes": 5010
111
+ },
112
+ "src/models/axial_prior.py": {
113
+ "source": "src/models/axial_prior.py",
114
+ "sha256": "5a4f94f4367a1d64068bc7d9e659bc9c73f88dd710f813b95e09aa371c87e29d",
115
+ "bytes": 969
116
+ },
117
+ "src/models/context.py": {
118
+ "source": "src/models/context.py",
119
+ "sha256": "a8c3545793eb5f9641347b37926992ae9cee87f27e5d75631a45d56f2dbc570c",
120
+ "bytes": 992
121
+ },
122
+ "src/models/global_context.py": {
123
+ "source": "src/models/global_context.py",
124
+ "sha256": "04e9d96e400e8e3a810bd1715eaf108556efd2f353a8e9387debeef136561c62",
125
+ "bytes": 2168
126
+ },
127
+ "src/models/unet.py": {
128
+ "source": "src/models/unet.py",
129
+ "sha256": "b32fe6dfdaa14315168cfcdbb089caea2b657bd750d00ee6934cd17a999413cc",
130
+ "bytes": 3796
131
+ },
132
+ "src/models/wide.py": {
133
+ "source": "src/models/wide.py",
134
+ "sha256": "ed6afbab0b1b5be688e9ee8d215a0d6d7d21bf5c4b5dd8b475aab6025710210a",
135
+ "bytes": 1525
136
+ },
137
+ "src/paths.py": {
138
+ "source": "src/paths.py",
139
+ "sha256": "ae1086d80632488af53810ffd3174a4310ed7ff08629403fead0eaecf0d153a6",
140
+ "bytes": 139
141
+ },
142
+ "src/protocol.py": {
143
+ "source": "src/protocol.py",
144
+ "sha256": "7daf3444431e52d28547ca11d213cd0670dbe4283f4e46eec6fcd668ab1fba0b",
145
+ "bytes": 11426
146
+ },
147
+ "src/training/__init__.py": {
148
+ "source": "src/training/__init__.py",
149
+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
150
+ "bytes": 0
151
+ },
152
+ "src/training/guard.py": {
153
+ "source": "src/training/guard.py",
154
+ "sha256": "494a07c49ae2a3d50ac42d62aa7a8d980814d014544d663c32248819094290ae",
155
+ "bytes": 1485
156
+ },
157
+ "src/training/unet.py": {
158
+ "source": "src/training/unet.py",
159
+ "sha256": "d8a1be7675de472522fb23b355c8106c5b4392bcee3b27ce3b44c24b7fd77117",
160
+ "bytes": 33225
161
+ },
162
+ "src/viewer.js": {
163
+ "source": "src/viewer.js",
164
+ "sha256": "a10d79e8681dbe05a9c36896850587a6e8984ff500b0c1475edf41a830b61580",
165
+ "bytes": 2320
166
+ },
167
+ "src/viewer.py": {
168
+ "source": "src/viewer.py",
169
+ "sha256": "1b3bebd10774adfb115d884dbb8eb549a08b0a39f569117946e8acf161c8f32f",
170
+ "bytes": 22993
171
+ }
172
+ }
173
+ }
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CPU build for the existing cpu-basic Space; the model's numerical code is unchanged.
2
+ --extra-index-url https://download.pytorch.org/whl/cpu
3
+ torch==2.9.1+cpu
4
+ gradio==6.31.0
5
+ numpy==2.5.4
6
+ pydicom==3.0.2
7
+ Pillow==12.3.0
8
+ scipy==1.18.1
9
+ pylibjpeg==2.1.0
10
+ pylibjpeg-openjpeg==2.6.0
11
+ pylibjpeg-libjpeg==2.5.0
12
+ pylibjpeg-rle==2.2.0
src/__init__.py ADDED
File without changes
src/annotations.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Canonical boundary conversion primitives; cohort/geometry audit must precede use.
2
+
3
+ Source depths are stored pixel coordinates. RWVM is retained for provenance and is
4
+ not blindly applied: Zeiss metadata can carry lateral rather than axial scale.
5
+ """
6
+ from pathlib import Path
7
+ import numpy as np
8
+ import pydicom
9
+
10
+ def code_key(segment):
11
+ codes=segment.get('SegmentedPropertyTypeCodeSequence',[])
12
+ if len(codes)!=1:raise ValueError('Boundary needs one unambiguous anatomical code')
13
+ return str(codes[0].CodingSchemeDesignator)+'_'+str(codes[0].CodeValue)
14
+
15
+ FRAME_MAPPING_VERSION='aireadi-heightmap-anatomy-v2'
16
+
17
+ def heightmap_frame_numbers(ds,device=None):
18
+ """Return effective anatomy IDs for stored frames, with a Topcon export erratum.
19
+
20
+ AI-READI Topcon payloads are in anatomical depth order. Their sequential frame
21
+ IDs use a different SegmentSequence order for OPL/ELM/EZ/RPE. The fixed mapping
22
+ below is supported by train/validation raw B-scan overlays, layer definitions,
23
+ and depth ordering; it does not sort or otherwise alter per-location values.
24
+ Heidelberg and Zeiss follow their explicit frame IDs. See discovery evidence.
25
+ """
26
+ declared=[int(f.SegmentIdentificationSequence[0].ReferencedSegmentNumber) for f in ds.get('PerFrameFunctionalGroupsSequence',[])]
27
+ if device not in ['topcon_maestro2','topcon_triton']:return declared
28
+ expected_n=9 if device=='topcon_maestro2' else 10
29
+ expected_codes=['SCT_280677004','DCM_128289','DCM_128290','DCM_128291','DCM_128320','DCM_128297','DCM_128299','DCM_128293','SCT_76710003']+(['DCM_128301'] if expected_n==10 else [])
30
+ segments={int(s.SegmentNumber):s for s in ds.SegmentSequence}
31
+ if sorted(segments)!=list(range(1,expected_n+1)) or [code_key(segments[n]) for n in sorted(segments)]!=expected_codes:raise ValueError('Unsupported Topcon codebook')
32
+ effective=[1,2,3,4,8,9,5,6,7]+([10] if expected_n==10 else [])
33
+ if declared==effective:return declared
34
+ if declared!=list(range(1,expected_n+1)):raise ValueError('Unsupported Topcon frame identity pattern')
35
+ return effective
36
+
37
+ def read_heightmap(path,height,device=None):
38
+ ds=pydicom.dcmread(path)
39
+ if str(ds.get('SegmentationType',''))!='HEIGHTMAP':raise ValueError('Unsupported segmentation representation')
40
+ raw=ds.pixel_array.astype(np.float32)
41
+ if raw.ndim==2:raw=raw[None]
42
+ frames=ds.get('PerFrameFunctionalGroupsSequence',[])
43
+ if len(frames)!=len(raw):raise ValueError('Missing frame-to-segment identities')
44
+ frame_numbers=heightmap_frame_numbers(ds,device)
45
+ segments={int(s.SegmentNumber):s for s in ds.SegmentSequence}
46
+ if len(frame_numbers)!=len(set(frame_numbers)) or set(frame_numbers)!=set(segments):raise ValueError('Ambiguous or incomplete heightmap frame mapping')
47
+ keys=[code_key(segments[n]) for n in sorted(segments)]
48
+ if len(keys)!=len(set(keys)):raise ValueError('Duplicate anatomical boundary code')
49
+ values=raw[[frame_numbers.index(n) for n in sorted(segments)]]
50
+ valid=np.isfinite(values)
51
+ padding=ds.get('FloatPixelPaddingValue',None)
52
+ if padding is not None:
53
+ low=float(padding);high=float(ds.get('FloatPixelPaddingRangeLimit',padding))
54
+ valid &= ~((values>=min(low,high))&(values<=max(low,high)))
55
+ # Zero is a real location. Values outside the imaged axial support cannot be
56
+ # scored in original-volume coordinates and remain source-invalid locations.
57
+ valid &= (values>=0)&(values<=height-1)
58
+ values=np.where(valid,values,0).astype(np.float32)
59
+ return values,np.asarray(keys),valid,ds
60
+
61
+ def source_support_statistics(ds,height,device=None):
62
+ """Audit every source location without changing reference measurement support."""
63
+ raw=np.asarray(ds.pixel_array,dtype=np.float32)
64
+ if raw.ndim==2:raw=raw[None]
65
+ numbers=heightmap_frame_numbers(ds,device)
66
+ segments={int(s.SegmentNumber):s for s in ds.SegmentSequence}
67
+ padding=ds.get('FloatPixelPaddingValue',None)
68
+ result={}
69
+ for number in sorted(segments):
70
+ values=raw[numbers.index(number)];finite=np.isfinite(values);pad=np.zeros(values.shape,bool)
71
+ if padding is not None:
72
+ low=float(padding);high=float(ds.get('FloatPixelPaddingRangeLimit',padding))
73
+ pad=(values>=min(low,high))&(values<=max(low,high))
74
+ outside=finite&~pad&((values<0)|(values>height-1));valid=finite&~pad&~outside
75
+ result[code_key(segments[number])]={'locations':int(values.size),'valid_points':int(valid.sum()),
76
+ 'nonfinite_points':int((~finite).sum()),'padding_points':int((finite&pad).sum()),
77
+ 'outside_imaged_axial_support':int(outside.sum()),'valid_zero_depth_points':int(((values==0)&valid).sum()),
78
+ 'b_scans_with_support':int(valid.any(-1).sum())}
79
+ return result
80
+
81
+ def axial_spacing_um(headers,device):
82
+ spacings=[]
83
+ for path,ds in headers:
84
+ shared=ds.get('SharedFunctionalGroupsSequence',[])
85
+ base=shared[0] if shared else ds
86
+ for fr in ds.get('PerFrameFunctionalGroupsSequence',[ds]):
87
+ pm=fr.get('PixelMeasuresSequence',base.get('PixelMeasuresSequence',[]))
88
+ if not pm:return None
89
+ spacing=float(pm[0].PixelSpacing[0])
90
+ if not np.isfinite(spacing) or spacing<=0:return None
91
+ spacings.append(spacing)
92
+ if not spacings or not np.allclose(spacings,spacings[0],rtol=1e-5,atol=1e-9):return None
93
+ return spacings[0]*1000
94
+
95
+ def convert_case(row,root,headers,geometry,with_audit=False):
96
+ b,h,a=geometry
97
+ if len(row['segmentation_paths'])!=1:raise ValueError('Multiple segmentation assembly requires explicit frozen mapping')
98
+ values,keys,valid,ds=read_heightmap(Path(root)/row['segmentation_paths'][0],h,row['device'])
99
+ if values.shape[1:]!=(b,a):raise ValueError('Heightmap and structural grid differ')
100
+ # Frame order and axes are verified by frozen metadata audit plus source overlays.
101
+ spacing=axial_spacing_um(headers,row['device'])
102
+ data={'positions':values,'boundary_keys':keys,'valid':valid,'height':np.asarray(h,dtype=np.int32),**({'axial_spacing_um':np.asarray(spacing)} if spacing else {})}
103
+ if with_audit:return data,source_support_statistics(ds,h,row['device'])
104
+ return data
src/app.py ADDED
@@ -0,0 +1,605 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Native Gradio controls for the retained OCT model and B-scan viewer."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import html
6
+ import os
7
+ from pathlib import Path
8
+
9
+ os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
10
+
11
+ import gradio as gr
12
+ from PIL import Image
13
+
14
+ from src.viewer import (
15
+ AUTO,
16
+ ENGINE,
17
+ FAMILIES,
18
+ STORE,
19
+ boundary_choices,
20
+ compute_choices,
21
+ legend,
22
+ load_upload,
23
+ render,
24
+ resolve_family,
25
+ volume_info,
26
+ )
27
+
28
+ DEFAULT_SCANNER = "heidelberg_spectralis"
29
+ EMPTY_MESSAGE = "Upload a structural OCT DICOM to begin."
30
+
31
+
32
+ def download_result(volume, artifact, png=None):
33
+ if artifact not in {"predictions", "overlay"}:
34
+ raise gr.Error("Choose a supported export format.")
35
+ label = "Download NPZ" if artifact == "predictions" else "Download PNG"
36
+ path = None
37
+ if volume and volume.prediction:
38
+ path = volume.prediction.export if artifact == "predictions" else png
39
+ ready = path is not None and Path(path).is_file()
40
+ return gr.update(value=str(path) if ready else None, label=label, interactive=ready)
41
+
42
+
43
+ def display_outputs(
44
+ identity,
45
+ frame,
46
+ keys,
47
+ overlay,
48
+ opacity,
49
+ width,
50
+ labels,
51
+ contrast,
52
+ brightness,
53
+ physical,
54
+ artifact="predictions",
55
+ ):
56
+ if not identity:
57
+ return (
58
+ None,
59
+ None,
60
+ "Upload a scan to browse B-scans.",
61
+ download_result(None, artifact),
62
+ )
63
+ volume = STORE.get(identity)
64
+ index = max(0, min(volume.shape[0] - 1, int(frame) - 1))
65
+ volume.view_frame = index
66
+ volume.view_settings = (
67
+ keys,
68
+ overlay,
69
+ opacity,
70
+ width,
71
+ labels,
72
+ contrast,
73
+ brightness,
74
+ physical,
75
+ artifact,
76
+ )
77
+ raw, result = render(
78
+ volume,
79
+ index,
80
+ keys,
81
+ overlay,
82
+ opacity,
83
+ width,
84
+ labels,
85
+ contrast,
86
+ brightness,
87
+ physical,
88
+ )
89
+ caption = f"B-scan {index + 1:,} / {volume.shape[0]:,}"
90
+ png = None
91
+ if result is not None:
92
+ png = volume.directory / f"overlay-bscan-{index + 1:04d}.png"
93
+ Image.fromarray(result).save(png)
94
+ caption += " · " + (
95
+ "full-volume prediction"
96
+ if volume.prediction.complete_volume
97
+ else "single-slice preview"
98
+ )
99
+ elif volume.prediction:
100
+ caption += " · this slice has not been segmented"
101
+ return raw, result, caption, download_result(volume, artifact, png)
102
+
103
+
104
+ def on_upload(
105
+ files,
106
+ previous,
107
+ scanner,
108
+ overlay,
109
+ opacity,
110
+ width,
111
+ labels,
112
+ contrast,
113
+ brightness,
114
+ physical,
115
+ artifact,
116
+ ):
117
+ volume = None
118
+ try:
119
+ volume = load_upload(files)
120
+ identity = STORE.add(volume)
121
+ frame = (volume.shape[0] - 1) // 2 + 1
122
+ family = volume.detected_family if scanner == AUTO else scanner
123
+ choices = boundary_choices(family)
124
+ keys = [value for _, value in choices]
125
+ raw, result, caption, download = display_outputs(
126
+ identity,
127
+ frame,
128
+ keys,
129
+ overlay,
130
+ opacity,
131
+ width,
132
+ labels,
133
+ contrast,
134
+ brightness,
135
+ physical,
136
+ artifact,
137
+ )
138
+ if previous:
139
+ STORE.remove(previous)
140
+ message = "Scan loaded. Check the scanner family, then run inference."
141
+ if family is None:
142
+ message = "Scan loaded. Select its scanner family, then run inference."
143
+ return (
144
+ identity,
145
+ raw,
146
+ result,
147
+ gr.update(
148
+ minimum=1,
149
+ maximum=max(2, volume.shape[0]),
150
+ value=frame,
151
+ interactive=volume.shape[0] > 1,
152
+ ),
153
+ gr.update(choices=choices, value=keys),
154
+ volume_info(volume, family),
155
+ legend(keys),
156
+ caption,
157
+ download,
158
+ message,
159
+ gr.update(interactive=True),
160
+ )
161
+ except Exception as exc:
162
+ if volume is not None:
163
+ STORE.remove(volume.identity)
164
+ raise gr.Error(str(exc)) from exc
165
+
166
+
167
+ def clear_upload(identity, artifact="predictions"):
168
+ STORE.remove(identity)
169
+ return (
170
+ None,
171
+ None,
172
+ None,
173
+ gr.update(minimum=1, maximum=2, value=1, interactive=False),
174
+ gr.update(choices=[], value=[]),
175
+ "No scan loaded.",
176
+ legend([]),
177
+ "Upload a scan to browse B-scans.",
178
+ download_result(None, artifact),
179
+ EMPTY_MESSAGE,
180
+ gr.update(interactive=False),
181
+ )
182
+
183
+
184
+ def clear_volume(identity):
185
+ return (
186
+ *clear_upload(identity),
187
+ gr.update(value=None),
188
+ DEFAULT_SCANNER,
189
+ "predictions",
190
+ )
191
+
192
+
193
+ def scanner_changed(
194
+ identity,
195
+ scanner,
196
+ frame,
197
+ overlay,
198
+ opacity,
199
+ width,
200
+ labels,
201
+ contrast,
202
+ brightness,
203
+ physical,
204
+ artifact,
205
+ ):
206
+ if not identity:
207
+ return (gr.skip(),) * 8
208
+ volume = STORE.get(identity)
209
+ family = volume.detected_family if scanner == AUTO else scanner
210
+ if volume.prediction is not None and volume.prediction.family != family:
211
+ volume.prediction = None
212
+ choices = boundary_choices(family)
213
+ keys = [value for _, value in choices]
214
+ outputs = display_outputs(
215
+ identity,
216
+ frame,
217
+ keys,
218
+ overlay,
219
+ opacity,
220
+ width,
221
+ labels,
222
+ contrast,
223
+ brightness,
224
+ physical,
225
+ artifact,
226
+ )
227
+ return (
228
+ gr.update(choices=choices, value=keys),
229
+ volume_info(volume, family),
230
+ legend(keys),
231
+ *outputs,
232
+ "Scanner selection updated. Run inference to apply it.",
233
+ )
234
+
235
+
236
+ def move_frame(delta):
237
+ def move(identity, frame, *settings):
238
+ if not identity:
239
+ return (gr.skip(),) * 5
240
+ volume = STORE.get(identity)
241
+ next_frame = max(1, min(volume.shape[0], int(frame) + delta))
242
+ return gr.update(value=next_frame), *display_outputs(
243
+ identity, next_frame, *settings
244
+ )
245
+
246
+ return move
247
+
248
+
249
+ def update_display(identity, frame, *settings):
250
+ outputs = display_outputs(identity, frame, *settings)
251
+ return *outputs, legend(settings[0]) if identity else legend([])
252
+
253
+
254
+ def segment(
255
+ identity,
256
+ frame,
257
+ scanner,
258
+ compute,
259
+ scope,
260
+ batch_size,
261
+ *settings,
262
+ progress=gr.Progress(),
263
+ ):
264
+ try:
265
+ volume = STORE.get(identity)
266
+ family = resolve_family(volume, scanner)
267
+ if batch_size is None or not 1 <= int(batch_size) <= 16:
268
+ raise ValueError("Enter a batch size from 1 to 16.")
269
+ progress(0, desc="Loading the saved model")
270
+ yield (
271
+ gr.skip(),
272
+ None,
273
+ "Loading model…",
274
+ download_result(None, settings[-1]),
275
+ "Loading the saved model. First use takes a little longer.",
276
+ gr.update(interactive=False),
277
+ )
278
+ for event in ENGINE.predict(
279
+ volume, family, compute, int(batch_size), scope == "volume", int(frame) - 1
280
+ ):
281
+ if "result" not in event:
282
+ progress(
283
+ event["completed"] / event["total"],
284
+ desc=f"Segmenting B-scans · {event['completed']:,}/{event['total']:,}",
285
+ )
286
+ message = (
287
+ f"Segmenting {event['completed']:,} / {event['total']:,} B-scans · "
288
+ f"{event['device'].upper()} · {event['seconds']:.0f} s elapsed"
289
+ )
290
+ yield (
291
+ gr.skip(),
292
+ gr.skip(),
293
+ gr.skip(),
294
+ gr.skip(),
295
+ message,
296
+ gr.update(interactive=False),
297
+ )
298
+ else:
299
+ result = event["result"]
300
+ outputs = display_outputs(
301
+ identity, volume.view_frame + 1, *(volume.view_settings or settings)
302
+ )
303
+ message = (
304
+ f"**Completed** · {FAMILIES[family]} · {result.compute.upper()} · "
305
+ f"{len(result.frames):,} B-scans · {len(result.keys)} boundaries · "
306
+ f"{result.seconds:.1f} s"
307
+ )
308
+ if not result.complete_volume:
309
+ message += " \nSlice preview omits smoothing between B-scans. Use Full volume for the saved pipeline."
310
+ yield *outputs, message, gr.update(interactive=True)
311
+ except Exception as exc:
312
+ message = html.escape(str(exc))
313
+ if "out of memory" in str(exc).lower():
314
+ message += " Try batch size 1, or select CPU."
315
+ yield (
316
+ gr.skip(),
317
+ gr.skip(),
318
+ gr.skip(),
319
+ gr.skip(),
320
+ f"**Error:** {message}",
321
+ gr.update(interactive=True),
322
+ )
323
+
324
+
325
+ def cancel(identity):
326
+ if identity:
327
+ STORE.get(identity).cancelled = True
328
+ return (
329
+ "Stop requested. Inference stops after the current model batch.",
330
+ gr.update(interactive=True),
331
+ )
332
+
333
+
334
+ def build_app():
335
+ zoom_js = Path(__file__).with_name("viewer.js").read_text()
336
+ with gr.Blocks(
337
+ title="MYXOS · OCT Viewer", analytics_enabled=False, delete_cache=(7200, 7200)
338
+ ) as demo:
339
+ identity = gr.State(value=None, time_to_live=7200, delete_callback=STORE.remove)
340
+ gr.Markdown("# OCT Boundary Explorer")
341
+ with gr.Row():
342
+ with gr.Column(scale=1, min_width=300):
343
+ gr.Markdown("### Configuration")
344
+ upload = gr.File(
345
+ label="OCT volume",
346
+ file_types=[".dcm", ".dicom"],
347
+ file_count="multiple",
348
+ type="filepath",
349
+ height=135,
350
+ )
351
+ scanner = gr.Dropdown(
352
+ choices=[
353
+ *[(name, key) for key, name in FAMILIES.items()],
354
+ ("Detect from DICOM", AUTO),
355
+ ],
356
+ value=DEFAULT_SCANNER,
357
+ label="Scanner family",
358
+ )
359
+ compute = gr.Dropdown(
360
+ choices=compute_choices(),
361
+ value="auto",
362
+ label="Processor",
363
+ info="Auto uses an available GPU, otherwise CPU.",
364
+ )
365
+ scope = gr.Radio(
366
+ choices=[
367
+ ("Full volume", "volume"),
368
+ ("Selected B-scan preview", "preview"),
369
+ ],
370
+ value="volume",
371
+ label="Inference scope",
372
+ )
373
+ with gr.Accordion("Performance settings", open=False):
374
+ batch = gr.Number(
375
+ value=1,
376
+ minimum=1,
377
+ maximum=16,
378
+ step=1,
379
+ precision=0,
380
+ label="Batch size",
381
+ info="Lower this if GPU memory is limited.",
382
+ )
383
+ with gr.Row():
384
+ run = gr.Button(
385
+ "Run inference",
386
+ variant="primary",
387
+ interactive=False,
388
+ scale=3,
389
+ min_width=135,
390
+ )
391
+ stop = gr.Button("Stop", variant="stop", scale=1, min_width=70)
392
+ clear = gr.Button("Clear volume", size="sm")
393
+ scan_info = gr.Markdown("No scan loaded.")
394
+ gr.Markdown(
395
+ "Upload one volume, or its contiguous DICOM parts together."
396
+ )
397
+ with gr.Column(scale=3, min_width=480):
398
+ gr.Markdown("### Boundary viewer")
399
+ messages = gr.Markdown(EMPTY_MESSAGE)
400
+ with gr.Tabs():
401
+ viewers = []
402
+ for tab, label, viewer_id in [
403
+ ("Boundary overlay", "Predicted boundaries", "overlay-viewer"),
404
+ ("Original OCT", "OCT B-scan", "source-viewer"),
405
+ ]:
406
+ with gr.Tab(tab):
407
+ buttons = []
408
+ for text, action in [
409
+ ("Zoom in", "in"),
410
+ ("Zoom out", "out"),
411
+ ("Fit", "fit"),
412
+ ]:
413
+ button = gr.Button(text, size="sm", render=False)
414
+ button.click(
415
+ fn=None,
416
+ js=f"() => ({zoom_js})('{viewer_id}', '{action}')",
417
+ queue=False,
418
+ api_name=False,
419
+ )
420
+ buttons.append(button)
421
+ viewers.append(
422
+ gr.Image(
423
+ label=label,
424
+ type="numpy",
425
+ format="png",
426
+ interactive=False,
427
+ height=480,
428
+ buttons=[*buttons, "fullscreen"],
429
+ elem_id=viewer_id,
430
+ )
431
+ )
432
+ segmented, original = viewers
433
+ gr.Markdown(
434
+ "Use Zoom in / Zoom out, then drag to pan. Fit resets the view."
435
+ )
436
+ with gr.Row():
437
+ previous = gr.Button("Previous", scale=0, min_width=85)
438
+ frame = gr.Slider(
439
+ 1,
440
+ 2,
441
+ value=1,
442
+ step=1,
443
+ label="B-scan",
444
+ scale=6,
445
+ interactive=False,
446
+ )
447
+ following = gr.Button("Next", scale=0, min_width=85)
448
+ caption = gr.Markdown("Upload a scan to browse B-scans.")
449
+ colors = gr.Markdown(legend([]))
450
+ with gr.Accordion("Visible boundaries", open=False):
451
+ boundaries = gr.CheckboxGroup(
452
+ choices=[], value=[], label="Overlay boundaries"
453
+ )
454
+ with gr.Accordion("Display settings", open=False):
455
+ with gr.Row():
456
+ show_overlay = gr.Checkbox(value=True, label="Show boundaries")
457
+ show_labels = gr.Checkbox(value=False, label="Label curves")
458
+ physical = gr.Checkbox(
459
+ value=True, label="Use physical pixel aspect"
460
+ )
461
+ with gr.Row():
462
+ opacity = gr.Slider(
463
+ 0.1, 1.0, value=0.9, step=0.05, label="Overlay opacity"
464
+ )
465
+ width = gr.Slider(1, 5, value=2, step=1, label="Line width")
466
+ with gr.Row():
467
+ contrast = gr.Slider(
468
+ 0.5, 3.0, value=1.0, step=0.05, label="Display contrast"
469
+ )
470
+ brightness = gr.Slider(
471
+ -0.4, 0.4, value=0.0, step=0.02, label="Display brightness"
472
+ )
473
+ with gr.Row():
474
+ artifact = gr.Dropdown(
475
+ choices=[
476
+ ("Boundary predictions · NPZ", "predictions"),
477
+ ("Current B-scan overlay · PNG", "overlay"),
478
+ ],
479
+ value="predictions",
480
+ label="Export format",
481
+ scale=2,
482
+ )
483
+ download = gr.DownloadButton(
484
+ "Download selected output",
485
+ interactive=False,
486
+ scale=1,
487
+ min_width=175,
488
+ )
489
+ gr.Markdown(
490
+ "Display settings change the view only. Predictions retain original OCT coordinates."
491
+ )
492
+
493
+ display_settings = [
494
+ boundaries,
495
+ show_overlay,
496
+ opacity,
497
+ width,
498
+ show_labels,
499
+ contrast,
500
+ brightness,
501
+ physical,
502
+ artifact,
503
+ ]
504
+ settings_without_keys = display_settings[1:]
505
+ display_widgets = [original, segmented, caption, download]
506
+ upload_outputs = [
507
+ identity,
508
+ original,
509
+ segmented,
510
+ frame,
511
+ boundaries,
512
+ scan_info,
513
+ colors,
514
+ caption,
515
+ download,
516
+ messages,
517
+ run,
518
+ ]
519
+ upload.upload(
520
+ on_upload,
521
+ [upload, identity, scanner, *settings_without_keys],
522
+ upload_outputs,
523
+ api_name="load_oct",
524
+ concurrency_id="viewer",
525
+ concurrency_limit=1,
526
+ )
527
+ upload.clear(
528
+ clear_upload,
529
+ [identity, artifact],
530
+ upload_outputs,
531
+ concurrency_id="viewer",
532
+ concurrency_limit=1,
533
+ )
534
+ clear.click(
535
+ clear_volume,
536
+ [identity],
537
+ upload_outputs + [upload, scanner, artifact],
538
+ api_name="clear_volume",
539
+ concurrency_id="viewer",
540
+ concurrency_limit=1,
541
+ )
542
+ scanner.change(
543
+ scanner_changed,
544
+ [identity, scanner, frame, *settings_without_keys],
545
+ [boundaries, scan_info, colors, *display_widgets, messages],
546
+ concurrency_id="viewer",
547
+ concurrency_limit=1,
548
+ )
549
+ frame.release(
550
+ display_outputs,
551
+ [identity, frame, *display_settings],
552
+ display_widgets,
553
+ queue=False,
554
+ api_name="view_bscan",
555
+ )
556
+ for button, delta in [(previous, -1), (following, 1)]:
557
+ button.click(
558
+ move_frame(delta),
559
+ [identity, frame, *display_settings],
560
+ [frame, *display_widgets],
561
+ queue=False,
562
+ api_name=False,
563
+ )
564
+ for control in (show_overlay, show_labels, physical, boundaries):
565
+ control.input(
566
+ update_display,
567
+ [identity, frame, *display_settings],
568
+ [*display_widgets, colors],
569
+ queue=False,
570
+ api_name=False,
571
+ )
572
+ for control in (opacity, width, contrast, brightness):
573
+ control.release(
574
+ display_outputs,
575
+ [identity, frame, *display_settings],
576
+ display_widgets,
577
+ queue=False,
578
+ api_name=False,
579
+ )
580
+ artifact.input(
581
+ display_outputs,
582
+ [identity, frame, *display_settings],
583
+ display_widgets,
584
+ queue=False,
585
+ api_name="select_export",
586
+ )
587
+ inference = run.click(
588
+ segment,
589
+ [identity, frame, scanner, compute, scope, batch, *display_settings],
590
+ [*display_widgets, messages, run],
591
+ api_name="segment_oct",
592
+ show_progress="full",
593
+ show_progress_on=[segmented, original],
594
+ concurrency_id="viewer",
595
+ concurrency_limit=1,
596
+ )
597
+ stop.click(
598
+ cancel,
599
+ [identity],
600
+ [messages, run],
601
+ cancels=[inference],
602
+ queue=False,
603
+ api_name="stop_segmentation",
604
+ )
605
+ return demo
src/inference/__init__.py ADDED
File without changes
src/inference/combination.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Combine model positions only, in the original structural coordinate grid."""
2
+
3
+ import numpy as np
4
+ from src.inference.postprocess import transform
5
+
6
+
7
+ def combine_positions(positions, method="mean", weights=None):
8
+ if not positions:
9
+ raise ValueError("No ensemble members")
10
+ shape = positions[0].shape
11
+ if any(p.shape != shape for p in positions):
12
+ raise ValueError("Ensemble geometry differs")
13
+ stack = np.stack(positions).astype(np.float32, copy=False)
14
+ if not np.isfinite(stack).all():
15
+ raise ValueError("Nonfinite ensemble member")
16
+ if method == "mean":
17
+ if weights is None:
18
+ return stack.mean(axis=0, dtype=np.float32)
19
+ weight = np.asarray(weights, dtype=np.float32)
20
+ if (
21
+ weight.shape != (len(positions),)
22
+ or not np.isfinite(weight).all()
23
+ or (weight < 0).any()
24
+ or weight.sum() <= 0
25
+ ):
26
+ raise ValueError("Invalid ensemble weights")
27
+ weight = weight / weight.sum()
28
+ return (stack * weight.reshape((-1,) + (1,) * len(shape))).sum(
29
+ 0, dtype=np.float32
30
+ )
31
+ if weights is not None:
32
+ raise ValueError("Only means accept weights")
33
+ if method == "median":
34
+ return np.median(stack, axis=0).astype(np.float32)
35
+ raise ValueError("Unknown position combination")
36
+
37
+
38
+ def finish_positions(positions, device, height, configuration):
39
+ return transform(positions, device, height, configuration or {})
40
+
41
+
42
+ def combine_named_heads(positions, member_names, boundary_keys, device, configuration):
43
+ """Select fixed trained heads by device/anatomy; no case-dependent routing."""
44
+ weighted = configuration.get("head_weights_by_device")
45
+ if weighted is not None:
46
+ if (
47
+ "head_source_by_device" in configuration
48
+ or "weights" in configuration
49
+ or configuration.get("method") != "weighted_heads"
50
+ ):
51
+ raise ValueError(
52
+ "Weighted anatomical heads require their own explicit combination method"
53
+ )
54
+ if (
55
+ not positions
56
+ or len(member_names) != len(positions)
57
+ or len(set(member_names)) != len(member_names)
58
+ ):
59
+ raise ValueError("Invalid weighted head sources")
60
+ if not boundary_keys or len(set(boundary_keys)) != len(boundary_keys):
61
+ raise ValueError("Invalid weighted boundary identities")
62
+ shape = positions[0].shape
63
+ if (
64
+ len(shape) != 3
65
+ or shape[0] != len(boundary_keys)
66
+ or any(p.shape != shape or not np.isfinite(p).all() for p in positions)
67
+ ):
68
+ raise ValueError("Invalid weighted head geometry or values")
69
+ if device not in weighted or set(weighted[device]) != set(boundary_keys):
70
+ raise ValueError("Weighted anatomical identities differ")
71
+ output = []
72
+ for index, key in enumerate(boundary_keys):
73
+ rule = weighted[device][key]
74
+ if (
75
+ not isinstance(rule, dict)
76
+ or not 1 <= len(rule) <= 2
77
+ or not set(rule) <= set(member_names)
78
+ ):
79
+ raise ValueError("One or two known weighted heads are required")
80
+ names = [name for name in member_names if name in rule]
81
+ weights = np.asarray([rule[name] for name in names], dtype=np.float32)
82
+ if (
83
+ not np.isfinite(weights).all()
84
+ or (weights <= 0).any()
85
+ or abs(float(weights.sum()) - 1) > 1e-6
86
+ ):
87
+ raise ValueError(
88
+ "Weighted head coefficients must be positive and sum to one"
89
+ )
90
+ output.append(
91
+ combine_positions(
92
+ [positions[member_names.index(name)][index] for name in names],
93
+ "mean",
94
+ weights,
95
+ )
96
+ )
97
+ return np.stack(output).astype(np.float32, copy=False)
98
+ mapping = configuration.get("head_source_by_device")
99
+ if mapping is None:
100
+ return combine_positions(
101
+ positions, configuration.get("method", "mean"), configuration.get("weights")
102
+ )
103
+ if "weights" in configuration or configuration.get("method", "mean") != "mean":
104
+ raise ValueError("Head selection cannot also configure averaging weights")
105
+ if len(member_names) != len(positions) or len(set(member_names)) != len(
106
+ member_names
107
+ ):
108
+ raise ValueError("Invalid named head sources")
109
+ if not positions or len(set(boundary_keys)) != len(boundary_keys):
110
+ raise ValueError("Invalid boundary identities")
111
+ shape = positions[0].shape
112
+ if (
113
+ len(shape) != 3
114
+ or shape[0] != len(boundary_keys)
115
+ or any(p.shape != shape for p in positions)
116
+ ):
117
+ raise ValueError("Head-source prediction geometry differs")
118
+ if device not in mapping or set(mapping[device]) != set(boundary_keys):
119
+ raise ValueError("Head-source anatomical identities differ")
120
+ if any(name not in member_names for name in mapping[device].values()):
121
+ raise ValueError("Unknown selected head source")
122
+ if any(not np.isfinite(p).all() for p in positions):
123
+ raise ValueError("Nonfinite head source")
124
+ return np.stack(
125
+ [
126
+ positions[member_names.index(mapping[device][key])][index]
127
+ for index, key in enumerate(boundary_keys)
128
+ ]
129
+ ).astype(np.float32, copy=False)
src/inference/context.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Bounded native B-scan windows, including adjacent/distant and global views."""
2
+
3
+ import numpy as np
4
+ from src.io import iter_volume_frames, normalize_frame, read_frames
5
+
6
+ OFFSETS = {1: [0], 3: [-1, 0, 1], 5: [-8, -1, 0, 1, 8], 6: [-1, 0, 1]}
7
+
8
+
9
+ def iter_normalized_contexts(headers, shapes):
10
+ if any(shape[2] not in OFFSETS for shape in shapes):
11
+ raise ValueError("Unsupported OCT image context")
12
+ spatial = sorted({tuple(shape[:2]) for shape in shapes})
13
+ total = sum(int(ds.get("NumberOfFrames", 1)) for _, ds in headers)
14
+ if total < 1:
15
+ raise ValueError("Empty structural volume")
16
+ global_views = {}
17
+ if any(shape[2] == 6 for shape in shapes):
18
+ selected = [0, total // 2, total - 1]
19
+ raw = read_frames(headers, sorted(set(selected)))
20
+ for shape in spatial:
21
+ global_views[shape] = np.stack(
22
+ [normalize_frame(raw[index], shape) for index in selected]
23
+ )
24
+ radius = max(abs(offset) for shape in shapes for offset in OFFSETS[shape[2]])
25
+ iterator = iter(iter_volume_frames(headers))
26
+ cache = {}
27
+ loaded = -1
28
+ for center in range(total):
29
+ last = min(total - 1, center + radius)
30
+ while loaded < last:
31
+ frame = next(iterator, None)
32
+ if frame is None:
33
+ raise ValueError("Structural frames ended before declared geometry")
34
+ loaded += 1
35
+ cache[loaded] = {shape: normalize_frame(frame, shape) for shape in spatial}
36
+ images = []
37
+ for shape in shapes:
38
+ key = tuple(shape[:2])
39
+ indices = [
40
+ max(0, min(total - 1, center + offset)) for offset in OFFSETS[shape[2]]
41
+ ]
42
+ image = np.stack([cache[index][key] for index in indices])
43
+ if shape[2] == 6:
44
+ image = np.concatenate([image, global_views[key]], axis=0)
45
+ images.append(image)
46
+ yield images
47
+ oldest = center + 1 - radius
48
+ for index in list(cache):
49
+ if index < oldest:
50
+ del cache[index]
51
+ if next(iterator, None) is not None:
52
+ raise ValueError("Surplus structural frames")
src/inference/decoding.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Alternative point estimators for axial categorical distributions."""
2
+
3
+ import torch
4
+
5
+
6
+ def estimates(logits, names):
7
+ probability = logits.float().softmax(-2)
8
+ height = logits.shape[-2]
9
+ grid = torch.arange(height, device=logits.device, dtype=torch.float32)[
10
+ None, None, :, None
11
+ ]
12
+ result = {}
13
+ if "mean" in names:
14
+ result["mean"] = (probability * grid).sum(-2)
15
+ if "median" in names:
16
+ cumulative = probability.cumsum(-2)
17
+ index = (cumulative < 0.5).sum(-2).clamp_max(height - 1)
18
+ mass = probability.gather(-2, index.unsqueeze(-2)).squeeze(-2)
19
+ before = cumulative.gather(-2, (index - 1).clamp_min(0).unsqueeze(-2)).squeeze(
20
+ -2
21
+ )
22
+ before = torch.where(index == 0, torch.zeros_like(before), before)
23
+ result["median"] = (
24
+ index.float() - 0.5 + (0.5 - before) / mass.clamp_min(1e-12)
25
+ ).clamp(0, height - 1)
26
+ if "quadratic_mode" in names:
27
+ index = logits.argmax(-2)
28
+ score = logits.float()
29
+ center = score.gather(-2, index.unsqueeze(-2)).squeeze(-2)
30
+ left = score.gather(-2, (index - 1).clamp_min(0).unsqueeze(-2)).squeeze(-2)
31
+ right = score.gather(
32
+ -2, (index + 1).clamp_max(height - 1).unsqueeze(-2)
33
+ ).squeeze(-2)
34
+ curvature = left - 2 * center + right
35
+ offset = torch.where(
36
+ (curvature < -1e-8) & (index > 0) & (index < height - 1),
37
+ 0.5 * (left - right) / curvature.clamp_max(-1e-8),
38
+ torch.zeros_like(center),
39
+ ).clamp(-0.5, 0.5)
40
+ result["quadratic_mode"] = (index.float() + offset).clamp(0, height - 1)
41
+ if set(result) != set(names):
42
+ raise ValueError("Unknown posterior estimator")
43
+ return result
44
+
45
+
46
+ def positions_from_logits(logits, config, device=None):
47
+ by_device = config.get("inference", {}).get("posterior_by_device")
48
+ if by_device is not None:
49
+ if device is None or device not in by_device:
50
+ raise ValueError("Configured head estimators require the device")
51
+ keys = config["device_keys"][device]
52
+ mapping = by_device[device]
53
+ if set(mapping) != set(keys) or logits.shape[1] != len(keys):
54
+ raise ValueError("Configured head estimators differ from output identities")
55
+ names = [mapping[key] for key in keys]
56
+ values = estimates(logits, sorted(set(names)))
57
+ return torch.stack(
58
+ [values[name][:, index] for index, name in enumerate(names)], dim=1
59
+ )
60
+ name = config.get("inference", {}).get("posterior", "mean")
61
+ if name not in ["mean", "median", "quadratic_mode"]:
62
+ raise ValueError("Unsupported configured posterior")
63
+ return estimates(logits, [name])[name]
src/inference/ensemble.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Label-free mixed-resolution/context ensemble inference, one OCT decode pass."""
2
+
3
+ import json
4
+ import time
5
+ import numpy as np
6
+ import torch
7
+ from torch.nn import functional as F
8
+ from torch.utils.data import DataLoader
9
+ from src.models import build_model, output_indices
10
+ from src.inference.stream import OCTBatches
11
+ from src.inference.runtime import autocast_options
12
+ from src.inference.decoding import positions_from_logits
13
+ from src.inference.combination import finish_positions, combine_named_heads
14
+ from src.inference.writer import PredictionWriter
15
+
16
+
17
+ def stream_ensemble(
18
+ rows, root, checkpoint, output_dir, device, batch_size, workers, precision
19
+ ):
20
+ config = checkpoint["config"]
21
+ members = []
22
+ for saved in checkpoint["members"]:
23
+ cfg = saved["config"]
24
+ model = build_model(cfg)
25
+ model.load_state_dict(saved["model"])
26
+ model.to(device).eval().requires_grad_(False)
27
+ members.append({"name": saved["name"], "config": cfg, "model": model})
28
+ if not members or len({m["name"] for m in members}) != len(members):
29
+ raise ValueError("Invalid ensemble members")
30
+ head_routes = config["combination"].get("head_source_by_device")
31
+ active_by_device = {}
32
+ if head_routes is not None:
33
+ if set(head_routes) != set(config["device_keys"]):
34
+ raise ValueError("Head-source device families differ")
35
+ available = {member["name"] for member in members}
36
+ for family, keys in config["device_keys"].items():
37
+ if set(head_routes[family]) != set(keys):
38
+ raise ValueError("Head-source anatomical identities differ")
39
+ active_by_device[family] = set(head_routes[family].values())
40
+ if not active_by_device[family] <= available:
41
+ raise ValueError("Unknown selected head source")
42
+ shapes = sorted(
43
+ {
44
+ (
45
+ m["config"]["height"],
46
+ m["config"]["width"],
47
+ m["config"].get("input_channels", 1),
48
+ )
49
+ for m in members
50
+ }
51
+ )
52
+ loader = DataLoader(
53
+ OCTBatches(rows, root, shapes, batch_size),
54
+ batch_size=None,
55
+ num_workers=workers,
56
+ pin_memory=device.startswith("cuda"),
57
+ **({"prefetch_factor": 2} if workers else {})
58
+ )
59
+ writer = PredictionWriter(output_dir, workers=2, maximum_pending_cases=2)
60
+ # PredictionWriter uses named subdirectories; final files belong at root.
61
+ pending = {}
62
+ done = 0
63
+ start = time.monotonic()
64
+ evaluated_frames = {}
65
+ for batch in loader:
66
+ index, offset = int(batch["index"]), int(batch["offset"])
67
+ b, h, a = map(int, batch["geometry"])
68
+ row = rows[index]
69
+ n = len(batch["images"][0])
70
+ keys = config["device_keys"][row["device"]]
71
+ if index not in pending:
72
+ buffers = []
73
+ for member in members:
74
+ if (
75
+ head_routes is not None
76
+ and member["name"] not in active_by_device[row["device"]]
77
+ ):
78
+ continue
79
+ cfg = member["config"]
80
+ own_keys = cfg["device_keys"][row["device"]]
81
+ if set(own_keys) != set(keys) or len(own_keys) != len(keys):
82
+ raise ValueError("Ensemble boundary vocabulary differs")
83
+ shape = (cfg["height"], cfg["width"], cfg.get("input_channels", 1))
84
+ buffers.append(
85
+ {
86
+ "member": member,
87
+ "shape": shape,
88
+ "indices": output_indices(cfg, row["device"]),
89
+ "order": [own_keys.index(k) for k in keys],
90
+ "positions": np.empty((len(keys), b, a), np.float32),
91
+ }
92
+ )
93
+ pending[index] = {"seen": np.zeros(b, bool), "buffers": buffers}
94
+ state = pending[index]
95
+ if offset + n > b or state["seen"][offset : offset + n].any():
96
+ raise ValueError("Invalid frame coverage")
97
+ tensors = [image.to(device, non_blocking=True) for image in batch["images"]]
98
+ with torch.inference_mode(), torch.autocast(
99
+ **autocast_options(device, precision)
100
+ ):
101
+ for buffer in state["buffers"]:
102
+ member = buffer["member"]
103
+ cfg = member["config"]
104
+ family_counts = evaluated_frames.setdefault(row["device"], {})
105
+ family_counts[member["name"]] = family_counts.get(member["name"], 0) + n
106
+ position = positions_from_logits(
107
+ member["model"](tensors[shapes.index(buffer["shape"])])[
108
+ :, buffer["indices"]
109
+ ],
110
+ cfg,
111
+ row["device"],
112
+ )
113
+ position = F.interpolate(
114
+ position, size=a, mode="linear", align_corners=False
115
+ )
116
+ position = ((position + 0.5) * h / cfg["height"] - 0.5).clamp(0, h - 1)
117
+ values = position.float().cpu().numpy().transpose(1, 0, 2)
118
+ buffer["positions"][:, offset : offset + n] = values[buffer["order"]]
119
+ state["seen"][offset : offset + n] = True
120
+ if state["seen"].all():
121
+ combination = config["combination"]
122
+ components = [
123
+ finish_positions(
124
+ item["positions"],
125
+ row["device"],
126
+ h,
127
+ item["member"]["config"]
128
+ .get("inference", {})
129
+ .get("postprocess", {}),
130
+ )
131
+ for item in state["buffers"]
132
+ ]
133
+ values = combine_named_heads(
134
+ components,
135
+ [item["member"]["name"] for item in state["buffers"]],
136
+ keys,
137
+ row["device"],
138
+ combination,
139
+ )
140
+ values = finish_positions(
141
+ values,
142
+ row["device"],
143
+ h,
144
+ config.get("inference", {}).get("postprocess", {}),
145
+ )
146
+ if not np.isfinite(values).all():
147
+ raise ValueError("Nonfinite ensemble output")
148
+ writer.submit(row["case_id"], [("", values, keys)])
149
+ del pending[index]
150
+ done += 1
151
+ if done % 10 == 0:
152
+ print(
153
+ json.dumps(
154
+ {
155
+ "completed_cases": done,
156
+ "elapsed_seconds": time.monotonic() - start,
157
+ }
158
+ ),
159
+ flush=True,
160
+ )
161
+ writer.close()
162
+ if pending or done != len(rows):
163
+ raise ValueError("Incomplete ensemble input manifest")
164
+ summary = {
165
+ "completed_cases": done,
166
+ "members": len(members),
167
+ "elapsed_seconds": time.monotonic() - start,
168
+ "evaluated_member_frames_by_device": evaluated_frames,
169
+ "static_inactive_networks_skipped": head_routes is not None,
170
+ }
171
+ print(json.dumps(summary), flush=True)
172
+ return summary
src/inference/postprocess.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Prediction-only native-grid filters; no reference arrays or masks are inputs."""
2
+
3
+ import numpy as np
4
+ from scipy.ndimage import gaussian_filter, median_filter
5
+
6
+ INTEGER_DEVICES = ["topcon_maestro2", "topcon_triton", "zeiss_cirrus"]
7
+
8
+
9
+ def transform(positions, device, height, configuration):
10
+ if "by_device" in configuration:
11
+ overrides = configuration["by_device"].get(device, {})
12
+ configuration = {
13
+ **{k: v for k, v in configuration.items() if k != "by_device"},
14
+ **overrides,
15
+ }
16
+ values = np.asarray(positions, dtype=np.float32)
17
+ if "boundary_filters" in configuration:
18
+ rules = configuration["boundary_filters"]
19
+ if (
20
+ not isinstance(rules, list)
21
+ or len(rules) != values.shape[0]
22
+ or not all(isinstance(rule, dict) for rule in rules)
23
+ ):
24
+ raise ValueError(
25
+ "One fixed filter configuration is required for every output boundary"
26
+ )
27
+ common = {
28
+ key: value
29
+ for key, value in configuration.items()
30
+ if key != "boundary_filters"
31
+ }
32
+ return np.concatenate(
33
+ [
34
+ transform(values[index : index + 1], device, height, {**common, **rule})
35
+ for index, rule in enumerate(rules)
36
+ ],
37
+ axis=0,
38
+ )
39
+ kind = configuration.get("filter", "none")
40
+ if kind == "gaussian":
41
+ values = gaussian_filter(
42
+ values,
43
+ sigma=(
44
+ 0,
45
+ float(configuration.get("sigma_b", 0)),
46
+ float(configuration.get("sigma_a", 0)),
47
+ ),
48
+ mode="nearest",
49
+ truncate=3,
50
+ )
51
+ elif kind == "median":
52
+ values = median_filter(
53
+ values,
54
+ size=(
55
+ 1,
56
+ int(configuration.get("size_b", 1)),
57
+ int(configuration.get("size_a", 1)),
58
+ ),
59
+ mode="nearest",
60
+ )
61
+ elif kind != "none":
62
+ raise ValueError("Unknown prediction filter")
63
+ if device in configuration.get("round_devices", []):
64
+ values = np.rint(values)
65
+ return np.clip(values, 0, height - 1).astype(np.float32)
66
+
67
+
68
+ CONFIGURATIONS = {
69
+ "round_integer_devices": {"round_devices": INTEGER_DEVICES},
70
+ "gaussian_b05": {"filter": "gaussian", "sigma_b": 0.5},
71
+ "gaussian_b1": {"filter": "gaussian", "sigma_b": 1.0},
72
+ "median_b3": {"filter": "median", "size_b": 3},
73
+ "gaussian_b05_round": {
74
+ "filter": "gaussian",
75
+ "sigma_b": 0.5,
76
+ "round_devices": INTEGER_DEVICES,
77
+ },
78
+ "gaussian_b1_round": {
79
+ "filter": "gaussian",
80
+ "sigma_b": 1.0,
81
+ "round_devices": INTEGER_DEVICES,
82
+ },
83
+ "median_b3_round": {
84
+ "filter": "median",
85
+ "size_b": 3,
86
+ "round_devices": INTEGER_DEVICES,
87
+ },
88
+ }
src/inference/predict.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Fresh-process, label-free OCT boundary inference. Network access is unnecessary."""
2
+
3
+ from pathlib import Path
4
+ import argparse
5
+ import json
6
+ import time
7
+ import numpy as np
8
+ import torch
9
+ from torch.nn import functional as F
10
+ from src.inference.decoding import positions_from_logits
11
+ from src.models import build_model, output_indices
12
+ from src.inference.runtime import configure_inference, autocast_options
13
+ from src.io import load_manifest, volume_headers
14
+ from src.inference.context import iter_normalized_contexts
15
+ from src.inference.combination import finish_positions
16
+
17
+
18
+ def predict_case(
19
+ row, data_root, model, config, device, batch_size=8, tta=False, precision="fp32"
20
+ ):
21
+ headers, (b, h, a) = volume_headers(data_root, row["oct_paths"])
22
+ keys = config["device_keys"][row["device"]]
23
+ indices = output_indices(config, row["device"])
24
+ values = np.empty((len(keys), b, a), np.float32)
25
+ shape = (config["height"], config["width"])
26
+ batch = []
27
+ completed = 0
28
+
29
+ def consume(images):
30
+ nonlocal completed
31
+ x = torch.from_numpy(np.stack(images)).to(device)
32
+ with torch.inference_mode(), torch.autocast(
33
+ **autocast_options(device, precision)
34
+ ):
35
+ pred = positions_from_logits(model(x)[:, indices], config, row["device"])
36
+ if tta:
37
+ pred = (
38
+ pred
39
+ + positions_from_logits(
40
+ model(x.flip(-1))[:, indices], config, row["device"]
41
+ ).flip(-1)
42
+ ) * 0.5
43
+ pred = F.interpolate(pred, size=a, mode="linear", align_corners=False)
44
+ pred = (pred + 0.5) * h / shape[0] - 0.5
45
+ pred = pred.clamp(0, h - 1)
46
+ arr = pred.float().cpu().numpy().transpose(1, 0, 2)
47
+ values[:, completed : completed + len(images)] = arr
48
+ completed += len(images)
49
+
50
+ for images in iter_normalized_contexts(
51
+ headers, [(shape[0], shape[1], config.get("input_channels", 1))]
52
+ ):
53
+ batch.append(images[0])
54
+ if len(batch) == batch_size:
55
+ consume(batch)
56
+ batch = []
57
+ if batch:
58
+ consume(batch)
59
+ if completed != b or not np.isfinite(values).all():
60
+ raise ValueError("Incomplete or nonfinite prediction")
61
+ return finish_positions(
62
+ values, row["device"], h, config.get("inference", {}).get("postprocess", {})
63
+ ), np.asarray(keys)
64
+
65
+
66
+ def main():
67
+ ap = argparse.ArgumentParser()
68
+ ap.add_argument("--input-manifest", required=True)
69
+ ap.add_argument("--data-root", required=True)
70
+ ap.add_argument("--checkpoint", required=True)
71
+ ap.add_argument("--output-dir", required=True)
72
+ ap.add_argument("--batch-size", type=int, default=32)
73
+ ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
74
+ ap.add_argument("--tta", action="store_true")
75
+ ap.add_argument("--workers", type=int, default=4)
76
+ ap.add_argument("--precision", choices=["fp32", "bf16", "tf32"], default=None)
77
+ ap.add_argument("--shard-index", type=int, default=0)
78
+ ap.add_argument("--num-shards", type=int, default=1)
79
+ args = ap.parse_args()
80
+ # Checkpoint contains model tensors and simple configuration; never labels,
81
+ # participant IDs, paths or a lookup table indexed by acquisition identifiers.
82
+ ck = torch.load(args.checkpoint, map_location="cpu", weights_only=True)
83
+ config = ck["config"]
84
+ args.precision = args.precision or config.get("inference", {}).get(
85
+ "precision", "fp32"
86
+ )
87
+ configure_inference(args.precision)
88
+ if config.get("architecture") == "BoundaryEnsemble":
89
+ from src.inference.ensemble import stream_ensemble
90
+
91
+ rows = [
92
+ {k: r[k] for k in ["case_id", "device", "oct_paths"]}
93
+ for r in load_manifest(args.input_manifest)
94
+ ]
95
+ selected = [
96
+ r for i, r in enumerate(rows) if i % args.num_shards == args.shard_index
97
+ ]
98
+ out = Path(args.output_dir)
99
+ out.mkdir(parents=True, exist_ok=True)
100
+ if args.tta:
101
+ raise ValueError("TTA must be explicitly validated before ensemble use")
102
+ stream_ensemble(
103
+ selected,
104
+ args.data_root,
105
+ ck,
106
+ out,
107
+ args.device,
108
+ args.batch_size,
109
+ args.workers,
110
+ args.precision,
111
+ )
112
+ return
113
+ model = build_model(config)
114
+ model.load_state_dict(ck["model"])
115
+ model.to(args.device).eval()
116
+ rows = load_manifest(args.input_manifest)
117
+ # Drop every field except the label-free contract, even if a caller passes more.
118
+ rows = [{k: r[k] for k in ["case_id", "device", "oct_paths"]} for r in rows]
119
+ out = Path(args.output_dir)
120
+ out.mkdir(parents=True, exist_ok=True)
121
+ t = time.monotonic()
122
+ count = 0
123
+ if args.workers:
124
+ from src.inference.stream import stream_predictions
125
+
126
+ selected = [
127
+ r for i, r in enumerate(rows) if i % args.num_shards == args.shard_index
128
+ ]
129
+ stream_predictions(
130
+ selected,
131
+ args.data_root,
132
+ [{"model": model, "config": config, "destination": out}],
133
+ args.device,
134
+ args.batch_size,
135
+ args.workers,
136
+ args.tta,
137
+ args.precision,
138
+ )
139
+ return
140
+ for i, row in enumerate(rows):
141
+ if i % args.num_shards != args.shard_index:
142
+ continue
143
+ target = out / (row["case_id"] + ".npz")
144
+ positions, keys = predict_case(
145
+ row,
146
+ args.data_root,
147
+ model,
148
+ config,
149
+ args.device,
150
+ args.batch_size,
151
+ args.tta,
152
+ args.precision,
153
+ )
154
+ tmp = target.with_suffix(".tmp.npz")
155
+ np.savez_compressed(tmp, positions=positions, boundary_keys=keys)
156
+ tmp.replace(target)
157
+ count += 1
158
+ if count % 10 == 0:
159
+ print(
160
+ json.dumps(
161
+ {
162
+ "completed_cases": count,
163
+ "elapsed_seconds": time.monotonic() - t,
164
+ "shard": args.shard_index,
165
+ }
166
+ ),
167
+ flush=True,
168
+ )
169
+ print(
170
+ json.dumps(
171
+ {
172
+ "completed_cases": count,
173
+ "elapsed_seconds": time.monotonic() - t,
174
+ "shard": args.shard_index,
175
+ }
176
+ ),
177
+ flush=True,
178
+ )
179
+
180
+
181
+ if __name__ == "__main__":
182
+ main()
src/inference/runtime.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Explicit inference numerics, shared by single/multiple-checkpoint runners."""
2
+
3
+ import torch
4
+
5
+
6
+ def configure_inference(precision):
7
+ if precision not in ["fp32", "bf16", "tf32"]:
8
+ raise ValueError("Unknown inference precision")
9
+ torch.set_num_threads(4)
10
+ torch.backends.cudnn.benchmark = False
11
+ torch.backends.cudnn.deterministic = True
12
+ torch.backends.cudnn.allow_tf32 = precision == "tf32"
13
+ torch.backends.cuda.matmul.allow_tf32 = precision == "tf32"
14
+ torch.set_float32_matmul_precision("high" if precision == "tf32" else "highest")
15
+
16
+
17
+ def autocast_options(device, precision):
18
+ cuda = device.startswith("cuda")
19
+ enabled = cuda and precision == "bf16"
20
+ if enabled and not torch.cuda.is_bf16_supported():
21
+ raise ValueError("BF16 inference requested on an unsupported GPU")
22
+ return {
23
+ "device_type": "cuda" if cuda else "cpu",
24
+ "dtype": torch.bfloat16,
25
+ "enabled": enabled,
26
+ }
src/inference/stream.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Bounded parallel, label-free OCT decoding for optional faster inference."""
2
+
3
+ from pathlib import Path
4
+ import json
5
+ import time
6
+ import numpy as np
7
+ import torch
8
+ from torch.nn import functional as F
9
+ from torch.utils.data import IterableDataset, DataLoader, get_worker_info
10
+ from src.inference.decoding import positions_from_logits
11
+ from src.inference.combination import finish_positions
12
+ from src.models import output_indices
13
+ from src.inference.runtime import autocast_options
14
+ from src.io import volume_headers
15
+ from src.inference.context import iter_normalized_contexts
16
+
17
+
18
+ class OCTBatches(IterableDataset):
19
+ def __init__(self, rows, root, shapes, batch_size):
20
+ self.rows, self.root = rows, root
21
+ self.shapes, self.batch_size = shapes, batch_size
22
+
23
+ def __iter__(self):
24
+ worker = get_worker_info()
25
+ worker_index, workers = (worker.id, worker.num_workers) if worker else (0, 1)
26
+ for index in range(worker_index, len(self.rows), workers):
27
+ row = self.rows[index]
28
+ headers, geometry = volume_headers(self.root, row["oct_paths"])
29
+ batches = [[] for _ in self.shapes]
30
+ completed = 0
31
+
32
+ def item():
33
+ nonlocal completed
34
+ n = len(batches[0])
35
+ if completed + n > geometry[0]:
36
+ raise ValueError(
37
+ "Decoded frames exceed declared structural geometry"
38
+ )
39
+ result = {
40
+ "index": index,
41
+ "offset": completed,
42
+ "geometry": geometry,
43
+ "images": [np.stack(batch) for batch in batches],
44
+ }
45
+ completed += n
46
+ for batch in batches:
47
+ batch.clear()
48
+ return result
49
+
50
+ for images in iter_normalized_contexts(headers, self.shapes):
51
+ for frame, batch in zip(images, batches):
52
+ batch.append(frame)
53
+ if len(batches[0]) == self.batch_size:
54
+ yield item()
55
+ if batches[0]:
56
+ yield item()
57
+ if completed != geometry[0]:
58
+ raise ValueError("Incomplete structural frame coverage")
59
+
60
+
61
+ def stream_predictions(
62
+ rows, root, candidates, device, batch_size, workers, tta=False, precision="fp32"
63
+ ):
64
+ """Candidates contain loaded models/configs/destinations; no reference inputs."""
65
+ shapes = sorted(
66
+ {
67
+ (
68
+ c["config"]["height"],
69
+ c["config"]["width"],
70
+ c["config"].get("input_channels", 1),
71
+ )
72
+ for c in candidates
73
+ }
74
+ )
75
+ dataset = OCTBatches(rows, root, shapes, batch_size)
76
+ loader = DataLoader(
77
+ dataset,
78
+ batch_size=None,
79
+ num_workers=workers,
80
+ pin_memory=device.startswith("cuda"),
81
+ **({"prefetch_factor": 2} if workers else {})
82
+ )
83
+ pending, done = {}, 0
84
+ start = time.monotonic()
85
+ for batch in loader:
86
+ index, offset = int(batch["index"]), int(batch["offset"])
87
+ b, h, a = map(int, batch["geometry"])
88
+ row = rows[index]
89
+ n = len(batch["images"][0])
90
+ if index not in pending:
91
+ pending[index] = {"seen": np.zeros(b, bool), "buffers": []}
92
+ for candidate in candidates:
93
+ config = candidate["config"]
94
+ keys = config["device_keys"][row["device"]]
95
+ shape = (
96
+ config["height"],
97
+ config["width"],
98
+ config.get("input_channels", 1),
99
+ )
100
+ pending[index]["buffers"].append(
101
+ {
102
+ "candidate": candidate,
103
+ "shape_index": shapes.index(shape),
104
+ "height": shape[0],
105
+ "keys": keys,
106
+ "indices": output_indices(config, row["device"]),
107
+ "positions": np.empty((len(keys), b, a), np.float32),
108
+ }
109
+ )
110
+ state = pending[index]
111
+ if state["seen"][offset : offset + n].any() or offset + n > b:
112
+ raise ValueError("Duplicate or out-of-range structural frames")
113
+ tensors = [image.to(device, non_blocking=True) for image in batch["images"]]
114
+ with torch.inference_mode(), torch.autocast(
115
+ **autocast_options(device, precision)
116
+ ):
117
+ for buffer in state["buffers"]:
118
+ x = tensors[buffer["shape_index"]]
119
+ model = buffer["candidate"]["model"]
120
+ pred = positions_from_logits(
121
+ model(x)[:, buffer["indices"]],
122
+ buffer["candidate"]["config"],
123
+ row["device"],
124
+ )
125
+ if tta:
126
+ pred = (
127
+ pred
128
+ + positions_from_logits(
129
+ model(x.flip(-1))[:, buffer["indices"]],
130
+ buffer["candidate"]["config"],
131
+ row["device"],
132
+ ).flip(-1)
133
+ ) * 0.5
134
+ pred = F.interpolate(pred, size=a, mode="linear", align_corners=False)
135
+ pred = ((pred + 0.5) * h / buffer["height"] - 0.5).clamp(0, h - 1)
136
+ buffer["positions"][:, offset : offset + n] = (
137
+ pred.float().cpu().numpy().transpose(1, 0, 2)
138
+ )
139
+ state["seen"][offset : offset + n] = True
140
+ if state["seen"].all():
141
+ for buffer in state["buffers"]:
142
+ values = finish_positions(
143
+ buffer["positions"],
144
+ row["device"],
145
+ h,
146
+ buffer["candidate"]["config"]
147
+ .get("inference", {})
148
+ .get("postprocess", {}),
149
+ )
150
+ if not np.isfinite(values).all():
151
+ raise ValueError("Nonfinite candidate prediction")
152
+ destination = Path(buffer["candidate"]["destination"])
153
+ path = destination / (row["case_id"] + ".npz")
154
+ temporary = path.with_suffix(".tmp.npz")
155
+ np.savez_compressed(
156
+ temporary,
157
+ positions=values,
158
+ boundary_keys=np.asarray(buffer["keys"]),
159
+ )
160
+ temporary.replace(path)
161
+ del pending[index]
162
+ done += 1
163
+ if done % 10 == 0:
164
+ print(
165
+ json.dumps(
166
+ {
167
+ "completed_cases": done,
168
+ "elapsed_seconds": time.monotonic() - start,
169
+ "decode_workers": workers,
170
+ }
171
+ ),
172
+ flush=True,
173
+ )
174
+ if pending or done != len(rows):
175
+ raise ValueError("Incomplete inference manifest coverage")
176
+ report = {
177
+ "completed_cases": done,
178
+ "elapsed_seconds": time.monotonic() - start,
179
+ "decode_workers": workers,
180
+ }
181
+ print(json.dumps(report), flush=True)
182
+ return report
src/inference/writer.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Bounded asynchronous NPZ serialization; values and dtypes are preserved."""
2
+
3
+ from collections import deque
4
+ from concurrent.futures import ThreadPoolExecutor
5
+ from pathlib import Path
6
+ import zipfile
7
+ import numpy as np
8
+
9
+
10
+ def save_npz(path, arrays, level=1):
11
+ path = Path(path)
12
+ temporary = path.with_suffix(".tmp.npz")
13
+ with zipfile.ZipFile(
14
+ temporary,
15
+ "w",
16
+ compression=zipfile.ZIP_DEFLATED,
17
+ compresslevel=level,
18
+ allowZip64=True,
19
+ ) as archive:
20
+ for name, value in arrays.items():
21
+ with archive.open(name + ".npy", "w", force_zip64=True) as handle:
22
+ np.lib.format.write_array(handle, np.asarray(value), allow_pickle=False)
23
+ temporary.replace(path)
24
+
25
+
26
+ class PredictionWriter:
27
+ def __init__(self, output_dir, workers=2, maximum_pending_cases=2):
28
+ self.output_dir = Path(output_dir)
29
+ self.queue = deque()
30
+ self.maximum_pending_cases = maximum_pending_cases
31
+ self.executor = ThreadPoolExecutor(max_workers=workers)
32
+
33
+ def submit(self, case_id, items):
34
+ # Keep at most this many completed-volume arrays alive behind inference.
35
+ while len(self.queue) >= self.maximum_pending_cases:
36
+ self.queue.popleft().result()
37
+
38
+ def write_case():
39
+ for name, positions, keys in items:
40
+ save_npz(
41
+ self.output_dir / name / (case_id + ".npz"),
42
+ {"positions": positions, "boundary_keys": np.asarray(keys)},
43
+ )
44
+
45
+ self.queue.append(self.executor.submit(write_case))
46
+
47
+ def close(self):
48
+ try:
49
+ while self.queue:
50
+ self.queue.popleft().result()
51
+ finally:
52
+ self.executor.shutdown(wait=True, cancel_futures=True)
src/io.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Label-free structural OCT I/O; no segmentation dependency."""
2
+ from pathlib import Path
3
+ import json,re
4
+ import numpy as np
5
+ import pydicom
6
+ from pydicom.pixels import pixel_array,iter_pixels
7
+ from PIL import Image
8
+
9
+ def load_manifest(path):
10
+ rows=[json.loads(s) for s in Path(path).read_text().splitlines() if s.strip()]
11
+ seen=set()
12
+ for row in rows:
13
+ name=row['case_id']
14
+ if not isinstance(name,str) or not re.fullmatch(r'[A-Za-z0-9_-]{1,64}',name):raise ValueError('Unsafe case identifier')
15
+ if name in seen:raise ValueError('Duplicate case')
16
+ seen.add(name)
17
+ return rows
18
+
19
+ def safe_path(root,relative):
20
+ p=Path(relative)
21
+ if p.is_absolute() or '..' in p.parts:raise ValueError('Input path must be dataset-relative')
22
+ return Path(root)/p
23
+
24
+ def volume_headers(root,oct_paths,with_spacing=False):
25
+ tags=['SOPClassUID','Rows','Columns','NumberOfFrames','ConcatenationFrameOffsetNumber']
26
+ if with_spacing:tags+=['SharedFunctionalGroupsSequence','PerFrameFunctionalGroupsSequence']
27
+ # Inference asks for geometry, never participant identifiers or clinical tags.
28
+ headers=[(safe_path(root,p),pydicom.dcmread(safe_path(root,p),stop_before_pixels=True,specific_tags=tags)) for p in oct_paths]
29
+ headers.sort(key=lambda v:int(v[1].get('ConcatenationFrameOffsetNumber',0)))
30
+ offsets=[int(d.get('ConcatenationFrameOffsetNumber',0)) for p,d in headers]
31
+ n=0;h=int(headers[0][1].Rows);w=int(headers[0][1].Columns)
32
+ for off,(p,d) in zip(offsets,headers):
33
+ if off!=n:raise ValueError('Missing or overlapping concatenation frames')
34
+ if (int(d.Rows),int(d.Columns))!=(h,w):raise ValueError('Inconsistent structural geometry')
35
+ if str(d.SOPClassUID)!='1.2.840.10008.5.1.4.1.1.77.1.5.4':raise ValueError('Input is not structural OCT')
36
+ n+=int(d.get('NumberOfFrames',1))
37
+ return headers,(n,h,w)
38
+
39
+ def read_frame(headers,index):
40
+ for path,ds in headers:
41
+ off=int(ds.get('ConcatenationFrameOffsetNumber',0));n=int(ds.get('NumberOfFrames',1))
42
+ if off<=index<off+n:
43
+ plugin='pylibjpeg' if ds.file_meta.TransferSyntaxUID.is_compressed else ''
44
+ return pixel_array(path,index=index-off,decoding_plugin=plugin)
45
+ raise IndexError('Frame outside volume')
46
+
47
+ def read_frames(headers,indices):
48
+ """Decode selected original frames with one file/decoder pass per source part."""
49
+ requested=[int(i) for i in indices];result={}
50
+ for path,ds in headers:
51
+ off=int(ds.get('ConcatenationFrameOffsetNumber',0));n=int(ds.get('NumberOfFrames',1))
52
+ local=[i-off for i in requested if off<=i<off+n]
53
+ if not local:continue
54
+ plugin='pylibjpeg' if ds.file_meta.TransferSyntaxUID.is_compressed else ''
55
+ for i,frame in zip(local,iter_pixels(path,indices=local,decoding_plugin=plugin)):
56
+ result[off+i]=frame
57
+ if set(result)!=set(requested):raise ValueError('Selected structural frame coverage is incomplete')
58
+ return result
59
+
60
+ def iter_volume_frames(headers):
61
+ for path,ds in headers:
62
+ plugin='pylibjpeg' if ds.file_meta.TransferSyntaxUID.is_compressed else ''
63
+ yield from iter_pixels(path,decoding_plugin=plugin)
64
+
65
+ def normalize_frame(image,shape):
66
+ """Deterministic within-image percentiles; no cohort fitting or metadata features."""
67
+ image=np.asarray(image,dtype=np.float32)
68
+ finite=np.isfinite(image)
69
+ if not finite.any():return np.zeros(shape,dtype=np.float32)
70
+ lo,hi=np.quantile(image[finite],[.01,.995])
71
+ image=np.nan_to_num(image,nan=float(lo),posinf=float(hi),neginf=float(lo))
72
+ image=np.clip((image-lo)/max(float(hi-lo),1e-6),0,1)
73
+ # PIL float bilinear matches center-based coordinate conversion.
74
+ return np.asarray(Image.fromarray(image).resize((shape[1],shape[0]),Image.Resampling.BILINEAR),dtype=np.float32).copy()
src/models/__init__.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Model construction and device-specific output selection.
2
+
3
+ Device is used only to select supervised/output heads. The convolutional network
4
+ receives OCT pixels, and its external boundary vocabulary stays anatomical.
5
+ Legacy checkpoints use one shared head per anatomical code.
6
+ """
7
+
8
+ from src.models.unet import BoundaryNet
9
+ import torch
10
+ from torch.nn import functional as F
11
+
12
+
13
+ def output_indices(config, device):
14
+ keys = config["device_keys"][device]
15
+ if "device_output_indices" in config:
16
+ indices = config["device_output_indices"][device]
17
+ labels = config["model_head_keys"]
18
+ if len(indices) != len(keys) or len(set(indices)) != len(indices):
19
+ raise ValueError("Invalid device output index mapping")
20
+ if any(labels[i] != device + "::" + key for i, key in zip(indices, keys)):
21
+ raise ValueError("Device heads do not match the external vocabulary")
22
+ return indices
23
+ return [config["boundary_keys"].index(k) for k in keys]
24
+
25
+
26
+ def configure_heads(config, mode="shared"):
27
+ config["head_mode"] = mode
28
+ if mode == "device":
29
+ labels, indices = [], {}
30
+ for device, keys in sorted(config["device_keys"].items()):
31
+ indices[device] = list(range(len(labels), len(labels) + len(keys)))
32
+ labels.extend(device + "::" + key for key in keys)
33
+ config["model_head_keys"] = labels
34
+ config["device_output_indices"] = indices
35
+ elif mode != "shared":
36
+ raise ValueError("Unknown output head mode")
37
+ for device in config["device_keys"]:
38
+ output_indices(config, device)
39
+ return config
40
+
41
+
42
+ def build_model(config):
43
+ n = len(config.get("model_head_keys", config["boundary_keys"]))
44
+ if config.get("architecture") == "BoundaryNetWidePrior":
45
+ from src.models.wide import WideBoundaryNet
46
+
47
+ return WideBoundaryNet(n, config["base"], config["height"])
48
+ if config.get("architecture") == "BoundaryNetGlobalPrior":
49
+ from src.models.global_context import GlobalBoundaryNet
50
+
51
+ return GlobalBoundaryNet(n, config["base"], config["height"])
52
+ if config.get("architecture") == "BoundaryNetAuxiliaryPrior":
53
+ from src.models.auxiliary import AuxiliaryBoundaryNet
54
+
55
+ return AuxiliaryBoundaryNet(
56
+ n, config["base"], config["height"], len(config["auxiliary_region_names"])
57
+ )
58
+ if config.get("architecture") in ["BoundaryNetContext", "BoundaryNetContextPrior"]:
59
+ from src.models.context import ContextBoundaryNet
60
+
61
+ return ContextBoundaryNet(
62
+ n,
63
+ config["base"],
64
+ (
65
+ config["height"]
66
+ if config["architecture"] == "BoundaryNetContextPrior"
67
+ else None
68
+ ),
69
+ )
70
+ if config.get("architecture") == "BoundaryNetAxialPrior":
71
+ from src.models.axial_prior import AxialPriorNet
72
+
73
+ return AxialPriorNet(n, config["base"], config["height"])
74
+ if config.get("architecture", "BoundaryNet") != "BoundaryNet":
75
+ raise ValueError("Unknown architecture")
76
+ return BoundaryNet(n, config["base"])
77
+
78
+
79
+ def remap_state(state, source_config, target_config):
80
+ """Duplicate this researcher's shared trained heads for a device-head run.
81
+
82
+ Every backbone tensor is retained. No weights from external assets are used.
83
+ Returning a remapping flag lets training explicitly reset optimizer state.
84
+ """
85
+ old = source_config.get("model_head_keys", source_config["boundary_keys"])
86
+ new = target_config.get("model_head_keys", target_config["boundary_keys"])
87
+ result = dict(state)
88
+ changed = False
89
+ indices = None
90
+ if old != new:
91
+ if "model_head_keys" in source_config or "model_head_keys" not in target_config:
92
+ raise ValueError(
93
+ "Only shared-to-device trained-head remapping is supported"
94
+ )
95
+ indices = [old.index(label.split("::", 1)[1]) for label in new]
96
+ for name in ["head.weight", "head.bias"]:
97
+ result[name] = state[name][indices].clone()
98
+ changed = True
99
+ target_prior = target_config.get("architecture") in [
100
+ "BoundaryNetAxialPrior",
101
+ "BoundaryNetContextPrior",
102
+ "BoundaryNetAuxiliaryPrior",
103
+ "BoundaryNetGlobalPrior",
104
+ "BoundaryNetWidePrior",
105
+ ]
106
+ if target_prior:
107
+ if "axial_prior" not in state:
108
+ result["axial_prior"] = state["head.weight"].new_zeros(
109
+ 1, len(new), target_config["height"], 1
110
+ )
111
+ changed = True
112
+ else:
113
+ prior = state["axial_prior"]
114
+ if indices is not None:
115
+ prior = prior[:, indices].clone()
116
+ if prior.shape[-2] != target_config["height"]:
117
+ prior = F.interpolate(
118
+ prior,
119
+ size=(target_config["height"], 1),
120
+ mode="bilinear",
121
+ align_corners=False,
122
+ )
123
+ changed = True
124
+ result["axial_prior"] = prior
125
+ elif "axial_prior" in state:
126
+ raise ValueError(
127
+ "Dropping a trained axial prior is not an implicit resume operation"
128
+ )
129
+ channels = (
130
+ 3
131
+ if target_config.get("architecture")
132
+ in ["BoundaryNetGlobalPrior", "BoundaryNetWidePrior"]
133
+ else target_config.get("input_channels", 1)
134
+ )
135
+ weight = result["down.0.net.0.weight"]
136
+ if weight.shape[1] != channels:
137
+ if weight.shape[1] != 1 or channels != 3:
138
+ raise ValueError("Unsupported input-channel resume mapping")
139
+ result["down.0.net.0.weight"] = weight.repeat(1, 3, 1, 1) / 3
140
+ changed = True
141
+ if target_config.get("architecture") == "BoundaryNetAuxiliaryPrior":
142
+ regions = len(target_config["auxiliary_region_names"])
143
+ if "auxiliary_head.weight" not in state:
144
+ result["auxiliary_head.weight"] = state["head.weight"].new_zeros(
145
+ regions, target_config["base"], 1, 1
146
+ )
147
+ result["auxiliary_head.bias"] = state["head.weight"].new_zeros(regions)
148
+ changed = True
149
+ elif "auxiliary_head.weight" in state:
150
+ raise ValueError(
151
+ "Dropping an auxiliary head is not an implicit resume operation"
152
+ )
153
+ if target_config.get("architecture") == "BoundaryNetGlobalPrior":
154
+ with torch.random.fork_rng(devices=[]):
155
+ torch.manual_seed(target_config["global_context"]["initialization_seed"])
156
+ initial = build_model(target_config).state_dict()
157
+ for name, value in initial.items():
158
+ if (
159
+ name.startswith(("global_encoder.", "global_film."))
160
+ and name not in state
161
+ ):
162
+ result[name] = value
163
+ changed = True
164
+ elif any(name.startswith(("global_encoder.", "global_film.")) for name in state):
165
+ raise ValueError("Dropping trained global context is not implicit")
166
+ if target_config.get("architecture") == "BoundaryNetWidePrior":
167
+ if "outer_conv.weight" not in state:
168
+ result["outer_conv.weight"] = state["head.weight"].new_zeros(
169
+ target_config["base"], 2, 3, 3
170
+ )
171
+ changed = True
172
+ elif "outer_conv.weight" in state:
173
+ raise ValueError("Dropping trained distant-view inputs is not implicit")
174
+ return result, changed
src/models/auxiliary.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Optional training-only ordinal interval supervision from coded boundaries.
2
+
3
+ The boundary objective keeps every original valid source target. Auxiliary
4
+ intervals are defined only in columns whose necessary boundaries are valid and
5
+ ordered. This auxiliary support is never used by reference conversion/scoring.
6
+ """
7
+
8
+ import torch
9
+ from torch import nn
10
+ from torch.nn import functional as F
11
+ from src.models.unet import BoundaryNet
12
+
13
+ ORDERS = {
14
+ "heidelberg_spectralis": [
15
+ "SCT_280677004",
16
+ "DCM_128289",
17
+ "DCM_128290",
18
+ "DCM_128291",
19
+ "DCM_128292",
20
+ "DCM_128293",
21
+ "SCT_76710003",
22
+ "DCM_128321",
23
+ "DCM_128323",
24
+ "DCM_128297",
25
+ "DCM_128300",
26
+ ],
27
+ "topcon_maestro2": [
28
+ "SCT_280677004",
29
+ "DCM_128289",
30
+ "DCM_128290",
31
+ "DCM_128291",
32
+ "DCM_128293",
33
+ "SCT_76710003",
34
+ "DCM_128320",
35
+ "DCM_128297",
36
+ "DCM_128299",
37
+ ],
38
+ "topcon_triton": [
39
+ "SCT_280677004",
40
+ "DCM_128289",
41
+ "DCM_128290",
42
+ "DCM_128291",
43
+ "DCM_128293",
44
+ "SCT_76710003",
45
+ "DCM_128320",
46
+ "DCM_128297",
47
+ "DCM_128299",
48
+ "DCM_128301",
49
+ ],
50
+ "zeiss_cirrus": ["SCT_280677004", "DCM_128298"],
51
+ }
52
+
53
+
54
+ def configure_auxiliary(config):
55
+ from src.models import output_indices
56
+
57
+ if config.get("input_channels", 1) != 1 or config.get("head_mode") != "device":
58
+ raise ValueError(
59
+ "This auxiliary experiment requires single-channel device heads"
60
+ )
61
+ mapping = {}
62
+ names = []
63
+ for device, keys in sorted(config["device_keys"].items()):
64
+ order = ORDERS[device]
65
+ if set(keys) != set(order):
66
+ raise ValueError("Unreviewed auxiliary boundary vocabulary")
67
+ indices = output_indices(config, device)
68
+ ordered = [indices[keys.index(key)] for key in order]
69
+ regions = list(range(len(names), len(names) + len(order) + 1))
70
+ intervals = (
71
+ ["above_" + order[0]]
72
+ + [a + "__to__" + b for a, b in zip(order[:-1], order[1:])]
73
+ + ["below_" + order[-1]]
74
+ )
75
+ names.extend(device + "::" + interval for interval in intervals)
76
+ mapping[device] = {"boundary_indices": ordered, "region_indices": regions}
77
+ config["auxiliary_mapping"] = mapping
78
+ config["auxiliary_region_names"] = names
79
+ return config
80
+
81
+
82
+ class AuxiliaryBoundaryNet(BoundaryNet):
83
+ def __init__(self, n_boundaries, base, height, n_regions):
84
+ super().__init__(n_boundaries, base)
85
+ self.axial_prior = nn.Parameter(torch.zeros(1, n_boundaries, height, 1))
86
+ self.auxiliary_head = nn.Conv2d(base, n_regions, 1)
87
+ nn.init.zeros_(self.auxiliary_head.weight)
88
+ nn.init.zeros_(self.auxiliary_head.bias)
89
+
90
+ def forward(self, image, return_aux=False):
91
+ x = image
92
+ skips = []
93
+ for index, layer in enumerate(self.down):
94
+ if index:
95
+ x = F.avg_pool2d(x, 2)
96
+ x = layer(x)
97
+ skips.append(x)
98
+ for index, layer in enumerate(self.up):
99
+ skip = skips[-2 - index]
100
+ x = F.interpolate(
101
+ x, size=skip.shape[-2:], mode="bilinear", align_corners=False
102
+ )
103
+ x = layer(torch.cat([x, skip], 1))
104
+ prior = self.axial_prior
105
+ if prior.shape[-2] != x.shape[-2]:
106
+ prior = F.interpolate(
107
+ prior, size=(x.shape[-2], 1), mode="bilinear", align_corners=False
108
+ )
109
+ boundary = self.head(x) + prior
110
+ return (boundary, self.auxiliary_head(x)) if return_aux else boundary
111
+
112
+
113
+ def interval_targets(positions, valid, height):
114
+ """Integer ordinal interval labels plus an independent auxiliary-column mask."""
115
+ support = valid.all(1) & (positions[:, 1:] >= positions[:, :-1]).all(1)
116
+ rows = torch.arange(height, device=positions.device, dtype=positions.dtype)[
117
+ None, None, :, None
118
+ ]
119
+ target = (rows >= positions[:, :, None, :]).sum(1)
120
+ return target, support
121
+
122
+
123
+ def auxiliary_loss(logits, positions, valid, config):
124
+ n, _, height, width = logits.shape
125
+ per_image = logits.float().flatten(1).sum(1) * 0
126
+ for mapping in config["auxiliary_mapping"].values():
127
+ indices = mapping["boundary_indices"]
128
+ regions = mapping["region_indices"]
129
+ target, columns = interval_targets(
130
+ positions[:, indices], valid[:, indices], height
131
+ )
132
+ support = columns[:, None, :].expand(-1, height, -1).float()
133
+ ce = F.cross_entropy(logits[:, regions].float(), target, reduction="none")
134
+ count = logits.new_zeros((n, len(regions)), dtype=torch.float32)
135
+ count.scatter_add_(1, target.flatten(1), support.flatten(1))
136
+ denominator = (
137
+ count.gather(1, target.flatten(1)).reshape(n, height, width).clamp_min(1)
138
+ )
139
+ per_image = per_image + (ce * support / denominator).sum((1, 2)) / (
140
+ count > 0
141
+ ).sum(1).clamp_min(1)
142
+ return per_image.mean()
src/models/axial_prior.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Image-only boundary network with a learned, zero-initialized axial logit bias.
2
+
3
+ The spatial parameters are trained jointly with the CNN on training targets. No
4
+ segmentation or acquisition identifier is an inference input. Device-specific
5
+ output selection is unchanged. A zero bias preserves the parent model exactly.
6
+ """
7
+
8
+ import torch
9
+ from torch import nn
10
+ from torch.nn import functional as F
11
+ from src.models.unet import BoundaryNet
12
+
13
+
14
+ class AxialPriorNet(BoundaryNet):
15
+ def __init__(self, n_boundaries, base, height):
16
+ super().__init__(n_boundaries, base)
17
+ self.axial_prior = nn.Parameter(torch.zeros(1, n_boundaries, height, 1))
18
+
19
+ def forward(self, image):
20
+ logits = super().forward(image)
21
+ prior = self.axial_prior
22
+ if prior.shape[-2] != logits.shape[-2]:
23
+ prior = F.interpolate(
24
+ prior, size=(logits.shape[-2], 1), mode="bilinear", align_corners=False
25
+ )
26
+ return logits + prior
src/models/context.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Three adjacent OCT B-scans as image channels, with center-frame outputs."""
2
+
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as F
6
+ from src.models.unet import BoundaryNet, Block
7
+
8
+
9
+ class ContextBoundaryNet(BoundaryNet):
10
+ def __init__(self, n_boundaries, base, prior_height=None):
11
+ super().__init__(n_boundaries, base)
12
+ self.down[0] = Block(3, base)
13
+ if prior_height is None:
14
+ self.register_parameter("axial_prior", None)
15
+ else:
16
+ self.axial_prior = nn.Parameter(
17
+ torch.zeros(1, n_boundaries, prior_height, 1)
18
+ )
19
+
20
+ def forward(self, image):
21
+ logits = super().forward(image)
22
+ if self.axial_prior is None:
23
+ return logits
24
+ prior = self.axial_prior
25
+ if prior.shape[-2] != logits.shape[-2]:
26
+ prior = F.interpolate(
27
+ prior, size=(logits.shape[-2], 1), mode="bilinear", align_corners=False
28
+ )
29
+ return logits + prior
src/models/global_context.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Local adjacent slices conditioned on three whole-volume OCT views."""
2
+
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as F
6
+ from src.models.context import ContextBoundaryNet
7
+ from src.models.unet import Block
8
+
9
+
10
+ class GlobalBoundaryNet(ContextBoundaryNet):
11
+ def __init__(self, n_boundaries, base, prior_height):
12
+ super().__init__(n_boundaries, base, prior_height)
13
+ self.global_encoder = nn.Sequential(
14
+ nn.Conv2d(3, base, 5, stride=2, padding=2, bias=False),
15
+ nn.GroupNorm(min(8, base), base),
16
+ nn.SiLU(),
17
+ Block(base, base * 2),
18
+ nn.AvgPool2d(2),
19
+ Block(base * 2, base * 4),
20
+ nn.AdaptiveAvgPool2d(1),
21
+ nn.Flatten(),
22
+ )
23
+ self.global_film = nn.Linear(base * 4, base * 12 * 2)
24
+ nn.init.zeros_(self.global_film.weight)
25
+ nn.init.zeros_(self.global_film.bias)
26
+
27
+ def forward(self, image):
28
+ if image.shape[1] != 6:
29
+ raise ValueError("Expected local3 plus global3 OCT channels")
30
+ global_image = F.interpolate(
31
+ image[:, 3:], size=(96, 128), mode="bilinear", align_corners=False
32
+ )
33
+ embedding = self.global_encoder(global_image)
34
+ scale, bias = self.global_film(embedding).chunk(2, dim=1)
35
+ x = image[:, :3].contiguous()
36
+ skips = []
37
+ for index, layer in enumerate(self.down):
38
+ if index:
39
+ x = F.avg_pool2d(x, 2)
40
+ x = layer(x)
41
+ if index == len(self.down) - 1:
42
+ x = x * (1 + scale[:, :, None, None]) + bias[:, :, None, None]
43
+ skips.append(x)
44
+ for index, layer in enumerate(self.up):
45
+ skip = skips[-2 - index]
46
+ x = F.interpolate(
47
+ x, size=skip.shape[-2:], mode="bilinear", align_corners=False
48
+ )
49
+ x = layer(torch.cat([x, skip], 1))
50
+ logits = self.head(x)
51
+ prior = self.axial_prior
52
+ if prior.shape[-2] != logits.shape[-2]:
53
+ prior = F.interpolate(
54
+ prior, size=(logits.shape[-2], 1), mode="bilinear", align_corners=False
55
+ )
56
+ return logits + prior
src/models/unet.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Randomly initialized U-Net with per-boundary axial distributions."""
2
+
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as F
6
+
7
+
8
+ class Block(nn.Module):
9
+ def __init__(self, a, b):
10
+ super().__init__()
11
+ g = min(8, b)
12
+ self.net = nn.Sequential(
13
+ nn.Conv2d(a, b, 3, padding=1, bias=False),
14
+ nn.GroupNorm(g, b),
15
+ nn.SiLU(),
16
+ nn.Conv2d(b, b, 3, padding=1, bias=False),
17
+ nn.GroupNorm(g, b),
18
+ nn.SiLU(),
19
+ )
20
+
21
+ def forward(self, x):
22
+ return self.net(x)
23
+
24
+
25
+ class BoundaryNet(nn.Module):
26
+ def __init__(self, n_boundaries, base=24):
27
+ super().__init__()
28
+ c = [base, base * 2, base * 4, base * 8, base * 12]
29
+ self.down = nn.ModuleList(
30
+ [Block(1, c[0])] + [Block(c[i - 1], c[i]) for i in range(1, 5)]
31
+ )
32
+ self.up = nn.ModuleList(
33
+ [Block(c[i] + c[i - 1], c[i - 1]) for i in range(4, 0, -1)]
34
+ )
35
+ self.head = nn.Conv2d(c[0], n_boundaries, 1)
36
+
37
+ def forward(self, x):
38
+ skips = []
39
+ for i, layer in enumerate(self.down):
40
+ if i:
41
+ x = F.avg_pool2d(x, 2)
42
+ x = layer(x)
43
+ skips.append(x)
44
+ for i, layer in enumerate(self.up):
45
+ skip = skips[-2 - i]
46
+ x = F.interpolate(
47
+ x, size=skip.shape[-2:], mode="bilinear", align_corners=False
48
+ )
49
+ x = layer(torch.cat([x, skip], 1))
50
+ return self.head(x)
51
+
52
+
53
+ def expected_positions(logits):
54
+ p = logits.float().softmax(dim=-2)
55
+ y = torch.arange(logits.shape[-2], device=logits.device, dtype=torch.float32)
56
+ return (p * y[None, None, :, None]).sum(-2)
57
+
58
+
59
+ def boundary_loss(
60
+ logits,
61
+ positions,
62
+ valid,
63
+ sigma=1.5,
64
+ regression_weight=0.1,
65
+ coordinate_scale=None,
66
+ regression_loss="smooth_l1",
67
+ active_head_packing=False,
68
+ ):
69
+ """Gaussian cross entropy plus a configured native-scaled position penalty."""
70
+ if active_head_packing:
71
+ n, k, h, w = logits.shape
72
+ support = valid.any(-1)
73
+ count = max(1, int(support.sum(-1).max().item()))
74
+ index = (
75
+ torch.arange(k, device=logits.device)[None]
76
+ .expand(n, -1)
77
+ .masked_fill(~support, k)
78
+ .sort(-1)
79
+ .values[:, :count]
80
+ )
81
+ keep = index < k
82
+ index = index.clamp_max(k - 1)
83
+ logits = logits.gather(1, index[:, :, None, None].expand(-1, -1, h, w))
84
+ positions = positions.gather(1, index[:, :, None].expand(-1, -1, w))
85
+ valid = valid.gather(1, index[:, :, None].expand(-1, -1, w)) & keep[:, :, None]
86
+ h = logits.shape[-2]
87
+ rows = torch.arange(h, device=logits.device, dtype=torch.float32)[
88
+ None, None, :, None
89
+ ]
90
+ target = torch.exp(-0.5 * ((rows - positions[:, :, None, :]) / sigma) ** 2)
91
+ target = target / target.sum(-2, keepdim=True).clamp_min(1e-8)
92
+ ce = -(target * logits.float().log_softmax(-2)).sum(-2)
93
+ if regression_loss == "smooth_l1":
94
+ reg = F.smooth_l1_loss(
95
+ expected_positions(logits), positions, reduction="none", beta=2
96
+ )
97
+ elif regression_loss == "l1":
98
+ reg = (expected_positions(logits) - positions).abs()
99
+ elif regression_loss == "expected_l1":
100
+ reg = (
101
+ logits.float().softmax(-2) * (rows - positions[:, :, None, :]).abs()
102
+ ).sum(-2)
103
+ else:
104
+ raise ValueError("Unknown position regression loss")
105
+ if coordinate_scale is not None:
106
+ reg = reg * coordinate_scale[:, None, None]
107
+ m = valid.float()
108
+ count = m.sum(-1)
109
+ loss = ((ce + regression_weight * reg) * m).sum(-1) / count.clamp_min(1)
110
+ included = count > 0
111
+ per_image = (loss * included).sum(1) / included.sum(1).clamp_min(1)
112
+ return per_image.mean()
src/models/wide.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Own C3 body plus zero-initialized distant OCT inputs; no metadata features."""
2
+
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as F
6
+ from src.models.context import ContextBoundaryNet
7
+
8
+
9
+ class WideBoundaryNet(ContextBoundaryNet):
10
+ def __init__(self, n_boundaries, base, prior_height):
11
+ super().__init__(n_boundaries, base, prior_height)
12
+ self.outer_conv = nn.Conv2d(2, base, 3, padding=1, bias=False)
13
+ nn.init.zeros_(self.outer_conv.weight)
14
+
15
+ def forward(self, image):
16
+ if image.ndim != 4 or image.shape[1] != 5:
17
+ raise ValueError("Five OCT input views required")
18
+ local = image[:, 1:4].contiguous()
19
+ outer = image[:, [0, 4]].contiguous()
20
+ first = self.down[0].net
21
+ x = first[0](local) + self.outer_conv(outer)
22
+ for layer in list(first)[1:]:
23
+ x = layer(x)
24
+ skips = [x]
25
+ for layer in self.down[1:]:
26
+ x = layer(F.avg_pool2d(x, 2))
27
+ skips.append(x)
28
+ for index, layer in enumerate(self.up):
29
+ skip = skips[-2 - index]
30
+ x = F.interpolate(
31
+ x, size=skip.shape[-2:], mode="bilinear", align_corners=False
32
+ )
33
+ x = layer(torch.cat([x, skip], 1))
34
+ logits = self.head(x)
35
+ prior = self.axial_prior
36
+ if prior.shape[-2] != logits.shape[-2]:
37
+ prior = F.interpolate(
38
+ prior, size=(logits.shape[-2], 1), mode="bilinear", align_corners=False
39
+ )
40
+ return logits + prior
src/paths.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """Project paths shared by commands and the local viewer."""
2
+
3
+ from pathlib import Path
4
+
5
+ PROJECT_ROOT = Path(__file__).resolve().parents[1]
src/protocol.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Public split and canonical-boundary checks; does not decode or pair DICOMs."""
2
+ from __future__ import annotations
3
+
4
+ import argparse
5
+ from collections import defaultdict
6
+ import hashlib
7
+ import json
8
+ from pathlib import Path, PurePosixPath
9
+ import re
10
+ from statistics import fmean
11
+
12
+ DEVICES = {'heidelberg_spectralis', 'topcon_maestro2', 'topcon_triton', 'zeiss_cirrus'}
13
+ SEED = 20261105
14
+
15
+
16
+ def digest(path):
17
+ return hashlib.sha256(Path(path).read_bytes()).hexdigest()
18
+
19
+
20
+ def relative_path(value):
21
+ if not isinstance(value, str) or not value or '\\' in value or '\x00' in value or re.match(r'^[A-Za-z]:', value):
22
+ raise ValueError('Dataset paths must be relative POSIX paths')
23
+ path = PurePosixPath(value)
24
+ if path.is_absolute() or '..' in path.parts or value == '.':
25
+ raise ValueError('Dataset paths must stay beneath the supplied data root')
26
+ return value
27
+
28
+
29
+ def load_pairs(path):
30
+ rows, identities, inputs = [], set(), set()
31
+ for line in Path(path).read_text().splitlines():
32
+ if not line.strip():
33
+ continue
34
+ row = json.loads(line)
35
+ if not isinstance(row, dict):
36
+ raise ValueError('Manifest rows must be objects')
37
+ name = row.get('case_id')
38
+ if not isinstance(name, str) or not re.fullmatch(r'[A-Za-z0-9_-]{1,64}', name) or name in identities:
39
+ raise ValueError('Case IDs must be unique safe filename stems')
40
+ if (not isinstance(row.get('participant_id'), str) or not row['participant_id'].strip()
41
+ or row['participant_id'] != row['participant_id'].strip()):
42
+ raise ValueError('Each case needs a nonempty participant identity')
43
+ if row.get('device') not in DEVICES:
44
+ raise ValueError('Device family must match the declared AI-READI cohort')
45
+ for field in ('oct_paths', 'segmentation_paths'):
46
+ values = row.get(field)
47
+ if not isinstance(values, list) or not values:
48
+ raise ValueError('Each case needs structural input and segmentation paths')
49
+ checked = [relative_path(item) for item in values]
50
+ if len(set(checked)) != len(checked):
51
+ raise ValueError('Duplicate paths within a case')
52
+ if inputs.intersection(row['oct_paths']):
53
+ raise ValueError('A structural input cannot be duplicated across cases')
54
+ inputs.update(row['oct_paths'])
55
+ identities.add(name)
56
+ rows.append(row)
57
+ if not rows:
58
+ raise ValueError('The manifest is empty')
59
+ return rows
60
+
61
+
62
+ def split_for(participant):
63
+ # Integer buckets avoid floating-point or Python-hash dependence.
64
+ key = f'aireadi-oct-v1:{SEED}:{participant}'.encode('utf-8')
65
+ bucket = int.from_bytes(hashlib.sha256(key).digest(), 'big') % 100
66
+ return 'train' if bucket < 80 else 'validation' if bucket < 90 else 'test'
67
+
68
+
69
+ def write_splits(pairs, output):
70
+ rows = load_pairs(pairs)
71
+ groups = {name: [] for name in ('train', 'validation', 'test')}
72
+ for row in sorted(rows, key=lambda item: item['case_id']):
73
+ groups[split_for(row['participant_id'])].append(row)
74
+ if any(not group for group in groups.values()):
75
+ raise ValueError('A split is empty; report this rather than rerolling the seed')
76
+ output = Path(output)
77
+ output.mkdir(parents=True, exist_ok=False, mode=0o700)
78
+ report = {'seed': SEED, 'algorithm': 'sha256-mod100-v1', 'ratios': [80, 10, 10],
79
+ 'source_manifest_sha256': digest(pairs), 'splits': {}}
80
+ for name, group in groups.items():
81
+ destination = output / (name + '.jsonl')
82
+ destination.write_text(''.join(json.dumps(row, sort_keys=True) + '\n' for row in group))
83
+ public = [{key: row[key] for key in ('case_id', 'device', 'oct_paths')} for row in group]
84
+ (output / (name + '-inputs.jsonl')).write_text(''.join(json.dumps(row) + '\n' for row in public))
85
+ report['splits'][name] = {'cases': len(group), 'participants': len({row['participant_id'] for row in group}),
86
+ 'devices': sorted({row['device'] for row in group}), 'manifest_sha256': digest(destination)}
87
+ (output / 'split-summary.json').write_text(json.dumps(report, indent=2) + '\n')
88
+ return report
89
+
90
+
91
+ def read_arrays(path, reference=False):
92
+ import numpy as np
93
+ with np.load(path, allow_pickle=False) as data:
94
+ required = {'positions', 'boundary_keys', 'valid', 'height'} if reference else {'positions', 'boundary_keys'}
95
+ allowed = required | ({'axial_spacing_um'} if reference else set())
96
+ if not required <= set(data.files) or set(data.files) - allowed:
97
+ raise ValueError('Unexpected or missing canonical array fields')
98
+ positions = data['positions']
99
+ keys = data['boundary_keys']
100
+ if positions.ndim != 3 or any(n == 0 for n in positions.shape) or positions.dtype.kind not in 'fiu':
101
+ raise ValueError('positions must be a nonempty real [boundary, B-scan, A-scan] array')
102
+ if keys.ndim != 1 or keys.dtype.kind not in 'US' or len(keys) != positions.shape[0]:
103
+ raise ValueError('boundary_keys must be a one-dimensional Unicode/byte-string array')
104
+ names = [x.decode('utf-8') if isinstance(x, bytes) else str(x) for x in keys.tolist()]
105
+ if any(not x or x != x.strip() for x in names) or len(set(names)) != len(names):
106
+ raise ValueError('Boundary keys must be nonempty and unique')
107
+ result = {'positions': positions.astype('float64'), 'keys': names}
108
+ if reference:
109
+ valid, height = data['valid'], data['height']
110
+ if valid.dtype.kind != 'b' or valid.shape != positions.shape:
111
+ raise ValueError('Reference valid must be a matching Boolean mask')
112
+ if height.shape != () or height.dtype.kind not in 'iu' or int(height) < 2:
113
+ raise ValueError('Reference height must be the original axial size')
114
+ if not valid.any() or not np.isfinite(positions[valid]).all():
115
+ raise ValueError('Reference needs finite valid annotation support')
116
+ if ((positions[valid] < 0) | (positions[valid] > int(height) - 1)).any():
117
+ raise ValueError('Reference coordinates are outside original geometry')
118
+ result.update(valid=valid.copy(), height=int(height), spacing=None)
119
+ if 'axial_spacing_um' in data:
120
+ spacing = data['axial_spacing_um']
121
+ if spacing.shape != () or spacing.dtype.kind not in 'fiu' or not np.isfinite(spacing) or float(spacing) <= 0:
122
+ raise ValueError('Axial spacing must be a positive finite scalar in micrometres')
123
+ result['spacing'] = float(spacing)
124
+ return result
125
+
126
+
127
+ def score(manifest, references, predictions, expected_manifest_sha256=None):
128
+ import numpy as np
129
+ fingerprint = digest(manifest)
130
+ if expected_manifest_sha256 is not None and fingerprint != expected_manifest_sha256:
131
+ raise ValueError('The frozen manifest changed')
132
+ rows = load_pairs(manifest)
133
+ expected = {row['case_id'] for row in rows}
134
+ if {path.stem for path in Path(predictions).glob('*.npz')} != expected:
135
+ raise ValueError('Prediction cases do not exactly cover the manifest')
136
+ grouped = defaultdict(lambda: defaultdict(list))
137
+ boundaries = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
138
+ boundary_counts = defaultdict(lambda: defaultdict(int))
139
+ physical = defaultdict(lambda: defaultdict(list))
140
+ spacing_cases = defaultdict(int)
141
+ for row in rows:
142
+ name = row['case_id'] + '.npz'
143
+ ref = read_arrays(Path(references) / name, reference=True)
144
+ pred = read_arrays(Path(predictions) / name)
145
+ if set(ref['keys']) != set(pred['keys']) or ref['positions'].shape != pred['positions'].shape:
146
+ raise ValueError('Prediction boundary identities or geometry do not match')
147
+ values = pred['positions'][[pred['keys'].index(key) for key in ref['keys']]]
148
+ if not np.isfinite(values).all() or ((values < 0) | (values > ref['height'] - 1)).any():
149
+ raise ValueError('All predicted coordinates must be finite and in original bounds')
150
+ device, participant = row['device'], row['participant_id']
151
+ errors = []
152
+ for index, key in enumerate(ref['keys']):
153
+ mask = ref['valid'][index]
154
+ boundary_counts[device][key] += int(mask.sum())
155
+ if mask.any():
156
+ error = float(np.abs(values[index][mask] - ref['positions'][index][mask]).mean())
157
+ errors.append(error)
158
+ boundaries[device][key][participant].append(error)
159
+ case_error = fmean(errors)
160
+ grouped[device][participant].append(case_error)
161
+ if ref['spacing'] is not None:
162
+ physical[device][participant].append(case_error * ref['spacing'])
163
+ spacing_cases[device] += 1
164
+ average = lambda participants: fmean(fmean(cases) for cases in participants.values())
165
+ devices = {}
166
+ for device, participants in sorted(grouped.items()):
167
+ count = sum(map(len, participants.values()))
168
+ devices[device] = {'cases': count, 'participants': len(participants), 'mae_pixels': average(participants),
169
+ 'spacing_cases': spacing_cases[device],
170
+ 'mae_um': average(physical[device]) if spacing_cases[device] == count else None,
171
+ 'boundaries': {key: {'mae_pixels': average(groups) if groups else None, 'participants': len(groups),
172
+ 'valid_points': boundary_counts[device][key]}
173
+ for key, groups in ((key, boundaries[device][key]) for key in sorted(boundary_counts[device]))}}
174
+ return {'measurement': 'informal_canonical_boundary_agreement', 'manifest_sha256': fingerprint,
175
+ 'cases': len(rows), 'participants': len({row['participant_id'] for row in rows}),
176
+ 'mae_pixels': fmean(item['mae_pixels'] for item in devices.values()),
177
+ 'mae_um': fmean(item['mae_um'] for item in devices.values()) if all(item['mae_um'] is not None for item in devices.values()) else None,
178
+ 'aggregation': 'valid points -> boundaries -> volume -> participant/device -> device -> macro device',
179
+ 'devices': devices}
180
+
181
+
182
+ def main():
183
+ parser = argparse.ArgumentParser(description=__doc__)
184
+ commands = parser.add_subparsers(dest='command', required=True)
185
+ split = commands.add_parser('split')
186
+ split.add_argument('--pairs', required=True, type=Path)
187
+ split.add_argument('--output-dir', required=True, type=Path)
188
+ measure = commands.add_parser('score')
189
+ measure.add_argument('--manifest', required=True, type=Path)
190
+ measure.add_argument('--reference-dir', required=True, type=Path)
191
+ measure.add_argument('--predictions-dir', required=True, type=Path)
192
+ measure.add_argument('--expected-manifest-sha256')
193
+ measure.add_argument('--output', required=True, type=Path)
194
+ args = parser.parse_args()
195
+ if args.command == 'split':
196
+ result = write_splits(args.pairs, args.output_dir)
197
+ else:
198
+ if args.output.exists():
199
+ raise FileExistsError('Choose a fresh metric report path')
200
+ result = score(args.manifest, args.reference_dir, args.predictions_dir, args.expected_manifest_sha256)
201
+ with args.output.open('x') as stream:
202
+ json.dump(result, stream, indent=2, allow_nan=False)
203
+ stream.write('\n')
204
+ print(json.dumps(result, allow_nan=False))
205
+
206
+
207
+ if __name__ == '__main__':
208
+ main()
src/training/__init__.py ADDED
File without changes
src/training/guard.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Metadata-only split checks before development payload access.
2
+
3
+ The frozen discovery audit establishes pairing. These checks independently reject
4
+ a mislabeled manifest participant before reading any image/segmentation payload.
5
+ This module is never imported by label-free inference.
6
+ """
7
+
8
+ import pydicom
9
+ from src.io import safe_path
10
+ from src.protocol import split_for
11
+
12
+
13
+ def check_development_identity(row, root, expected_split):
14
+ participant = str(row["participant_id"])
15
+ if (
16
+ expected_split not in ["train", "validation"]
17
+ or split_for(participant) != expected_split
18
+ ):
19
+ raise ValueError("Development manifest split violation")
20
+ classes = [
21
+ ("oct_paths", "1.2.840.10008.5.1.4.1.1.77.1.5.4"),
22
+ ("segmentation_paths", "1.2.840.10008.5.1.4.1.1.66.8"),
23
+ ]
24
+ for field, sop_class in classes:
25
+ for relative in row[field]:
26
+ header = pydicom.dcmread(
27
+ safe_path(root, relative),
28
+ stop_before_pixels=True,
29
+ specific_tags=["PatientID", "SOPClassUID"],
30
+ )
31
+ actual = str(header.get("PatientID", ""))
32
+ if actual != participant or split_for(actual) != expected_split:
33
+ raise ValueError(
34
+ "DICOM participant identity/split differs from frozen manifest"
35
+ )
36
+ if str(header.get("SOPClassUID", "")) != sop_class:
37
+ raise ValueError("Unexpected development source object class")
src/training/unet.py ADDED
@@ -0,0 +1,790 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Original boundary model training. Run only through managed compute.
2
+
3
+ Canonical references and sampled training OCT caches are produced by prepare.py.
4
+ All checkpoints begin randomly unless --resume supplies this researcher's weights.
5
+ """
6
+
7
+ from src.paths import PROJECT_ROOT
8
+ from pathlib import Path
9
+ from collections import defaultdict, OrderedDict
10
+ import argparse
11
+ import json
12
+ import random
13
+ import time
14
+ import math
15
+ import subprocess
16
+ import sys
17
+ import os
18
+
19
+ SCRIPT_STARTED = time.time()
20
+ SCRIPT_TIMER = time.monotonic()
21
+ print(
22
+ json.dumps({"stage": "scientific_imports", "process_started_unix": SCRIPT_STARTED}),
23
+ flush=True,
24
+ )
25
+ import numpy as np
26
+ import torch
27
+ from torch.utils.data import Dataset, DataLoader
28
+ from src.models.unet import boundary_loss
29
+ from src.models import build_model, configure_heads, output_indices, remap_state
30
+ from src.io import load_manifest
31
+ from src.protocol import split_for, digest
32
+
33
+
34
+ class CachedFrames(Dataset):
35
+ def __init__(self, rows, cache, keys, packed=None, config=None, global_cache=None):
36
+ self.config = config
37
+ self.global_images = None
38
+ self.global_cases = {}
39
+ if global_cache:
40
+ global_root = Path(global_cache)
41
+ meta = json.loads((global_root / "global-meta.json").read_text())
42
+ self.global_cases = {r["case_id"]: int(r["index"]) for r in meta["cases"]}
43
+ if set(self.global_cases) != {r["case_id"] for r in rows}:
44
+ raise ValueError("Global cache cases differ")
45
+ if sorted(self.global_cases.values()) != list(range(len(rows))):
46
+ raise ValueError("Global cache indices differ")
47
+ self.global_images = np.load(
48
+ global_root / "global-images.npy", mmap_mode="r", allow_pickle=False
49
+ )
50
+ if self.global_images.shape != (
51
+ len(rows),
52
+ 3,
53
+ config["height"],
54
+ config["width"],
55
+ ):
56
+ raise ValueError("Global image geometry differs")
57
+ self.rows = rows
58
+ self.cache = Path(cache) / "frames-train"
59
+ self.keys = keys
60
+ self.memo = OrderedDict()
61
+ self.samples = []
62
+ self.weights = []
63
+ self.packed = None
64
+ self.packed_cases = {}
65
+ if packed:
66
+ packed = Path(packed)
67
+ meta = json.loads((packed / "packed.json").read_text())
68
+ if meta["boundary_keys"] != keys:
69
+ raise ValueError("Packed vocabulary differs")
70
+ self.packed_cases = {r["case_id"]: r for r in meta["cases"]}
71
+ self.packed = {
72
+ k: np.load(packed / (k + ".npy"), mmap_mode="r", allow_pickle=False)
73
+ for k in ["images", "positions", "valid", "original_height"]
74
+ }
75
+ sampling = Path(cache) / "sampling.jsonl"
76
+ counts = {}
77
+ if sampling.is_file():
78
+ for line in sampling.read_text().splitlines():
79
+ r = json.loads(line)
80
+ if r.get("split") == "train" and "error" not in r:
81
+ counts[r["case_id"]] = len(r["frames"])
82
+ devices = defaultdict(lambda: defaultdict(list))
83
+ for i, r in enumerate(rows):
84
+ devices[r["device"]][r["participant_id"]].append(i)
85
+ for i, row in enumerate(rows):
86
+ if self.packed is not None and row["case_id"] not in self.packed_cases:
87
+ raise ValueError("Packed case missing")
88
+ if row["case_id"] in counts:
89
+ n = counts[row["case_id"]]
90
+ else:
91
+ with np.load(
92
+ self.cache / (row["case_id"] + ".npz"), allow_pickle=False
93
+ ) as f:
94
+ n = len(f["images"])
95
+ if n < 1:
96
+ raise ValueError(
97
+ "A training case has no cached source-annotated frames"
98
+ )
99
+ if (
100
+ self.packed is not None
101
+ and self.packed_cases[row["case_id"]]["count"] != n
102
+ ):
103
+ raise ValueError("Packed frame sampling differs")
104
+ group = devices[row["device"]]
105
+ for j in range(n):
106
+ self.samples.append((i, j))
107
+ self.weights.append(
108
+ 1 / (len(group) * len(group[row["participant_id"]]) * n)
109
+ )
110
+
111
+ def __len__(self):
112
+ return len(self.samples)
113
+
114
+ def __getitem__(self, index):
115
+ i, j = self.samples[index]
116
+ row = self.rows[i]
117
+ name = row["case_id"]
118
+ if self.packed is not None:
119
+ index = self.packed_cases[name]["offset"] + j
120
+ data = self.packed
121
+ pos, valid = self.project(
122
+ row, data["positions"][index].copy(), data["valid"][index].copy()
123
+ )
124
+ image = data["images"][index].astype(np.float32) / 255
125
+ if image.ndim == 2:
126
+ image = image[None]
127
+ image = self.join_global(image, name)
128
+ return (
129
+ torch.from_numpy(image),
130
+ torch.from_numpy(pos),
131
+ torch.from_numpy(valid),
132
+ np.float32(data["original_height"][index]),
133
+ )
134
+ if name in self.memo:
135
+ data = self.memo.pop(name)
136
+ else:
137
+ with np.load(self.cache / (name + ".npz"), allow_pickle=False) as f:
138
+ data = {k: f[k] for k in f.files}
139
+ self.memo[name] = data
140
+ while len(self.memo) > 16:
141
+ self.memo.popitem(last=False)
142
+ image = data["images"][j].astype(np.float32) / 255
143
+ if image.ndim == 2:
144
+ image = image[None]
145
+ pos = np.zeros((len(self.keys), image.shape[-1]), np.float32)
146
+ valid = np.zeros_like(pos, dtype=bool)
147
+ for k, key in enumerate(data["boundary_keys"].tolist()):
148
+ global_index = self.keys.index(key)
149
+ pos[global_index] = data["positions"][k, j]
150
+ valid[global_index] = data["valid"][k, j]
151
+ pos, valid = self.project(row, pos, valid)
152
+ image = self.join_global(image, name)
153
+ return (
154
+ torch.from_numpy(image),
155
+ torch.from_numpy(pos),
156
+ torch.from_numpy(valid),
157
+ np.float32(data["original_height"]),
158
+ )
159
+
160
+ def join_global(self, image, name):
161
+ if self.global_images is None:
162
+ return image
163
+ if image.shape[0] != 3:
164
+ raise ValueError("Local context must contain three channels")
165
+ return np.concatenate(
166
+ [
167
+ image,
168
+ self.global_images[self.global_cases[name]].astype(np.float32) / 255,
169
+ ],
170
+ axis=0,
171
+ )
172
+
173
+ def project(self, row, pos, valid):
174
+ if self.config is None or "device_output_indices" not in self.config:
175
+ return pos, valid
176
+ target = np.zeros(
177
+ (len(self.config["model_head_keys"]), pos.shape[-1]), np.float32
178
+ )
179
+ support = np.zeros_like(target, dtype=bool)
180
+ for key, index in zip(
181
+ self.config["device_keys"][row["device"]],
182
+ output_indices(self.config, row["device"]),
183
+ ):
184
+ source = self.keys.index(key)
185
+ target[index] = pos[source]
186
+ support[index] = valid[source]
187
+ return target, support
188
+
189
+
190
+ def seed_worker(worker_id):
191
+ seed = torch.initial_seed() % 2**32
192
+ np.random.seed(seed)
193
+ random.seed(seed)
194
+
195
+
196
+ def augment(image, pos, valid):
197
+ n, _, h, w = image.shape
198
+ flip = torch.rand(n, device=image.device) < 0.5
199
+ image[flip] = image[flip].flip(-1)
200
+ pos[flip] = pos[flip].flip(-1)
201
+ valid[flip] = valid[flip].flip(-1)
202
+ # Axial shifts preserve the coordinate contract; locations shifted outside the
203
+ # augmented image are not training support, with the original masks unchanged.
204
+ shifts = torch.randint(-(h // 12), h // 12 + 1, (n,), device=image.device)
205
+ # Preserve native image-edge targets; translating only their visible support
206
+ # would systematically turn valid zero-depth annotations into positive depths.
207
+ touches_edge = (valid & ((pos <= 0) | (pos >= h - 1))).flatten(1).any(1)
208
+ shifts[touches_edge] = 0
209
+ for i, shift in enumerate(shifts.tolist()):
210
+ image[i] = torch.roll(image[i], shift, -2)
211
+ if shift > 0:
212
+ image[i, :, :shift] = 0
213
+ elif shift < 0:
214
+ image[i, :, shift:] = 0
215
+ pos[i] += shift
216
+ valid &= (pos >= 0) & (pos <= h - 1)
217
+ pos = pos.clamp(0, h - 1)
218
+ gamma = torch.empty(n, 1, 1, 1, device=image.device).uniform_(0.75, 1.4)
219
+ gain = torch.empty(n, 1, 1, 1, device=image.device).uniform_(0.8, 1.2)
220
+ image = (
221
+ image.clamp_min(0).pow(gamma) * gain + torch.randn_like(image) * 0.015
222
+ ).clamp(0, 1)
223
+ return image, pos, valid
224
+
225
+
226
+ def source_hashes():
227
+ return {
228
+ p.relative_to(PROJECT_ROOT).as_posix(): digest(p)
229
+ for folder in [PROJECT_ROOT, PROJECT_ROOT / "src", PROJECT_ROOT / "tools"]
230
+ for p in sorted(
231
+ folder.glob("*.py") if folder == PROJECT_ROOT else folder.rglob("*.py")
232
+ )
233
+ }
234
+
235
+
236
+ def main():
237
+ ap = argparse.ArgumentParser()
238
+ ap.add_argument("--train-manifest", required=True)
239
+ ap.add_argument("--validation-manifest", required=True)
240
+ ap.add_argument("--data-root", required=True)
241
+ ap.add_argument("--output-dir", required=True)
242
+ ap.add_argument("--seed", type=int, required=True)
243
+ ap.add_argument("--cache-dir")
244
+ ap.add_argument("--global-cache")
245
+ ap.add_argument("--packed-cache")
246
+ ap.add_argument("--steps", type=int, default=30000)
247
+ ap.add_argument("--batch-size", type=int, default=8)
248
+ ap.add_argument("--workers", type=int, default=4)
249
+ ap.add_argument("--base", type=int, default=24)
250
+ ap.add_argument("--height", type=int, default=384)
251
+ ap.add_argument("--width", type=int, default=256)
252
+ ap.add_argument("--learning-rate", type=float, default=0.0005)
253
+ ap.add_argument("--save-every", type=int, default=2000)
254
+ ap.add_argument("--sigma", type=float, default=1.5)
255
+ ap.add_argument("--regression-weight", type=float, default=0.1)
256
+ ap.add_argument(
257
+ "--regression-loss",
258
+ choices=["smooth_l1", "l1", "expected_l1"],
259
+ default="smooth_l1",
260
+ )
261
+ ap.add_argument("--input-channels", type=int, choices=[1, 3, 5, 6], default=1)
262
+ ap.add_argument("--pack-active-heads", action="store_true")
263
+ ap.add_argument("--axial-prior", action="store_true")
264
+ ap.add_argument("--auxiliary-region-weight", type=float, default=0)
265
+ ap.add_argument("--resume")
266
+ ap.add_argument("--reset-optimizer", action="store_true")
267
+ ap.add_argument("--head-mode", choices=["shared", "device"], default="shared")
268
+ ap.add_argument("--device", default="cuda")
269
+ args = ap.parse_args()
270
+ torch.set_num_threads(4)
271
+ random.seed(args.seed)
272
+ np.random.seed(args.seed)
273
+ torch.manual_seed(args.seed)
274
+ torch.cuda.manual_seed_all(args.seed)
275
+ torch.backends.cudnn.benchmark = True
276
+ rows = load_manifest(args.train_manifest)
277
+ val = load_manifest(args.validation_manifest)
278
+ if any(split_for(r["participant_id"]) != "train" for r in rows) or any(
279
+ split_for(r["participant_id"]) != "validation" for r in val
280
+ ):
281
+ raise ValueError("Fixed split violation")
282
+ out = Path(args.output_dir).resolve()
283
+ out.mkdir(parents=True, exist_ok=True)
284
+ print("training_output", str(out), flush=True)
285
+ if args.input_channels in [5, 6] and not args.axial_prior:
286
+ raise ValueError("Six-channel global training requires --axial-prior")
287
+ if args.global_cache and args.input_channels != 6:
288
+ raise ValueError("Global cache requires six-channel configuration")
289
+ if args.auxiliary_region_weight and args.input_channels != 1:
290
+ raise ValueError("This auxiliary architecture uses one local OCT channel")
291
+ local_channels = 3 if args.input_channels == 6 else args.input_channels
292
+ cache = args.cache_dir or str(out / "cache")
293
+ if args.cache_dir is None:
294
+ subprocess.run(
295
+ [
296
+ sys.executable,
297
+ str(PROJECT_ROOT / "tools/prepare.py"),
298
+ "--train-manifest",
299
+ args.train_manifest,
300
+ "--validation-manifest",
301
+ args.validation_manifest,
302
+ "--data-root",
303
+ args.data_root,
304
+ "--output-dir",
305
+ cache,
306
+ "--height",
307
+ str(args.height),
308
+ "--width",
309
+ str(args.width),
310
+ "--seed",
311
+ str(args.seed),
312
+ "--input-channels",
313
+ str(local_channels),
314
+ "--workers",
315
+ str(max(1, args.workers)),
316
+ ],
317
+ check=True,
318
+ )
319
+ if args.input_channels == 6 and args.global_cache is None:
320
+ args.global_cache = str(out / "global-cache")
321
+ subprocess.run(
322
+ [
323
+ sys.executable,
324
+ str(PROJECT_ROOT / "tools/prepare_global.py"),
325
+ "--train-manifest",
326
+ args.train_manifest,
327
+ "--data-root",
328
+ args.data_root,
329
+ "--output-dir",
330
+ args.global_cache,
331
+ "--height",
332
+ str(args.height),
333
+ "--width",
334
+ str(args.width),
335
+ "--workers",
336
+ str(max(1, args.workers)),
337
+ ],
338
+ check=True,
339
+ )
340
+ preparation = json.loads((Path(cache) / "preparation-summary.json").read_text())
341
+ if preparation["failures"] or preparation["manifest_hashes"] != {
342
+ "train": digest(args.train_manifest),
343
+ "validation": digest(args.validation_manifest),
344
+ }:
345
+ raise ValueError("Complete cache must match the frozen manifests")
346
+ if preparation["decoder_sha256"] != digest(PROJECT_ROOT / "src/annotations.py"):
347
+ raise ValueError("Canonical decoder changed after preparation")
348
+ local_channels = 3 if args.global_cache else args.input_channels
349
+ if preparation["arguments"].get("input_channels", 1) != local_channels:
350
+ raise ValueError("Cached input channels differ")
351
+ if (preparation["arguments"]["height"], preparation["arguments"]["width"]) != (
352
+ args.height,
353
+ args.width,
354
+ ):
355
+ raise ValueError("Cached image resolution differs from training configuration")
356
+ if args.packed_cache:
357
+ packed_meta = json.loads((Path(args.packed_cache) / "packed.json").read_text())
358
+ if (
359
+ packed_meta["training_manifest_sha256"] != digest(args.train_manifest)
360
+ or packed_meta["decoder_sha256"] != preparation["decoder_sha256"]
361
+ or packed_meta["source_sampling_sha256"]
362
+ != digest(Path(cache) / "sampling.jsonl")
363
+ or packed_meta["source_preparation_sha256"]
364
+ != digest(Path(cache) / "preparation-summary.json")
365
+ ):
366
+ raise ValueError("Packed cache provenance differs")
367
+ if packed_meta.get("input_channels", 1) != local_channels:
368
+ raise ValueError("Packed input channels differ")
369
+ if (packed_meta["height"], packed_meta["width"]) != (args.height, args.width):
370
+ raise ValueError("Packed resolution differs")
371
+ device_keys = {}
372
+ for r in rows:
373
+ if r["device"] not in device_keys:
374
+ with np.load(
375
+ Path(cache) / "frames-train" / (r["case_id"] + ".npz"),
376
+ allow_pickle=False,
377
+ ) as f:
378
+ device_keys[r["device"]] = f["boundary_keys"].tolist()
379
+ keys = sorted(set(k for v in device_keys.values() for k in v))
380
+ config = {
381
+ "architecture": "BoundaryNet",
382
+ "base": args.base,
383
+ "height": args.height,
384
+ "width": args.width,
385
+ "boundary_keys": keys,
386
+ "device_keys": device_keys,
387
+ "normalization": "within-frame-percentile-1-99.5-bilinear-v1",
388
+ "initialization": "random",
389
+ "seed": args.seed,
390
+ "frame_sampling_seed": preparation["arguments"]["seed"],
391
+ "cache_sampling_sha256": digest(Path(cache) / "sampling.jsonl"),
392
+ "sampling_balance": "device -> participant -> volume -> cached frame",
393
+ "augmentation": {
394
+ "horizontal_flip_probability": 0.5,
395
+ "vertical_shift_fraction": 1 / 12,
396
+ "vertical_shift_on_edge_touching_labels": False,
397
+ "gamma": [0.75, 1.4],
398
+ "gain": [0.8, 1.2],
399
+ "noise_std": 0.015,
400
+ },
401
+ "optimizer": {
402
+ "name": "AdamW",
403
+ "weight_decay": 1e-4,
404
+ "gradient_clip_norm": 5,
405
+ "warmup_steps": 500,
406
+ "cosine_floor": 0.05,
407
+ "ema_max_decay": 0.9995,
408
+ },
409
+ "numerics": {
410
+ "threads": 4,
411
+ "active_head_packing": args.pack_active_heads,
412
+ "cudnn_benchmark": True,
413
+ "deterministic_algorithms": False,
414
+ "allow_tf32_matmul": torch.backends.cuda.matmul.allow_tf32,
415
+ "allow_tf32_cudnn": torch.backends.cudnn.allow_tf32,
416
+ "mixed_precision": "bfloat16_if_supported_otherwise_float16_on_cuda",
417
+ },
418
+ "loss": {
419
+ "gaussian_sigma": args.sigma,
420
+ "regression_weight": args.regression_weight,
421
+ "regression_units": "original axial pixels",
422
+ "regression_loss": args.regression_loss,
423
+ "smooth_l1_beta_model_pixels": (
424
+ 2 if args.regression_loss == "smooth_l1" else None
425
+ ),
426
+ },
427
+ "decoder_sha256": preparation["decoder_sha256"],
428
+ "training_manifest_sha256": digest(args.train_manifest),
429
+ "validation_manifest_sha256": digest(args.validation_manifest),
430
+ }
431
+ config["training_schedule"] = {
432
+ "total_final_step": args.steps,
433
+ "batch_size": args.batch_size,
434
+ "maximum_learning_rate": args.learning_rate,
435
+ "save_every_steps": args.save_every,
436
+ }
437
+ configure_heads(config, args.head_mode)
438
+ if args.axial_prior:
439
+ config["architecture"] = "BoundaryNetAxialPrior"
440
+ config["axial_prior"] = {
441
+ "initialization": "zero for a new prior; restore or interpolate an existing own-trained prior on resume",
442
+ "shape": [1, len(config.get("model_head_keys", keys)), args.height, 1],
443
+ "training": "joint supervised training only; no label inputs at inference",
444
+ }
445
+ config["input_channels"] = args.input_channels
446
+ if args.input_channels == 3:
447
+ config["architecture"] = (
448
+ "BoundaryNetContextPrior" if args.axial_prior else "BoundaryNetContext"
449
+ )
450
+ config["input_context"] = {
451
+ "frame_offsets": [-1, 0, 1],
452
+ "endpoints": "replicated",
453
+ "supervised_frame": "center",
454
+ }
455
+ if args.input_channels == 5:
456
+ config["architecture"] = "BoundaryNetWidePrior"
457
+ config["input_context"] = {
458
+ "frame_offsets": [-8, -1, 0, 1, 8],
459
+ "endpoints": "replicated",
460
+ "supervised_frame": "center",
461
+ "initialization": "own C3 body unchanged plus zero distant-view convolution before first normalization",
462
+ }
463
+ if args.global_cache:
464
+ if args.input_channels != 6 or not args.axial_prior:
465
+ raise ValueError("Global context requires six channels and axial prior")
466
+ global_meta = json.loads(
467
+ (Path(args.global_cache) / "global-meta.json").read_text()
468
+ )
469
+ if global_meta["failures"] or global_meta["manifest_sha256"] != digest(
470
+ args.train_manifest
471
+ ):
472
+ raise ValueError("Global cache must match full training manifest")
473
+ if (global_meta["height"], global_meta["width"]) != (args.height, args.width):
474
+ raise ValueError("Global context resolution differs")
475
+ if global_meta["normalization_source_sha256"] != digest(
476
+ PROJECT_ROOT / "src/io.py"
477
+ ):
478
+ raise ValueError("Global normalization differs")
479
+ config["architecture"] = "BoundaryNetGlobalPrior"
480
+ config["global_context"] = {
481
+ "local_channels": 3,
482
+ "local_frame_offsets": [-1, 0, 1],
483
+ "global_frame_rule": "[0, floor(B/2), B-1]",
484
+ "encoder_input_shape": [96, 128],
485
+ "conditioning": "zero-initialized bottleneck channel scale and bias",
486
+ "initialization_seed": args.seed + 19991,
487
+ "global_cache_meta_sha256": digest(
488
+ Path(args.global_cache) / "global-meta.json"
489
+ ),
490
+ }
491
+ elif args.input_channels == 6:
492
+ raise ValueError("Six-channel training requires a global image cache")
493
+ if args.auxiliary_region_weight:
494
+ if not args.axial_prior or args.auxiliary_region_weight < 0:
495
+ raise ValueError("Positive auxiliary weight with axial prior required")
496
+ from src.models.auxiliary import configure_auxiliary
497
+
498
+ configure_auxiliary(config)
499
+ config["architecture"] = "BoundaryNetAuxiliaryPrior"
500
+ config["loss"]["auxiliary_region"] = {
501
+ "weight": args.auxiliary_region_weight,
502
+ "normalization": "per image and present ordinal interval class",
503
+ "support": "all required boundaries source-valid and nondecreasing; boundary objective unchanged",
504
+ "initialization": "zero auxiliary head; own trained backbone/prior retained",
505
+ }
506
+ if args.resume:
507
+ config["initialization"] = "own-trained-checkpoint"
508
+ config["resume_checkpoint_sha256"] = digest(args.resume)
509
+ (out / "config.json").write_text(json.dumps(config, indent=2))
510
+ (out / "provenance.json").write_text(
511
+ json.dumps(
512
+ {
513
+ "arguments": vars(args),
514
+ "source_hashes": source_hashes(),
515
+ "config_sha256": digest(out / "config.json"),
516
+ "approved_weights_manifest_sha256": (
517
+ digest(os.environ["AI_READI_WEIGHTS_MANIFEST"])
518
+ if os.environ.get("AI_READI_WEIGHTS_MANIFEST")
519
+ else None
520
+ ),
521
+ "environment_lock_sha256": (
522
+ digest(os.environ["AI_READI_ENVIRONMENT_LOCK"])
523
+ if os.environ.get("AI_READI_ENVIRONMENT_LOCK")
524
+ else None
525
+ ),
526
+ "resume_checkpoint_sha256": (
527
+ digest(args.resume) if args.resume else None
528
+ ),
529
+ "process_started_unix": SCRIPT_STARTED,
530
+ "import_and_cache_setup_seconds": time.monotonic() - SCRIPT_TIMER,
531
+ "torch_version": str(torch.__version__),
532
+ "numpy_version": np.__version__,
533
+ "data_source": "AI-READI v3.0.0 external read-only",
534
+ "pretrained_weights": [],
535
+ },
536
+ indent=2,
537
+ )
538
+ )
539
+ dataset = CachedFrames(
540
+ rows, cache, keys, args.packed_cache, config, args.global_cache
541
+ )
542
+ g = torch.Generator().manual_seed(args.seed)
543
+ model = build_model(config).to(args.device)
544
+ ema = build_model(config).to(args.device)
545
+ ema.load_state_dict(model.state_dict())
546
+ ema.requires_grad_(False)
547
+ optimizer = torch.optim.AdamW(
548
+ model.parameters(), lr=args.learning_rate, weight_decay=1e-4
549
+ )
550
+ start_step = 0
551
+ if args.resume:
552
+ ck = torch.load(args.resume, map_location=args.device, weights_only=False)
553
+ if ck["config"]["boundary_keys"] != keys:
554
+ raise ValueError("Checkpoint vocabulary changed")
555
+ state, remapped = remap_state(
556
+ ck.get("raw_model", ck["model"]), ck["config"], config
557
+ )
558
+ ema_state, _ = remap_state(ck["model"], ck["config"], config)
559
+ model.load_state_dict(state)
560
+ ema.load_state_dict(ema_state)
561
+ start_step = ck["step"]
562
+ optimizer_restored = (
563
+ "optimizer" in ck and not remapped and not args.reset_optimizer
564
+ )
565
+ if optimizer_restored:
566
+ optimizer.load_state_dict(ck["optimizer"])
567
+ (out / "resume-mapping.json").write_text(
568
+ json.dumps(
569
+ {
570
+ "source_step": start_step,
571
+ "state_shape_adapted": remapped,
572
+ "head_weights_duplicated": ck["config"].get("model_head_keys", keys)
573
+ != config.get("model_head_keys", keys),
574
+ "axial_prior_added": args.axial_prior
575
+ and "axial_prior" not in ck["model"],
576
+ "input_channel_mapping": (
577
+ "repeat_parent_filter_and_divide_by_three"
578
+ if ck["config"].get("input_channels", 1) == 1
579
+ and args.input_channels == 3
580
+ else "unchanged"
581
+ ),
582
+ "optimizer_restored": optimizer_restored,
583
+ "optimizer_reset_requested": args.reset_optimizer,
584
+ "source_checkpoint_sha256": digest(args.resume),
585
+ },
586
+ indent=2,
587
+ )
588
+ )
589
+ if start_step >= args.steps:
590
+ raise ValueError(
591
+ "--steps is the total final step and must exceed the resumed step"
592
+ )
593
+ draws = torch.multinomial(
594
+ torch.as_tensor(dataset.weights, dtype=torch.double),
595
+ num_samples=(args.steps - start_step) * args.batch_size,
596
+ replacement=True,
597
+ generator=g,
598
+ ).numpy()
599
+ loader = DataLoader(
600
+ dataset,
601
+ batch_size=args.batch_size,
602
+ sampler=draws.tolist(),
603
+ num_workers=args.workers,
604
+ pin_memory=args.device.startswith("cuda"),
605
+ worker_init_fn=seed_worker,
606
+ persistent_workers=args.workers > 0,
607
+ )
608
+ use_amp = args.device.startswith("cuda")
609
+ dtype = (
610
+ torch.bfloat16 if use_amp and torch.cuda.is_bf16_supported() else torch.float16
611
+ )
612
+ scaler = torch.amp.GradScaler("cuda", enabled=use_amp and dtype == torch.float16)
613
+ t = time.monotonic()
614
+ running = 0
615
+ step = start_step
616
+
617
+ def save(step, full=False):
618
+ state = {
619
+ "model": {k: v.detach().cpu() for k, v in ema.state_dict().items()},
620
+ "config": config,
621
+ "step": step,
622
+ "seed": args.seed,
623
+ "elapsed_seconds": time.monotonic() - t,
624
+ "start_step": start_step,
625
+ "frame_presentations_this_run": (step - start_step) * args.batch_size,
626
+ }
627
+ path = out / ("resume.pt" if full else f"candidate-{step:06d}.pt")
628
+ if full:
629
+ state.update(raw_model=model.state_dict(), optimizer=optimizer.state_dict())
630
+ consumed = (step - start_step) * args.batch_size
631
+ counts = np.bincount(draws[:consumed], minlength=len(dataset))
632
+ sample_cases = np.asarray([i for i, j in dataset.samples], dtype=np.int32)
633
+ np.savez_compressed(
634
+ out / "training-sample-counts.npz",
635
+ draw_indices=draws[:consumed].astype(np.int32),
636
+ sample_counts=counts,
637
+ case_ids=np.asarray([r["case_id"] for r in rows]),
638
+ case_index=sample_cases,
639
+ cached_frame_index=np.asarray(
640
+ [j for i, j in dataset.samples], dtype=np.int16
641
+ ),
642
+ )
643
+ device_usage = {}
644
+ for device in sorted(device_keys):
645
+ used = {
646
+ int(i)
647
+ for i in sample_cases[counts > 0]
648
+ if rows[int(i)]["device"] == device
649
+ }
650
+ device_usage[device] = {
651
+ "volumes_used": len(used),
652
+ "participants_used": len({rows[i]["participant_id"] for i in used}),
653
+ "distinct_cached_frames_used": int(
654
+ sum(
655
+ counts[n] > 0
656
+ for n, (i, j) in enumerate(dataset.samples)
657
+ if rows[i]["device"] == device
658
+ )
659
+ ),
660
+ "frame_presentations": int(
661
+ sum(
662
+ counts[n]
663
+ for n, (i, j) in enumerate(dataset.samples)
664
+ if rows[i]["device"] == device
665
+ )
666
+ ),
667
+ }
668
+ (out / "trained-cohort-summary.json").write_text(
669
+ json.dumps(
670
+ {
671
+ "step": step,
672
+ "start_step": start_step,
673
+ "presentations_in_this_run": consumed,
674
+ "cache_sampling_sha256": config["cache_sampling_sha256"],
675
+ "seed": args.seed,
676
+ "devices": device_usage,
677
+ },
678
+ indent=2,
679
+ )
680
+ )
681
+ tmp = path.with_suffix(".tmp")
682
+ torch.save(state, tmp)
683
+ tmp.replace(path)
684
+
685
+ model.train()
686
+ with (out / "training.jsonl").open("a") as log:
687
+ for index, (image, pos, valid, original_height) in enumerate(loader):
688
+ next_step = start_step + index + 1
689
+ if next_step > args.steps:
690
+ break
691
+ step = next_step
692
+ image = image.to(args.device, non_blocking=True)
693
+ pos = pos.to(args.device, non_blocking=True)
694
+ valid = valid.to(args.device, non_blocking=True)
695
+ image, pos, valid = augment(image, pos, valid)
696
+ coordinate_scale = (
697
+ original_height.to(args.device, non_blocking=True).float()
698
+ / image.shape[-2]
699
+ )
700
+ lr = (
701
+ args.learning_rate
702
+ * min(1, step / 500)
703
+ * (0.05 + 0.95 * 0.5 * (1 + math.cos(math.pi * step / args.steps)))
704
+ )
705
+ for group in optimizer.param_groups:
706
+ group["lr"] = lr
707
+ optimizer.zero_grad(set_to_none=True)
708
+ with torch.autocast(
709
+ device_type="cuda" if use_amp else "cpu", dtype=dtype, enabled=use_amp
710
+ ):
711
+ if args.auxiliary_region_weight:
712
+ from src.models.auxiliary import auxiliary_loss
713
+
714
+ boundary, regions = model(image, return_aux=True)
715
+ loss = boundary_loss(
716
+ boundary,
717
+ pos,
718
+ valid,
719
+ sigma=args.sigma,
720
+ regression_weight=args.regression_weight,
721
+ coordinate_scale=coordinate_scale,
722
+ regression_loss=args.regression_loss,
723
+ active_head_packing=args.pack_active_heads,
724
+ )
725
+ loss = loss + args.auxiliary_region_weight * auxiliary_loss(
726
+ regions, pos, valid, config
727
+ )
728
+ else:
729
+ loss = boundary_loss(
730
+ model(image),
731
+ pos,
732
+ valid,
733
+ sigma=args.sigma,
734
+ regression_weight=args.regression_weight,
735
+ coordinate_scale=coordinate_scale,
736
+ regression_loss=args.regression_loss,
737
+ active_head_packing=args.pack_active_heads,
738
+ )
739
+ if not torch.isfinite(loss):
740
+ raise FloatingPointError("Nonfinite training loss")
741
+ scaler.scale(loss).backward()
742
+ scaler.unscale_(optimizer)
743
+ torch.nn.utils.clip_grad_norm_(model.parameters(), 5)
744
+ scaler.step(optimizer)
745
+ scaler.update()
746
+ decay = min(0.9995, 1 - 1 / (step + 1))
747
+ with torch.no_grad():
748
+ for e, p in zip(ema.parameters(), model.parameters()):
749
+ e.lerp_(p, 1 - decay)
750
+ running += float(loss.detach())
751
+ if step % 50 == 0:
752
+ record = {
753
+ "step": step,
754
+ "loss": running / 50,
755
+ "learning_rate": lr,
756
+ "elapsed_seconds": time.monotonic() - t,
757
+ "frame_presentations_in_this_run": (step - start_step)
758
+ * args.batch_size,
759
+ }
760
+ log.write(json.dumps(record) + "\n")
761
+ log.flush()
762
+ print(json.dumps(record), flush=True)
763
+ running = 0
764
+ if step % args.save_every == 0:
765
+ save(step)
766
+ save(step, full=True)
767
+ save(step)
768
+ save(step, full=True)
769
+ summary = {
770
+ "step": step,
771
+ "seed": args.seed,
772
+ "training_loop_seconds": time.monotonic() - t,
773
+ "process_seconds": time.monotonic() - SCRIPT_TIMER,
774
+ "process_started_unix": SCRIPT_STARTED,
775
+ "device": args.device,
776
+ "cuda_version": torch.version.cuda,
777
+ "gpu_name": torch.cuda.get_device_name() if use_amp else None,
778
+ "peak_cuda_allocated_bytes": (
779
+ torch.cuda.max_memory_allocated() if use_amp else None
780
+ ),
781
+ "autocast_dtype": str(dtype) if use_amp else None,
782
+ "batch_size": args.batch_size,
783
+ "source_hashes": source_hashes(),
784
+ }
785
+ (out / "run-summary.json").write_text(json.dumps(summary, indent=2))
786
+ print(json.dumps(summary), flush=True)
787
+
788
+
789
+ if __name__ == "__main__":
790
+ main()
src/viewer.js ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (id, action) => {
2
+ const image = document.getElementById(id)?.querySelector(".image-frame img");
3
+ if (!image?.naturalWidth) return;
4
+ const viewport = image.closest("button");
5
+ const fresh = !image.octZoom;
6
+ const state = image.octZoom ??= { scale: 1, x: 0, y: 0, drag: null };
7
+ const paint = () => {
8
+ const bounds = viewport.getBoundingClientRect();
9
+ if (!bounds.width || !bounds.height) return;
10
+ const fit = Math.min(
11
+ image.clientWidth / image.naturalWidth,
12
+ image.clientHeight / image.naturalHeight,
13
+ 1
14
+ );
15
+ const limitX = Math.max(0, (image.naturalWidth * fit * state.scale - bounds.width) / 2);
16
+ const limitY = Math.max(0, (image.naturalHeight * fit * state.scale - bounds.height) / 2);
17
+ state.x = Math.max(-limitX, Math.min(limitX, state.x));
18
+ state.y = Math.max(-limitY, Math.min(limitY, state.y));
19
+ image.style.transformOrigin = "center";
20
+ image.style.transform = `translate(${state.x}px, ${state.y}px) scale(${state.scale})`;
21
+ viewport.style.overflow = "hidden";
22
+ viewport.style.cursor = state.scale > 1 ? "grab" : "";
23
+ viewport.style.touchAction = state.scale > 1 ? "none" : "";
24
+ };
25
+ if (fresh) {
26
+ image.draggable = false;
27
+ viewport.addEventListener("pointerdown", (event) => {
28
+ if (state.scale === 1 || event.button !== 0) return;
29
+ event.preventDefault();
30
+ viewport.setPointerCapture(event.pointerId);
31
+ state.drag = { id: event.pointerId, x: event.clientX, y: event.clientY };
32
+ });
33
+ viewport.addEventListener("pointermove", (event) => {
34
+ if (state.drag?.id !== event.pointerId) return;
35
+ state.x += event.clientX - state.drag.x;
36
+ state.y += event.clientY - state.drag.y;
37
+ state.drag.x = event.clientX;
38
+ state.drag.y = event.clientY;
39
+ paint();
40
+ });
41
+ for (const event of ["pointerup", "pointercancel", "lostpointercapture"]) {
42
+ viewport.addEventListener(event, () => { state.drag = null; });
43
+ }
44
+ image.addEventListener("load", paint);
45
+ }
46
+ state.scale = action === "fit" ? 1 : Math.max(
47
+ 1, Math.min(16, state.scale * (action === "in" ? 1.5 : 1 / 1.5))
48
+ );
49
+ paint();
50
+ }
src/viewer.py ADDED
@@ -0,0 +1,637 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Local DICOM viewing and inference adapter for the retained OCT submission.
2
+
3
+ The scientific model, normalization, head combinations and saved postprocessing
4
+ are imported from the researcher's original files. This module adds upload,
5
+ session and display handling; it does not retrain or alter the checkpoint.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from src.paths import PROJECT_ROOT
11
+
12
+ from collections import OrderedDict
13
+ from dataclasses import dataclass, field
14
+ from pathlib import Path
15
+ from threading import Lock, RLock
16
+ from typing import Iterator
17
+ import hashlib
18
+ import html
19
+ import json
20
+ import math
21
+ import os
22
+ import shutil
23
+ import tempfile
24
+ import time
25
+ import uuid
26
+
27
+ import numpy as np
28
+ from PIL import Image, ImageDraw
29
+ import pydicom
30
+ import torch
31
+ from torch.nn import functional as F
32
+
33
+ from src.models import build_model, output_indices
34
+ from src.inference.context import OFFSETS, iter_normalized_contexts
35
+ from src.inference.runtime import configure_inference, autocast_options
36
+ from src.io import volume_headers, read_frame, read_frames, normalize_frame
37
+ from src.inference.decoding import positions_from_logits
38
+ from src.inference.combination import combine_named_heads, finish_positions
39
+
40
+ ROOT = PROJECT_ROOT
41
+ CHECKPOINT = ROOT / "artifacts/checkpoint.pt"
42
+ STRUCTURAL_OCT = "1.2.840.10008.5.1.4.1.1.77.1.5.4"
43
+ FAMILIES = {
44
+ "heidelberg_spectralis": "Heidelberg Spectralis",
45
+ "topcon_maestro2": "Topcon Maestro2",
46
+ "topcon_triton": "Topcon Triton",
47
+ "zeiss_cirrus": "Zeiss Cirrus",
48
+ }
49
+ AUTO = "auto"
50
+ NAMES = {
51
+ "SCT_280677004": "ILM",
52
+ "DCM_128289": "RNFL · outer",
53
+ "DCM_128290": "GCL · outer",
54
+ "DCM_128291": "IPL · outer",
55
+ "DCM_128292": "INL · outer",
56
+ "DCM_128293": "OPL · outer",
57
+ "SCT_76710003": "ELM",
58
+ "DCM_128320": "EZ · inner",
59
+ "DCM_128321": "EZ · midline",
60
+ "DCM_128323": "Interdigitation zone · inner",
61
+ "DCM_128297": "RPE · inner",
62
+ "DCM_128299": "RPE · outer",
63
+ "DCM_128300": "Bruch’s membrane · outer",
64
+ "DCM_128298": "RPE · centre",
65
+ "DCM_128301": "Choroid–sclera interface",
66
+ }
67
+ PALETTE = [
68
+ "#22d3ee",
69
+ "#a3e635",
70
+ "#fbbf24",
71
+ "#fb7185",
72
+ "#c084fc",
73
+ "#34d399",
74
+ "#f472b6",
75
+ "#60a5fa",
76
+ "#f97316",
77
+ "#e879f9",
78
+ "#2dd4bf",
79
+ "#fde047",
80
+ "#a5b4fc",
81
+ "#fda4af",
82
+ "#d9f99d",
83
+ ]
84
+ COLORS = {key: PALETTE[i % len(PALETTE)] for i, key in enumerate(NAMES)}
85
+
86
+
87
+ class ViewerError(ValueError):
88
+ """A recoverable error suitable for displaying in the viewer."""
89
+
90
+
91
+ @dataclass
92
+ class Prediction:
93
+ positions: np.ndarray # K, number of predicted B-scans, original A
94
+ keys: list[str]
95
+ frames: list[int]
96
+ family: str
97
+ compute: str
98
+ complete_volume: bool
99
+ seconds: float
100
+ export: Path
101
+
102
+
103
+ @dataclass
104
+ class Volume:
105
+ identity: str
106
+ directory: Path
107
+ paths: list[Path]
108
+ headers: list
109
+ shape: tuple[int, int, int]
110
+ detected_family: str | None
111
+ axial_mm: float | None
112
+ lateral_mm: float | None
113
+ filename: str
114
+ prediction: Prediction | None = None
115
+ view_frame: int = 0
116
+ view_settings: tuple | None = None
117
+ frames: OrderedDict = field(default_factory=OrderedDict)
118
+ cancelled: bool = False
119
+ updated: float = field(default_factory=time.monotonic)
120
+
121
+ def frame(self, index: int) -> np.ndarray:
122
+ index = max(0, min(int(index), self.shape[0] - 1))
123
+ self.updated = time.monotonic()
124
+ if index not in self.frames:
125
+ try:
126
+ pixels = np.asarray(read_frame(self.headers, index))
127
+ except Exception as exc:
128
+ raise ViewerError(
129
+ "Could not decode this DICOM. Install requirements.txt "
130
+ "for JPEG2000 and lossless JPEG support. " + str(exc)
131
+ ) from exc
132
+ if pixels.shape != self.shape[1:]:
133
+ raise ViewerError(
134
+ "Decoded image dimensions do not match the DICOM header."
135
+ )
136
+ self.frames[index] = pixels
137
+ while len(self.frames) > 8:
138
+ self.frames.popitem(last=False)
139
+ self.frames.move_to_end(index)
140
+ return self.frames[index]
141
+
142
+
143
+ class SessionStore:
144
+ def __init__(self):
145
+ self.volumes: dict[str, Volume] = {}
146
+ self.lock = RLock()
147
+
148
+ def add(self, volume: Volume) -> str:
149
+ with self.lock:
150
+ self.reap()
151
+ self.volumes[volume.identity] = volume
152
+ return volume.identity
153
+
154
+ def get(self, identity: str | None) -> Volume:
155
+ with self.lock:
156
+ if not identity or identity not in self.volumes:
157
+ raise ViewerError("Upload a structural OCT DICOM first.")
158
+ volume = self.volumes[identity]
159
+ volume.updated = time.monotonic()
160
+ return volume
161
+
162
+ def remove(self, identity: str | None):
163
+ with self.lock:
164
+ volume = self.volumes.pop(identity, None)
165
+ if volume:
166
+ volume.cancelled = True
167
+ shutil.rmtree(volume.directory, ignore_errors=True)
168
+
169
+ def reap(self):
170
+ for identity, volume in list(self.volumes.items()):
171
+ if time.monotonic() - volume.updated > 7200:
172
+ self.remove(identity)
173
+
174
+
175
+ STORE = SessionStore()
176
+
177
+
178
+ def detect_family(header) -> str | None:
179
+ description = " ".join(
180
+ str(header.get(tag, ""))
181
+ for tag in ("Manufacturer", "ManufacturerModelName", "SeriesDescription")
182
+ ).lower()
183
+ if "spectralis" in description or "heidelberg" in description:
184
+ return "heidelberg_spectralis"
185
+ if "maestro" in description:
186
+ return "topcon_maestro2"
187
+ if "triton" in description:
188
+ return "topcon_triton"
189
+ if "cirrus" in description or "zeiss" in description:
190
+ return "zeiss_cirrus"
191
+ return None
192
+
193
+
194
+ def pixel_spacing(header) -> tuple[float | None, float | None]:
195
+ candidates = []
196
+ for key in ("SharedFunctionalGroupsSequence", "PerFrameFunctionalGroupsSequence"):
197
+ sequence = header.get(key, [])
198
+ if sequence:
199
+ measures = sequence[0].get("PixelMeasuresSequence", [])
200
+ if measures:
201
+ candidates.append(measures[0].get("PixelSpacing"))
202
+ candidates.append(header.get("PixelSpacing"))
203
+ for values in candidates:
204
+ if values is not None and len(values) == 2:
205
+ row, col = map(float, values)
206
+ if math.isfinite(row) and math.isfinite(col) and row > 0 and col > 0:
207
+ return row, col
208
+ return None, None
209
+
210
+
211
+ def load_upload(files: list[str] | str) -> Volume:
212
+ if isinstance(files, str):
213
+ files = [files]
214
+ if not files:
215
+ raise ViewerError("Choose a .dcm file to begin.")
216
+ directory = Path(tempfile.mkdtemp(prefix="myxos-oct-viewer-"))
217
+ os.chmod(directory, 0o700)
218
+ try:
219
+ paths = []
220
+ all_headers = []
221
+ for index, uploaded in enumerate(files):
222
+ source = Path(uploaded)
223
+ if not source.is_file():
224
+ raise ViewerError(
225
+ "An uploaded DICOM is no longer available. Upload it again."
226
+ )
227
+ # A private copy decouples inference from Gradio's upload-cache lifetime.
228
+ path = directory / f"part-{index:03d}.dcm"
229
+ shutil.copyfile(source, path)
230
+ os.chmod(path, 0o600)
231
+ header = pydicom.dcmread(path, stop_before_pixels=True)
232
+ if str(header.get("SOPClassUID", "")) != STRUCTURAL_OCT:
233
+ raise ViewerError(
234
+ "This is not a structural OCT DICOM. Upload the OCT volume, rather than an OCTA or segmentation export."
235
+ )
236
+ if int(header.get("SamplesPerPixel", 1)) != 1:
237
+ raise ViewerError(
238
+ "The selected checkpoint expects grayscale structural OCT."
239
+ )
240
+ all_headers.append(header)
241
+ paths.append(path)
242
+ for tag in ("StudyInstanceUID", "SeriesInstanceUID", "ConcatenationUID"):
243
+ identifiers = {str(h.get(tag)) for h in all_headers if h.get(tag)}
244
+ if len(identifiers) > 1:
245
+ raise ViewerError(
246
+ "Upload one OCT volume, or the contiguous DICOM parts of that same volume."
247
+ )
248
+ headers, shape = volume_headers(directory, [p.name for p in paths])
249
+ if not all(int(n) > 0 for n in shape):
250
+ raise ViewerError("The DICOM contains an empty volume.")
251
+ axial, lateral = pixel_spacing(all_headers[0])
252
+ volume = Volume(
253
+ uuid.uuid4().hex,
254
+ directory,
255
+ paths,
256
+ headers,
257
+ tuple(shape),
258
+ detect_family(all_headers[0]),
259
+ axial,
260
+ lateral,
261
+ Path(files[0]).name,
262
+ )
263
+ # Fail early when a required codec is missing.
264
+ volume.frame((shape[0] - 1) // 2)
265
+ return volume
266
+ except ViewerError:
267
+ shutil.rmtree(directory, ignore_errors=True)
268
+ raise
269
+ except Exception as exc:
270
+ shutil.rmtree(directory, ignore_errors=True)
271
+ raise ViewerError("Unable to load this OCT volume: " + str(exc)) from exc
272
+
273
+
274
+ def resolve_family(volume: Volume, selected: str) -> str:
275
+ family = volume.detected_family if selected == AUTO else selected
276
+ if family not in FAMILIES:
277
+ raise ViewerError(
278
+ "The scanner could not be identified. Select its scanner family below."
279
+ )
280
+ return family
281
+
282
+
283
+ def boundary_choices(family: str | None) -> list[tuple[str, str]]:
284
+ cfg = json.loads((ROOT / "artifacts/config.json").read_text())
285
+ keys = cfg["device_keys"].get(family, [])
286
+ return [(NAMES.get(key, key), key) for key in keys]
287
+
288
+
289
+ def compute_choices() -> list[tuple[str, str]]:
290
+ choices = [("Automatic", "auto"), ("CPU", "cpu")]
291
+ if torch.cuda.is_available():
292
+ choices.append(("NVIDIA GPU · CUDA", "cuda"))
293
+ if torch.backends.mps.is_available():
294
+ choices.append(("Apple GPU · MPS", "mps"))
295
+ return choices
296
+
297
+
298
+ def resolve_compute(selected: str) -> str:
299
+ if selected == "auto":
300
+ return (
301
+ "cuda"
302
+ if torch.cuda.is_available()
303
+ else ("mps" if torch.backends.mps.is_available() else "cpu")
304
+ )
305
+ if selected not in {value for _, value in compute_choices()}:
306
+ raise ViewerError("That compute device is not available on this machine.")
307
+ return selected
308
+
309
+
310
+ def selected_contexts(headers, frames: list[int], shapes: list[tuple]):
311
+ total = sum(int(ds.get("NumberOfFrames", 1)) for _, ds in headers)
312
+ indices = set()
313
+ for frame in frames:
314
+ for shape in shapes:
315
+ indices.update(
316
+ max(0, min(total - 1, frame + offset)) for offset in OFFSETS[shape[2]]
317
+ )
318
+ if shape[2] == 6:
319
+ indices.update([0, total // 2, total - 1])
320
+ raw = read_frames(headers, sorted(indices))
321
+ normalized = {}
322
+ for frame in frames:
323
+ images = []
324
+ for h, a, channels in shapes:
325
+ context = [
326
+ max(0, min(total - 1, frame + offset)) for offset in OFFSETS[channels]
327
+ ]
328
+ if channels == 6:
329
+ context += [0, total // 2, total - 1]
330
+ channels_out = []
331
+ for index in context:
332
+ key = index, h, a
333
+ if key not in normalized:
334
+ normalized[key] = normalize_frame(raw[index], (h, a))
335
+ channels_out.append(normalized[key])
336
+ images.append(np.stack(channels_out))
337
+ yield images
338
+
339
+
340
+ class InferenceEngine:
341
+ def __init__(self):
342
+ # A plain Lock can span generator yields, unlike thread-owned RLock.
343
+ self.lock = Lock()
344
+ self.checkpoint = None
345
+ self.models = {}
346
+ self.device = None
347
+
348
+ def load(self, device: str):
349
+ if self.checkpoint is None:
350
+ self.checkpoint = torch.load(
351
+ CHECKPOINT, map_location="cpu", weights_only=True
352
+ )
353
+ if self.checkpoint["config"]["architecture"] != "BoundaryEnsemble":
354
+ raise ViewerError(
355
+ "This viewer expects the retained Researcher 1 ensemble checkpoint."
356
+ )
357
+ if self.device != device:
358
+ self.models.clear()
359
+ self.device = device
360
+ if torch.cuda.is_available():
361
+ torch.cuda.empty_cache()
362
+ if torch.backends.mps.is_available():
363
+ torch.mps.empty_cache()
364
+ return self.checkpoint
365
+
366
+ def members(self, checkpoint, family):
367
+ cfg = checkpoint["config"]
368
+ combination = cfg["combination"]
369
+ active = None
370
+ if "head_weights_by_device" in combination:
371
+ active = {
372
+ name
373
+ for rule in combination["head_weights_by_device"][family].values()
374
+ for name in rule
375
+ }
376
+ elif "head_source_by_device" in combination:
377
+ active = set(combination["head_source_by_device"][family].values())
378
+ result = []
379
+ for saved in checkpoint["members"]:
380
+ if active is not None and saved["name"] not in active:
381
+ continue
382
+ if saved["name"] not in self.models:
383
+ model = build_model(saved["config"])
384
+ model.load_state_dict(saved["model"], strict=True)
385
+ self.models[saved["name"]] = (
386
+ model.to(self.device).eval().requires_grad_(False)
387
+ )
388
+ result.append(
389
+ {
390
+ "name": saved["name"],
391
+ "config": saved["config"],
392
+ "model": self.models[saved["name"]],
393
+ }
394
+ )
395
+ return result
396
+
397
+ def predict(
398
+ self,
399
+ volume: Volume,
400
+ family: str,
401
+ compute: str,
402
+ batch_size: int,
403
+ full_volume: bool,
404
+ frame_index: int,
405
+ ) -> Iterator[dict]:
406
+ device = resolve_compute(compute)
407
+ batch_size = max(1, min(32, int(batch_size)))
408
+ b, h, a = volume.shape
409
+ frames = (
410
+ list(range(b)) if full_volume else [max(0, min(b - 1, int(frame_index)))]
411
+ )
412
+ started = time.monotonic()
413
+ volume.cancelled = False
414
+ with self.lock:
415
+ configure_inference("fp32")
416
+ checkpoint = self.load(device)
417
+ members = self.members(checkpoint, family)
418
+ cfg = checkpoint["config"]
419
+ keys = cfg["device_keys"][family]
420
+ shapes = sorted(
421
+ {
422
+ (
423
+ m["config"]["height"],
424
+ m["config"]["width"],
425
+ m["config"].get("input_channels", 1),
426
+ )
427
+ for m in members
428
+ }
429
+ )
430
+ buffers = [
431
+ np.empty((len(keys), len(frames), a), np.float32) for _ in members
432
+ ]
433
+ iterator = (
434
+ iter_normalized_contexts(volume.headers, shapes)
435
+ if full_volume
436
+ else selected_contexts(volume.headers, frames, shapes)
437
+ )
438
+ batch = []
439
+ completed = 0
440
+ for images in iterator:
441
+ if volume.cancelled:
442
+ raise ViewerError("Segmentation cancelled.")
443
+ batch.append(images)
444
+ if len(batch) < batch_size and completed + len(batch) < len(frames):
445
+ continue
446
+ # Do not keep thread-local inference contexts open across yields.
447
+ with torch.inference_mode(), torch.autocast(
448
+ **autocast_options(device, "fp32")
449
+ ):
450
+ tensors = [
451
+ torch.from_numpy(np.stack([item[i] for item in batch])).to(
452
+ device
453
+ )
454
+ for i in range(len(shapes))
455
+ ]
456
+ for member, buffer in zip(members, buffers):
457
+ own = member["config"]
458
+ shape = (
459
+ own["height"],
460
+ own["width"],
461
+ own.get("input_channels", 1),
462
+ )
463
+ indices = output_indices(own, family)
464
+ logits = member["model"](tensors[shapes.index(shape)])[
465
+ :, indices
466
+ ]
467
+ positions = positions_from_logits(logits, own, family)
468
+ positions = F.interpolate(
469
+ positions, size=a, mode="linear", align_corners=False
470
+ )
471
+ positions = ((positions + 0.5) * h / own["height"] - 0.5).clamp(
472
+ 0, h - 1
473
+ )
474
+ order = [own["device_keys"][family].index(key) for key in keys]
475
+ values = (
476
+ positions.float().cpu().numpy().transpose(1, 0, 2)[order]
477
+ )
478
+ buffer[:, completed : completed + len(batch)] = values
479
+ del logits, positions
480
+ completed += len(batch)
481
+ batch = []
482
+ volume.updated = time.monotonic()
483
+ yield {
484
+ "completed": completed,
485
+ "total": len(frames),
486
+ "device": device,
487
+ "seconds": time.monotonic() - started,
488
+ }
489
+ if volume.cancelled:
490
+ raise ViewerError("Segmentation cancelled.")
491
+ components = [
492
+ finish_positions(
493
+ values,
494
+ family,
495
+ h,
496
+ member["config"].get("inference", {}).get("postprocess", {}),
497
+ )
498
+ for values, member in zip(buffers, members)
499
+ ]
500
+ positions = combine_named_heads(
501
+ components,
502
+ [m["name"] for m in members],
503
+ keys,
504
+ family,
505
+ cfg["combination"],
506
+ )
507
+ positions = finish_positions(
508
+ positions, family, h, cfg.get("inference", {}).get("postprocess", {})
509
+ )
510
+ if completed != len(frames) or not np.isfinite(positions).all():
511
+ raise ViewerError(
512
+ "Inference did not produce complete finite boundary predictions."
513
+ )
514
+ suffix = "volume" if full_volume else f"preview-bscan-{frames[0] + 1:04d}"
515
+ output = volume.directory / f"boundaries-{suffix}.npz"
516
+ arrays = {"positions": positions, "boundary_keys": np.asarray(keys)}
517
+ if not full_volume:
518
+ arrays["frame_indices"] = np.asarray(frames, dtype=np.int32)
519
+ np.savez_compressed(output, **arrays)
520
+ prediction = Prediction(
521
+ positions,
522
+ list(keys),
523
+ frames,
524
+ family,
525
+ device,
526
+ full_volume,
527
+ time.monotonic() - started,
528
+ output,
529
+ )
530
+ volume.prediction = prediction
531
+ yield {"result": prediction}
532
+
533
+
534
+ ENGINE = InferenceEngine()
535
+
536
+
537
+ def display_dimensions(volume: Volume, physical: bool) -> tuple[int, int]:
538
+ _, h, a = volume.shape
539
+ ratio = h / a
540
+ if physical and volume.axial_mm and volume.lateral_mm:
541
+ ratio *= volume.axial_mm / volume.lateral_mm
542
+ width = 1000
543
+ height = max(1, round(width * ratio))
544
+ scale = min(1.0, 1400 / max(width, height))
545
+ return max(1, round(width * scale)), max(1, round(height * scale))
546
+
547
+
548
+ def render(
549
+ volume: Volume,
550
+ index: int,
551
+ selected_keys: list[str] | None = None,
552
+ overlay: bool = True,
553
+ opacity: float = 0.9,
554
+ line_width: int = 2,
555
+ labels: bool = False,
556
+ contrast: float = 1.0,
557
+ brightness: float = 0.0,
558
+ physical: bool = True,
559
+ ) -> tuple[np.ndarray, np.ndarray | None]:
560
+ index = max(0, min(volume.shape[0] - 1, int(index)))
561
+ image = np.asarray(volume.frame(index), dtype=np.float32)
562
+ finite = image[np.isfinite(image)]
563
+ lo, hi = np.quantile(finite, [0.01, 0.995]) if finite.size else (0, 1)
564
+ gray = np.nan_to_num((image - lo) / max(float(hi - lo), 1e-6))
565
+ gray = np.clip((gray - 0.5) * float(contrast) + 0.5 + float(brightness), 0, 1)
566
+ width, height = display_dimensions(volume, physical)
567
+ base = (
568
+ Image.fromarray(np.uint8(np.round(gray * 255)))
569
+ .convert("RGB")
570
+ .resize((width, height), Image.Resampling.BILINEAR)
571
+ )
572
+ raw = np.asarray(base)
573
+ prediction = volume.prediction
574
+ if prediction is None or index not in prediction.frames:
575
+ return raw, None
576
+ if not overlay:
577
+ return raw, raw.copy()
578
+ frame = prediction.frames.index(index)
579
+ selected = set(prediction.keys if selected_keys is None else selected_keys)
580
+ layer = base.convert("RGBA")
581
+ annotation = Image.new("RGBA", base.size)
582
+ draw = ImageDraw.Draw(annotation)
583
+ _, native_h, native_a = volume.shape
584
+ x = np.linspace(0, width - 1, native_a)
585
+ for key, values in zip(prediction.keys, prediction.positions[:, frame]):
586
+ if key not in selected:
587
+ continue
588
+ y = np.clip(values, 0, native_h - 1) * (height - 1) / max(1, native_h - 1)
589
+ color = COLORS.get(key, "#ffffff")
590
+ rgb = tuple(int(color[i : i + 2], 16) for i in (1, 3, 5))
591
+ points = list(zip(x.tolist(), y.tolist()))
592
+ if len(points) > 1:
593
+ draw.line(
594
+ points,
595
+ fill=(*rgb, round(255 * float(opacity))),
596
+ width=max(1, int(line_width)),
597
+ joint="curve",
598
+ )
599
+ if labels:
600
+ text = NAMES.get(key, key)
601
+ anchor = max(
602
+ 0,
603
+ min(
604
+ height - 14,
605
+ round(float(np.median(y[: max(1, native_a // 8)])) - 13),
606
+ ),
607
+ )
608
+ box = draw.textbbox((8, anchor), text)
609
+ draw.rectangle(
610
+ (box[0] - 3, box[1] - 2, box[2] + 3, box[3] + 2), fill=(8, 15, 26, 210)
611
+ )
612
+ draw.text((8, anchor), text, fill=(*rgb, 255))
613
+ return raw, np.asarray(Image.alpha_composite(layer, annotation).convert("RGB"))
614
+
615
+
616
+ def legend(keys: list[str]) -> str:
617
+ if not keys:
618
+ return "Boundary colours appear after a scan is loaded."
619
+ items = []
620
+ for key in keys:
621
+ items.append(
622
+ f'<span style="color:{COLORS.get(key, "#94a3b8")}">●</span> {html.escape(NAMES.get(key, key))}'
623
+ )
624
+ return " · ".join(items)
625
+
626
+
627
+ def volume_info(volume: Volume, family: str | None) -> str:
628
+ b, h, a = volume.shape
629
+ scanner = FAMILIES.get(family, "Select scanner family")
630
+ spacing = f" · axial {volume.axial_mm * 1000:.2f} µm" if volume.axial_mm else ""
631
+ return (
632
+ f"**{html.escape(scanner)}** \n{b:,} B-scans · {h:,} × {a:,} pixels{spacing}"
633
+ )
634
+
635
+
636
+ def checkpoint_identity() -> str:
637
+ return hashlib.sha256(CHECKPOINT.read_bytes()).hexdigest()