CNN神经网络猫狗分类经典案例,深度学习过程中间层激活特征图可视化

AI:CNN神经网络猫狗分类经典案例,深度学习过程中间层激活特征图可视化

基于前文 https://zhangphil.blog.csdn.net/article/details/103581736 ,这一次把前文神经网络在深度学习过程中,中间层的每一层激活的特征图可视化展现出来(中间层激活可视化),

取前8层

# 深度学习过程中每一层的神经网络激活图。
def visible():
    model = load_model(model_file_name)

    layer_outputs = [layer.output for layer in model.layers[:8]]
    activation_model = models.Model(inputs=model.input, outputs=layer_outputs)

    img_path = './data/test/cat/cat.4041.jpg'
    img = image.load_img(img_path, target_size=(150, 150))
    img_tensor = image.img_to_array(img)
    img_tensor = img_tensor / 255
    img_tensor = np.expand_dims(img_tensor, axis=0)

    activations = activation_model.predict(img_tensor)
    for k in range(len(activations)):
        first_layer_activation = activations[k]
        print(first_layer_activation.shape)

        col = 8
        plt.figure(figsize=(50, 50))
        for i in range(first_layer_activation.shape[3]):
            # col列。
            plt.subplot((first_layer_activation.shape[3] / col) + 1, col, i + 1)
            plt.imshow(first_layer_activation[0, :, :, i], cmap='viridis')
        #保存图片。
        plt.savefig(str(k) + ".jpg", dpi=100)
        plt.show()

第一层(0)的激活图可视化:

第2层(1)的激活图可视化:

扫描二维码关注公众号,回复: 8675238 查看本文章

最后一层

第8层(7)神经网络激活图:

可以看到,随着深度学习往纵深发展,神经网络学习到的知识越来越抽象。层数加深,人类的直观越来越难以理解。

发布了1029 篇原创文章 · 获赞 987 · 访问量 336万+

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/103599615
今日推荐