Skip to content

A Deep Learning library for EEG Tasks (Signals) Classification, based on TensorFlow.

License

Notifications You must be signed in to change notification settings

YanYu96/EEG-DL

 
 

Repository files navigation



Chat on Gitter Python Version TensorFlow Version MIT License


Welcome to EEG Deep Learning Library

EEG-DL is a Deep Learning (DL) library written by TensorFlow for EEG Tasks (Signals) Classification. It provides the latest DL algorithms and keeps updated.

Table of Contents

Documentation

The supported models include

No. Model Codes
1 Deep Neural Networks DNN
2 Convolutional Neural Networks [Paper] [Tutorial] CNN
3 Deep Residual Convolutional Neural Networks [Paper] ResNet
4 Thin Residual Convolutional Neural Networks [Paper] Thin ResNet
5 Densely Connected Convolutional Neural Networks [Paper] DenseNet
6 Fully Convolutional Neural Networks [Paper] FCN
7 One Shot Learning with Siamese Networks (CNNs Backbone)
[Paper] [Tutorial]
Siamese Networks
8 Graph Convolutional Neural Networks
[Paper] [Presentation] [Tutorial]
[GCN / GNN Summary for Chinese Readers]
[GNN-related Algorithms Review for Chinese Readers]
[Literature of Deep Learning for Graphs]
GCN / Graph CNN
9 Graph Convolutional Neural Networks
(Pure Python Implementation from Reza Amini)
GCN / Graph CNN
10 Deep Residual Graph Convolutional Neural Networks [Paper] ResGCN
11 Densely Connected Graph Convolutional Neural Networks DenseGCN
12 Bayesian Convolutional Neural Network
via Variational Inference
[Paper] [Thesis]
(PyTorch Implementation by Kumar Shridhar)
[Latest Codes]
Bayesian CNNs
13 Recurrent Neural Networks [Paper] RNN
14 Attention-based Recurrent Neural Networks [Paper] RNN with Attention
15 Bidirectional Recurrent Neural Networks [Paper] BiRNN
16 Attention-based Bidirectional Recurrent Neural Networks [Paper] BiRNN with Attention
17 Long-short Term Memory [Paper] LSTM
18 Attention-based Long-short Term Memory [Paper] LSTM with Attention
19 Bidirectional Long-short Term Memory [Paper] BiLSTM
20 Attention-based Bidirectional Long-short Term Memory [Paper] BiLSTM with Attention
21 Gated Recurrent Unit [Paper] GRU
22 Attention-based Gated Recurrent Unit [Paper] GRU with Attention
23 Bidirectional Gated Recurrent Unit [Paper] BiGRU
24 Attention-based Bidirectional Gated Recurrent Unit [Paper] BiGRU with Attention
25 Attention-based BiLSTM + GCN [Paper] Attention-based BiLSTM
GCN
26 Transformer [Paper] [Paper] Transformer

One EEG Motor Imagery (MI) benchmark is currently supported. Other benchmarks in the field of EEG or BCI can be found here.

No. Dataset Tutorial
1 EEG Motor Movement/Imagery Dataset Tutorial

The evaluation criteria consists of

Evaluation Metrics Tutorial
Confusion Matrix Tutorial
Accuracy / Precision / Recall / F1 Score / Kappa Coefficient Tutorial
Receiver Operating Characteristic (ROC) Curve / Area under the Curve (AUC) -
Paired-wise t-test via R language Tutorial

The evaluation metrics are mainly supported for four-class classification. If you wish to switch to two-class or three-class classification, please modify this file to adapt to your personal Dataset classes. Meanwhile, the details about the evaluation metrics can be found in this paper.

