-
Notifications
You must be signed in to change notification settings - Fork 66
/
conformer_seed_1s_5fold.py
537 lines (435 loc) · 17.9 KB
/
conformer_seed_1s_5fold.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
"""
EEG conformer
Test SEED data 1 second
perform strict 5-fold cross validation
"""
import argparse
import os
gpus = [1]
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(map(str, gpus))
import numpy as np
import math
import glob
import random
import itertools
import datetime
import time
import datetime
import sys
import scipy.io
import torchvision.transforms as transforms
from torchvision.utils import save_image, make_grid
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torchsummary import summary
import torch.autograd as autograd
from torchvision.models import vgg19
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.nn.init as init
from torch.utils.data import Dataset
from PIL import Image
import torchvision.transforms as transforms
from sklearn.decomposition import PCA
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
from torch import nn
from torch import Tensor
from PIL import Image
from torchvision.transforms import Compose, Resize, ToTensor
from einops import rearrange, reduce, repeat
from einops.layers.torch import Rearrange, Reduce
import matplotlib.pyplot as plt
# from torch.utils.tensorboard import SummaryWriter
from torch.backends import cudnn
cudnn.benchmark = False
cudnn.deterministic = True
class PatchEmbedding(nn.Module):
def __init__(self, emb_size=40):
super().__init__()
self.eegnet = nn.Sequential(
nn.Conv2d(1, 8, (1, 125), (1, 1)),
nn.BatchNorm2d(8),
nn.Conv2d(8, 16, (22, 1), (1, 1)),
nn.BatchNorm2d(16),
nn.ELU(),
nn.AvgPool2d((1, 4), (1, 4)),
nn.Dropout(0.5),
nn.Conv2d(16, 16, (1, 16), (1, 1)),
nn.BatchNorm2d(16),
nn.ELU(),
nn.AvgPool2d((1, 8), (1, 8)),
nn.Dropout2d(0.5)
)
self.shallownet = nn.Sequential(
nn.Conv2d(1, 40, (1, 25), (1, 1)),
nn.Conv2d(40, 40, (62, 1), (1, 1)),
nn.BatchNorm2d(40),
nn.ELU(),
nn.AvgPool2d((1, 75), (1, 15)),
nn.Dropout(0.5),
)
self.projection = nn.Sequential(
nn.Conv2d(40, emb_size, (1, 1), stride=(1, 1)), # 5 is better than 1
Rearrange('b e (h) (w) -> b (h w) e'),
)
def forward(self, x: Tensor) -> Tensor:
b, _, _, _ = x.shape
x = self.shallownet(x)
x = self.projection(x)
return x
class MultiHeadAttention(nn.Module):
def __init__(self, emb_size, num_heads, dropout):
super().__init__()
self.emb_size = emb_size
self.num_heads = num_heads
self.keys = nn.Linear(emb_size, emb_size)
self.queries = nn.Linear(emb_size, emb_size)
self.values = nn.Linear(emb_size, emb_size)
self.att_drop = nn.Dropout(dropout)
self.projection = nn.Linear(emb_size, emb_size)
def forward(self, x: Tensor, mask: Tensor = None) -> Tensor:
queries = rearrange(self.queries(x), "b n (h d) -> b h n d", h=self.num_heads)
keys = rearrange(self.keys(x), "b n (h d) -> b h n d", h=self.num_heads)
values = rearrange(self.values(x), "b n (h d) -> b h n d", h=self.num_heads)
energy = torch.einsum('bhqd, bhkd -> bhqk', queries, keys) # batch, num_heads, query_len, key_len
if mask is not None:
fill_value = torch.finfo(torch.float32).min
energy.mask_fill(~mask, fill_value)
scaling = self.emb_size ** (1 / 2)
att = F.softmax(energy / scaling, dim=-1)
att = self.att_drop(att)
out = torch.einsum('bhal, bhlv -> bhav ', att, values)
out = rearrange(out, "b h n d -> b n (h d)")
out = self.projection(out)
return out
class ResidualAdd(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x, **kwargs):
res = x
x = self.fn(x, **kwargs)
x += res
return x
class FeedForwardBlock(nn.Sequential):
def __init__(self, emb_size, expansion, drop_p):
super().__init__(
nn.Linear(emb_size, expansion * emb_size),
nn.GELU(),
nn.Dropout(drop_p),
nn.Linear(expansion * emb_size, emb_size),
)
class GELU(nn.Module):
def forward(self, input: Tensor) -> Tensor:
return input*0.5*(1.0+torch.erf(input/math.sqrt(2.0)))
class TransformerEncoderBlock(nn.Sequential):
def __init__(self,
emb_size,
num_heads=5,
drop_p=0.5,
forward_expansion=4,
forward_drop_p=0.5):
super().__init__(
ResidualAdd(nn.Sequential(
nn.LayerNorm(emb_size),
MultiHeadAttention(emb_size, num_heads, drop_p),
nn.Dropout(drop_p)
)),
ResidualAdd(nn.Sequential(
nn.LayerNorm(emb_size),
FeedForwardBlock(
emb_size, expansion=forward_expansion, drop_p=forward_drop_p),
nn.Dropout(drop_p)
)
))
class TransformerEncoder(nn.Sequential):
def __init__(self, depth, emb_size):
super().__init__(*[TransformerEncoderBlock(emb_size) for _ in range(depth)])
class ClassificationHead(nn.Sequential):
def __init__(self, emb_size, n_classes):
super().__init__()
self.cov = nn.Sequential(
nn.Conv1d(190, 1, 1, 1),
nn.LeakyReLU(0.2),
nn.Dropout(0.5)
)
self.clshead = nn.Sequential(
Reduce('b n e -> b e', reduction='mean'),
nn.LayerNorm(emb_size),
nn.Linear(emb_size, n_classes)
)
self.clshead_fc = nn.Sequential(
Reduce('b n e -> b e', reduction='mean'),
nn.LayerNorm(emb_size),
nn.Linear(emb_size, 32),
nn.ELU(),
nn.Dropout(0.5),
nn.Linear(32, n_classes)
)
self.fc = nn.Sequential(
nn.Linear(280, 32),
nn.ELU(),
nn.Dropout(0.3),
nn.Linear(32, 3)
)
def forward(self, x):
x = x.contiguous().view(x.size(0), -1)
out = self.fc(x)
return x, out
# ! Rethink the use of Transformer for EEG signal
class ViT(nn.Sequential):
def __init__(self, emb_size=40, depth=6, n_classes=4, **kwargs):
super().__init__(
PatchEmbedding(emb_size),
TransformerEncoder(depth, emb_size),
ClassificationHead(emb_size, n_classes)
)
class ExGAN():
def __init__(self, nsub, fold):
super(ExGAN, self).__init__()
self.batch_size = 200
self.n_epochs = 600 #1000
self.img_height = 22
self.img_width = 600
self.channels = 1
self.c_dim = 4
self.lr = 0.0002
self.b1 = 0.5
self.b2 = 0.999
self.alpha = 0.0002
self.dimension = (190, 50)
self.nSub = nsub
self.start_epoch = 0
self.root = '/Data/SEED/seed_syh/data_cv5fold/'
self.pretrain = False
self.log_write = open("/Code/CT/results/D_base_comp/seed/5-fold/real/log_subject%d_fold%d.txt" % (self.nSub, fold+1), "w")
self.img_shape = (self.channels, self.img_height, self.img_width)
self.Tensor = torch.cuda.FloatTensor
self.LongTensor = torch.cuda.LongTensor
self.criterion_l1 = torch.nn.L1Loss().cuda()
self.criterion_l2 = torch.nn.MSELoss().cuda()
self.criterion_cls = torch.nn.CrossEntropyLoss().cuda()
self.model = ViT().cuda()
self.model = nn.DataParallel(self.model, device_ids=[i for i in range(len(gpus))])
self.model = self.model.cuda()
self.centers = {}
def interaug(self, timg, label):
aug_data = []
aug_label = []
for cls4aug in range(3):
cls_idx = np.where(label == cls4aug)
tmp_data = timg[cls_idx]
tmp_label = label[cls_idx]
tmp_aug_data = np.zeros((int(self.batch_size / 3), 1, 62, 200))
for ri in range(int(self.batch_size / 3)):
for rj in range(8):
rand_idx = np.random.randint(0, tmp_data.shape[0], 8)
tmp_aug_data[ri, :, :, rj * 25:(rj + 1) * 25] = tmp_data[rand_idx[rj], :, :,
rj * 25:(rj + 1) * 25]
aug_data.append(tmp_aug_data)
aug_label.append(tmp_label[:int(self.batch_size / 3)])
aug_data = np.concatenate(aug_data)
aug_label = np.concatenate(aug_label)
aug_shuffle = np.random.permutation(len(aug_data))
aug_data = aug_data[aug_shuffle, :, :]
aug_label = aug_label[aug_shuffle]
aug_data = torch.from_numpy(aug_data).cuda()
aug_data = aug_data.float()
aug_label = torch.from_numpy(aug_label).cuda()
aug_label = aug_label.long()
return aug_data, aug_label
def get_source_data(self, fold):
self.all_data = np.load(self.root + 'S%d_session1.npy' % self.nSub, allow_pickle=True)
self.all_label = np.load(self.root + 'S%d_session1_label.npy' % self.nSub, allow_pickle=True)
self.train_data = []
self.train_label = []
self.test_data = []
self.test_label = []
for tri in range(np.shape(self.all_data)[0]):
tmp_tri = np.array(self.all_data[tri])
tmp_tri_label = np.array(self.all_label[tri])
one_fold_num = np.shape(tmp_tri)[0] // 5
tri_num = one_fold_num * 5
tmp_tri_idx = np.arange(tri_num)
test_idx = np.arange(one_fold_num * fold, one_fold_num * (fold+1))
train_idx = np.delete(tmp_tri_idx, test_idx)
self.train_data.append(tmp_tri[train_idx])
self.train_label.append(tmp_tri_label[train_idx])
self.test_data.append(tmp_tri[test_idx])
self.test_label.append(tmp_tri_label[test_idx])
self.train_data = np.concatenate(self.train_data)
self.train_data = np.expand_dims(self.train_data, axis=1)
self.train_label = np.concatenate(self.train_label)
self.test_data = np.concatenate(self.test_data)
self.test_data = np.expand_dims(self.test_data, axis=1)
self.test_label = np.concatenate(self.test_label)
shuffle_num = np.random.permutation(len(self.train_data))
self.train_data = self.train_data[shuffle_num, :, :, :]
self.train_label = self.train_label[shuffle_num]
# standardize
target_mean = np.mean(self.train_data)
target_std = np.std(self.train_data)
self.train_data = (self.train_data - target_mean) / target_std
self.test_data = (self.test_data - target_mean) / target_std
return self.train_data, self.train_label, self.test_data, self.test_label
def update_lr(self, optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def aug(self, img, label):
aug_data = []
aug_label = []
for cls4aug in range(4):
cls_idx = np.where(label == cls4aug + 1)
tmp_data = img[cls_idx]
tmp_label = label[cls_idx]
tmp_aug_data = np.zeros(tmp_data.shape)
for ri in range(tmp_data.shape[0]):
for rj in range(8):
rand_idx = np.random.randint(0, tmp_data.shape[0], 8)
tmp_aug_data[ri, :, :, rj * 125:(rj + 1) * 125] = tmp_data[rand_idx[rj], :, :, rj * 125:(rj + 1) * 125]
aug_data.append(tmp_aug_data)
aug_label.append(tmp_label)
aug_data = np.concatenate(aug_data)
aug_label = np.concatenate(aug_label)
aug_shuffle = np.random.permutation(len(aug_data))
aug_data = aug_data[aug_shuffle, :, :]
aug_label = aug_label[aug_shuffle]
return aug_data, aug_label
def update_centers(self, feature, label):
deltac = {}
count = {}
count[0] = 0
for i in range(len(label)):
l = label[i]
if l in deltac:
deltac[l] += self.centers[l]-feature[i]
else:
deltac[l] = self.centers[l]-feature[i]
if l in count:
count[l] += 1
else:
count[l] = 1
for ke in deltac.keys():
deltac[ke] = deltac[ke]/(count[ke]+1)
return deltac
def train(self, fold):
img, label, test_data, test_label = self.get_source_data(fold)
img = torch.from_numpy(img)
label = torch.from_numpy(label + 1)
dataset = torch.utils.data.TensorDataset(img, label)
self.dataloader = torch.utils.data.DataLoader(dataset=dataset, batch_size=self.batch_size, shuffle=True)
test_data = torch.from_numpy(test_data)
test_label = torch.from_numpy(test_label + 1)
test_dataset = torch.utils.data.TensorDataset(test_data, test_label)
self.test_dataloader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=self.batch_size, shuffle=True)
for i in range(self.c_dim):
self.centers[i] = torch.randn(self.dimension)
self.centers[i] = self.centers[i].cuda()
# Optimizers
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.lr, betas=(self.b1, self.b2))
test_data = Variable(test_data.type(self.Tensor))
test_label = Variable(test_label.type(self.LongTensor))
bestAcc = 0
averAcc = 0
num = 0
Y_true = 0
Y_pred = 0
# Train the cnn model
total_step = len(self.dataloader)
curr_lr = self.lr
for e in range(self.n_epochs):
in_epoch = time.time()
self.model.train()
for i, (img, label) in enumerate(self.dataloader):
img = Variable(img.cuda().type(self.Tensor))
# img = self.active_function(img)
label = Variable(label.cuda().type(self.LongTensor))
tok, outputs = self.model(img)
# Central loss
cen_feature = tok
cen_label = label
nplabela = cen_label.cpu().numpy()
loss = self.criterion_cls(outputs, label)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
out_epoch = time.time()
if (e + 1) % 1 == 0:
self.model.eval()
Tok, Cls = self.model(test_data)
loss_test = self.criterion_cls(Cls, test_label)
y_pred = torch.max(Cls, 1)[1]
acc = float((y_pred == test_label).cpu().numpy().astype(int).sum()) / float(test_label.size(0))
train_pred = torch.max(outputs, 1)[1]
train_acc = float((train_pred == label).cpu().numpy().astype(int).sum()) / float(label.size(0))
# print('The epoch is:', e, ' The accuracy is:', acc)
print('Epoch:', e,
' Train loss: %.4f' % loss.detach().cpu().numpy(),
' Test loss: %.4f' % loss_test.detach().cpu().numpy(),
' Train acc: %.4f' % train_acc,
' Test acc: %.4f' % acc)
self.log_write.write(str(e) + " " + str(acc) + "\n")
num = num + 1
averAcc = averAcc + acc
if acc > bestAcc:
bestAcc = acc
Y_true = test_label
Y_pred = y_pred
averAcc = averAcc / num
print('The average accuracy of fold%d is:' %(fold+1), averAcc)
print('The best accuracy of fold%d is:' %(fold+1), bestAcc)
self.log_write.write('The average accuracy of fold%d is: ' %(fold+1) + str(averAcc) + "\n")
self.log_write.write('The best accuracy fold%d is: ' %(fold+1) + str(bestAcc) + "\n")
return bestAcc, averAcc, Y_true, Y_pred
# writer.close()
def main():
best = 0
aver = 0
result_write = open("/Code/CT/results/seed/5-fold/sub_result.txt", "w")
for i in range(15):
starttime = datetime.datetime.now()
seed_n = np.random.randint(2021)
result_write.write('--------------------------------------------------')
# print('seed is ' + str(seed_n))
random.seed(seed_n)
np.random.seed(seed_n)
torch.manual_seed(seed_n)
torch.cuda.manual_seed(seed_n)
torch.cuda.manual_seed_all(seed_n)
print('Subject %d' % (i+1))
result_write.write('Subject ' + str(i + 1) + ' : ' + 'Seed is: ' + str(seed_n) + "\n")
ba = 0
aa = 0
bestAcc = 0
averAcc = 0
for fold in range(5):
exgan = ExGAN(i + 1, fold)
ba, aa, _, _ = exgan.train(fold)
# print('THE BEST ACCURACY IS ' + str(ba))
result_write.write('Best acc of fold' + str(fold+1) + 'is: ' + str(ba) + "\n")
result_write.write('Aver acc of fold' + str(fold+1) + 'is: ' + str(aa) + "\n")
bestAcc += ba
averAcc += aa
bestAcc /= 5
averAcc /= 5
result_write.write('5-fold Best acc is: ' + str(bestAcc) + "\n")
result_write.write('5-fold Aver acc is: ' + str(averAcc) + "\n")
# plot_confusion_matrix(Y_true, Y_pred, i+1)
best = best + bestAcc
aver = aver + averAcc
endtime = datetime.datetime.now()
print('subject %d duration: '%(i+1) + str(endtime - starttime))
best = best / 15
aver = aver / 15
result_write.write('--------------------------------------------------')
result_write.write('All subject Best accuracy is: ' + str(best) + "\n")
result_write.write('All subject Aver accuracy is: ' + str(aver) + "\n")
result_write.close()
if __name__ == "__main__":
print(time.asctime(time.localtime(time.time())))
main()
print(time.asctime(time.localtime(time.time())))