liveBook · Manning

Convolutional neural network

As explained in Deep Learning with Python, Third Edition

A neural network built from convolution and pooling layers that processes data with local spatial structure.

A convolutional neural network (CNN or ConvNet) learns patterns from local regions of its input. For images, this makes it useful for recognizing features wherever they appear, rather than learning a separate pattern for every location. section 8.1.1 section 8.0

Classifying handwritten digits

A small MNIST ConvNet takes 28 × 28 grayscale images, alternates convolution and max-pooling layers, and finishes with global average pooling and a 10-class softmax classifier. After five training epochs, the example achieves 99.1% test accuracy, compared with 97.8% for the earlier densely connected model. 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)

Inspect the Keras model: three Conv2D layers build feature maps, pooling reduces their spatial dimensions, and a Dense layer predicts digit classes. section 8.1

A convolution applies the same learned transformation to small patches across a feature map. Each output channel records a learned pattern’s response at different positions. Early layers can detect edges and textures; later layers combine earlier features into more abstract patterns. Pooling reduces spatial dimensions, while convolution layers often increase the number of channels. section 8.1.1 section 8.2.3 section 10.5

Practical choices for image classification:

  • Match the input format to the framework. Keras commonly uses (height, width, channels); channels-first is configurable. section 8.1
  • For a binary classifier, use a single sigmoid output with binary crossentropy. A larger input may call for more convolution and pooling stages. section 8.2.3
  • With limited image data, watch for overfitting. Data augmentation can help; a pretrained ConvNet can also supply features for a new task, with fine-tuning as a further adaptation. section 8.4 section 8.3

ConvNets are not limited to images: Conv1D slides windows over sequences, and Conv3D uses windows over volumes. Their assumptions must fit the data. In temperature forecasting, a 1D ConvNet performed poorly where the time of day and the recency of observations mattered; pooling also discarded much of the order information. section 13.2.4 section 20.1.6

To inspect an image ConvNet, visualize intermediate activations or learned filters. Grad-CAM can highlight image areas associated with a classification decision. section 10.0 section 10.5

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains local convolution, feature maps, spatial hierarchies, model construction, transfer learning, and visualization; defers Xception’s architectural details.
Examples
Works through MNIST digit classification, cats-versus-dogs classification, and a 1D temperature-forecasting model.
What it assumes
Introduces convolution through a concrete model while drawing on prior familiarity with dense classifiers and the Keras Functional API.
What you can do after
Apply ConvNets via Keras to classification and assess whether their assumptions fit a forecasting problem.
How it connects
Connects convolution to pooling, dense classifiers, data augmentation, transfer learning, fine-tuning, and Grad-CAM.

Also explained in

Compare how 18 books explain convolutional neural network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.