可视化VGG16的过滤器,通过输入空间的梯度下降
Keras实例目录
效果展示
http://i.imgur.com/4nj4KjN.jpg
代码注释
'''Visualization of the filters of VGG16, via gradient ascent in input space. 可视化VGG16的过滤器,通过输入空间的梯度下降 This script can run on CPU in a few minutes. 本脚本在CPU上需要运行一段时间 Results example: http://i.imgur.com/4nj4KjN.jpg 结果实例:http://i.imgur.com/4nj4KjN.jpg ''' from __future__ import print_function from scipy.misc import imsave import numpy as np import time from keras.applications import vgg16 from keras import backend as K # dimensions of the generated pictures for each filter. # 每个过滤器生成的图像的尺寸。 img_width = 128 img_height = 128 # the name of the layer we want to visualize # (see model definition at keras/applications/vgg16.py) # 可视化的层名(见模型定义,在keras/applications/vgg16.py) layer_name = 'block5_conv1' # util function to convert a tensor into a valid image # 函数将张量转换为有效图像 def deprocess_image(x): # normalize tensor: center on 0., ensure std is 0.1 # 归一化张量:0在中心,保证标准是0.1 x -= x.mean() x /= (x.std() + K.epsilon()) x *= 0.1 # clip to [0, 1] # 剪辑到[ 0, 1 ] x += 0.5 x = np.clip(x, 0, 1) # convert to RGB array # 转换为RGB数组 x *= 255 if K.image_data_format() == 'channels_first': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x # build the VGG16 network with ImageNet weights # 使用ImageNet权重建立VGG16网络 model = vgg16.VGG16(weights='imagenet', include_top=False) print('Model loaded.') model.summary() # this is the placeholder for the input images # 输入图像的占位符 input_img = model.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[1:]]) def normalize(x): # utility function to normalize a tensor by its L2 norm # 利用L2范数正规化张量的效用函数 return x / (K.sqrt(K.mean(K.square(x))) + K.epsilon()) kept_filters = [] for filter_index in range(200): # we only scan through the first 200 filters, # but there are actually 512 of them # 只扫描前200个过滤器,实际有512个过滤器 print('Processing filter %d' % filter_index) start_time = time.time() # we build a loss function that maximizes the activation # of the nth filter of the layer considered # 建立损失函数,它最大化期望层过滤器的激活函数 layer_output = layer_dict[layer_name].output if K.image_data_format() == 'channels_first': loss = K.mean(layer_output[:, filter_index, :, :]) else: loss = K.mean(layer_output[:, :, :, filter_index]) # we compute the gradient of the input picture wrt this loss # 计算输入图像的梯度损耗。 grads = K.gradients(loss, input_img)[0] # normalization trick: we normalize the gradient # 规范化技巧:规范梯度 grads = normalize(grads) # this function returns the loss and grads given the input picture # 此函数返回输入图片的损失和梯度 iterate = K.function([input_img], [loss, grads]) # step size for gradient ascent # 梯度上升的步长 step = 1. # we start from a gray image with some random noise # 从有随机噪音的灰色图片开始 if K.image_data_format() == 'channels_first': input_img_data = np.random.random((1, 3, img_width, img_height)) else: input_img_data = np.random.random((1, img_width, img_height, 3)) input_img_data = (input_img_data - 0.5) * 20 + 128 # we run gradient ascent for 20 steps # 运行梯度上升20步 for i in range(20): loss_value, grads_value = iterate([input_img_data]) input_img_data += grads_value * step print('Current loss value:', loss_value) if loss_value <= 0.: # some filters get stuck to 0, we can skip them # 有些过滤器卡在0,跳过它们。 break # decode the resulting input image # 解码得到的输入图像 if loss_value > 0: img = deprocess_image(input_img_data[0]) kept_filters.append((img, loss_value)) end_time = time.time() print('Filter %d processed in %ds' % (filter_index, end_time - start_time)) # we will stich the best 64 filters on a 8 x 8 grid. # 在8×8网格上列出最好的64个过滤器 n = 8 # the filters that have the highest loss are assumed to be better-looking. # 假设具有最高损失的滤波器效果更好。 # we will only keep the top 64 filters. # 只需要保持前64个过滤器 kept_filters.sort(key=lambda x: x[1], reverse=True) kept_filters = kept_filters[:n * n] # build a black picture with enough space for # our 8 x 8 filters of size 128 x 128, with a 5px margin in between # 建立一个黑色的图片,大小为128×128,有足够的空间为我们的8×8过滤器处理,之间的差距为5px margin = 5 width = n * img_width + (n - 1) * margin height = n * img_height + (n - 1) * margin stitched_filters = np.zeros((width, height, 3)) # fill the picture with our saved filters # 用保存的过滤器填充图片 for i in range(n): for j in range(n): img, loss = kept_filters[i * n + j] stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width, (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img # save the result to disk # 保存结果(为图片) imsave('stitched_filters_%dx%d.png' % (n, n), stitched_filters)
代码执行
Keras详细介绍
中文:http://keras-cn.readthedocs.io/en/latest/
实例下载
https://github.com/keras-team/keras
https://github.com/keras-team/keras/tree/master/examples
方便没积分童鞋,请加企鹅452205574,共享文件夹。
包括:代码、数据集合(图片)、已生成model、安装库文件等。