Convolutional Neural Networks

In this section of the course, weโ€™ll learn about some of the most famous neural network architectures. These paved the way to modern-day AI.

This Lesson Will Cover
  • ๐Ÿ›๏ธ The fundamental architecture of Convolutional Neural Networks (CNNs)
  • ๐Ÿ–ผ๏ธ How CNNs process image data through convolution and pooling operations
  • ๐Ÿ“œ Key historical developments in CNN architectures
  • ๐Ÿงฎ Practical examples of CNN computations
  • ๐Ÿš€ Modern applications and relevance of CNNs

Introduction to CNNs

CNNs (Convolutional Neural Networks) are designed for processing structured grid-like data, like images.

  • Introduced in the late 1980s by Yann LeCun with LeNet architecture
    • LeNet-5 was designed for handwritten digit recognition and pioneered the use of convolutional layers
    • It had a simple architecture of two convolutional layers followed by three fully connected layers
  • CNNs had a landmark success in 2012 with AlexNet
    • Created by Alex Krizhevsky, Ilya Sutskever, and Geoffrey Hinton
    • Achieved groundbreaking performance in the ImageNet competition
    • AlexNet was much deeper than LeNet, with five convolutional layers and three fully connected layers
    • It used ReLU activation functions and dropout regularization to prevent overfitting

CNNs are great for computer vision tasks, like identifying objects in an image, e.g. recognizing faces, or detecting medical anomalies in scans.

They are the backbone of modern visual data processing.

Why CNNs Work for Images

Images are fundamentally two-dimensional data structures composed of pixels.

Each pixel has numerical values representing intensity and color channels.

For example, an RGB image with transparency (e.g., PNG) would be composed of four such 2D grids of pixel intensitiesโ€”one for each color channel and one for the opacity channel.

A CNN processes these pixel grids by applying mathematical operations called convolutions.

As we look at how these work, consider their benefits for image recognition tasks:

  • Spatial Hierarchy: Convolutions allow the network to capture patterns like edges, textures, and complex shapes across layers.
  • Parameter Efficiency: Instead of learning a separate weight for every pixel, CNNs learn small filter kernels that are shared across the image.

How CNNs Work

At a high level, a CNN consists of three main building blocks:

  1. Convolutional Layers:

    • These layers slide filters (or kernels) across the image to create feature maps.

      Input Image:
      [1, 2, 0, 1]
      [0, 1, 3, 1]
      [1, 0, 2, 0]
      [2, 1, 0, 3]
      
      Kernel:
      [1, 0, -1]
      [0, 1, -1]
      [1, -1, 0]
      

      The convolution operation slides this kernel across the image, applying element-wise multiplication and summing up the results. With a 4x4 input image and a 3x3 kernel, the output feature map would be 2x2 in size.

  2. Pooling Layers:

    • These layers reduce the spatial size of feature maps to make computations more efficient and to focus on dominant features.
  3. Fully Connected Layers:

    • After extracting features, these layers map them to the final output, such as classification labels.

Simple Example with Data

  1. Convolutional Layer

Imagine we have a grayscale image:

Input (10ร—10):\text{Input } (10 \times 10):
[5213014231132142102321423021143215123421142321023221314321134212124321132431214221412432133213121421]\begin{bmatrix} 5 & 2 & 1 & 3 & 0 & 1 & 4 & 2 & 3 & 1 \\ 1 & 3 & 2 & 1 & 4 & 2 & 1 & 0 & 2 & 3 \\ 2 & 1 & 4 & 2 & 3 & 0 & 2 & 1 & 1 & 4 \\ 3 & 2 & 1 & 5 & 1 & 2 & 3 & 4 & 2 & 1 \\ 1 & 4 & 2 & 3 & 2 & 1 & 0 & 2 & 3 & 2 \\ 2 & 1 & 3 & 1 & 4 & 3 & 2 & 1 & 1 & 3 \\ 4 & 2 & 1 & 2 & 1 & 2 & 4 & 3 & 2 & 1 \\ 1 & 3 & 2 & 4 & 3 & 1 & 2 & 1 & 4 & 2 \\ 2 & 1 & 4 & 1 & 2 & 4 & 3 & 2 & 1 & 3 \\ 3 & 2 & 1 & 3 & 1 & 2 & 1 & 4 & 2 & 1 \end{bmatrix}

We apply a filter:

Filter (3ร—3):\text{Filter } (3 \times 3):
[10โˆ’110โˆ’110โˆ’1]\begin{bmatrix} 1 & 0 & -1 \\ 1 & 0 & -1 \\ 1 & 0 & -1 \end{bmatrix}

After convolution (8x8 output):

Feature Map:\text{Feature Map}:
[1003001โˆ’5โˆ’1โˆ’2โˆ’142โˆ’11โˆ’3โˆ’1โˆ’3171โˆ’4โˆ’100โˆ’2โˆ’132โˆ’1โˆ’1111โˆ’1010001โˆ’1โˆ’21011โˆ’10โˆ’110โˆ’3120โˆ’1โˆ’21100โˆ’11]\begin{bmatrix} 1 & 0 & 0 & 3 & 0 & 0 & 1 & -5 \\ -1 & -2 & -1 & 4 & 2 & -1 & 1 & -3 \\ -1 & -3 & 1 & 7 & 1 & -4 & -1 & 0 \\ 0 & -2 & -1 & 3 & 2 & -1 & -1 & 1 \\ 1 & 1 & -1 & 0 & 1 & 0 & 0 & 0 \\ 1 & -1 & -2 & 1 & 0 & 1 & 1 & -1 \\ 0 & -1 & 1 & 0 & -3 & 1 & 2 & 0 \\ -1 & -2 & 1 & 1 & 0 & 0 & -1 & 1 \end{bmatrix}

We arrived at this result by:

  1. Sliding the Filter: The 3ร—33 \times 3 filter was slid across the input image, computing a dot product at each valid position.
  2. Summation: For each position, the element-wise products of the filter and corresponding image region were summed to get a scalar result.
  3. Reduction in Size: The output size is 8ร—88 \times 8 due to the convolution over the original image without padding.

For example, consider the top-left 3ร—33 \times 3 region of the input:

Region=[521132214] \text{Region} = \begin{bmatrix} 5 & 2 & 1 \\ 1 & 3 & 2 \\ 2 & 1 & 4 \end{bmatrix}

The convolution is computed as:

(5ร—1)+(2ร—0)+(1ร—โˆ’1)+(1ร—1)+(3ร—0)+(2ร—โˆ’1)+(2ร—1)+(1ร—0)+(4ร—โˆ’1) (5 \times 1) + (2 \times 0) + (1 \times -1) + (1 \times 1) + (3 \times 0) + (2 \times -1) + (2 \times 1) + (1 \times 0) + (4 \times -1)
=5+0โˆ’1+1+0โˆ’2+2+0โˆ’4=1 = 5 + 0 - 1 + 1 + 0 - 2 + 2 + 0 - 4 = 1

This value (11) is placed in the top-left corner of the output.

  1. Pooling Layer

Letโ€™s continue working through this example to demonstrate how pooling works.

Taking the feature map above and applying 2x2 max pooling:

Max Pooled Output (4ร—4):\text{Max Pooled Output } (4 \times 4):
[1421072111110112]\begin{bmatrix} 1 & 4 & 2 & 1 \\ 0 & 7 & 2 & 1 \\ 1 & 1 & 1 & 1 \\ 0 & 1 & 1 & 2 \end{bmatrix}

  1. Fully Connected Layer

Finally, letโ€™s consider how we could map this pooling layer into classification probabilities, using a fully-connected layer with the following parameters:

Weights:[0.1โˆ’0.050.020.20.03โˆ’0.010.10.020.010.15โˆ’0.020.030.050.01โˆ’0.020.12โˆ’0.030.020.080.02โˆ’0.010.11โˆ’0.010.020.090.03โˆ’0.020.14โˆ’0.020.010.070.010.020.13โˆ’0.02โˆ’0.010.060.020.010.1โˆ’0.01โˆ’0.020.160.010.020.08โˆ’0.020.01]Biases:[0.10.05โˆ’0.05] \text{Weights:} \begin{bmatrix} 0.1 & -0.05 & 0.02 \\ 0.2 & 0.03 & -0.01 \\ 0.1 & 0.02 & 0.01 \\ 0.15 & -0.02 & 0.03 \\ 0.05 & 0.01 & -0.02 \\ 0.12 & -0.03 & 0.02 \\ 0.08 & 0.02 & -0.01 \\ 0.11 & -0.01 & 0.02 \\ 0.09 & 0.03 & -0.02 \\ 0.14 & -0.02 & 0.01 \\ 0.07 & 0.01 & 0.02 \\ 0.13 & -0.02 & -0.01 \\ 0.06 & 0.02 & 0.01 \\ 0.1 & -0.01 & -0.02 \\ 0.16 & 0.01 & 0.02 \\ 0.08 & -0.02 & 0.01 \end{bmatrix} \quad \text{Biases}: \begin{bmatrix} 0.1 \\ 0.05 \\ -0.05 \end{bmatrix}

We first flatten the pooled output into a 16-element vector:

Flattened Vector:[1421072111110112] \text{Flattened Vector}: \begin{bmatrix} 1 & 4 & 2 & 1 & 0 & 7 & 2 & 1 & 1 & 1 & 1 & 1 & 0 & 1 & 1 & 2 \end{bmatrix}

