-
Notifications
You must be signed in to change notification settings - Fork 0
/
vae_torch.py
398 lines (329 loc) · 11.8 KB
/
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
from cgi import test
from vrae.utils import *
from vae_models import vanilla_vae
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
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 cmath import inf
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 Subject Selection', 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('-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('-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()
targ_subj = args.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)
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 = args.features
data_load = torch.split(X_train,batch_size)
channels = len(X_train[0,0,:,0])
print("Number of Features: " + str(features))
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")
# learning parameters
epochs = args.epochs
lr = 0.0005
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
## Define Model
model = vanilla_vae.VanillaVAE(filters=filters,channels=channels,features=features,data_type=args.data,data_length=len(data_load[0][:,0,0,0])).to(device)
# print(model)
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))
# Save file name
file_name = "./vae_torch" + '_' + str(args.data) + '_' + str(targ_subj) + '_' + str(filters) + '_' + str(channels) + '_' + str(features) + ".pt"
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 final_loss(bce_loss, mu, logvar):
BCE = bce_loss
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return BCE + KLD
def fit(model):
model.train()
running_loss = 0.0
# For each batch
for batch in tqdm(range(0,len(data_load))):
optimizer.zero_grad()
reconstruction, mu, logvar = model(data_load[batch])
bce_loss = recon_loss(reconstruction,data_load[batch])
loss = final_loss(bce_loss, mu, logvar)
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):
model.eval()
running_loss = 0.0
full_recon_loss = 0.0
with torch.no_grad():
# For each image in batch
# for batch in range(0,len(X_valid)):
reconstruction, mu, logvar = model(X_valid)
bce_loss = recon_loss(reconstruction,X_valid)
loss = final_loss(bce_loss, mu, logvar)
running_loss += loss.item()
full_recon_loss += bce_loss.item()
val_loss = running_loss/len(X_valid)
full_recon_loss = full_recon_loss/len(X_valid)
print(f"Recon Loss: {full_recon_loss:.4f}")
return val_loss, full_recon_loss
def eval(model):
model.eval()
nll_loss_eval = 0
with torch.no_grad():
reconstruction, mu, logvar = model(X_test)
recon_1 = recon_loss(reconstruction,X_test)
KLD_1 = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
nll_loss_eval = (recon_1 + KLD_1)/len(X_test)
return nll_loss_eval.item()
train_loss = []
val_loss = []
eval_loss = []
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)
eval_epoch_loss = eval(model)
train_loss.append(train_epoch_loss)
val_loss.append(val_epoch_loss)
eval_loss.append(eval_epoch_loss)
#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}")
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 = 'vae_train.xlsx'
df.to_excel(filepath, index=False)
df = pd.DataFrame(np.asarray(eval_loss))
## save to xlsx file
filepath = 'vae_eval.xlsx'
df.to_excel(filepath, index=False)
model = vanilla_vae.VanillaVAE(filters=filters,channels=channels,features=features,data_type=args.data,data_length=len(data_load[0][:,0,0,0])).to(device)
model.load_state_dict(torch.load(file_name))
# open('vae_output_LDA.txt', 'w').close()
# open('vae_output_LDA2.txt', 'w').close()
# open('vae_output_recon.txt', 'w').close()
# open('vae_output_NLL.txt', 'w').close()
## Anomaly Detection
model.eval()
with torch.no_grad():
reconstruction, mu, logvar = model(X_test)
reconstruction_train, mu_train, logvar_train = model(X_train)
test_recon_loss = recon_loss(reconstruction,X_test)
if (test_recon_loss > 20000 or torch.isnan(test_recon_loss)) and args.data == 'eeg':
# print("Anomaly Detected")
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)):
reconstruction, mu, logvar = model(data_test[i])
recon_1 = recon_loss(reconstruction,data_test[i])
KLD_1 = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
nll_loss = (recon_1 + KLD_1)
if recon_1 > 50000 or torch.isnan(test_recon_loss):
index_list.append(i)
total_loss += nll_loss
if len(index_list) == 40:
index_list = []
for i in range(0,len(data_test)):
reconstruction, mu, logvar = model(data_test[i])
recon_1 = recon_loss(reconstruction,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]
# Get Results
# Plot results
model.eval()
with torch.no_grad():
reconstruction, mu, logvar = model(X_test)
reconstruction_train, mu_train, logvar_train = model(X_train)
test_recon_loss = recon_loss(reconstruction,X_test)
print("Test Recon Loss: " + str(recon_loss(reconstruction,X_test)))
# Plot results
X_train_np = X_test.cpu().detach().numpy()
reconstruction = reconstruction.cpu().detach().numpy()
# print(X_train_np.shape)
plt.plot(X_train_np[0,0,0,:])
# plt.show()
plt.plot(reconstruction[0,0,0,:])
# plt.show()
mu = mu.cpu().detach().numpy()
mu_train = mu_train.cpu().detach().numpy()
## Latent Space Extracted Features
plt.plot(mu)
plt.show()
# fig, axs = plt.subplots(features, sharex=True, sharey=True, gridspec_kw={'hspace': 0})
# fig.suptitle('Learned Latent Features')
# hex_colors = []
# for _ in range(0,features):
# hex_colors.append('#%06X' % randint(0, 0xFFFFFF))
# colors = [hex_colors[int(i)] for i in range(0,features)]
# for i in range(0,features):
# axs[i].plot(mu[:,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()
# print(mu.shape)
for i in range(0,200):
y_test[i] = 0
for i in range(200,400):
y_test[i] = 1
plot_clustering(mu, y_test, engine='matplotlib', download = False)
def LDA(x,y):
lda = LinearDiscriminantAnalysis()
lda.fit(mu_train,y_train)
lda_score = lda.score(x,y)
return lda_score
score = LDA(mu,y_test)
print("LDA Score: " + str(score))
f = open("vae_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,y_test)
f = open("vae_output_LDA_test.txt", "a")
f.write(f"{score}\n")
f.close()
f = open("vae_output_recon.txt", "a")
f.write(f"{test_recon_loss}\n")
f.close()
## NLL
with torch.no_grad():
reconstruction, mu, logvar = model(X_test)
recon_1 = recon_loss(reconstruction,X_test)
KLD_1 = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
nll_loss = (recon_1 + KLD_1)/len(X_test)
print(nll_loss)
f = open("vae_output_NLL.txt", "a")
f.write(f"{nll_loss}\n")
f.close()