Spaces:
Sleeping
Sleeping
Download src/inference/predict.py from farrell236/OCT_Segmentation_a1: direct link, hf CLI and curl.
- Browser
- Download file 6.47 kB
-
https://huggingface.co/spaces/farrell236/OCT_Segmentation_a1/resolve/main/src/inference/predict.py
- Command line
-
hf download hf://spaces/farrell236/OCT_Segmentation_a1/src/inference/predict.py
-
curl -L -o predict.py https://huggingface.co/spaces/farrell236/OCT_Segmentation_a1/resolve/main/src/inference/predict.py
6.47 kB
| """Fresh-process, label-free OCT boundary inference. Network access is unnecessary.""" | |
| from pathlib import Path | |
| import argparse | |
| import json | |
| import time | |
| import numpy as np | |
| import torch | |
| from torch.nn import functional as F | |
| from src.inference.decoding import positions_from_logits | |
| from src.models import build_model, output_indices | |
| from src.inference.runtime import configure_inference, autocast_options | |
| from src.io import load_manifest, volume_headers | |
| from src.inference.context import iter_normalized_contexts | |
| from src.inference.combination import finish_positions | |
| def predict_case( | |
| row, data_root, model, config, device, batch_size=8, tta=False, precision="fp32" | |
| ): | |
| headers, (b, h, a) = volume_headers(data_root, row["oct_paths"]) | |
| keys = config["device_keys"][row["device"]] | |
| indices = output_indices(config, row["device"]) | |
| values = np.empty((len(keys), b, a), np.float32) | |
| shape = (config["height"], config["width"]) | |
| batch = [] | |
| completed = 0 | |
| def consume(images): | |
| nonlocal completed | |
| x = torch.from_numpy(np.stack(images)).to(device) | |
| with torch.inference_mode(), torch.autocast( | |
| **autocast_options(device, precision) | |
| ): | |
| pred = positions_from_logits(model(x)[:, indices], config, row["device"]) | |
| if tta: | |
| pred = ( | |
| pred | |
| + positions_from_logits( | |
| model(x.flip(-1))[:, indices], config, row["device"] | |
| ).flip(-1) | |
| ) * 0.5 | |
| pred = F.interpolate(pred, size=a, mode="linear", align_corners=False) | |
| pred = (pred + 0.5) * h / shape[0] - 0.5 | |
| pred = pred.clamp(0, h - 1) | |
| arr = pred.float().cpu().numpy().transpose(1, 0, 2) | |
| values[:, completed : completed + len(images)] = arr | |
| completed += len(images) | |
| for images in iter_normalized_contexts( | |
| headers, [(shape[0], shape[1], config.get("input_channels", 1))] | |
| ): | |
| batch.append(images[0]) | |
| if len(batch) == batch_size: | |
| consume(batch) | |
| batch = [] | |
| if batch: | |
| consume(batch) | |
| if completed != b or not np.isfinite(values).all(): | |
| raise ValueError("Incomplete or nonfinite prediction") | |
| return finish_positions( | |
| values, row["device"], h, config.get("inference", {}).get("postprocess", {}) | |
| ), np.asarray(keys) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--input-manifest", required=True) | |
| ap.add_argument("--data-root", required=True) | |
| ap.add_argument("--checkpoint", required=True) | |
| ap.add_argument("--output-dir", required=True) | |
| ap.add_argument("--batch-size", type=int, default=32) | |
| ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu") | |
| ap.add_argument("--tta", action="store_true") | |
| ap.add_argument("--workers", type=int, default=4) | |
| ap.add_argument("--precision", choices=["fp32", "bf16", "tf32"], default=None) | |
| ap.add_argument("--shard-index", type=int, default=0) | |
| ap.add_argument("--num-shards", type=int, default=1) | |
| args = ap.parse_args() | |
| # Checkpoint contains model tensors and simple configuration; never labels, | |
| # participant IDs, paths or a lookup table indexed by acquisition identifiers. | |
| ck = torch.load(args.checkpoint, map_location="cpu", weights_only=True) | |
| config = ck["config"] | |
| args.precision = args.precision or config.get("inference", {}).get( | |
| "precision", "fp32" | |
| ) | |
| configure_inference(args.precision) | |
| if config.get("architecture") == "BoundaryEnsemble": | |
| from src.inference.ensemble import stream_ensemble | |
| rows = [ | |
| {k: r[k] for k in ["case_id", "device", "oct_paths"]} | |
| for r in load_manifest(args.input_manifest) | |
| ] | |
| selected = [ | |
| r for i, r in enumerate(rows) if i % args.num_shards == args.shard_index | |
| ] | |
| out = Path(args.output_dir) | |
| out.mkdir(parents=True, exist_ok=True) | |
| if args.tta: | |
| raise ValueError("TTA must be explicitly validated before ensemble use") | |
| stream_ensemble( | |
| selected, | |
| args.data_root, | |
| ck, | |
| out, | |
| args.device, | |
| args.batch_size, | |
| args.workers, | |
| args.precision, | |
| ) | |
| return | |
| model = build_model(config) | |
| model.load_state_dict(ck["model"]) | |
| model.to(args.device).eval() | |
| rows = load_manifest(args.input_manifest) | |
| # Drop every field except the label-free contract, even if a caller passes more. | |
| rows = [{k: r[k] for k in ["case_id", "device", "oct_paths"]} for r in rows] | |
| out = Path(args.output_dir) | |
| out.mkdir(parents=True, exist_ok=True) | |
| t = time.monotonic() | |
| count = 0 | |
| if args.workers: | |
| from src.inference.stream import stream_predictions | |
| selected = [ | |
| r for i, r in enumerate(rows) if i % args.num_shards == args.shard_index | |
| ] | |
| stream_predictions( | |
| selected, | |
| args.data_root, | |
| [{"model": model, "config": config, "destination": out}], | |
| args.device, | |
| args.batch_size, | |
| args.workers, | |
| args.tta, | |
| args.precision, | |
| ) | |
| return | |
| for i, row in enumerate(rows): | |
| if i % args.num_shards != args.shard_index: | |
| continue | |
| target = out / (row["case_id"] + ".npz") | |
| positions, keys = predict_case( | |
| row, | |
| args.data_root, | |
| model, | |
| config, | |
| args.device, | |
| args.batch_size, | |
| args.tta, | |
| args.precision, | |
| ) | |
| tmp = target.with_suffix(".tmp.npz") | |
| np.savez_compressed(tmp, positions=positions, boundary_keys=keys) | |
| tmp.replace(target) | |
| count += 1 | |
| if count % 10 == 0: | |
| print( | |
| json.dumps( | |
| { | |
| "completed_cases": count, | |
| "elapsed_seconds": time.monotonic() - t, | |
| "shard": args.shard_index, | |
| } | |
| ), | |
| flush=True, | |
| ) | |
| print( | |
| json.dumps( | |
| { | |
| "completed_cases": count, | |
| "elapsed_seconds": time.monotonic() - t, | |
| "shard": args.shard_index, | |
| } | |
| ), | |
| flush=True, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |