Skip to content

Repository files navigation

CTNet for motor imagery EEG classification

CTNet: A Convolutional Transformer Network for EEG-Based Motor Imagery Classification [Paper]

core idea: CNN (an improved version of EEGNet) + Transformer encoder

Our research builds upon and improves the EEG Conformer and EEG-ATCNet, and we sincerely thank the creators of these open-source project.

News

🎉🎉🎉 We've joined in braindecode toolbox. Use here for detailed info. Thanks to Bru and colleagues for helping with the modifications.

Abstract:

Brain-computer interface (BCI) technology bridges the direct communication between the brain and machines, unlocking new possibilities for human interaction and rehabilitation. EEG-based motor imagery (MI) plays a pivotal role in BCI, enabling the translation of thought into actionable commands for interactive and assistive technologies. However, the constrained decoding performance of brain signals poses a limitation to the broader application and development of BCI systems. In this study, we introduce a convolutional Transformer network (CTNet) designed for EEG-based MI classification. Firstly, CTNet employs a convolutional module analogous to EEGNet, dedicated to extracting local and spatial features from EEG time series. Subsequently, it incorporates a Transformer encoder module, leveraging a multi-head attention mechanism to discern the global dependencies of EEG's high-level features. Finally, a straightforward classifier module comprising fully connected layers is followed to categorize EEG signals. In subject-specific evaluations, CTNet achieved remarkable decoding accuracies of 82.52% and 88.49% on the BCI IV-2a and IV-2b datasets, respectively. Furthermore, in the challenging cross-subject assessments, CTNet achieved recognition accuracies of 58.64% on the BCI IV-2a dataset and 76.27% on the BCI IV-2b dataset. In both subject-specific and cross-subject evaluations, CTNet holds a leading position when compared to some of the state-of-the-art methods. This underscores the exceptional efficacy of our approach and its potential to set a new benchmark in EEG decoding.

Overall Framework:

architecture of CTNet

Requirements:

Python 3.10

Pytorch 1.13.1

mne 1.5.1

Datasets

datasets: BCI Competition IV-2a & IV-2b datasets

Create the BCICIV_2a_gdf directory to store the downloaded BCI IV-2a dataset.

Create the BCICIV_2b_gdf directory to store the downloaded BCI IV-2b dataset.

labels: BCI IV-2a, BCI IV-2b

Create the true_labels directory to store the label data of the downloaded BCI IV-2a and 2b datasets.

Preprocessing dataset: Merge data and labels to create a dataloader for training the model (EDF -> MAT).

Run python3 preprocessing_for_2a.py for 2a dataset Run python3 preprocessing_for_2b.py for 2b dataset

Traning

For subject-dependent

run python3 main_subject_specific.py or CTNet_2a_82.91.ipynb

For subject-independent

run main_cross_subject_LOSO.ipynb

Under the LOSO setting, if data augmentation is required, it is recommended to refer to the updated training framework code, in which the data augmentation pipeline has been optimized: specifically, S&R performs data augmentation on each subject in the training set one by one, and then concatenates the data from different subjects to construct the training set. Refer to the LOSO code of TCANet. https://github.com/snailpt/TCANet/blob/main/LOSO_TCANet.ipynb

Experimental Setup:

The original training set was split into training and validation subsets with a ratio of 7:3. Data augmentation was applied to expand the training set to three times (N_AUG=3) its original size.

Note: We observed that increasing the data augmentation factor (N_Aug in our code) leads to improved classification accuracy, but also results in a corresponding increase in training time.

Performance Comparison:

Comparison of Subject-specific classification accuracy (in %) and kappa on the BCI IV-2a dataset.

MethodAverage±Std.Kappa
ShallowConvNet75.69±11.760.6759
DeepConvNet77.78±14.420.7037
EEGNet77.39±12.470.6986
TSF-STAN83.0±11.40.7650
Conformer77.66±13.350.7022
MI-CAT76.81±13.800.6920
CTNet (Proposed)82.52±9.610.7670

Comparison of Subject-specific classification accuracy (in %) and kappa on the BCI IV-2b dataset.

MethodAverage±Std.Kappa
ShallowConvNet85.13±10.740.7026
DeepConvNet85.21±9.560.7042
EEGNet87.71±9.330.7542
TSF-STAN88.0±9.6-
Conformer85.87±10.730.7174
MI-CAT85.28±12.930.7060
CTNet (Proposed)88.49±9.030.7697

Comparison of cross-subject classification accuracy (in %) and kappa on the BCI IV-2a dataset.

MethodA01A02A03A04A05A06A07A08A09Average±Std.Kappa
ShallowConvNet]66.8446.5367.5352.2634.3839.7665.4571.1866.8456.75±13.770.4234
DeepConvNet68.5847.4078.9952.2650.8741.8469.4471.7060.2460.15±12.710.4686
EEGNet69.7942.0179.5150.8735.7637.1565.8067.3663.3756.85±15.820.4246
Conformer68.7537.3369.6243.5829.5135.2458.3374.4863.8953.41±17.080.3789
CTNet (Proposed)69.2743.9279.3455.3843.9236.1165.1070.6664.0658.64±14.610.4486

Comparison of cross-subject classification accuracy (in %) and kappa on the BCI IV-2b dataset.

MethodB01B02B03B04B05B06B07B08B09Average±Std.Kappa
ShallowConvNet74.0363.5359.7282.8482.4380.9774.8672.3777.7874.28±8.130.4856
DeepConvNet74.0365.1563.4780.8182.7074.8681.3976.3277.9275.18±6.840.5037
EEGNet74.4469.2662.3680.4183.2475.5679.8673.5577.5075.13±6.350.5026
Conformer71.3962.3565.2882.9780.4169.3175.0076.3278.6173.52±6.960.4703
CTNet (Proposed)76.2571.0366.3981.7683.1177.2279.1773.5677.9276.27±5.260.5252

Citation

Hope this code can be useful. I would appreciate you citing us in your paper. 😊

Zhao, W., Jiang, X., Zhang, B. et al. CTNet: a convolutional transformer network for EEG-based motor imagery classification. Sci Rep 14, 20237 (2024). https://doi.org/10.1038/s41598-024-71118-7

Communication

QQ discussion group (Motor imagery and Seizure Detection): 837800443

Email: zhaowei701@163.com

Releases

Used by

Contributors

Languages