Convolutional Neural Networks


Convolutional Neural Networks

Anna
Today we’re looking at neural-network architecture through a classic example: the convolutional neural network, or CNN. CNNs are especially associated with image tasks, and we’ll use image classification to understand why architecture matters. The key idea is that a network designed around useful properties of a task can often do better than one that ignores them.

Hope
Let’s define the task first. We give a model an image and ask it to decide what object or objects are in it. For this discussion, assume the input images have a fixed size, such as 100 by 100 pixels. In practice, images may have different dimensions or aspect ratios, so a common approach is to rescale them to a consistent size before feeding them into the system.

Anna
The model’s output is a classification vector. We can represent the correct label as a one-hot vector: if the image is a cat, the cat’s position is 1 and the other positions are 0. The vector’s length determines how many classes the model can represent—2,000 entries means 2,000 possible object categories. The model produces its predicted vector through a Softmax, and training aims to make its prediction agree with the target, for example by minimizing cross-entropy.

Hope
So what does a computer actually receive when we say ‘an image’? A color image can be represented as a three-dimensional tensor: height, width, and channels. Each pixel has red, green, and blue values, so a color image has three channels. A 100-by-100 RGB image therefore contains 100 × 100 × 3 numbers, each describing the intensity of a particular color at a particular pixel.

Anna
A basic fully connected network expects a vector, so the simplest thing is to flatten the image tensor into one long vector. For that 100-by-100 RGB example, the input has 30,000 values. If the first layer has 1,000 neurons, and every neuron connects to every input value, that layer has 1,000 × 30,000 weights—about 30 million, before counting biases.

Hope
That’s a lot of parameters for just the first layer. More parameters can make a model more flexible, but they can also increase the risk of overfitting. Image recognition gives us a reason to ask whether every neuron really needs to consider every pixel in the entire image. The answer is often no.

Anna
Think about recognizing a bird. A system might detect useful patterns such as a beak, an eye, or claws, and combine those cues to identify the animal. People also often recognize objects by noticing important features. A detector for a beak does not necessarily need to inspect the whole image; a small local patch may contain enough information.

Hope
That leads to the receptive field: the part of the input image that a particular neuron is allowed to inspect. Instead of connecting one neuron to all 30,000 image values, we might connect it only to a small patch. For example, a 3-by-3 patch across all three color channels contains 3 × 3 × 3, or 27, values. The neuron uses 27 weights and a bias to turn that local input into an output.

Anna
Different neurons can inspect different patches, and those patches can overlap. We can also assign several neurons to the same patch, because different neurons may detect different patterns in that same region. The patch size is a design choice: some patterns may fit in a small area, while others may need a wider view.

Hope
The receptive field does not have to be square, either. It could be rectangular, or, for a special task, even cover separated parts of an image. But a usual image pattern is local and connected, so common image models use connected regions. In standard image CNNs, filters usually look across all the color channels, which means we describe the region’s height and width rather than choosing just one RGB channel.

Anna
That height and width are called the kernel size. A common choice is 3 by 3; kernels such as 7 by 7 or 9 by 9 are larger. A single receptive field is typically handled by a group of neurons—perhaps 64 or 128—so the system can detect several kinds of pattern in the same location.

Hope
To cover the image, we move the receptive fields across it. The distance moved each time is the stride, a hyperparameter that we choose. Strides of 1 or 2 are common in the explanation here, and overlapping fields are useful: if a pattern lies right at the boundary between two non-overlapping patches, it might otherwise be missed.

Anna
As the filter moves, it can run into the image boundary. We generally still want the edge pixels to be considered, so we can pad the image. Zero padding extends the image with zeros where the filter would otherwise fall outside it. Padding means filling those missing values; zeros are common, though other choices—such as using an average value or repeating edge values—are possible.

Hope
There’s a second important observation: the same pattern can appear in different parts of an image. A beak could be near the top-left or near the center. If we trained a separate beak detector for every possible location, we would repeat essentially the same work and use many more parameters.

Anna
CNNs address that with parameter sharing. Neurons that inspect different image patches can use the same weights. Their outputs can still differ, because each neuron receives different pixel values: the same weights applied to one patch and another patch need not produce the same result. Sharing is useful when the pattern-detection task is the same but the location changes.

Hope
In a typical convolutional layer, each location has a group of neurons, and corresponding neurons at different locations share their weights. One shared set of weights is called a filter. If the layer has 64 filters, it can learn 64 different detectors, and each detector is applied across the image.

Anna
Put those ideas together and you get a convolutional layer: local receptive fields restrict what each neuron sees, and parameter sharing makes detectors reusable at different locations. Compared with a fully connected layer, this gives the model less freedom. In machine-learning terms, that is a stronger bias toward patterns that are local and useful wherever they appear.

Hope
A stronger bias is not automatically a disadvantage. A fully connected layer can represent many different relationships, but that flexibility can make it easier to overfit. A convolutional layer is deliberately designed for image-like structure, and those design assumptions can help on images. They may not be appropriate for a task that lacks those properties, so architecture should reflect the problem.

Anna
There’s another way to describe exactly the same operation, using filters directly. Imagine a grayscale 6-by-6 image and a 3-by-3 filter whose nine values are known. Place the filter over the top-left 3-by-3 patch, multiply corresponding values, and add them—that is an inner product. Then slide the filter across the image and repeat the calculation at each position.

