-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFilter Visualization_Keras_VGG16.py
73 lines (50 loc) · 1.77 KB
/
Filter Visualization_Keras_VGG16.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 1 20:11:42 2020
@author: liuji
"""
from keras.applications import VGG16
from keras import backend as K
import matplotlib.pyplot as plt
model = VGG16(weights='imagenet',include_top = False)
import numpy as np
def deprocess_image(x):
x -= x.mean()
x /= (x.std() + 1e-5)
x *= 0.1
x += 0.5
x = np.clip(x,0,1)
x *= 255
x = np.clip(x,0,255).astype('uint8')
return x
def generate_pattern(layer_name, filter_index, size =150):
layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:,:,:,filter_index])
grads = K.gradients(loss,model.input)[0]
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
iterate = K.function([model.input],[loss,grads])
input_img_data = np.random.random((1,size,size,3)) * 20 + 128.
step = 1.
for i in range(40):
loss_value,grads_value = iterate([input_img_data])
input_img_data += grads_value * step
img = input_img_data[0]
return deprocess_image(img)
# plt.imshow(generate_pattern('block1_conv1',0))
# plt.show()
layer_name = 'block3_conv1'
size = 64
margin = 5
results = np.zeros((8 * size + 7 * margin, 8 * size + 7 * margin,3))
for i in range(8):
for j in range(8):
filter_img = generate_pattern(layer_name,i + (j * 8),size = size)
horizontal_start = i * size + i * margin
horizontal_end = horizontal_start + size
vertical_start = j * size + j * margin
vertical_end = vertical_start + size
results[horizontal_start:horizontal_end,vertical_start:vertical_end,:] = filter_img
results = np.clip(results,0,255).astype('uint8')
plt.figure(figsize=(20,20))
plt.imshow(results)
plt.show()