-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
380 lines (286 loc) · 13.2 KB
/
analysis.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
import matplotlib.pyplot as plt
import matplotlib
import joblib
import numpy as np
import pandas as pd
from preprocessing import FileSourceDataset, LogspecLoader, KaldiSource, KaldiLabelDataSource
from nnmnkwii.datasets import PaddedFileSourceDataset
from vis.visualization import visualize_cam
from vis.utils import utils
import tqdm
from keras import activations
import model.resnet
from keras import optimizers
import tensorflow as tf
import keras.backend as K
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
def ltas_lasso_interpreter(model):
lasso = joblib.load(model)
M = len(lasso.coef_)
idx = np.arange(len(lasso.coef_))
print(idx[abs(lasso.coef_) > 0])
duration = 1000
heatmap = np.zeros((1000,M))
mean_feat = lasso.coef_[:257]
std_feat = lasso.coef_[257:]
heatmap = np.tile(mean_feat,(1000,1)).T
heatmap_2 = np.tile(std_feat,(1000,1)).T
y = np.linspace(0, 8000, 257)
#font = {'size': 22}
#matplotlib.rc('font', **font)
fig_fontsize = 7
fig_linewidth = 1
fig = plt.figure(num=None, figsize=(3.14, 3.14*0.50), dpi=100, facecolor='w', edgecolor='k')
plt.plot(y,mean_feat,linewidth=1)
plt.plot(y,std_feat,linewidth=1)
plt.ylabel("coefficient for frequency bin",fontsize=fig_fontsize) # 32
plt.xlabel("frequency (Hz)",fontsize=fig_fontsize) # 32
plt.legend(["mean LTAS features","std LTAS features"],fontsize=8)
#fancy_spectrogram(heatmap_2,"control",cmap="gray")
#plt.colorbar()
#plt.subplots_adjust(left=0, bottom=0, right=0, top=0, wspace=0, hspace=0)
plt.tight_layout(pad=0)
fig.tight_layout(pad=0)
#fig.set_size_inches(3.14, 3.14*0.62)
fig.savefig("figures/ltas_sparsity.pdf",pad_len=0.005)
#plt.show()
def confusion_dnn(calculate):
if calculate:
config = tf.ConfigProto(allow_soft_placement=True,
log_device_placement=False)
config.gpu_options.allow_growth = False
config.gpu_options.per_process_gpu_memory_fraction = 0.8
sess = tf.Session(graph=tf.get_default_graph(),
config=config)
K.set_session(sess)
learning_rate = 0.001
pad_len = 4000
resnet = model.resnet.ResNet_Logspec(pad_len, 257)
optimiser = optimizers.Adam(lr=learning_rate)
resnet.trainer.compile(optimiser, loss='categorical_crossentropy',
weighted_metrics=["accuracy"],
metrics=['accuracy'],
sample_weight_mode="None")
experiment = "masked_100_frames_50_earlystopped_3"
resnet.trainer.load_weights("checkpoints/" + experiment + ".hd5")
test_ROOT = "/home/boomkin/repos/kaldi/egs/cancer_30/data/test_spec_vad/feats.scp"
cancer_list = ["id001", "id005", "id012","id013","id006","id008"]
healthy_list = ["id010013","id10094","id10110","id10509","id10855","id11217"]
print("Test set")
for speaker in cancer_list:
test_acoustic_source = KaldiSource(test_ROOT, subset=speaker, transpose=False)
test_acoustic = PaddedFileSourceDataset(test_acoustic_source, pad_len)
test_label = np.zeros((len(test_acoustic),2))
test_label[:,1] = 1
shuffle = False
batch_size = 1
val_gen = LogspecLoader(test_acoustic, test_label, shuffle, batch_size)
results = resnet.trainer.evaluate_generator(val_gen)
print(speaker,end="\t")
print(results[1])
for speaker in healthy_list:
test_acoustic_source = KaldiSource(test_ROOT, subset=speaker, transpose=False)
test_acoustic = PaddedFileSourceDataset(test_acoustic_source, pad_len)
test_label = np.zeros((len(test_acoustic),2))
test_label[:,0] = 1
shuffle = False
batch_size = 1
val_gen = LogspecLoader(test_acoustic, test_label, shuffle, batch_size)
results = resnet.trainer.evaluate_generator(val_gen)
print(speaker, end="\t")
print(results[1])
train_ROOT = "/home/boomkin/repos/kaldi/egs/cancer_30/data/train_spec_vad/feats.scp"
cancer_list = ["id002", "id003", "id004", "id007", "id011"]
healthy_list = ["id10078","id100111", "id10242", "id10571", "id11250"]
print("Train set")
for speaker in cancer_list:
test_acoustic_source = KaldiSource(train_ROOT, subset=speaker, transpose=False)
test_acoustic = PaddedFileSourceDataset(test_acoustic_source, pad_len)
test_label = np.zeros((len(test_acoustic),2))
test_label[:,1] = 1
shuffle = False
batch_size = 1
val_gen = LogspecLoader(test_acoustic, test_label, shuffle, batch_size)
results = resnet.trainer.evaluate_generator(val_gen)
print(speaker, end="\t")
print(results[1])
for speaker in healthy_list:
test_acoustic_source = KaldiSource(train_ROOT, subset=speaker, transpose=False)
test_acoustic = PaddedFileSourceDataset(test_acoustic_source, pad_len)
test_label = np.zeros((len(test_acoustic),2))
test_label[:,0] = 1
shuffle = False
batch_size = 1
val_gen = LogspecLoader(test_acoustic, test_label, shuffle, batch_size)
results = resnet.trainer.evaluate_generator(val_gen)
print(speaker, end="\t")
print(results[1])
def phonet_gmm_figure(model_cancer, model_control):
font = {'size': 22}
matplotlib.rc('font', **font)
cancer_gmm = joblib.load(model_cancer)
control_gmm = joblib.load(model_control)
meandiff = np.mean(cancer_gmm.means_.T - control_gmm.means_.T,axis=1)
proxy = np.zeros_like(meandiff)
print(meandiff.shape)
print(meandiff)
fig = plt.figure(num=None, figsize=(15, 12), dpi=80, facecolor='w', edgecolor='k')
ax = plt.subplot(111)
cats = ["vocalic", "consonantal", "back", "anterior", "open", "close", "nasal", "stop", "continuant", "lateral",
"flap", "trill", "voice", "strident", "labial", "dental", "velar"]
meandiff, cats = (list(t) for t in zip(*sorted(zip(meandiff, cats))))
meandiff = np.array(meandiff)
color = np.array(list("r" * len(cats)))
print(color.shape)
color_binary = meandiff > 0
color[color_binary] = "r"
color[~color_binary] = "b"
ax.grid(b=True,color="black",axis="x")
ax.set_axisbelow(b=True)
ax.barh(cats, width=meandiff, height=1, color=list(color),linewidth=1,edgecolor="black")
ax.barh(cats,proxy)
ax.text(0.002, -0.2, "/p/, /b/, /t/, /k/, /g/, /tS/, /d/", fontsize=22)
ax.text(-0.018, 15.8, "/m/, /n/", fontsize=22)
plt.xticks(rotation=50)
plt.xlabel("mean difference of GMM bins",fontsize=32)
plt.legend(["more cancer like","more control like"],fontsize=40)
leg = ax.get_legend()
leg.legendHandles[0].set_color('red')
leg.legendHandles[1].set_color('blue')
plt.savefig("figures/ppg_gmm_barplot.png")
def asr_gmm_figure(model_cancer, model_control):
cancer_gmm = joblib.load(model_cancer)
control_gmm = joblib.load(model_control)
meandiff = np.mean(cancer_gmm.means_.T - control_gmm.means_.T,axis=1)
# 15,12
fig_fontsize = 7
fig = plt.figure(num=None, figsize=(3.14, 3.14*0.7), dpi=100, facecolor='w', edgecolor='k')
ax = plt.subplot(111)
cats = pd.read_csv("fac_via_ppg/test/data/phoneme_table", delimiter="\t", header=None, names=["a","b"])
cats = cats["a"]
print(cats)
cats = cats[:-1]
print(cats.shape)
print(meandiff.shape)
idx = np.abs(meandiff) > 0.005
print(idx.shape)
cats = cats[idx]
meandiff = meandiff[idx]
meandiff, cats = (list(t) for t in zip(*sorted(zip(meandiff, cats))))
meandiff = np.array(meandiff)
color = np.array(list("r" * len(cats)))
print(color.shape)
color_binary = meandiff > 0
color[color_binary] = "r"
color[~color_binary] = "b"
ax.grid(b=True,color="black",axis="x")
ax.set_axisbelow(b=True)
ax.barh(cats, width=meandiff, height=1, color=list(color),linewidth=1,edgecolor="black")
proxy = np.zeros_like(meandiff)
ax.barh(cats,proxy)
plt.xlabel("mean difference of GMM bins",fontsize=fig_fontsize)
plt.legend(["more cancer like","more control like"],fontsize=fig_fontsize + 1)
leg = ax.get_legend()
leg.legendHandles[0].set_color('red')
leg.legendHandles[1].set_color('blue')
fig.tight_layout(pad=0)
plt.savefig("figures/ppg_gmm_barplot_2.pdf",pad_len=0.005)
def mean_gradcam(calculate=False):
if calculate:
config = tf.ConfigProto(allow_soft_placement=True,
log_device_placement=False)
config.gpu_options.allow_growth = False
config.gpu_options.per_process_gpu_memory_fraction = 0.8
sess = tf.Session(graph=tf.get_default_graph(),
config=config)
K.set_session(sess)
learning_rate = 0.001
pad_len = 4000
resnet = model.resnet.ResNet_Logspec(pad_len,257)
optimiser = optimizers.Adam(lr=learning_rate)
resnet.trainer.compile(optimiser, loss='categorical_crossentropy',
weighted_metrics=["accuracy"],
metrics=['accuracy'],
sample_weight_mode="None")
experiment = "masked_100_frames_50_earlystopped_3"
resnet.trainer.load_weights("checkpoints/" + experiment + ".hd5")
test_ROOT = "/home/boomkin/repos/kaldi/egs/cancer_30/data/test_spec_vad/feats.scp"
test_acoustic_source = KaldiSource(test_ROOT,subset="ignore",transpose=False)
test_acoustic = PaddedFileSourceDataset(test_acoustic_source,pad_len)
test_label = FileSourceDataset(KaldiLabelDataSource(test_ROOT))
shuffle = False
batch_size = 1
val_gen = LogspecLoader(test_acoustic,test_label, shuffle, batch_size)
dim_1 = val_gen.dim_1
dim_2 = val_gen.dim_2
# TODO: last dimension should be 3?
healthy_grads = np.zeros((dim_1,dim_2,3))
cancer_grads = np.zeros((dim_1,dim_2,3))
samples = 863
stacked_labels = np.zeros((samples,2),dtype=int)
num_healthy = 0
num_cancer = 0
# Utility to search for layer index by name.
# Alternatively we can specify this as
# -1 since it corresponds to the last layer.
layer_idx = -1
# Swap softmax with linear
# print(model.layers[layer_idx])
resnet.trainer.layers[layer_idx].activation = activations.linear
resnet.trainer = utils.apply_modifications(resnet.trainer)
for i in tqdm.tqdm(range(len(val_gen))):
output,labels = val_gen[i]
class_idx = labels[0]
stacked_labels[i] = class_idx
if class_idx[0] == 0:
cam = visualize_cam(resnet.trainer, layer_idx, filter_indices=0,
seed_input=output)
healthy_grads = healthy_grads + cam
num_healthy = num_healthy + 1
else:
cam = visualize_cam(resnet.trainer, layer_idx, filter_indices=1,
seed_input=output)
cancer_grads = cancer_grads + cam
num_cancer = num_cancer + 1
# Calculation of mean activations on spectograms
healthy_grads = healthy_grads / num_healthy
cancer_grads = cancer_grads / num_cancer
np.save("healthy_2.npy", healthy_grads)
np.save("cancer_2.npy",cancer_grads)
healthy_grads = np.load("healthy.npy")
cancer_grads = np.load("cancer.npy")
fig = plt.figure(num=None, figsize=(15, 10), dpi=100, facecolor='w', edgecolor='k')
font = {'size': 26}
matplotlib.rc('font', **font)
plt.subplot(2,1,1)
fancy_spectrogram(np.mean(healthy_grads[:1000,:,:],axis=2).T,"healthys speech")
plt.subplot(2,1,2)
fancy_spectrogram(np.mean(cancer_grads[:1000,:,:],axis=2).T,"oral cancer speech")
plt.tight_layout()
plt.savefig("figures/mean_activation_maps.pdf",bbox_inches='tight',pad_len=0.005)
def fancy_spectrogram(spectrogram,title,cmap="jet"):
# Flip upside down
plt.imshow(np.flipud(spectrogram),cmap)
# Labels
plt.ylabel("Hz")
plt.xlabel("frames",fontsize=32)
plt.title(title,fontsize=30 )
y = np.linspace(8000, 0, 257)
# the grid to which your data corresponds
ny = y.shape[0]
no_labels = 7 # how many labels to see on axis x
step_y = int(ny / (no_labels - 1)) # step between consecutive labels
y_positions = np.arange(0, ny, step_y) # pixel count at label position
y_labels = y[::step_y] # labels you want to see
plt.yticks(y_positions, y_labels)
# Calculate sum and normalise
sum_val = np.sum(spectrogram, axis=1)
if __name__ == '__main__':
#confusion_dnn(calculate=True)
# This produces the ppg_gmm_barplot_2.pdf
asr_gmm_figure("gmm_checkpoints/gmm_ppg_30sec_16_components_cancer.pkl",
"gmm_checkpoints/gmm_ppg_30sec_16_components_healthy.pkl")
mean_gradcam(calculate=False)
# This produces the ltas_sparsity.pdf
ltas_lasso_interpreter("svm_checkpoints/ard_ltas_0.01_components.pkl")