Hope
If a filter’s values are arranged to respond strongly to a particular pattern, its output will be large where that pattern appears. For example, a filter might respond to a particular arrangement of neighboring pixel values. Scanning the filter over the image tells us both whether that pattern appears and where it appears.

Anna
One filter produces a grid of output values called a feature map. Each position in that grid corresponds to a location where the filter was applied. A layer with 64 filters produces 64 feature maps, which we can think of as a new image with 64 channels rather than the original image’s three RGB channels.

Hope
The filter’s values are learned parameters, rather than numbers we ordinarily choose by hand. In a practical convolutional layer, filters also generally have biases, even if a simplified illustration leaves them out. The operation of sliding a shared filter across the image is what connects the filter description to the earlier story about neurons sharing weights.

Anna
What happens when we stack convolutional layers? The next layer takes the previous layer’s feature maps as its input. If the first layer used 64 filters, its output has 64 channels, so a filter in the next layer spans those 64 input channels as well as its spatial kernel—for example, 3 by 3 by 64.

Hope
Stacking small kernels also lets deeper layers consider larger regions of the original image. With two 3-by-3 layers in the simple stride-one example, an output can depend on a 5-by-5 region of the original input. So using 3-by-3 kernels does not mean the whole network can only recognize patterns that fit inside 3 by 3 pixels; the effective view grows as layers are stacked.

Anna
Now let’s add a common operation: pooling. The intuition is that an image can sometimes be reduced in size without changing what object it depicts. Pooling operates on feature-map values and, unlike a learned convolutional filter, it has no weights to learn. It is a fixed operation rather than a parameterized layer.

Hope
In max pooling, we divide each feature map into groups—for example, 2-by-2 groups—and keep the largest value in each group. Other pooling choices are possible, such as taking an average or a minimum, and the group size is a design choice too. With 2-by-2 pooling, a 4-by-4 feature map becomes 2 by 2, while the number of channels stays the same.

Anna
A classic image-recognition pipeline alternates convolution and pooling, perhaps doing two convolutions and then a pooling operation. Pooling reduces spatial size and can reduce later computation, but reducing detail can hurt when the task depends on fine distinctions. That’s one reason pooling is not compulsory: some modern designs use convolution throughout, especially when their computation budget allows it.

Hope
After the convolution and pooling stages, a classic network can flatten the remaining feature maps into a vector. That vector goes into fully connected layers, and a final Softmax can produce class predictions. So a familiar image classifier might combine convolution, optional pooling, flattening, and fully connected classification layers.

Anna
CNNs can also be used for Go, as in AlphaGo. We can frame a move as a classification problem: the input describes the board, and the output assigns scores or probabilities to possible next positions. A simple board encoding is a 19-by-19 array, with 1 for a black stone, minus 1 for a white stone, and 0 for an empty point.

Hope
That is one possible representation, not the only one. In AlphaGo’s original paper, each board position was described using 48 channels, so the board could be treated as a 19-by-19-by-48 image-like tensor. Those channels encode information about what is happening at each point; examples mentioned include whether a stone is threatened and what kinds of neighboring stones are present.

Anna
Why might a CNN work for Go? Local patterns matter: a small patch of the board can show that a stone is surrounded or that a capture is possible, without requiring a detector to inspect every point on the board. And the same local pattern can occur in different places, such as a corner or another region, which makes shared filters useful.

Hope
But pooling is a different matter. In image recognition, reducing spatial detail can leave the object recognizable; on a Go board, removing a row or column would change the position and could change the game. That means we should not assume that a design choice useful for images is automatically useful for Go.

Anna
The lecture’s description of AlphaGo’s architecture makes that concrete. It treats the board as a 19-by-19-by-48 input, uses zero padding, and starts with 192 filters of size 5 by 5 and stride 1. It then uses layers two through twelve with 3-by-3 filters, again with 192 filters per layer and stride 1, along with a rectifier nonlinearity, or ReLU.

Hope
And notably, that architecture does not use pooling. The lesson is not that pooling is bad, but that its assumptions may not fit every task. CNN design is about deciding what the architecture’s choices mean for the data you have and whether those choices match the structure of the problem.

Anna
The same caution applies when moving CNN ideas from images to speech or text. CNNs have been used in those areas, but their receptive fields and parameter-sharing arrangements need to reflect the properties of speech or language. An image-specific design cannot simply be assumed to work well in another domain.

Hope
There are also limits to what an ordinary CNN handles automatically. If it is trained on dogs at one scale, it may not recognize the same dog after it is enlarged. A rotated object can cause a similar problem: even if a person sees the same shape, the pixel values and their positions have changed, and the network may treat the new image as very different.

Anna
That is why image training often uses data augmentation. For example, we can take crops and enlarge them so the model sees patterns at different sizes, or rotate images so it encounters objects in different orientations. The point is to show the model examples of the transformations we want it to handle.

Hope
There are architecture ideas aimed at handling transformations more directly, too. The lecture names the Spatial Transformer Layer as one such approach, while leaving its details for further study. The broad takeaway is that CNNs are powerful partly because they build in assumptions—but recognizing their assumptions helps us choose when they are useful and when additional design choices are needed.

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