神经网络在训练过程中很像一个黑盒,除了输入层,输出层的结果可以一目了然,中间的隐层在训练中的变化,我们不太方便查看,本篇博文将利用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
_________________________________________________________________
"""
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)
)
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()
输入的测试图像:

浅层:

中间层:

深层:
