Skip to content

About

Repository for the SPDTransNet model, a Transformer-based architecture to analyze sequences of SPD matrices without loss of their Riemannian structure, and said model's SP-MHA (or Structure-Preserving Multihead Attention) component.

Resources

Stars

37 stars

Watchers

1 watching

Forks

Repository files navigation

SPDTransNet - A Transformer-based structure-preserving model for the analysis of Symmetric Positive Definite matrices

The SPDTransNet architecture

This repository is the official implementation of the SPDTransNet model, as presented in the paper Structure-Preserving Transformers for Sequences of SPD Matrices by Mathieu Seraphim, Alexis Lechervy, Florian Yger, Luc Brun and Olivier Etard - Conference EUSIPCO 2024, France (available on arXiv).

A previous, non-structure-preserving version of this method was presented in the paper Temporal Sequences of EEG Covariance Matrices for Automated Sleep Stage Scoring with Attention Mechanisms by Mathieu Seraphim, Paul Dequidt, Alexis Lechervy, Florian Yger, Luc Brun and Olivier Etard - Conference CAIP 2023, Cyprus (official Version on Record here - Accepted Manuscript here).
Other references can be found in this repository's main paper.

Important: due to space constraints, some specifics were left out of the paper. To selectively read the relevant sections, please prioritize sections marked with an asterisk, as well as the similarly marked subsections within them.
A spreadsheet with our model's results (as summarized in the paper) can be found here.

For additional information, feel free to contact us.

Brief overview

The SPDTransNet model (see figure above) is designed to adapt Transformers to analyze timeseries of Symmetric Positive Definite (SPD) matrices.
More specifically, it takes as input a sequence of elements, and outputs a classification only for the central element of the sequence (the surrounding elements providing contextual information). Each element described as a (potentially multichannel) sequence of SPD matrices (as seen in the figure), bijectively tokenized to be processed by the model.

A first intra-element step extracting element-wise features, and a second inter-element step to compare said features. Both steps are based on Transformer encoders, utilizing our SP-MHA auto-attention mechanism described below.

Structure-Preserving Multihead Attention (SP-MHA)

The SP-MHA bloc generates $h$ attention maps from projected tokens in the same way as the original Linear MHA, bui it then combines them and applies them to the unaltered tokens (i.e. 1D vectors) in the V(alue) tensor.

The tokens in V only undergo linear combinations weighted by the attention maps, without linear mappings or concatenations. Hence, this auto-attention mechanism does not alter the geometric properties of the inputs, as long as:

  • Said inputs may be described in vector form without loss of structural information,
  • Linear combinations between these tokens do not cause a loss of structural information.

Both of these are true for our tokenized SPD matrices (cf. paper).

Application to EEG Sleep Staging

EEG sleep staging refers to the subdivision of a set of concurrent 1D electrophysiological signals (including EEG) into fixed-length windows called "epochs", which are manually labeled ("scored") with a given sleep stage.
Theoretical justifications for this approach can be found in the paper.

In this repository, we use the data from the Montreal Archive of Sleep Studies' SS3 dataset (MASS-SS3), comprised of 62 healthy subjects and scored using the AASM scoring rules, with 30s epochs and 5 sleep stages: Awake, REM sleep, and the N1, N2 and N3 non-REM sleep stages.

Each epoch is subdivided into 30 1s segments, to capture relevant events of around 1 to 2 seconds in duration. As these events also exhibit distinctive frequential characteristics, we filter the signals to isolate frequency bands.

Our default preprocessing strategy uses 8 EEG signals and 6 distinct frequency bands - giving us, for each epoch, a timeseries of $S$ = 30 SPD matrices of size 9 $\times$ 9 over $C$ = 7 channels after preprocessing (cf. figure).

Our results

