Deep Learning with Python 系列笔记(四):卷积处理可视化

可视化中间激活层,包括在一个网络中显示由不同convolution和pooling 层输出的特征映射,给定一个特定的输入(一个层的输出通常称为它的“activation”,即激活函数的输出)。这就给出了如何将输入分解到网络所学习的不同 filters 的视图。我们想要可视化的这些功能图有3个维度:宽度、高度和深度(通道)。每个通道都编码相对独立的特征,因此,作为二维图像,可视化这些特征图的正确方法是:通过独立地绘制每个通道的内容。

>>> from keras.models import load_model
>>> model = load_model('cats_and_dogs_small_2.h5')
>>> model.summary() # As a reminder.
________________________________________________________________
Layer (type) Output Shape Param #
================================================================
conv2d_5 (Conv2D) (None, 148, 148, 32) 896
________________________________________________________________
maxpooling2d_5 (MaxPooling2D) (None, 74, 74, 32) 0
________________________________________________________________
conv2d_6 (Conv2D) (None, 72, 72, 64) 18496
________________________________________________________________
maxpooling2d_6 (MaxPooling2D) (None, 36, 36, 64) 0
________________________________________________________________
conv2d_7 (Conv2D) (None, 34, 34, 128) 73856
________________________________________________________________
maxpooling2d_7 (MaxPooling2D) (None, 17, 17, 128) 0
________________________________________________________________
conv2d_8 (Conv2D) (None, 15, 15, 128) 147584
________________________________________________________________
maxpooling2d_8 (MaxPooling2D) (None, 7, 7, 128) 0
________________________________________________________________
flatten_2 (Flatten) (None, 6272) 0
________________________________________________________________
dropout_1 (Dropout) (None, 6272) 0
________________________________________________________________
dense_3 (Dense) (None, 512) 3211776
________________________________________________________________
dense_4 (Dense) (None, 1) 513
================================================================
Total params: 3,453,121
Trainable params: 3,453,121
Non-trainable params: 0

我们将使用猫的图片作为输入图像,而不是网络训练的图像的一部分。

img_path = '/Users/fchollet/Downloads/cats_and_dogs_small/test/cats/cat.1700.jpg'

# We preprocess the image into a 4D tensor
from keras.preprocessing import image
import numpy as np

img = image.load_img(img_path, target_size=(150, 150))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
# Remember that the model was trained on inputs
# that were preprocessed in the following way:
img_tensor /= 255.

# Its shape is (1, 150, 150, 3)
print(img_tensor.shape)

显示图片:

import matplotlib.pyplot as plt

plt.imshow(img_tensor[0])

这里写图片描述
为了提取我们想要查看的feature map,我们将创建一个Keras模型,它将批量的图像作为输入,并输出所有卷积和池化层的激活。为此,我们将使用Keras类模型。使用两个参数实例化一个模型:输入张量(或输入张量的列表)和输出张量(或输出张量列表)。结果类是一个Keras模型,就像您熟悉的顺序模型一样,将指定的输入映射到指定的输出。模型类的区别在于,它允许有多个输出的模型,而不像序列。

from keras import models

# Extracts the outputs of the top 8 layers:
layer_outputs = [layer.output for layer in model.layers[:8]]
# Creates a model that will return these outputs, given the model input:
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)

当输入图像输入时,该模型返回原始模型中图层激活的值。这是第一次遇到多输出模型:直到现在,所看到的模型只有一个输入和一个输出。在一般情况下,模型可以有任意数量的输入和输出。这个有一个输入和5个输出,每层激活一个输出。

# This will return a list of 5 Numpy arrays:
# one array per layer activation
activations = activation_model.predict(img_tensor)

例如,这是我们的cat图像输入的第一个卷积层的激活:

>>> first_layer_activation = activations[0]
>>> print(first_layer_a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值