-
Notifications
You must be signed in to change notification settings - Fork 216
Plot brain decode #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Plot brain decode #345
Changes from all commits
Commits
Show all changes
93 commits
Select commit
Hold shift + click to select a range
b935134
Fix bug for MotorImagery All events, setting n_classes
carraraig ecc0499
Merge branch 'develop' into develop
carraraig 70d0539
Fix bug for MotorImagery All events, setting n_classes
carraraig 989f38d
Merge remote-tracking branch 'origin/develop' into develop
carraraig a556c8a
Fix is Valid
carraraig ced2bda
Update docs/source/whats_new.rst
sylvchev 8aeb748
Merge branch 'develop' into develop
sylvchev 4ad6970
Merge branch 'NeuroTechX:develop' into develop
carraraig 56fb305
plot_BrainDecode first draft
carraraig c39c67a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 52949fd
plot_BrainDecode
carraraig b5ce192
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 78a2c83
plot_BrainDecode
carraraig 084af1f
Merge remote-tracking branch 'origin/develop' into develop
carraraig 0baad7b
plot_BrainDecode
carraraig 3b7d3a2
Update examples/plot_BrainDecode.py
carraraig 188fe0c
Update examples/plot_BrainDecode.py
carraraig 3ed2dcd
plot_BrainDecode
carraraig 400fe41
plot_BrainDecode, definition of setup_seed in moabb.utils
carraraig 0b1b1ff
plot_BrainDecode, definition of setup_seed in moabb.utils
carraraig fc6744b
brain decode try benchmark
carraraig d3482c6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c1384b7
Plot_function
carraraig dede68c
Merge remote-tracking branch 'origin/develop' into develop
carraraig 5e06156
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1540dd4
Delete PyTorch_EEGNetv4.yml
carraraig fb2c66d
Delete plot_benchmark_BrainDecode.py
carraraig c642d78
Renaming the file
bruAristimunha 67cfeb5
Changing init file
bruAristimunha 4c03f28
Changing the tutorial
bruAristimunha 607c295
Renaming the file to lower
bruAristimunha a43cc6b
Updating the InputSetterEEG
bruAristimunha 7b5a238
Adding new function to get the models from braindecode, and forcing t…
bruAristimunha 27d5aa2
Partially init the shallow model
bruAristimunha 57af303
Changing to ShallowNet
bruAristimunha 29e0b4c
=)
bruAristimunha 5c4fa88
draft test
bruAristimunha fb7b43f
removing test
bruAristimunha 2753caa
Merge branch 'develop' into develop
bruAristimunha 47c2454
Plot_function
carraraig 8171bad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 09f4ec8
update plot_braindecode
carraraig a7a4742
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 617f8c1
Plot BranDecode with dynamically input setter
carraraig e139a1e
Merge branch 'develop' into plot_brainDecode
carraraig f6ba105
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 28b840e
Plot BranDecode with dynamically input setter
carraraig 121d161
Merge remote-tracking branch 'origin/plot_brainDecode' into plot_brai…
carraraig b5de29f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c910871
remove plot_BrainDecode from GitIgnore
carraraig 3a69065
Merge remote-tracking branch 'origin/plot_brainDecode' into plot_brai…
carraraig 8b718e4
Plot_Benchmark:
carraraig 6f9a999
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] edeadbe
Plot_Benchmark:
carraraig afffb8e
Plot_Benchmark:
carraraig 1ea8f17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8dd2aaa
precommit stuffs
bruAristimunha 5dcb88c
cleaning line
bruAristimunha 8253fc3
Changing the set seed
bruAristimunha 5087507
Changing the import
bruAristimunha 9fb0d8e
Renaming BrainDecode to braindecode
bruAristimunha 58460ca
Update moabb/benchmark.py
bruAristimunha a20413f
renaming to lowercase
bruAristimunha 0775cbb
lower case for pipeline folder of braindecode
carraraig 6c50f57
Update examples/plot_braindecode.py
carraraig 2e1629f
Update examples/plot_braindecode.py
carraraig cf19877
Update examples/pipelines_braindecode/braindecode_Deep4Net.py
carraraig d084210
Update examples/pipelines_braindecode/braindecode_Deep4Net.py
carraraig 3d35329
Update examples/pipelines_braindecode/braindecode_EEGInception.py
carraraig 2ea2f6c
Update examples/pipelines_braindecode/braindecode_EEGNetv4.py
carraraig 20f07ed
Modification requested
carraraig 2195b3d
Modification requested
carraraig 374cd62
typos, and another small general corrections.
bruAristimunha e50a735
Merge remote-tracking branch 'carraraig/plot_brainDecode' into plot_b…
bruAristimunha 0ce838a
Fixing small things for the review
bruAristimunha bf5e7ad
Update examples/plot_benchmark_braindecode.py
bruAristimunha 7a0c41d
Update examples/plot_braindecode.py
bruAristimunha f4f96e3
Update examples/pipelines_braindecode/braindecode_ShallowFBCSPNet.py
bruAristimunha cd9d80d
fixing tests
bruAristimunha 91bad31
Adding the Pipeline of braincode in the pipeline folder
carraraig 1463545
Merge remote-tracking branch 'origin/plot_brainDecode' into plot_brai…
carraraig d2e20b0
removing deep4net
bruAristimunha 849e8d2
Merge remote-tracking branch 'carraraig/plot_brainDecode' into plot_b…
bruAristimunha 59d92fa
fixing test
bruAristimunha ec8694d
adding text
bruAristimunha c311edf
changing to parametrize
bruAristimunha 34b1b28
Updating function
bruAristimunha 8835dc1
Updating test function
bruAristimunha 53b106d
changing the init file
bruAristimunha 4c36c62
adding braindecode to the folder
bruAristimunha 94224e8
testing hidden util braindecode
bruAristimunha 8999f17
splitting the util tests to not download
bruAristimunha cd3ba5b
updating the test
bruAristimunha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
examples/pipelines_braindecode/braindecode_EEGInception.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import torch | ||
| from braindecode import EEGClassifier | ||
| from braindecode.models import EEGInception | ||
| from sklearn.pipeline import Pipeline | ||
| from skorch.callbacks import EarlyStopping, EpochScoring | ||
| from skorch.dataset import ValidSplit | ||
|
|
||
| from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG | ||
|
|
||
|
|
||
| # Set up GPU if it is there | ||
| cuda = torch.cuda.is_available() | ||
| device = "cuda" if cuda else "cpu" | ||
|
|
||
| # Hyperparameter | ||
| LEARNING_RATE = 0.0001 | ||
| WEIGHT_DECAY = 0 | ||
| BATCH_SIZE = 64 | ||
| SEED = 42 | ||
| VERBOSE = 1 | ||
| EPOCH = 10 | ||
| PATIENCE = 3 | ||
|
|
||
| # Create the dataset | ||
| create_dataset = BraindecodeDatasetLoader() | ||
|
|
||
| # Set EEG Inception model | ||
| model = EEGInception(in_channels=1, n_classes=2, input_window_samples=100) | ||
|
|
||
| # Define a Skorch classifier | ||
| clf = EEGClassifier( | ||
| module=model, | ||
| criterion=torch.nn.CrossEntropyLoss, | ||
| optimizer=torch.optim.Adam, | ||
| optimizer__lr=LEARNING_RATE, | ||
| batch_size=BATCH_SIZE, | ||
| max_epochs=EPOCH, | ||
| train_split=ValidSplit(0.2, random_state=SEED), | ||
| device=device, | ||
| callbacks=[ | ||
| EarlyStopping(monitor="valid_loss", patience=PATIENCE), | ||
| EpochScoring( | ||
| scoring="accuracy", on_train=True, name="train_acc", lower_is_better=False | ||
| ), | ||
| EpochScoring( | ||
| scoring="accuracy", on_train=False, name="valid_acc", lower_is_better=False | ||
| ), | ||
| InputShapeSetterEEG( | ||
| params_list=["in_channels", "input_window_samples", "n_classes"], | ||
| ), | ||
| ], | ||
| verbose=VERBOSE, # Not printing the results for each epoch | ||
| ) | ||
|
|
||
| # Create the pipelines | ||
| pipes = Pipeline([("Braindecode_dataset", create_dataset), ("EEGInception", clf)]) | ||
|
|
||
| # this is what will be loaded | ||
| PIPELINE = { | ||
| "name": "braindecode_EEGInception", | ||
| "paradigms": ["LeftRightImagery", "MotorImagery"], | ||
| "pipeline": pipes, | ||
| "citations": "https://doi.org/10.1109/TNSRE.2020.3048106", | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import torch | ||
| from braindecode import EEGClassifier | ||
| from braindecode.models import EEGNetv4 | ||
| from sklearn.pipeline import Pipeline | ||
| from skorch.callbacks import EarlyStopping, EpochScoring | ||
| from skorch.dataset import ValidSplit | ||
|
|
||
| from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG | ||
|
|
||
|
|
||
| # Set up GPU if it is there | ||
| cuda = torch.cuda.is_available() | ||
| device = "cuda" if cuda else "cpu" | ||
|
|
||
| # Hyperparameter | ||
| LEARNING_RATE = 0.0001 | ||
| WEIGHT_DECAY = 0 | ||
| BATCH_SIZE = 64 | ||
| SEED = 42 | ||
| VERBOSE = 1 | ||
| EPOCH = 10 | ||
| PATIENCE = 3 | ||
|
|
||
| # Create the dataset | ||
| create_dataset = BraindecodeDatasetLoader() | ||
|
|
||
| # Set EEGNetv4 model | ||
| model = EEGNetv4(in_chans=1, n_classes=2, input_window_samples=100) | ||
|
|
||
| # Define a Skorch classifier | ||
| clf = EEGClassifier( | ||
| module=model, | ||
| criterion=torch.nn.CrossEntropyLoss, | ||
| optimizer=torch.optim.Adam, | ||
| optimizer__lr=LEARNING_RATE, | ||
| batch_size=BATCH_SIZE, | ||
| max_epochs=EPOCH, | ||
| train_split=ValidSplit(0.2, random_state=SEED), | ||
| device=device, | ||
| callbacks=[ | ||
| EarlyStopping(monitor="valid_loss", patience=PATIENCE), | ||
| EpochScoring( | ||
| scoring="accuracy", on_train=True, name="train_acc", lower_is_better=False | ||
| ), | ||
| EpochScoring( | ||
| scoring="accuracy", on_train=False, name="valid_acc", lower_is_better=False | ||
| ), | ||
| InputShapeSetterEEG( | ||
| params_list=["in_chans", "input_window_samples", "n_classes"], | ||
| ), | ||
| ], | ||
| verbose=VERBOSE, # Not printing the results for each epoch | ||
| ) | ||
|
|
||
| # Create the pipelines | ||
| pipes = Pipeline([("braindecode_dataset", create_dataset), ("EEGNetv4", clf)]) | ||
|
|
||
| # this is what will be loaded | ||
| PIPELINE = { | ||
| "name": "braindecode_EEGNetv4", | ||
| "paradigms": ["LeftRightImagery", "MotorImagery"], | ||
| "pipeline": pipes, | ||
carraraig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "citations": "https://doi.org/10.1088/1741-2552/aace8c", | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
examples/pipelines_braindecode/braindecode_ShallowFBCSPNet.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import torch | ||
| from braindecode import EEGClassifier | ||
| from braindecode.models import ShallowFBCSPNet | ||
| from sklearn.pipeline import Pipeline | ||
| from skorch.callbacks import EarlyStopping, EpochScoring | ||
| from skorch.dataset import ValidSplit | ||
|
|
||
| from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG | ||
|
|
||
|
|
||
| # Set up GPU if it is there | ||
| cuda = torch.cuda.is_available() | ||
| device = "cuda" if cuda else "cpu" | ||
|
|
||
| # Hyperparameter | ||
| LEARNING_RATE = 0.0001 | ||
| WEIGHT_DECAY = 0 | ||
| BATCH_SIZE = 64 | ||
| SEED = 42 | ||
| VERBOSE = 1 | ||
| EPOCH = 10 | ||
| PATIENCE = 3 | ||
|
|
||
| # Create the dataset | ||
| create_dataset = BraindecodeDatasetLoader() | ||
|
|
||
| # Set Shallow Filter Bank CSP Net model | ||
| model = ShallowFBCSPNet( | ||
| in_chans=1, n_classes=2, input_window_samples=100, final_conv_length="auto" | ||
| ) | ||
|
|
||
| # Define a Skorch classifier | ||
| clf = EEGClassifier( | ||
| module=model, | ||
| criterion=torch.nn.CrossEntropyLoss, | ||
| optimizer=torch.optim.Adam, | ||
| optimizer__lr=LEARNING_RATE, | ||
| batch_size=BATCH_SIZE, | ||
| max_epochs=EPOCH, | ||
| train_split=ValidSplit(0.2, random_state=SEED), | ||
| device=device, | ||
| callbacks=[ | ||
| EarlyStopping(monitor="valid_loss", patience=PATIENCE), | ||
| EpochScoring( | ||
| scoring="accuracy", on_train=True, name="train_acc", lower_is_better=False | ||
| ), | ||
| EpochScoring( | ||
| scoring="accuracy", on_train=False, name="valid_acc", lower_is_better=False | ||
| ), | ||
| InputShapeSetterEEG( | ||
| params_list=["in_chans", "input_window_samples", "n_classes"], | ||
| ), | ||
| ], | ||
| verbose=VERBOSE, # Not printing the results for each epoch | ||
| ) | ||
|
|
||
| # Create the pipelines | ||
| pipes = Pipeline([("braindecode_dataset", create_dataset), ("ShallowFBCSPNet", clf)]) | ||
|
|
||
| # this is what will be loaded | ||
| PIPELINE = { | ||
| "name": "braindecode_ShallowFBCSPNet", | ||
| "paradigms": ["LeftRightImagery", "MotorImagery"], | ||
| "pipeline": pipes, | ||
bruAristimunha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "citations": "https://doi.org/10.1002/hbm.23730", | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """ | ||
| ======================================================================= | ||
| Benchmarking on MOABB with Braindecode (PyTorch) deep net architectures | ||
| ======================================================================= | ||
| This example shows how to use MOABB to benchmark a set of Braindecode pipelines (deep learning | ||
| architectures) on all available datasets. | ||
| For this example, we will use only 2 datasets to keep the computation time low, but this benchmark is designed | ||
| to easily scale to many datasets. | ||
| """ | ||
| # Authors: Igor Carrara <[email protected]> | ||
| # Bruno Aristimunha <[email protected]> | ||
| # Sylvain Chevallier <[email protected]> | ||
| # | ||
| # License: BSD (3-clause) | ||
|
|
||
| import os | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import torch | ||
| from absl.logging import ERROR, set_verbosity | ||
|
|
||
| from moabb import benchmark, set_log_level | ||
| from moabb.analysis.plotting import score_plot | ||
| from moabb.datasets import BNCI2014001, BNCI2014004 | ||
| from moabb.utils import setup_seed | ||
|
|
||
|
|
||
| set_log_level("info") | ||
| # Avoid output Warning | ||
| set_verbosity(ERROR) | ||
| os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" | ||
|
|
||
| # Print Information PyTorch | ||
| print(f"Torch Version: {torch.__version__}") | ||
|
|
||
| # Set up GPU if it is there | ||
| cuda = torch.cuda.is_available() | ||
| device = "cuda" if cuda else "cpu" | ||
| print("GPU is", "AVAILABLE" if cuda else "NOT AVAILABLE") | ||
|
|
||
| ############################################################################### | ||
| # In this example, we will use only 2 subjects from the dataset ``BNCI2014001`` and ``BNCI2014004``. | ||
| # | ||
| # Running the benchmark | ||
| # --------------------- | ||
| # | ||
| # The benchmark is run using the ``benchmark`` function. You need to specify the | ||
| # folder containing the pipelines, the kind of evaluation, and the paradigm | ||
| # to use. By default, the benchmark will use all available datasets for all | ||
| # paradigms listed in the pipelines. You could restrict to specific evaluation and | ||
| # paradigm using the ``evaluations`` and ``paradigms`` arguments. | ||
| # | ||
| # To save computation time, the results are cached. If you want to re-run the | ||
| # benchmark, you can set the ``overwrite`` argument to ``True``. | ||
| # | ||
| # It is possible to indicate the folder to cache the results and the one to save | ||
| # the analysis & figures. By default, the results are saved in the ``results`` | ||
| # folder, and the analysis & figures are saved in the ``benchmark`` folder. | ||
| # | ||
| # This code is implemented to run on CPU. If you're using a GPU, do not use multithreading | ||
| # (i.e. set n_jobs=1) | ||
| # | ||
| # In order to allow the benchmark function to work with return_epoch=True (Required to use Braindecode( | ||
| # we need to call each pipeline as "braindecode_xxx...", with xxx the name of the model to be | ||
| # handled correctly by the benchmark function. | ||
|
|
||
carraraig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Set up reproducibility of Tensorflow | ||
| setup_seed(42) | ||
|
|
||
| # Restrict this example only to the first two subjects of BNCI2014001 | ||
| dataset = BNCI2014001() | ||
| dataset2 = BNCI2014004() | ||
| dataset.subject_list = dataset.subject_list[:2] | ||
| dataset2.subject_list = dataset2.subject_list[:2] | ||
| datasets = [dataset, dataset2] | ||
|
|
||
| results = benchmark( | ||
| pipelines="./pipelines_braindecode", | ||
| evaluations=["CrossSession"], | ||
| paradigms=["LeftRightImagery"], | ||
| include_datasets=datasets, | ||
| results="./results/", | ||
| overwrite=False, | ||
| plot=False, | ||
| output="./benchmark/", | ||
| n_jobs=-1, | ||
| ) | ||
|
|
||
| ############################################################################### | ||
| # The deep learning architectures implemented in MOABB using Braindecode are: | ||
| # | ||
| # - Shallow Convolutional Network [1]_ | ||
bruAristimunha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # - Deep Convolutional Network [1]_ | ||
| # - EEGNetv4 [2]_ | ||
| # - EEGInception [3]_ | ||
| # | ||
| # Benchmark prints a summary of the results. Detailed results are saved in a | ||
| # pandas dataframe, and can be used to generate figures. The analysis & figures | ||
| # are saved in the ``benchmark`` folder. | ||
|
|
||
| score_plot(results) | ||
| plt.show() | ||
|
|
||
| ############################################################################## | ||
| # References | ||
| # ---------- | ||
| # .. [1] Schirrmeister, R. T., Springenberg, J. T., Fiederer, L. D. J., | ||
| # Glasstetter, M., Eggensperger, K., Tangermann, M., ... & Ball, T. (2017). | ||
| # `Deep learning with convolutional neural networks for EEG decoding and | ||
| # visualization <https://doi.org/10.1002/hbm.23730>`_. | ||
| # Human brain mapping, 38(11), 5391-5420. | ||
| # .. [2] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon, S. M., | ||
| # Hung, C. P., & Lance, B. J. (2018). `EEGNet: a compact convolutional neural | ||
| # network for EEG-based brain-computer interfaces. | ||
| # <https://doi.org/10.1088/1741-2552/aace8c>`_ | ||
| # Journal of neural engineering, 15(5), 056013. | ||
| # .. [3] Santamaria-Vazquez, E., Martinez-Cagigal, V., Vaquerizo-Villar, | ||
| # F., & Hornero, R. (2020). `EEG-inception: A novel deep convolutional neural network | ||
| # for assistive ERP-based brain-computer interfaces. | ||
| # <https://doi.org/10.1109/TNSRE.2020.3048106>`_ | ||
| # IEEE Transactions on Neural Systems and Rehabilitation Engineering | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.