简体   繁体   English

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

[英]How to get featuremap of convolutional layer in keras

I have a model that I loaded using Keras. 我有一个使用Keras加载的模型。 I need to be able to find individual feature maps (print values of each feature map). 我需要能够找到各个要素图(每个要素图的打印值)。 I was able to print weights. 我能够打印砝码。 Following is my code: 以下是我的代码:

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

    print g

    print h

The model consists of one convlayer which has total 384 neurons. 该模型由一个共有384个神经元的卷积层组成。 First 128 have filter size 3, next 4 and last 128 have filter size 5. Then, there are relu and maxpool layers and then it is fed into softmax layer. 前128个过滤器的大小为3,下4个过滤器,最后128个的过滤器大小为5。然后,存在relu和maxpool层,然后将其馈入softmax层。 I want to be able to find outputs (values not shapes) of convlayer, relu and maxpool. 我希望能够找到convlayer,relu和maxpool的输出(值而不是形状)。 I have seen codes online but I'm unable to comprehend on how to map them to my situation. 我已经在网上看到代码,但是无法理解如何将代码映射到我的情况。

If you are looking for a way to find the activation (ie feature map or output) of a layer given one or more input samples, you can simply define a backend function that takes the input array(s) and gives the activation(s) as its output. 如果您正在寻找一种方法来查找给定一个或多个输入样本的图层的激活(即要素图或输出),则可以简单地定义一个后端函数 ,该函数采用输入数组并给出激活作为其输出。 Here is an example for illustration (ie you may need to adapt it to your needs and your model architecture): 这是一个示例示例(即,您可能需要使其适应您的需求和模型架构):

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM