Keras学习教程九 可视化,热度图

Keras学习教程九

 

原文地址:https://nbviewer.jupyter.org/github/fchollet/deep-learning-with-python-notebooks/blob/master/5.4-visualizing-what-convnets-learn.ipynb

 

Visualizing what convnets learn

 

    人们经常说深度学习模式是“黑盒子”,学习表示难以提取并以人类可读的形式呈现。虽然这对于某些类型的深度学习模型来说是部分正确的,但对于网点来说绝对是不正确的。通过小节点学习的表示非常适合可视化,很大程度上是因为它们是视觉概念的表示。自2013年以来,已经开发了多种技术来对这些表示进行可视化和解释。我们不会对所有这些进行调查,但我们将介绍其中最易于使用和最有用的三个:

    可视化中间转换输出(“中间激活”)。这对理解连续的重叠网层如何转换它们的输入以及首先了解各个重叠网络过滤器的含义很有用。
    可视化过滤器。这对于准确理解每个过滤器在可接受的范围内的视觉模式或概念很有用。
可视化图像中类激活的热图。这对了解识别为属于给定类的图像的哪一部分是有用的,并且因此允许图像中的对象本地化。

对于第一种方法 - 激活可视化 - 我们将使用我们在两节前从零开始训练猫与狗分类问题的小型小网络。对于接下来的两种方法,我们将使用我们在前一节介绍的VGG16模型。

 

    可视化中间层

    可视化中间激活包括在给定某个输入(层的输出通常称为“激活”,即激活函数的输出)的情况下,显示由网络中的各种卷积和积聚层输出的特征映射。 这给出了如何将输入分解到网络学习的不同滤波器的视图。 这些要显示的特征图有三个维度:宽度,高度和深度(通道)。 每个通道都编码相对独立的特征,因此将这些特征图可视化的正确方法是将每个通道的内容独立绘制为2D图像。 首先加载我们在第5.2节中保存的模型:

  1.  
    from keras.models import load_model
  2.  
     
  3.  
    model = load_model('cats_and_dogs_small_2.h5')
  4.  
    model.summary() # As a reminder.

    这将是我们将使用的输入图像 - 猫的图片,而不是网络培训的图像的一部分:

  1.  
    img_path = '/Users/fchollet/Downloads/cats_and_dogs_small/test/cats/cat.1700.jpg'
  2.  
     
  3.  
    # We preprocess the image into a 4D tensor
  4.  
    from keras.preprocessing import image
  5.  
    import numpy as np
  6.  
     
  7.  
    img = image.load_img(img_path, target_size=(150, 150))
  8.  
    img_tensor = image.img_to_array(img)
  9.  
    img_tensor = np.expand_dims(img_tensor, axis=0)
  10.  
    # Remember that the model was trained on inputs
  11.  
    # that were preprocessed in the following way:
  12.  
    img_tensor /= 255.
  13.  
     
  14.  
    # Its shape is (1, 150, 150, 3)
  15.  
    print(img_tensor.shape)

    让我们来展示我们的图片:

  1.  
    import matplotlib.pyplot as plt
  2.  
     
  3.  
    plt.imshow(img_tensor[0])
  4.  
    plt.show()

    为了提取我们想要看的特征地图,我们将创建一个Keras模型,它将批量图像作为输入,并输出所有卷积和积分图层的激活。 为此,我们将使用Keras类模型。 模型使用两个参数实例化:输入张量(或输入张量列表)和输出张量(或输出张量列表)。 得到的类是Keras模型,就像您熟悉的Sequential模型一样,将指定的输入映射到指定的输出。 Model类的不同之处在于它允许具有多个输出的模型,而不像Sequential。 有关Model类的更多信息,请参见第7章第1节。

  1.  
    from keras import models
  2.  
     
  3.  
    # Extracts the outputs of the top 8 layers:
  4.  
    layer_outputs = [layer.output for layer in model.layers[:8]]
  5.  
    # Creates a model that will return these outputs, given the model input:
  6.  
    activation_model = models.Model(inputs=model.input, outputs=layer_outputs)

    提供图像输入时,此模型会返回原始模型中图层激活的值。 这是您在本书中第一次遇到多输出模型:到目前为止,您所看到的模型只有一个输入和一个输出。 在一般情况下,模型可以有任何数量的输入和输出。 这一个有一个输入和8个输出,每层激活一个输出。

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

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

  1.  
    first_layer_activation = activations[0]
  2.  
    print(first_layer_activation.shape)

    这是一张带有32个频道的148x148功能地图。 让我们试着想象第三个频道:

  1.  
    import matplotlib.pyplot as plt
  2.  
     
  3.  
    plt.matshow(first_layer_activation[0, :, :, 3], cmap='viridis')
  4.  
    plt.show()

    该通道似乎编码对角边缘检测器。 让我们试试第30个频道 - 但请注意,您自己的频道可能会有所不同,因为卷积层学到的特定滤波器不是确定性的。

  1.  
    plt.matshow(first_layer_activation[0, :, :, 30], cmap='viridis')
  2.  
    plt.show()

    这个看起来像一个“鲜绿色的点”检测器,对编码猫眼有用。 在这一点上,让我们去绘制网络中所有激活的完整可视化图。 我们将在我们的8个激活图中的每个激活图中提取并绘制每个通道,并将结果叠加在一个大图像张量中,并排叠加通道。

  1.  
    import keras
  2.  
     
  3.  
    # These are the names of the layers, so can have them as part of our plot
  4.  
    layer_names = []
  5.  
    for layer in model.layers[:8]:
  6.  
    layer_names.append(layer.name)
  7.  
     
  8.  
    images_per_row = 16
  9.  
     
  10.  
    # Now let's display our feature maps
  11.  
    for layer_name, layer_activation in zip(layer_names, activations):
  12.  
    # This is the number of features in the feature map
  13.  
    n_features = layer_activation.shape[-1]
  14.  
     
  15.  
    # The feature map has shape (1, size, size, n_features)
  16.  
    size = layer_activation.shape[1]
  17.  
     
  18.  
    # We will tile the activation channels in this matrix
  19.  
    n_cols = n_features // images_per_row
  20.  
    display_grid = np.zeros((size * n_cols, images_per_row * size))
  21.  
     
  22.  
    # We'll tile each filter into this big horizontal grid
  23.  
    for col in range(n_cols):
  24.  
    for row in range(images_per_row):
  25.  
    channel_image = layer_activation[0,
  26.  
    :, :,
  27.  
    col * images_per_row + row]
  28.  
    # Post-process the feature to make it visually palatable
  29.  
    channel_image -= channel_image.mean()
  30.  
    channel_image /= channel_image.std()
  31.  
    channel_image *= 64
  32.  
    channel_image += 128
  33.  
    channel_image = np.clip(channel_image, 0, 255).astype('uint8')
  34.  
    display_grid[col * size : (col + 1) * size,
  35.  
    row * size : (row + 1) * size] = channel_image
  36.  
     
  37.  
    # Display the grid
  38.  
    scale = 1. / size
  39.  
    plt.figure(figsize=(scale * display_grid.shape[1],
  40.  
    scale * display_grid.shape[0]))
  41.  
    plt.title(layer_name)
  42.  
    plt.grid(False)
  43.  
    plt.imshow(display_grid, aspect='auto', cmap='viridis')
  44.  
     
  45.  
    plt.show()

    在这里需要注意一些非凡的事情:
    第一层充当各种边缘检测器的集合。在那个阶段,激活仍然保留了最初图像中的所有信息。
        随着我们越来越高级,激活变得越来越抽象,并且在视觉上可视化程度越来越低。他们开始编码更高层次的概念,如“猫耳朵”或“猫眼”。较高级的演示文件携带越来越少的关于图像视觉内容的信息,以及越来越多的与图像类别相关的信息。
        激活的稀疏性随着图层的深度而增加:在第一层中,所有滤镜都由输入图像激活,但在以下图层中,越来越多的滤镜是空白的。这意味着在输入图像中找不到滤镜编码的图案。
    我们刚刚证明了深度神经网络所表示的一个非常重要的通用特征:一层提取的特征随着层的深度越来越抽象。较高层的激活越来越少地传递关于所看到的特定输入的信息以及关于目标(在我们的例子中,图像的类别:猫或狗)的越来越多的信息。一个深度神经网络有效地作为一个信息蒸馏管道,原始数据进入(在我们的例子中为RBG图片),并进行反复变换,以便滤除不相关的信息(例如图像的特定视觉外观),同时提供有用的信息得到放大和细化(例如图像的类)。

    这与人类和动物感知世界的方式类似:观察场景几秒钟后,人类可以记住哪些抽象物体存在于其中(例如自行车,树),但不记得这些物体的具体外观。事实上,如果你现在想从头脑中抽出一辆普通的自行车,即使在你有生之年见过成千上万的自行车,你也不可能在遥远的地方找到它。立即尝试:这种效果是绝对真实的。你的大脑已经学会完全抽象它的视觉输入,将其转化为高级视觉概念,同时完全滤除不相关的视觉细节,使得记住我们​​身边的事物实际上看起来非常困难。

 

