Skip to content

Instantly share code, notes, and snippets.

@oeway
Created October 10, 2016 16:25
Show Gist options
  • Save oeway/17e9585c55fbaa5ab5401bb078ebf87c to your computer and use it in GitHub Desktop.
Save oeway/17e9585c55fbaa5ab5401bb078ebf87c to your computer and use it in GitHub Desktop.
visualize filter by gradient descent on a random image
from keras import backend as K
img_width, img_height = 28,28
def visualize_filter(model, layer_name = 'convolution2d_1', filter_index = 0):
first_layer = model.layers[0]
# this is a placeholder tensor that will contain our generated images
input_img = first_layer.input
# get the symbolic outputs of each "key" layer (we gave them unique names).
layer_dict = dict([(layer.name, layer) for layer in model.layers])
# build a loss function that maximizes the activation
# of the nth filter of the layer considered
layer_output = layer_dict[layer_name].output
loss = K.mean(layer_output[:, filter_index, :, :])
# compute the gradient of the input picture wrt this loss
grads = K.gradients(loss, input_img)[0]
# normalization trick: we normalize the gradient
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
# this function returns the loss and grads given the input picture
iterate = K.function([input_img], [loss, grads])
import numpy as np
# we start from a gray image with some noise
input_img_data = np.random.random((1, 1, img_width, img_height)) * 20 + 128.
# run gradient ascent for 20 steps
step = 1.0
for i in range(20):
loss_value, grads_value = iterate([input_img_data])
input_img_data += grads_value * step
return input_img_data.reshape((img_width, img_height))
# usage
nb_filter = 10
for i in range(nb_filter):
img = visualize_filter(model, layer_name = 'convolution2d_2', filter_index = i)
plt.figure()
plt.imshow(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment