DEEP LEARNING LESSON 10 CONVOLUTIONAL NEURAL NETWORKS

Filters and Kernels

Filters and kernels are small matrices used by a CNN to detect useful patterns in an image. They slide across the image and perform calculations to create feature maps.

What Is a Kernel?

A kernel is a small matrix containing numbers called weights.

For example, a kernel can be:

Kernel

1   0
0   1

This kernel is much smaller than a typical image. During convolution, it moves across different parts of the image.

Image
   ↓
Small Kernel
   ↓
Move across image
   ↓
Calculate values
   ↓
Feature Map

So, the simple definition is:

Kernel = Small matrix of weights
          used to detect patterns

What Is a Filter?

A filter is used to detect a particular type of visual pattern in an image.

For example, one filter might learn to respond strongly to vertical edges.

Vertical Edge Filter

-1   0   1
-1   0   1
-1   0   1

Another filter might respond to horizontal edges.

Horizontal Edge Filter

-1  -1  -1
 0   0   0
 1   1   1

The important idea is that different filters can detect different patterns.

Filter vs Kernel

Beginners often get confused by these two words because they are frequently used as if they mean the same thing.

Kernel
   ↓
Small matrix of weights
   ↓
Slides over the image


Filter
   ↓
Pattern detector
   ↓
Uses kernel weights
   ↓
Produces a feature map

In many beginner tutorials, you will see the terms filter and kernel used interchangeably.

For learning CNN fundamentals, you can think of them as the small pattern-detecting matrix used during convolution.

Simple Example

Suppose we have this image:

Image

1   2   3
4   5   6
7   8   9

And this kernel:

Kernel

1   0
0   1

The kernel looks at the first 2 × 2 region:

Image region

1   2
4   5

Multiply corresponding values:

1 × 1 = 1
2 × 0 = 0
4 × 0 = 0
5 × 1 = 5

Add them:

1 + 0 + 0 + 5 = 6

The kernel then moves to another position and repeats the same calculation.

Kernel
   ↓
6
   ↓
Move
   ↓
8
   ↓
Move
   ↓
10
   ↓
Move
   ↓
12

The resulting feature map is:

6   8
10  12

Why Do We Need Different Kernels?

A single kernel cannot detect every useful pattern in an image.

An image can contain many different visual features.

Image
  │
  ├── Edges
  │
  ├── Lines
  │
  ├── Corners
  │
  ├── Curves
  │
  ├── Textures
  │
  └── Shapes

Different kernels can learn to respond to different features.

Kernel 1 → Edge

Kernel 2 → Curve

Kernel 3 → Corner

Kernel 4 → Texture

Kernel 5 → Another pattern

Multiple Filters in a CNN

A convolutional layer normally contains multiple filters.

Input Image
      │
      ├── Filter 1
      │       ↓
      │   Feature Map 1
      │
      ├── Filter 2
      │       ↓
      │   Feature Map 2
      │
      ├── Filter 3
      │       ↓
      │   Feature Map 3
      │
      └── Filter 4
              ↓
          Feature Map 4

Each filter can learn a different pattern.

Therefore, one convolutional layer can extract many different types of information from the same image.

Example 1 — Edge Detection

Imagine an image containing a vertical line.

Image

0  0  1  0
0  0  1  0
0  0  1  0
0  0  1  0

A filter designed to detect vertical changes can produce a strong response when it encounters that line.

Image
   ↓
Vertical-edge filter
   ↓
Strong response
   ↓
Vertical edge detected

This is the basic idea behind feature detection using convolution.

Example 2 — Learning Features of a Face

Consider a CNN that is trained to recognize faces.

Image
   ↓
Early filters
   ↓
Edges and lines
   ↓
Middle filters
   ↓
Curves and shapes
   ↓
Deeper filters
   ↓
Eyes, nose, mouth and other structures
   ↓
Face prediction

The filters in the network are learned from training data. We do not normally tell the CNN exactly which filter should detect an eye or a nose.

Are Filter Values Fixed?

This is an important point.

In a real CNN, the filter values are usually learned during training.

We might start with random values:

Initial kernel

0.12   -0.03
0.07    0.21

During training, backpropagation and the optimizer adjust these values.

Random weights
      ↓
Forward propagation
      ↓
Calculate loss
      ↓
Backpropagation
      ↓
Update kernel weights
      ↓
Better feature detector

After many training steps, the CNN learns filters that are useful for the specific task.

Kernel Size

Kernel size tells us the height and width of the kernel.

Common kernel sizes include:

3 × 3

5 × 5

7 × 7

For example, a 3 × 3 kernel looks at a small 3 × 3 region of the image at a time.

Image

[■ ■ ■] ■ ■
[■ ■ ■] ■ ■
[■ ■ ■] ■ ■
 ■ ■ ■  ■ ■

        ↑
     3 × 3
     kernel

Smaller kernels focus on local details, while larger kernels can look at a larger area at once.

How Does a Kernel Move?

The kernel slides across the image from one position to another.

Step 1

[■ ■] ■
[■ ■] ■
 ■  ■  ■


Step 2

■ [■ ■]
■ [■ ■]
 ■  ■  ■


Step 3

■  ■  ■
[■ ■] ■
[■ ■] ■

At each position, the kernel performs a calculation and stores the result in the feature map.

Kernel → Feature Map

A useful way to understand the relationship is:

Input Image
      +
    Kernel
      ↓
  Convolution
      ↓
 Feature Map

The kernel is the detector, while the feature map shows where that detector found a strong or weak response.

Kernel:
"Look for this pattern"

        ↓

Feature Map:
"Here is where I found that pattern"

Filters and Kernels With Python

We can create a simple kernel using NumPy.

import numpy as np

kernel = np.array([
    [1, 0, -1],
    [1, 0, -1],
    [1, 0, -1]
])

print(kernel)

Output:

[[ 1  0 -1]
 [ 1  0 -1]
 [ 1  0 -1]]

This is a 3 × 3 kernel.

Height = 3
Width  = 3

Kernel size = 3 × 3

Creating Filters With Keras

In a real neural network, we usually do not manually create the filter values. Keras creates the trainable weights for the convolution layer.

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(
        filters=32,
        kernel_size=(3, 3),
        activation='relu',
        input_shape=(128, 128, 3)
    )
])

model.summary()

Here:

filters=32
→ Create 32 different filters

kernel_size=(3, 3)
→ Each filter uses a 3 × 3 kernel

activation='relu'
→ Apply ReLU after convolution

input_shape=(128, 128, 3)
→ Image height = 128
→ Image width = 128
→ RGB channels = 3

What Happens With RGB Images?

A color image normally has three channels:

Red
Green
Blue

So an RGB image has three dimensions for every pixel location.

Height × Width × Channels

128 × 128 × 3

A convolution filter works across the input channels as well.

RGB Image
     ↓
3 × 3 × 3 kernel
     ↓
One feature map

If the convolutional layer has 32 filters, it produces 32 output feature maps.

Input:
128 × 128 × 3

32 filters
     ↓

Output:
Feature maps for 32 learned patterns

The Big Picture

Image
  ↓
Kernel / Filter
  ↓
Slides across image
  ↓
Multiply + Add
  ↓
Feature Map
  ↓
Activation Function
  ↓
Next CNN Layer
  ↓
More complex features
  ↓
Prediction

Early filters often learn simple visual patterns. As the network becomes deeper, later layers can combine those patterns into more complex features.

Easy Way to Remember

Kernel
  ↓
Small matrix of weights

Filter
  ↓
Pattern detector

Convolution
  ↓
Moves the filter across the image

Feature Map
  ↓
Shows the filter's responses

The simplest way to remember the entire concept is:

Filter = "What pattern am I looking for?"

Kernel = "The numbers used to detect that pattern."

Feature Map = "Where did I find that pattern?"
QUICK CHECK

Check Your Understanding

What is a kernel?
A small matrix containing weights that is used during convolution.

What is a filter?
A pattern detector that responds to particular visual features in an image.

Why do we need multiple filters?
Because an image contains many different types of patterns, such as edges, curves, textures, and shapes.

Are CNN filters manually designed?
In a typical CNN, the filter weights are learned automatically during training.

What does a feature map tell us?
It shows the responses produced when a filter scans the image.

What does kernel size mean?
It tells us the height and width of the kernel, such as 3 × 3.