This repository provides code for the Attention Temporal Convolutional Network (ATCNet) proposed in:
Physics-informed attention temporal convolutional network for EEG-based motor imagery classification
https://ieeexplore.ieee.org/document/9852687
🔄 PyTorch & new repository (2025-09-26)
A PyTorch implementation is available in our new repository: Altaheri/TCFormer.
Thanks to Martin Wimpff for the PyTorch implementation contributions.
- This repository (EEG-ATCNet) contains the original TensorFlow code.
- A PyTorch version of ATCNet is also available via Braindecode.
Many thanks to the Braindecode team for implementing ATCNet in PyTorch and for maintaining high-quality EEG tooling.
- The regularization parameters of ATCNet have been updated to improve performance and robustness to overfitting.
- The current
main_TrainTest.py, which follows the methodology in Paper 1 and Paper 2, is not aligned with best practice.
Please use the refinedmain_TrainValTest.py, which adopts a Train–Val–Test split as recommended in this guide:
https://braindecode.org/stable/auto_examples/model_building/plot_how_train_test_and_tune.html# (see Option 2: Train–Val–Test Split).
In addition to the proposed ATCNet model, the models.py file includes the implementation of other related methods, which can be compared with ATCNet, including:
- EEGNet, [paper, original code]
- EEG-TCNet, [paper, original code]
- TCNet_Fusion, [paper]
- MBEEG_SENet, [paper]
- EEGNeX, [paper, original code]
- DeepConvNet, [paper, original code]
- ShallowConvNet, [paper, original code]
The following table shows the performance of ATCNet and other reproduced models based on the methodology defined in the main_TrainValTest.py file:
| Model | #params | BCI Competition IV-2a dataset (BCI 4-2a) | High Gamma Dataset (HGD)* | ||
| training time (m) 1,2 | accuracy (%) | training time (m) 1,2 | accuracy (%) | ||
| ATCNet | 113,732 | 13.5 | 81.10 | 62.6 | 92.05 |
| TCNet_Fusion | 17,248 | 8.8 | 69.83 | 65.2 | 89.73 |
| EEGTCNet | 4,096 | 7.0 | 65.36 | 36.8 | 87.80 |
| MBEEG_SENet | 10,170 | 15.2 | 69.21 | 104.3 | 90.13 |
| EEGNet | 2,548 | 6.3 | 68.67 | 36.5 | 88.25 |
| DeepConvNet | 553,654 | 7.5 | 42.78 | 43.9 | 87.53 |
| ShallowConvNet | 47,364 | 8.2 | 67.48 | 61.8 | 87.00 |
2 (500 epochs, without early stopping)
* please note that HGD is for "executed movements" NOT "motor imagery"
This repository includes the implementation of the following attention schemes in the attention_models.py file:
- Multi-head self-attention (mha)
- Multi-head attention with locality self-attention (mhla)
- Squeeze-and-excitation attention (se)
- Convolutional block attention module (cbam)
These attention blocks can be called using the attention_block(net, attention_model) method in the attention_models.py file, where 'net' is the input layer and 'attention_model' indicates the type of the attention mechanism, which has five options: None, 'mha', 'mhla', 'cbam', and 'se'.
Example:
input = Input(shape = (10, 100, 1))
block1 = Conv2D(1, (1, 10))(input)
block2 = attention_block(block1, 'mha') # mha: multi-head self-attention
output = Dense(4, activation="softmax")(Flatten()(block2))
The preprocess.py file loads and divides the dataset based on two approaches:
- Subject-specific (subject-dependent) approach. In this approach, we used the same training and testing data as the original BCI-IV-2a competition division, i.e., trials in session 1 for training, and trials in session 2 for testing.
- Leave One Subject Out (LOSO) approach. LOSO is used for Subject-independent evaluation. In LOSO, the model is trained and evaluated by several folds, equal to the number of subjects, and for each fold, one subject is used for evaluation and the others for training. The LOSO evaluation technique ensures that separate subjects (not visible in the training data) are used to evaluate the model.
The get_data() method in the preprocess.py file is used to load the dataset and split it into training and testing. This method uses the subject-specific approach by default. If you want to use the subject-independent (LOSO) approach, set the parameter LOSO = True.
ATCNet is inspired in part by the Vision Transformer (ViT). ATCNet differs from ViT by the following:
- ViT uses single-layer linear projection while ATCNet uses multilayer nonlinear projection, i.e., convolutional projection specifically designed for EEG-based brain signals.
- ViT consists of a stack of encoders where the output of the previous encoder is the input of the subsequent. ATCNet consists of parallel encoders and the outputs of all encoders are concatenated.
- The encoder block in ViT consists of a multi-head self-attention (MHA) followed by a multilayer perceptron (MLP), while in ATCNet the MHA is followed by a temporal convolutional network (TCN).
- The first encoder in ViT receives the entire input sequence, while each encoder in ATCNet receives a shifted window from the input sequence.
ATCNet model consists of three main blocks:
- Convolutional (CV) block: encodes low-level spatio-temporal information within the MI-EEG signal into a sequence of high-level temporal representations through three convolutional layers.
- Attention (AT) block: highlights the most important information in the temporal sequence using a multi-head self-attention (MHA).
- Temporal convolutional (TC) block: extracts high-level temporal features from the highlighted information using a temporal convolutional layer
- ATCNet model also utilizes the convolutional-based sliding window to augment MI data and boost the performance of MI classification efficiently.
Visualize the transition of data in the ATCNet model.
Models were trained and tested by a single GPU, Nvidia GTX 2070 8GB (Driver Version: 512.78, CUDA 11.3), using Python 3.7 with TensorFlow framework. Anaconda 3 was used on Ubuntu 20.04.4 LTS and Windows 11. The following packages are required:
- TensorFlow 2.7
- matplotlib 3.5
- NumPy 1.20
- scikit-learn 1.0
- SciPy 1.7
The BCI Competition IV-2a dataset needs to be downloaded, and the data path should be set in the 'data_path' variable in the main_TrainValTest.py file. The dataset can be downloaded from here.
If you find this work useful in your research, please use the following BibTeX entry for citation
@article{9852687,
title={Physics-Informed Attention Temporal Convolutional Network for EEG-Based Motor Imagery Classification},
author={Altaheri, Hamdi and Muhammad, Ghulam and Alsulaiman, Mansour},
journal={IEEE Transactions on Industrial Informatics},
year={2023},
volume={19},
number={2},
pages={2249--2258},
publisher={IEEE},
doi={10.1109/TII.2022.3197419}
}
@article{10142002,
title={Dynamic convolution with multilevel attention for EEG-based motor imagery decoding},
author={Altaheri, Hamdi and Muhammad, Ghulam and Alsulaiman, Mansour},
journal={IEEE Internet of Things Journal},
year={2023},
volume={10},
number={21},
pages={18579-18588},
publisher={IEEE},
doi={10.1109/JIOT.2023.3281911}
}
@article{altaheri2023deep,
title={Deep learning techniques for classification of electroencephalogram (EEG) motor imagery (MI) signals: A review},
author={Altaheri, Hamdi and Muhammad, Ghulam and Alsulaiman, Mansour and Amin, Syed Umar and Altuwaijri, Ghadir Ali and Abdul, Wadood and Bencherif, Mohamed A and Faisal, Mohammed},
journal={Neural Computing and Applications},
year={2023},
volume={35},
number={20},
pages={14681--14722},
publisher={Springer},
doi={10.1007/s00521-021-06352-5}
}