-
Notifications
You must be signed in to change notification settings - Fork 0
/
dual_vae_torch.py
503 lines (418 loc) · 16.9 KB
/
dual_vae_torch.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
from cmath import inf
from vrae.utils import *
from vae_models import dualchain_vae
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import pandas as pd
import sys
import torch
import torch.nn as nn
import torch.optim as optim
import argparse
import matplotlib.pyplot as plt
from tqdm import tqdm
import numpy as np
import random
random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
np.random.seed(0)
torch.backends.cudnn.deterministic = True
from braindecode.torch_ext.util import set_random_seeds
import h5py
set_random_seeds(seed=0, cuda=True)
parser = argparse.ArgumentParser(
description='VAE Subject Selection')
parser.add_argument('-subj', type=int,
help='Target Subject for Feature Extraction', required=True)
parser.add_argument('-epochs', type=int, default= 100,
help='Number of Epochs', required=False)
parser.add_argument('-features', type=int, default= 16,
help='Number of Latent Features', required=False)
parser.add_argument('-alpha', type=float, default= 0.5,
help='Alpha Hyperparameter for First Chain KL Divergence', required=False)
parser.add_argument('-beta', type=float, default= 0.5,
help='Beta Hyperparameter for Second Chain KL Divergence', required=False)
parser.add_argument('-lr', type=float, default= 0.0005,
help='Set Learning Rate', required=False)
parser.add_argument('-clip', type=float, default= 0,
help='Set Gradient Clipping Threshold', required=False)
parser.add_argument('-loss', type=str, default= "default",
help='Sets the various lower bound loss functions to train DCVAE. Default - Default DCVAE loss. full - Entire reconstruction loss. indiv - Individual chain loss.', required=False)
parser.add_argument('-data', type=str, default= 'eeg',
help='Choose Type of Data: eeg or semg', required=False)
parser.add_argument('-datapath', type=str, help='Path to data',required=True)
parser.add_argument('-all', default=False, action='store_true')
parser.add_argument('-flow', default=False, action='store_true')
args = parser.parse_args()
if args.loss not in ['default', 'full', 'indiv']:
parser.print_help()
sys.exit(1)
targ_subj = args.subj
print("Subject: " + str(targ_subj))
if args.flow == True:
print("Using Normalising Flows")
# Get data from single subject.
def get_data(subj):
dpath = 's' + str(subj)
X = dfile[dpath]['X']
Y = dfile[dpath]['Y']
return X, Y
def get_multi_data(subjs):
Xs = []
Ys = []
for s in subjs:
x, y = get_data(s)
Xs.append(x[:])
Ys.append(y[:])
X = np.concatenate(Xs, axis=0)
Y = np.concatenate(Ys, axis=0)
return X, Y
# Randomly shuffled subject.
if args.data == 'eeg':
datapath = args.datapath + '/KU_mi_smt.h5'
else:
datapath = args.datapath + '/semg_flexex_smt.h5'
dfile = h5py.File(datapath, 'r')
torch.cuda.set_device(0)
set_random_seeds(seed=20200205, cuda=True)
if args.data == 'eeg':
X_train_all , y_train_all = get_multi_data([targ_subj])
X_train_all = np.expand_dims(X_train_all,axis=1)
if args.all == False:
X_test , y_test = get_multi_data([targ_subj])
X_test = np.expand_dims(X_test,axis=1)
X_valid = X_train_all[320:360]
X_test = X_train_all[360:]
X_train = X_train_all[:320]
y_train = y_test[:320]
y_test = y_test[360:]
else:
# Data visualisation
X_train = X_train_all
X_test = X_train_all
X_valid = X_train_all
y_train = y_train_all
y_test = y_train_all
## sEMG data
else:
subject_list = list(range(1,41))
subject_list.remove(targ_subj)
if args.all == False:
X_train, y_train = get_multi_data(subject_list[3:])
X_valid, y_valid = get_multi_data(subject_list[:3])
X_test, y_test = get_multi_data([targ_subj])
X_train = np.expand_dims(X_train,axis=1)
X_valid = np.expand_dims(X_valid,axis=1)
X_test = np.expand_dims(X_test,axis=1)
else:
X_train, y_train = get_multi_data(subject_list)
X_valid, y_valid = get_multi_data(subject_list)
X_test, y_test = get_multi_data(subject_list)
X_train = np.expand_dims(X_train,axis=1)
X_valid = np.expand_dims(X_valid,axis=1)
X_test = np.expand_dims(X_test,axis=1)
# print(X_train.shape)
X_train = torch.from_numpy(X_train)
X_train = X_train.to('cuda')
X_valid = torch.from_numpy(X_valid)
X_valid = X_valid.to('cuda')
X_test = torch.from_numpy(X_test)
X_test = X_test.to('cuda')
# VAE model
input_shape=(X_train.shape[1:])
batch_size = 16
kernel_size = 5
filters = 8
features = int(args.features / 2)
data_load = torch.split(X_train,batch_size)
channels = len(X_train[0,0,:,0])
print("Number of Features: " + str(features * 2))
if args.clip > 0:
print("Gradient Clipping: " + str(args.clip))
else:
print("No Gradient Clipping")
if args.data == 'eeg':
print("Data Loaded: " + args.data)
else:
print("Data Loaded: semg")
# leanring parameters
epochs = args.epochs
lr = 0.0005
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
## Model
model = dualchain_vae.DCVAE(filters=filters,channels=channels,features=features,data_type=args.data,flow=args.flow).to(device)
optimizer = optim.Adam(model.parameters(), lr=lr,betas=(0.5, 0.999),weight_decay=0.5*lr)
criterion = nn.BCELoss(reduction='sum')
## Number of trainable params
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Total number of trainable params: " + str(pytorch_total_params))
def recon_loss(outputs,targets):
outputs = torch.flatten(outputs)
targets = torch.flatten(targets)
loss = nn.MSELoss()
recon_loss = loss(outputs,targets)
return recon_loss
def fit(model):
model.train()
running_loss = 0.0
# For each batch
for batch in tqdm(range(0,len(data_load))):
optimizer.zero_grad()
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,logdet,logdet_2 = model(data_load[batch])
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(data_load[batch])
# print(reconstruction.shape)
# print(X_train.shape)
# bce_loss = criterion(reconstruction, X_train)
bce_loss = recon_loss(reconstruction,data_load[batch])
kl_1_loss = args.alpha * -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
mask_loss = recon_loss(reconstruction_2,new_inputs)
full_loss = recon_loss(reconstruction+reconstruction_2,data_load[batch])
kl_2_loss = args.beta * -0.5 * torch.sum(1 + logvar_2 - mu_2.pow(2) - logvar_2.exp())
# loss = loss + mask_loss + bce_loss + kl_2_loss
if args.loss == 'full':
loss = kl_1_loss + kl_2_loss + full_loss
elif args.loss == 'indiv':
loss = kl_1_loss + kl_2_loss + bce_loss + mask_loss
else:
loss = kl_1_loss + mask_loss + kl_2_loss
if args.flow == True:
loss = loss - args.alpha * torch.sum(logdet) - args.beta * torch.sum(logdet_2)
running_loss += loss.item()
loss.backward()
if args.clip > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
optimizer.step()
train_loss = running_loss/len(X_train)
return train_loss
def validate(model,data):
model.eval()
running_loss = 0.0
full_recon_loss = 0.0
mask_recon_loss = 0.0
with torch.no_grad():
# For each image in batch
# for batch in range(0,len(data_load)):
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,logdet,logdet_2 = model(data)
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(data)
# bce_loss = criterion(reconstruction, X_train)
bce_loss = recon_loss(reconstruction,data)
kl_1_loss = args.alpha * -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
mask_loss = recon_loss(reconstruction_2,new_inputs)
full_loss = recon_loss(reconstruction+reconstruction_2,data)
kl_2_loss = args.beta * -0.5 * torch.sum(1 + logvar_2 - mu_2.pow(2) - logvar_2.exp())
if args.loss == 'full':
loss = kl_1_loss + kl_2_loss + full_loss
elif args.loss == 'indiv':
loss = kl_1_loss + kl_2_loss + bce_loss + mask_loss
else:
loss = kl_1_loss + mask_loss + kl_2_loss
if args.flow == True:
loss = loss - args.alpha * torch.sum(logdet) - args.beta * torch.sum(logdet_2)
running_loss += loss.item()
full_recon_loss += full_loss.item()
mask_recon_loss += mask_loss.item()
val_loss = running_loss/len(data)
full_recon_loss = full_recon_loss/len(data)
mask_recon_loss = mask_recon_loss/len(data)
print(f"2nd Chain Recon Loss: {mask_recon_loss:.4f}")
print(f"Full Recon Loss: {full_recon_loss:.4f}")
return val_loss, full_recon_loss
# Save file name
file_name = "./dual_vae_torch" + '_' + str(args.data) + '_' + str(targ_subj) + '_' + str(filters) + '_' + str(channels) + '_' + str(args.features) + ".pt"
train_loss = []
val_loss = []
eval_loss = []
recon_loss_array = []
best_val_loss = inf
for epoch in range(epochs):
print(f"Epoch {epoch+1} of {epochs}")
train_epoch_loss = fit(model)
val_epoch_loss, full_recon_loss = validate(model,X_valid)
eval_epoch_loss, _ = validate(model,X_test)
#Save best model
if val_epoch_loss < best_val_loss:
best_val_loss = val_epoch_loss
torch.save(model.state_dict(),file_name)
print(f"Saving Model... Best Val Loss: {best_val_loss:.4f}")
train_loss.append(train_epoch_loss)
val_loss.append(val_epoch_loss)
eval_loss.append(eval_epoch_loss)
recon_loss_array.append(full_recon_loss)
print(f"Train Loss: {train_epoch_loss:.4f}")
print(f"Val Loss: {val_epoch_loss:.4f}")
## Save Training Info
df = pd.DataFrame(np.asarray(train_loss))
## save to xlsx file
filepath = 'dcvae_train.xlsx'
df.to_excel(filepath, index=False)
df = pd.DataFrame(np.asarray(eval_loss))
## save to xlsx file
filepath = 'dcvae_eval.xlsx'
df.to_excel(filepath, index=False)
# Plot results
model = dualchain_vae.DCVAE(filters=filters,channels=channels,features=features,data_type=args.data,flow=args.flow).to(device)
model.load_state_dict(torch.load(file_name))
# open('dual_output_LDA.txt', 'w').close()
# open('dual_output_LDA2.txt', 'w').close()
# open('dual_output_recon.txt', 'w').close()
# open('dual_output_NLL.txt', 'w').close()
## Anomaly detection
model.eval()
with torch.no_grad():
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,_,_ = model(X_test)
reconstruction_train, mu_train, logvar_train, new_inputs_train, reconstruction_2_train,mu_2_train,logvar_2_train,_,_ = model(X_train)
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(X_test)
reconstruction_train, mu_train, logvar_train, new_inputs_train, reconstruction_2_train,mu_2_train,logvar_2_train = model(X_train)
recon_full = reconstruction + reconstruction_2
# print(recon_loss(reconstruction,X_train))
test_recon_loss = recon_loss(reconstruction+reconstruction_2,X_test)
if (test_recon_loss > 20000 or torch.isnan(test_recon_loss)) and args.data == 'eeg':
index_list = []
model.eval()
with torch.no_grad():
total_loss = 0
data_test = torch.split(X_test,1)
for i in range(0,len(data_test)):
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,_,_ = model(data_test[i])
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(data_test[i])
recon_1 = recon_loss(reconstruction_2,new_inputs)
# print(recon_1)
if recon_1 > 50000 or torch.isnan(recon_1):
index_list.append(i)
if len(index_list) == 40:
index_list = []
for i in range(0,len(data_test)):
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,_,_ = model(data_test[i])
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(data_test[i])
recon_1 = recon_loss(reconstruction_2,new_inputs)
recon_2 = recon_loss(reconstruction+reconstruction_2,data_test[i])
if recon_1 > 100000 or torch.isnan(recon_1):
index_list.append(i)
old_list = list(range(0,40))
new_list = [number for number in old_list if number not in index_list]
X_test = X_test[new_list,:,:,:]
y_test = y_test[new_list]
## Evaluation
model.eval()
with torch.no_grad():
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,_,_ = model(X_test)
reconstruction_train, mu_train, logvar_train, new_inputs_train, reconstruction_2_train,mu_2_train,logvar_2_train,_,_ = model(X_train)
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(X_test)
reconstruction_train, mu_train, logvar_train, new_inputs_train, reconstruction_2_train,mu_2_train,logvar_2_train = model(X_train)
recon_full = reconstruction + reconstruction_2
# print(recon_loss(reconstruction,X_train))
test_recon_loss = recon_loss(reconstruction+reconstruction_2,X_test)
full_recon = torch.flatten(reconstruction+reconstruction_2) - torch.flatten(X_test)
print("Test Recon Loss: " + str(recon_loss(reconstruction+reconstruction_2,X_test)))
X_train_np = X_test.cpu().detach().numpy()
reconstruction_np = reconstruction.cpu().detach().numpy()
recon_full = recon_full.cpu().detach().numpy()
plt.plot(X_train_np[0,0,0,:])
# plt.show()
plt.plot(reconstruction_np[0,0,0,:])
# plt.show()
plt.plot(recon_full[0,0,0,:])
# plt.show()
plt.figure(figsize=(10,5))
plt.title("Training and Validation Loss")
plt.plot(val_loss,label="val")
plt.plot(train_loss,label="train")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
# plt.show()
plt.plot(recon_loss_array,label="recon")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
# plt.show()
mu_train = mu_train.cpu().detach().numpy()
mu_2_train = mu_2_train.cpu().detach().numpy()
mu = mu.cpu().detach().numpy()
mu_2 = mu_2.cpu().detach().numpy()
mu_3 = np.concatenate((mu,mu_2),axis=1)
mu_3_train = np.concatenate((mu_train,mu_2_train),axis=1)
print(mu_3.shape)
# print(mu_3[20,:])
## Latent Space Extracted Features
plt.plot(mu)
# plt.show()
plt.plot(mu_2)
# plt.show()
plt.plot(mu_3)
# plt.show()
# fig, axs = plt.subplots(arg.features, sharex=True, sharey=True, gridspec_kw={'hspace': 0})
# fig.suptitle('Learned Latent arg.features')
# hex_colors = []
# for _ in range(0,arg.features):
# hex_colors.append('#%06X' % randint(0, 0xFFFFFF))
# colors = [hex_colors[int(i)] for i in range(0,arg.features)]
# for i in range(0,arg.features):
# axs[i].plot(mu_3[:,i], linewidth=3, color=colors[i])
# # Hide x labels and tick labels for all but bottom plot.
# for ax in axs:
# ax.label_outer()
# plt.show()
## PCA and TSNE
# plot_clustering(mu_3, y_test, engine='matplotlib', download = False)
## LDA
def LDA_1(x,y):
lda = LinearDiscriminantAnalysis()
lda.fit(mu_3_train,y_train)
lda_score = lda.score(x,y)
return lda_score
score = LDA_1(mu_3,y_test)
print("LDA Score: " + str(score))
f = open("dual_output_LDA_train.txt", "a")
f.write(f"{score}\n")
f.close()
def LDA_2(x,y):
lda = LinearDiscriminantAnalysis()
lda.fit(x,y)
lda_score = lda.score(x,y)
return lda_score
score = LDA_2(mu_3,y_test)
f = open("dual_output_LDA_test.txt", "a")
f.write(f"{score}\n")
f.close()
f = open("dual_output_recon.txt", "a")
f.write(f"{test_recon_loss}\n")
f.close()
## NLL
model.eval()
with torch.no_grad():
if args.flow == True:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2,logdet,logdet_2 = model(X_test)
else:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(X_test)
recon_1 = recon_loss(reconstruction_2,new_inputs)
recon_orig = recon_loss(reconstruction,X_test)
recon_full = recon_loss(reconstruction+reconstruction_2,X_test)
KLD_1 = args.alpha * -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
KLD_2 = args.beta * -0.5 * torch.sum(1 + logvar_2 - mu_2.pow(2) - logvar_2.exp())
if args.flow == True:
KLD_1 = KLD_1 - args.alpha * torch.sum(logdet)
KLD_2 = KLD_2 - args.beta * torch.sum(logdet_2)
if args.loss == 'full':
loss = KLD_2 + KLD_1 + recon_full
elif args.loss == 'indiv':
loss = KLD_2 + KLD_1 + recon_1 + recon_orig
else:
loss = recon_1 + KLD_2 + KLD_1
nll_loss = (loss)/len(X_test)
print(nll_loss)
f = open("dual_output_NLL.txt", "a")
f.write(f"{nll_loss}\n")
f.close()