first, the initialization of variables
# for each filter, generate the dimension of the image
Img_width =
Img_height = +
# We want to go to the visual layer name
# (see Model definition in keras/applications/vgg16.py )
layer_name = ' block5_conv1 '
convert the tensor to a valid image
def deprocess_image (x):
# Normalize tensor
x-= X.mean ()
x/= (X.STD () + 1e-5)
x *= 0.1
# clip to [0, 1]
x + = 0.5
x = np.clip (x, 0, 1)
# Convert to RGB array
x *= 255
x = np.clip (x, 0, 255). Astype (' uint8 ')
return X
three, standardized vector
def normalize (x):
# utility function normalized tensor by its L2 norm
return x/(K.sqrt (K.mean (K.square (x))) + 1e-5)
Iv. Building Models
call a pre-trained VGG16 model
Model = Vgg16. VGG16 (weights= ' imagenet ', include_top=false)
print (' Model loaded. ')
Model.summary ()
define a placeholder for the input image
Input_img = Model.input
store each layer of the VGG16 model with a dictionary layer_dict
Layer_dict = Dict ([(layer.name, layer) for layer in Model.layers[1:]])
print (Layer_dict.keys ())
print (layer_ Dict.values ())
Five, Extraction filter
Kept_filters = [] for Filter_index in range (0, 200): # We only scan the first 200 filters, # But actually there are 512 print (' Processing filter%d ' % filter_index) start_time = Time.time () # We construct a loss function that maximizes the activation of the nth filter in the considered layer layer_output = Layer_dict[layer_na
Me].output if k.image_dim_ordering () = = ' th ': Loss = K.mean (layer_output[:, Filter_index,:,:]) Else: Loss = K.mean (layer_output[:,:,:, Filter_index]) # We calculate the gradient of the input image with this loss grads = K.gradients (loss, input_img) [0 ] # Normalization Technique: We normalize gradient grads = normalize (grads) # This function returns the loss and gradient of the given input image iterate = K.function ([input_img], [loss,
Grads] # gradient Ascending Step step = 1. # We start with a gray image with some random noise if k.image_dim_ordering () = = ' th ': Input_img_data = Np.random.random ((1, 3, img_width, IM g_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 up 20 steps for I in range: 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 fall into 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.tim
E () print (' Filter%d processed in%ds '% (Filter_index, end_time-start_time)) # We will select the best 64 filters on a 8 x 8 grid.
n = 8 # The filter with the highest loss is assumed to be better looking.
# We will keep only the first 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 # our size is X 128 8 x 8 filter with 5px margin margin = 5 width = n * img_width + (n-1) * Margin height = n * img_height + (n-1) * Margin Stit Ched_filters = Np.zeros ((width, height, 3)) # Fill the picture with our saved filter 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
VI. Display Filter
Plt.imshow (IMG)
plt.show ()
plt.imshow (stitched_filters)
plt.show ()
# Saves the result, saves the array as an image
Imsave (' Stitched_filters_%dx%d.png '% (n, N), stitched_filters)
Source Address:
Https://github.com/Zheng-Wenkai/Keras_Demo