Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 40 ยท Convolutional Networks

Building a CNN with Keras

Build and train a convolutional network in Keras for image classification: stacking Conv2D and pooling, reading model.summary, counting parameters and avoiding overfitting.

You now know the parts: convolution, padding, strides and pooling. This lesson assembles them into a complete image classifier. The recipe is almost always the same: a feature extractor (repeated blocks of convolution and pooling that make the image smaller and the channel count larger) followed by a classifier head (global pooling or flatten, then dense layers ending in one output per class).

Keras and TensorFlow do not run in the browser here, so the model code is read-only. The runnable blocks use NumPy to compute shapes, parameter counts and a forward pass, which is exactly what model.summary() does for you.

A small CNN for 28x28 digits

Python
import keras
from keras import layers

model = keras.Sequential([
    keras.Input(shape=(28, 28, 1)),
    layers.Conv2D(32, 3, activation="relu"),
    layers.MaxPooling2D(2),
    layers.Conv2D(64, 3, activation="relu"),
    layers.MaxPooling2D(2),
    layers.Flatten(),
    layers.Dropout(0.5),
    layers.Dense(10, activation="softmax"),
])

model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)
model.summary()

Each block halves the spatial size and doubles the channels. The idea is that early layers see small details at high resolution, and later layers see larger structures built from them, so we trade spatial size for more feature channels.

Computing the summary by hand

Reproduce what Keras prints. A Conv2D has k*k*c_in*c_out + c_out parameters; pooling and flatten have none; a Dense layer has in*out + out.

Python Shape and parameter calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result is 34,826 parameters, and the 1600 flattened values feeding the dense layer account for about 16,000 of them. If you removed the pooling layers, that dense layer would need more than 20 times as many inputs (36,864 instead of 1600). This is why pooling and global pooling matter.

A forward pass in NumPy

Training adjusts weights, but the forward pass is just the layer math. Here is a tiny network (one convolution, ReLU, pooling, dense, softmax) running on a made-up image.

Python Forward pass of a tiny CNN

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The probabilities add up to 1 and the predicted class is the largest one. With random weights the prediction is meaningless; training moves the weights until the right class wins.

Training it

Python
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.astype("float32")[..., None] / 255.0
x_test = x_test.astype("float32")[..., None] / 255.0

history = model.fit(
    x_train, y_train,
    batch_size=128,
    epochs=5,
    validation_split=0.1,
)
print(model.evaluate(x_test, y_test))

Points worth remembering:

  • Scale pixels to 0 to 1 and add the channel axis ([..., None]); Conv2D needs (28, 28, 1), not (28, 28).
  • sparse_categorical_crossentropy takes integer labels. Use categorical_crossentropy if your labels are one-hot vectors.
  • With a model like this, accuracy of roughly 98 to 99 percent on MNIST is typical after a few epochs. Exact numbers vary from run to run.

Making the network deeper without exploding

Common upgrades, all read-only here:

Python
inputs = keras.Input(shape=(32, 32, 3))
x = layers.Conv2D(32, 3, padding="same", use_bias=False)(inputs)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(32, 3, padding="same", activation="relu")(x)
x = layers.MaxPooling2D()(x)
x = layers.Dropout(0.25)(x)
# ... repeat with 64 and 128 filters ...
x = layers.GlobalAveragePooling2D()(x)
outputs = layers.Dense(10, activation="softmax")(x)
model = keras.Model(inputs, outputs)

This uses the functional API (see Building Models with Keras). Padding "same" keeps sizes predictable, BatchNormalization stabilizes training, and global average pooling keeps the head small.

Reading signs of trouble

  • Training accuracy keeps rising but validation accuracy stalls: overfitting. Add dropout, data augmentation (next lesson), or reduce the model.
  • Both stay low: the model is too small, the learning rate is wrong, or the inputs are not scaled.
  • A shape error mentioning (None, 28, 28): you forgot the channel axis.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many parameters does Conv2D(32, 3) have on a single-channel input?

    331*32 weights plus 32 biases = 320.
  2. 2. Which loss should you use with integer class labels and softmax outputs?

    Sparse categorical cross-entropy accepts integer labels directly; categorical cross-entropy expects one-hot vectors.
  3. 3. In a typical CNN feature extractor, what happens to height/width and channels as you go deeper?

    Pooling or strides reduce spatial size while each new convolution block uses more filters.
  4. 4. Your model gets 99% training accuracy but 90% validation accuracy. This is most likely:

    A large gap between training and validation performance is the classic sign of overfitting.

Summary

  • A CNN is a feature extractor (Conv2D plus pooling blocks) and a classifier head (Flatten or global pooling plus Dense).
  • Convolutions have k*k*c_in*c_out + c_out parameters; pooling has none.
  • Add the channel axis and scale pixels before training; pick the loss that matches your label format.
  • Use model.summary() to check shapes and parameter counts before you train.
  • A train/validation gap means overfitting; the next lesson shows how to reduce it.

Next: Augmentation and Transfer Learning