Skip to content

Commit 8273f27

Browse files
committedDec 15, 2024
Initial commit: mPCG Wav2Vec heart-sound classification and synthetic augmentation
Code for "Scaling to Multimodal and Multichannel Heart Sound Classification with Synthetic and Augmented Biosignals" (Marocchi et al. 2025, arXiv:2509.11606): augmentation-supported fine-tuning of a pre-trained Wav2Vec 2.0 encoder across single-channel PCG, synchronised PCG+ECG, and multichannel vest settings. The augmentation and generative synthetic-signal approach (WaveGrad, DiffWave) is inspired by Abbott et al. 2025 (arXiv:2410.10125). Includes NumPy and batched GPU-capable tensor signal processing/augmentation, DiffWave and WaveGrad generators, classification and ablation runners (single-PCG, PCG+ECG two-branch, vest, synthetic-schedule, leave-source-DB-out), split-CSV generation, results aggregation, a CLI, reproduction docs, and tests.
0 parents  commit 8273f27

78 files changed

Lines changed: 8749 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
 

‎.gitignore‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*.egg-info/
5+
.venv/
6+
build/
7+
dist/
8+
9+
# Torch / experiment artefacts
10+
*.pt
11+
*.ckpt
12+
runs/
13+
modelout/
14+
logs/
15+
generated_data/
16+
lightning_logs/
17+
splits/
18+
results/
19+
tables/
20+
generated/
21+
22+
# OS / editor
23+
.DS_Store
24+
.idea/
25+
.vscode/

