- Notifications
You must be signed in to change notification settings - Fork39
EEG Sleep stage classification using CNN with Keras
License
CVxTz/EEG_classification
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Description of the approach :https://towardsdatascience.com/sleep-stage-classification-from-single-channel-eeg-using-convolutional-neural-networks-5c710d92d38e
Sleep Stage Classification from Single Channel EEG using Convolutional NeuralNetworks
Quality Sleep is an important part of a healthy lifestyle as lack of it cancause a list ofissueslike a higher risk of cancer and chronic fatigue. This means that having thetools to automatically and easily monitor sleep can be powerful to help peoplesleep better.
Doctors use a recording of a signal called EEG which measuresthe electrical activity of the brain using an electrode to understand sleepstages of a patient and make a diagnosis about the quality if their sleep.
In this post we will train a neural network to do the sleep stage classificationautomatically from EEGs.
In our input we have a sequence of 30s epochs of EEG where each epoch has alabel{“W”, “N1”, “N2”, “N3”,“REM”}.
Fig 1 : EEG Epoch
Fig 2 : Sleep stages through the night
This post is based on a publicly available EEG Sleep data (Sleep-EDF ) thatwas done on 20 subject, 19 of which have 2 full nights of sleep. We use thepre-processing scripts available in thisrepo and split the train/test sothat no study subject is in both at the same time.
The general objective is to go from a 1D sequence like in fig 1 and predict theoutput hypnogram like in fig 2.
Recent approaches[1] use a sub-modelthat encodes each epoch into a 1D vector of fixed size and then a secondsequential sub-model that maps each epoch’s vector into a class from{“W”,“N1”, “N2”, “N3”, “REM”}.
Here we use a 1D CNN to encode each Epoch and then another 1D CNN or LSTM thatlabels the sequence of epochs to create the finalhypnogram. This allows the predictionfor an epoch to take into account the context.
Sub-model 1 : Epoch encoder
Sub-model 2 : Sequential model for epoch classification
The full model takes as input the sequence of EEG epochs ( 30 seconds each)where the sub-model 1 is applied to each epoch using the TimeDistributed LayerofKeras which produces a sequence of vectors. The sequenceof vectors is then fed into a another sub-model like an LSTM or a CNN thatproduces the sequence of output labels.
We also use a linear ChainCRF for one of themodels and show that it can improve the performance.
The full model is trained end-to-end from scratch using Adam optimizer with aninitial learning rate of 1e⁻³ that is reduced each time the validation accuracyplateaus using the ReduceLROnPlateau Keras Callbacks.
Accuracy Training curves
We compare 3 different models :
- CNN-CNN : This ones used a 1D CNN for the epoch encoding and then another 1D CNNfor the sequence labeling.
- CNN-CNN-CRF : This model used a 1D CNN for the epoch encoding and then a 1DCNN-CRF for the sequence labeling.
- CNN-LSTM : This ones used a 1D CNN for the epoch encoding and then an LSTM forthe sequence labeling.
We evaluate each model on an independent test set and get the following results:
- CNN-CNN : F1 = 0.81, ACCURACY = 0.87
- CNN-CNN-CRF : F1 = 0.82, ACCURACY =0.89
- CNN-LSTM : F1 = 0.71, ACCURACY = 0.76
The CNN-CNN-CRF outperforms the two other models because the CRF helps learn thetransition probabilities between classes. The LSTM based model does not work aswell because it is most sensitive to hyper-parameters like the optimizer and thebatch size and requires extensive tuning to perform well.
Ground Truth Hypnogram
Predicted Hypnogram using CNN-CNN-CRF
Source code available here :https://github.com/CVxTz/EEG_classification
I look forward to your suggestions and feedback.
[1] DeepSleepNet: a Model for Automatic Sleep Stage Scoring based on RawSingle-Channel EEG
How to cite:
@software{mansar_youness_2020_4060151, author = {Mansar Youness}, title = {CVxTz/EEG\_classification: v1.0}, month = sep, year = 2020, publisher = {Zenodo}, version = {v1.0}, doi = {10.5281/zenodo.4060151}, url = {https://doi.org/10.5281/zenodo.4060151}}
About
EEG Sleep stage classification using CNN with Keras