-
Notifications
You must be signed in to change notification settings - Fork 15
/
nice_stand.py
445 lines (341 loc) · 15.9 KB
/
nice_stand.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
"""
Object recognition Things-EEG2 dataset
use 250 Hz data
"""
import os
import argparse
import random
import itertools
import datetime
import time
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch import Tensor
from torch.autograd import Variable
from einops.layers.torch import Rearrange
gpus = [6]
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(map(str, gpus))
result_path = '/home/NICE/results/'
model_idx = 'test0'
parser = argparse.ArgumentParser(description='Experiment Stimuli Recognition test with CLIP encoder')
parser.add_argument('--dnn', default='clip', type=str)
parser.add_argument('--epoch', default='200', type=int)
parser.add_argument('--num_sub', default=10, type=int,
help='number of subjects used in the experiments. ')
parser.add_argument('-batch_size', '--batch-size', default=1000, type=int,
metavar='N',
help='mini-batch size (default: 256), this is the total '
'batch size of all GPUs on the current node when '
'using Data Parallel or Distributed Data Parallel')
parser.add_argument('--seed', default=2023, type=int,
help='seed for initializing training. ')
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('Linear') != -1:
init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
class PatchEmbedding(nn.Module):
def __init__(self, emb_size=40):
super().__init__()
# revised from shallownet
self.tsconv = nn.Sequential(
nn.Conv2d(1, 40, (1, 25), (1, 1)),
nn.AvgPool2d((1, 51), (1, 5)),
nn.BatchNorm2d(40),
nn.ELU(),
nn.Conv2d(40, 40, (63, 1), (1, 1)),
nn.BatchNorm2d(40),
nn.ELU(),
nn.Dropout(0.5),
)
self.projection = nn.Sequential(
nn.Conv2d(40, emb_size, (1, 1), stride=(1, 1)),
Rearrange('b e (h) (w) -> b (h w) e'),
)
def forward(self, x: Tensor) -> Tensor:
# b, _, _, _ = x.shape
x = self.tsconv(x)
x = self.projection(x)
return x
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 FlattenHead(nn.Sequential):
def __init__(self):
super().__init__()
def forward(self, x):
x = x.contiguous().view(x.size(0), -1)
return x
class Enc_eeg(nn.Sequential):
def __init__(self, emb_size=40, **kwargs):
super().__init__(
PatchEmbedding(emb_size),
FlattenHead()
)
class Proj_eeg(nn.Sequential):
def __init__(self, embedding_dim=1440, proj_dim=768, drop_proj=0.5):
super().__init__(
nn.Linear(embedding_dim, proj_dim),
ResidualAdd(nn.Sequential(
nn.GELU(),
nn.Linear(proj_dim, proj_dim),
nn.Dropout(drop_proj),
)),
nn.LayerNorm(proj_dim),
)
class Proj_img(nn.Sequential):
def __init__(self, embedding_dim=768, proj_dim=768, drop_proj=0.3):
super().__init__(
nn.Linear(embedding_dim, proj_dim),
ResidualAdd(nn.Sequential(
nn.GELU(),
nn.Linear(proj_dim, proj_dim),
nn.Dropout(drop_proj),
)),
nn.LayerNorm(proj_dim),
)
def forward(self, x):
return x
# Image2EEG
class IE():
def __init__(self, args, nsub):
super(IE, self).__init__()
self.args = args
self.num_class = 200
self.batch_size = args.batch_size
self.batch_size_test = 400
self.batch_size_img = 500
self.n_epochs = args.epoch
self.lambda_cen = 0.003
self.alpha = 0.5
self.proj_dim = 256
self.lr = 0.0002
self.b1 = 0.5
self.b2 = 0.999
self.nSub = nsub
self.start_epoch = 0
self.eeg_data_path = '/home/Data/Things-EEG2/Preprocessed_data_250Hz/'
self.img_data_path = './dnn_feature/'
self.test_center_path = './dnn_feature/'
self.pretrain = False
self.log_write = open(result_path + "log_subject%d.txt" % self.nSub, "w")
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.Enc_eeg = Enc_eeg().cuda()
self.Proj_eeg = Proj_eeg().cuda()
self.Proj_img = Proj_img().cuda()
self.Enc_eeg = nn.DataParallel(self.Enc_eeg, device_ids=[i for i in range(len(gpus))])
self.Proj_eeg = nn.DataParallel(self.Proj_eeg, device_ids=[i for i in range(len(gpus))])
self.Proj_img = nn.DataParallel(self.Proj_img, device_ids=[i for i in range(len(gpus))])
self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
self.centers = {}
print('initial define done.')
def get_eeg_data(self):
train_data = []
train_label = []
test_data = []
test_label = np.arange(200)
train_data = np.load(self.eeg_data_path + '/sub-' + format(self.nSub, '02') + '/preprocessed_eeg_training.npy', allow_pickle=True)
train_data = train_data['preprocessed_eeg_data']
train_data = np.mean(train_data, axis=1)
train_data = np.expand_dims(train_data, axis=1)
test_data = np.load(self.eeg_data_path + '/sub-' + format(self.nSub, '02') + '/preprocessed_eeg_test.npy', allow_pickle=True)
test_data = test_data['preprocessed_eeg_data']
test_data = np.mean(test_data, axis=1)
test_data = np.expand_dims(test_data, axis=1)
return train_data, train_label, test_data, test_label
def get_image_data(self):
train_img_feature = np.load(self.img_data_path + self.args.dnn + '_feature_maps_training.npy', allow_pickle=True)
test_img_feature = np.load(self.img_data_path + self.args.dnn + '_feature_maps_test.npy', allow_pickle=True)
train_img_feature = np.squeeze(train_img_feature)
test_img_feature = np.squeeze(test_img_feature)
return train_img_feature, test_img_feature
def update_lr(self, optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def train(self):
self.Enc_eeg.apply(weights_init_normal)
self.Proj_eeg.apply(weights_init_normal)
self.Proj_img.apply(weights_init_normal)
train_eeg, _, test_eeg, test_label = self.get_eeg_data()
train_img_feature, _ = self.get_image_data()
test_center = np.load(self.test_center_path + 'center_' + self.args.dnn + '.npy', allow_pickle=True)
# shuffle the training data
train_shuffle = np.random.permutation(len(train_eeg))
train_eeg = train_eeg[train_shuffle]
train_img_feature = train_img_feature[train_shuffle]
val_eeg = torch.from_numpy(train_eeg[:740])
val_image = torch.from_numpy(train_img_feature[:740])
train_eeg = torch.from_numpy(train_eeg[740:])
train_image = torch.from_numpy(train_img_feature[740:])
dataset = torch.utils.data.TensorDataset(train_eeg, train_image)
self.dataloader = torch.utils.data.DataLoader(dataset=dataset, batch_size=self.batch_size, shuffle=True)
val_dataset = torch.utils.data.TensorDataset(val_eeg, val_image)
self.val_dataloader = torch.utils.data.DataLoader(dataset=val_dataset, batch_size=self.batch_size, shuffle=False)
test_eeg = torch.from_numpy(test_eeg)
# test_img_feature = torch.from_numpy(test_img_feature)
test_center = torch.from_numpy(test_center)
test_label = torch.from_numpy(test_label)
test_dataset = torch.utils.data.TensorDataset(test_eeg, test_label)
self.test_dataloader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=self.batch_size_test, shuffle=False)
# Optimizers
self.optimizer = torch.optim.Adam(itertools.chain(self.Enc_eeg.parameters(), self.Proj_eeg.parameters(), self.Proj_img.parameters()), lr=self.lr, betas=(self.b1, self.b2))
num = 0
best_loss_val = np.inf
for e in range(self.n_epochs):
in_epoch = time.time()
self.Enc_eeg.train()
self.Proj_eeg.train()
self.Proj_img.train()
# starttime_epoch = datetime.datetime.now()
for i, (eeg, img) in enumerate(self.dataloader):
eeg = Variable(eeg.cuda().type(self.Tensor))
# img = Variable(img.cuda().type(self.Tensor))
img_features = Variable(img.cuda().type(self.Tensor))
# label = Variable(label.cuda().type(self.LongTensor))
labels = torch.arange(eeg.shape[0]) # used for the loss
labels = Variable(labels.cuda().type(self.LongTensor))
# obtain the features
eeg_features = self.Enc_eeg(eeg)
# img_features = self.Enc_img(img).last_hidden_state[:,0,:]
# project the features to a multimodal embedding space
eeg_features = self.Proj_eeg(eeg_features)
img_features = self.Proj_img(img_features)
# normalize the features
eeg_features = eeg_features / eeg_features.norm(dim=1, keepdim=True)
img_features = img_features / img_features.norm(dim=1, keepdim=True)
# cosine similarity as the logits
logit_scale = self.logit_scale.exp()
logits_per_eeg = logit_scale * eeg_features @ img_features.t()
logits_per_img = logits_per_eeg.t()
loss_eeg = self.criterion_cls(logits_per_eeg, labels)
loss_img = self.criterion_cls(logits_per_img, labels)
loss_cos = (loss_eeg + loss_img) / 2
# total loss
loss = loss_cos
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
if (e + 1) % 1 == 0:
self.Enc_eeg.eval()
self.Proj_eeg.eval()
self.Proj_img.eval()
with torch.no_grad():
# * validation part
for i, (veeg, vimg) in enumerate(self.val_dataloader):
veeg = Variable(veeg.cuda().type(self.Tensor))
vimg_features = Variable(vimg.cuda().type(self.Tensor))
vlabels = torch.arange(veeg.shape[0])
vlabels = Variable(vlabels.cuda().type(self.LongTensor))
veeg_features = self.Enc_eeg(veeg)
veeg_features = self.Proj_eeg(veeg_features)
vimg_features = self.Proj_img(vimg_features)
veeg_features = veeg_features / veeg_features.norm(dim=1, keepdim=True)
vimg_features = vimg_features / vimg_features.norm(dim=1, keepdim=True)
logit_scale = self.logit_scale.exp()
vlogits_per_eeg = logit_scale * veeg_features @ vimg_features.t()
vlogits_per_img = vlogits_per_eeg.t()
vloss_eeg = self.criterion_cls(vlogits_per_eeg, vlabels)
vloss_img = self.criterion_cls(vlogits_per_img, vlabels)
vloss = (vloss_eeg + vloss_img) / 2
if vloss <= best_loss_val:
best_loss_val = vloss
best_epoch = e + 1
torch.save(self.Enc_eeg.module.state_dict(), './model/' + model_idx + 'Enc_eeg_cls.pth')
torch.save(self.Proj_eeg.module.state_dict(), './model/' + model_idx + 'Proj_eeg_cls.pth')
torch.save(self.Proj_img.module.state_dict(), './model/' + model_idx + 'Proj_img_cls.pth')
print('Epoch:', e,
' Cos eeg: %.4f' % loss_eeg.detach().cpu().numpy(),
' Cos img: %.4f' % loss_img.detach().cpu().numpy(),
' loss val: %.4f' % vloss.detach().cpu().numpy(),
)
self.log_write.write('Epoch %d: Cos eeg: %.4f, Cos img: %.4f, loss val: %.4f\n'%(e, loss_eeg.detach().cpu().numpy(), loss_img.detach().cpu().numpy(), vloss.detach().cpu().numpy()))
# * test part
all_center = test_center
total = 0
top1 = 0
top3 = 0
top5 = 0
self.Enc_eeg.load_state_dict(torch.load('./model/' + model_idx + 'Enc_eeg_cls.pth'), strict=False)
self.Proj_eeg.load_state_dict(torch.load('./model/' + model_idx + 'Proj_eeg_cls.pth'), strict=False)
self.Proj_img.load_state_dict(torch.load('./model/' + model_idx + 'Proj_img_cls.pth'), strict=False)
self.Enc_eeg.eval()
self.Proj_eeg.eval()
self.Proj_img.eval()
with torch.no_grad():
for i, (teeg, tlabel) in enumerate(self.test_dataloader):
teeg = Variable(teeg.type(self.Tensor))
tlabel = Variable(tlabel.type(self.LongTensor))
all_center = Variable(all_center.type(self.Tensor))
tfea = self.Proj_eeg(self.Enc_eeg(teeg))
tfea = tfea / tfea.norm(dim=1, keepdim=True)
similarity = (100.0 * tfea @ all_center.t()).softmax(dim=-1) # no use 100?
_, indices = similarity.topk(5)
tt_label = tlabel.view(-1, 1)
total += tlabel.size(0)
top1 += (tt_label == indices[:, :1]).sum().item()
top3 += (tt_label == indices[:, :3]).sum().item()
top5 += (tt_label == indices).sum().item()
top1_acc = float(top1) / float(total)
top3_acc = float(top3) / float(total)
top5_acc = float(top5) / float(total)
print('The test Top1-%.6f, Top3-%.6f, Top5-%.6f' % (top1_acc, top3_acc, top5_acc))
self.log_write.write('The best epoch is: %d\n' % best_epoch)
self.log_write.write('The test Top1-%.6f, Top3-%.6f, Top5-%.6f\n' % (top1_acc, top3_acc, top5_acc))
return top1_acc, top3_acc, top5_acc
# writer.close()
def main():
args = parser.parse_args()
num_sub = args.num_sub
cal_num = 0
aver = []
aver3 = []
aver5 = []
for i in range(num_sub):
cal_num += 1
starttime = datetime.datetime.now()
seed_n = np.random.randint(args.seed)
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))
ie = IE(args, i + 1)
Acc, Acc3, Acc5 = ie.train()
print('THE BEST ACCURACY IS ' + str(Acc))
endtime = datetime.datetime.now()
print('subject %d duration: '%(i+1) + str(endtime - starttime))
aver.append(Acc)
aver3.append(Acc3)
aver5.append(Acc5)
aver.append(np.mean(aver))
aver3.append(np.mean(aver3))
aver5.append(np.mean(aver5))
column = np.arange(1, cal_num+1).tolist()
column.append('ave')
pd_all = pd.DataFrame(columns=column, data=[aver, aver3, aver5])
pd_all.to_csv(result_path + 'result.csv')
if __name__ == "__main__":
print(time.asctime(time.localtime(time.time())))
main()
print(time.asctime(time.localtime(time.time())))