1. Home
  2. Explore
  3. Computer Science
  4. Convolutional Neural Networks

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Convolutional Neural Networks

Convolutional Neural Networks

Image classification as a learning problem

A Convolutional Neural Network (CNN) is a neural-network architecture designed especially for image-related tasks. Image classification provides a useful example of how an architecture can incorporate assumptions about a problem and thereby improve performance.

In image classification, the model receives an image and predicts which object or category it contains. The discussion assumes that images have a fixed input size, such as 100×100100 \times 100100×100 pixels. In practice, images may have different dimensions or aspect ratios, so they are commonly resized to a consistent size before being given to the model.

The target for a classification task can be represented as a one-hot vector:

  • The vector has one position, or dimension, for each possible class.

  • The correct class has value 111; all other classes have value 000.

  • For example, if the image contains a cat, the cat’s position is 111, and the other class positions are 000.

  • The vector’s length determines how many classes the model can represent. A length of 2,0002{,}0002,000 represents 2,0002{,}0002,000 possible classes; large image-classification systems may distinguish thousands or even tens of thousands of objects.

The model produces a prediction, commonly by applying Softmax to its output. Training aims to make the prediction agree with the target, often by minimizing cross-entropy between them.


Representing an image for a neural network

Images as tensors

To a computer, a color image can be represented as a three-dimensional tensor: an array with more than two dimensions. Its dimensions correspond to:

  • Height: the number of pixel rows.

  • Width: the number of pixel columns.

  • Channels: the number of values stored for each pixel.

A standard color image has three channels—red, green, and blue—so each pixel is represented by three intensity values. A grayscale image generally has one channel.

For an image of size 100×100100 \times 100100×100 with three color channels, the total number of values is:

100×100×3=30,000100 \times 100 \times 3 = 30{,}000100×100×3=30,000

Each value represents the intensity of one color channel at one pixel location.

Flattening an image

A fully connected neural network generally expects a vector as input. An image tensor can be converted into a vector by flattening it: arranging all pixel-channel values in a single sequence. The 100×100×3100 \times 100 \times 3100×100×3 image therefore becomes a vector of length 30,00030{,}00030,000.

Flattening makes the image usable as input to a fully connected network, but it does not take advantage of the way images are structured. It also produces a very large number of connections.

The parameter cost of full connectivity

In a fully connected layer, every neuron has a weight for every input value. If the input vector has 30,00030{,}00030,000 values and the first layer has 1,0001{,}0001,000 neurons, that layer has:

30,000×1,000=30,000,00030{,}000 \times 1{,}000 = 30{,}000{,}00030,000×1,000=30,000,000

weights, not counting biases. Such a large parameter count increases computation and gives the model considerable flexibility. Greater flexibility can help a model learn complex relationships, but it can also increase the risk of overfitting: fitting the training examples too closely without generalizing well to new data.

CNNs reduce unnecessary parameters by using assumptions about how visual information is organized.


Why image structure suggests a different architecture

Local patterns carry useful information

Objects can often be recognized from combinations of meaningful local features. A bird, for example, may be associated with patterns such as a beak, an eye, or claws. People also rely on characteristic features when identifying objects.

A neuron looking for a particular small visual pattern does not necessarily need to inspect every pixel in the entire image. It may be sufficient for it to inspect only a small neighborhood. This motivates the CNN concept of a receptive field.

Receptive fields

A receptive field is the region of the input image that a particular neuron examines. Instead of connecting each neuron to every value in the image, a CNN can restrict the neuron to a local region.

For example, a neuron might examine a region of size 3×33 \times 33×3 pixels across all three color channels. This region contains:

3×3×3=273 \times 3 \times 3 = 273×3×3=27

values. The neuron uses a weight for each of those values, along with a bias, to calculate its response. In general, it computes a weighted combination of the values in its receptive field and then applies an activation function.

Receptive fields are a design choice:

  • They may be larger or smaller, depending on the patterns the task requires.

  • They may be rectangular rather than square.

  • In some specialized architectures, they may include selected channels rather than all channels.

  • They can overlap, and a single image region can be monitored by multiple neurons.

  • They are usually connected local regions because common visual patterns tend to occupy a local part of an image. Disconnected regions could be useful for a specially designed task, but are not the usual choice for ordinary image recognition.

A common arrangement uses a receptive field spanning all input channels and describes its spatial dimensions by its kernel size. For example, a kernel size of 3×33 \times 33×3 means that the filter examines three rows and three columns. Kernels of 3×33 \times 33×3 are common in image recognition; sizes such as 7×77 \times 77×7 or 9×99 \times 99×9 are larger.

A receptive field at each image location is often handled by a group of neurons, rather than just one. For example, a group might contain 646464 or 128128128 neurons, allowing the same local region to be examined for multiple patterns.

Stride and coverage

A receptive field is moved across the image to examine different locations. The distance it moves each time is called the stride.

  • A stride of 111 moves the receptive field one position at a time.

  • A stride of 222 moves it two positions at a time.

The stride is a hyperparameter chosen when designing the network. Small strides, often 111 or 222, create substantial overlap between neighboring receptive fields. Overlap helps avoid missing a pattern that lies between two positions.

The receptive field is typically moved both horizontally and vertically so that different regions of the image are covered.

Padding and image boundaries

When a receptive field reaches an edge, part of it may extend beyond the image. Padding supplies values outside the original image so the filter can still be applied near the boundary.

The simplest method is zero padding, which treats all values beyond the edge as 000. Other padding methods can use values such as the image average or repeat values from the edge. Padding makes it possible to process boundary regions instead of leaving them uncovered.


Parameter sharing and filters

Reusing the same pattern detector

The same visual pattern can occur at many locations. A bird’s beak, for instance, might appear in the upper-left, center, or another part of an image. If each location had a separate neuron with an independent set of weights for detecting that pattern, many parameters would be duplicated.

Parameter sharing addresses this redundancy: neurons that inspect different image regions can use the same weights. Their outputs need not be identical, because they receive different local inputs. A shared set of weights applied to one region may produce a different result from the same weights applied to another region.

In a typical CNN, corresponding neurons at different image locations share parameters. Thus, the same detector can search the image for a particular pattern without learning a separate copy of that detector for every location.

Filters

A shared set of weights is called a filter. A filter can be thought of as a learned pattern detector. If a layer has several filters, each can learn to respond to a different local feature.

For example, one filter might respond strongly to a particular arrangement of pixels, while another responds to a different pattern. A convolutional layer commonly uses many filters—perhaps 646464, 128128128, or more—so it can detect multiple kinds of features.

A filter generally has a bias as well as weights. Its values are learned from data during training.


Convolutional layers and feature maps

How a filter is applied

A convolutional layer applies each filter across the input image:

  1. Place a filter over a local region of the image.

  2. Multiply corresponding filter and image values.

  3. Sum the products to obtain a response for that location, with a bias typically added.

  4. Apply an activation function, such as ReLU.

  5. Move the filter according to the chosen stride and repeat across the image.

For a grayscale input, a 3×33 \times 33×3 filter contains 999 values. At each location, those values are paired with the 3×33 \times 33×3 image patch and combined using an inner product. A filter responds strongly where the image patch resembles the pattern that the filter has learned to detect.

Feature maps

Applying one filter across the image produces a two-dimensional collection of responses called a feature map. Each response indicates how strongly the filter detects its pattern at a particular location.

Applying multiple filters produces multiple feature maps:

  • One filter produces one map.

  • A layer with 646464 filters produces 646464 maps.

  • These maps can be viewed as the channels of a new, transformed image.

For example, applying a 3×33 \times 33×3 filter to a 6×66 \times 66×6 image with stride 111 and no padding produces a 4×44 \times 44×4 map, because the filter can occupy four valid positions horizontally and four vertically.

The output’s number of channels is determined by the number of filters in the layer. A layer with 646464 filters therefore produces an output with 646464 channels.

Stacking convolutional layers

Convolutional layers can be stacked. The input to a later layer is the feature-map output of the previous layer.

If the previous layer produces 646464 channels, each filter in the next layer must process those 646464 channels. A filter with spatial size 3×33 \times 33×3 therefore has dimensions 3×3×643 \times 3 \times 643×3×64.

Although individual filters may remain spatially small, deeper layers can respond to larger regions of the original image. A unit in a second convolutional layer combines information from several neighboring positions in the first layer’s feature map. Those positions themselves correspond to different regions of the original image. Consequently, stacking 3×33 \times 33×3 convolutions expands the effective receptive field. In the described two-layer example, a 3×33 \times 33×3 region of the first layer’s map covers a 5×55 \times 55×5 region of the original image.

Thus, deeper networks can detect larger patterns without requiring every layer to use a large kernel.


Two complementary ways to understand CNNs

CNNs can be understood through two equivalent descriptions:

Neuron and receptive-field view

  • Each neuron observes only a local receptive field.

  • Multiple neurons at different locations can share weights.

  • The shared weights allow the same pattern detector to be applied across the image.

Filter and feature-map view

  • A filter is a shared set of weights.

  • The filter slides across the image, producing a feature map.

  • Multiple filters produce multiple output channels.

The filter-scanning operation is what is commonly called convolution in this context. The receptive-field description explains how individual neurons are connected; the filter description explains how shared weights are applied systematically across the image.


CNNs as a form of inductive bias

A fully connected layer can, in principle, learn to ignore most of the image by assigning zero weight to irrelevant pixels. It can also learn different weights for similar patterns at different locations. This gives it considerable flexibility, but it leaves the model to discover useful image structure from data.

CNNs deliberately restrict this flexibility:

  • Local receptive fields require neurons to focus on small regions.

  • Parameter sharing requires the same detector to be used across locations.

These constraints create an inductive bias: the network is built around assumptions about the task. For image recognition, the assumptions that useful patterns are local and may appear in different locations are often effective. The constraints reduce the number of parameters and can reduce overfitting, while preserving strong performance on image tasks.

A larger model bias is not automatically harmful. A highly flexible fully connected network may fit many kinds of relationships, but that flexibility can make it more prone to overfitting. A CNN’s constraints can be beneficial when they match the task. They may be unsuitable when the task does not share the relevant image-like properties.


Pooling

Purpose and behavior

Pooling is a commonly used operation that reduces the spatial dimensions of feature maps. The motivation is that modest downsampling may preserve the identity of an object while making the representation smaller. For example, a smaller image of a bird may still clearly depict a bird.

Pooling has no learned weights and does not learn its behavior from data. It is a fixed operation, which is why it is sometimes compared to an activation function rather than regarded as a parameter-learning layer.

Max pooling and other variants

In max pooling, a small window is divided into groups, and each group is represented by its largest value. For example, a 2×22 \times 22×2 pooling window replaces four values with their maximum.

Other possible pooling choices include:

  • Min pooling: selecting the smallest value.

  • Average pooling: selecting the mean value.

  • Geometric-mean pooling: representing a group with its geometric mean.

The window size is also a design choice. A 2×22 \times 22×2 window is common, but other sizes can be used.

Effect on feature-map dimensions

Pooling generally reduces height and width while keeping the number of channels unchanged. For example, a feature map of size 4×44 \times 44×4 becomes 2×22 \times 22×2 when each non-overlapping 2×22 \times 22×2 group is replaced by one value. If the original map has 646464 channels, the pooled map still has 646464 channels.

Pooling can reduce subsequent computation, but downsampling can remove fine details. This may harm performance when the task depends on small, precise features. Some modern image-recognition networks therefore omit pooling and use fully convolutional designs instead. The choice depends on the task and available computational resources.


A common image-recognition pipeline

A classic CNN image-classification system may use this sequence:

  1. Convolutional layers extract local patterns and produce feature maps.

  2. Pooling operations, if used, reduce the maps’ spatial dimensions.

  3. Further convolution and pooling operations build increasingly useful representations.

  4. Flattening converts the final feature maps into a vector.

  5. One or more fully connected layers use that vector to make a classification.

  6. Softmax can convert the final scores into class probabilities.

Convolution and pooling may alternate—for example, several convolutional layers followed by one pooling operation. Pooling is optional: a model can instead remain convolutional through most or all of its structure.


CNNs for Go

Representing the board and move prediction

Go can be treated as a classification problem. The input describes the board position, and the output selects the location for the next move. On a 19×1919 \times 1919×19 board, there are 19×1919 \times 1919×19 possible board positions that can serve as candidate moves.

One simple board representation is a vector with one value for each location:

  • A black stone can be represented by 111.

  • A white stone can be represented by −1-1−1.

  • An empty position can be represented by 000.

This is only one possible representation. The essential requirement is that the board position be encoded in a form the network can process.

The board can also be viewed as a small image of spatial size 19×1919 \times 1919×19. In this view, each board location is analogous to a pixel. The input can have multiple channels that describe information about each position. One described Go representation uses 484848 channels per location, encoding features such as stone configuration and whether a position is under threat.

Why image-style assumptions can apply

Go and image recognition share useful structural properties:

  • Some important patterns can be identified from a local region rather than the entire board.

  • The same pattern can occur at different board locations.

For example, a local capture pattern can occur in multiple parts of the board. This makes local receptive fields and shared filters potentially useful for Go as well as for images.

An example AlphaGo architecture

A described AlphaGo network treats the board as a 19×19×4819 \times 19 \times 4819×19×48 input and uses zero padding. Its first convolutional layer uses 192192192 filters with a 5×55 \times 55×5 kernel, stride 111, and a ReLU activation. Subsequent layers, from the second through the twelfth, use 3×33 \times 33×3 kernels, each with 192192192 filters and stride 111. A Softmax output supports move selection.

Notably, this architecture does not use pooling. Removing rows or columns from a Go board can change the game position, so spatial downsampling that may be acceptable for ordinary image classification is not necessarily appropriate for Go. The example shows why architecture choices need to reflect the task rather than being copied mechanically from a different application.


CNNs beyond images

CNNs have also been applied to speech and text processing, but their design should be adapted to the structure of those data. The receptive fields and parameter-sharing schemes suitable for images are not automatically suitable for speech or language. A model designer must consider what counts as a meaningful local pattern in the relevant domain and choose connections accordingly.

The broader principle is that a CNN is not guaranteed to work well simply because a task involves data arranged in a grid or sequence. Its assumptions should match the task’s structure.


Scale, rotation, and data augmentation

Ordinary CNNs do not automatically recognize a pattern at every scale or orientation. If a model is trained on dogs that appear at one typical size, it may not reliably recognize the same dog after the image is enlarged. Rotation can create a similar problem.

To a person, two images may show the same shape. To a CNN, however, scaling or rotating an object changes the arrangement and values of the pixels. The network may therefore treat the transformed image as substantially different unless it has learned from examples of those transformations.

Data augmentation helps expose a model to such variation during training. It can include:

  • Cropping a smaller region from an image and enlarging it, so the model sees objects at different apparent sizes.

  • Rotating images, so the model encounters objects in different orientations.

These transformations can improve recognition of patterns at sizes and orientations not present in the original training examples. A specialized architecture such as a Spatial Transformer Layer can also be used to address geometric variation, though its design is distinct from the basic CNN operations covered here.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent