Search code examples
pythonkerasdeep-learning

Keras CNN architecture


I need to understand how convolution works in the following code and how the shape of the array changes after each layer/step

in Conv2D(32,3,1,...) 32 is the number of filters but what is 3 and 1?

What I think is that I have a table of n_rows (number of rows) and n_cols (number of columns). this table is reshaped so that columns are transferred to another dimension (number of dimensions was increased).

Now the 2D convolution: each row is convolved with 32 filters, what is the resultant shape of matrix after this step? convolution occurs to each raw alone or does it mix more than one row?

Xc_train = X_train.reshape((n_rows, 1, n_cols, 1))

cnn = Sequential()
cnn.add(Convolution2D(32, 3, 1,
    border_mode="same",
    activation="relu",
    input_shape=(1, n_cols, 1)))
cnn.add(Convolution2D(32, 3, 1, border_mode="same", activation="relu"))

Solution

  • This is old code, Keras 1 version.

    It should be updated to Keras 2 to be according with the documentation.

    • Those numbers 3 and 1 are the size of the filter, 3 x 1 pixels.
    • The shape of input data should be (n_images, n_rows, n_cols, n_channels)

    With padding = 'same' (border_mode in old code), the image size will not be changed.

    The only dimension that changes is the channels dimension.

    All convolution filters always consider all the input channels, mix them together and bring a new amount of output channels (this is very very similar to what Dense layers do, except that you're doing it for the entire image, for each pixel).