OpenTSLM-LFM3-1.7B-ECG

A 12-lead ECG reader built with OpenTSLM-SoftPrompt on LFM3-1.7B-Base. It reads a raw 10-second, 500 Hz ECG, answers a question about it with a short step-by-step rationale, and ends with an answer from a list of options.

Write-up and interactive demo: nathanrchn.com/post/lfm3-ecg. Training code: github.com/nathanrchn/OpenTSLM.

Usage

# pip install torch transformers chronos-forecasting huggingface_hub wfdb
import sys, wfdb
from huggingface_hub import snapshot_download

path = snapshot_download("nathanrchn/OpenTSLM-LFM3-1.7B-ECG")
sys.path.append(path)
from inference import EcgReader

reader = EcgReader(path)  # CUDA, Apple GPU (MPS) or CPU
# a PTB-XL recording: 10 s, 12 leads, 500 Hz, millivolts
ecg = wfdb.rdrecord("09879_hr", pn_dir="ptb-xl/1.0.3/records500/09000").p_signal
out = reader.ask(
    ecg,
    "Which rhythm-related symptom does this ECG show, "
    "sinus tachycardia or atrial fibrillation?",
    options=["sinus tachycardia", "atrial fibrillation"],
)
print(out["answer"])     # atrial fibrillation
print(out["rationale"])  # The ECG recording shows an irregularly irregular rhythm ...

ecg must be a 10 s, 12-lead recording at 500 Hz in millivolts, shape (5000, 12) or (12, 5000), leads in the order I, II, III, aVR, aVL, aVF, V1–V6. ask also takes an optional clinical_context string. Generation is greedy, up to 368 tokens, about 2–5 s per question on an Apple M4 Max. Tested with torch 2.15, transformers 5.23, chronos-forecasting 2.3.

Model

  • Language model: LFM3-1.7B-Base, fine-tuned with LoRA (rank 16) and merged (llm/).
  • ECG encoder: Chronos-2 (120M), fine-tuned (encoder/). The 12 leads of a recording attend to each other inside the encoder.
  • Projector: pairs of 16-sample patch embeddings are concatenated and projected by an MLP into the language model's embedding space, giving about 157 soft tokens per lead (projector.safetensors).
  • Prompt: text and ECG tokens are interleaved (This is ECG Lead II, it has mean … and std …: <ECG tokens>), as in OpenTSLM. inference.py reproduces the training prompt exactly.

Training followed the OpenTSLM curriculum (TSQA, M4 captioning, HAR, Sleep, then ECG-QA-CoT) on AWS L40S GPUs.

Results

ECG-QA-CoT test set (41,093 questions over 3,207 PTB-XL recordings), scored with the OpenTSLM paper's scorer:

Model Accuracy Macro-F1 (unweighted) Macro-F1 (weighted)
This model, greedy 45.93 44.46 38.88
This model, majority vote of 8 samples 48.37 46.01 40.75
OpenTSLM-Flamingo, Llama-3.2-3B (paper) 46.25 40.25 –
OpenTSLM-Flamingo + Chronos-2, Llama-3.2-1B (paper) 44.01 41.92 –

The paper's scorer computes both an unweighted and a sample-weighted average of the per-template macro-F1, and the paper does not say which one it reports, so both are listed. A baseline that ignores the ECG and answers each question with its most common training answer reaches 44.0% accuracy but only 29.1 unweighted F1, so macro-F1 is the more informative metric.

Limitations

  • Research model, not a medical device. Do not use it for diagnosis.
  • The training rationales were written by GPT-4o, so the model learned to explain a label more than to derive it.
  • The released ECG-QA-CoT prompts contain inaccurate clinical context (patient sex and artifact fields); the model was trained on them and may repeat such claims.
  • Trained and evaluated on PTB-XL only (one device population, 10 s recordings at 500 Hz).

License

The language-model weights are a derivative of LFM3-1.7B-Base and are distributed under the LFM Open License v1.0. The files in llm/ were modified from the original (fine-tuned weights). The encoder in encoder/ is a fine-tuned derivative of amazon/chronos-2 (Apache-2.0). Training data: ECG-QA / ECG-QA-CoT on PTB-XL (CC BY 4.0).

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for nathanrchn/OpenTSLM-LFM3-1.7B-ECG

Finetuned
(32)
this model