【问题标题】:Display extracted feature vector from trained layer of the model as an image将从模型的训练层提取的特征向量显示为图像
【发布时间】:2019-02-03 13:05:59
【问题描述】:

我正在使用迁移学习来识别物体。我使用经过训练的 VGG16 模型作为基础模型,并使用 Keras 在其上添加了我的分类器。然后我根据我的数据训练模型,模型运行良好。我想查看模型的中间层为给定数据生成的特征。为此,我使用了以下代码:

def ModeloutputAtthisLayer(model, layernme, imgnme, width, height):

    layer_name = layernme
    intermediate_layer_model = Model(inputs=model.input,
                                     outputs=model.get_layer(layer_name).output)
    img = image.load_img(imgnme, target_size=(width, height))
    imageArray = image.img_to_array(img)
    image_batch = np.expand_dims(imageArray, axis=0)
    processed_image = preprocess_input(image_batch.copy())
    intermediate_output = intermediate_layer_model.predict(processed_image)
    print("outshape of ", layernme, "is ", intermediate_output.shape)

在代码中,我使用np.expand_dims 为批次添加了一个额外维度,因为网络的输入矩阵应采用(batchsize, height, width, channels) 的形式。这段代码工作正常。特征向量的形状为1, 224, 224, 64

现在我希望将其显示为图像,为此我知道有一个附加维度作为批处理添加,因此我应该将其删除。在此之后,我使用了以下代码行:

imge = np.squeeze(intermediate_output, axis=0)
plt.imshow(imge)

但是它会抛出一个错误:

“图像数据的尺寸无效”

我想知道如何将提取的特征向量显示为图像。请有任何建议。

【问题讨论】:

    标签: python numpy keras feature-extraction keras-layer


    【解决方案1】:

    您的特征形状是(1,224,224,64),您不能直接绘制64 通道图像。您可以做的是独立绘制各个通道,如下所示

    imge = np.squeeze(intermediate_output, axis=0)
    filters = imge.shape[2]
    plt.figure(1, figsize=(32, 32))   # plot image of size (32x32)
    n_columns = 8
    n_rows = math.ceil(filters / n_columns) + 1
    for i in range(filters):
        plt.subplot(n_rows, n_columns, i+1)
        plt.title('Filter ' + str(i))
        plt.imshow(imge[:,:,i], interpolation="nearest", cmap="gray")
    

    这将在8 行和8 列中绘制64 图像。

    【讨论】:

    • 谢谢,这是否意味着一个简单的具有三个通道的RGB图像也应该单独绘制,
    • 它们可以单独绘制,但您不需要。
    • 我的观点是,如果我们可以将所有三个(即 RGB 通道)绘制为一张图像,为什么我们不能对超过三个通道(在本例中为 64 个通道)执行相同操作。
    【解决方案2】:

    一种可能的方法是通过这样的加权和将 64 个通道组合成一个单通道图像:

    weighted_imge = np.sum(imge*weights, axis=-1)
    

    其中weights 是一个具有 64 个加权系数的数组。

    如果您希望为所有通道赋予相同的权重,您可以简单地计算平均值:

    weighted_imge = np.mean(imge, axis=-1)
    

    演示

    import numpy as np
    import matplotlib.pyplot as plt
    
    intermediate_output = np.random.randint(size=(1, 224, 224, 64), 
                                            low=0, high=2**8, dtype=np.uint8)
    imge = np.squeeze(intermediate_output, axis=0)
    weights = np.random.random(size=(imge.shape[-1],))
    
    weighted_imge = np.sum(imge*weights, axis=-1)
    
    plt.imshow(weighted_imge)
    plt.colorbar()
    

    In [33]: intermediate_output.shape
    Out[33]: (1, 224, 224, 64)
    
    In [34]: imge.shape
    Out[34]: (224, 224, 64)
    
    In [35]: weights.shape
    Out[35]: (64,)
    
    In [36]: weighted_imge.shape
    Out[36]: (224, 224)
    

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 2016-05-11
      • 1970-01-01
      • 2019-08-03
      • 2016-12-21
      • 2019-10-05
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多