Home Topics CNN

Convolutional Neural Networks

The backbone of computer vision

7 hours 15 examples Intermediate
Beginner

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

Intermediate

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

1
2
3
4
5
6
7
8
9

Filter (3x3)

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

Feature Map

→ Edges
Intermediate

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.

Advanced

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!

Advanced

Build a CNN

cnn_model.py
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'])

Next: RNN & LSTM

Learn how to process sequential data.