‎CITATION.cff‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
cff-version: 1.2.0
2+
message: "If you use this software, please cite the papers below."
3+
title: >-
4+
Scaling to Multimodal and Multichannel Heart Sound Classification with Synthetic and
5+
Augmented Biosignals
6+
authors:
7+
- family-names: Marocchi
8+
given-names: Milan
9+
- family-names: Fynn
10+
given-names: Matthew
11+
- family-names: Mandana
12+
given-names: Kayapanda
13+
- family-names: Rong
14+
given-names: Yue
15+
year: 2025
16+
identifiers:
17+
- type: other
18+
value: "arXiv:2509.11606"
19+
url: "https://arxiv.org/abs/2509.11606"
20+
references:
21+
- type: article
22+
title: >-
23+
Generative Deep Learning and Signal Processing for Data Augmentation of Cardiac
24+
Auscultation Signals: Improving Model Robustness Using Synthetic Audio
25+
authors:
26+
- family-names: Abbott
27+
given-names: Leigh
28+
- family-names: Marocchi
29+
given-names: Milan
30+
- family-names: Fynn
31+
given-names: Matthew
32+
- family-names: Rong
33+
given-names: Yue
34+
- family-names: Nordholm
35+
given-names: Sven
36+
year: 2025
37+
identifiers:
38+
- type: other
39+
value: "arXiv:2410.10125"
40+
url: "https://arxiv.org/abs/2410.10125"

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Milan Marocchi and contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# mPCG Wav2Vec
2+
3+
Code for augmentation-supported fine-tuning of a pre-trained Wav2Vec 2.0 encoder for abnormal
4+
heart-sound classification, with synthetic data generated by denoising-diffusion models
5+
(WaveGrad and DiffWave). This is the code for:
6+
7+
> M. Marocchi, M. Fynn, K. Mandana, Y. Rong. *Scaling to Multimodal and Multichannel Heart Sound
8+
> Classification with Synthetic and Augmented Biosignals.* arXiv:2509.11606, 2025.
9+
> https://arxiv.org/abs/2509.11606
10+
11+
The augmentation and generative synthetic-signal approach builds on and is inspired by:
12+
13+
> L. Abbott, M. Marocchi, M. Fynn, Y. Rong, S. Nordholm. *Generative Deep Learning and Signal
14+
> Processing for Data Augmentation of Cardiac Auscultation Signals: Improving Model Robustness
15+
> Using Synthetic Audio.* arXiv:2410.10125, 2025. https://arxiv.org/abs/2410.10125
16+
17+
## Overview
18+
19+
The pipeline covers three classification settings and one synthetic-generation stage:
20+
21+
| Setting | Data | Model |
22+
|---|---|---|
23+
| Single-channel PCG | CinC 2016 (16 kHz) | Wav2Vec 2.0 + MLP head |
24+
| PCG + ECG | Training-A (4.125 kHz) | two-branch fusion (`big_rnn:2:wav2vec`) |
25+
| Multichannel PCG | wearable vest (4.125 kHz) | Wav2Vec 2.0 + time-varying sinc beamformer, optional LoRA |
26+
| Synthetic generation | CinC / vest | DiffWave and WaveGrad (mel- and label-conditioned) |
27+
28+
Signal processing, augmentation and preprocessing are available in both a NumPy form and a
29+
batched, GPU-capable tensor form (`mpcg_wav2vec.signalproc.torchproc`,
30+
`mpcg_wav2vec.augment.torchaug`) that operate on whole batches at once.
31+
32+
## Layout
33+
34+
```
35+
src/mpcg_wav2vec/
36+
signalproc/ filtering, normalisation, Schmidt despiking, resampling, mel-spectrograms,
37+
segmentation; batched tensor equivalents in torchproc.py
38+
augment/ HPSS, noise, time/amplitude warp, parametric EQ, baseline wander (+ real-noise
39+
sources); batched tensor augmentation in torchaug.py
40+
datasets/ CinC / vest / generative / generated loaders, label vocabularies, schedules
41+
generative/ DiffWave, WaveGrad, diffusion schedules, samplers, trainer, generation
42+
classify/ Wav2Vec classifier, two-branch fusion, beamformer, metrics, trainer, evaluation, SVM
43+
experiments/ runners for the three settings + synthetic-schedule and leave-source-DB-out
44+
cli.py command-line entry point
45+
tests/ unit, smoke and numerical-equivalence tests
46+
docs/ REPRODUCE.md (all experiments/ablations) and DATA.md (data acquisition)
47+
scripts/ run_generators.sh, run_ablations.sh
48+
```
49+
50+
## Install
51+
52+
```bash
53+
uv venv && uv sync # or: pip install -e .
54+
```
55+
56+
The Wav2Vec 2.0 encoder (`facebook/wav2vec2-base-960h`) is downloaded from Hugging Face on first
57+
use; set `HF_HUB_OFFLINE=1` once it is cached.
58+
59+
## Quick start
60+
61+
```bash
62+
# Generate a patient-level, label-stratified split CSV from CinC REFERENCE.csv labels
63+
mpcg-wav2vec make-splits --data-dir <cinc>/training-a --out splits/training-a.csv --folds 5
64+
65+
# Train a generator, then synthesise an augmentation dataset
66+
mpcg-wav2vec gen-train --model diffwave --data-dir <cinc> --csv <ref.csv> --output-dir modelout/diffwave
67+
mpcg-wav2vec gen-sample --model diffwave --weights modelout/diffwave/weights.pt \
68+
--data-dir <cinc> --csv <ref.csv> --output-dir generated/diffwave --per-item 3
69+
70+
# Classification
71+
mpcg-wav2vec classify-cinc --data-dir <cinc> --csv <ref.csv> --mode pcg --fs 16000
72+
mpcg-wav2vec classify-cinc --data-dir <traina> --csv <ref.csv> --mode pcg_ecg --fs 4125
73+
mpcg-wav2vec classify-vest --data-dir <vest> --csv <ref.csv> --channels 1,2,3,4,5,6 --lora
74+
```
75+
76+
Every command accepts `--max-batches` (classification) / `--max-train-batches` (generation) for
77+
quick smoke runs. See **[docs/REPRODUCE.md](docs/REPRODUCE.md)** for the full experiment and
78+
ablation matrix, and **[docs/DATA.md](docs/DATA.md)** for data acquisition and the expected
79+
on-disk layout.
80+
81+
**Monitoring & options.** Pass `--logdir runs/<name>` to any train/classify command to log
82+
metrics (and, for generators, periodic generated audio + mel images) to TensorBoard
83+
(`tensorboard --logdir runs`). Classification uses cross-entropy by default; the vest command
84+
also accepts `--loss contrastive-focal` (supervised-contrastive + cross-entropy + center loss).
85+
Generator training rearranges heart cycles by default when `--segment-dir` is given
86+
(`--no-rearrange` to disable); generation never rearranges.
87+
88+
## Data layout
89+
90+
* Records readable by `wfdb` at `<data-dir>/<patient>` (CinC/Training-A: channel 0 = PCG,
91+
channel 1 = ECG; vest: one channel per microphone).
92+
* A reference CSV with a `patient` column, a binary label column (`abnormality`/`label`), and
93+
per-fold split columns (`split`, `split2`, …) valued `train`/`valid`/`test`.
94+
95+
## Preprocessing
96+
97+
Following the paper, PCG is resampled, Schmidt spike-removed, band-limited (25–450 Hz) and
98+
abs-max normalised; ECG is resampled, band-limited (2–40 Hz) and abs-max normalised. Recordings
99+
are segmented into overlapping windows (4 s for CinC/Training-A, 2 s for vest; 0.25 s overlap;
100+
first 0.3 s discarded). Classification runs at 16 kHz for CinC and 4.125 kHz for Training-A and
101+
the vest data.
102+
103+
## Tests
104+
105+
```bash
106+
uv run --with pytest pytest
107+
```
108+
109+
The suite covers the signal processing, augmentation, generators, datasets/schedules and
110+
classifier, and checks that the NumPy and batched tensor preprocessing paths agree.
111+
112+
## Citation
113+
114+
If you use this code, please cite the Marocchi et al. 2025 paper (this work), and the
115+
Abbott et al. 2025 paper for the augmentation and synthetic-signal methods it builds on. See
116+
[CITATION.cff](CITATION.cff) for both entries.

0 commit comments

Comments
 (0)
Please sign in to comment.