|
a |
|
b/README.md |
|
|
1 |
# EEG-ATCNet |
|
|
2 |
|
|
|
3 |
[](https://paperswithcode.com/sota/eeg-4-classes-on-bci-competition-iv-2a?p=physics-inform-attention-temporal) |
|
|
4 |
|
|
|
5 |
This repository provides code for the Attention Temporal Convolutional Network [(ATCNet)](https://doi.org/10.1109/TII.2022.3197419) proposed in the paper: [Physics-informed attention temporal convolutional network for EEG-based motor imagery classification](https://doi.org/10.1109/TII.2022.3197419) |
|
|
6 |
|
|
|
7 |
Authors: Hamdi Altaheri, Ghulam Muhammad, Mansour Alsulaiman |
|
|
8 |
|
|
|
9 |
Center of Smart Robotics Research, King Saud University, Saudi Arabia |
|
|
10 |
## |
|
|
11 |
**Updates**: |
|
|
12 |
* The regularization parameters of [ATCNet](https://doi.org/10.1109/TII.2022.3197419) have been modified, resulting in an enhancement in the model's performance and fortifying it against overfitting. |
|
|
13 |
* The current [*main_TrainTest.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/main_TrainTest.py) file, following the training and evaluation method outlined in [Paper 1](https://doi.org/10.1109/TII.2022.3197419) and [paper 2](https://ieeexplore.ieee.org/document/10142002), has been identified as not aligning with industry best practices. In response, we strongly recommend adopting the methodology implemented in the refined [*main_TrainValTest.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/main_TrainValTest.py) file. This updated version splits the data into train/valid/test sets, following the guidelines detailed in this [post](https://braindecode.org/stable/auto_examples/model_building/plot_how_train_test_and_tune.html#) ([Option 2](https://braindecode.org/stable/auto_examples/model_building/plot_how_train_test_and_tune.html#option-2-train-val-test-split:~:text=Number%20of%20samples.%27%3E-,Option%202%3A%20Train%2DVal%2DTest%20Split,-When%20evaluating%20different)). |
|
|
14 |
## |
|
|
15 |
In addition to the proposed [ATCNet](https://doi.org/10.1109/TII.2022.3197419) model, the [*models.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/models.py) file includes the implementation of other related methods, which can be compared with [ATCNet](https://doi.org/10.1109/TII.2022.3197419), including: |
|
|
16 |
* **EEGNet**, [[paper](https://arxiv.org/abs/1611.08024), [original code](https://github.com/vlawhern/arl-eegmodels)] |
|
|
17 |
* **EEG-TCNet**, [[paper](https://arxiv.org/abs/2006.00622), [original code](https://github.com/iis-eth-zurich/eeg-tcnet)] |
|
|
18 |
* **TCNet_Fusion**, [[paper](https://doi.org/10.1016/j.bspc.2021.102826)] |
|
|
19 |
* **MBEEG_SENet**, [[paper](https://doi.org/10.3390/diagnostics12040995)] |
|
|
20 |
* **EEGNeX**, [[paper](https://arxiv.org/abs/2207.12369), [original code](https://github.com/chenxiachan/EEGNeX)] |
|
|
21 |
* **DeepConvNet**, [[paper](https://onlinelibrary.wiley.com/doi/full/10.1002/hbm.23730), [original code](https://github.com/braindecode/braindecode)] |
|
|
22 |
* **ShallowConvNet**, [[paper](https://onlinelibrary.wiley.com/doi/full/10.1002/hbm.23730), [original code](https://github.com/braindecode/braindecode)] |
|
|
23 |
|
|
|
24 |
The following table shows the performance of [ATCNet](https://doi.org/10.1109/TII.2022.3197419) and other reproduced models based on the methodology defined in the [*main_TrainValTest.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/main_TrainValTest.py) file: |
|
|
25 |
|
|
|
26 |
<table> |
|
|
27 |
<tr> |
|
|
28 |
<td rowspan="2">Model</td> |
|
|
29 |
<td rowspan="2">#params</td> |
|
|
30 |
<td colspan="2">BCI Competition IV-2a dataset (<a href="https://www.bbci.de/competition/iv/#dataset2a">BCI 4-2a</a>) </td> |
|
|
31 |
<td colspan="2">High Gamma Dataset (<a href="https://github.com/robintibor/high-gamma-dataset">HGD</a>)<sup>*</sup></td> |
|
|
32 |
</tr> |
|
|
33 |
<tr> |
|
|
34 |
<td>training time (m) <sup>1,2</sup></td> |
|
|
35 |
<td>accuracy (%)</td> |
|
|
36 |
<td>training time (m) <sup>1,2</sup></td> |
|
|
37 |
<td>accuracy (%)</td> |
|
|
38 |
</tr> |
|
|
39 |
<tr> |
|
|
40 |
<td>ATCNet</td> |
|
|
41 |
<td>113,732</td> |
|
|
42 |
<td>13.5</td> |
|
|
43 |
<td>81.10</td> |
|
|
44 |
<td>62.6</td> |
|
|
45 |
<td>92.05</td> |
|
|
46 |
</tr> |
|
|
47 |
<tr> |
|
|
48 |
<td>TCNet_Fusion</td> |
|
|
49 |
<td>17,248</td> |
|
|
50 |
<td>8.8</td> |
|
|
51 |
<td>69.83</td> |
|
|
52 |
<td>65.2</td> |
|
|
53 |
<td>89.73</td> |
|
|
54 |
</tr> |
|
|
55 |
<tr> |
|
|
56 |
<td>EEGTCNet</td> |
|
|
57 |
<td>4,096</td> |
|
|
58 |
<td>7.0</td> |
|
|
59 |
<td>65.36</td> |
|
|
60 |
<td>36.8</td> |
|
|
61 |
<td>87.80</td> |
|
|
62 |
</tr> |
|
|
63 |
<tr> |
|
|
64 |
<td>MBEEG_SENet</td> |
|
|
65 |
<td>10,170</td> |
|
|
66 |
<td>15.2</td> |
|
|
67 |
<td>69.21</td> |
|
|
68 |
<td>104.3</td> |
|
|
69 |
<td>90.13</td> |
|
|
70 |
</tr> |
|
|
71 |
<tr> |
|
|
72 |
<td>EEGNet</td> |
|
|
73 |
<td>2,548</td> |
|
|
74 |
<td>6.3</td> |
|
|
75 |
<td>68.67</td> |
|
|
76 |
<td>36.5</td> |
|
|
77 |
<td>88.25</td> |
|
|
78 |
</tr> |
|
|
79 |
<tr> |
|
|
80 |
<td>DeepConvNet</td> |
|
|
81 |
<td>553,654</td> |
|
|
82 |
<td>7.5</td> |
|
|
83 |
<td>42.78</td> |
|
|
84 |
<td>43.9</td> |
|
|
85 |
<td>87.53</td> |
|
|
86 |
</tr> |
|
|
87 |
<tr> |
|
|
88 |
<td>ShallowConvNet</td> |
|
|
89 |
<td>47,364</td> |
|
|
90 |
<td>8.2</td> |
|
|
91 |
<td>67.48</td> |
|
|
92 |
<td>61.8</td> |
|
|
93 |
<td>87.00</td> |
|
|
94 |
</tr> |
|
|
95 |
</table> |
|
|
96 |
<sup>1 using Nvidia GTX 1080 Ti 12GB </sup><br> |
|
|
97 |
<sup>2 (500 epochs, without early stopping)</sup><br> |
|
|
98 |
<sup>* please note that <a href="https://github.com/robintibor/high-gamma-dataset">HGD</a> is for "executed movements" NOT "motor imagery"</sup> |
|
|
99 |
|
|
|
100 |
## |
|
|
101 |
This repository includes the implementation of the following attention schemes in the [*attention_models.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/attention_models.py) file: |
|
|
102 |
* [Multi-head self-attention (mha)](https://arxiv.org/abs/1706.03762) |
|
|
103 |
* [Multi-head attention with locality self-attention (mhla)](https://arxiv.org/abs/2112.13492v1) |
|
|
104 |
* [Squeeze-and-excitation attention (se)](https://arxiv.org/abs/1709.01507) |
|
|
105 |
* [Convolutional block attention module (cbam)](https://arxiv.org/abs/1807.06521) |
|
|
106 |
|
|
|
107 |
These attention blocks can be called using the *attention_block(net, attention_model)* method in the [*attention_models.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/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'*](https://arxiv.org/abs/1706.03762), [*'mhla'*](https://arxiv.org/abs/2112.13492v1), [*'cbam'*](https://arxiv.org/abs/1807.06521), and [*'se'*](https://arxiv.org/abs/1709.01507). |
|
|
108 |
``` |
|
|
109 |
Example: |
|
|
110 |
input = Input(shape = (10, 100, 1)) |
|
|
111 |
block1 = Conv2D(1, (1, 10))(input) |
|
|
112 |
block2 = attention_block(block1, 'mha') # mha: multi-head self-attention |
|
|
113 |
output = Dense(4, activation="softmax")(Flatten()(block2)) |
|
|
114 |
``` |
|
|
115 |
## |
|
|
116 |
The [*preprocess.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/preprocess.py) file loads and divides the dataset based on two approaches: |
|
|
117 |
1. [Subject-specific (subject-dependent)](https://link.springer.com/article/10.1007/s00521-021-06352-5#Sec9:~:text=Full%20size%20table-,Performance%20evaluation,-For%20the%20MI) approach. In this approach, we used the same training and testing data as the original [BCI-IV-2a](https://www.bbci.de/competition/iv/) competition division, i.e., trials in session 1 for training, and trials in session 2 for testing. |
|
|
118 |
2. [Leave One Subject Out (LOSO)](https://link.springer.com/article/10.1007/s00521-021-06352-5#Sec9:~:text=Full%20size%20table-,Performance%20evaluation,-For%20the%20MI) 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. |
|
|
119 |
|
|
|
120 |
The *get_data()* method in the [*preprocess.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/preprocess.py) file is used to load the dataset and split it into training and testing. This method uses the [subject-specific](https://link.springer.com/article/10.1007/s00521-021-06352-5#Sec9:~:text=Full%20size%20table-,Performance%20evaluation,-For%20the%20MI) approach by default. If you want to use the [subject-independent (LOSO)](https://link.springer.com/article/10.1007/s00521-021-06352-5#Sec9:~:text=Full%20size%20table-,Performance%20evaluation,-For%20the%20MI) approach, set the parameter *LOSO = True*. |
|
|
121 |
|
|
|
122 |
|
|
|
123 |
## About ATCNet |
|
|
124 |
ATCNet is inspired in part by the Vision Transformer ([ViT](https://arxiv.org/abs/2010.11929)). [ATCNet](https://doi.org/10.1109/TII.2022.3197419) differs from [ViT](https://arxiv.org/abs/2010.11929) by the following: |
|
|
125 |
* [ViT](https://arxiv.org/abs/2010.11929) uses single-layer linear projection while [ATCNet](https://doi.org/10.1109/TII.2022.3197419) uses multilayer nonlinear projection, i.e., convolutional projection specifically designed for EEG-based brain signals. |
|
|
126 |
* [ViT](https://arxiv.org/abs/2010.11929) consists of a stack of encoders where the output of the previous encoder is the input of the subsequent. [ATCNet](https://doi.org/10.1109/TII.2022.3197419) consists of parallel encoders and the outputs of all encoders are concatenated. |
|
|
127 |
* The encoder block in [ViT](https://arxiv.org/abs/2010.11929) consists of a multi-head self-attention (MHA) followed by a multilayer perceptron (MLP), while in [ATCNet](https://doi.org/10.1109/TII.2022.3197419) the MHA is followed by a temporal convolutional network (TCN). |
|
|
128 |
* The first encoder in [ViT](https://arxiv.org/abs/2010.11929) receives the entire input sequence, while each encoder in [ATCNet](https://doi.org/10.1109/TII.2022.3197419) receives a shifted window from the input sequence. |
|
|
129 |
|
|
|
130 |
 |
|
|
131 |
|
|
|
132 |
[ATCNet](https://doi.org/10.1109/TII.2022.3197419) model consists of three main blocks: |
|
|
133 |
1. **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. |
|
|
134 |
2. **Attention (AT) block**: highlights the most important information in the temporal sequence using a multi-head self-attention (MHA). |
|
|
135 |
3. **Temporal convolutional (TC) block**: extracts high-level temporal features from the highlighted information using a temporal convolutional layer |
|
|
136 |
* [ATCNet](https://doi.org/10.1109/TII.2022.3197419) model also utilizes the convolutional-based sliding window to augment MI data and boost the performance of MI classification efficiently. |
|
|
137 |
|
|
|
138 |
<p align="center"> |
|
|
139 |
Visualize the transition of data in the ATCNet model. |
|
|
140 |
</p> |
|
|
141 |
<p align="center"> |
|
|
142 |
<img src="https://user-images.githubusercontent.com/25565236/185449791-e8539453-d4fa-41e1-865a-2cf7e91f60ef.png" alt="The components of the proposed ATCNet model" width="700"/> |
|
|
143 |
</p> |
|
|
144 |
|
|
|
145 |
## Development environment |
|
|
146 |
Models were trained and tested by a single GPU, Nvidia [GTX 2070 8GB](https://www.nvidia.com/en-me/geforce/graphics-cards/rtx-2070/) (Driver Version: [512.78](https://www.nvidia.com/download/driverResults.aspx/188599/en-us/), [CUDA 11.3](https://developer.nvidia.com/cuda-11.3.0-download-archive)), using Python 3.7 with [TensorFlow](https://www.tensorflow.org/) framework. [Anaconda 3](https://www.anaconda.com/products/distribution) was used on [Ubuntu 20.04.4 LTS](https://releases.ubuntu.com/20.04/) and [Windows 11](https://www.microsoft.com/en-hk/software-download/windows11). |
|
|
147 |
The following packages are required: |
|
|
148 |
* TensorFlow 2.7 |
|
|
149 |
* matplotlib 3.5 |
|
|
150 |
* NumPy 1.20 |
|
|
151 |
* scikit-learn 1.0 |
|
|
152 |
* SciPy 1.7 |
|
|
153 |
|
|
|
154 |
## Dataset |
|
|
155 |
The [BCI Competition IV-2a](https://www.bbci.de/competition/iv/#dataset2a) dataset needs to be downloaded, and the data path should be set in the 'data_path' variable in the [*main_TrainValTest.py*](https://github.com/Altaheri/EEG-ATCNet/blob/main/main_TrainValTest.py) file. The dataset can be downloaded from [here](http://bnci-horizon-2020.eu/database/data-sets). |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
## References |
|
|
159 |
If you find this work useful in your research, please use the following BibTeX entry for citation |
|
|
160 |
|
|
|
161 |
``` |
|
|
162 |
@article{9852687, |
|
|
163 |
title={Physics-Informed Attention Temporal Convolutional Network for EEG-Based Motor Imagery Classification}, |
|
|
164 |
author={Altaheri, Hamdi and Muhammad, Ghulam and Alsulaiman, Mansour}, |
|
|
165 |
journal={IEEE Transactions on Industrial Informatics}, |
|
|
166 |
year={2023}, |
|
|
167 |
volume={19}, |
|
|
168 |
number={2}, |
|
|
169 |
pages={2249--2258}, |
|
|
170 |
publisher={IEEE}, |
|
|
171 |
doi={10.1109/TII.2022.3197419} |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
@article{10142002, |
|
|
175 |
title={Dynamic convolution with multilevel attention for EEG-based motor imagery decoding}, |
|
|
176 |
author={Altaheri, Hamdi and Muhammad, Ghulam and Alsulaiman, Mansour}, |
|
|
177 |
journal={IEEE Internet of Things Journal}, |
|
|
178 |
year={2023}, |
|
|
179 |
volume={10}, |
|
|
180 |
number={21}, |
|
|
181 |
pages={18579-18588}, |
|
|
182 |
publisher={IEEE}, |
|
|
183 |
doi={10.1109/JIOT.2023.3281911} |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
@article{altaheri2023deep, |
|
|
187 |
title={Deep learning techniques for classification of electroencephalogram (EEG) motor imagery (MI) signals: A review}, |
|
|
188 |
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}, |
|
|
189 |
journal={Neural Computing and Applications}, |
|
|
190 |
year={2023}, |
|
|
191 |
volume={35}, |
|
|
192 |
number={20}, |
|
|
193 |
pages={14681--14722}, |
|
|
194 |
publisher={Springer}, |
|
|
195 |
doi={10.1007/s00521-021-06352-5} |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
``` |