-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_multiclass_supervised.py
353 lines (310 loc) · 11.2 KB
/
run_multiclass_supervised.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import os
import argparse
import pickle
import torch
from tqdm import tqdm
import numpy as np
import torch.nn as nn
import pytorch_lightning as pl
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.strategies import DDPStrategy
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pyhealth.metrics import multiclass_metrics_fn
from model import (
SPaRCNet,
ContraWR,
CNNTransformer,
FFCL,
STTransformer,
BIOTClassifier,
)
from utils import TUEVLoader, HARLoader
class LitModel_finetune(pl.LightningModule):
def __init__(self, args, model):
super().__init__()
self.args = args
self.model = model
def training_step(self, batch, batch_idx):
X, y = batch
prod = self.model(X)
loss = nn.CrossEntropyLoss()(prod, y)
self.log("train_loss", loss)
return loss
def validation_step(self, batch, batch_idx):
X, y = batch
with torch.no_grad():
convScore = self.model(X)
step_result = convScore.cpu().numpy()
step_gt = y.cpu().numpy()
return step_result, step_gt
def validation_epoch_end(self, val_step_outputs):
result = []
gt = np.array([])
for out in val_step_outputs:
result.append(out[0])
gt = np.append(gt, out[1])
result = np.concatenate(result, axis=0)
result = multiclass_metrics_fn(
gt, result, metrics=["accuracy", "cohen_kappa", "f1_weighted"]
)
self.log("val_acc", result["accuracy"], sync_dist=True)
self.log("val_cohen", result["cohen_kappa"], sync_dist=True)
self.log("val_f1", result["f1_weighted"], sync_dist=True)
print(result)
def test_step(self, batch, batch_idx):
X, y = batch
with torch.no_grad():
convScore = self.model(X)
step_result = convScore.cpu().numpy()
step_gt = y.cpu().numpy()
return step_result, step_gt
def test_epoch_end(self, test_step_outputs):
result = []
gt = np.array([])
for out in test_step_outputs:
result.append(out[0])
gt = np.append(gt, out[1])
result = np.concatenate(result, axis=0)
result = multiclass_metrics_fn(
gt, result, metrics=["accuracy", "cohen_kappa", "f1_weighted"]
)
self.log("test_acc", result["accuracy"], sync_dist=True)
self.log("test_cohen", result["cohen_kappa"], sync_dist=True)
self.log("test_f1", result["f1_weighted"], sync_dist=True)
return result
def configure_optimizers(self):
optimizer = torch.optim.Adam(
self.model.parameters(),
lr=self.args.lr,
weight_decay=self.args.weight_decay,
)
return [optimizer] # , [scheduler]
def prepare_TUEV_dataloader(args):
# set random seed
seed = 4523
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
root = "/srv/local/data/TUH/tuh_eeg_events/v2.0.0/edf"
train_files = os.listdir(os.path.join(root, "processed_train"))
train_sub = list(set([f.split("_")[0] for f in train_files]))
print("train sub", len(train_sub))
test_files = os.listdir(os.path.join(root, "processed_eval"))
val_sub = np.random.choice(train_sub, size=int(
len(train_sub) * 0.1), replace=False)
train_sub = list(set(train_sub) - set(val_sub))
val_files = [f for f in train_files if f.split("_")[0] in val_sub]
train_files = [f for f in train_files if f.split("_")[0] in train_sub]
# prepare training and test data loader
train_loader = torch.utils.data.DataLoader(
TUEVLoader(
os.path.join(
root, "processed_train"), train_files, args.sampling_rate
),
batch_size=args.batch_size,
shuffle=True,
drop_last=True,
num_workers=args.num_workers,
persistent_workers=True,
)
test_loader = torch.utils.data.DataLoader(
TUEVLoader(
os.path.join(
root, "processed_eval"), test_files, args.sampling_rate
),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
persistent_workers=True,
)
val_loader = torch.utils.data.DataLoader(
TUEVLoader(
os.path.join(
root, "processed_train"), val_files, args.sampling_rate
),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
persistent_workers=True,
)
print(len(train_files), len(val_files), len(test_files))
print(len(train_loader), len(val_loader), len(test_loader))
return train_loader, test_loader, val_loader
def prepare_HAR_dataloader(args):
# set random seed
seed = 12345
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
root = "/srv/local/data/HAR/processed/"
train_files = os.listdir(os.path.join(root, "train"))
test_files = os.listdir(os.path.join(root, "test"))
val_files = os.listdir(os.path.join(root, "val"))
# prepare training and test data loader
train_loader = torch.utils.data.DataLoader(
HARLoader(os.path.join(root, "train"),
train_files, args.sampling_rate),
batch_size=args.batch_size,
shuffle=True,
drop_last=True,
num_workers=args.num_workers,
persistent_workers=True,
)
test_loader = torch.utils.data.DataLoader(
HARLoader(os.path.join(root, "test"), test_files, args.sampling_rate),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
persistent_workers=True,
)
val_loader = torch.utils.data.DataLoader(
HARLoader(os.path.join(root, "val"), val_files, args.sampling_rate),
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
persistent_workers=True,
)
print(len(train_files), len(val_files), len(test_files))
print(len(train_loader), len(val_loader), len(test_loader))
return train_loader, test_loader, val_loader
def supervised(args):
# get data loaders
if args.dataset == "TUEV":
train_loader, test_loader, val_loader = prepare_TUEV_dataloader(args)
else:
raise NotImplementedError
# define the model
if args.model == "SPaRCNet":
model = SPaRCNet(
in_channels=args.in_channels,
sample_length=int(args.sample_length * args.sampling_rate),
n_classes=args.n_classes,
block_layers=4,
growth_rate=16,
bn_size=16,
drop_rate=0.5,
conv_bias=True,
batch_norm=True,
)
elif args.model == "ContraWR":
model = ContraWR(
in_channels=args.in_channels,
n_classes=args.n_classes,
fft=args.token_size,
steps=args.hop_length // 5,
)
elif args.model == "CNNTransformer":
model = CNNTransformer(
in_channels=args.in_channels,
n_classes=args.n_classes,
fft=args.sampling_rate,
steps=args.hop_length // 5,
dropout=0.2,
nhead=4,
emb_size=256,
n_segments=4 if args.dataset == "HAR" else 5,
)
elif args.model == "FFCL":
model = FFCL(
in_channels=args.in_channels,
n_classes=args.n_classes,
fft=args.token_size,
steps=args.hop_length // 5,
sample_length=int(args.sample_length * args.sampling_rate),
shrink_steps=16 if args.dataset == "HAR" else 20,
)
elif args.model == "STTransformer":
model = STTransformer(
emb_size=256,
depth=4,
n_classes=args.n_classes,
channel_legnth=int(
args.sampling_rate * args.sample_length
), # (sampling_rate * duration)
n_channels=args.in_channels,
)
elif args.model == "BIOT":
model = BIOTClassifier(
n_classes=args.n_classes,
# set the n_channels according to the pretrained model if necessary
n_channels=args.in_channels,
n_fft=args.token_size,
hop_length=args.hop_length,
)
if args.pretrain_model_path and (args.sampling_rate == 200):
model.biot.load_state_dict(torch.load(args.pretrain_model_path))
print(f"load pretrain model from {args.pretrain_model_path}")
else:
raise NotImplementedError
lightning_model = LitModel_finetune(args, model)
# logger and callbacks
version = f"{args.dataset}-{args.model}-{args.lr}-{args.batch_size}-{args.sampling_rate}-{args.token_size}-{args.hop_length}"
logger = TensorBoardLogger(
save_dir="./",
version=version,
name="log",
)
early_stop_callback = EarlyStopping(
monitor="val_cohen", patience=5, verbose=False, mode="max"
)
trainer = pl.Trainer(
devices=[0],
accelerator="gpu",
strategy=DDPStrategy(find_unused_parameters=False),
auto_select_gpus=True,
benchmark=True,
enable_checkpointing=True,
logger=logger,
max_epochs=args.epochs,
callbacks=[early_stop_callback],
)
# train the model
trainer.fit(
lightning_model, train_dataloaders=train_loader, val_dataloaders=val_loader
)
# test the model
pretrain_result = trainer.test(
model=lightning_model, ckpt_path="best", dataloaders=test_loader
)[0]
print(pretrain_result)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--epochs", type=int, default=100,
help="number of epochs")
parser.add_argument("--lr", type=float, default=1e-3, help="learning rate")
parser.add_argument("--weight_decay", type=float,
default=1e-5, help="weight decay")
parser.add_argument("--batch_size", type=int,
default=512, help="batch size")
parser.add_argument("--num_workers", type=int,
default=32, help="number of workers")
parser.add_argument("--dataset", type=str, default="TUAB", help="dataset")
parser.add_argument(
"--model", type=str, default="SPaRCNet", help="which supervised model to use"
)
parser.add_argument(
"--in_channels", type=int, default=12, help="number of input channels"
)
parser.add_argument(
"--sample_length", type=float, default=10, help="length (s) of sample"
)
parser.add_argument(
"--n_classes", type=int, default=1, help="number of output classes"
)
parser.add_argument(
"--sampling_rate", type=int, default=200, help="sampling rate (r)"
)
parser.add_argument("--token_size", type=int,
default=200, help="token size (t)")
parser.add_argument(
"--hop_length", type=int, default=100, help="token hop length (t - p)"
)
parser.add_argument(
"--pretrain_model_path", type=str, default="", help="pretrained model path"
)
args = parser.parse_args()
print(args)
supervised(args)