-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaliency.py
More file actions
117 lines (94 loc) · 4.4 KB
/
saliency.py
File metadata and controls
117 lines (94 loc) · 4.4 KB
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
import visualizations
import numpy as np
import matplotlib.pyplot as plt
def get_gradients(conf, model, examples_batch, kerasvis=False):
if kerasvis:
from vis.visualization import visualize_saliency
else:
visualize_saliency = None # Get rid of PyCharm warning
subkey_gradients = []
# Get number of outputs
num_outputs = model.model.output.shape[1]
# Get gradients for each subkey
for neuron_index in range(0, num_outputs):
if kerasvis is False:
gradients = model.get_output_gradients(neuron_index,
examples_batch,
square_gradients=True,
mean_of_gradients=conf.saliency_mean_gradient)
else:
gradients = np.zeros(examples_batch.shape)
for i in range(0, examples_batch.shape[0]):
gradients[i, :] = visualize_saliency(model.model, -1, filter_indices=neuron_index, seed_input=examples_batch[i, :])
subkey_gradients.append(gradients)
return subkey_gradients
def plot_saliency_2d_overlay(conf, salvis_result):
"""
Plot saliency over a gray colormap of the EM traces, seperately for each subkey.
:param conf:
:param salvis_result:
:return:
"""
for neuron_index in range(len(salvis_result.gradients)):
# Plot the result
visualizations.plot_colormap(salvis_result.examples_batch,
cmap='gray',
show=False,
draw_axis=False,
alpha=1.0)
visualizations.plot_colormap(salvis_result.gradients[neuron_index],
cmap='inferno',
show=True,
draw_axis=False,
alpha=0.8,
title='Neuron: %d' % neuron_index,
xlabel='Time (samples)',
ylabel='Trace index')
def plot_saliency_2d(conf, salvis_result):
"""
First plots the input batch using a color map, and then plots the saliency color maps
for each subkey separately.
:param conf:
:param salvis_result:
:return:
"""
visualizations.plot_colormap(salvis_result.examples_batch, cmap='plasma')
for neuron_index in range(len(salvis_result.gradients)):
visualizations.plot_colormap(salvis_result.gradients[neuron_index])
def plot_saliency_1d(conf, salvis_result):
"""
Takes the mean signal of a batch and then plots a time series of this signal, overlayed
with the saliency for each subkey.
:param conf:
:param salvis_result:
:return:
"""
from dsp import normalize
# Get mean signal of examples batch
mean_signal = np.mean(salvis_result.examples_batch, axis=0)
plt.plot(normalize(mean_signal), color='tab:blue', label='Mean signal (normalized)')
for neuron_index in range(len(salvis_result.gradients)):
# Get gradient of mean signal
gradients = salvis_result.gradients[neuron_index]
mean_gradient = gradients[0]
# Visualize mean gradients
plt.plot(normalize(mean_gradient), label='Mean neuron %d gradient (normalized)' % neuron_index, alpha=0.6)
plt.legend()
plt.show()
def plot_saliency_kerasvis(conf, salvis_result):
for neuron_index in range(len(salvis_result.gradients)):
visualizations.plot_colormap(salvis_result.gradients[neuron_index])
def plot_saliency_2d_overlayold(conf, salvis_result):
from matplotlib.colors import ListedColormap
visualizations.plot_colormap(salvis_result.examples_batch, cmap='gray', show=False, draw_axis=False)
colormaps = ['Purples', 'Blues', 'Greens', 'Oranges', 'Reds']
# colormaps = ['inferno', 'inferno', 'inferno', 'inferno', 'inferno']
for neuron_index in range(len(salvis_result.gradients)):
gradients = salvis_result.gradients[neuron_index]
# Plot
cmap_orig = plt.get_cmap(colormaps.pop(0))
cmap_alpha = cmap_orig(np.arange(cmap_orig.N))
cmap_alpha[:, -1] = np.linspace(0, 1, cmap_orig.N) # dim 0: pixel, dim 1: channel
cmap_alpha = ListedColormap(cmap_alpha)
visualizations.plot_colormap(gradients, cmap=cmap_alpha, show=False, draw_axis=False)
plt.show()