从 CNN 层获取过滤器值

Getting the filters values from CNN layers

我有以下型号(举例)

input_img = Input(shape=(224,224,1)) # size of the input image
x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)

我的自动编码器模型中有好几层这样的层。我对第一层的过滤器特别感兴趣。有 64 个过滤器,每个大小为 3x3。

为了获取过滤器,我尝试使用以下代码:

x.layers[0].get_weights()[0]

但我收到如下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-166-96506292d6d7> in <module>()
      4 x = Conv2D(64, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img)
      5 
----> 6 x.layers[0].get_weights()[0]

AttributeError: 'Tensor' object has no attribute 'layers'

我没有使用顺序模型。我的模型将在几个这样的层之后使用以下命令形成。

 model = Model()

我是 CNN 的新手,我什至不知道 get_weights 函数是否可以帮助我获取过滤器值。我如何获得过滤器的价值?

目前您的代码正在层定义本身上调用 layers 函数。

首先需要编译模型,然后您可以在模型上使用layers函数来检索特定层的权重。

你的情况:

weights = model.layers[1].get_weights()

会给你第一个卷积层的权重集

编译模型后可以使用:

model = Model(inputs=input_img, output=b)

其中 b 指模型中的最后一层。