liveBook · Manning

Convolutional neural network

As explained in Deep Learning with PyTorch, Second Edition

A neural network that uses convolutional layers to process spatial data.

A convolutional neural network (CNN) processes an image without first treating it as one long list of pixels. A fully connected network connects every neuron to every neuron in the preceding layer; for image classification, that can create many parameters while making it difficult to recognize an object in different positions. A CNN replaces some of those dense transformations with convolutions. section 8.0

A convolution applies a small kernel across an image. A hand-set kernel can blur an image or highlight vertical edges. In a CNN, training instead adjusts kernels to produce useful feature maps: successive layers turn a multichannel image into new channels representing learned features. section 8.2.2

Small kernels need help detecting larger patterns. Stacking convolutions lets later layers work with earlier features; pooling reduces the spatial size of feature maps between layers. For example, 2 × 2 max pooling keeps the largest value from each tile, changing a 32 × 32 image into a 16 × 16 feature map. section 8.2.3

Bird or airplane?

Start with a three-channel image. A convolution produces 16 channels, followed by Tanh and max pooling; a second convolution produces eight channels, followed by another Tanh and pooling. The resulting 8 × 8 feature maps must be flattened before fully connected layers can produce two classification outputs. Leaving out that reshape causes a matrix-shape error rather than a prediction. section 8.2.4

# In[23]:
model = nn.Sequential(
            nn.Conv2d(3, 16, kernel_size=3, padding=1),
            nn.Tanh(),
            nn.MaxPool2d(2),
            nn.Conv2d(16, 8, kernel_size=3, padding=1),
            nn.Tanh(),
            nn.MaxPool2d(2),
            # ...                       #1
            nn.Linear(8 * 8 * 8, 32),
            nn.Tanh(),
            nn.Linear(32, 2))

The PyTorch layer listing makes the two convolution-and-pooling stages and the final linear layers concrete. Its placeholder is important: flattening is still needed between them. section 8.2.4

The same broad architecture can be adapted beyond small 2D photographs. A first-pass tumor detector uses 3D input and a design built around repeated blocks, with additional changes for that task. section 13.4

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through learned kernels, pooling, layer shapes and a PyTorch classifier, including a missing-reshape error.
Examples
Uses blurred and edge-highlighted bird images, a bird-versus-airplane classifier and a 3D tumor-detection design.
What it assumes
Builds on a fully connected image classifier while introducing convolutions and pooling through concrete examples.
What you can do after
Apply it via PyTorch nn.Conv2d and nn.MaxPool2d to assemble a bird-versus-airplane classifier.
How it connects
Connects convolution kernels and feature maps to max pooling, activation functions and fully connected layers.

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 PyTorch, Second Edition. The section links point to the parts of the book it draws on.