如何在 keras 中获取卷积层的特征图



我有一个使用 Keras 加载的模型。我需要能够找到单个特征图(每个特征图的打印值(。我能够打印重量。以下是我的代码:

for layer in model.layers:
g=layer.get_config()
h=layer.get_weights()
print g
print h

该模型由一个共有384个神经元的凸模组成。前 128 个过滤器尺寸为 3,接下来的 4 个和后 128 个过滤器尺寸为 5。然后,有 relu 和 maxpool 层,然后将其馈送到 softmax 层中。我希望能够找到 convlayer、relu 和 maxpool 的输出(值而不是形状(。我在网上看到了代码,但我无法理解如何将它们映射到我的情况。

如果您正在寻找一种方法来查找给定一个或多个输入样本的层的激活(即特征图或输出(,您可以简单地定义一个后端函数,该函数采用输入数组并提供激活作为其输出。下面是一个说明示例(即您可能需要根据自己的需求和模型架构进行调整(:

from keras import backend as K
# define a function to get the activation of all layers
outputs = [layer.output for layer in model.layers]
active_func = K.function([model.input], [outputs])
# you can use it like this
activations = active_func([my_input_array])

最新更新