Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Convolutional vs. Fully Connected Layers for Image Processing

Recommended resources

Hello! Welcome to the first lesson in our course on Segmentation-Guided Latent Diffusion. Your goal is to build a sophisticated model for smile enhancement, and to do that effectively, we'll start by solidifying the foundational concepts. You've expressed a desire to revisit the basics from a "first principles" perspective, so our journey begins with the most fundamental building block of modern computer vision: the convolutional layer.

In this lesson, we will explore why convolutional layers are fundamentally more effective than the fully-connected layers of a standard multilayer perceptron (MLP) for image processing tasks. Understanding this is not just an academic exercise; it's the key to grasping why architectures like DeepLabV3, U-Nets, and Diffusion Models are designed the way they are. We'll deconstruct the problem of applying traditional neural networks to images and see how the convolutional layer emerges as an elegant and efficient solution.

The Problem with a "Brute-Force" Approach

Let's start with a basic question. Given your background, you know that a multilayer perceptron (MLP) is a powerful universal function approximator. An image, at its core, is just a large grid of numerical pixel values. So, why can't we just flatten this grid into a single long vector and feed it into a standard MLP?

This "brute-force" approach seems plausible, but it fails for two critical reasons: one structural and one practical. A short video from the channel DataMListic provides an excellent visualization of the structural problem.

Convolutional Neural Networks (CNNs) - Explained

Please watch the beginning of the video "Convolutional Neural Networks (CNNs) - Explained".

Watch the segment from the start until the explanation of the problem with flattening an image. Pay close attention to how flattening discards the spatial relationships between neighboring pixels.

As the video illustrates, flattening an image treats it as an unstructured vector, destroying the crucial concept of spatial locality. Pixels that were immediate neighbors in the 2D grid can end up far apart in the 1D vector. The network loses the inherent information that nearby pixels are highly correlated and together form meaningful local patterns like edges, corners, and textures.

The second failure is a practical one: an explosion in the number of parameters. An MLP's first layer must connect every input neuron (pixel) to every neuron in the first hidden layer. This is called a fully-connected or dense layer. For any reasonably sized image, this leads to a computationally infeasible number of weights.

The article "Why CNNs Needed to Exist" provides a crisp explanation of this parameter explosion.

Why CNNs Work for Images: The Real Design Logic Behind ...

This article clearly lays out the motivation for moving beyond MLPs for image data.

Please read the first two sections, "Why CNNs Needed to Exist" and "The MLP Problem in One Example". The example with the 200x200 image makes the scale of the problem very clear.

To put it into perspective, a single neuron in the first hidden layer of an MLP processing a modest 200x200 RGB image would require 200 * 200 * 3 = 120,000 weights. A hidden layer with 1,000 neurons would therefore have 120 million weights. This not only demands immense computational resources and memory but also makes the model incredibly difficult to train without overfitting, as it has to learn to recognize the same pattern (e.g., a vertical edge) independently at every single location in the image.


Building a Better Model: Exploiting the Structure of Images

The failure of the MLP approach tells us something important: a good architecture should have its assumptions, or inductive biases, aligned with the structure of the data it processes. Natural images have strong statistical properties that MLPs ignore. A more effective architecture should exploit them.

Two properties are paramount:

  1. Locality of Pixel Correlations: As we've noted, pixels are not independent. The information required to detect a simple, low-level feature like an edge or a corner is contained within a small, local region of the image. A neuron trying to detect a whisker on a cat doesn't need to see a pixel from the background in the opposite corner of the image.

  2. Translation Invariance: The identity of an object does not depend on its location. A cat is still a cat whether it appears in the top-left or the bottom-right of an image. Therefore, a feature detector that is useful in one part of the image (e.g., a filter that detects the curve of an eye) should be equally useful in other parts of the image.

The textbook Dive into Deep Learning formalizes these ideas and uses them to motivate the transition from dense layers to convolutions.

6.1. From Dense Layers to Convolutions — Dive into Deep Learning 0.1.0 documentation

This excerpt provides a more formal discussion of the principles that should guide the design of a neural network for vision.

Please read the section "Invariances". Focus on the two desiderata it enumerates: translation invariance and locality.


