The math behind the Conv2D function in Keras

I am using a Conv2D

model Keras 2.0

. However, I cannot fully understand what the function does mathematically. I am trying to understand the math using randomly generated data and a very simple web:

import numpy as np
import keras
from keras.layers import Input, Conv2D
from keras.models import Model
from keras import backend as K

# create the model
inputs = Input(shape=(10,10,1)) # 1 channel, 10x10 image
outputs = Conv2D(32, (3, 3), activation='relu', name='block1_conv1')(inputs)
model = Model(outputs=outputs, inputs=inputs)

# input
x = np.random.random(100).reshape((10,10))

# predicted output for x
y_pred = model.predict(x.reshape((1,10,10,1))) # y_pred.shape = (1,8,8,32)

      

I tried to calculate, for example, the value of the first row, first column in the first function map, after a demonstration in here .

w = model.layers[1].get_weights()[0] # w.shape = (3,3,1,32)
w0 = w[:,:,0,0]
b = model.layers[1].get_weights()[1] # b.shape = (32,)
b0 = b[0] # b0 = 0

y_pred_000 = np.sum(x[0:3,0:3] * w0) + b0

      

But relu(y_pred_000)

not equal y_pred[0][0][0][0]

.

Can anyone point out what happened to my understanding? Thank.

+3


source to share


1 answer


It's easy and it comes from Theano

dim ordering. The result of applying the filter is stored in a so-called dimension channel

. In case TensorFlow

this is the last dimension and why the results are good. In the case, Theano

this is the second dimension (the result of the convolution has a shape (cases, channels, width, height)

, so to solve your problem, you need to change the prediction line to:

y_pred = model.predict(x.reshape((1,1,10,10))) 

      



Also, you need to change the way you get the weights, since the weights in Theano

have a shape (output_channels, input_channels, width, height)

, you need to change the getter weight to:

w = model.layers[1].get_weights()[0] # w.shape = (32,1,3,3)
w0 = w[0,0,:,:]

      

+2


source







All Articles