OCT_Segmentation_a2 / src /viewer.py
farrell236's picture
Deploy OCT viewer runtime and bundled checkpoint
bf88397 verified
Raw History Blame Contribute Delete
27 kB
"""Local DICOM viewing and inference adapter for the retained OCT submission.
The Gradio controls and display behavior match OCT_submission. Prediction uses
this project's saved model, normalization and postprocessing in an isolated
worker; model weights and research records are unchanged.
"""
from __future__ import annotations
import argparse
import copy
import html
import json
import math
import os
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import uuid
from collections import OrderedDict
from dataclasses import dataclass, field
from pathlib import Path
from threading import Lock, RLock
import numpy as np
import pydicom
import torch
from PIL import Image, ImageDraw, ImageOps
from src.io import source_frames, structural_header
ROOT = Path(__file__).resolve().parents[1]
CHECKPOINT = ROOT / "artifacts/checkpoint.pt"
STRUCTURAL_OCT = "1.2.840.10008.5.1.4.1.1.77.1.5.4"
FAMILIES = {
"heidelberg_spectralis": "Heidelberg Spectralis",
"topcon_maestro2": "Topcon Maestro2",
"topcon_triton": "Topcon Triton",
"zeiss_cirrus": "Zeiss Cirrus",
}
AUTO = "auto"
NAMES = {
"sct_280677004": "ILM",
"dcm_128289": "RNFL · outer",
"dcm_128290": "GCL · outer",
"dcm_128291": "IPL · outer",
"dcm_128292": "INL · outer",
"dcm_128293": "OPL · outer",
"sct_76710003": "ELM",
"dcm_128320": "EZ · inner",
"dcm_128321": "EZ · midline",
"dcm_128323": "Interdigitation zone · inner",
"dcm_128297": "RPE · inner",
"dcm_128299": "RPE · outer",
"dcm_128300": "Bruch’s membrane · outer",
"dcm_128298": "RPE · centre",
"dcm_128301": "Choroid–sclera interface",
}
PALETTE = [
"#22d3ee",
"#a3e635",
"#fbbf24",
"#fb7185",
"#c084fc",
"#34d399",
"#f472b6",
"#60a5fa",
"#f97316",
"#e879f9",
"#2dd4bf",
"#fde047",
"#a5b4fc",
"#fda4af",
"#d9f99d",
]
COLORS = {key: PALETTE[i % len(PALETTE)] for i, key in enumerate(NAMES)}
class ViewerError(ValueError):
"""A recoverable error suitable for displaying in the viewer."""
@dataclass
class Prediction:
positions: np.ndarray # K, number of predicted B-scans, original A
keys: list[str]
frames: list[int]
family: str
compute: str
complete_volume: bool
seconds: float
export: Path
@dataclass
class Volume:
identity: str
directory: Path
paths: list[Path]
headers: list
shape: tuple[int, int, int]
detected_family: str | None
axial_mm: float | None
lateral_mm: float | None
filename: str
prediction: Prediction | None = None
view_frame: int = 0
view_settings: tuple | None = None
frames: OrderedDict = field(default_factory=OrderedDict)
cancelled: bool = False
updated: float = field(default_factory=time.monotonic)
def frame(self, index: int) -> np.ndarray:
index = max(0, min(int(index), self.shape[0] - 1))
self.updated = time.monotonic()
if index not in self.frames:
try:
for path, header in self.headers:
offset = int(header.get("ConcatenationFrameOffsetNumber", 0))
if offset <= index < offset + int(header.NumberOfFrames):
pixels = np.asarray(
next(source_frames(path, indices=[index - offset]))
)
break
else:
raise IndexError("Frame outside volume")
except Exception as exc:
raise ViewerError(
"Could not decode this DICOM. Install requirements.txt "
"for JPEG2000 and lossless JPEG support. " + str(exc)
) from exc
if pixels.shape != self.shape[1:]:
raise ViewerError(
"Decoded image dimensions do not match the DICOM header."
)
self.frames[index] = pixels
while len(self.frames) > 8:
self.frames.popitem(last=False)
self.frames.move_to_end(index)
return self.frames[index]
class SessionStore:
def __init__(self):
self.volumes: dict[str, Volume] = {}
self.lock = RLock()
def add(self, volume: Volume) -> str:
with self.lock:
self.reap()
self.volumes[volume.identity] = volume
return volume.identity
def get(self, identity: str | None) -> Volume:
with self.lock:
if not identity or identity not in self.volumes:
raise ViewerError("Upload a structural OCT DICOM first.")
volume = self.volumes[identity]
volume.updated = time.monotonic()
return volume
def remove(self, identity: str | None):
with self.lock:
volume = self.volumes.pop(identity, None)
if volume:
volume.cancelled = True
shutil.rmtree(volume.directory, ignore_errors=True)
def reap(self):
for identity, volume in list(self.volumes.items()):
if time.monotonic() - volume.updated > 7200:
self.remove(identity)
STORE = SessionStore()
def detect_family(header) -> str | None:
description = " ".join(
str(header.get(tag, ""))
for tag in ("Manufacturer", "ManufacturerModelName", "SeriesDescription")
).lower()
if "spectralis" in description or "heidelberg" in description:
return "heidelberg_spectralis"
if "maestro" in description:
return "topcon_maestro2"
if "triton" in description:
return "topcon_triton"
if "cirrus" in description or "zeiss" in description:
return "zeiss_cirrus"
return None
def pixel_spacing(header) -> tuple[float | None, float | None]:
candidates = []
for key in ("SharedFunctionalGroupsSequence", "PerFrameFunctionalGroupsSequence"):
sequence = header.get(key, [])
if sequence:
measures = sequence[0].get("PixelMeasuresSequence", [])
if measures:
candidates.append(measures[0].get("PixelSpacing"))
candidates.append(header.get("PixelSpacing"))
for values in candidates:
if values is not None and len(values) == 2:
row, col = map(float, values)
if math.isfinite(row) and math.isfinite(col) and row > 0 and col > 0:
return row, col
return None, None
def load_upload(files: list[str] | str) -> Volume:
if isinstance(files, str):
files = [files]
if not files:
raise ViewerError("Choose a .dcm file to begin.")
directory = Path(tempfile.mkdtemp(prefix="myxos-oct-viewer-"))
os.chmod(directory, 0o700)
try:
data = directory / "data"
data.mkdir(mode=0o700)
paths = []
all_headers = []
for index, uploaded in enumerate(files):
source = Path(uploaded)
if not source.is_file():
raise ViewerError(
"An uploaded DICOM is no longer available. Upload it again."
)
# A private copy decouples inference from Gradio's upload-cache lifetime.
path = data / f"part-{index:03d}.dcm"
shutil.copyfile(source, path)
os.chmod(path, 0o600)
header = pydicom.dcmread(path, stop_before_pixels=True)
if str(header.get("SOPClassUID", "")) != STRUCTURAL_OCT:
raise ViewerError(
"This is not a structural OCT DICOM. Upload the OCT volume, rather than an OCTA or segmentation export."
)
if int(header.get("SamplesPerPixel", 1)) != 1:
raise ViewerError(
"The selected checkpoint expects grayscale structural OCT."
)
all_headers.append(header)
paths.append(path)
for tag in ("StudyInstanceUID", "SeriesInstanceUID", "ConcatenationUID"):
identifiers = {str(h.get(tag)) for h in all_headers if h.get(tag)}
if len(identifiers) > 1:
raise ViewerError(
"Upload one OCT volume, or the contiguous DICOM parts of that same volume."
)
headers = [(path, structural_header(path)) for path in paths]
headers.sort(
key=lambda item: int(item[1].get("ConcatenationFrameOffsetNumber", 0))
)
count = 0
height, width = int(headers[0][1].Rows), int(headers[0][1].Columns)
for path, header in headers:
if int(header.get("ConcatenationFrameOffsetNumber", 0)) != count:
raise ViewerError("Missing or overlapping concatenation frames.")
if (int(header.Rows), int(header.Columns)) != (height, width):
raise ViewerError("Inconsistent structural geometry.")
if header.PhotometricInterpretation not in {"MONOCHROME1", "MONOCHROME2"}:
raise ViewerError(
"The selected checkpoint expects grayscale structural OCT."
)
count += int(header.NumberOfFrames)
if any(
int(h.get("InConcatenationTotalNumber", len(paths))) != len(paths)
for h in all_headers
):
raise ViewerError("Upload all contiguous DICOM parts of this volume.")
paths = [path for path, _ in headers]
shape = count, height, width
if not all(int(n) > 0 for n in shape):
raise ViewerError("The DICOM contains an empty volume.")
axial, lateral = pixel_spacing(all_headers[0])
volume = Volume(
uuid.uuid4().hex,
directory,
paths,
headers,
tuple(shape),
detect_family(all_headers[0]),
axial,
lateral,
Path(files[0]).name,
)
# Fail early when a required codec is missing.
volume.frame((shape[0] - 1) // 2)
return volume
except ViewerError:
shutil.rmtree(directory, ignore_errors=True)
raise
except Exception as exc:
shutil.rmtree(directory, ignore_errors=True)
raise ViewerError("Unable to load this OCT volume: " + str(exc)) from exc
def resolve_family(volume: Volume, selected: str) -> str:
family = volume.detected_family if selected == AUTO else selected
if family not in FAMILIES:
raise ViewerError(
"The scanner could not be identified. Select its scanner family below."
)
return family
def boundary_choices(family: str | None) -> list[tuple[str, str]]:
config = ENGINE.checkpoint.with_name("config.json")
cfg = (
json.loads(config.read_text())
if config.is_file()
else torch.load(ENGINE.checkpoint, map_location="cpu", weights_only=False)[
"config"
]
)
keys = cfg["boundary_keys"].get(family, [])
return [(NAMES.get(key, key), key) for key in keys]
def compute_choices() -> list[tuple[str, str]]:
choices = [("Automatic", "auto"), ("CPU", "cpu")]
if ENGINE.checkpoint.suffix == ".npz":
return choices
if torch.cuda.is_available():
choices.append(("NVIDIA GPU · CUDA", "cuda"))
if torch.backends.mps.is_available():
choices.append(("Apple GPU · MPS", "mps"))
return choices
def resolve_compute(selected: str) -> str:
if selected == "auto":
if ENGINE.checkpoint.suffix == ".npz":
return "cpu"
return (
"cuda"
if torch.cuda.is_available()
else ("mps" if torch.backends.mps.is_available() else "cpu")
)
if selected not in {value for _, value in compute_choices()}:
raise ViewerError("That compute device is not available on this machine.")
return selected
class InferenceEngine:
def __init__(self):
self.lock = Lock()
self.checkpoint = CHECKPOINT
self.threads = 2
self.decode_workers = 2
self.timeout = 1800
def predict(self, volume, family, compute, batch_size, full_volume, frame_index):
device = resolve_compute(compute)
frames = list(range(volume.shape[0])) if full_volume else [int(frame_index)]
started = time.monotonic()
volume.cancelled = False
with (
self.lock,
tempfile.TemporaryDirectory(
prefix="inference-", dir=volume.directory
) as directory,
):
work = Path(directory)
job = {
"checkpoint": str(self.checkpoint),
"data_root": str(volume.directory / "data"),
"oct_paths": [path.name for path in volume.paths],
"family": family,
"device": device,
"batch_size": batch_size,
"full_volume": full_volume,
"frame_index": int(frame_index),
"threads": self.threads,
"decode_workers": self.decode_workers,
"output": str(work / "prediction.npz"),
"stop": str(work / "stop"),
}
spec = work / "job.json"
spec.write_text(json.dumps(job))
log_path = work / "worker.log"
with log_path.open("w") as log, log_path.open() as updates:
with subprocess.Popen(
[sys.executable, "-m", "src.viewer", "--job", str(spec)],
cwd=ROOT,
stdout=log,
stderr=subprocess.STDOUT,
start_new_session=os.name == "posix",
) as process:
try:
while True:
if volume.cancelled:
Path(job["stop"]).touch()
for line in updates:
try:
event = json.loads(line)
except json.JSONDecodeError:
continue
if isinstance(event, dict) and "completed" in event:
volume.updated = time.monotonic()
yield event
try:
returncode = process.wait(timeout=0.1)
break
except subprocess.TimeoutExpired:
if time.monotonic() - started > self.timeout:
raise ViewerError(
f"Inference exceeded the {self.timeout}-second time limit."
)
if volume.cancelled:
raise ViewerError("Segmentation cancelled.")
if returncode:
detail = log_path.read_text()[-6000:]
if "out of memory" in detail.lower():
raise ViewerError("GPU out of memory.")
raise ViewerError(
"Inference failed: " + detail.strip().splitlines()[-1]
)
with np.load(job["output"], allow_pickle=False) as arrays:
positions = arrays["positions"].copy()
keys = arrays["boundary_keys"].tolist()
if (
positions.shape != (len(keys), len(frames), volume.shape[2])
or not keys
or not np.isfinite(positions).all()
or positions.min() < 0
or positions.max() > volume.shape[1] - 1
):
raise ViewerError(
"Inference returned invalid prediction geometry."
)
suffix = (
"volume"
if full_volume
else f"preview-bscan-{frames[0] + 1:04d}"
)
output = volume.directory / f"boundaries-{suffix}.npz"
shutil.copyfile(job["output"], output)
prediction = Prediction(
positions,
keys,
frames,
family,
device,
full_volume,
time.monotonic() - started,
output,
)
volume.prediction = prediction
yield {"result": prediction}
finally:
if process.poll() is None:
if os.name == "posix":
os.killpg(process.pid, signal.SIGKILL)
else:
process.kill()
process.wait()
ENGINE = InferenceEngine()
def worker(job):
"""Run the saved pipeline with batch notifications, isolated from Gradio."""
from src.inference import apply_policy
from src.inference.density import predict_bprob
from src.inference.predict import (
ordered_process_prefetch,
predict_prepared,
prepare_pixels,
)
from src.io import enforce_input_opens, resize_frames
from src.models import make_model
torch.set_num_threads(job["threads"])
row = {"case_id": "volume", "device": job["family"], "oct_paths": job["oct_paths"]}
root = Path(job["data_root"])
started = time.monotonic()
if Path(job["checkpoint"]).suffix == ".npz":
from src.profile import load_checkpoint, predict_case
models, cfg, _ = load_checkpoint(job["checkpoint"])
enforce_input_opens(root, [row])
positions, keys = predict_case(row, root, models, cfg, job["batch_size"])
if not job["full_volume"]:
positions = positions[:, job["frame_index"] : job["frame_index"] + 1]
else:
saved = torch.load(job["checkpoint"], map_location="cpu", weights_only=False)
if saved["config"].get("inference_wrapper") == "device_router_v1":
route = saved["config"]["device_routes"][job["family"]]
saved = saved["route_checkpoints"][route]
cfg = saved["config"]
device = torch.device(job["device"])
net = make_model(cfg)
net.load_state_dict(saved["model"])
net.to(device).eval()
audit = enforce_input_opens(root, [row])
selected = None
if job["full_volume"]:
prepared = (
list(ordered_process_prefetch([row], root, cfg, 1, audit))[0][1]
if job["decode_workers"]
else prepare_pixels(row, root, cfg)
)
else:
if cfg["architecture"] not in {
"boundary_ensemble_v1",
"boundary_unet_v1",
"boundary_unet_axial_v1",
"boundary_unet_spatial_v1",
}:
raise ViewerError("Use Full volume for this checkpoint architecture.")
configs = [
cfg,
*[component["config"] for component in cfg.get("components", [])],
]
radius = max(c.get("context_radius", 0) for c in configs)
headers = [
(root / name, structural_header(root / name))
for name in row["oct_paths"]
]
count = sum(int(header.NumberOfFrames) for _, header in headers)
index = job["frame_index"]
lo, hi = max(0, index - radius), min(count, index + radius + 1)
selected = index - lo
frames = []
for path, header in headers:
offset = int(header.get("ConcatenationFrameOffsetNumber", 0))
indices = range(
max(0, lo - offset), min(int(header.NumberOfFrames), hi - offset)
)
if indices:
frames.extend(source_frames(path, indices=list(indices)))
pixels = resize_frames(frames, cfg["height"], cfg["width"], "cpu").to(
torch.float16
)
prepared = [(pixels, int(headers[0][1].Rows), int(headers[0][1].Columns))]
cfg = copy.deepcopy(cfg)
for c in [
cfg,
*[component["config"] for component in cfg.get("components", [])],
]:
if "density_b_sigma" in c:
c["density_b_sigma"] = 0
policy = c.get("prediction_policy", {})
for settings in [policy, *policy.get("by_device", {}).values()]:
settings.pop("median_b", None)
settings.pop("gaussian_b_sigma", None)
def report(completed, total):
if Path(job["stop"]).exists():
raise ViewerError("Segmentation cancelled.")
if selected is not None:
completed, total = int(completed > selected), 1
print(
json.dumps(
{
"completed": completed,
"total": total,
"device": job["device"],
"seconds": time.monotonic() - started,
}
),
flush=True,
)
if Path(job["stop"]).exists():
raise ViewerError("Segmentation cancelled.")
if cfg.get("inference_wrapper") == "b_probability_smooth_v1":
positions, keys = predict_bprob(
row, prepared, net, cfg, device, job["batch_size"], progress=report
)
else:
positions, keys = predict_prepared(
row,
prepared,
net,
cfg,
device,
job["batch_size"],
cfg.get("tta_horizontal", False),
progress=report,
)
positions = apply_policy(positions, job["family"], cfg.get("prediction_policy"))
if selected is not None:
positions = positions[:, selected : selected + 1]
if Path(job["stop"]).exists():
raise ViewerError("Segmentation cancelled.")
height = int(structural_header(root / row["oct_paths"][0]).Rows)
arrays = {
"positions": np.clip(positions, 0, height - 1).astype("float32"),
"boundary_keys": np.asarray(keys),
}
if not job["full_volume"]:
arrays["frame_indices"] = np.asarray([job["frame_index"]], dtype=np.int32)
np.savez_compressed(job["output"], **arrays)
def display_dimensions(volume: Volume, physical: bool) -> tuple[int, int]:
_, h, a = volume.shape
ratio = h / a
if physical and volume.axial_mm and volume.lateral_mm:
ratio *= volume.axial_mm / volume.lateral_mm
width = 1000
height = max(1, round(width * ratio))
scale = min(1.0, 1400 / max(width, height))
return max(1, round(width * scale)), max(1, round(height * scale))
def render(
volume: Volume,
index: int,
selected_keys: list[str] | None = None,
overlay: bool = True,
opacity: float = 0.9,
line_width: int = 2,
labels: bool = False,
contrast: float = 1.0,
brightness: float = 0.0,
physical: bool = True,
) -> tuple[np.ndarray, np.ndarray | None]:
index = max(0, min(volume.shape[0] - 1, int(index)))
image = np.asarray(volume.frame(index), dtype=np.float32)
finite = image[np.isfinite(image)]
lo, hi = np.quantile(finite, [0.01, 0.995]) if finite.size else (0, 1)
gray = np.nan_to_num((image - lo) / max(float(hi - lo), 1e-6))
gray = np.clip((gray - 0.5) * float(contrast) + 0.5 + float(brightness), 0, 1)
width, height = display_dimensions(volume, physical)
base = (
Image.fromarray(np.uint8(np.round(gray * 255)))
.convert("RGB")
.resize((width, height), Image.Resampling.BILINEAR)
)
if volume.headers[0][1].PhotometricInterpretation == "MONOCHROME1":
base = ImageOps.invert(base)
raw = np.asarray(base)
prediction = volume.prediction
if prediction is None or index not in prediction.frames:
return raw, None
if not overlay:
return raw, raw.copy()
frame = prediction.frames.index(index)
selected = set(prediction.keys if selected_keys is None else selected_keys)
layer = base.convert("RGBA")
annotation = Image.new("RGBA", base.size)
draw = ImageDraw.Draw(annotation)
_, native_h, native_a = volume.shape
x = np.linspace(0, width - 1, native_a)
for key, values in zip(prediction.keys, prediction.positions[:, frame]):
if key not in selected:
continue
y = np.clip(values, 0, native_h - 1) * (height - 1) / max(1, native_h - 1)
color = COLORS.get(key, "#ffffff")
rgb = tuple(int(color[i : i + 2], 16) for i in (1, 3, 5))
points = list(zip(x.tolist(), y.tolist()))
if len(points) > 1:
draw.line(
points,
fill=(*rgb, round(255 * float(opacity))),
width=max(1, int(line_width)),
joint="curve",
)
if labels:
text = NAMES.get(key, key)
anchor = max(
0,
min(
height - 14,
round(float(np.median(y[: max(1, native_a // 8)])) - 13),
),
)
box = draw.textbbox((8, anchor), text)
draw.rectangle(
(box[0] - 3, box[1] - 2, box[2] + 3, box[3] + 2), fill=(8, 15, 26, 210)
)
draw.text((8, anchor), text, fill=(*rgb, 255))
return raw, np.asarray(Image.alpha_composite(layer, annotation).convert("RGB"))
def legend(keys: list[str]) -> str:
if not keys:
return "Boundary colours appear after a scan is loaded."
items = []
for key in keys:
items.append(
f'<span style="color:{COLORS.get(key, "#94a3b8")}">●</span> {html.escape(NAMES.get(key, key))}'
)
return " · ".join(items)
def volume_info(volume: Volume, family: str | None) -> str:
b, h, a = volume.shape
scanner = FAMILIES.get(family, "Select scanner family")
spacing = f" · axial {volume.axial_mm * 1000:.2f} µm" if volume.axial_mm else ""
return (
f"**{html.escape(scanner)}** \n{b:,} B-scans · {h:,} × {a:,} pixels{spacing}"
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Isolated OCT viewer inference worker")
parser.add_argument("--job", type=Path, required=True)
worker(json.loads(parser.parse_args().job.read_text()))