Convolution & CNN architecture
This lecture builds convolution from addition and sliding kernels to CNN blocks. It covers size formulas, RGB channels, sparsity and pooling. After this page you can hand-compute a 1D convolution, apply the output-size formula and compare dense and CNN counts.
Addition vs convolutionSearch on YouTube
Addition works element by element. Convolution slides one array over the other and sums products. The two operations give very different outputs from the same inputs.
Take input and kernel . Addition pairs equal positions. Convolution combines neighbourhoods.
is the output at position . is the input value. is the kernel value at the same position.
Worked example. Add and .
| 1 | 5 | 4 |
| 1 | 2 | -2 |
So . Length stays 3.
Mathematical convolutionSearch on YouTube
Mathematical convolution reverses the kernel first. Then it slides the reversed kernel over the input. Partial overlaps at the edges count, so the output is longer than the input.
The kernel here is a small weight array. The term kernel (also called filter) means the same sliding array in this lecture. After this line the page uses only "kernel".
Flip the kernel end for end. Then drag it across the input. Each stop gives one output value.
is the full convolution output. has length . has length . is the output length.
Reversal for the slide values gives . The slide trace below keeps all five overlap positions. Length is .
Worked example. Full trace with and . The slide order gives .
| 1 | 5 | 4 |
Step 1. First overlap.
Step 2. Second overlap.
Step 3. Full overlap.
Step 4. Fourth overlap.
Step 5. Last overlap.
Full output is . Check: and .
Do not mix full mode and valid mode in one answer. Full mode keeps edge overlaps and returns 5 values here. Valid mode keeps only the full overlap and returns one value.
Convolution in neural networksSearch on YouTube
Neural networks skip the reversal. They slide the kernel as stored and keep only full overlaps. This operation is cross-correlation, but the slides call it convolution in NN. The output is shorter than the input.
is the output length. is the input size. is the kernel size. is padding (zeroes added symmetrically at the ends). is stride (step size of the kernel).
With and , the formula gives . For and , the output has one value.
Worked example. Valid-mode output of with .
| 1 | 5 | 4 |
The single valid value is 3. It matches the middle value of the full trace.
In the exam write the valid dot product in one line. Then write the size formula with numbers. Examiners accept .
1D convolutionSearch on YouTube
1D convolution slides a short kernel along a vector. It detects local patterns such as rises, falls or bumps. The output is a feature map. A feature map is the convolution output.
The diagram labels from the slides are kernel, input vector and feature map. Each output position uses one window of the input.
is the output at position . is the input at . is the kernel weight at position . is the kernel length.
is the input length. is the kernel length. is padding. is stride.
Stride in 1DSearch on YouTube
Stride is the step size. Stride 1 moves the kernel by one position. Stride 2 skips one position between stops. Larger stride gives a shorter output.
2D convolutionSearch on YouTube
2D convolution slides a small matrix over an image. It sums element-wise products inside each window. The same cross-correlation rule holds in two axes.
The slides show a target with a kernel. Only full overlaps count in valid mode.
is the output at row and column . is the input. is the kernel with size.
and are input columns and rows. and are kernel columns and rows. is padding. is stride.
Output size, padding and strideSearch on YouTube
Padding adds zeroes around the borders. It lets the kernel see edge pixels. It also controls the output size. Stride skips positions. It reduces compute and downsamples the map.
- Padding: add zeroes symmetrically at the borders. It preserves edge data. It can keep the output size equal to the input size.
- Stride: move the kernel by steps. It controls output size. It cuts compute and builds spatial hierarchy in CNNs.
In 2D, padding adds a zero ring around all borders. Stride can move along rows and columns together. The slides use the same stride on both axes.
Worked example. Input length , kernel .
Padding 1 keeps size 5. Stride 2 cuts the output to 2. For images, input with kernel, and gives .
Do not forget the final in the size formula. Without it a -input with a -kernel gives 2. The right valid count is 3.
Drag the kernel across the input and watch each dot product form. Change size, channels and pooling and check each result against the page numbers.
1. Slide the kernel over [1, 5, 4]
Drag the kernel across the input. The readout shows the live dot product.
| 1 | -8 | 3 | -2 | -8 |
y[2] = 1 x 1 + 5 x 2 + 4 x (-2) = 1 + 10 - 8 = 3
2. Output size O = (N - M + 2P) / S + 1
O = (5 - 3 + 2x0) / 1 + 1 = 3
3. RGB channels sum to one value
| 1 | 0 |
| 2 | 1 |
| 1 | 0 |
| -1 | 1 |
R sum = 1 + 0 - 2 + 1 = 0. O = 0 + 2 + 3 + 0 = 5
4. Pool a 4x4 input with a 2x2 window
| 1 | 3 | 2 | 0 |
| 5 | 6 | 7 | 8 |
| 4 | 2 | 1 | 0 |
| 3 | 9 | 5 | 6 |
top-left: max(1, 3, 5, 6) = 6
Convolution on imagesSearch on YouTube
Each window gives one output value. The kernel values decide the effect. Different kernels extract different structure from the same patch.
- Averaging neighbours smooths the image. It blurs detail.
- Subtracting neighbours responds to change. It marks edges.
So the same slide, convolve and slide operation stays fixed. Only the kernel numbers change the meaning of the feature map.
RGB channels and multiple kernelsSearch on YouTube
An RGB image is a 3D array. It has height, width and depth 3. The kernel depth must match the input depth. Each channel has its own kernel slice.
For each spot, take one patch per channel. Multiply each patch by its kernel slice. Sum all products across all channels. The sum is one output value. The result is a 2D feature map.
is the output value. runs over R, G and B. and run over kernel rows and columns. is the bias for that kernel.
Zero padding can keep the height and width. Each colour channel carries distinct data. Separate kernel slices let the model learn colour-specific edges and textures.
Worked example. One spot across three channels. Compute one output value, no bias.
| 1 | 0 |
| 2 | 1 |
| 0 | 1 |
| 1 | 2 |
| 2 | 2 |
| 0 | 1 |
Kernels: R , G , B .
One spot gives value 5. Check: R gives 0, G gives 2, B gives 3, total 5.
Multiple kernels and depthSearch on YouTube
One 3D kernel gives one 2D feature map. kernels give feature maps. Stack the maps to add depth. Output depth equals kernel count.
The slides show kernel 1, kernel 2 and kernel 3 on one RGB input. A later slide shows to . With 96 kernels the output depth is 96.
Why convolutionSearch on YouTube
Classical vision uses fixed kernels. Sobel and Prewitt mark vertical or horizontal edges. Their numbers stay frozen.
Deep learning learns the kernels. Treat kernel values as learnable parameters. Update them with backpropagation together with classifier weights. The data decides the features.
Fixed kernels are ready-made glasses. Learned kernels grind new glasses for each dataset.
| Fixed kernels | Learned kernels |
|---|---|
| Handcrafted numbers (Sobel, Prewitt) | Numbers learned by backpropagation |
| Detect chosen edges | Detect task-useful patterns |
| Same for all datasets | Adapt to the input dataset |
CNN vs dense networkSearch on YouTube
CNNs copy the visual cortex plan. Cortical cells watch small visual regions. Each cell fires for a distinct edge direction. Some cells like vertical lines. Others like horizontal or diagonal lines.
A dense network flattens the image first. It loses spatial layout. A CNN keeps the image as a grid. It slides kernels over local patches.
The count of units in one conv layer is . and are output width and height. is the kernel count.
Sparse connectivitySearch on YouTube
Each output sees only a small patch. Distant inputs do not meet in one layer. In the slide example, inputs and do not interact in hidden layer 1. They meet later through . So links are sparse but indirect paths remain.
Sparsity cuts learnable parameters. The model stores fewer weights. It also overfits less.
Weight sharingSearch on YouTube
Weight sharing reuses one kernel at all spots. A vertical-edge kernel should fire anywhere in the image. So the same weights scan all patches. Each kernel thus detects one pattern across the whole image. Learning gets easier because the model learns each pattern once.
Dense uses 9 inputs to 4 units plus 4 biases. CNN shares one kernel shape across spots and needs 4 weights. Add one bias per kernel if used, so .
Worked example. 9 inputs to 4 units vs shared kernels.
The CNN uses about one-eighth of the count. Check: , .
CNN building blocksSearch on YouTube
The slides list six parts. Each part has one role. Data flows in this order.
- Input layer: the input image.
- Convolutional layer: kernels extract edges and textures into feature maps.
- Activation function: usually ReLU.
- Pooling layer: downsamples maps with max or average pooling.
- Fully connected layer: flattens features for classification or regression.
- Output layer: final result, often softmax for classification.
Early layers learn small-scale edges, textures and corners. Middle layers learn shapes and object parts. Deep layers learn whole-object patterns. This ladder supports final classification.
C-R-P-F-O keeps Full Valid straight
Conv, ReLU, Pool, FC, Output is the block order. Full adds (). Valid subtracts ().
PoolingSearch on YouTube
Pooling shrinks height and width. It keeps key data. It cuts compute and controls overfitting. The two slide types are max pooling and average pooling.
The max form picks the largest value in a window. The average form takes the mean of the four values. Both use stride between windows.
Stride 2 gives disjoint windows. Stride 1 gives overlapping windows. The slides show both for max pooling. Replace max by mean for average pooling.
Worked example. input, window, stride 2. Outputs are .
| 1 | 3 | 2 | 0 |
| 5 | 6 | 7 | 8 |
| 4 | 2 | 1 | 0 |
| 3 | 9 | 5 | 6 |
Max pooling. Pick the max of each window.
Average pooling. Take the mean of each window.
Max keeps strong responses. Average smooths the window.
Quick self-testSearch on YouTube
1. Add and . What is the result?
. Add equal positions: , , .
2. State the full-mode length for and . List the slide trace values.
. Values are . Middle value .
3. Compute the valid-mode output of with .
. One value only.
4. Input size is 7, kernel is 3, padding is 0, stride is 2. What is the output size?
. The output has 3 positions.
5. Input is , kernel is , padding is 1, stride is 1. What is the output size?
Width . Height is also 6. Output is .
6. Differentiate mathematical convolution and NN convolution.
Math convolution reverses the kernel and keeps edge overlaps. NN convolution skips reversal and keeps only full overlaps. NN form is cross-correlation.
7. R, G and B sums at one spot are 0, 2 and 3. Bias is 1. What is the output?
. Sum across channels first, then add the bias of that kernel.
8. Differentiate learned kernels and Sobel kernels.
Sobel kernels have fixed numbers for chosen edges. Learned kernels start as weights and change by backpropagation. Learned kernels adapt to the dataset.
9. Compare 9 inputs to 4 dense units with a 4-weight shared kernel. Give both counts.
Dense needs . CNN needs , or 5 with one bias. Sharing removes repeat weights.
10. Pool the window by max and by average.
Max is 6. Average is . Max keeps the peak. Average reports the level.
- Addition pairs equal positions: .
- Full convolution length is . Slide trace gives .
- NN mode skips reversal and keeps full overlaps only. Valid value here is 3.
- 1D rule is . 2D rule is .
- Output size is . Width and height use the same form.
- Padding adds zeroes to save edges and set size. Stride sets step size and downsamples.
- RGB output sums all channels: . Output depth equals kernel count.
- Kernels are learned by backpropagation, unlike fixed Sobel and Prewitt kernels.
- CNN uses sparsity and weight sharing. Dense needs 40 here; shared kernel needs 4 (5 with bias).
- Blocks run Conv, ReLU, Pool, FC, Output. Pooling is max or average over a window.
CNN Image Classification
Inside a Neural Network
CNNs Explained