Usage Demo

  1. (Under Any Python Environment) Download the EEG Motor Movement/Imagery Dataset via this script.

    $ python MIND_Get_EDF.py
    
  2. (Under Python 2.7 Environment) Read the .edf files (One of the raw EEG signals formats) and save them into Matlab .m files via this script. FYI, this script must be executed under the Python 2 environment (Python 2.7 is recommended) due to some Python 2 syntax. If using Python 3 environment to run the file, there might be no error, but the labels of EEG tasks would be totally messed up.

    $ python Extract-Raw-Data-Into-Matlab-Files.py
    
  3. Preprocessed the Dataset via the Matlab and save the data into the Excel files (training_set, training_label, test_set, and test_label) via these scripts with regards to different models. FYI, every lines of the Excel file is a sample, and the columns can be regarded as features, e.g., 4096 columns mean 64 channels X 64 time points. Later, the models will reshape 4096 columns into a Matrix with the shape 64 channels X 64 time points. You should can change the number of columns to fit your own needs, e.g., the real dimension of your own Dataset.

  4. (Prerequsites) Train and test deep learning models under the Python 3.6 Environment (Highly Recommended) for EEG signals / tasks classification via the EEG-DL library, which provides multiple SOTA DL models.

    Python Version: Python 3.6 (Recommended)
    TensorFlow Version: TensorFlow 1.13.1
    

    Use the below command to install TensorFlow GPU Version 1.13.1:

    $ pip install --upgrade --force-reinstall tensorflow-gpu==1.13.1 --user
  5. Read evaluation criterias (through iterations) via the Tensorboard. You can follow this tutorial. When you finished training the model, you will find the "events.out.tfevents.***" in the folder, e.g., "/Users/shuyuej/Desktop/trained_model/". You can use the following command in your terminal:

    $ tensorboard --logdir="/Users/shuyuej/Desktop/trained_model/" --host=127.0.0.1

    You can open the website in the Google Chrome (Highly Recommended).

    http://127.0.0.1:6006/

    Then you can read and save the criterias into Excel .csv files.

  6. Finally, draw beautiful paper photograph using Matlab or Python. Please follow these scripts.

Notice

  1. I have tested all the files (Python and Matlab) under the macOS. Be advised that for some Matlab files, several Matlab functions are different between Windows Operating System (OS) and macOS. For example, I used "readmatrix" function to read CSV files in the MacOS. However, I have to use “csvread” function in the Windows because there was no such "readmatrix" Matlab function in the Windows. If you have met similar problems, I recommend you to Google or Baidu them. You can definitely work them out.

  2. For the GCNs-Net (GCN Model), for the graph Convolutional layer, the dimensionality of the graph will be unchanged, and for the max-pooling layer, the dimensionality of the graph will be reduced by 2. That means, if you have N X N graph Laplacian, after the max-pooling layer, the dimension will be N/2 X N/2. If you have a 15-channel EEG system, it cannot use max-pooling unless you selected 14 --> 7 or 12 --> 6 --> 3 or 10 --> 5 or 8 --> 4 --> 2 --> 1, etc. The details can be reviewed from this paper.

  3. The Loss Function can be changed or modified from this file.

  4. The Dataset Loader can be changed or modified from this file.

Research Ideas

  1. Dynamic Graph Convolutional Neural Networks [Paper Survey] [Paper Reading]

  2. Neural Architecture Search / AutoML (Automatic Machine Learning) [Tsinghua AutoGraph]

  3. Reinforcement Learning Algorithms (e.g., Deep Q-Learning) [Tsinghua Tianshou] [Doc for Chinese Readers]

  4. Bayesian Convolutional Neural Networks [Paper] [Thesis] [Codes]

  5. Transformer Model [Paper Collections] [Codes]

Common Issues

  1. ValueError: Cannot feed value of shape (1024, 1) for Tensor 'input/label:0', which has shape '(1024,)'

    To solve this issue, you have to squeeze the shape of the labels from (1024, 1) to (1024,) using np.squeeze. Please edit the DataLoader.py file. From original codes:

    train_labels = pd.read_csv(DIR + 'training_label.csv', header=None)
    train_labels = np.array(train_labels).astype('float32')
    
    test_labels = pd.read_csv(DIR + 'test_label.csv', header=None)
    test_labels = np.array(test_labels).astype('float32')

    to

    train_labels = pd.read_csv(DIR + 'training_label.csv', header=None)
    train_labels = np.array(train_labels).astype('float32')
    train_labels = np.squeeze(train_labels)
    
    test_labels = pd.read_csv(DIR + 'test_label.csv', header=None)
    test_labels = np.array(test_labels).astype('float32')
    test_labels = np.squeeze(test_labels)
  2. InvalidArgumentError: Nan in summary histogram for training/logits/bias/gradients

    To solve this issue, you have to comment all the histogram summary. Please edit the GCN_Model.py file.

    # Comment the above tf.summary.histogram from the GCN_Model.py File
    
    # # Histograms.
    # for grad, var in grads:
    #     if grad is None:
    #         print('warning: {} has no gradient'.format(var.op.name))
    #     else:
    #         tf.summary.histogram(var.op.name + '/gradients', grad)
    
    def _weight_variable(self, shape, regularization=True):
        initial = tf.truncated_normal_initializer(0, 0.1)
        var = tf.get_variable('weights', shape, tf.float32, initializer=initial)
        if regularization:
            self.regularizers.append(tf.nn.l2_loss(var))
        # tf.summary.histogram(var.op.name, var)
        return var
    
    def _bias_variable(self, shape, regularization=True):
        initial = tf.constant_initializer(0.1)
        var = tf.get_variable('bias', shape, tf.float32, initializer=initial)
        if regularization:
            self.regularizers.append(tf.nn.l2_loss(var))
        # tf.summary.histogram(var.op.name, var)
        return var
  3. TypeError: len() of unsized object

    To solve this issue, you have to change the coarsen level to your own needs, and you can definitely change it to see the difference. Please edit the main-GCN.py file. For example, if you want to implement the GCNs-Net to a 10-channel EEG system, you have to set "levels" equal to 1 or 0 because there is at most only one max-pooling (10 --> 5). And you can change argument "level" to 1 or 0 to see the difference.

    # This is the coarsen levels, you can definitely change the level to observe the difference
    graphs, perm = coarsening.coarsen(Adjacency_Matrix, levels=5, self_connections=False)

    to

    # This is the coarsen levels, you can definitely change the level to observe the difference
    graphs, perm = coarsening.coarsen(Adjacency_Matrix, levels=1, self_connections=False)
  4. tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 7 which is outside the valid range of [0, 7). Label values: 5 2 3 3 1 5 5 4 7 4 2 2 1 7 5 6 3 4 2 4

    To solve this issue, for the GCNs-Net, when you make your dataset, you have to make your labels from 0 rather than 1. For example, if you have seven classes, your labels should be 0 (First class), 1 (Second class), 2 (Third class), 3 (Fourth class), 4 (Fifth class), 5 (Sixth class), 6 (Seventh class) instead of 1, 2, 3, 4, 5, 6, 7.

  5. IndexError: list index out of range

    To solve this issue, first of all, please double-check the Python Environment. Python 2.7 Environment is highly recommended. Besides, please install 0.1.11 version of pydeflib Python package. The install instruction is as follows:

    $ pip install pydeflib==0.1.11

Structure of the code

At the root of the project, you will see:

├── Download_Raw_EEG_Data
│   ├── Extract-Raw-Data-Into-Matlab-Files.py
│   ├── MIND_Get_EDF.py
│   ├── README.md
│   └── electrode_positions.txt
├── Draw_Photos
│   ├── Draw_Accuracy_Photo.m
│   ├── Draw_Box_Photo.m
│   ├── Draw_Confusion_Matrix.py
│   ├── Draw_Loss_Photo.m
│   ├── Draw_ROC_and_AUC.py
│   └── figure_boxplot.m
├── LICENSE
├── Logo.png
├── MANIFEST.in
├── Models
│   ├── DatasetAPI
│   │   └── DataLoader.py
│   ├── Evaluation_Metrics
│   │   └── Metrics.py
│   ├── Initialize_Variables
│   │   └── Initialize.py
│   ├── Loss_Function
│   │   └── Loss.py
│   ├── Network
│   │   ├── BiGRU.py
│   │   ├── BiGRU_with_Attention.py
│   │   ├── BiLSTM.py
│   │   ├── BiLSTM_with_Attention.py
│   │   ├── BiRNN.py
│   │   ├── BiRNN_with_Attention.py
│   │   ├── CNN.py
│   │   ├── DNN.py
│   │   ├── DenseCNN.py
│   │   ├── Fully_Conv_CNN.py
│   │   ├── GRU.py
│   │   ├── GRU_with_Attention.py
│   │   ├── LSTM.py
│   │   ├── LSTM_with_Attention.py
│   │   ├── RNN.py
│   │   ├── RNN_with_Attention.py
│   │   ├── ResCNN.py
│   │   ├── Siamese_Network.py
│   │   ├── Thin_ResNet.py
│   │   └── lib_for_GCN
│   │       ├── DenseGCN_Model.py
│   │       ├── GCN_Model.py
│   │       ├── ResGCN_Model.py
│   │       ├── coarsening.py
│   │       └── graph.py
│   ├── __init__.py
│   ├── main-BiGRU-with-Attention.py
│   ├── main-BiGRU.py
│   ├── main-BiLSTM-with-Attention.py
│   ├── main-BiLSTM.py
│   ├── main-BiRNN-with-Attention.py
│   ├── main-BiRNN.py
│   ├── main-CNN.py
│   ├── main-DNN.py
│   ├── main-DenseCNN.py
│   ├── main-DenseGCN.py
│   ├── main-FullyConvCNN.py
│   ├── main-GCN.py
│   ├── main-GRU-with-Attention.py
│   ├── main-GRU.py
│   ├── main-LSTM-with-Attention.py
│   ├── main-LSTM.py
│   ├── main-RNN-with-Attention.py
│   ├── main-RNN.py
│   ├── main-ResCNN.py
│   ├── main-ResGCN.py
│   ├── main-Siamese-Network.py
│   └── main-Thin-ResNet.py
├── NEEPU.png
├── Preprocess_EEG_Data
│   ├── For-CNN-based-Models
│   │   └── make_dataset.m
│   ├── For-DNN-based-Models
│   │   └── make_dataset.m
│   ├── For-GCN-based-Models
│   │   └── make_dataset.m
│   ├── For-RNN-based-Models
│   │   └── make_dataset.m
│   └── For-Siamese-Network-One-Shot-Learning
│       └── make_dataset.m
├── README.md
├── Saved_Files
│   └── README.md
├── requirements.txt
└── setup.py

