Training and fine-tuning framework for ECG foundation models.
This repository provides tools for:
- Pretraining ECG foundation models with Fyler-code as supervision
- Fine-tuning pretrained models on downstream tasks
- Evaluation on various ECG-based prediction tasks
# Create environment
conda env create -f environment.yml
conda activate ECG-FM
# Install dependencies
pip install -r requirements.txtsbatch pretrain_fc.sh # Pretraining with Fyler codessbatch finetune_fc.sh # Fine-tune Fyler-code pretrained model
# Other baseline ECG foundation models:
sbatch finetune_ecgfounder.sh # Fine-tune ECGFounder model
sbatch finetune_vitmae.sh # Fine-tune ViT-MAE modelsbatch train_scratch.sh # Train a classification model from scratch (ResNet)The Fyler-code pretrained checkpoint from scripts/pretrain_fc.sh can be loaded
with fyler_embedding.py. This script reconstructs the
pretrained ResNet encoder and returns the feature vector right before the final
classification layer.
For the current pretrained model in ../ECG_Fyler, the expected ECG input shape
is [batch, 12, 2048], and the output embedding dimension is 2560.
Python API:
import torch
from fyler_embedding import load_fyler_encoder
model = load_fyler_encoder("../ECG_Fyler/ECG_Fyler.ckpt", device="cpu")
# ECG shape: [batch, 12, 2048]
ecg = torch.randn(1, 12, 2048)
embedding = model.encode(ecg)
print(embedding.shape) # [1, 2560]Command line:
python fyler_embedding.py \
--checkpoint ../ECG_Fyler/ECG_Fyler.ckpt \
--input your_ecg.npy \
--output your_embedding.npySupported .npy ECG input shapes:
[12, 2048][2048, 12][batch, 12, 2048][batch, 2048, 12]
├── train_model_fc.py # Pretraining and finetuning script
├── train_model_spec.py # Finetuning ViTMAE (Spectrogram-input)
├── data.py # Data utilities
├── preprocessing/ # Data preprocessing tools
├── postprocessing/ # Results postprocessing
├── models/ # Model architectures
├── fyler_embedding.py # Load pretrained checkpoint and extract embeddings