# Model MF1 Macro Acc. N1 F1 Valid. metric Token dim. $d(m)$ # Feat. Tokens $t$
1 SPDTransNet, $L=13$ 81.06 $\pm$ 3.49 84.87 $\pm$ 2.47 60.39 $\pm$ 6.77 MF1 351 ($m = 26$) 7
2 SPDTransNet, $L=21$ 81.24 $\pm$ 3.29 84.40 $\pm$ 2.61 60.50 $\pm$ 6.18 MF1 351 ($m = 26$) 10
3 SPDTransNet, $L=29$ 80.83 $\pm$ 3.40 84.29 $\pm$ 2.65 60.35 $\pm$ 6.01 N1 F1 351 ($m = 26$) 5
4 Classic MHA 80.82 $\pm$ 3.40 84.60 $\pm$ 2.95 60.16 $\pm$ 7.20 MF1 351 ($m = 26$) 10
5 DeepSleepNet 78.14 $\pm$ 4.12 80.05 $\pm$ 3.47 53.52 $\pm$ 8.24 N/A N/A N/A
6 IITNet 78.48 $\pm$ 3.15 81.88 $\pm$ 2.89 56.01 $\pm$ 6.54 N/A N/A N/A
7 GraphSleepNet 75.58 $\pm$ 3.75 79.75 $\pm$ 3.41 50.80 $\pm$ 8.06 N/A N/A N/A
8 Dequidt et al. 81.04 $\pm$ 3.26 82.59 $\pm$ 3.45 58.42 $\pm$ 6.09 N/A N/A N/A
9 Seraphim et al. 79.78 $\pm$ 4.56 81.76 $\pm$ 4.61 58.43 $\pm$ 6.41 MF1 Concatenation 1

The reported results for models from the literature differ from those published by the original authors, as we re-trained their models using our methodology.
A more detailed account of our results can be found here.

More information in the paper.

A few caveats

In this repository, the term "epoch" may be used in two contexts:

  • In reference to the model, it corresponds to a training cycle (standard Deep Learning nomenclature).
  • In reference to the data, it corresponds to a period of 30s in the input signal, and the features describing it (standard sleep medicine nomenclature).

This implementation has been build with modularity in mind. However, the approach taken is fairly nonstandard:

  • Folders are organized starting with an underscore, to keep them in a sorted order - this goes against Python coding conventions (marking them as "protected"), but doesn't impact computations,
  • The way classes are instantiated from YAML files (the "Wrapper" classes) is a complete workaround to how the utilized functions were supposed to work.

Consequently, the code remains modular and adaptable, but might be challenging to reuse in another context.

EDIT / IMPORTANT: I recently got contacted by someone who spent way too much time trying to reuse my code in another context. I may have been too subtle in the above sentence, so let me be clear: the structure of this project is an overengineered piece of garbage. It works well enough as a standalone project, but making it compatible with any decent framework would be a nightmare. I beg of you, if you want to reuse my code, please contact me by e-mail using the link at the top of this page. You'll save yourself a world of trouble.

Instructions

The following instructions should be sufficient to reproduce the results presented in the paper. To expand upon it, see the further documentation linked below.
Please note that the MASS dataset is only available upon demand.

IMPORTANT: renaming or moving around files runs the risk of breaking absolutely everything. Besides Python imports, many scripts refer to other repository files through some form of relative pathing, including the present document.
You have been warned.

Python environment

This repository was built with PyTorch 1.11 and PyTorch Lightning 1.9.5.

It has been tested with Python versions 3.8.10 and 3.9.12, with the full list of used libraries available here.
Compatibility with other versions of the presented libraries is not guaranteed.

Due to the way Python imports are managed, almost all Python scripts must be ran from the repository root. The only exceptions are scripts that do not import anything from other repository files.
.sh scripts can be ran from any directory, but must be ran with Bash.

IMPORTANT: this repository does not currently work with versions 2.x and above of PyTorch or PyTorch Lightning!

HPC Computing & Slurm

IMPORTANT: if you are running everything locally, you may ignore this section and any subsequent reference to Slurm.

This repository was built for mono-GPU usage on a remote server managed using Slurm. Example Slurm scripts (with the .sl extension) can be found in the slurm_scripts subfolder of the 6_3_slurm_execution folder.

Unless you are using the French Jean Zay supercomputer, you will have to modify the Slurm scripts before execution.

Should you wish to run the .sl Slurm scripts directly, follow these instructions:

  • Unless you are using Slurm 23.02 or above, make sure that a [ROOT]/log folder exists. If not, create one.
  • Run the scripts using the sbatch command, either from the repository root or by using the option --chdir=[ROOT].

Alternatively, you can use the provided .sh Bash files, which you can run from anywhere, doing one of the following:

  • Run the _bash_run_any_slurm_script.sh script, with the relevant Slurm script's file basename as argument (i.e. bash [PATH TO _6_3_slurm_execution]/_bash_run_any_slurm_script.sh [SLURM SCRIPT BASENAME].sl).
  • Directly run the Bash script associated with the wanted script, i.e. one of the other .sh files here.

