-
Notifications
You must be signed in to change notification settings - Fork 0
/
activation_maximization.py
44 lines (35 loc) · 1.34 KB
/
activation_maximization.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
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential, Model,load_model
from keras.layers import Dense, Dropout, Flatten, Activation, Input
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
## Load the model the see the activation map
model = load_model('handwritten.h5')
print('hello')
# visualize the dense layer
from vis.visualization import visualize_activation
from vis.utils import utils
from keras import activations
from matplotlib import pyplot as plt
plt.rcParams['figure.figsize'] = (18, 6)
# Utility to search for layer index by name.
# Alternatively we can specify this as -1 since it corresponds to the last layer.
layer_idx = utils.find_layer_idx(model, 'preds')
# Swap softmax with linear
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
# This is the output node we want to maximize.
filter_idx = 0
img = visualize_activation(model, layer_idx, filter_indices=filter_idx)
plt.imshow(img[..., 0])
plt.savefig('foo.png')
import string
s = string.ascii_uppercase
for output_idx in np.arange(26):
img = visualize_activation(model, layer_idx, filter_indices=output_idx, input_range=(0., 1.))
plt.figure()
plt.title(f'Networks perception of {s[output_idx]}')
plt.imshow(img[..., 0])
plt.savefig(f'{output_idx}.png')