可视化convnet过滤器

    检查由convnets学习的过滤器的另一个容易的事情是显示每个过滤器应该响应的视觉模式。 这可以通过输入空间中的渐变上升来完成:将梯度下降应用于convnet的输入图像的值,以便从空白输入图像开始最大化特定滤镜的响应。 得到的输入图像将是所选滤波器最大响应的图像。
    该过程很简单:我们将建立一个损失函数,使给定卷积层中给定滤波器的值最大化,然后我们将使用随机梯度下降来调整输入图像的值,以最大化该激活值。 例如,在ImageNet上预先训练的VGG16网络层“block3_conv1”中激活过滤器0的过程如下:

  1.  
    from keras.applications import VGG16
  2.  
    from keras import backend as K
  3.  
     
  4.  
    model = VGG16(weights='imagenet',
  5.  
    include_top=False)
  6.  
     
  7.  
    layer_name = 'block3_conv1'
  8.  
    filter_index = 0
  9.  
     
  10.  
    layer_output = model.get_layer(layer_name).output
  11.  
    loss = K.mean(layer_output[:, :, :, filter_index])

    为了实现梯度下降,我们需要相对于模型输入的这种损失的梯度。 为此,我们将使用与Keras后端模块一起打包的渐变函数:

  1.  
    # The call to `gradients` returns a list of tensors (of size 1 in this case)
  2.  
    # hence we only keep the first element -- which is a tensor.
  3.  
    grads = K.gradients(loss, model.input)[0]

    梯度下降过程平稳使用的一个非显而易见的技巧是通过将梯度张量除以其L2范数(张量中的平均值的平方的平方根)来归一化梯度张量。 这确保了对输入图像进行更新的大小始终在相同的范围内。

  1.  
    # We add 1e-5 before dividing so as to avoid accidentally dividing by 0.
  2.  
    grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

    现在我们需要一种方法来计算输入图像中损耗张量和梯度张量的值。 我们可以定义Keras后端函数来执行此操作:iterate是一个函数,它采用Numpy张量(作为大小为1的张量列表)并返回两个Numpy张量的列表:损失值和梯度值。

  1.  
    iterate = K.function([model.input], [loss, grads])
  2.  
     
  3.  
    # Let's test it:
  4.  
    import numpy as np
  5.  
    loss_value, grads_value = iterate([np.zeros((1, 150, 150, 3))])

    在这一点上,我们可以定义一个Python循环来做随机梯度下降:

  1.  
    # We start from a gray image with some noise
  2.  
    input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128.
  3.  
     
  4.  
    # Run gradient ascent for 40 steps
  5.  
    step = 1. # this is the magnitude of each gradient update
  6.  
    for i in range(40):
  7.  
    # Compute the loss value and gradient value
  8.  
    loss_value, grads_value = iterate([input_img_data])
  9.  
    # Here we adjust the input image in the direction that maximizes the loss
  10.  
    input_img_data += grads_value * step

    得到的图像张量将是形状的浮点张量(1,150,150,3),其值在[0,255]内可能不是整数。 因此,我们需要对这张张进行后期处理,将其变成可显示的图像。 我们用以下直接的实用功能来做到这一点:

  1.  
    def deprocess_image(x):
  2.  
    # normalize tensor: center on 0., ensure std is 0.1
  3.  
    x -= x.mean()
  4.  
    x /= (x.std() + 1e-5)
  5.  
    x *= 0.1
  6.  
     
  7.  
    # clip to [0, 1]
  8.  
    x += 0.5
  9.  
    x = np.clip(x, 0, 1)
  10.  
     
  11.  
    # convert to RGB array
  12.  
    x *= 255
  13.  
    x = np.clip(x, 0, 255).astype('uint8')
  14.  
    return x

    现在我们已经完成了所有工作,让我们把它们放在一个Python函数中,该函数将输入图层名称和过滤器索引作为输入,并返回一个有效的图像张量,表示最大化激活指定过滤器的模式:

  1.  
    def generate_pattern(layer_name, filter_index, size=150):
  2.  
    # Build a loss function that maximizes the activation
  3.  
    # of the nth filter of the layer considered.
  4.  
    layer_output = model.get_layer(layer_name).output
  5.  
    loss = K.mean(layer_output[:, :, :, filter_index])
  6.  
     
  7.  
    # Compute the gradient of the input picture wrt this loss
  8.  
    grads = K.gradients(loss, model.input)[0]
  9.  
     
  10.  
    # Normalization trick: we normalize the gradient
  11.  
    grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
  12.  
     
  13.  
    # This function returns the loss and grads given the input picture
  14.  
    iterate = K.function([model.input], [loss, grads])
  15.  
     
  16.  
    # We start from a gray image with some noise
  17.  
    input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.
  18.  
     
  19.  
    # Run gradient ascent for 40 steps
  20.  
    step = 1.
  21.  
    for i in range(40):
  22.  
    loss_value, grads_value = iterate([input_img_data])
  23.  
    input_img_data += grads_value * step
  24.  
     
  25.  
    img = input_img_data[0]
  26.  
    return deprocess_image(img)

    让我们试试这个:

  1.  
    plt.imshow(generate_pattern('block3_conv1', 0))
  2.  
    plt.show()

    看来层block3_conv1中的过滤器0对波尔卡点图案有响应。

    现在有趣的部分:我们可以开始可视化每个图层中的每个过滤器。 为了简单起见,我们只会看每层中的前64个滤波器,并且只会查看每个卷积块的第一层(块1_conv1,块2_conv1,块3_conv1,块4_conv1,块5_conv1)。 我们会将输出安排在64x64滤镜模式的8x8网格上,每个滤镜模式之间会有一些黑色边距。

  1.  
    for layer_name in ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1']:
  2.  
    size = 64
  3.  
    margin = 5
  4.  
     
  5.  
    # This a empty (black) image where we will store our results.
  6.  
    results = np.zeros((8 * size + 7 * margin, 8 * size + 7 * margin, 3))
  7.  
     
  8.  
    for i in range(8): # iterate over the rows of our results grid
  9.  
    for j in range(8): # iterate over the columns of our results grid
  10.  
    # Generate the pattern for filter `i + (j * 8)` in `layer_name`
  11.  
    filter_img = generate_pattern(layer_name, i + (j * 8), size=size)
  12.  
     
  13.  
    # Put the result in the square `(i, j)` of the results grid
  14.  
    horizontal_start = i * size + i * margin
  15.  
    horizontal_end = horizontal_start + size
  16.  
    vertical_start = j * size + j * margin
  17.  
    vertical_end = vertical_start + size
  18.  
    results[horizontal_start: horizontal_end, vertical_start: vertical_end, :] = filter_img
  19.  
     
  20.  
    # Display the results grid
  21.  
    plt.figure(figsize=(20, 20))
  22.  
    plt.imshow(results)
  23.  
    plt.show()

    这些过滤器可视化告诉了我们很多关于如何使用convnet图层来查看世界的信息:convnet中的每个图层都会简单地学习一组过滤器,以便可以将它们的输入表示为过滤器的组合。 这与傅立叶变换如何将信号分解到一组余弦函数上类似。 随着我们在模型中进一步提高,这些闭环滤波器组中的滤波器变得越来越复杂和精致:
        来自模型第一层(block1_conv1)的滤镜对一些简单的方向边缘和颜色(在某些情况下为彩色边缘)进行编码。
        来自block2_conv1的过滤器对由边和颜色组合而成的简单纹理进行编码。

        较高层中的过滤器开始类似于自然图像中的纹理:羽毛,眼睛,叶子等。

 