This will generate the aforementioned [ROOT]/log folder.

For the rest of this documentation, the link associated with a given "Slurm script" will point to its associated Bash script.

IMPORTANT: do not delete the [ROOT]/log folder until all Slurm jobs have completed!

Preprocessing

  1. Move the MASS SS3 dataset folder into the _2_1_original_datasets directory. It should be named MASS_SS3, and contain all * Base.edf and * PSG.edf files for all 62 subjects - numbered from 01-03-0001 to 01-03-0064. Subjects 43 and 49 should be missing; that is normal.
  2. Directly run the MASS_extraction.py Python script, or the corresponding Slurm script. This should generate the MASS_SS3_extracted folder in the appropriate location, containing 62 .pkl files.
  3. Directly run the SPDFromEEGPreprocessor.py Python script, or the corresponding Slurm script. This should generate the _2_4_preprocessed_data directory (if it doesn't exist), as well as the MASS_SS3_dataset_with_EUSIPCO_signals_config folder folder therein. This folder contains a .pkl file for every epoch in the dataset (59317 in total).

Reproducing our results

  1. Run the model for training using the appropriate BASH script 31 times sequentially, e.g. through the command bash bash_one_run_full_spd_best_mf1_length_21.sh [X], substituting [X] with every integer from 0 to 30; or run the corresponding Slurm script once. This should generate the lightning_logs folder at the project root, as well as a log folder within called version_[X]. This will also create a folder called tmp_vectorized_data (if it doesn't already exist), as well as .zip files that will be automatically deleted if the run terminates without error.
  2. When all training runs have ended, the tmp_vectorized_data folder may be safely deleted.
  3. Directly run the command_line_tester.py Python script, or the corresponding Slurm script.
  4. This should generate the test_logs folder at the project root, which may safely be deleted after the program ends. Same thing for tmp_vectorized_data, which should be re-generated as before.
  5. The output folder should be generated within the _5_1_runs_analysis directory. It should contain the runs_analysis.csv file, containing the computed metrics on all folds and corresponding hyperparameters, as well as the confusion_matrices folder, containing .png images of confusion matrices on the test set. To prevent any overrides, be sure to rename or move these elements before re-attempting step 3.

IMPORTANT: do not run the model training on a fold for which a folder exists in lightning_logs, as this will combine both logs and lead to issues down the line. If you want to keep both log folders, rename the first one or remove it from lightning_logs.

From hyperparameter research to final results

This is similar to the above instructions, with the following changes:

  • Before step 1, generate the [ROOT]/db/database.db file necessary for the hyperparameter research by running the appropriate BASH script (for local execution or on a Slurm server).
  • In step 1, run the hyperparameter research Slurm script, or a local equivalent (not provided here). This should run a total of 50 runs oo fold 11, with a maximum of 5 running at any moment.
  • Once the search is finished, substitute contents of the current hyperparameters configuration file with the contents of the file [ROOT]/lightning_logs/version_[X}/hparams.yaml, with [X] the index of the run yielding the highest validation MF1, as seen in runs_analysis.csv. Be sure to set the hyperparameter save_in_single_file to true.
  • Repeat the steps in the section above.

IMPORTANT: unlike in the above section, temporary data is stored in separate files within the tmp_vectorized_data folder during hyperparameter research, as opposed to a single .zip files. This is way faster, but generates large amounts of files (more than 50000 per simultaneous run). This behavior is controlled by the save_in_single_file hyperparameter mentioned above.
As files systems usually have a limit to the amount of files allowed to exist at one given time, this behavior can be a limiting factor in terms of amount of simultaneous runs possible.
If this aspect causes issues, please refer to the relevant documentation.

Further Documentation*

To further understand our approach and/or expand upon it, please refer to the following documentation files:

Acknowledgements

The first author is supported by the French National Research Agency (ANR) and Region Normandie under grant HAISCoDe.
This work was granted access to the HPC resources of IDRIS under the allocation 2022-AD010613618 made by GENCI, and to the computing resources of CRIANN (Normandy, France).

About

Repository for the SPDTransNet model, a Transformer-based architecture to analyze sequences of SPD matrices without loss of their Riemannian structure, and said model's SP-MHA (or Structure-Preserving Multihead Attention) component.

Resources

Stars

37 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages