DEEP LEARNING LESSON 10 CONVOLUTIONAL NEURAL NETWORKS

What Is a CNN?

A Convolutional Neural Network, or CNN, is a type of neural network designed especially for working with images and other data that has a grid-like structure.

What Is a CNN?

CNN stands for Convolutional Neural Network.

It is a type of deep neural network that is especially good at finding patterns inside images.

For example, an image contains thousands of pixels. A CNN learns useful patterns from those pixels and uses those patterns to recognize what the image contains.

Image
  ↓
CNN
  ↓
Find Patterns
  ↓
Understand Features
  ↓
Prediction

Instead of manually telling the computer what features to look for, the CNN learns useful features during training.

Think of a CNN Like Human Vision

Imagine looking at a picture of a cat.

You don't normally examine every pixel individually and calculate whether the image contains a cat.

Your brain recognizes smaller visual patterns and combines them.

Small edges
    ↓
Curves and shapes
    ↓
Eyes / ears / nose
    ↓
Face
    ↓
Cat

A CNN works in a somewhat similar hierarchical way.

Pixels
  ↓
Edges
  ↓
Simple shapes
  ↓
Textures / patterns
  ↓
Object parts
  ↓
Complete object

This is the main idea behind CNNs.

Why Do We Need CNNs?

A normal fully connected neural network can process image data, but it becomes inefficient when the image is large.

Consider an image with:

224 × 224 × 3

The total number of pixel values is:

224 × 224 × 3
= 150,528 values

Connecting all of those values directly to a large dense layer can create a huge number of parameters.

CNNs solve this problem by looking at smaller local regions of the image and reusing the same filters across the image.

An Image Is a Grid of Numbers

A computer does not see an image the same way a human does. It represents an image using numbers.

A simple grayscale image might look conceptually like this:

[
    [  0,  20,  50,  80 ],
    [ 10,  40,  70, 100 ],
    [ 20,  60,  90, 120 ],
    [ 30,  70, 110, 150 ]
]

Each number represents the intensity of a pixel.

A color image usually has three channels:

Red
Green
Blue

These are commonly called RGB channels.

How Does a CNN Process an Image?

At a high level, a CNN takes an image and gradually extracts useful information from it.

Input Image
     ↓
Convolution
     ↓
Activation
     ↓
Pooling
     ↓
More Convolution
     ↓
More Pooling
     ↓
Flatten
     ↓
Dense Layer
     ↓
Output

Each stage has a different job.

What Does Convolution Do?

Convolution is one of the most important operations in a CNN.

A small matrix called a filter or kernel moves across the image.

Image
+-------------------+
|                   |
|     [ 3 × 3 ]     |
|      Filter       |
|                   |
+-------------------+
          ↓
    Pattern Detection

The filter looks at small areas of the image and detects specific patterns.

For example, a filter might learn to respond strongly to an edge.

We will study the actual convolution calculation in the next topic.

What Patterns Can a CNN Learn?

Early CNN layers generally learn simple visual patterns.

First layers
    ↓
Edges
Lines
Corners

Middle layers
    ↓
Curves
Textures
Simple shapes

Deeper layers
    ↓
Object parts
Faces
Complex patterns

The network learns these patterns automatically from the training data.

Example 1 — Cat Image

Suppose we want a CNN to determine whether an image contains a cat.

Cat Image
    ↓
Convolution
    ↓
Detect edges
    ↓
Detect curves and textures
    ↓
Detect eyes, ears and nose
    ↓
Combine features
    ↓
Cat probability = 0.96

The CNN does not need us to manually program rules such as "look for two ears and two eyes."

During training, it learns useful visual features from many examples.

Example 2 — Handwritten Digit

CNNs are also useful for recognizing handwritten digits.

Image of "7"
     ↓
Detect lines
     ↓
Detect diagonal shape
     ↓
Detect horizontal shape
     ↓
Combine features
     ↓
Prediction
     ↓
7

For example, the output layer might produce probabilities for the ten digits:

0 → 0.01
1 → 0.02
2 → 0.01
3 → 0.01
4 → 0.02
5 → 0.01
6 → 0.01
7 → 0.88
8 → 0.01
9 → 0.02

The largest probability is for digit 7, so the model predicts:

Prediction = 7

CNN vs Regular Neural Network

A regular dense neural network treats the input features more independently. A CNN takes advantage of the fact that nearby pixels are related.

Regular Neural Network

Pixels
  ↓
Flatten
  ↓
Dense Layers
  ↓
Prediction
CNN

Image
  ↓
Convolution
  ↓
Feature Detection
  ↓
Pooling
  ↓
More Feature Detection
  ↓
Dense Layers
  ↓
Prediction

For image tasks, the second approach is usually much more appropriate because it preserves and exploits spatial relationships.

Main Parts of a CNN

You will see these components repeatedly when working with CNNs.

1. Convolution
   → Detect features

2. Activation
   → Add non-linearity

3. Pooling
   → Reduce spatial size

4. Flatten
   → Convert feature maps into a vector

5. Dense Layer
   → Make the final decision

6. Output Layer
   → Produce prediction

We will study each of these components separately in the following topics.

A Simple CNN Architecture

Input Image
    ↓
Conv2D
    ↓
ReLU
    ↓
MaxPooling
    ↓
Conv2D
    ↓
ReLU
    ↓
MaxPooling
    ↓
Flatten
    ↓
Dense
    ↓
Output

Don't worry about memorizing this architecture yet.

The important thing is to understand the direction:

Raw Pixels
    ↓
Useful Features
    ↓
Smaller Feature Representation
    ↓
Classification
    ↓
Prediction

Real-World Example

Imagine building an application that detects whether a photo contains a dog or a cat.

Photo
  ↓
CNN
  ↓
Learn edges
  ↓
Learn shapes
  ↓
Learn ears / eyes / fur patterns
  ↓
Combine visual information
  ↓
Dog: 0.08
Cat: 0.92
  ↓
Prediction = Cat

This is why CNNs are widely used for image classification and other computer-vision tasks.

The Most Important Idea

The biggest idea to remember is this:

CNN = Neural Network
      +
Automatic Feature Detection
      +
Image Understanding

You provide images and their correct labels during training. The CNN learns which visual patterns are useful for making those predictions.

You don't manually create every edge detector, shape detector, or object detector. The network learns the filters through training.

Remember the CNN Flow

Image
  ↓
Pixels
  ↓
Convolution
  ↓
Features
  ↓
Pooling
  ↓
Smaller Representation
  ↓
More Features
  ↓
Dense Layer
  ↓
Prediction

If you understand this flow, you have the foundation needed for the rest of CNNs.

QUICK CHECK

Check Your Understanding

What does CNN stand for?
Convolutional Neural Network.

What is a CNN mainly used for?
It is especially useful for image and computer-vision tasks.

What does convolution help with?
It allows the network to detect useful local patterns and features in an image.

Does a CNN manually receive rules for detecting edges and shapes?
No. During training, the network learns useful filters and features from the data.

Why is CNN better suited to images than simply using dense layers?
CNNs exploit local spatial relationships and reuse learned filters across the image, making them much more suitable for visual data.