Bosses, in the process of learning computer vision, met a question about visual convolution neural network filter,
After built three functions, will VGG16 four layers, each layer of the top 64 filter, into 8 * 8 grid by 64 * 64 pixels, the output of the grid is blank, rather than the maximum response of each filter, is likely to be found in the debug process initializes the grid or process out of the question that is put in the grid but didn't find the root cause,
Bosses to Chou Chou is what problem, how to change it?
Python code is as follows:
The from keras. Applications import VGG16
The from keras import backend as K
The import numpy as np
The import matplotlib. Pyplot as PLT
='imagenet' model=VGG16 (weights,
Include_top=False)
Def deprocess_image (x) :
X=x.m ean ()
X/=(x.s td () + 1 e - 5)
*=0.1 x
X +=0.5
X=np. Clip (x, 0, 1)
X *=255
X=np. The clip (x, 0, 255). Astype (' uint8)
Return the x
Def generate_pattern (layer_name filter_index, size=150) :
Layer_output=model. Get_layer (layer_name). The output
Loss=K.m ean (layer_output [:, :, :, filter_index])
Grads=K.g radients (loss, the model input) [0]
Grads/=(Keith QRT (K.m ean (Keith quare (grads))) + 1 e - 5)
Interate=K.f unction ([model. Input], [loss, grads])
Input_img_data=https://bbs.csdn.net/topics/np.random.random ((1, size, size, 3)) * 20 + 128.
Step=1.
For I in range (40) :
Loss_value, grads_value=https://bbs.csdn.net/topics/interate ([input_img_data])
Input_img_data +=grads_value * step
Img=input_img_data [0]
Return deprocess_image (img)
PLT. Imshow (generate_pattern (' block3_conv1, 0))
PLT. The show ()
For layer_name in [' block1_conv1 ', 'block2_conv1', 'block3_conv1', 'block4_conv1] :
Size=64
Margin=5
Results=np. Zeros ((8 * 8 * size + 7 * margin, margin size + 7 *, 3))
For I in range (8) :
For j in range (8) :
Filter_image=generate_pattern (layer_name, I + (j * 8), size=size)
Horizontal_start=I + I * * size margin
Horizontal_end=horizontal_start + size
Vertical_start=margin size + j j * *
Vertical_end=vertical_start + size
Results [horizontal_start: horizontal_end vertical_start: vertical_end, :]=filter_image
PLT. Figure (figsize=(20, 20))
PLT. Imshow (results)
PLT. The show ()