深度学习系列笔记——叁 (提取出神经网络的某一层特征,可视化显示)

神经网络在训练过程中很像一个黑盒,除了输入层,输出层的结果可以一目了然,中间的隐层在训练中的变化,我们不太方便查看,本篇博文将利用keras的API,对已训练好的模型在预测过程中的变化进行提取。

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import time

config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.3
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

IMG_HEIGHT = 200
IMG_WIDTH = 200

# 模型为前面博客中训练得到的
model = tf.keras.models.load_model('catVSdog/model_9')
model.summary()
"""
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 200, 200, 32)      896       
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 200, 200, 32)      9248      
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 200, 200, 64)      18496     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 100, 100, 64)      0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 100, 100, 64)      36928     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 50, 50, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 50, 50, 128)       73856     
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 50, 50, 128)       147584    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 25, 25, 128)       0         
_________________________________________________________________
flatten (Flatten)            (None, 80000)             0         
_________________________________________________________________
dense (Dense)                (None, 1024)              81921024  
_________________________________________________________________
dropout (Dropout)            (None, 1024)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               524800    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 128)               65664     
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 129       
=================================================================
Total params: 82,798,625
Trainable params: 82,798,625
Non-trainable params: 0
_________________________________________________________________
"""
# shallow_sub_model = tf.keras.models.Model(inputs=model.input, outputs=model.get_layer('conv2d').output)
sub_model = tf.keras.models.Model(inputs=model.input,
                                  outputs=(model.get_layer('conv2d').output, model.get_layer('conv2d_2').output,
                                           model.get_layer('conv2d_3').output)
                                  )


# deep_sub_model = tf.keras.models.Model(inputs=model.input, outputs=model.get_layer('conv2d_5').output)


def preprocess_image(image):
    image = tf.image.decode_jpeg(image, channels=3)
    image = tf.image.resize(image, [IMG_HEIGHT, IMG_WIDTH])
    image /= 255.0
    return image


def load_and_preprocess_image(path):
    image = tf.io.read_file(path)
    return preprocess_image(image)


start = None
end = None
while True:
    image_paths = str(input("请输入你想要检测的图片绝对路径:"))
    image = load_and_preprocess_image(image_paths)
    image_array = tf.keras.preprocessing.image.img_to_array(image)
    image_array = tf.expand_dims(image_array, 0)

    start = time.time()
    shallow_prediction, middle_prediction, deep_prediction = sub_model.predict(image_array)
    end = time.time()
    print("prediction costs time : ", str((end - start)))

    shallow_prediction = np.squeeze(shallow_prediction, 0)
    middle_prediction = np.squeeze(middle_prediction, 0)
    deep_prediction = np.squeeze(deep_prediction, 0)

    # 浅层图像输出
    plt.figure(figsize=(8, 4))
    plt.title("shallow output features")
    for x in range(0, 32):
        ax = plt.subplot(4, 8, x + 1)
        label_tmp = shallow_prediction[:, :, x]
        plt.imshow(label_tmp.reshape(200, 200))  # 需要和该层输出的图片尺寸对应

        # 去除坐标轴
        plt.xticks([])
        plt.yticks([])
        # 去除黑框
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)
    plt.tight_layout()  # 会自动调整子图参数,使之填充整个图像区域
    plt.show()

    # 中间层图像输出
    plt.figure(figsize=(16, 16))
    plt.title("middle output features")
    for x in range(0, 64):
        ax = plt.subplot(8, 8, x + 1)
        label_tmp = middle_prediction[:, :, x]
        plt.imshow(label_tmp.reshape(200, 200))  # 需要和该层输出的图片尺寸对应

        # 去除坐标轴
        plt.xticks([])
        plt.yticks([])
        # 去除黑框
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)
    plt.tight_layout()  # 会自动调整子图参数,使之填充整个图像区域
    plt.show()

    # 深层图像输出
    plt.figure(figsize=(16, 16))
    plt.title("deep output features")
    for x in range(0, 64):
        ax = plt.subplot(8, 8, x + 1)
        label_tmp = deep_prediction[:, :, x]
        plt.imshow(label_tmp.reshape(100, 100))  # 需要和该层输出的图片尺寸对应

        # 去除坐标轴
        plt.xticks([])
        plt.yticks([])
        # 去除黑框
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.spines['left'].set_visible(False)
    plt.tight_layout()  # 会自动调整子图参数,使之填充整个图像区域
    plt.show()

输入的测试图像:

在这里插入图片描述

浅层:

在这里插入图片描述

中间层:

在这里插入图片描述

深层:

在这里插入图片描述

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值