GithubHelp home page GithubHelp logo

cvxtz / eeg_classification Goto Github PK

View Code? Open in Web Editor NEW
141.0 6.0 39.0 396 KB

EEG Sleep stage classification using CNN with Keras

License: Apache License 2.0

Python 74.14% Shell 25.86%
eeg keras tensorflow convolutional-neural-networks

eeg_classification's Introduction

DOI

EEG_classification

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 Neural Networks


Photo by Paul M on Unsplash

Quality Sleep is an important part of a healthy lifestyle as lack of it can cause a list of issues like a higher risk of cancer and chronic fatigue. This means that having the tools to automatically and easily monitor sleep can be powerful to help people sleep better.
Doctors use a recording of a signal called EEG which measures the electrical activity of the brain using an electrode to understand sleep stages 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 classification automatically from EEGs.

Data

In our input we have a sequence of 30s epochs of EEG where each epoch has a label {“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 ) that was done on 20 subject, 19 of which have 2 full nights of sleep. We use the pre-processing scripts available in this repo and split the train/test so that 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 the output hypnogram like in fig 2.

Model Description

Recent approaches [1] use a sub-model that encodes each epoch into a 1D vector of fixed size and then a second sequential 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 that labels the sequence of epochs to create the final hypnogram. This allows the prediction for 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 Layer of Keras which produces a sequence of vectors. The sequence of vectors is then fed into a another sub-model like an LSTM or a CNN that produces the sequence of output labels.
We also use a linear Chain CRF for one of the models and show that it can improve the performance.

Training Procedure

The full model is trained end-to-end from scratch using Adam optimizer with an initial learning rate of 1e⁻³ that is reduced each time the validation accuracy plateaus using the ReduceLROnPlateau Keras Callbacks.

Accuracy Training curves

Results

We compare 3 different models :

  • CNN-CNN : This ones used a 1D CNN for the epoch encoding and then another 1D CNN for the sequence labeling.
  • CNN-CNN-CRF : This model used a 1D CNN for the epoch encoding and then a 1D CNN-CRF for the sequence labeling.
  • CNN-LSTM : This ones used a 1D CNN for the epoch encoding and then an LSTM for the 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 the transition probabilities between classes. The LSTM based model does not work as well because it is most sensitive to hyper-parameters like the optimizer and the batch 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 Raw Single-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}
}

eeg_classification's People

Contributors

cvxtz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

eeg_classification's Issues

Alignment of base model output and get_model_input

Hello I'm trying to alter your code for a project.
From what I see the get_model_cnn is expecting input shape of (None, 3000, 1) (see in seq_input) , but you are passing input as (None, 64) (which is output of get_base_model). This is causing an Error when checking target: expected 3 dimensions, but got array with shape (32, 6) in the out layer of get_model_cnn.

Could let me if know if this is the intended output and input shapes of the base_model and get_model_cnn?

Unable to preprocess data

Hi,
I am having issues with prepare_physionet.py. If I run it with python2, the following error appears.
image

According to this, it seems to be resolved with python3. If I run it with python3, Following error is seen image
But, according to this issue, we need to change scale_time to scaling_time. But, it is already scaling_time in the code.

Can you please help in getting the processing done.
It would be great if you can share the already preprocessed npy/csv files. So that I can focus on next part(training).
Thanks

Problem with data preprocess

Hello Youness,

Thank you so much for your contribution. I got the following error when trying to run the prepare_physionet.py:

Traceback (most recent call last):
File "prepare_physionet.py", line 15, in
from mne import Epochs, pick_types, find_events
File "L:\Programs\anaconda3\envs\env_py2\lib\site-packages\mne_init_.py", line 22, in
from .utils import (set_log_level, set_log_file, verbose, set_config,
File "L:\Programs\anaconda3\envs\env_py2\lib\site-packages\mne\utils_init_.py", line 23, in
from .fetching import _fetch_file, _url_to_local_path
File "L:\Programs\anaconda3\envs\env_py2\lib\site-packages\mne\utils\fetching.py", line 11, in
from urllib import parse, request
ImportError: cannot import name parse

It seems the dependency library is out of date? Is there any way to counter the problem?
Thank you so much.

how

hi
I cant understand how do you read data epoch
base_path = "/media/ml/data_ml/EEG/deepsleepnet/data_npy"
Where does this point?

Python Version?

Dear Paul,
Great work you have here. May I know what Python version you are using in the development of this work?

Getting the error codec can't decode byte

Hi, while executing the file prepare_physionet, the following error was thrown out

UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 2090: character maps to <undefined>

This error was generated specifically when the compiler read the prepare_physionet line 104 which direct to

assert f.tell() == 0 # check file position

Also, this was done under Python 3.7. While author advised to use Python 2.x, but it seems there is no MNE support available for older version.

Appreciate for any advise regarding this matter.

utils.py-rescale_array()

Hi! I have tested that rescale method affect largely on the results, so I am very curious about why the rescale method works.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.