Concepts / Convolutional layers

Convolutional layers

The diagram represents the model as a sequence, not as four independent layer declarations. The first Conv2D layer is followed by a MaxPooling2D layer, then another Conv2D layer, and finally another MaxPooling2D layer.

  • Programming
Interactive lab

Try it: Convolution

How a convolution layer slides a small kernel over an image, multiplying and summing at each position to build a feature map.

How it works

  1. Place the 3×3 kernel over the top-left 3×3 window of the input.
  2. Multiply each input value by the kernel weight on top of it and add the nine products — that sum is one output cell.
  3. Slide the window by the stride and repeat until every position is covered.
  4. Zero padding adds a border so edge pixels are covered too; output size = ⌊(n + 2p − k) / s⌋ + 1.

Default run (11 steps): 5×5 input, 3×3 kernel, stride 1. Output is ⌊(5 + 2×0 − 3) / 1⌋ + 1 = 3 per side. … Done: the kernel visited all 9 positions and produced a 3×3 feature map.

Simplified: One 5×5 single-channel input and one 3×3 kernel. As in deep-learning libraries, the kernel is not flipped (strictly, cross-correlation).

Educational simulation

Loading the simulation…

Read the Model from Top to Bottom

A small convolutional network can be understood by reading its model definition as an ordered list. The example creates a Keras Sequential model and adds Conv2D and MaxPooling2D layers to it. Before asking how the network is trained, first identify what each layer contributes and where it appears in the sequence.

What do you think happens?

Before examining the individual arguments, predict the structure of the small convnet: how many Conv2D layers are added, how many MaxPooling2D layers are added, and which type is added first?

  • One Conv2D layer, two MaxPooling2D layers, with pooling first

  • Two Conv2D layers, two MaxPooling2D layers, with Conv2D first

  • Two Conv2D layers, one MaxPooling2D layer, with pooling first

Reveal answer
Answer:

Two Conv2D layers and two MaxPooling2D layers are added, with Conv2D first.

The source definition alternates convolution and pooling. Reading each model.add call from top to bottom reveals the order.

The Sequential Layer Stack

A Sequential model is a linear stack of layers used for building simple neural networks. In this example, each call to model.add adds the next layer to that stack. Consequently, the order of the model.add calls is not merely presentation order: it is the intended order through which data moves in the model.

Conv2Dfilters, (3, 3),activation, input shapeMaxPooling2D(2, 2)Conv2Dfilters, (3, 3), activationMaxPooling2D(2, 2)next layernext layernext layer
How does data move through the Sequential model as the Conv2D and MaxPooling2D layers are added in order?
Key point

The diagram represents the model as a sequence, not as four independent layer declarations. The first Conv2D layer is followed by a MaxPooling2D layer, then another Conv2D layer, and finally another MaxPooling2D layer.

What Each Layer Contributes

Definition

Conv2D is a two-dimensional convolutional layer used for image-processing tasks. In the source listing, each Conv2D call specifies a number of filters and a kernel size. The kernel size shown is (3, 3). The first Conv2D call also specifies an activation and the input shape.

Definition

MaxPooling2D is a two-dimensional max-pooling layer used for downsampling images. It requires a pool size. Both MaxPooling2D calls in the source listing use the pool size (2, 2).

LayerRole in the exampleConfiguration to look for
Conv2DTwo-dimensional convolutional processing for image-processing tasksFilter count and kernel size; the first call also shows activation and input shape
MaxPooling2DDownsamplingPool size, shown as (2, 2)
Sequential modelLinear stack that stores the layers in added orderThe order of the model.add calls

A reading guide for the three main parts of the model definition

Conv2Dlayer typefilter countnumber of filters(3, 3)kernel sizeactivationshown on the first callinput shapeshown on the first callconfigured byalongsidefirst call also includesfirst call also includes
What does the Conv2D layer represent, and where do its configuration values appear in the layer declaration?

Tracing the Complete Definition

Worked example

Identifying the layer sequence