And then calculate the logits z1z_1, z2z_2, and z3z_3 by performing the dot product of the flattened vector with the weights for each class, then adding the respective biases:

zi=โˆ‘j=116(xjโ‹…wj,i)+bi z_i = \sum_{j=1}^{16} (x_j \cdot w_{j,i}) + b_i

Class 1 (z1z_1):

z1=(1โ‹…0.1)+(4โ‹…0.2)+โ‹ฏ+(2โ‹…0.08)+0.1=3.31 \begin{aligned} z_1 &= (1 \cdot 0.1) + (4 \cdot 0.2) + \dots + (2 \cdot 0.08) + 0.1 \\ &= 3.31 \end{aligned}

Class 2 (z2z_2):

z2=(1โ‹…โˆ’0.05)+(4โ‹…0.03)+โ‹ฏ+(2โ‹…โˆ’0.02)+0.05=โˆ’0.08 \begin{aligned} z_2 &= (1 \cdot -0.05) + (4 \cdot 0.03) + \dots + (2 \cdot -0.02) + 0.05 \\ &= -0.08 \end{aligned}

Class 3 (z3z_3):

z3=(1โ‹…0.02)+(4โ‹…โˆ’0.01)+โ‹ฏ+(2โ‹…0.01)โˆ’0.05=0.14 \begin{aligned} z_3 &= (1 \cdot 0.02) + (4 \cdot -0.01) + \dots + (2 \cdot 0.01) - 0.05 \\ &= 0.14 \end{aligned}

Passing these logits through a softmax activation:

Pi=eziโˆ‘jezj P_i = \frac{e^{z_i}}{\sum_{j} e^{z_j}}
  1. Compute exponentials:
ez1=e3.31=27.39,ez2=eโˆ’0.08=0.923,ez3=e0.14=1.15 e^{z_1} = e^{3.31} = 27.39, \quad e^{z_2} = e^{-0.08} = 0.923, \quad e^{z_3} = e^{0.14} = 1.15
  1. Compute the sum of exponentials:
Sum=27.39+0.923+1.15=29.463 \text{Sum} = 27.39 + 0.923 + 1.15 = 29.463
  1. Compute probabilities:
P1=27.3929.463โ‰ˆ0.9296,P2=0.92329.463โ‰ˆ0.0313,P3=1.1529.463โ‰ˆ0.0390 P_1 = \frac{27.39}{29.463} \approx 0.9296, \quad P_2 = \frac{0.923}{29.463} \approx 0.0313, \quad P_3 = \frac{1.15}{29.463} \approx 0.0390

We arrive at our final output, representing the predicted probabilities of each classification label for our image:

P=[0.9296,0.0313,0.0390] P = [0.9296, 0.0313, 0.0390]

Relevance of CNNs Today

CNNs remain highly relevant, especially for image processing tasks.

Advanced architectures such as ResNet, Inception, and EfficientNet have further refined their accuracy and computational efficiency, making them benchmarks in many applications.

Recently, Transformer models (e.g., Vision Transformers) have demonstrated significant success in computer vision tasks:

  • Transformers excel at capturing global dependencies in data
  • CNNs, on the other hand, with their convolutional operations, are optimized for learning localized patterns

Many state-of-the-art models now combine CNNs and Transformers, leveraging CNNs for efficient local feature extraction and Transformers for capturing global contextual relationships.

Conclusion

If you want to go deeper on CNNs and treat yourself to an enjoyable 10 minutes of learning, I recommend watching the Futurology โ€œCNN Visualizedโ€ YouTube video[1].

Key Takeaways

๐Ÿ–ผ๏ธ CNNs are specialized neural networks designed for processing grid-like data, particularly images.

๐Ÿ”‘ Key components include convolutional layers, pooling layers, and fully connected layers.

๐Ÿž๏ธ Convolutional layers use filters to detect features like edges, textures, and shapes.

๐Ÿ“‰ Pooling layers reduce spatial dimensions and help focus on dominant features.

๐ŸŽ›๏ธ CNNs achieve parameter efficiency through weight sharing across the image.

๐Ÿ›๏ธ Landmark architectures include LeNet (1980s) and AlexNet (2012).

๐Ÿ”„ Modern applications combine CNNs with Transformers for both local and global feature detection.

๐ŸŽฏ CNNs remain fundamental to computer vision tasks like object detection and image classification.

Next: Recurrent Neural Networks (RNNs)

Like CNNs revolutionized image processing, RNNs (Recurrent Neural Networks) revolutionized sequential data processing.

As weโ€™ll see, they can be used to extract semantic meaning from text, where each wordโ€™s meaning depends on the words that came before it.

Footnotes


  1. Futurology - Convolutional Neural Networks Explained (CNN Visualized) YouTube video โ†ฉ๏ธŽ

Comments (0)

No comments yet.