简体   繁体   English

VGG16 特征可视化

[英]VGG16 feature visualization

I want to visualize the extracted features from the layer 7x7x512 and I tried some different codes and there was always an error like this one.我想可视化从层 7x7x512 提取的特征,我尝试了一些不同的代码,但总是出现这样的错误。

When I try the visualization with this code,当我尝试使用此代码进行可视化时,

from keras.models import Model
layer_outputs = [layer.output for layer in my_model_11.layers]
activation_model = Model(inputs=my_model_11.input, outputs=layer_outputs)
activations = activation_model.predict(X_train_224[359].reshape(1,224,224,1))

def display_activation(activations, col_size, row_size, act_index): 
    activation = activations[act_index]
    activation_index=0
    fig, ax = plt.subplots(row_size, col_size, figsize=(row_size*2,col_size*2))
    for row in range(0,row_size):
       for col in range(0,col_size):
           ax[row][col].imshow(activation[0, :, :, activation_index], cmap='gray')
           activation_index += 1
    plt.savefig('Displa12y.png',bbox_inches= 'tight')
display_activation(activations, 2, 2, 1)

gives an error.给出一个错误。

Layer vgg16 has multiple inbound nodes, hence the notion of "layer output" is ill-defined.层 vgg16 有多个入站节点,因此“层输出”的概念定义不明确。 Use get_output_at(node_index) instead改用get_output_at(node_index)

edit:编辑:

I find the solution.我找到了解决方案。

ixs = [17,18] 
outputs = [model.layers[i].output for i in ixs]
model = Model(inputs=model.inputs, outputs=outputs)


feature_maps = model.predict(X_train_224[359].reshape(1,224,224,3))
# plot the output from each block
square = 8
for fmap in feature_maps:
    # plot all 64 maps in an 8x8 squares
    ix = 1
    for _ in range(square):
        plt.figure(figsize=(64,64))
        for _ in range(square):
           

            # specify subplot and turn of axis
            ax = pyplot.subplot(square, square, ix)
            ax.set_xticks([])
            ax.set_yticks([])
            
            # plot filter channel in grayscale
            plt.imshow(fmap[0, :, :, ix-1], cmap='gray')
            ix += 1
    # show the figure

        
    plt.show()

You should try Keract it's a python package made to plot activations.您应该尝试使用 Keract ,它是 python package 对 plot 激活所做的。

You can pull it through pip and then use it in your code.您可以通过 pip 将其拉出,然后在您的代码中使用它。

Here is a sample of code I used to plot这是我用来 plot 的代码示例

for i in range(0, 1000, 100):
    directory = 'data/activations/{}/{}/{}'.format(model_name, FLAGS.zone,i)
    os.mkdir(directory)
    activations = get_activations(model, test_generator[i], auto_compile=True)
    display_activations(activations, save=True, directory=directory)

You can also display heatmaps on an input image.您还可以在输入图像上显示热图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM