How CNNs Work
Convolutional Neural Networks (CNNs) are the architecture behind most image AI systems. Understanding how they work helps you understand why deep learning is so good at images.
The Core Idea: Convolution
Instead of connecting every pixel to every neuron (which would be billions of connections for a large image), CNNs use a smarter approach: sliding filters.
A filter is a small grid of weights (e.g., 3x3). It slides across the entire image, one position at a time. At each position, it multiplies the filter values with the pixel values underneath and sums the result.
Different filters detect different things:
- An edge-detecting filter outputs high values where there is a strong edge
- A blur filter outputs smoothed values
- A sharpening filter highlights fine details
CNN training automatically learns which filters are most useful for the task.
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
CNN Structure
- Convolutional layer: applies multiple learned filters to detect patterns
- Activation (ReLU): adds non-linearity, outputs only positive values
- Pooling layer: reduces the spatial size by taking the maximum or average in each region
- More convolutional layers: detect more complex patterns from the previous layer's output
- Flatten: convert the 2D feature map into a 1D vector
- Dense layers: combine features and make final prediction
- Output layer (Softmax): probability for each class
Convolution in Code
# Demonstrating what a convolution filter does
def apply_filter(image, filter_kernel):
"""Slide a filter across an image and compute outputs."""
img_h = len(image)
img_w = len(image[0])
flt_h = len(filter_kernel)
flt_w = len(filter_kernel[0])
output_h = img_h - flt_h + 1
output_w = img_w - flt_w + 1
output = []
for i in range(output_h):
row = []
for j in range(output_w):
# Element-wise multiply and sum
value = sum(
image[i + fi][j + fj] * filter_kernel[fi][fj]
for fi in range(flt_h)
for fj in range(flt_w)
)
row.append(round(value, 2))
output.append(row)
return output
# 6x6 grayscale image (numbers represent brightness)
image = [
[0, 0, 0, 255, 255, 255],
[0, 0, 0, 255, 255, 255],
[0, 0, 0, 255, 255, 255],
[0, 0, 0, 255, 255, 255],
[0, 0, 0, 255, 255, 255],
[0, 0, 0, 255, 255, 255],
]
# Normalize image to 0-1
img_norm = [[px/255 for px in row] for row in image]
# Edge detection filter (detects vertical edges)
vertical_edge_filter = [
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1],
]
output = apply_filter(img_norm, vertical_edge_filter)
print("Input image (6x6, 0=black, 1=white):")
for row in img_norm:
print(" " + " ".join(f"{v:.0f}" for v in row))
print()
print("After vertical edge detection filter (4x4 output):")
for row in output:
print(" " + " ".join(f"{v:+.1f}" for v in row))
print()
print("High positive values indicate a strong edge at that position.")Tip
Tip
You do not need to design CNN architectures from scratch. Popular architectures like ResNet, EfficientNet, and MobileNet are pre-trained on millions of images. You can use them directly and fine-tune the last few layers on your own dataset.
Key Takeaways
- Convolutional Neural Networks (CNNs) are the architecture behind most image AI systems.
- Convolutional layer: applies multiple learned filters to detect patterns
- Activation (ReLU): adds non-linearity, outputs only positive values
- Pooling layer: reduces the spatial size by taking the maximum or average in each region