Before the Labels: a self-supervised ECG encoder (SimCLR, 70,244 unlabelled ECGs)

A small ECG foundation encoder: it turns any 2.5-second, 100 Hz ECG window (12 leads, or fewer with the missing ones set to 0) into a 256-number embedding. It was pretrained with SimCLR contrastive learning on 70,244 unlabelled 12-lead ECGs from Germany (PTB-XL folds 1–8) and China (Chapman–Shaoxing, Ningbo, CPSC 2018 + extra), without any diagnosis labels.

Use it to train a good ECG classifier from very few labels, or to search for and cluster ECGs by similarity.

⚠️ Research model, not a medical device. Trained and tested on public retrospective data only. Do not use it for diagnosis or treatment decisions.

Code, full report and model card: https://github.com/rigel08/ecg-foundation-model

Does pretraining help? (PTB-XL test fold 10, macro AUROC, mean of 3 seeds, 95% bootstrap CI)

Labels used From scratch This encoder, fine-tuned Gain
1% (171 ECGs) 0.819 0.846 +0.027 [+0.022, +0.033]
10% 0.890 0.897 +0.007 [+0.003, +0.011]
100% 0.929 0.933 +0.004 [+0.002, +0.006]
  • The gain is biggest when labels are scarce, and small with all labels.
  • On an unseen US hospital (Georgia, never seen in pretraining or training): 0.788 vs 0.764 at 1% labels.
  • Lead I only (smartwatch-style), 171 labels: this encoder kept frozen, with a logistic regression on top, scores 0.798. A full network trained from scratch on the same ECGs scores 0.767.

Files

File What it is
encoder.onnx Encoder. Input x (batch, 12, time), output embedding (batch, 256). Matches PyTorch to within 4e-6
encoder.pt PyTorch state_dict for the Encoder class in the GitHub repo (src/models.py)
config.json Lead order, per-lead normalisation, window length, pretraining details
example_ecg.json One real PTB-XL test ECG
example.py Embeds the example ECG with all 12 leads and with Lead I only

Use it

pip install onnxruntime huggingface_hub numpy
python example.py
import json, numpy as np, onnxruntime as ort
from huggingface_hub import hf_hub_download

repo = "rigel08/before-the-labels-ecg-encoder"
cfg = json.load(open(hf_hub_download(repo, "config.json")))
enc = ort.InferenceSession(hf_hub_download(repo, "encoder.onnx"))

ecg = np.array(json.load(open(hf_hub_download(repo, "example_ecg.json")))["x_mV_time_by_lead"], np.float32)  # (1000, 12) mV, 100 Hz
x = (ecg - np.array(cfg["per_lead_mean_mV"])) / np.array(cfg["per_lead_std_mV"])   # per-lead normalisation
windows = np.stack([x[s:s + 250].T for s in range(0, 751, 125)]).astype(np.float32) # (7, 12, 250)
emb = enc.run(None, {"x": windows})[0].mean(0)                                    # (256,) embedding of the whole ECG
# Single-lead ECG? Normalise, then set the other 11 leads to 0 before embedding.

Then train a logistic regression or small MLP on these embeddings with your own labels.

Pretraining

  • SimCLR (NT-Xent loss): two random augmentations of the same window are pulled together and other windows are pushed apart. Augmentations include random crop, scaling, Gaussian noise, baseline wander and lead dropout.
  • Encoder: 7-tap conv stem + 3 residual blocks (width 128), then concat(average, max) pooling (405,760 parameters). 40 epochs on an Apple M2 (8 GB).
  • Every source was harmonised to 100 Hz, millivolts and the standard lead order. Records with more than 2 flat leads were removed.

Limitations

  • Data from two countries only (Germany, China). Validated downstream on PTB-XL and one US dataset (Georgia).
  • Gains at 100% labels are small (+0.004). The value is in low-label settings.
  • Lead I results use Lead I taken from 12-lead recordings, not real smartwatch signals.

Portfolio: https://rigel08.github.io/portfolio-site/ · GitHub: https://github.com/rigel08 · All my models and demos: https://huggingface.co/rigel08

Downloads last month
32
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Spaces using rigel08/before-the-labels-ecg-encoder 2