简体   繁体   English

Python keras如何将密集层转换为卷积层

[英]Python keras how to transform a dense layer into a convolutional layer

I have a problem finding the correct mapping of the weights in order to transform a dense layer into a convolutional layer. 我在找到权重的正确映射时遇到问题,以便将密集层转换为卷积层。

This is an excerpt of a ConvNet that I'm working on: 这是我正在研究的ConvNet的摘录:

model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))

After the MaxPooling, the input is of shape (512,7,7). 在MaxPooling之后,输入的形状(512,7,7)。 I would like to transform the dense layer into a convolutional layer to make it look like this: 我想将密集层转换为卷积层,使其看起来像这样:

model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(4096, 7, 7, activation='relu'))

However, I don't know how I need to reshape the weights in order to correctly map the flattened weights to the (4096,512,7,7) structure that is needed for the convolutional layer? 但是,我不知道如何重新塑造权重以便将扁平权重正确映射到卷积层所需的(4096,512,7,7)结构? Right now, the weights of the dense layer are of dimension (25088,4096). 现在,致密层的重量具有尺寸(25088,4096)。 I need to somehow map these 25088 elements to a dimension of (512,7,7) while preserving the correct mapping of the weights to the neurons. 我需要以某种方式将这些25088元素映射到(512,7,7)维度,同时保留权重到神经元的正确映射。 So far, I have tried multiple ways of reshaping and then transposing but I haven't been able to find the correct mapping. 到目前为止,我已经尝试了多种重塑方式然后进行转置,但我无法找到正确的映射。

An example of what I have been trying would be this: 我一直在尝试的一个例子是:

weights[0] = np.transpose(np.reshape(weights[0],(512,7,7,4096)),(3,0,1,2))

but it doesn't map the weights correctly. 但它没有正确映射权重。 I verified whether the mapping is correct by comparing the output for both models. 我通过比较两个模型的输出来验证映射是否正确。 If done correctly, I expect the output should be the same. 如果正确完成,我希望输出应该是相同的。

Still looking for solution? 仍在寻找解决方案? Here it is: 这里是:

new_conv_weights = dense_weights.transpose(1,0).reshape(new_conv_shape)[:,:,::-1,::-1]

in your case: 在你的情况下:

weights[0] = weights[0].transpose(1,0).reshape((4096,512,7,7))[:,:,::-1,::-1]

The tricky part is conv filters flipping [:,:,::-1,::-1]. 棘手的部分是转换flipping [:,:,:: - 1,:: - 1]。 Theano does convolution not correlation (unlike caffe eg). Theano确实卷积不相关(与caffe不同)。 Hence, in Keras filter like: 因此,在Keras过滤器中:

1 0
0 0

applied to matrix: 应用于矩阵:

1 2 3 4 5
6 7 8 9 0
1 2 3 4 5

results in matrix: 结果矩阵:

7 8 9 0 
2 3 4 5

not this, as one would expect with correlation: 不是这个,正如人们所期望的那样:

1 2 3 4
6 7 8 9

In order to make things working as expected, you need to rotate filters 180 deg. 为了使工作按预期工作,您需要将滤镜旋转180度。 Just solved this problem for myself, hopefully this will be of help for you or for others. 刚刚为我自己解决了这个问题,希望这对你或他人都有帮助。 Cheers. 干杯。

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

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