-
Notifications
You must be signed in to change notification settings - Fork 0
/
vae_eval.py
332 lines (276 loc) · 10.3 KB
/
vae_eval.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
from cmath import inf
from vae.utils import *
from vae_models import dualchain_vae
from vae_models import vanilla_vae
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import h5py
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
parser = argparse.ArgumentParser(
description='Model Evaluation')
parser.add_argument('-all', default=False, action='store_true')
parser.add_argument('-pretrained', default=True, action='store_true')
parser.add_argument('-datapath', type=str, help='Path to training data',required=True)
parser.add_argument('-model', type=str, help='Path to model data',required=True)
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)
args = parser.parse_args()
if args.pretrained == True:
data = 'eeg'
targ_subj = 6
filters = 8
channels = 62
features = 16
else:
string = args.model
x = string.split("_")
index = x.index('torch')
x = x[index + 1:]
data = x[0]
targ_subj = int(x[1])
filters = int(x[2])
channels = int(x[3])
features = int(x[4][:2])
if data == 'eeg':
datapath = args.datapath + '/KU_mi_smt.h5'
else:
datapath = args.datapath + '/semg_flexex_smt.h5'
dfile = h5py.File(datapath, 'r')
# 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
if 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')
data_load = torch.split(X_train,16)
def recon_loss(outputs,targets):
outputs = torch.flatten(outputs)
targets = torch.flatten(targets)
loss = nn.MSELoss()
recon_loss = loss(outputs,targets)
return recon_loss
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if 'dual' in args.model:
features = int(features / 2)
model = dualchain_vae.DCVAE(filters=filters,channels=channels,features=features,data_type=data).to(device)
else:
features = features
model = vanilla_vae.VanillaVAE(filters=filters,channels=channels,features=features,data_type=data,data_length=len(data_load[0][:,0,0,0])).to(device)
# Plot results
if args.pretrained == False:
if 'dual' in args.model:
model.load_state_dict(torch.load(string))
else:
model.load_state_dict(torch.load(string))
else:
if 'dual' in args.model:
model.load_state_dict(torch.load("./pre_trained/dual_vae_torch.pt"))
else:
model.load_state_dict(torch.load("./pre_trained/vae_torch.pt"))
# 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 'dual' in args.model:
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)
else:
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 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)):
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)):
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 'dual' in args.model:
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)))
else:
reconstruction, mu, logvar = model(X_test)
reconstruction_train, mu_train, logvar_train = model(X_train)
test_recon_loss = recon_loss(reconstruction,X_test)
# Original
X_train_np = X_test.cpu().detach().numpy()
plt.plot(X_train_np[0,0,0,:])
plt.title('Original Input')
plt.show()
# Recon 1
reconstruction_np = reconstruction.cpu().detach().numpy()
plt.plot(reconstruction_np[0,0,0,:])
plt.title('First Chain Output')
plt.show()
# Recon 2
if 'dual' in args.model:
recon_full = recon_full.cpu().detach().numpy()
plt.plot(recon_full[0,0,0,:])
plt.title('Second Chain Output')
plt.show()
mu = mu.cpu().detach().numpy()
mu_2 = mu_2.cpu().detach().numpy()
mu_3 = np.concatenate((mu,mu_2),axis=1)
mu_train = mu_train.cpu().detach().numpy()
mu_2_train = mu_2_train.cpu().detach().numpy()
mu_3_train = np.concatenate((mu_train,mu_2_train),axis=1)
else:
mu_3 = mu.cpu().detach().numpy()
mu_3_train = mu_train.cpu().detach().numpy()
plt.plot(mu_3)
plt.title("Learned Features Overlapped")
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_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.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_LDA2.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 'dual' in args.model:
reconstruction, mu, logvar, new_inputs, reconstruction_2,mu_2,logvar_2 = model(X_test)
recon_1 = recon_loss(reconstruction_2,new_inputs)
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())
nll_loss = (recon_1 + KLD_2 + KLD_1)/len(X_test)
else:
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("Approx Log-Likelihood: " + str(nll_loss.item()))
f = open("dual_output_NLL.txt", "a")
f.write(f"{nll_loss}\n")
f.close()