Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bruAristimunha committed Oct 2, 2024
1 parent 292b987 commit 770fc3d
Show file tree
Hide file tree
Showing 61 changed files with 1,261 additions and 1,269 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"outputs": [],
"source": [
"def load_example_data(preload, window_len_s, n_recordings=10):\n \"\"\"Create windowed dataset from subjects of the TUH Abnormal dataset.\n\n Parameters\n ----------\n preload: bool\n If True, use eager loading, otherwise use lazy loading.\n window_len_s: int\n Window length in seconds.\n n_recordings: list of int\n Number of recordings to load.\n\n Returns\n -------\n windows_ds: BaseConcatDataset\n Windowed data.\n\n .. warning::\n The recordings from the TUH Abnormal corpus do not all share the same\n sampling rate. The following assumes that the files have already been\n resampled to a common sampling rate.\n \"\"\"\n\n recording_ids = list(range(n_recordings))\n\n ds = TUHAbnormal(\n TUH_PATH,\n recording_ids=recording_ids,\n target_name=\"pathological\",\n preload=preload,\n )\n\n fs = ds.datasets[0].raw.info[\"sfreq\"]\n window_len_samples = int(fs * window_len_s)\n window_stride_samples = int(fs * 4)\n # window_stride_samples = int(fs * window_len_s)\n windows_ds = create_fixed_length_windows(\n ds,\n start_offset_samples=0,\n stop_offset_samples=None,\n window_size_samples=window_len_samples,\n window_stride_samples=window_stride_samples,\n drop_last_window=True,\n preload=preload,\n )\n\n # Drop bad epochs\n # XXX: This could be parallelized.\n # XXX: Also, this could be implemented in the Dataset object itself.\n for ds in windows_ds.datasets:\n ds.windows.drop_bad()\n assert ds.windows.preload == preload\n\n return windows_ds\n\n\ndef create_example_model(\n n_channels, n_classes, window_len_samples, kind=\"shallow\", cuda=False\n):\n \"\"\"Create model, loss and optimizer.\n\n Parameters\n ----------\n n_channels : int\n Number of channels in the input\n n_times : int\n Window length in the input\n n_classes : int\n Number of classes in the output\n kind : str\n 'shallow' or 'deep'\n cuda : bool\n If True, move the model to a CUDA device.\n\n Returns\n -------\n model : torch.nn.Module\n Model to train.\n loss :\n Loss function\n optimizer :\n Optimizer\n \"\"\"\n if kind == \"shallow\":\n model = ShallowFBCSPNet(\n n_channels,\n n_classes,\n input_window_samples=window_len_samples,\n n_filters_time=40,\n filter_time_length=25,\n n_filters_spat=40,\n pool_time_length=75,\n pool_time_stride=15,\n final_conv_length=\"auto\",\n split_first_layer=True,\n batch_norm=True,\n batch_norm_alpha=0.1,\n drop_prob=0.5,\n )\n elif kind == \"deep\":\n model = Deep4Net(\n n_channels,\n n_classes,\n input_window_samples=window_len_samples,\n final_conv_length=\"auto\",\n n_filters_time=25,\n n_filters_spat=25,\n filter_time_length=10,\n pool_time_length=3,\n pool_time_stride=3,\n n_filters_2=50,\n filter_length_2=10,\n n_filters_3=100,\n filter_length_3=10,\n n_filters_4=200,\n filter_length_4=10,\n first_pool_mode=\"max\",\n later_pool_mode=\"max\",\n drop_prob=0.5,\n double_time_convs=False,\n split_first_layer=True,\n batch_norm=True,\n batch_norm_alpha=0.1,\n stride_before_pool=False,\n )\n else:\n raise ValueError\n\n if cuda:\n model.cuda()\n\n optimizer = optim.Adam(model.parameters())\n loss = nn.CrossEntropyLoss()\n\n return model, loss, optimizer\n\n\ndef run_training(model, dataloader, loss, optimizer, n_epochs=1, cuda=False):\n \"\"\"Run training loop.\n\n Parameters\n ----------\n model : torch.nn.Module\n Model to train.\n dataloader : torch.utils.data.Dataloader\n Data loader which will serve examples to the model during training.\n loss :\n Loss function.\n optimizer :\n Optimizer.\n n_epochs : int\n Number of epochs to train the model for.\n cuda : bool\n If True, move X and y to CUDA device.\n\n Returns\n -------\n model : torch.nn.Module\n Trained model.\n \"\"\"\n for i in range(n_epochs):\n loss_vals = list()\n for X, y, _ in dataloader:\n model.train()\n model.zero_grad()\n\n y = y.long()\n if cuda:\n X, y = X.cuda(), y.cuda()\n\n loss_val = loss(model(X), y)\n loss_vals.append(loss_val.item())\n\n loss_val.backward()\n optimizer.step()\n\n print(f\"Epoch {i + 1} - mean training loss: {np.mean(loss_vals)}\")\n\n return model"
"def load_example_data(preload, window_len_s, n_recordings=10):\n \"\"\"Create windowed dataset from subjects of the TUH Abnormal dataset.\n\n Parameters\n ----------\n preload: bool\n If True, use eager loading, otherwise use lazy loading.\n window_len_s: int\n Window length in seconds.\n n_recordings: list of int\n Number of recordings to load.\n\n Returns\n -------\n windows_ds: BaseConcatDataset\n Windowed data.\n\n .. warning::\n The recordings from the TUH Abnormal corpus do not all share the same\n sampling rate. The following assumes that the files have already been\n resampled to a common sampling rate.\n \"\"\"\n\n recording_ids = list(range(n_recordings))\n\n ds = TUHAbnormal(\n TUH_PATH,\n recording_ids=recording_ids,\n target_name=\"pathological\",\n preload=preload,\n )\n\n fs = ds.datasets[0].raw.info[\"sfreq\"]\n window_len_samples = int(fs * window_len_s)\n window_stride_samples = int(fs * 4)\n # window_stride_samples = int(fs * window_len_s)\n windows_ds = create_fixed_length_windows(\n ds,\n start_offset_samples=0,\n stop_offset_samples=None,\n window_size_samples=window_len_samples,\n window_stride_samples=window_stride_samples,\n drop_last_window=True,\n preload=preload,\n )\n\n # Drop bad epochs\n # XXX: This could be parallelized.\n # XXX: Also, this could be implemented in the Dataset object itself.\n # We don't support drop_bad since the last version braindecode,\n # to optimize the dataset speed. If you know how to fix, please open a PR.\n # for ds in windows_ds.datasets:\n # ds.raw.drop_bad()\n # assert ds.raw.preload == preload\n\n return windows_ds\n\n\ndef create_example_model(\n n_channels, n_classes, window_len_samples, kind=\"shallow\", cuda=False\n):\n \"\"\"Create model, loss and optimizer.\n\n Parameters\n ----------\n n_channels : int\n Number of channels in the input\n n_times : int\n Window length in the input\n n_classes : int\n Number of classes in the output\n kind : str\n 'shallow' or 'deep'\n cuda : bool\n If True, move the model to a CUDA device.\n\n Returns\n -------\n model : torch.nn.Module\n Model to train.\n loss :\n Loss function\n optimizer :\n Optimizer\n \"\"\"\n if kind == \"shallow\":\n model = ShallowFBCSPNet(\n n_channels,\n n_classes,\n input_window_samples=window_len_samples,\n n_filters_time=40,\n filter_time_length=25,\n n_filters_spat=40,\n pool_time_length=75,\n pool_time_stride=15,\n final_conv_length=\"auto\",\n split_first_layer=True,\n batch_norm=True,\n batch_norm_alpha=0.1,\n drop_prob=0.5,\n )\n elif kind == \"deep\":\n model = Deep4Net(\n n_channels,\n n_classes,\n input_window_samples=window_len_samples,\n final_conv_length=\"auto\",\n n_filters_time=25,\n n_filters_spat=25,\n filter_time_length=10,\n pool_time_length=3,\n pool_time_stride=3,\n n_filters_2=50,\n filter_length_2=10,\n n_filters_3=100,\n filter_length_3=10,\n n_filters_4=200,\n filter_length_4=10,\n first_pool_mode=\"max\",\n later_pool_mode=\"max\",\n drop_prob=0.5,\n split_first_layer=True,\n batch_norm=True,\n batch_norm_alpha=0.1,\n stride_before_pool=False,\n )\n else:\n raise ValueError\n\n if cuda:\n model.cuda()\n\n optimizer = optim.Adam(model.parameters())\n loss = nn.CrossEntropyLoss()\n\n return model, loss, optimizer\n\n\ndef run_training(model, dataloader, loss, optimizer, n_epochs=1, cuda=False):\n \"\"\"Run training loop.\n\n Parameters\n ----------\n model : torch.nn.Module\n Model to train.\n dataloader : torch.utils.data.Dataloader\n Data loader which will serve examples to the model during training.\n loss :\n Loss function.\n optimizer :\n Optimizer.\n n_epochs : int\n Number of epochs to train the model for.\n cuda : bool\n If True, move X and y to CUDA device.\n\n Returns\n -------\n model : torch.nn.Module\n Trained model.\n \"\"\"\n for i in range(n_epochs):\n loss_vals = list()\n for X, y, _ in dataloader:\n model.train()\n model.zero_grad()\n\n y = y.long()\n if cuda:\n X, y = X.cuda(), y.cuda()\n\n loss_val = loss(model(X), y)\n loss_vals.append(loss_val.item())\n\n loss_val.backward()\n optimizer.step()\n\n print(f\"Epoch {i + 1} - mean training loss: {np.mean(loss_vals)}\")\n\n return model"
]
},
{
Expand Down Expand Up @@ -105,7 +105,7 @@
},
"outputs": [],
"source": [
"all_results = list()\nfor (\n i,\n preload,\n n_recordings,\n win_len_s,\n n_epochs,\n batch_size,\n model_kind,\n num_workers,\n pin_memory,\n cuda,\n) in product(\n range(N_REPETITIONS),\n PRELOAD,\n N_RECORDINGS,\n WINDOW_LEN_S,\n N_EPOCHS,\n BATCH_SIZE,\n MODEL,\n NUM_WORKERS,\n PIN_MEMORY,\n CUDA,\n):\n results = {\n \"repetition\": i,\n \"preload\": preload,\n \"n_recordings\": n_recordings,\n \"win_len_s\": win_len_s,\n \"n_epochs\": n_epochs,\n \"batch_size\": batch_size,\n \"model_kind\": model_kind,\n \"num_workers\": num_workers,\n \"pin_memory\": pin_memory,\n \"cuda\": cuda,\n }\n print(f\"\\nRepetition {i + 1}/{N_REPETITIONS}:\\n{results}\")\n\n # Load the dataset\n data_loading_start = time.time()\n dataset = load_example_data(preload, win_len_s, n_recordings=n_recordings)\n data_loading_end = time.time()\n\n # Create the data loader\n training_setup_start = time.time()\n dataloader = DataLoader(\n dataset,\n batch_size=batch_size,\n shuffle=False,\n pin_memory=pin_memory,\n num_workers=num_workers,\n worker_init_fn=None,\n )\n\n # Instantiate model and optimizer\n n_channels = len(dataset.datasets[0].windows.ch_names)\n n_times = len(dataset.datasets[0].windows.times)\n n_classes = 2\n model, loss, optimizer = create_example_model(\n n_channels, n_classes, n_times, kind=model_kind, cuda=cuda\n )\n training_setup_end = time.time()\n\n # Start training loop\n model_training_start = time.time()\n trained_model = run_training(\n model, dataloader, loss, optimizer, n_epochs=n_epochs, cuda=cuda\n )\n model_training_end = time.time()\n\n del dataset, model, loss, optimizer, trained_model\n\n # Record timing results\n results[\"data_preparation\"] = data_loading_end - data_loading_start\n results[\"training_setup\"] = training_setup_end - training_setup_start\n results[\"model_training\"] = model_training_end - model_training_start\n all_results.append(results)"
"all_results = list()\nfor (\n i,\n preload,\n n_recordings,\n win_len_s,\n n_epochs,\n batch_size,\n model_kind,\n num_workers,\n pin_memory,\n cuda,\n) in product(\n range(N_REPETITIONS),\n PRELOAD,\n N_RECORDINGS,\n WINDOW_LEN_S,\n N_EPOCHS,\n BATCH_SIZE,\n MODEL,\n NUM_WORKERS,\n PIN_MEMORY,\n CUDA,\n):\n results = {\n \"repetition\": i,\n \"preload\": preload,\n \"n_recordings\": n_recordings,\n \"win_len_s\": win_len_s,\n \"n_epochs\": n_epochs,\n \"batch_size\": batch_size,\n \"model_kind\": model_kind,\n \"num_workers\": num_workers,\n \"pin_memory\": pin_memory,\n \"cuda\": cuda,\n }\n print(f\"\\nRepetition {i + 1}/{N_REPETITIONS}:\\n{results}\")\n\n # Load the dataset\n data_loading_start = time.time()\n dataset = load_example_data(preload, win_len_s, n_recordings=n_recordings)\n data_loading_end = time.time()\n\n # Create the data loader\n training_setup_start = time.time()\n dataloader = DataLoader(\n dataset,\n batch_size=batch_size,\n shuffle=False,\n pin_memory=pin_memory,\n num_workers=num_workers,\n worker_init_fn=None,\n )\n # Instantiate model and optimizer\n n_channels = dataset[0][0].shape[0]\n n_times = dataset[0][0].shape[1]\n n_classes = 2\n model, loss, optimizer = create_example_model(\n n_channels, n_classes, n_times, kind=model_kind, cuda=cuda\n )\n training_setup_end = time.time()\n\n # Start training loop\n model_training_start = time.time()\n trained_model = run_training(\n model, dataloader, loss, optimizer, n_epochs=n_epochs, cuda=cuda\n )\n model_training_end = time.time()\n\n del dataset, model, loss, optimizer, trained_model\n\n # Record timing results\n results[\"data_preparation\"] = data_loading_end - data_loading_start\n results[\"training_setup\"] = training_setup_end - training_setup_start\n results[\"model_training\"] = model_training_end - model_training_start\n all_results.append(results)"
]
},
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ def load_example_data(preload, window_len_s, n_recordings=10):
# Drop bad epochs
# XXX: This could be parallelized.
# XXX: Also, this could be implemented in the Dataset object itself.
for ds in windows_ds.datasets:
ds.windows.drop_bad()
assert ds.windows.preload == preload
# We don't support drop_bad since the last version braindecode,
# to optimize the dataset speed. If you know how to fix, please open a PR.
# for ds in windows_ds.datasets:
# ds.raw.drop_bad()
# assert ds.raw.preload == preload

return windows_ds

Expand Down Expand Up @@ -188,7 +190,6 @@ def create_example_model(
first_pool_mode="max",
later_pool_mode="max",
drop_prob=0.5,
double_time_convs=False,
split_first_layer=True,
batch_norm=True,
batch_norm_alpha=0.1,
Expand Down Expand Up @@ -335,10 +336,9 @@ def run_training(model, dataloader, loss, optimizer, n_epochs=1, cuda=False):
num_workers=num_workers,
worker_init_fn=None,
)

# Instantiate model and optimizer
n_channels = len(dataset.datasets[0].windows.ch_names)
n_times = len(dataset.datasets[0].windows.times)
n_channels = dataset[0][0].shape[0]
n_times = dataset[0][0].shape[1]
n_classes = 2
model, loss, optimizer = create_example_model(
n_channels, n_classes, n_times, kind=model_kind, cuda=cuda
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified dev/_images/sphx_glr_plot_benchmark_preprocessing_001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dev/_images/sphx_glr_plot_benchmark_preprocessing_thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 770fc3d

Please sign in to comment.