Citation

If you find our library useful, please considering citing our paper in your publications. We provide a BibTeX entry below.

@article{hou2019novel,  
    year = 2020,  
    month = {feb},  
    publisher = {IOP Publishing},  
    volume = {17},  
    number = {1},  
    pages = {016048},  
    author = {Yimin Hou and Lu Zhou and Shuyue Jia and Xiangmin Lun},  
    title = {A novel approach of decoding {EEG} four-class motor imagery tasks via scout {ESI} and {CNN}},  
    journal = {Journal of Neural Engineering}  
}

@article{Lun2020GCNs,
  title={GCNs-Net: A Graph Convolutional Neural Network Approach for Decoding Time-resolved EEG Motor Imagery Signals},
  author={Lun, Xiangmin and Jia, Shuyue and Hou, Yimin and Shi, Yan and Li, Yang and Yang, Hanrui and Zhang, Shu and Lv, Jinglei},
  journal={arXiv preprint arXiv:2006.08924},
  year={2020}
}

@article{Hou2020DeepFM,
  title={Deep Feature Mining via Attention-based BiLSTM-GCN for Human Motor Imagery Recognition},
  author={Hou, Yimin and Jia, Shuyue and Zhang, Shu and Lun, Xiangmin and Shi, Yan and Li, Yang and Yang, Hanrui and Zeng, Rui and Lv, Jinglei},
  journal={arXiv preprint arXiv:2005.00777},
  year={2020}
}

@article{Jia2020AttentionGCN,
  title={Attention-based Graph ResNet for Motor Intent Detection from Raw EEG signals},
  author={Jia, Shuyue and Hou, Yimin and Shi, Yan and Li, Yang},
  journal={arXiv preprint arXiv:2007.13484},
  year={2020}
}

Our papers can be downloaded from:

  1. A Novel Approach of Decoding EEG Four-class Motor Imagery Tasks via Scout ESI and CNN
    Codes and Tutorials for this work can be found here.
Project1

  1. GCNs-Net: A Graph Convolutional Neural Network Approach for Decoding Time-resolved EEG Motor Imagery Signals
    Presentation for this work can be found here.
Project2

  1. Deep Feature Mining via Attention-based BiLSTM-GCN for Human Motor Imagery Recognition
Project3.1
Project4.1

  1. Attention-based Graph ResNet for Motor Intent Detection from Raw EEG signals

Other Useful Resources

I think the following presentations could be helpful when you guys get engaged with Python & TensorFlow or build models.

  1. Python Environment Setting-up Tutorial download

  2. Usage of Cloud Server and Setting-up Tutorial download

  3. TensorFlow for Deep Learning Tutorial download

Contribution

We always welcome contributions to help make EEG-DL Library better. If you would like to contribute or have any question, please feel free to contact me, and my email is shuyuej@ieee.org.

Organizations

The library was created and open-sourced by Shuyue Jia, supervised by Prof. Yimin Hou @ Human Sensor Laboratory, School of Automation Engineering, Northeast Electric Power University, Jilin, China.

About

A Deep Learning library for EEG Tasks (Signals) Classification, based on TensorFlow.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 88.1%
  • MATLAB 11.9%