Why this project matters
Medical-imaging AI is moving toward real-time use: in the operating room, at the bedside, and on embedded devices. In those settings a model has to be fast enough to keep up with the clinician and reliable enough to trust.
This project tackles exactly that engineering problem on a public dataset: take a trained medical-imaging model, deploy it through ONNX, make it fast with TensorRT, and prove the fast version is still correct. The same approach applies to real-time surgical AI, where inference speed and verification both matter.
The problem
A model that scores well in a research notebook is often too slow to use. A single 3D CT scan took about 34 seconds in standard PyTorch on a T4 GPU. In a clinical or embedded setting, speed, size and trustworthiness matter as much as accuracy.
How it was done
- Prepare data. 41 labelled CT scans from the Medical Segmentation Decathlon, with a fixed seeded split (33 train, 8 validation) and a deterministic MONAI preprocessing pipeline.
- Train. A SwinUNETR (a transformer-based 3D segmentation network, 62.2M parameters) trained with gradient checkpointing to fit in T4 memory.
- Export to ONNX. Convert the best checkpoint into ONNX, a portable model format, with a fixed input shape of 4x1x96x96x96.
- Verify the export (parity gate). Run fixed-seed inputs through PyTorch and ONNX Runtime and compare outputs within a tolerance. If they drift too far, the script exits with an error and nothing moves on.
- Compile to TensorRT FP16. NVIDIA TensorRT rewrites the model for one specific GPU: it fuses layers, picks the best kernels, and uses half-precision (FP16) arithmetic where it is safe.
- Benchmark honestly. Time whole-volume sliding-window inference with GPU synchronisation, warm-up runs and repeats, and report mean and p95 latency alongside the Dice score.
Results
| Backend | Mean latency / volume | p95 | Speed-up | Dice |
|---|---|---|---|---|
| PyTorch FP32 | 34.5 s | 59.1 s | 1.00x | 0.5873 |
| PyTorch AMP (mixed precision) | 14.6 s | 24.9 s | 2.36x | 0.5874 |
| TensorRT FP16 | 6.2 s | 10.2 s | 5.58x | 0.5873 |
Precision changes did not change the answer: Dice is the same across all three backends, and ONNX agrees with PyTorch on 99.99997% of voxels.
What this shows
- Deployment engineering: PyTorch to ONNX to TensorRT, end to end.
- Verification mindset: an automated parity gate with per-precision tolerances, instead of trusting an export.
- Honest measurement: synchronised timing, warm-ups, p95, and clear caveats about what each number means.
- Reproducibility: seeded splits, fixed golden inputs, CPU tests and CI.
FAQ
What is TensorRT?
NVIDIA software that rewrites a trained model so it runs as fast as possible on one specific NVIDIA GPU, a bit like compiling code for one machine.
What is FP16 and does it hurt accuracy?
FP16 does arithmetic with fewer digits than FP32. It is faster, and here Dice did not change at all.
Is this a medical device?
No. It is an engineering and research demonstration on public data. It has not been clinically validated.