Python keras how to transform a dense layer into a convolutional layer
<p>I have a problem finding the correct mapping of the weights in order to transform a dense layer into a convolutional layer.</p>

<p>This is an excerpt of a ConvNet that I'm working on:</p>

<pre><code>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'))
</code></pre>

<p>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:</p>

<pre><code>model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(4096, 7, 7, activation='relu'))
</code></pre>

<p>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. </p>

<p>An example of what I have been trying would be this:</p>

<pre><code>weights[0] = np.transpose(np.reshape(weights[0],(512,7,7,4096)),(3,0,1,2))
</code></pre>

<p>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.</p>