Skip to content
/ ML-BCI Public

Machine Learning based Brain Computer Interface (BCI) by analyzing EEG Data using PyTorch

Notifications You must be signed in to change notification settings

PhilKes/ML-BCI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ML-BCI

This repo contains the implementation for my bachelor thesis "Deep Learning based Motor Imagery Brain Computer Interface" for the THU Ulm

You can further read about the project's topic in the published paper

Investigation of a Deep-Learning Based Brain–Computer Interface With Respect to a Continuous Control Application

Machine Learning based Brain-Computer Interface Framework


! Datasets ARE NOT INCLUDED in this repository !

  • PHYS: Dataset is automatically loaded via the MNE Library
  • BCIC: Has to be downloaded manually (see bcic_data_loading.py main method)
  • LSMR21: Has to be downloaded via the scripts/lsmr21_download_convert.py script

Change the Location of the Datasets in paths.py's datasets_folder variable

main.py

Main Script to run Training/Benchmarking of EEGNet

main.py -train

  • n-Class-Classification Training
  • K-Fold Crossvalidation
  • Saving results and trained model in ./results/{DateTime/Name}/training
  • Dataset Selection with --dataset argument

main.py -benchmark

  • Inference Benchmarking in batches with specified trained model (default size: 16)
  • TensorRT optimization possible with --trt flag
  • Saving results (Batch Latency, Inference time per trial) in ./results/{model_path}/benchmark

main.py -train_ss

  • Transfer Learning for further subject-specific Training of pretrained model
  • Select pretrained model with --model argument
  • Subject to train on with --subject argument
  • Saving subject-specific trained model in ./results/{model_path}/training_ss/S{subject}

main.py -live_sim

  • Simulation of a live EEG run classification of a single subject with subject-specific trained model
  • Select subject-specific trained model with --model argument
  • Plotting the run in ./results/{model_path}/training_ss/S{subject}/live_sim

main.py --help for all arguments


config.py

Global Default Configuration/Settings

  • Training/Benchmarking Settings
  • Bandpassfilters
  • EEG Channel Configurations for sensorimotor Events

/machine_learning/models/eegnet.py

PyTorch Implementation of EEGNet

Original Source: xiaywang/q-eegnet_torch

/machine_learning/modes.py

Main loops for

  • EEGNet Training + Testing on Physionet Dataset using 5-Fold CV
  • Subject-specific Training with Transfer Learning
  • Benchmarking of Inferencing over Physionet Dataset with pretrained model
  • Live Simulation of real time Classification on Physionet Dataset Run

/machine_learning/inference_training

Main methods for

  • Training
  • Testing
  • Benchmark
  • Predicting on Batches of Data

/data/datasets/{...}_data_loading.py's

  • Data Loaders for the used Datasets
  • Wrap Datasets in PyTorch Dataloader

/data/datasets/physionet_dataset.py

  • All important Properties of the Physionet Motorimagery Dataset
  • List of available Subjects, Tasks, Runs
  • Default Values for Epoching (tmin, tmax, ...)

/data/datasets/bcic_dataset.py

  • All important Properties of the BCIC Motorimagery Dataset
  • List of available Subjects, Tasks, Runs
  • Default Values for Epoching (tmin, tmax, ...)

/data/datasets/lsmr21_dataset.py

  • All important Properties of the LSMR21 Motorimagery Dataset
  • List of available Subjects, Tasks, Runs
  • Default Values for Epoching (tmin, tmax, ...)

/data/data_utils.py

  • TrialsDataset class for usage with PyTorch Dataloader
  • Utility methods ofr preprocessing data

Scripts

Have to be executed as python modules with:

python3 -m scripts.<script_name>

example_all_modes.py

  • Showcases usage of all available modes in a single script
  • Gives overview on how to use main.py with its arguments in a Terminal

batch_training.py

  • Can be used for Batchruns of Training Runs
  • Training Configurations editable in Python Dictionary
  • Creates .csv and .txt files containing Results of Training Batchruns

neural_responses_training.py

  • Can be used to analyze influence of different Frequency bands
  • Test neural responses in different time slices

best_fold_frequency_test.py

  • Can be used to Test trained model of neural_responses_training.py
  • Tests different Fx-filtered Test Data of trained Model

bench_all_confs.py

  • Runs main.py with all possible Configurations in benchmark mode (/w TRT (fp16/32))
  • Saves results in parent folder ./results/{model_path}/benchmark/
  • Benchmarking with different Batch Sizes with --bs argument

visualize_bench_all.py

  • Plots and saves Results from bench_all_confs.py Runs as .png
  • --model specifies the folder location of the results.npz file

/paths.py

  • All relevant paths, results folder locations, file names, ...

/util/plot.py

  • Plot Results with Matplotlib

/util/configs_results.py

  • Handles storing results/config .txt files

python_test.py

Python Playground for testing, has no further use


Libraries

Use python3 -m pip install -r requirements.txt to ensure all necessary libraries are installed

For usage of -benchmark also ensure TensorRT is correctly setup on the system

Numpy

PyTorch

PyCuda

MNE

torch2trt

Matplotlib

Pandas

About

Machine Learning based Brain Computer Interface (BCI) by analyzing EEG Data using PyTorch

Topics

Resources

Stars

Watchers

Forks

Languages