An LSTM model that predicts respiratory motion 250 ms ahead to close the latency gap of optical motion tracking in real-time dose calculation for carbon ion therapy.
Bachelor thesis, Medical Engineering, TU Darmstadt (2024) – "AI-based modeling of breathing trajectories in particle therapy"
In particle therapy, the dose is deposited very precisely at the Bragg peak. For lung tumors, breathing moves the target during irradiation. The real-time dose engine 4D-RIDOS receives new motion data from the optical tracking system (OTS) only every 250 ms. This model predicts the next position so the dose calculation can work with an up-to-date motion estimate.
| Metric | Result |
|---|---|
| Mean absolute error, irregular test trajectory | 0.51 mm (without augmentation: 7.23 mm) |
| Mean absolute error, second test trajectory | 0.45 mm |
| Gamma pass rate, dose from predicted motion vs. detector (3%/3mm) | ≥ 96 % for all 9 deliveries |
| Gamma pass rate, dose from predicted vs. real motion | 99.2 % (3%/3mm), 96.6 % (2%/2mm), mean over 9 deliveries |
The dose reconstructed from the predicted motion agreed with the ionization chamber measurements as well as the dose reconstructed from the real motion.
flowchart LR
A[OTS motion trace<br/>sampled every 250 ms] --> B[Last 32 samples<br/>= 8 s window]
B --> C[LSTM<br/>64 units]
C --> D[Dense 128<br/>+ Dropout 0.1]
D --> E[Dense 1<br/>position in 250 ms]
E --> F[4D-RIDOS<br/>real-time dose]
- Pre-study with synthetic data: sin⁴ curves with random amplitude and frequency per cycle, Gaussian noise and linear baseline drift (
lstm_motion/synthetic.py). - Data augmentation: Irregular breathing was under-represented in the training data. Sections of the training curves were modified with positive and negative baseline drift, linearly increasing and decreasing amplitude, and multiplication with a sine (
lstm_motion/augmentation.py). This increased the training set to 3–4 times its original size. - Hyperparameter search with Keras Tuner: 1–4 LSTM layers (32–256 units), up to 3 dense layers (16–128 units), dropout 0–0.5, Adam learning rate 1e-4 to 1e-2, MAE loss, early stopping.
- Offline vs. offline + online: Retraining on the first part of each test trace improved the error only marginally (0.51 → 0.48 mm) at the cost of computation time, so the offline model was used.
- Dosimetric validation: Predicted traces were fed into 4D-RIDOS and compared with ionization chamber array measurements using gamma index analysis.
| Training data | Test 1: MAE / max error | Test 2: MAE / max error |
|---|---|---|
| No augmentation | 7.23 mm / 19.11 mm | 0.77 mm / 15.63 mm |
| Moderate augmentation (2× data) | 4.24 mm / 13.63 mm | 0.52 mm / 15.58 mm |
| Extensive augmentation (3–4× data) | 0.51 mm / 2.44 mm | 0.45 mm / 9.33 mm |
| Extensive + online retraining | 0.48 mm / 2.18 mm | 0.44 mm / 9.35 mm |
The remaining large maximum error on test trajectory 2 comes from a few isolated jumps in the motion trace. On most of the trace the error stays below 2.5 mm.
The motion traces were recorded with an optical tracking system on a breathing motion phantom during carbon ion experiments at CNAO (Centro Nazionale di Adroterapia Oncologica) and were provided by GSI. They are not public and are not included in this repository. The code runs out of the box with the included synthetic data generator (see the demo above).
The recorded traces are not public, so the repository includes a synthetic breathing generator and a demo that reproduces the main effect of the thesis: training the final architecture on regular traces only, and again with augmented copies, then testing both on an irregular trace.
python scripts/demo_synthetic.py| Training data (synthetic) | MAE | Max error |
|---|---|---|
| Regular traces only | 3.46 mm | 22.79 mm |
| With augmentation | 0.45 mm | 5.75 mm |
Synthetic data only. The irregular test trace contains a baseline jump, a growing amplitude and a slow amplitude variation that do not occur in the training traces.
git clone https://github.com/KhaledAbuMustafa/LSTM-Model.git
cd LSTM-Model
pip install -r requirements.txt
python scripts/demo_synthetic.pyTested with Python 3.11, TensorFlow 2.21 and Keras 3.15 (the thesis used Python 3.12, TensorFlow 2.16 and Keras 3.3). The code in this repository is a cleaned-up and tested version of the original thesis code; the augmentation now draws its sections and strengths at random instead of using fixed values.
With your own traces (two columns: time in s, position in mm, sampled every 250 ms; see data/README.md):
# Hyperparameter search (Keras Tuner, Hyperband)
python scripts/tune.py --train data/train_*.txt --augment 3
# Train the final architecture
python scripts/train.py --train data/train_*.txt --augment 3 --out models/lstm.keras
# Predict and evaluate a test trace (optionally with online retraining)
python scripts/predict.py --model models/lstm.keras --trace data/test.txt
python scripts/predict.py --model models/lstm.keras --trace data/test.txt --retrain 30lstm_motion/
__init__.py sampling interval, window size, horizon
data.py loading traces, sliding windows, chronological split
synthetic.py synthetic sin^4 breathing traces with noise and drift
augmentation.py baseline drift, amplitude modulation, sine modulation
model.py final architecture and Keras Tuner search space
evaluate.py prediction, error metrics, plots
scripts/
demo_synthetic.py end-to-end demo without real data
tune.py hyperparameter search
train.py training on your own traces
predict.py evaluation, optional online retraining
data/ your traces (ignored by git)
docs/ figures for this README
- Only 9 phantom traces (about 76 minutes) were available, 7 for training and 2 for testing. Generalization to real patients with different breathing patterns still has to be shown.
- Longer prediction horizons (500 ms, 750 ms) and a real-time test during an experiment are the next logical steps.
- An open question is how far data augmentation from a single trace can replace additional recorded data.
Python · TensorFlow / Keras · Keras Tuner · NumPy · scikit-learn · Matplotlib
Written at the Department of Electrical Engineering and Information Technology, TU Darmstadt (Radiation Therapy Technology). The motion data were provided by GSI Helmholtzzentrum für Schwerionenforschung.
