Convolution operation and filters
Overview
The convolution operation is the fundamental building block of Convolutional Neural Networks (CNNs). It's a mathematical operation that slides a small matrix (called a filter or kernel) across an input image to detect local patterns like edges, textures, or shapes. Unlike fully connected layers that treat each pixel independently, convolution preserves spatial relationships between neighboring pixels.

The Mathematics of Convolution
What Is the Discrete2D Convolution?
This is technically cross-correlation, but in deep learning we call it "convolution" (true mathematical convolution flips the kernel, but we learn the weights anyway, so the flip is irrelevant).
Derivation from First Principles
WHY this formula? We want to measure "how much does this local patch of the image match the pattern encoded in the filter?"
Step 1: Local receptive field
The filter looks at a window of the input. For a 3×3 filter at position , it sees pixels .
Step 2: Element-wise multiplication
Each filter weight multiplies the corresponding input pixel . This is a weighted sum where the filter weights determine what pattern we're looking for.
Step 3: Accumulation
We sum all products. A large positive sum means "strong match," near-zero means "pattern not present."
Step 4: Sliding the filter
Repeat for every valid position in the input. The stride controls how many pixels we skip: incrementing by instead of 1.
where is padding (zeros added around input borders), is stride.
WHY padding? Without it, the output shrinks by pixels in each dimension. Padding preserves spatial size. WHY stride ? Downsamples the output, reducing computation (like pooling).
What Does a Filter Actually Detect?
Worked Examples
Example 1: 3×3 Filter on 5×5 Input (Stride 1, No Padding)
Input :
Filter (vertical edge):
Output size: , so output.
Compute top-left output element :
Why this step? We overlay the filter on the top-left 3×3 patch, multiply corresponding elements, and sum. The positive result (3) indicates a weak vertical edge (right side slightly brighter than left).
Full output (computing remaining 8 positions similarly):
Example 2: Effect of Stride and Padding
Same input/filter, stride , padding .
Paded input (add one row/column of zeros on all sides) → :
Output size: .
Top-left computation now starts at the paded position , but most elements are 0:
Why stride 2? We skip every other position, so the filter moves from to to , then , etc. Output is smaller but faster to compute.
Example 3: Multi-Channel Input (RGB Image)
Real images have 3 channels (R, G, B). The filter must also have 3 channels.
Input: (height, width, channels)
Filter: (one slice per input channel)
Convolution:
Why this form? We apply the filter separately to each channel, then sum across channels to produce a single output value. The filter learns correlations across color channels (e.g., "red and green high, blue low" = yellow).
Multiple filters: A CNN layer typically has filters (say 64). Each produces its own feature map, so the output is .
Common Mistakes and Fixes
Active Recall Flashcards
#flashcards/ai-ml
What is the discrete2D convolution formula for position (i,j)? ::
What is the output height formula after convolution?
Why do we use convolution instead of fully connected layers for images?
What does a vertical edge detection filter look like (3×3)?
What is the difference between stride and dilation?
For an RGB image (3 channels), what shape must a filter have?
What is translation invariance in CNs?
If input is 32×32×3, filter is 5×5×3, stride=1, padding=2 output shape?
Feynman Explanation
Recall Explain to a 12-Year-Old
Imagine you have a big photo of your dog, and you want to find all the spots where there's a vertical line (like the edge of a door). You can't look at the whole photo at once—that's too much! Instead, you use a magnifying glass (the filter) that only sees a tiny 3×3 square at a time.
Inside that magnifying glass, you have a special pattern drawn: "If the left side is dark and the right side is bright, this is a vertical edge!" You multiply each pixel you see by this pattern and add them up. If the sum is big, you write "Found an edge here!" on a new sheet of paper.
Now you slide the magnifying glass one step to the right, and check again. Slide, check, slide, check—all the way across the photo, row by row. The new sheet of paper (the feature map) shows you everywhere the vertical edge pattern appeared.
The amazing part? You only designed the magnifying glass pattern once, but you used it thousands of times across the photo. That's way easier than making a separate magnifying glass for every single pixel!
In a CNN, the computer learns what patterns to look for (edges, colors, shapes) by trying millions of examples. Early layers find simple stuff like edges. Deeper layers combine those edges into complex things like "dog ears" or "bicycle wheels."
Connections
- 3.4.02-Pooling-layers — After convolution, pooling downsamples feature maps
- 3.4.03-CNN-architectures — Stack convolution + pooling to build networks like LeNet, AlexNet
- 3.3.01-Backpropagation — How filters learn: gradients flow back through convolution
- 2.2.05-Receptive-fields — Each output neuron "sees" a local input region
- 3.4.10-Depthwise-separable-convolutions — Efficient variant splitting spatial and channel-wise convolution
- 4.1.03-Transfer-learning — Pretrained filters (ImageNet) transfer low-level features to new tasks
- 3.4.06-Batch-normalization — Normalizes feature maps to stabilize training
Mnemonic
Channels: "Filter Depth Must Match Input Depth" (FD = ID).
Key Takeaways
- Convolution = local pattern matching: Filters encode templates (edges, textures), output measures match strength at each location.
- Parameter sharing: Same small filter reused across entire image → far fewer weights than fully connected.
- Translation invariance: Learned features work anywhere in the image.
- Dimensions matter: Filter depth = input channels. Output channels = number of filters.
- Stride & padding control: Stride downsamples, padding preserves size. Formula: .
- Learned representations: Backprop tunes filter weights to extract useful features for the task.
Master convolution, and you understand the heart of computer vision models.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Convolution ka matlab hai ek chhoti si template (filter) ko puri image par slide karna aur dekhna ki kahin pattern match ho raha hai ya nahi. Socho tumhare pas ek photo hai aur tumhe sare vertical edges (jaise door ki boundary) dhundhne hain. Agar tum pori image koek sath dekho toh bohot complicated ho jayega. Instead, ek3×3 ka magnifying glass lo jo sirf thoda sa area dekhta hai. Us magnifying glass mein ek pattern likha hua hai: "agar left side dark hai aur right side bright hai, toh yeh edge hai!" Ab tum us glass ko left-se-right slide karte jao, har jagah check karo, aur jahan match mile wahan "edge mila!" likh do. Yeh convolution operation hai.
CNN mein yeh filters khud seekhte hain. Matlab tumhe manually edge detector banana nahi padta—network khud training data se samajh jata hai ki kaunsa pattern useful hai. Pehli layer simple chezein seekhti hai (edges, colors), deeper layers complex cheezein seekhti hain (dog ka face, car ka wheel). Translation invariance ka fayda yeh hai ki agar tumne top-left corner mein cat detect karna seekha, toh bottom-right mein bhi kaam karega—same filter reuse hota hai. Isliye CNN billions of parameters ki jagah thousands mein kaam kar lete hain. Stride bata hai kitne pixels chhod kar filter move karega (faster computation), aur padding image ke size ko preserve karta hai taki output chhota na ho jaye. RGB images ke liye filter ko bhi teen channels chahiye (R, G, B)—yeh teno channels ka weighted sum output deta hai. Yeh technique hi computer vision ka foundation hai!