What is a CNN?
A Convolutional Neural Network (CNN) is a deep learning architecture specifically designed for processing visual data like images and videos. CNNs automatically learn spatial hierarchies of features.
Why CNNs for Images?
Regular neural networks don't scale to images. A 200x200 color image = 120,000 inputs! CNNs exploit spatial structure to reduce parameters dramatically.
Image Classification
Object Detection
Face Recognition
Convolution Layer
The core building block of CNNs. A filter (kernel) slides across the image, detecting features like edges, textures, and patterns.
Convolution Operation
Input Image
Filter (3x3)
Feature Map
Pooling Layer
Pooling reduces the spatial dimensions of feature maps, keeping the most important information while reducing computation.
Max Pooling
Takes maximum value from each region. Most common approach.
Average Pooling
Takes average value from each region. Smoother output.
Famous CNN Architectures
LeNet-5 (1998)
The pioneer. Used for digit recognition.
AlexNet (2012)
Won ImageNet. Sparked the deep learning revolution.
VGG-16 (2014)
Simple architecture. 16 layers deep.
ResNet (2015)
Introduced skip connections. 152+ layers possible!
Build a CNN
from tensorflow.keras import layers, models
# Build CNN for image classification
model = models.Sequential([
# Convolution Block 1
layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
# Convolution Block 2
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
# Flatten and Dense layers
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])