Python Keras如何将密集层转换为卷积层 [英] Python keras how to transform a dense layer into a convolutional layer

查看:117
本文介绍了Python Keras如何将密集层转换为卷积层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将权重映射正确以将密集层转换为卷积层时,我遇到了问题.

I have a problem finding the correct mapping of the weights in order to transform a dense layer into a convolutional layer.

这是我正在使用的ConvNet的摘录:

This is an excerpt of a ConvNet that I'm working on:

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

MaxPooling之后,输入的形状为(512,7,7).我想将密集层转换为卷积层,使其看起来像这样:

After the MaxPooling, the input is of shape (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'))

但是,我不知道如何重新调整权重才能正确地将展平的权重映射到卷积层所需的(4096,512,7,7)结构? 目前,致密层的权重为(25088,4096).我需要以某种方式将这25088个元素映射到(512,7,7)的维度,同时保留权重到神经元的正确映射.到目前为止,我已经尝试了多种重塑然后移调的方法,但是我还没有找到正确的映射.

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? Right now, the weights of the dense layer are of dimension (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. 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]

在您的情况下:

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

棘手的部分是转换滤镜[[,,:,::-1,::-1]. Theano确实进行卷积而不是相关(例如与caffe不同).因此,在Keras过滤器中,例如:

The tricky part is conv filters flipping [:,:,::-1,::-1]. Theano does convolution not correlation (unlike caffe e.g.). Hence, in Keras filter like:

1 0
0 0

应用于矩阵:

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

以矩阵形式得出的结果

7 8 9 0 
2 3 4 5

不是这样,正如人们期望的那样:

not this, as one would expect with correlation:

1 2 3 4
6 7 8 9

为了使事情按预期工作,您需要将滤镜旋转180度.刚刚为自己解决了这个问题,希望对您或其他人有帮助.干杯.

In order to make things working as expected, you need to rotate filters 180 deg. Just solved this problem for myself, hopefully this will be of help for you or for others. Cheers.

这篇关于Python Keras如何将密集层转换为卷积层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