Skip to content

Commit

Permalink
Update Braindecode dependency to 0.8 (#542)
Browse files Browse the repository at this point in the history
* Update braindecode to 0.8

* Simplify plot_braindecode.py

* Update whats_new.rst

* Update load_model.py

* Remove unused code in examples

* Update braindecode pipelines
  • Loading branch information
PierreGtch committed Feb 1, 2024
1 parent 1b03827 commit fcfae76
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 222 deletions.
1 change: 1 addition & 0 deletions docs/source/whats_new.rst
Expand Up @@ -21,6 +21,7 @@ Enhancements
- Adding cache option to the evaluation (:gh:`517` by `Bruno Aristimunha`_)
- Option to interpolate channel in paradigms' `match_all` method (:gh:`480` by `Gregoire Cattan`_)
- Adding leave k-Subjects out evaluations (:gh:`470` by `Bruno Aristimunha`_)
- Update Braindecode dependency to 0.8 (:gh:`542` by `Pierre Guetschel`_)

Bugs
~~~~
Expand Down
17 changes: 3 additions & 14 deletions examples/load_model.py
Expand Up @@ -16,13 +16,12 @@
from braindecode import EEGClassifier
from braindecode.models import EEGInception
from scikeras.wrappers import KerasClassifier
from sklearn.pipeline import Pipeline
from sklearn.pipeline import Pipeline, make_pipeline
from skorch.callbacks import EarlyStopping, EpochScoring
from skorch.dataset import ValidSplit

from moabb import set_log_level
from moabb.pipelines.features import StandardScaler_Epoch
from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG
from moabb.utils import setup_seed


Expand Down Expand Up @@ -70,9 +69,6 @@
###############################################################################
# Loading the PyTorch model

# Set EEG Inception model
model = EEGInception(in_channels=22, n_classes=2)

# Hyperparameter
LEARNING_RATE = 0.0001
WEIGHT_DECAY = 0
Expand All @@ -84,8 +80,7 @@

# Define a Skorch classifier
clf = EEGClassifier(
module=model,
criterion=torch.nn.CrossEntropyLoss,
module=EEGInception,
optimizer=torch.optim.Adam,
optimizer__lr=LEARNING_RATE,
batch_size=BATCH_SIZE,
Expand All @@ -99,9 +94,6 @@
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
)
Expand All @@ -114,8 +106,5 @@

clf.load_params(f_params=f_params, f_optimizer=f_optimizer, f_history=f_history)

# Create the dataset
create_dataset = BraindecodeDatasetLoader(drop_last_window=False)

# Create the pipelines
pipes_pytorch = Pipeline([("Braindecode_dataset", create_dataset), ("EEGInception", clf)])
pipes_pytorch = make_pipeline(clf)
14 changes: 1 addition & 13 deletions examples/pipelines_braindecode/braindecode_EEGInception.py
Expand Up @@ -6,7 +6,6 @@
from skorch.dataset import ValidSplit

from moabb.pipelines.features import Resampler_Epoch
from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG


# Set up GPU if it is there
Expand All @@ -22,16 +21,9 @@
EPOCH = 10
PATIENCE = 3

# Create the dataset
create_dataset = BraindecodeDatasetLoader()

# Set random 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,
module=EEGInception,
optimizer=torch.optim.Adam,
optimizer__lr=LEARNING_RATE,
batch_size=BATCH_SIZE,
Expand All @@ -46,9 +38,6 @@
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
)
Expand All @@ -57,7 +46,6 @@
pipes = Pipeline(
[
("resample", Resampler_Epoch(128)),
("braindecode_dataset", create_dataset),
("EEGInception", clf),
]
)
Expand Down
14 changes: 1 addition & 13 deletions examples/pipelines_braindecode/braindecode_EEGNetv4.py
Expand Up @@ -6,7 +6,6 @@
from skorch.dataset import ValidSplit

from moabb.pipelines.features import Resampler_Epoch
from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG


# Set up GPU if it is there
Expand All @@ -22,16 +21,9 @@
EPOCH = 10
PATIENCE = 3

# Create the dataset
create_dataset = BraindecodeDatasetLoader()

# Set random 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,
module=EEGNetv4,
optimizer=torch.optim.Adam,
optimizer__lr=LEARNING_RATE,
batch_size=BATCH_SIZE,
Expand All @@ -46,9 +38,6 @@
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
)
Expand All @@ -57,7 +46,6 @@
pipes = Pipeline(
[
("resample", Resampler_Epoch(128)),
("braindecode_dataset", create_dataset),
("EEGNetv4", clf),
]
)
Expand Down
17 changes: 2 additions & 15 deletions examples/pipelines_braindecode/braindecode_ShallowFBCSPNet.py
Expand Up @@ -6,7 +6,6 @@
from skorch.dataset import ValidSplit

from moabb.pipelines.features import Resampler_Epoch
from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG


# Set up GPU if it is there
Expand All @@ -22,18 +21,10 @@
EPOCH = 5
PATIENCE = 3

# Create the dataset
create_dataset = BraindecodeDatasetLoader()

# Set random 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,
module=ShallowFBCSPNet,
module__final_conv_length="auto",
optimizer=torch.optim.Adam,
optimizer__lr=LEARNING_RATE,
batch_size=BATCH_SIZE,
Expand All @@ -48,9 +39,6 @@
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
)
Expand All @@ -59,7 +47,6 @@
pipes = Pipeline(
[
("resample", Resampler_Epoch(250)),
("braindecode_dataset", create_dataset),
("ShallowFBCSPNet", clf),
]
)
Expand Down
19 changes: 3 additions & 16 deletions examples/plot_braindecode.py
Expand Up @@ -16,14 +16,13 @@
import torch
from braindecode import EEGClassifier
from braindecode.models import EEGNetv4
from sklearn.pipeline import Pipeline
from sklearn.pipeline import make_pipeline
from skorch.callbacks import EarlyStopping, EpochScoring
from skorch.dataset import ValidSplit

from moabb.datasets import BNCI2014_001
from moabb.evaluations import CrossSessionEvaluation
from moabb.paradigms import MotorImagery
from moabb.pipelines.utils_pytorch import BraindecodeDatasetLoader, InputShapeSetterEEG
from moabb.utils import setup_seed


Expand Down Expand Up @@ -81,8 +80,6 @@
)
subjects = [1]
X, _, _ = paradigm.get_data(dataset=dataset, subjects=subjects)
# Define Transformer of Dataset compatible with Brain Decode
create_dataset = BraindecodeDatasetLoader()

##############################################################################
# Create Pipelines
Expand All @@ -94,16 +91,9 @@
# callbacks InputShapeSetterEEG, where we have to specify the correct name of the parameter.
# Here, we will use the EEGNet v4 model [1]_ .

model = EEGNetv4(in_chans=1, n_classes=1, input_window_samples=100)

# Send model to GPU
if cuda:
model.cuda()

# Define a Skorch classifier
clf = EEGClassifier(
module=model,
criterion=torch.nn.CrossEntropyLoss,
module=EEGNetv4,
optimizer=torch.optim.Adam,
optimizer__lr=LEARNING_RATE,
batch_size=BATCH_SIZE,
Expand All @@ -118,16 +108,13 @@
EpochScoring(
scoring="accuracy", on_train=False, name="valid_acc", lower_is_better=False
),
InputShapeSetterEEG(
params_list=["in_chans", "input_window_samples", "n_classes"],
),
],
verbose=1, # Not printing the results for each epoch
)

# Create the pipelines
pipes = {}
pipes["EEGNetV4"] = Pipeline([("Braindecode_dataset", create_dataset), ("Net", clf)])
pipes["EEGNetV4"] = make_pipeline(clf)

##############################################################################
# Evaluation
Expand Down

0 comments on commit fcfae76

Please sign in to comment.