Case study ยท Medical AI deployment

Making a 3D medical-imaging AI model 5.6x faster, without changing its answers

I took a SwinUNETR model that outlines the spleen in CT scans from research PyTorch code to an optimised NVIDIA TensorRT engine. Then I proved with an automated check that the faster model still gives the same results.

View the code on GitHubHow it was done

5.58xfaster than PyTorch FP32
2.36xfaster than PyTorch AMP
7.6e-5max ONNX vs PyTorch difference
0.5873Dice, identical on all 3 backends

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.

Independent portfolio project. It uses only public data (Medical Segmentation Decathlon, Task09 Spleen) and is not a clinical product or medical device.

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

  1. 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.
  2. Train. A SwinUNETR (a transformer-based 3D segmentation network, 62.2M parameters) trained with gradient checkpointing to fit in T4 memory.
  3. Export to ONNX. Convert the best checkpoint into ONNX, a portable model format, with a fixed input shape of 4x1x96x96x96.
  4. 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.
  5. 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.
  6. 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

NVIDIA Tesla T4, 8 validation volumes x 3 repeats, TensorRT 10.16
BackendMean latency / volumep95Speed-upDice
PyTorch FP3234.5 s59.1 s1.00x0.5873
PyTorch AMP (mixed precision)14.6 s24.9 s2.36x0.5874
TensorRT FP166.2 s10.2 s5.58x0.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.

An honest caveat. This run trained for 20 epochs instead of the configured 150, so the Dice of 0.587 is far below what this architecture can reach (about 0.9 or more on this dataset). The speed-up and the parity results do not depend on training quality, so they are valid, but the accuracy number is not a claim about the model's ceiling.
Training loss and validation Dice curves for SwinUNETR over 20 epochs
Validation Dice was still rising when training stopped, which is why accuracy is low.
Example CT slice from the spleen dataset with the spleen region highlighted
Example CT data from the Medical Segmentation Decathlon spleen task.

What this shows

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.