如何用Tensorflow显示CNN上使用的卷积滤波器?

如何用Tensorflow显示CNN上使用的卷积滤波器?,tensorflow,filter,conv-neural-network,Tensorflow,Filter,Conv Neural Network,我想提供与此类似的数据: 为此,我使用Tensorflow加载我的模型,然后使用此代码从一个层中选择带有过滤器的变量: # search for the name of the specific layer with the filters I want to display for v in tf.trainable_variables(): print(v.name) # store the filters into a variable var = [v for v in tf.

我想提供与此类似的数据:

为此,我使用Tensorflow加载我的模型,然后使用此代码从一个层中选择带有过滤器的变量:

# search for the name of the specific layer with the filters I want to display
for v in tf.trainable_variables():
    print(v.name)
# store the filters into a variable
var = [v for v in tf.trainable_variables() if v.name == "model/center/kernel:0"][0]
执行
var.eval()
我能够将
var
存储到一个numpy数组中。 此numpy数组具有以下形状:(3,3,512,512),对应于内核大小:
3x3
和过滤器数量:512


我的问题是:如何从这个3,3512512数组中提取1个过滤器来显示它?如果我知道如何做到这一点,我将找到如何显示512个过滤器

,因为您使用的是Tensorflow,您可能正在使用
tf.keras.Sequential
来构建CNN模型,
Model.summary()
给出了所有层的名称以及形状,如下所示:

一旦您有了层名,您就可以可视化CNN层的卷积滤波器,如下代码所示:

#-------------------------------------------------
#Utility function for displaying filters as images
#-------------------------------------------------

def deprocess_image(x):

    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1
    x += 0.5
    x = np.clip(x, 0, 1)
    x *= 255
    x = np.clip(x, 0, 255).astype('uint8')
    return x

#---------------------------------------------------------------------------------------------------
#Utility function for generating patterns for given layer starting from empty input image and then 
#applying Stochastic Gradient Ascent for maximizing the response of particular filter in given layer
#---------------------------------------------------------------------------------------------------

def generate_pattern(layer_name, filter_index, size=150):

    layer_output = model.get_layer(layer_name).output
    loss = K.mean(layer_output[:, :, :, filter_index])
    grads = K.gradients(loss, model.input)[0]
    grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
    iterate = K.function([model.input], [loss, grads])
    input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.
    step = 1.
    for i in range(80):
        loss_value, grads_value = iterate([input_img_data])
        input_img_data += grads_value * step

    img = input_img_data[0]
    return deprocess_image(img)

#------------------------------------------------------------------------------------------
#Generating convolution layer filters for intermediate layers using above utility functions
#------------------------------------------------------------------------------------------

layer_name = 'conv2d_4'
size = 299
margin = 5
results = np.zeros((8 * size + 7 * margin, 8 * size + 7 * margin, 3))

for i in range(8):
    for j in range(8):
        filter_img = generate_pattern(layer_name, i + (j * 8), size=size)
        horizontal_start = i * size + i * margin
        horizontal_end = horizontal_start + size
        vertical_start = j * size + j * margin
        vertical_end = vertical_start + size
        results[horizontal_start: horizontal_end, vertical_start: vertical_end, :] = filter_img

plt.figure(figsize=(20, 20))
plt.savefig(results)
上面的代码只显示一个层的64个过滤器。你可以相应地改变它

有关更多信息,请参阅