可视化类激活的热图

    我们将介绍另一种可视化技术,该技术对于理解给定图像的哪些部分导致其最终分类决定的蛛丝马迹是有用的。这有助于“调试”一个定位点的决策过程,特别是在出现分类错误的情况下。它还允许您在图像中查找特定对象。
    这种一般类别的技术被称为“类激活图”(CAM)可视化,并且包括在输入图像上产生“类激活”的热图。 “类激活”热图是与特定输出类相关联的二维网格,针对任何输入图像中的每个位置计算,指示每个位置相对于所考虑的类的重要程度。例如,如果将图像输入到我们的“猫与狗”之一的小圆点中,则类激活图可视化允许我们为类“猫”生成热图,指示图像的不同部分如何像猫一样,同样地为类“狗”,指示如何像狗像不同部分的形象。
    我们将使用的具体实现是Grad-CAM中描述的那个:你为什么这么说?深度网络通过基于渐变的本地化的可视化解释。它非常简单:它包含在给定输入图像的情况下获取卷积图层的输出特征映射,并且通过类别相对于通道的梯度来权衡该特征映射中的每个通道。直观地说,理解这种技巧的一种方法是,我们通过“每个通道对于课程的重要程度”来加权一个“输入图像激活不同通道的强度如何”的空间图,从而产生“多强烈的空间图”输入图像激活类“。

    我们将再次使用预先训练的VGG16网络演示此技术:

  1.  
    from keras.applications.vgg16 import VGG16
  2.  
     
  3.  
    K.clear_session()
  4.  
     
  5.  
    # Note that we are including the densely-connected classifier on top;
  6.  
    # all previous times, we were discarding it.
  7.  
    model = VGG16(weights='imagenet')

    让我们将这个图像转换为VGG16模型可以读取的内容:模型在224x244大小的图像上进行训练,根据封装在效用函数keras.applications.vgg16.preprocess_input中的一些规则进行预处理。 因此,我们需要加载图像,将其大小调整为224x224,将其转换为Numpy float32张量,并应用这些预处理规则。

  1.  
    from keras.preprocessing import image
  2.  
    from keras.applications.vgg16 import preprocess_input, decode_predictions
  3.  
    import numpy as np
  4.  
     
  5.  
    # The local path to our target image
  6.  
    img_path = '/Users/fchollet/Downloads/creative_commons_elephant.jpg'
  7.  
     
  8.  
    # `img` is a PIL image of size 224x224
  9.  
    img = image.load_img(img_path, target_size=(224, 224))
  10.  
     
  11.  
    # `x` is a float32 Numpy array of shape (224, 224, 3)
  12.  
    x = image.img_to_array(img)
  13.  
     
  14.  
    # We add a dimension to transform our array into a "batch"
  15.  
    # of size (1, 224, 224, 3)
  16.  
    x = np.expand_dims(x, axis=0)
  17.  
     
  18.  
    # Finally we preprocess the batch
  19.  
    # (this does channel-wise color normalization)
  20.  
    x = preprocess_input(x)
  1.  
    preds = model.predict(x)
  2.  
    print('Predicted:', decode_predictions(preds, top=3)[0])

    此图片预测的前三类是:
        非洲大象(92.5%的概率)
        Tusker(有7%的概率)
        印度大象(有0.4%的概率)

    因此,我们的网络已经认识到我们的形象包含未定数量的非洲大象。 预测向量中被最大程度激活的条目是对应于“非洲象”类的条目,在索引386处:

    为了可视化我们图像的哪些部分是最像“非洲象”的那样,让我们设置Grad-CAM过程:

  1.  
    # This is the "african elephant" entry in the prediction vector
  2.  
    african_elephant_output = model.output[:, 386]
  3.  
     
  4.  
    # The is the output feature map of the `block5_conv3` layer,
  5.  
    # the last convolutional layer in VGG16
  6.  
    last_conv_layer = model.get_layer('block5_conv3')
  7.  
     
  8.  
    # This is the gradient of the "african elephant" class with regard to
  9.  
    # the output feature map of `block5_conv3`
  10.  
    grads = K.gradients(african_elephant_output, last_conv_layer.output)[0]
  11.  
     
  12.  
    # This is a vector of shape (512,), where each entry
  13.  
    # is the mean intensity of the gradient over a specific feature map channel
  14.  
    pooled_grads = K.mean(grads, axis=(0, 1, 2))
  15.  
     
  16.  
    # This function allows us to access the values of the quantities we just defined:
  17.  
    # `pooled_grads` and the output feature map of `block5_conv3`,
  18.  
    # given a sample image
  19.  
    iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
  20.  
     
  21.  
    # These are the values of these two quantities, as Numpy arrays,
  22.  
    # given our sample image of two elephants
  23.  
    pooled_grads_value, conv_layer_output_value = iterate([x])
  24.  
     
  25.  
    # We multiply each channel in the feature map array
  26.  
    # by "how important this channel is" with regard to the elephant class
  27.  
    for i in range(512):
  28.  
    conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
  29.  
     
  30.  
    # The channel-wise mean of the resulting feature map
  31.  
    # is our heatmap of class activation
  32.  
    heatmap = np.mean(conv_layer_output_value, axis=-1)

    为了可视化目的,我们还将0到1之间的热图标准化:

  1.  
    heatmap = np.maximum(heatmap, 0)
  2.  
    heatmap /= np.max(heatmap)
  3.  
    plt.matshow(heatmap)
  4.  
    plt.show()

    最后,我们将使用OpenCV生成一张图像,该图像将原始图像与我们刚获得的热图叠加在一起:

    1.  
      import cv2
    2.  
       
    3.  
      # We use cv2 to load the original image
    4.  
      img = cv2.imread(img_path)
    5.  
       
    6.  
      # We resize the heatmap to have the same size as the original image
    7.  
      heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
    8.  
       
    9.  
      # We convert the heatmap to RGB
    10.  
      heatmap = np.uint8(255 * heatmap)
    11.  
       
    12.  
      # We apply the heatmap to the original image
    13.  
      heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
    14.  
       
    15.  
      # 0.4 here is a heatmap intensity factor
    16.  
      superimposed_img = heatmap * 0.4 + img
    17.  
       
    18.  
      # Save the image to disk
    19.  
      cv2.imwrite('/Users/fchollet/Downloads/elephant_cam.jpg', superimposed_img)
    20.  

 

posted @ 2021-02-08 11:45  菜鸡一枚  阅读(341)  评论(0)    收藏  举报