-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
166 lines (157 loc) · 3.92 KB
/
train.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
164
165
166
# coding:utf-8
import os
import timeit
import tensorflow as tf
from tensorflow.python.keras.api._v2.keras import backend as K
from core.get_model import create_EEGNet, create_TSGLEEGNet
from core.training import crossValidate, gridSearch
from core.dataloaders import RawDataloader
from core.generators import RawGenerator
from core.splits import StratifiedKFold, AllTrain
from core.metrics import Kappa
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
print(gpus)
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
K.set_image_data_format('channels_last')
srate = 250
num_classes = 4
batch_size = 10
def time_format(secs):
mins = int(secs // 60)
secs %= 60
hours = mins // 60
mins %= 60
days = hours // 24
hours %= 24
return days, hours, mins, secs
train_datapath = os.path.join('data', 'A', 'TrainSet', 'example_data.mat')
test_datapath = os.path.join('data', 'A', 'TestSet', 'example_data.mat')
datadir = None
# train_datapath = None
# test_datapath = None
# datadir = os.path.join('data', 'A')
start = timeit.default_timer()
# Change kFold, epochs and patience to get higher acc
crossValidate(
create_TSGLEEGNet,
dataLoader=RawDataloader,
splitMethod=AllTrain,
dataGent=RawGenerator,
traindata_filepath=train_datapath,
testdata_filepath=test_datapath,
datadir=datadir,
kFold=5,
# If use 'traindata_filepath' or 'testdata_filepath', set subs=[1]
subs=[1],
shuffle=True,
norm_mode='z-score',
preserve_initfile=False,
reinit=True,
# If needed, turn cropping on.
# But its accuracy evaluation indicator is not clear.
cropping=False,
cpt=0.5,
step=int(0.2 * srate),
max_crop=6,
beg=0.,
end=4.,
srate=srate,
batch_size=batch_size,
epochs=1200,
patience=300)(
nClasses=num_classes,
Chans=22,
F=16,
D=10,
Ns=20,
l1=1e-4,
l21=7.5e-5,
tl1=2.5e-6,
metrics=[
'accuracy',
Kappa(num_classes, sparse_labels=True)
],
lrate=1e-3,
)
# parameters = {
# 'l1': {
# '1': [2.5e-5],
# '2': [1e-3],
# '3': [1e-4],
# '4': [7.5e-5],
# '5': [2.5e-5],
# '6': [5e-5],
# '7': [7.5e-5],
# '8': [1e-3],
# '9': [7.5e-5]
# },
# 'l21':
# {
# '1': [2.5e-5],
# '2': [1e-4],
# '3': [7.5e-5],
# '4': [1e-4],
# '5': [1e-4],
# '6': [1e-4],
# '7': [1e-4],
# '8': [1e-4],
# '9': [1e-4]
# },
# 'tl1': {
# '1': [7.5e-6],
# '2': [7.5e-6],
# '3': [2.5e-6],
# '4': [1e-5],
# '5': [7.5e-6],
# '6': [1e-6],
# '7': [2.5e-6],
# '8': [5e-6],
# '9': [2.5e-5]
# }
# }
# OR
# parameters = {
# 'l1': {
# # '1': [5e-3],
# '2':
# list(np.linspace(1e-2, 2.5e-3, 4)) +
# list(np.linspace(1e-3, 2.5e-4, 4)) +
# list(np.linspace(1e-4, 2.5e-5, 4)) + [1e-5, 0.],
# # '3': [7.5e-4]
# },
# 'l21': [1e-3],
# 'tl1': {
# # '1': [7.5e-4],
# '2': [2.5e-5],
# # '3': [7.5e-4]
# }
# }
# # OR mix them
# gridSearch(
# create_TSGLEEGNet,
# parameters,
# dataLoader=RawDataloader,
# splitMethod=AllTrain,
# dataGent=RawGenerator,
# traindata_filepath=train_datapath,
# testdata_filepath=test_datapath,
# datadir=datadir,
# kFold=5,
# subs=range(2, 3),
# shuffle=True,
# norm_mode='z-score',
# preserve_initfile=False,
# reinit=True,
# cpt=0.5,
# step=int(0.2 * srate),
# max_crop=6,
# beg=0.,
# end=4.,
# srate=srate,
# epochs=1200, # change them
# patience=300)(4, Chans=60, F=16, D=10, Ns=20)
end = timeit.default_timer()
print("Time used: {0:0>2d}d {1:0>2d}h {2:0>2d}m {3:.4f}s".format(
*time_format(end - start)))