An image contains useful patterns wherever they appear: an edge in one corner can matter just as much as the same edge in another. A convolutional neural network (ConvNet or CNN) learns local patterns in small windows, rather than asking every layer to learn relationships across the entire image. This makes ConvNets a practical choice for image classification. section 8.1.1 section 20.1.6
Start with handwritten digits
A small ConvNet classifies 28 × 28 grayscale MNIST digits by alternating Conv2D and MaxPooling2D layers, then pooling the resulting features and producing ten class probabilities. Its reported test accuracy is 0.991, compared with 0.978 for an earlier densely connected model. The example is basic, but it shows why preserving image structure is useful. section 8.1
Listing 8.1 Instantiating a small ConvNet
import keras from keras import layers inputs = keras.Input(shape=(28, 28, 1)) x = layers.Conv2D(filters=64, kernel_size=3, activation="relu")(inputs) x = layers.MaxPooling2D(pool_size=2)(x) x = layers.Conv2D(filters=128, kernel_size=3, activation="relu")(x) x = layers.MaxPooling2D(pool_size=2)(x) x = layers.Conv2D(filters=256, kernel_size=3, activation="relu")(x) x = layers.GlobalAveragePooling2D()(x) outputs = layers.Dense(10, activation="softmax")(x) model = keras.Model(inputs=inputs, outputs=outputs)
Follow the complete Keras model from its image-shaped input through convolution, pooling, and the softmax classifier. section 8.1
Read the stack from input to output:
- Input is a height × width × channels tensor: MNIST has one grayscale channel; RGB images have three. Keras uses channels-last by default, though the format is configurable. section 8.1 section 8.1.1
- Each convolution applies the same learned transformation to patches across the input. Its output is a feature map whose channels represent learned patterns. Reusing a pattern across locations helps the model learn from fewer examples. section 8.1.1
- As layers accumulate, early patterns such as edges can combine into more abstract visual concepts. In typical stacks, spatial dimensions shrink while the number of feature channels grows. section 8.1.1 section 8.2.3
- A final pooling layer and Dense classifier turn the learned features into predictions. For a binary dogs-versus-cats classifier, use one sigmoid output with binary crossentropy. section 8.1 section 8.2.3
For a larger image problem, add convolution-and-pooling stages rather than simply reusing the digit model unchanged. A dogs-versus-cats example takes 180 × 180 RGB images, rescales pixel values, and reduces its feature maps to 7 × 7 before global average pooling. On small datasets, watch for overfitting: data augmentation can help, while feature extraction or fine-tuning can reuse a pretrained ConvNet such as Xception. section 8.2.3 section 8.3 section 8.4
Local-pattern reuse is an assumption, not a guarantee of success. A 1D ConvNet performed poorly on temperature forecasting because morning and evening readings behave differently, and pooling discarded useful order information. For image decisions, inspect learned filters, response maps, or Grad-CAM heatmaps to see what patterns and regions influenced a classification. section 13.2.4 section 10.5

