Read the small Keras convnet from top to bottom and record the layer type and important configuration at each addition.

  1. 1
    First addition

    The first layer added is Conv2D. Its declaration includes a filter count, the kernel size (3, 3), an activation argument, and the input shape.

  2. 2
    Second addition

    The next layer added is MaxPooling2D. Its pool size is (2, 2), and this layer is used for downsampling.

  3. 3
    Third addition

    A second Conv2D layer is added. It specifies a filter count, the kernel size (3, 3), and the activation argument.

  4. 4
    Fourth addition

    A second MaxPooling2D layer is added with the pool size (2, 2).

Result

The model is a four-layer sequence: Conv2D, MaxPooling2D, Conv2D, MaxPooling2D.

This trace shows why reading from top to bottom is useful. It identifies both the layer count and the alternating pattern. It also distinguishes the first Conv2D call from the later Conv2D call: the first includes the input shape, while the later Conv2D calls specify filter counts, kernel sizes, and the activation argument.

Configuration-Reading Mistakes

  • Common mistake

    Counting only one Conv2D layer because the two convolutional declarations are similar.

    What it looks like

    The source definition adds Conv2D, then MaxPooling2D, then another Conv2D.

    Why it fails

    Similar configuration does not make two model.add calls into one layer.

    Do this instead

    Count each model.add call separately while reading from top to bottom.

  • Common mistake

    Calling MaxPooling2D a convolutional layer.

    What it looks like

    Treating the pool-size argument (2, 2) as if it were the Conv2D kernel size.

    Why it fails

    MaxPooling2D is a separate two-dimensional max-pooling layer used for downsampling.

    Do this instead

    Check the layer type first, then interpret its arguments according to that layer.

  • Common mistake

    Confusing the Conv2D kernel size with the MaxPooling2D pool size.

    What it looks like

    Replacing the Conv2D kernel size (3, 3) with the MaxPooling2D pool size (2, 2) when describing the model.

    Why it fails

    The source uses (3, 3) for Conv2D kernel size and (2, 2) for both pooling operations.

    Do this instead

    Associate (3, 3) with Conv2D and (2, 2) with MaxPooling2D.

  • Common mistake

    Assuming every Conv2D call has exactly the same full configuration.

    What it looks like

    Saying that the later Conv2D calls also include the input shape.

    Why it fails

    The first Conv2D call includes the input shape; the later Conv2D calls specify filter counts, kernel sizes, and the activation argument.

    Do this instead

    Inspect each declaration rather than copying the first layer's complete set of arguments to every later layer.

  • Common mistake

    Ignoring the order of model.add calls.

    What it looks like

    Listing all convolutional layers first and all pooling layers afterward.

    Why it fails

    A Sequential model is a linear stack, so the added order reveals the intended model order.

    Do this instead

    Record the layer type after every model.add call.

Best practice

When debugging a model declaration, compare the intended sequence with the actual model.add calls one call at a time. For each call, check the layer type first, then check its filter or pool-size argument, and finally check additional configuration arguments such as the kernel size, activation, or input shape.

Check Your Reading

EASY

Write the four layer types in the order they are added in the source example. Then annotate each Conv2D entry with the kernel size shown, and each MaxPooling2D entry with the pool size shown. Finally, note which Conv2D entry includes the input shape.

Hints
  • Start with the first model.add call and move downward.
  • The two Conv2D calls use (3, 3) as their kernel size.
  • Both MaxPooling2D calls use (2, 2), and the first Conv2D call includes the input shape.
  1. 1
    A Sequential model represents a linear stack of layers, and each model.add call appends the next layer. In the source example, the stack alternates Conv2D and MaxPooling2D. Conv2D is a two-dimensional convolutional layer whose declarations specify filter counts and a (3, 3) kernel size; the first call also includes activation and input shape. MaxPooling2D is a two-dimensional max-pooling layer used for downsampling, and both of its calls use a (2, 2) pool size. The safest way to debug the definition is to inspect each layer addition from top to bottom.

Key Takeaways

  • Sequential stores the model as a linear, ordered stack of layers.
  • The source model adds two Conv2D layers and two MaxPooling2D layers in alternating order.
  • Conv2D declarations show filter counts and a (3, 3) kernel size; the first Conv2D also shows activation and input shape.
  • MaxPooling2D performs downsampling in the example and uses a (2, 2) pool size in both calls.
  • To debug the declaration, inspect each model.add call by checking its layer type, its main configuration value, and its additional arguments.

Related concepts

More concepts starting with C