首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VGG16特征可视化

VGG16特征可视化
EN

Stack Overflow用户
提问于 2020-06-15 20:48:14
回答 1查看 458关注 0票数 0

我想要可视化从7x7x512层提取的特性,我尝试了一些不同的代码,总是有一个类似的错误。

当我尝试使用这些代码进行可视化时,

代码语言:javascript
复制
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)

给出一个错误。

Layer vgg16有多个入站节点,因此“层输出”的概念定义不明确。使用get_output_at(node_index)代替

编辑:

我找到解决办法了。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2020-06-16 07:24:52

您应该尝试使用克拉斯,这是一个用于绘图激活的python包。

您可以通过pip完成它,然后在代码中使用它。

下面是我用来绘制的代码示例

代码语言:javascript
复制
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)

还可以在输入图像上显示热图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62396742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档