2017-06-19 66 views
0

我是弗朗索瓦·乔利特(Francois Chollet)如何看待世界的虚拟网络博客文章,以便可视化由微博网络学到的功能。这里是我的代码:在卷积图层的可视化特征中超出范围的索引错误

from __future__ import print_function 
from scipy.misc import imsave 
import numpy as np 
import time 
from keras import applications 
from keras import backend as K 
K.set_image_dim_ordering('tf') 
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img 

# 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) 
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 
    x -= x.mean() 
    x /= (x.std() + 1e-5) 
    x *= 0.1 

    # clip to [0, 1] 
    x += 0.5 
    x = np.clip(x, 0, 1) 
# build the VGG16 network with ImageNet weights 
model = applications.VGG16(include_top=False, weights='imagenet', input_shape=(128,128,3)) 
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 
    return x/(K.sqrt(K.mean(K.square(x))) + 1e-5) 
kept_filters = [] 

for filter_index in range(0, 20): 
    # we only scan through the first 50 filters, 
    # but there are actually 512 of them 
    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 
    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 
    img = load_img('para1.jpg') # this is a PIL image 
    x = img_to_array(img) 
    x = x.reshape((1,) + x.shape) 
    input_img_data = x 
    input_img_data = (input_img_data - 0.5) * 20 + 128 
    # we run gradient ascent for 20 steps 
    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 
      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. 
n = 8 
# the filters that have the highest loss are assumed to be better-looking. 
# we will only keep the top 64 filters. 
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 
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) 

当我运行代码,我坚持错误:

File "C:/Users/rajaramans2/codes/untitled8.py", line 94, in <module> 
     img, loss = kept_filters[i * n + j] 

    IndexError: list index out of range 

请与修改帮助。我正在使用尺寸为128,128的RGB图像,并尝试在vgg16网络的第5部分显示卷积图层1。

回答

0

在第76行中,keep_filters被添加到第42行的循环中。所以,reserved_filters的长度至多为20.然而,在第94行中,您想访问keep_filters中的8 * 8 = 64个元素,的范围。

+0

真棒:)完美的作品:) – shiva