The Convolutional Layer: A Principled Solution

Convolutional Neural Networks (CNNs) succeed precisely because their architecture is built upon these principles of locality and translation invariance. This is achieved through two key mechanisms: local connectivity and parameter sharing.

The image below provides a perfect visual summary of how these concepts constrain a fully connected layer to produce a convolutional layer.

This diagram illustrates the progression from a fully connected layer (left), where every output is connected to every input, to a locally connected layer (center), which respects locality, and finally to a convolutional layer (right), which adds weight sharing.

This diagram illustrates the progression from a fully connected layer (left), where every output is connected to every input, to a locally connected layer (center), which respects locality, and finally to a convolutional layer (right), which adds weight sharing.

Let's break down this transition:

  1. Fully Connected (MLP): On the left, we see the standard dense layer. Every output activation (top) is connected to every input signal (bottom). This is the inefficient model we started with.

  2. Locally Connected: In the center, we apply the principle of locality. Each output activation is now connected only to a small, local patch of the input. This patch is known as the neuron's receptive field. This dramatically reduces the number of connections. However, notice that the connections for each patch (purple vs. green) are distinct; each local region would be processed by a different set of weights.

  3. Convolutional (CNN): On the right, we introduce parameter sharing (or weight sharing). We now enforce that the weights used to process each local patch are the same. This single, shared set of weights is called a convolutional kernel or filter. The network learns this filter once and then slides it across the entire input to produce the output activation map. This not only reduces the number of parameters to a tiny fraction of the original but also naturally builds in the desired translational property. If the input feature shifts, the activation in the output map also shifts. This property is more formally called translation equivariance.

The mathematical derivation from a fully connected layer to a convolutional one is quite elegant. By imposing the constraints of locality and translation invariance on the general equation for a dense layer, you arrive directly at the convolution operation.

6.1. From Dense Layers to Convolutions — Dive into Deep Learning 0.1.0 documentation

This section provides the mathematical justification for the convolutional layer. Given your background, you may find this "from first principles" derivation particularly insightful.

Please read the section "Constraining the MLP". Follow the logic of how imposing the two principles (translation invariance and locality) on the weight tensor V transforms the equation for a dense layer into the equation for a convolutional layer.


Summary: The Efficiency of Convolutional Layers

To recap, convolutional layers are more effective than fully-connected layers for images because their core design principles—local connectivity and parameter sharing—introduce an inductive bias that is perfectly suited to the spatial structure of visual data.

The benefits are twofold:

  • Parameter Efficiency: Instead of millions or billions of parameters, a convolutional layer requires only a few hundred or thousand (the size of the kernel multiplied by the number of input/output channels). This makes the models smaller, faster to train, and less prone to overfitting.
  • Translation Equivariance: By reusing the same filter across the image, the model learns to detect features regardless of their position. This is a far more efficient way to learn than forcing an MLP to re-learn the same pattern at every possible location.

The following video offers a final, clear summary of these core ideas.

Convolutional Neural Networks (CNNs) - Explained

Let's return to the "Convolutional Neural Networks (CNNs) - Explained" video for a concise recap.

Please watch the segment from this timestamp. It effectively summarizes the inductive biases we've discussed: local connectivity, translation equivariance, and parameter sharing.


Key Takeaways

The fundamental difference between an MLP and a CNN lies in their underlying assumptions about the data.

  • MLPs treat inputs as unstructured vectors, assuming every input feature is potentially related to every other in a complex, non-local way. This is a poor fit for images.
  • CNNs treat inputs as spatially structured grids, assuming that patterns are local and can appear anywhere in the grid. This architectural assumption aligns with the reality of images.

This alignment is why CNNs are not just "more efficient" but are a fundamentally better model for computer vision. They waste less of their capacity learning redundant information and can generalize more effectively from less data.

In our next lesson, we'll build on this foundation. The convolution operation itself is a linear operation (a weighted sum). To enable our network to learn the kind of complex, non-linear patterns found in the real world, we must introduce a non-linear component. We will explore the critical role of activation functions, like the Rectified Linear Unit (ReLU), in making deep learning possible.