-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlinear_baseline.py
163 lines (127 loc) · 5.95 KB
/
linear_baseline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Example experiment for a linear baseline method."""
import glob
import json
import logging
import os
# set gpu private
os.environ['TF_GPU_THREAD_MODE'] = 'gpu_private'
import tensorflow as tf
import sys
# add base path to sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
import scipy.stats
import numpy as np
from task2_regression.models.linear import simple_linear_model, pearson_loss_cut, pearson_metric_cut, pearson_metric_cut_non_averaged
from util.dataset_generator import DataGenerator, create_tf_dataset
def evaluate_model(model, test_dict):
"""Evaluate a model.
Parameters
----------
model: tf.keras.Model
Model to evaluate.
test_dict: dict
Mapping between a subject and a tf.data.Dataset containing the test
set for the subject.
Returns
-------
dict
Mapping between a subject and the loss/evaluation score on the test set
"""
evaluation = {}
for subject, ds_test in test_dict.items():
logging.info(f"Scores for subject {subject}:")
# evaluate model
ds = [x for x in ds_test]
eeg = tf.concat([ x[0] for x in ds], axis=0)
labels =tf.concat([ x[1] for x in ds], axis=0)
reconstructions = model.predict(eeg)
correlations = np.squeeze(pearson_metric_cut_non_averaged(labels, reconstructions))
# calculate pearson correlation per band
results = model.evaluate(ds_test, verbose=2)
metrics = model.metrics_names
evaluation[subject] = dict(zip(metrics, results))
evaluation[subject]["pearson_correlation_per_band"] = np.mean(correlations, axis=0).tolist()
# metrics = model.metrics_names
# evaluation[subject] = dict(zip(metrics, results))
return evaluation
if __name__ == "__main__":
# Parameters
# Length of the decision window
fs = 64
window_length = 60 * fs # 10 seconds
# Hop length between two consecutive decision windows
hop_length = 30*fs
epochs = 100
patience = 5
batch_size = 64
only_evaluate = True
# Get the path to the config gile
experiments_folder = os.path.dirname(__file__)
task_folder = os.path.dirname(experiments_folder)
util_folder = os.path.join(os.path.dirname(task_folder), "util")
config_path = os.path.join(util_folder, 'config.json')
# Load the config
with open(config_path) as fp:
config = json.load(fp)
# Provide the path of the dataset
# which is split already to train, val, test
data_folder = os.path.join(config["dataset_folder"],config["derivatives_folder"], config["split_folder"])
stimulus_features = ["mel"]
features = ["eeg"] + stimulus_features
# Create a directory to store (intermediate) results
results_folder = os.path.join(experiments_folder, "results_linear_baseline")
os.makedirs(results_folder, exist_ok=True)
# train a sub dependent model for each sub
# Create a dataset generator for each training subject
# Get all different subjects from the training set
all_subs = list(
set([os.path.basename(x).split("_-_")[1] for x in glob.glob(os.path.join(data_folder, "train_-_*"))]))
# create a simple linear model
model = simple_linear_model(integration_window = int(fs*0.25), nb_filters=10)
model.summary()
model_path = os.path.join(results_folder, f"model.h5")
training_log_filename = f"training_log.csv"
results_filename = f'eval.json'
if only_evaluate:
# load weights
model.load_weights(model_path)
else:
train_files = [x for x in glob.glob(os.path.join(data_folder, "train_-_*")) if os.path.basename(x).split("_-_")[-1].split(".")[0] in features ]
# Create list of numpy array files
train_generator = DataGenerator(train_files, window_length)
dataset_train = create_tf_dataset(train_generator, window_length, None, hop_length, batch_size, data_types=(tf.float32, tf.float32), feature_dims=(64, 10))
# Create the generator for the validation set
val_files = [x for x in glob.glob(os.path.join(data_folder, "val_-_*")) if os.path.basename(x).split("_-_")[-1].split(".")[0] in features]
val_generator = DataGenerator(val_files, window_length)
dataset_val = create_tf_dataset(val_generator, window_length, None, hop_length, batch_size, data_types=(tf.float32, tf.float32), feature_dims=(64, 10))
# Train the model
model.fit(
dataset_train,
epochs=epochs,
validation_data=dataset_val,
callbacks=[
tf.keras.callbacks.ModelCheckpoint(model_path, save_best_only=True),
tf.keras.callbacks.CSVLogger(os.path.join(results_folder, training_log_filename)),
tf.keras.callbacks.EarlyStopping(patience=patience, restore_best_weights=True),
],
workers = tf.data.AUTOTUNE,
use_multiprocessing=True
)
# Evaluate the model on test set
# Create a dataset generator for each test subject
test_files = [x for x in glob.glob(os.path.join(data_folder, "test_-_*")) if os.path.basename(x).split("_-_")[-1].split(".")[0] in features]
# Get all different subjects from the test set
subjects = list(set([os.path.basename(x).split("_-_")[1] for x in test_files]))
datasets_test = {}
# Create a generator for each subject
for sub in subjects:
files_test_sub = [f for f in test_files if sub in os.path.basename(f)]
test_generator = DataGenerator(files_test_sub, window_length)
datasets_test[sub] = create_tf_dataset(test_generator, window_length, None, hop_length, batch_size=1, data_types=(tf.float32, tf.float32), feature_dims=(64, 10))
# Evaluate the model
evaluation = evaluate_model(model, datasets_test)
# We can save our results in a json encoded file
results_path = os.path.join(results_folder, results_filename)
with open(results_path, "w") as fp:
json.dump(evaluation, fp)
logging.info(f"Results saved at {results_path}")