DEEP LEARNING LESSON 10 CONVOLUTIONAL NEURAL NETWORKS

CNN Architecture

CNN architecture describes how different layers are arranged to process an image. A typical CNN gradually transforms raw pixels into useful features and finally uses those features to make a prediction.

What Is CNN Architecture?

A CNN is made up of several layers. Each layer has a specific job.

Image
  ↓
Convolution
  ↓
Activation
  ↓
Pooling
  ↓
Convolution
  ↓
Activation
  ↓
Pooling
  ↓
Flatten
  ↓
Dense Layer
  ↓
Output
  ↓
Prediction

The complete arrangement of these layers is called the CNN architecture.

Why Does a CNN Need Multiple Layers?

A CNN does not usually recognize a complex object from raw pixels in one step.

Instead, it learns increasingly useful features as the data moves through the network.

Raw Pixels
    ↓
Edges
    ↓
Textures and Shapes
    ↓
Object Parts
    ↓
Complete Object
    ↓
Prediction

For example, when recognizing a cat, early layers might detect edges, while deeper layers can combine those patterns into shapes such as ears, eyes, and eventually a representation useful for identifying a cat.

Basic CNN Architecture

A simple CNN can look like this:

Input Image
     ↓
Convolution Layer
     ↓
ReLU Activation
     ↓
Max Pooling
     ↓
Convolution Layer
     ↓
ReLU Activation
     ↓
Max Pooling
     ↓
Flatten
     ↓
Dense Layer
     ↓
Output Layer
     ↓
Prediction

Each stage performs a different job.

1. Input Image

The CNN starts with an image represented as numbers.

For a color image, the input normally has three channels: red, green, and blue.

Example:

Image size = 128 × 128
Channels   = 3

Input shape:

128 × 128 × 3

The CNN does not see the image like a human. It receives numerical pixel values.

Image
  ↓
Pixels
  ↓
Numbers
  ↓
CNN

2. Convolution Layer

The convolution layer applies filters to the image to detect useful patterns.

Input Image
     ↓
Filters
     ↓
Convolution
     ↓
Feature Maps

Early convolution layers commonly learn simple patterns such as edges and textures.

Image
  ↓
Convolution
  ↓
Feature Maps

Feature 1 → Edge
Feature 2 → Texture
Feature 3 → Shape

3. Activation Function

After convolution, an activation function such as ReLU is commonly applied.

ReLU(x) = max(0, x)

This means negative values become zero while positive values remain.

Input:

-3   -1   2   5

ReLU:

 0    0   2   5

The activation function gives the network the ability to learn non-linear patterns.

4. Pooling Layer

Pooling reduces the spatial size of feature maps.

Feature Map
     ↓
Max Pooling
     ↓
Smaller Feature Map

For example, a 2 × 2 max-pooling operation keeps the largest value from each 2 × 2 region.

2 × 2 region:

1   4
2   3

Maximum = 4

Pooling reduces computation and can help preserve strong feature responses while reducing spatial detail.

5. More Convolution Layers

CNNs usually contain more than one convolution layer.

First Convolution
        ↓
Simple Features
        ↓
Second Convolution
        ↓
More Complex Features
        ↓
Third Convolution
        ↓
Higher-Level Features

This is important. Deeper layers receive representations created by earlier layers and can combine simpler patterns into more complex ones.

6. Flatten Layer

After the convolution and pooling stages, we may have a three-dimensional feature representation.

Feature Maps

2 × 2 × 3

        ↓

Flatten

        ↓

12 values

Flatten converts the multi-dimensional feature maps into one long vector so it can be passed to a dense layer.

Before:

[
  [
    [1, 2],
    [3, 4]
  ]
]

After Flatten:

[1, 2, 3, 4]

7. Dense Layer

The dense layer uses the extracted features to help make the final decision.

Flattened Features
        ↓
Dense Layer
        ↓
Learned Combination
        ↓
Classification

For example, if a CNN is classifying animals, the dense layers can combine the features detected earlier to determine which class is most likely.

8. Output Layer

The output layer produces the final prediction.

For a three-class classification problem, the output could look like:

Cat       0.10
Dog       0.85
Horse     0.05

The largest probability is for Dog, so the prediction is:

Prediction = Dog

Complete Example

Suppose we want to build a CNN that identifies whether an image contains a cat or a dog.

Input
128 × 128 × 3
        ↓
Conv2D
        ↓
ReLU
        ↓
MaxPooling
        ↓
Conv2D
        ↓
ReLU
        ↓
MaxPooling
        ↓
Flatten
        ↓
Dense
        ↓
Output
        ↓
Cat / Dog

The network gradually changes the image representation.

128 × 128 × 3
      ↓
Detect simple patterns
      ↓
Smaller feature maps
      ↓
Detect more complex patterns
      ↓
Flatten features
      ↓
Classify
      ↓
Cat or Dog

CNN Architecture With Keras

Here is a simple CNN using TensorFlow and Keras:

import tensorflow as tf

model = tf.keras.Sequential([

    tf.keras.layers.Conv2D(
        32,
        (3, 3),
        activation='relu',
        input_shape=(128, 128, 3)
    ),

    tf.keras.layers.MaxPooling2D(
        (2, 2)
    ),

    tf.keras.layers.Conv2D(
        64,
        (3, 3),
        activation='relu'
    ),

    tf.keras.layers.MaxPooling2D(
        (2, 2)
    ),

    tf.keras.layers.Flatten(),

    tf.keras.layers.Dense(
        128,
        activation='relu'
    ),

    tf.keras.layers.Dense(
        1,
        activation='sigmoid'
    )
])

model.summary()

Understand the Python Code

The first convolution layer contains 32 filters.

tf.keras.layers.Conv2D(
    32,
    (3, 3),
    activation='relu',
    input_shape=(128, 128, 3)
)

This means:

32
→ Number of filters

(3, 3)
→ Kernel size

relu
→ Activation function

128 × 128 × 3
→ Input image shape

Next, max pooling reduces the spatial dimensions.

tf.keras.layers.MaxPooling2D(
    (2, 2)
)

Then another convolution layer learns additional features.

tf.keras.layers.Conv2D(
    64,
    (3, 3),
    activation='relu'
)

Notice that the number of filters increased from 32 to 64. This is a common design pattern because deeper layers can learn a richer set of feature representations.

Another pooling layer reduces the spatial dimensions again.

tf.keras.layers.MaxPooling2D(
    (2, 2)
)

Flatten converts the feature maps into one vector.

tf.keras.layers.Flatten()

The dense layer then processes those extracted features.

tf.keras.layers.Dense(
    128,
    activation='relu'
)

Finally, the output layer produces the binary classification result.

tf.keras.layers.Dense(
    1,
    activation='sigmoid'
)

Because this example has two classes, such as Cat and Dog, a single sigmoid output can represent the probability of one class.

How the Data Changes Through the CNN

One of the most important things to understand is that the data changes shape as it moves through the network.

Input

128 × 128 × 3

        ↓

Convolution

126 × 126 × 32

        ↓

Max Pooling

63 × 63 × 32

        ↓

Convolution

61 × 61 × 64

        ↓

Max Pooling

30 × 30 × 64

        ↓

Flatten

57,600 values

        ↓

Dense

128 values

        ↓

Output

1 value

The exact dimensions depend on kernel size, padding, stride, and pooling configuration. The example above uses valid convolution with stride 1 and 2 × 2 pooling.

Feature Extraction vs Classification

A useful way to understand CNN architecture is to divide it into two major parts.

PART 1 — FEATURE EXTRACTION

Convolution
     ↓
Activation
     ↓
Pooling
     ↓
Convolution
     ↓
Activation
     ↓
Pooling

Goal:
Find useful visual features.


PART 2 — CLASSIFICATION

Flatten
     ↓
Dense Layers
     ↓
Output

Goal:
Use those features to make a prediction.

This separation makes the overall CNN much easier to understand.

Real-World Example — Cat vs Dog

Cat Image
    ↓
Convolution
    ↓
Detect edges
    ↓
Pooling
    ↓
Reduce information
    ↓
Convolution
    ↓
Detect shapes / textures
    ↓
Pooling
    ↓
Reduce information
    ↓
Flatten
    ↓
Dense Layer
    ↓
Combine learned features
    ↓
Output
    ↓
Cat = 0.92
Dog = 0.08

Prediction = Cat

The network is not manually programmed with rules such as "cats have pointed ears." During training, the filters learn useful visual patterns from examples.

The Big Picture

                 CNN

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

The CNN starts with raw pixels and gradually transforms them into a representation that is useful for making a prediction.

Easy Way to Remember

CONVOLUTION
→ Find features

ACTIVATION
→ Add non-linearity

POOLING
→ Reduce spatial size

FLATTEN
→ Convert to a vector

DENSE
→ Combine features

OUTPUT
→ Make prediction

Remember the complete flow:

Image
 ↓
Find features
 ↓
Reduce size
 ↓
Find more complex features
 ↓
Convert features to vector
 ↓
Classify
 ↓
Prediction
QUICK CHECK

Check Your Understanding

What is CNN architecture?
It is the arrangement of layers used by a CNN to transform an input image into a prediction.

What does convolution do?
It uses filters to detect useful patterns in the image.

Why is pooling used?
It reduces spatial dimensions while retaining useful information.

Why do CNNs use multiple convolution layers?
Deeper layers can build more complex representations from features detected by earlier layers.

What does Flatten do?
It converts multi-dimensional feature maps into a one-dimensional vector.

What does the Dense layer do?
It combines the extracted features to help produce the final prediction.