python函数运算卷积 python的卷积函数( 四 )


研究者能做的最简单的事情就是绘制出模型结构图,此外还可以标注神经网络中每层的形状及参数 。在keras中,可以使用如下命令完成模型结构图的绘制:
model.summary()_________________________________________________________________Layer (type)Output ShapeParam #
=================================================================conv2d_1 (Conv2D)(None, 26, 26, 32)320_________________________________________________________________conv2d_2 (Conv2D)(None, 24, 24, 64)18496_________________________________________________________________max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64)0_________________________________________________________________dropout_1 (Dropout)(None, 12, 12, 64)0_________________________________________________________________flatten_1 (Flatten)(None, 9216)0_________________________________________________________________dense_1 (Dense)(None, 128)1179776_________________________________________________________________dropout_2 (Dropout)(None, 128)0_________________________________________________________________preds (Dense)(None, 10)1290
=================================================================Total params: 1,199,882Trainable params: 1,199,882Non-trainable params: 0
还可以用一个更富有创造力和表现力的方式呈现模型结构框图,可以使用keras.utils.vis_utils函数完成模型体系结构图的绘制 。
另一种方法是绘制训练模型的过滤器 , 这样就可以了解这些过滤器的表现形式 。例如 , 第一层的第一个过滤器看起来像:
top_layer = model.layers[0]plt.imshow(top_layer.get_weights()[0][:, :, :, 0].squeeze(), cmap='gray')
一般来说 , 神经网络的底层主要是作为边缘检测器 , 当层数变深时,过滤器能够捕捉更加抽象的概念,比如人脸等 。
为了理解神经网络的工作过程,可以在输入图像上应用过滤器,然后绘制其卷积后的输出,这使得我们能够理解一个过滤器其特定的激活模式是什么 。比如,下图是一个人脸过滤器 , 当输入图像是人脸图像时候,它就会被激活 。
from vis.visualization import visualize_activation
from vis.utils import utils
from keras import activations
from matplotlib import pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (18, 6)
# Utility to search for layer index by name.
# Alternatively we can specify this as -1 since it corresponds to the last layer.
layer_idx = utils.find_layer_idx(model, 'preds')
# Swap softmax with linear
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
# This is the output node we want to maximize.filter_idx = 0
img = visualize_activation(model, layer_idx, filter_indices=filter_idx)
plt.imshow(img[..., 0])
同理,可以将这个想法应用于所有的类别,并检查它们的模式会是什么样子 。
for output_idx in np.arange(10):
# Lets turn off verbose output this time to avoid clutter and just see the output.
img = visualize_activation(model, layer_idx, filter_indices=output_idx, input_range=(0., 1.))
plt.figure()
plt.title('Networks perception of {}'.format(output_idx))
plt.imshow(img[..., 0])
在图像分类问题中,可能会遇到目标物体被遮挡,有时候只有物体的一小部分可见的情况 。基于图像遮挡的方法是通过一个灰色正方形系统地输入图像的不同部分并监视分类器的输出 。这些例子清楚地表明模型在场景中定位对象时,若对象被遮挡,其分类正确的概率显著降低 。
为了理解这一概念,可以从数据集中随机抽取图像,并尝试绘制该图的热图(heatmap) 。这使得我们直观地了解图像的哪些部分对于该模型而言的重要性,以便对实际类别进行明确的区分 。

推荐阅读