-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunCVDA.py
More file actions
242 lines (223 loc) · 7.71 KB
/
RunCVDA.py
File metadata and controls
242 lines (223 loc) · 7.71 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import torch
import argparse
import itertools
import subprocess
import time
from AllFnc.utilities import (
restricted_float,
positive_float,
positive_int_nozero,
positive_int,
makeGrid,
)
print_string = True
custom_env = True
def run_single_training(arg_dict, print_string = False, custom_environment = None):
# create args string
arg_str = " -D " + arg_dict["dataPath"] + \
" -p " + arg_dict["pipelineToEval"] + \
" -t " + arg_dict["taskToEval"] + \
" -m " + arg_dict["modelToEval"] + \
" -o " + str(arg_dict["outer"]) + \
" -i " + str(arg_dict["inner"]) + \
" -d " + str(arg_dict["downsample"]) + \
" -z " + str(arg_dict["z_score"]) + \
" -r " + str(arg_dict["rem_interp"]) + \
" -b " + str(arg_dict["batchsize"]) + \
" -O " + str(arg_dict["overlap"]) + \
" -l " + str(arg_dict["lr"]) + \
" -a " + str(arg_dict["adamdecay"]) + \
" -w " + str(arg_dict["window"]) + \
" -c " + str(arg_dict["csp"]) + \
" -f " + str(arg_dict["csp_filters"])
if arg_dict["augmentations"] is None:
arg_str += " -A " + str(arg_dict["augmentations"])
else:
arg_str += " -A " + "\"" + str(arg_dict["augmentations"]) + "\""
arg_str += " -W " + str(arg_dict["workers"]) + \
" -v " + str(arg_dict["verbose"]) + \
" -g " + str(arg_dict["gpudevice"]) + \
" -s " + str(arg_dict["seed"])
# print argument string if needed
if print_string:
print(arg_str)
p = subprocess.run(
#"python3 RunSingleTraining.py" + arg_str,
"python3 RunSingleTrainingFullPD.py" + arg_str,
shell = True,
check = True,
timeout = 7200,
env = custom_environment
)
return
if __name__ == '__main__':
if custom_env:
env = os.environ.copy()
env["OMP_NUM_THREADS"] = "16" # export OMP_NUM_THREADS=1
env["OPENBLAS_NUM_THREADS"] = "16" # export OPENBLAS_NUM_THREADS=1
env["MKL_NUM_THREADS"] = "16" # export MKL_NUM_THREADS=1
env["VECLIB_MAXIMUM_THREADS"] = "16" # export VECLIB_MAXIMUM_THREADS=1
env["NUMEXPR_NUM_THREADS"] = "16" # export NUMEXPR_NUM_THREADS=1
env["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
else:
env = None
help_d = """
RunCVDA is a copy of RunCV used to run a 10-10 N-LNSO
for custom combinations of (models, data augmentation).
"""
parser = argparse.ArgumentParser(description=help_d)
parser.add_argument(
"-d",
"--datapath",
dest = "dataPath",
metavar = "datasets path",
type = str,
nargs = '?',
required = False,
default = None,
help = """
The dataset path. It must point to a directory containing a set of
subdirecotries, with all the preprocessed EEGs stored as pickle files.
Each subfolder is expected to contain the EEGs preprocessed with a
specific preprocessing pipeline.
So, the path should look like.
root_path
| + pipeline_1
| | + EEG_1
| | + EEG_2
| | + EEG_3
| | + ...
| | + EEG_n
| + pipeline_2
| | + EEG_1
| | + ...
| | + EEG_n
| + ...
| + pipeline_n
""",
)
parser.add_argument(
"-s",
"--start",
dest = "start_idx",
metavar = "starting index",
type = positive_int,
nargs = '?',
required = False,
default = 0,
help = """
The starting index.
It can be used to restart the trainings if one failed for some reasons.
"""
)
parser.add_argument(
"-e",
"--end",
dest = "end_idx",
metavar = "ending index",
type = positive_int,
nargs = '?',
required = False,
default = 0,
help = """
The ending index.
It can be used to stop the trainings at specific positions.
You can use it if you want to split the total number of trainings on multiple
GPUs or you want to run multiple training in parallel on the same GPU
"""
)
# basically we overwrite the dataPath if something was given
args = vars(parser.parse_args())
dataPathInput = args['dataPath']
StartIdx = args['start_idx']
EndIdx = args['end_idx']
aug_list = [
'flip_horizontal',
'flip_vertical',
'add_band_noise',
'add_eeg_artifact',
'add_noise_snr',
'channel_dropout',
'masking',
'warp_signal',
'random_FT_phase',
'phase_swap',
]
models = [
"xeegnet", "eegnet", "shallownet", "eegconformer",
"deepconvnet", "transformeeg", "resnet", "atcnet"
]
augmns = [
["add_eeg_artifact", "phase_swap"],
["channel_dropout", "add_band_noise"],
["channel_dropout", "masking"],
["add_eeg_artifact", "add_band_noise"],
["phase_swap", "add_band_noise"],
["masking", "flip_horizontal"],
["random_FT_phase", "add_eeg_artifact"],
["phase_swap", "masking"]
]
arg_list = []
for model, aug in zip(models,augmns):
PIPE_args = {
"dataPath": ['/data/delpup/datasets/eegpickle/'],
"pipelineToEval": ["ica"],
"taskToEval": ["parkinson"],
"modelToEval": [model],
"downsample": [True],
"z_score": [True],
"rem_interp": [True],
"batchsize": [64],
"window": [16.0],
"overlap": [0.25],
"lr": [2.5e-4],
"adamdecay": [0.0],
"workers": [0],
"verbose": [False],
"csp": [False],
"csp_filters": [10],
"augmentations": [aug],
"gpudevice": ["cuda:1"],
"seed": [42], #[42],
"inner": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"outer": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
if dataPathInput is not None:
PIPE_args['dataPath'] = [dataPathInput]
arg_list += makeGrid(PIPE_args)
# print the final dictionary
print("running trainings with the following set of parameters:")
print(" ")
for key in PIPE_args:
if key == "augmentations":
print(f"{key:15} ==> ", augmns)
elif key == "modelToEval":
print(f"{key:15} ==> ", models)
else:
print( f"{key:15} ==> {PIPE_args[key]}")
# Run each training in a sequential manner
N = len(arg_list)
print(f"the following setting requires to run {N:5} trainings")
if StartIdx>0:
print(f"Restart from training number {StartIdx:5}")
StartIdx = StartIdx - 1
if EndIdx>0:
print(f"Will end at training number {EndIdx:5}")
EndIdx = EndIdx - 1
if EndIdx>0 and StartIdx>0 and EndIdx<=StartIdx:
raise ValueError("ending index cannot be lower than the starting index")
for i in range(StartIdx, N):
if i==EndIdx:
print(f"reached end idx. Stopping simulation at training number {i+1:<5}")
break
print(f"running training number {i+1:<5} out of {N:5}")
Tstart = time.time()
run_single_training(arg_list[i], print_string, env)
Tend = time.time()
Total = int(Tend - Tstart)
print(f"training performed in {Total:<5} seconds")
print(f"Completed all {N:5} trainings")
# Just a reminder to keep your GPU cool
if (N-StartIdx)>1000:
print(f"...Is your GPU still alive?")