-
Notifications
You must be signed in to change notification settings - Fork 10
/
vis_densenet.py
53 lines (41 loc) · 1.74 KB
/
vis_densenet.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
from vis.utils import utils
from vis.visualization import visualize_activation
from vis.input_modifiers import Jitter
from keras.models import load_model
from vis.visualization import get_num_filters
import numpy as np
import scipy
model = load_model('dense_augmodel-ep0300-loss0.112-acc0.999-val_loss0.332-val_acc0.946.h5')
model.summary()
'''
Don't need to swap softmax with linear because we won't visualize the classification layer
model.layers[-1].activation = activations.linear
model = utils.apply_modifications(model)
'''
vis_images = []
layer_names = ['activation_7', 'activation_13', 'activation_14', 'activation_15', 'activation_23', 'activation_33', 'activation_39']
'''
Can also visualize other layers
layer_names = ['conv2d_xx']
layer_names = ['batch_normalization_xx']
'''
'''
Can print layer weights
layer_idx = utils.find_layer_idx(model, 'batch_normalization_xx')
print(model.layers[layer_idx].get_weights())
'''
for layer_name in layer_names:
layer_idx = utils.find_layer_idx(model, layer_name)
# Visualize all filters in this layer.
filters = np.arange(get_num_filters(model.layers[layer_idx]))
vis_images = []
for idx in filters:
#if idx % 2 == 0:
# Generate input image for each filter.
img = visualize_activation(model, layer_idx, filter_indices=idx
, act_max_weight=10, lp_norm_weight=0.01, tv_weight=0.05#, lp_norm_weight=0, tv_weight=0
, max_iter=200, input_modifiers=[Jitter()]) #, verbose=True
vis_images.append(img)
print(idx)
stitched = utils.stitch_images(vis_images, cols=24)
scipy.misc.imsave(layer_name + '.png', stitched)